Skip to content

Commit ff2be58

Browse files
Merge pull request #310 from NNPDF/add-precommit-hooks
Introduce pre-commit hooks
2 parents f990dec + 0791e42 commit ff2be58

17 files changed

+231
-140
lines changed

.github/workflows/release.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,21 @@ jobs:
240240
# install APPLgrid
241241
curl -L "https://applgrid.hepforge.org/downloads?f=applgrid-${APPLGRID_V}.tgz" | tar xzf -
242242
cd applgrid-${APPLGRID_V}
243-
patch -p0 <<EOF
243+
patch -l -p0 <<EOF
244244
--- src/combine.cxx 2024-04-23 16:35:27.000000000 +0200
245245
+++ src/combine.cxx.new 2024-07-06 12:29:12.813303074 +0200
246246
@@ -56,12 +56,6 @@
247-
}
248-
249-
250-
-double integral( appl::TH1D* h ) {
247+
}
248+
249+
250+
-double integral( appl::TH1D* h ) {
251251
- double d = 0;
252252
- for ( int i=0 ; i<h->GetNbinsX() ; i++ ) d += h->GetBinContent(i+1);
253253
- return d;
254254
-}
255255
-
256-
257-
void print( appl::TH1D* h ) {
256+
257+
void print( appl::TH1D* h ) {
258258
for ( int i=1 ; i<=h->GetNbinsX() ; i++ ) std::cout << h->GetBinContent(i) << " ";
259259
EOF
260260
# compile static libraries with PIC to make statically linking PineAPPL's CLI work

.pre-commit-config.yaml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v4.6.0
6+
hooks:
7+
- id: trailing-whitespace
8+
# `.rs` files are taken care of by `cargo fmt`
9+
exclude: ^.*\.rs$
10+
- id: end-of-file-fixer
11+
- id: check-merge-conflict
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
# A fast Python linter and code formatter. See
14+
# https://docs.astral.sh/ruff/ for more details.
15+
rev: v0.6.5
16+
hooks:
17+
- id: ruff
18+
args: [--fix]
19+
exclude: ^pineappl_cli/src/plot.py
20+
- id: ruff-format
21+
args: []
22+
# TODO: remove this exclusion once we've merged this into master
23+
exclude: ^pineappl_cli/src/plot.py
24+
- repo: local
25+
hooks:
26+
- id: fmt
27+
name: cargo fmt
28+
description: Format Rust files with cargo fmt.
29+
entry: cargo fmt --
30+
language: system
31+
files: ^pineappl\S*\/.*\.rs$
32+
args: []
33+
- id: check
34+
name: cargo check
35+
description: Run cargo check.
36+
entry: bash -c 'cargo check --all-targets --features=evolve,fktable'
37+
language: system
38+
require_serial: true
39+
types: [rust]
40+
- id: clippy
41+
name: cargo clippy
42+
description: Check Rust files with cargo clippy.
43+
# Show only errors and ignore warnings
44+
entry: cargo clippy --all-targets --features=evolve,fktable -- -Awarnings
45+
pass_filenames: false
46+
types: [file, rust]
47+
language: system
48+
- repo: https://github.com/pre-commit/pre-commit
49+
rev: v3.8.0
50+
hooks:
51+
- id: validate_manifest

CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
## Rust
44

5-
- Before you commit, make sure that your code compiles with `cargo check` and
6-
that it has been formatted properly; `cargo fmt` does that for you. Also
7-
check if your changes introduce any new linter warnings by running `cargo
8-
clippy`
5+
- Before you commit, make sure that you have [pre-commit](https://pre-commit.com/)
6+
installed. This will ensure that the code is formatted correctly and that
7+
it compiles properly. Also, check if your changes introduce any new linter
8+
warnings by running `cargo clippy`.
99
- Make sure to keep `CHANGELOG.md` up-to-date.
1010
- Make sure not to use Rust features newer than the specified minimum supported
1111
Rust Version (MSRV), which is documented in the [README](README.md). You can

docs/maintainers-guide.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- `_config.yml`: configuration file for PineAPPL's Github-pages website
5151
- `install-capi.sh`: POSIX-compliant shell script to download and install
5252
PineAPPL's pre-built CAPI
53+
- `.pre-commit-config.yaml`: pre-commit hooks configuration
5354

5455
[cargo-xtask]: https://github.com/matklad/cargo-xtask
5556
[release page]: https://github.com/NNPDF/pineappl/releases

examples/cpp/advanced-filling.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,3 @@ int main() {
6666
// release memory
6767
pineappl_grid_delete(grid);
6868
}
69-

examples/cpp/merge-grids.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@ int main(int argc, char* argv[]) {
4141
pineappl_grid_delete(clone);
4242
pineappl_grid_delete(grid1);
4343
}
44-

examples/cpp/modify-grid.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,3 @@ int main(int argc, char* argv[]) {
8282
// release memory
8383
pineappl_grid_delete(grid);
8484
}
85-

examples/fortran/lhapdf_example.f90

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ program lhapdf_example
2626

2727
call lhapdf_initpdfset_byname(0, "nCTEQ15_1_1")
2828
call lhapdf_initpdfset_byname(1, "nCTEQ15FullNuc_208_82")
29-
29+
3030
! calling pineappl_grid_convolve without any flags
3131
xfx => xfx_test1
3232
alphas => alphas_test1
@@ -48,7 +48,7 @@ function xfx_test1(pdg_id, x, q2, state) bind(c)
4848
use iso_c_binding
4949

5050
implicit none
51-
51+
5252
integer(c_int32_t), value, intent(in) :: pdg_id
5353
real(c_double), value, intent(in) :: x, q2
5454
type(c_ptr), value, intent(in) :: state
@@ -61,7 +61,7 @@ function xfx_test2(pdg_id, x, q2, state) bind(c)
6161
use iso_c_binding
6262

6363
implicit none
64-
64+
6565
integer(c_int32_t), value, intent(in) :: pdg_id
6666
real(c_double), value, intent(in) :: x, q2
6767
type(c_ptr), value, intent(in) :: state
@@ -78,7 +78,7 @@ function alphas_test1(q2, state) bind(c)
7878
use iso_c_binding
7979

8080
implicit none
81-
81+
8282
real(c_double), value, intent(in) :: q2
8383
type(c_ptr), value, intent(in) :: state
8484
real(c_double) :: alphas_test1
@@ -90,7 +90,7 @@ function alphas_test2(q2, state) bind(c)
9090
use iso_c_binding
9191

9292
implicit none
93-
93+
9494
real(c_double), value, intent(in) :: q2
9595
type(c_ptr), value, intent(in) :: state
9696
real(c_double) :: alphas_test2

examples/fortran/pineappl.f90

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function pineappl_alphas(q2, state) bind(c)
3333
use iso_c_binding
3434

3535
implicit none
36-
36+
3737
real(c_double), value, intent(in) :: q2
3838
type (c_ptr), value, intent(in) :: state
3939
real(c_double) :: pineappl_alphas
@@ -353,15 +353,15 @@ subroutine string_delete(string) bind(c, name = 'pineappl_string_delete')
353353
! https://stackoverflow.com/a/20121335 and https://community.intel.com/t5/Intel-Fortran-Compiler/Converting-c-string-to-Fortran-string/m-p/959515
354354
function c_f_string(c_str) result(f_str)
355355
use :: iso_c_binding
356-
356+
357357
type(c_ptr), intent(in) :: c_str
358358
character(kind=c_char), dimension(:), pointer :: arr_f_ptr => null()
359359
character(len=:, kind=c_char), allocatable :: f_str
360360
integer(kind=c_size_t) :: i, length
361361

362362
length = strlen(c_str)
363363
call c_f_pointer(c_str, arr_f_ptr, [length])
364-
364+
365365
if (.not.associated(arr_f_ptr)) then
366366
f_str = "NULL"
367367
return
@@ -378,15 +378,15 @@ integer function pineappl_grid_bin_count(grid)
378378
implicit none
379379

380380
type (pineappl_grid), intent(in) :: grid
381-
381+
382382
pineappl_grid_bin_count = grid_bin_count(grid%ptr)
383383
end function
384384

385385
integer function pineappl_grid_bin_dimensions(grid)
386386
implicit none
387387

388388
type (pineappl_grid), intent(in) :: grid
389-
389+
390390
pineappl_grid_bin_dimensions = grid_bin_dimensions(grid%ptr)
391391
end function
392392

@@ -439,9 +439,9 @@ type (pineappl_grid) function pineappl_grid_clone(grid)
439439

440440
function pineappl_grid_convolve_with_one(grid, pdg_id, xfx, alphas, order_mask, lumi_mask, xi_ren, xi_fac, state) result(res)
441441
use iso_c_binding
442-
442+
443443
implicit none
444-
444+
445445
type (pineappl_grid), intent(in) :: grid
446446
integer, intent(in) :: pdg_id
447447
! no pointer attribute here, see https://community.intel.com/t5/Intel-Fortran-Compiler/Segfault-when-passing-procedure-pointer-to-function-but-not-when/m-p/939797
@@ -478,9 +478,9 @@ function pineappl_grid_convolve_with_one(grid, pdg_id, xfx, alphas, order_mask,
478478
function pineappl_grid_convolve_with_two(grid, pdg_id1, xfx1, pdg_id2, xfx2, alphas, &
479479
order_mask, lumi_mask, xi_ren, xi_fac, state) result(res)
480480
use iso_c_binding
481-
481+
482482
implicit none
483-
483+
484484
type (pineappl_grid), intent(in) :: grid
485485
integer, intent(in) :: pdg_id1, pdg_id2
486486
procedure (pineappl_xfx) :: xfx1, xfx2
@@ -570,7 +570,7 @@ function pineappl_grid_key_value(grid, key) result(res)
570570
type (pineappl_grid), intent(in) :: grid
571571
character (*), intent(in) :: key
572572
character (:), allocatable :: res
573-
573+
574574
res = c_f_string(grid_key_value(grid%ptr, key // c_null_char))
575575
end function
576576

examples/fortran/test.f90

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
program test_pineappl
22
use pineappl
33
use iso_c_binding
4-
4+
55
implicit none
66

77
integer, parameter :: dp = kind(0.0d0)
@@ -164,7 +164,7 @@ function xfx1_test(pdg_id, x, q2, state) bind(c)
164164
use iso_c_binding
165165

166166
implicit none
167-
167+
168168
integer(c_int32_t), value, intent(in) :: pdg_id
169169
real(c_double), value, intent(in) :: x, q2
170170
type(c_ptr), value, intent(in) :: state
@@ -177,7 +177,7 @@ function xfx2_test(pdg_id, x, q2, state) bind(c)
177177
use iso_c_binding
178178

179179
implicit none
180-
180+
181181
integer(c_int32_t), value, intent(in) :: pdg_id
182182
real(c_double), value, intent(in) :: x, q2
183183
type(c_ptr), value, intent(in) :: state
@@ -190,7 +190,7 @@ function alphas_test(q2, state) bind(c)
190190
use iso_c_binding
191191

192192
implicit none
193-
193+
194194
real(c_double), value, intent(in) :: q2
195195
type(c_ptr), value, intent(in) :: state
196196
real(c_double) :: alphas_test

examples/python/positivity.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ def main(filename, Q2):
3838
)
3939
grid.set_subgrid(0, bin_, 0, subgrid.into())
4040
# set the correct observables
41-
normalizations = np.array(
42-
[1.0] * bins
43-
) # `normalizations` has to be `np.ndarray`
41+
normalizations = np.array([1.0] * bins) # `normalizations` has to be `np.ndarray`
4442
remapper = pineappl.bin.BinRemapper(normalizations, limits)
4543
grid.set_remapper(remapper)
4644

pineappl_cli/src/subgrid-pull-plot.py

+30-28
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,60 @@
55
from math import fabs, log10
66
from scipy.interpolate import griddata
77

8-
x1 = np.array([{x1}])
9-
x2 = np.array([{x2}])
10-
z = np.array([{z}])
8+
x1 = np.array([{x1}]) # noqa: F821
9+
x2 = np.array([{x2}]) # noqa: F821
10+
z = np.array([{z}]) # noqa: F821
1111
x = 0.5 * np.log(x1 / x2)
1212
y = np.sqrt(x1 * x2)
1313

1414
nrap = 50
1515
nmass = 50
1616

1717
sym_min = -max(fabs(np.min(x)), fabs(np.max(x)))
18-
sym_max = max(fabs(np.min(x)), fabs(np.max(x)))
18+
sym_max = max(fabs(np.min(x)), fabs(np.max(x)))
1919

2020
xi = np.linspace(sym_min, sym_max, (nrap // 2) * 2 + 1)
2121
yi = np.logspace(log10(np.min(y)), log10(np.max(y)), nmass)
22-
zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='linear', rescale=True)
22+
zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method="linear", rescale=True)
2323

24-
#print(xi.shape)
25-
#print(yi.shape)
26-
#print(zi.shape)
24+
# print(xi.shape)
25+
# print(yi.shape)
26+
# print(zi.shape)
2727

2828
# mask impossible kinematic values
2929
for iy, ix in np.ndindex(zi.shape):
30-
#print(ix, iy)
30+
# print(ix, iy)
3131
x1v = yi[iy] * np.exp(xi[ix])
3232
x2v = yi[iy] / np.exp(xi[ix])
3333

34-
#print('y = {{}} m/s = {{}} -> x1 = {{}} x2 = {{}}'.format(xi[ix], yi[iy], x1v, x2v))
34+
# print('y = {{}} m/s = {{}} -> x1 = {{}} x2 = {{}}'.format(xi[ix], yi[iy], x1v, x2v))
3535

3636
if x1v > 1.0 or x2v > 1.0:
3737
zi[iy, ix] = np.nan
3838

3939
figure, axes = plt.subplots(1, 2, constrained_layout=True)
4040
figure.set_size_inches(10, 5)
4141

42-
mesh = axes[0].pcolormesh(xi, yi, zi, shading='nearest', linewidth=0, snap=True)
43-
axes[0].scatter(x, y, marker='*', s=5)
44-
axes[0].set_yscale('log')
45-
axes[0].set_xlabel(r'$y = 1/2 \log (x_1/x_2)$')
46-
axes[0].set_ylabel(r'$M/\sqrt{{s}} = \sqrt{{x_1 x_2}}$')
47-
#axes[0].set_aspect('equal', share=True)
42+
mesh = axes[0].pcolormesh(xi, yi, zi, shading="nearest", linewidth=0, snap=True)
43+
axes[0].scatter(x, y, marker="*", s=5)
44+
axes[0].set_yscale("log")
45+
axes[0].set_xlabel(r"$y = 1/2 \log (x_1/x_2)$")
46+
axes[0].set_ylabel(r"$M/\sqrt{{s}} = \sqrt{{x_1 x_2}}$")
47+
# axes[0].set_aspect('equal', share=True)
4848

4949
x1i = np.logspace(log10(np.min(x1)), log10(np.max(x1)), 50)
5050
x2i = np.logspace(log10(np.min(x2)), log10(np.max(x2)), 50)
51-
z12i = griddata((x1, x2), z, (x1i[None, :], x2i[:, None]), method='linear', rescale=True)
52-
53-
mesh = axes[1].pcolormesh(x1i, x2i, z12i, shading='nearest', linewidth=0, snap=True)
54-
axes[1].set_xscale('log')
55-
axes[1].set_yscale('log')
56-
axes[1].scatter(x1, x2, marker='*', s=5)
57-
axes[1].set_aspect('equal', share=True)
58-
axes[1].set_xlabel(r'$x_1$')
59-
axes[1].set_ylabel(r'$x_2$')
60-
61-
figure.colorbar(mesh, ax=axes, extend='min')
62-
figure.savefig('plot.pdf')
51+
z12i = griddata(
52+
(x1, x2), z, (x1i[None, :], x2i[:, None]), method="linear", rescale=True
53+
)
54+
55+
mesh = axes[1].pcolormesh(x1i, x2i, z12i, shading="nearest", linewidth=0, snap=True)
56+
axes[1].set_xscale("log")
57+
axes[1].set_yscale("log")
58+
axes[1].scatter(x1, x2, marker="*", s=5)
59+
axes[1].set_aspect("equal", share=True)
60+
axes[1].set_xlabel(r"$x_1$")
61+
axes[1].set_ylabel(r"$x_2$")
62+
63+
figure.colorbar(mesh, ax=axes, extend="min")
64+
figure.savefig("plot.pdf")

0 commit comments

Comments
 (0)