1
1
import numpy as np
2
2
import pytest
3
+
4
+ from dataclasses import dataclass
5
+ from typing import List , Tuple
6
+
3
7
from pineappl .boc import Channel , Order
4
8
from pineappl .convolutions import Conv , ConvType
5
9
from pineappl .grid import Grid
14
18
CONVOBJECT = Conv (conv_type = TYPECONV , pid = 2212 )
15
19
16
20
21
+ @dataclass
22
+ class OperatorInfo :
23
+ x_grids : List [np .ndarray ]
24
+ scale : List [float ]
25
+ array : np .ndarray
26
+
27
+
17
28
def test_issue_164 (pdf , fake_grids ):
18
29
# https://github.com/NNPDF/pineappl/issues/164
19
30
# DIS-like convolution now ONLY requires one entry of `PID`
20
31
channels = [Channel ([([2 ], 1.0 )])] # DIS-case
21
32
orders = [Order (0 , 0 , 0 , 0 , 0 )]
22
33
23
- def convolve_grid (q2_min : float = Q2_MIN ) -> Grid :
34
+ def convolve_grid (q2_min : float = Q2_MIN ) -> np . ndarray :
24
35
grid = fake_grids .grid_with_generic_convolution (
25
36
nb_convolutions = 1 ,
26
37
orders = orders ,
@@ -52,7 +63,7 @@ def convolve_grid(q2_min: float = Q2_MIN) -> Grid:
52
63
53
64
54
65
class TestSubgrid :
55
- def fake_grid (self , fake_grids ):
66
+ def fake_grid (self , fake_grids ) -> Grid :
56
67
channels = [Channel ([([2 ], 1.0 )]), Channel ([([3 ], 0.5 )])]
57
68
orders = [Order (0 , 0 , 0 , 0 , 0 )]
58
69
return fake_grids .grid_with_generic_convolution (
@@ -62,19 +73,19 @@ def fake_grid(self, fake_grids):
62
73
convolutions = [CONVOBJECT ],
63
74
)
64
75
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 )]
67
78
xgrid_size = [x .size for x in x_grids ]
68
79
Q2s = np .linspace (10 , 20 , 2 )
69
80
scale = [q2 for q2 in Q2s ] # One single scale Q2
70
81
array = np .random .rand (len (Q2s ), * xgrid_size )
82
+ infos = OperatorInfo (x_grids , scale , array )
71
83
subgrid = ImportSubgridV1 (array = array , node_values = [scale , * x_grids ])
72
- return subgrid , [ * x_grids , scale , array ]
84
+ return subgrid , infos
73
85
74
86
def test_subgrid_methods (self , fake_grids ):
75
87
grid = self .fake_grid (fake_grids )
76
88
test_subgrid , infos = self .fake_importonlysubgrid ()
77
- x1s , x2s , mu2s , _ = (obj for obj in infos )
78
89
grid .set_subgrid (0 , 0 , 0 , test_subgrid .into ())
79
90
extr_subgrid = grid .subgrid (0 , 0 , 0 )
80
91
assert isinstance (extr_subgrid , SubgridEnum )
@@ -83,22 +94,27 @@ def test_subgrid_methods(self, fake_grids):
83
94
extr_subgrid .scale (factor = 100 )
84
95
assert isinstance (extr_subgrid .into (), SubgridEnum )
85
96
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 ):
88
99
"""This simply checks that the commands run without raising any
89
100
errors and that the objects have been succesfully instantiated.
90
101
"""
91
- subgrid , info = self .fake_importonlysubgrid (nb_dim = nb_dim )
102
+ subgrid , info = self .fake_importonlysubgrid (nb_xdim = nb_xdim )
92
103
assert isinstance (subgrid , ImportSubgridV1 )
93
104
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 ):
98
106
grid = self .fake_grid (fake_grids )
99
107
test_subgrid , infos = self .fake_importonlysubgrid ()
100
- _ , _ , _ , array = (obj for obj in infos )
101
108
grid .set_subgrid (0 , 0 , 0 , test_subgrid .into ())
102
109
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