Skip to content

Commit b10dc84

Browse files
Increase Python coverage and add cosmetic changes
1 parent 230f648 commit b10dc84

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

pineappl_py/tests/test_subgrid.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import numpy as np
22
import pytest
3+
4+
from dataclasses import dataclass
5+
from typing import List, Tuple
6+
37
from pineappl.boc import Channel, Order
48
from pineappl.convolutions import Conv, ConvType
59
from pineappl.grid import Grid
@@ -14,13 +18,20 @@
1418
CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212)
1519

1620

21+
@dataclass
22+
class OperatorInfo:
23+
x_grids: List[np.ndarray]
24+
scale: List[float]
25+
array: np.ndarray
26+
27+
1728
def test_issue_164(pdf, fake_grids):
1829
# https://github.com/NNPDF/pineappl/issues/164
1930
# DIS-like convolution now ONLY requires one entry of `PID`
2031
channels = [Channel([([2], 1.0)])] # DIS-case
2132
orders = [Order(0, 0, 0, 0, 0)]
2233

23-
def convolve_grid(q2_min: float = Q2_MIN) -> Grid:
34+
def convolve_grid(q2_min: float = Q2_MIN) -> np.ndarray:
2435
grid = fake_grids.grid_with_generic_convolution(
2536
nb_convolutions=1,
2637
orders=orders,
@@ -52,7 +63,7 @@ def convolve_grid(q2_min: float = Q2_MIN) -> Grid:
5263

5364

5465
class TestSubgrid:
55-
def fake_grid(self, fake_grids):
66+
def fake_grid(self, fake_grids) -> Grid:
5667
channels = [Channel([([2], 1.0)]), Channel([([3], 0.5)])]
5768
orders = [Order(0, 0, 0, 0, 0)]
5869
return fake_grids.grid_with_generic_convolution(
@@ -62,19 +73,19 @@ def fake_grid(self, fake_grids):
6273
convolutions=[CONVOBJECT],
6374
)
6475

65-
def fake_importonlysubgrid(self, nb_dim: int = 2) -> tuple:
66-
x_grids = [np.linspace(0.1, 1, 2) for _ in range(nb_dim)]
76+
def fake_importonlysubgrid(self, nb_xdim: int = 1) -> Tuple[ImportSubgridV1, OperatorInfo]:
77+
x_grids = [np.linspace(0.1, 1, 2) for _ in range(nb_xdim)]
6778
xgrid_size = [x.size for x in x_grids]
6879
Q2s = np.linspace(10, 20, 2)
6980
scale = [q2 for q2 in Q2s] # One single scale Q2
7081
array = np.random.rand(len(Q2s), *xgrid_size)
82+
infos = OperatorInfo(x_grids, scale, array)
7183
subgrid = ImportSubgridV1(array=array, node_values=[scale, *x_grids])
72-
return subgrid, [*x_grids, scale, array]
84+
return subgrid, infos
7385

7486
def test_subgrid_methods(self, fake_grids):
7587
grid = self.fake_grid(fake_grids)
7688
test_subgrid, infos = self.fake_importonlysubgrid()
77-
x1s, x2s, mu2s, _ = (obj for obj in infos)
7889
grid.set_subgrid(0, 0, 0, test_subgrid.into())
7990
extr_subgrid = grid.subgrid(0, 0, 0)
8091
assert isinstance(extr_subgrid, SubgridEnum)
@@ -83,22 +94,27 @@ def test_subgrid_methods(self, fake_grids):
8394
extr_subgrid.scale(factor=100)
8495
assert isinstance(extr_subgrid.into(), SubgridEnum)
8596

86-
@pytest.mark.parametrize("nb_dim", [1, 2, 3, 4])
87-
def test_subgrid_arrays(self, nb_dim):
97+
@pytest.mark.parametrize("nb_xdim", [1, 2, 3, 4])
98+
def test_subgrid_arrays(self, nb_xdim: int):
8899
"""This simply checks that the commands run without raising any
89100
errors and that the objects have been succesfully instantiated.
90101
"""
91-
subgrid, info = self.fake_importonlysubgrid(nb_dim=nb_dim)
102+
subgrid, info = self.fake_importonlysubgrid(nb_xdim=nb_xdim)
92103
assert isinstance(subgrid, ImportSubgridV1)
93104

94-
@pytest.mark.skip(reason="No implementation of Array3 for subgrid.")
95-
def test_to_array3(self, fake_grids):
96-
# TODO: extract and check the dense array of the subgrid
97-
# requires `impl From<&SubgridEnum> for Array3<f64>`
105+
def test_to_array(self, fake_grids):
98106
grid = self.fake_grid(fake_grids)
99107
test_subgrid, infos = self.fake_importonlysubgrid()
100-
_, _, _, array = (obj for obj in infos)
101108
grid.set_subgrid(0, 0, 0, test_subgrid.into())
102109
extr_subgrid = grid.subgrid(0, 0, 0)
103-
test_array = extr_subgrid.to_array3()
104-
np.testing.assert_allclose(test_array, array)
110+
111+
# Check that the shape of the subgrid matches specs
112+
extr_subgrid_shape = extr_subgrid.shape
113+
assert tuple(extr_subgrid_shape) == infos.array.shape
114+
115+
# Check that the `node_values` correspond with the Kinematics
116+
node_values = extr_subgrid.node_values
117+
np.testing.assert_allclose(node_values, [infos.scale, *infos.x_grids])
118+
119+
test_array = extr_subgrid.to_array(shape=extr_subgrid_shape)
120+
np.testing.assert_allclose(test_array, infos.array)

0 commit comments

Comments
 (0)