Skip to content

Commit

Permalink
Almost fully completes Python interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Radonirinaunimi committed Oct 26, 2024
1 parent a4b44ed commit 489edbd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pineappl_py/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::fk_table::PyFkTable;
use super::interpolation::PyInterp;
use super::pids::PyPidBasis;
use super::subgrid::PySubgridEnum;
use itertools::izip;
use ndarray::CowArray;
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray4};
use pineappl::convolutions::ConvolutionCache;
Expand Down Expand Up @@ -121,6 +122,55 @@ impl PyGrid {
}
}

/// Add an array to the grid.
///
/// Useful to avoid multiple python calls, leading to performance improvement.
///
/// Parameters
/// ----------
/// order : int
/// order index
/// observables : list(float)
/// list of reference point (to be binned)
/// channel : int
/// channel index
/// ntuples: list(list(float))
/// list of `ntuple` kinematics
/// weights : np.array(float)
/// cross section weight for all events
pub fn fill_array(
&mut self,
order: usize,
observables: Vec<f64>,
channel: usize,
ntuples: Vec<Vec<f64>>,
weights: Vec<f64>,
) {
for (ntuple, &observable, &weight) in
izip!(ntuples.iter(), observables.iter(), weights.iter())
{
self.grid.fill(order, observable, channel, ntuple, weight);
}
}

/// Add a point to the grid for all channels.
///
/// Parameters
/// ----------
/// order : int
/// order index
/// observable : float
/// reference point (to be binned)
/// ntuple: list(float)
/// list containing information on kinematics
/// weights : np.array(float)
/// cross section weights, one for each channels
pub fn fill_all(&mut self, order: usize, observable: f64, ntuple: Vec<f64>, weights: Vec<f64>) {
for (channel, &weight) in weights.iter().enumerate() {
self.grid.fill(order, observable, channel, &ntuple, weight);
}
}

/// Set a subgrid.
///
/// Parameters
Expand Down
43 changes: 43 additions & 0 deletions pineappl_py/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,49 @@ def test_fill(self):
)
pytest.approx(res) == 0.0

def test_fill_array(self):
g = self.fake_grid()
g.fill_array(
order=0,
observables=np.array([1e-3, 1e-2]),
channel=0,
ntuples=[
np.array([0.5, 1.0, 1.0]),
np.array([0.5, 1.0, 1.0]),
np.array([0.5, 1.0, 1.0]),
],
weights=np.array([10.0, 100.0]),
)

# Convolution of two symmetrical hadrons
h = ConvType(polarized=False, time_like=False)
h_conv = Conv(conv_type=h, pid=2212)
res = g.convolve(
pdg_convs=[h_conv, h_conv],
xfxs=[lambda pid, x, q2: x, lambda pid, x, q2: x],
alphas=lambda q2: 1.0,
)
pytest.approx(res) == 0.0

def test_fill_all(self):
g = self.fake_grid()
g.fill_all(
order=0,
observable=1e-2,
ntuple=[1.0, 1.0, 1.0],
weights=np.array([10.0]),
)

# Convolution of two symmetrical hadrons
h = ConvType(polarized=False, time_like=False)
h_conv = Conv(conv_type=h, pid=2212)
res = g.convolve(
pdg_convs=[h_conv, h_conv],
xfxs=[lambda pid, x, q2: x, lambda pid, x, q2: x],
alphas=lambda q2: 1.0,
)
pytest.approx(res) == 0.0

def test_merge(self):
g = self.fake_grid(bins=[1, 2, 3])
g1 = self.fake_grid(bins=[3, 4, 5])
Expand Down

0 comments on commit 489edbd

Please sign in to comment.