From 8a1e9295396b4b3f88b8d880f3eb863712a1e1af Mon Sep 17 00:00:00 2001 From: Felix Hekhorn Date: Mon, 2 Sep 2024 16:06:49 +0300 Subject: [PATCH 1/6] Adjust PyGrid::convolve_* --- pineappl_py/src/grid.rs | 24 ++++++++++----------- pineappl_py/tests/test_grid.py | 39 ++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 6eda73c1..f6bdc48a 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -267,9 +267,9 @@ impl PyGrid { pdg_id: i32, xfx: &Bound<'py, PyAny>, alphas: &Bound<'py, PyAny>, - order_mask: Option>, - bin_indices: Option>, - channel_mask: Option>, + order_mask: Option>, + bin_indices: Option>, + channel_mask: Option>, xi: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { @@ -280,9 +280,9 @@ impl PyGrid { self.grid .convolve( &mut lumi_cache, - &order_mask.map_or(vec![], |b| b.to_vec().unwrap()), - &bin_indices.map_or(vec![], |c| c.to_vec().unwrap()), - &channel_mask.map_or(vec![], |d| d.to_vec().unwrap()), + &order_mask.map_or(vec![], |b| b.to_vec()), + &bin_indices.map_or(vec![], |c| c.to_vec()), + &channel_mask.map_or(vec![], |d| d.to_vec()), &xi.map_or(vec![(1.0, 1.0)], |m| m), ) .into_pyarray_bound(py) @@ -332,9 +332,9 @@ impl PyGrid { pdg_id2: i32, xfx2: &Bound<'py, PyAny>, alphas: &Bound<'py, PyAny>, - order_mask: Option>, - bin_indices: Option>, - channel_mask: Option>, + order_mask: Option>, + bin_indices: Option>, + channel_mask: Option>, xi: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { @@ -347,9 +347,9 @@ impl PyGrid { self.grid .convolve( &mut lumi_cache, - &order_mask.map_or(vec![], |b| b.to_vec().unwrap()), - &bin_indices.map_or(vec![], |c| c.to_vec().unwrap()), - &channel_mask.map_or(vec![], |d| d.to_vec().unwrap()), + &order_mask.map_or(vec![], |b| b.to_vec()), + &bin_indices.map_or(vec![], |c| c.to_vec()), + &channel_mask.map_or(vec![], |d| d.to_vec()), &xi.map_or(vec![(1.0, 1.0)], |m| m), ) .into_pyarray_bound(py) diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index f0678db9..fbc4d17d 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -96,13 +96,48 @@ def test_convolve_with_one(self): g.convolve_with_one(2212, lambda pid, x, q2: 0.0, lambda q2: 0.0), [0.0] * 2, ) + v = 5e6 / 9999 np.testing.assert_allclose( g.convolve_with_one(2212, lambda pid, x, q2: 1, lambda q2: 1.0), - [5e6 / 9999, 0.0], + [v, 0.0], + ) + np.testing.assert_allclose( + g.convolve_with_one( + 2212, lambda pid, x, q2: 1, lambda q2: 1.0, bin_indices=[0] + ), + [v], + ) + # block first bins with additional args + np.testing.assert_allclose( + g.convolve_with_one( + 2212, + lambda pid, x, q2: 1, + lambda q2: 1.0, + bin_indices=[0], + order_mask=[False], + ), + [0.0], + ) + np.testing.assert_allclose( + g.convolve_with_one( + 2212, + lambda pid, x, q2: 1, + lambda q2: 1.0, + bin_indices=[0], + channel_mask=[False], + ), + [0.0], + ) + # second bin is empty + np.testing.assert_allclose( + g.convolve_with_one( + 2212, lambda pid, x, q2: 1, lambda q2: 1.0, bin_indices=[1] + ), + [0.0], ) np.testing.assert_allclose( g.convolve_with_one(2212, lambda pid, x, q2: 1, lambda q2: 2.0), - [2**3 * 5e6 / 9999, 0.0], + [2**3 * v, 0.0], ) def test_io(self, tmp_path): From 72af22efb71e1fa872a96c4c3fb3b41cf0a23d65 Mon Sep 17 00:00:00 2001 From: Felix Hekhorn Date: Mon, 2 Sep 2024 16:45:27 +0300 Subject: [PATCH 2/6] Fix typo in doc --- pineappl_py/src/grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index f6bdc48a..b638909c 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -365,7 +365,7 @@ impl PyGrid { /// Returns /// ------- /// PyEvolveInfo : - /// evolution informations + /// evolution information pub fn evolve_info(&self, order_mask: PyReadonlyArray1) -> PyEvolveInfo { PyEvolveInfo { evolve_info: self.grid.evolve_info(order_mask.as_slice().unwrap()), From 5117ea1e17f84ef2729326bc9cd4b154aca9455a Mon Sep 17 00:00:00 2001 From: Felix Hekhorn Date: Mon, 2 Sep 2024 16:46:22 +0300 Subject: [PATCH 3/6] Swap map_or for unwrap_or --- pineappl_py/src/grid.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index b638909c..42e1c42e 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -280,10 +280,10 @@ impl PyGrid { self.grid .convolve( &mut lumi_cache, - &order_mask.map_or(vec![], |b| b.to_vec()), - &bin_indices.map_or(vec![], |c| c.to_vec()), - &channel_mask.map_or(vec![], |d| d.to_vec()), - &xi.map_or(vec![(1.0, 1.0)], |m| m), + &order_mask.unwrap_or(vec![]), + &bin_indices.unwrap_or(vec![]), + &channel_mask.unwrap_or(vec![]), + &xi.unwrap_or(vec![(1.0, 1.0)]), ) .into_pyarray_bound(py) } @@ -347,10 +347,10 @@ impl PyGrid { self.grid .convolve( &mut lumi_cache, - &order_mask.map_or(vec![], |b| b.to_vec()), - &bin_indices.map_or(vec![], |c| c.to_vec()), - &channel_mask.map_or(vec![], |d| d.to_vec()), - &xi.map_or(vec![(1.0, 1.0)], |m| m), + &order_mask.unwrap_or(vec![]), + &bin_indices.unwrap_or(vec![]), + &channel_mask.unwrap_or(vec![]), + &xi.unwrap_or(vec![(1.0, 1.0)]), ) .into_pyarray_bound(py) } From 9a163bb4371df3187965b9b0d2b3f2147906705d Mon Sep 17 00:00:00 2001 From: Felix Hekhorn Date: Tue, 3 Sep 2024 10:12:43 +0300 Subject: [PATCH 4/6] Swap unwrap_or for unwrap_or_default --- pineappl_py/src/grid.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 42e1c42e..bed3825f 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -280,9 +280,9 @@ impl PyGrid { self.grid .convolve( &mut lumi_cache, - &order_mask.unwrap_or(vec![]), - &bin_indices.unwrap_or(vec![]), - &channel_mask.unwrap_or(vec![]), + &order_mask.unwrap_or_default(), + &bin_indices.unwrap_or_default(), + &channel_mask.unwrap_or_default(), &xi.unwrap_or(vec![(1.0, 1.0)]), ) .into_pyarray_bound(py) @@ -347,9 +347,9 @@ impl PyGrid { self.grid .convolve( &mut lumi_cache, - &order_mask.unwrap_or(vec![]), - &bin_indices.unwrap_or(vec![]), - &channel_mask.unwrap_or(vec![]), + &order_mask.unwrap_or_default(), + &bin_indices.unwrap_or_default(), + &channel_mask.unwrap_or_default(), &xi.unwrap_or(vec![(1.0, 1.0)]), ) .into_pyarray_bound(py) From 9c7ed1cd69dda7753d90721ace4a24d4a5228135 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Wed, 25 Sep 2024 17:23:58 +0200 Subject: [PATCH 5/6] replace all instances of PyReadonlyArray1 -> Vec --- pineappl_py/src/bin.rs | 5 ++- pineappl_py/src/fk_table.rs | 18 +++++----- pineappl_py/src/grid.rs | 50 +++++++++++++------------- pineappl_py/src/import_only_subgrid.rs | 34 +++++++----------- 4 files changed, 49 insertions(+), 58 deletions(-) diff --git a/pineappl_py/src/bin.rs b/pineappl_py/src/bin.rs index 17398e17..f4447b36 100644 --- a/pineappl_py/src/bin.rs +++ b/pineappl_py/src/bin.rs @@ -1,6 +1,5 @@ //! Binnning interface. -use numpy::{PyArrayMethods, PyReadonlyArray1}; use pineappl::bin::BinRemapper; use pyo3::prelude::*; @@ -23,9 +22,9 @@ impl PyBinRemapper { /// limits : list(tuple(float, float)) /// bin limits #[new] - pub fn new(normalizations: PyReadonlyArray1, limits: Vec<(f64, f64)>) -> Self { + pub fn new(normalizations: Vec, limits: Vec<(f64, f64)>) -> Self { Self { - bin_remapper: BinRemapper::new(normalizations.to_vec().unwrap(), limits).unwrap(), + bin_remapper: BinRemapper::new(normalizations, limits).unwrap(), } } } diff --git a/pineappl_py/src/fk_table.rs b/pineappl_py/src/fk_table.rs index cafedc2d..7061186a 100644 --- a/pineappl_py/src/fk_table.rs +++ b/pineappl_py/src/fk_table.rs @@ -1,7 +1,7 @@ //! FK table interface. use super::grid::PyGrid; -use numpy::{IntoPyArray, PyArray1, PyArray4, PyArrayMethods, PyReadonlyArray1}; +use numpy::{IntoPyArray, PyArray1, PyArray4}; use pineappl::convolutions::LumiCache; use pineappl::fk_table::{FkAssumptions, FkTable}; use pineappl::grid::Grid; @@ -238,8 +238,8 @@ impl PyFkTable { &self, pdg_id: i32, xfx: &Bound<'py, PyAny>, - bin_indices: Option>, - channel_mask: Option>, + bin_indices: Option>, + channel_mask: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { let mut xfx = |id, x, q2| xfx.call1((id, x, q2)).unwrap().extract().unwrap(); @@ -248,8 +248,8 @@ impl PyFkTable { self.fk_table .convolve( &mut lumi_cache, - &bin_indices.map_or(vec![], |b| b.to_vec().unwrap()), - &channel_mask.map_or(vec![], |l| l.to_vec().unwrap()), + &bin_indices.unwrap_or_default(), + &channel_mask.unwrap_or_default(), ) .into_pyarray_bound(py) } @@ -278,8 +278,8 @@ impl PyFkTable { xfx1: &Bound<'py, PyAny>, pdg_id2: i32, xfx2: &Bound<'py, PyAny>, - bin_indices: Option>, - channel_mask: Option>, + bin_indices: Option>, + channel_mask: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { let mut xfx1 = |id, x, q2| xfx1.call1((id, x, q2)).unwrap().extract().unwrap(); @@ -290,8 +290,8 @@ impl PyFkTable { self.fk_table .convolve( &mut lumi_cache, - &bin_indices.map_or(vec![], |b| b.to_vec().unwrap()), - &channel_mask.map_or(vec![], |l| l.to_vec().unwrap()), + &bin_indices.unwrap_or_default(), + &channel_mask.unwrap_or_default(), ) .into_pyarray_bound(py) } diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index bed3825f..86df8e33 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -7,7 +7,7 @@ use super::fk_table::PyFkTable; use super::subgrid::{PySubgridEnum, PySubgridParams}; use itertools::izip; use ndarray::CowArray; -use numpy::{IntoPyArray, PyArray1, PyArrayMethods, PyReadonlyArray1, PyReadonlyArray4}; +use numpy::{IntoPyArray, PyArray1, PyReadonlyArray4}; use pineappl::convolutions::LumiCache; use pineappl::evolution::AlphasTable; use pineappl::grid::{Grid, Ntuple}; @@ -45,14 +45,14 @@ impl PyGrid { pub fn new_grid( channels: Vec>, orders: Vec>, - bin_limits: PyReadonlyArray1, + bin_limits: Vec, subgrid_params: PySubgridParams, ) -> Self { Self { grid: Grid::new( channels.iter().map(|pyc| pyc.entry.clone()).collect(), orders.iter().map(|pyo| pyo.order.clone()).collect(), - bin_limits.to_vec().unwrap(), + bin_limits, subgrid_params.subgrid_params, ), } @@ -116,20 +116,20 @@ impl PyGrid { /// cross section weight for all events pub fn fill_array( &mut self, - x1s: PyReadonlyArray1, - x2s: PyReadonlyArray1, - q2s: PyReadonlyArray1, + x1s: Vec, + x2s: Vec, + q2s: Vec, order: usize, - observables: PyReadonlyArray1, + observables: Vec, channel: usize, - weights: PyReadonlyArray1, + weights: Vec, ) { for (&x1, &x2, &q2, &observable, &weight) in izip!( - x1s.as_array().iter(), - x2s.as_array().iter(), - q2s.as_array().iter(), - observables.as_array().iter(), - weights.as_array().iter(), + x1s.iter(), + x2s.iter(), + q2s.iter(), + observables.iter(), + weights.iter(), ) { self.grid.fill( order, @@ -163,7 +163,7 @@ impl PyGrid { q2: f64, order: usize, observable: f64, - weights: PyReadonlyArray1, + weights: Vec, ) { self.grid.fill_all( order, @@ -174,7 +174,7 @@ impl PyGrid { q2, weight: (), }, - &weights.to_vec().unwrap(), + &weights, ); } @@ -366,9 +366,9 @@ impl PyGrid { /// ------- /// PyEvolveInfo : /// evolution information - pub fn evolve_info(&self, order_mask: PyReadonlyArray1) -> PyEvolveInfo { + pub fn evolve_info(&self, order_mask: Vec) -> PyEvolveInfo { PyEvolveInfo { - evolve_info: self.grid.evolve_info(order_mask.as_slice().unwrap()), + evolve_info: self.grid.evolve_info(order_mask.as_slice()), } } @@ -394,7 +394,7 @@ impl PyGrid { pub fn evolve_with_slice_iter<'py>( &self, slices: &Bound<'py, PyIterator>, - order_mask: PyReadonlyArray1, + order_mask: Vec, xi: (f64, f64), ren1: Vec, alphas: Vec, @@ -414,7 +414,7 @@ impl PyGrid { )) }), // TODO: make `order_mask` a `Vec` - &order_mask.to_vec().unwrap(), + &order_mask, xi, &AlphasTable { ren1, alphas }, ) @@ -448,7 +448,7 @@ impl PyGrid { &self, slices_a: &Bound<'py, PyIterator>, slices_b: &Bound<'py, PyIterator>, - order_mask: PyReadonlyArray1, + order_mask: Vec, xi: (f64, f64), ren1: Vec, alphas: Vec, @@ -479,7 +479,7 @@ impl PyGrid { )) }), // TODO: make `order_mask` a `Vec` - &order_mask.to_vec().unwrap(), + &order_mask, xi, &AlphasTable { ren1, alphas }, ) @@ -648,8 +648,8 @@ impl PyGrid { /// ---------- /// factors : numpy.ndarray[float] /// bin-dependent factors by which to scale - pub fn scale_by_bin(&mut self, factors: PyReadonlyArray1) { - self.grid.scale_by_bin(&factors.to_vec().unwrap()); + pub fn scale_by_bin(&mut self, factors: Vec) { + self.grid.scale_by_bin(&factors); } /// Delete bins. @@ -660,8 +660,8 @@ impl PyGrid { /// ---------- /// bin_indices : numpy.ndarray[int] /// list of indices of bins to removed - pub fn delete_bins(&mut self, bin_indices: PyReadonlyArray1) { - self.grid.delete_bins(&bin_indices.to_vec().unwrap()) + pub fn delete_bins(&mut self, bin_indices: Vec) { + self.grid.delete_bins(&bin_indices) } } diff --git a/pineappl_py/src/import_only_subgrid.rs b/pineappl_py/src/import_only_subgrid.rs index b58a006c..3d12f58c 100644 --- a/pineappl_py/src/import_only_subgrid.rs +++ b/pineappl_py/src/import_only_subgrid.rs @@ -1,7 +1,7 @@ //! PyImportOnlySubgrid* interface. use super::subgrid::PySubgridEnum; -use numpy::{PyArrayMethods, PyReadonlyArray1, PyReadonlyArray3}; +use numpy::PyReadonlyArray3; use pineappl::import_only_subgrid::ImportOnlySubgridV1; use pineappl::import_only_subgrid::ImportOnlySubgridV2; use pineappl::sparse_array3::SparseArray3; @@ -34,14 +34,10 @@ impl PyImportOnlySubgridV2 { pub fn new( array: PyReadonlyArray3, mu2_grid: Vec<(f64, f64)>, - x1_grid: PyReadonlyArray1, - x2_grid: PyReadonlyArray1, + x1_grid: Vec, + x2_grid: Vec, ) -> Self { - let mut sparse_array = SparseArray3::new( - mu2_grid.len(), - x1_grid.as_slice().unwrap().len(), - x2_grid.as_slice().unwrap().len(), - ); + let mut sparse_array = SparseArray3::new(mu2_grid.len(), x1_grid.len(), x2_grid.len()); for ((imu2, ix1, ix2), value) in array .as_array() @@ -60,8 +56,8 @@ impl PyImportOnlySubgridV2 { fac: *fac, }) .collect(), - x1_grid.to_vec().unwrap(), - x2_grid.to_vec().unwrap(), + x1_grid, + x2_grid, ), } } @@ -112,15 +108,11 @@ impl PyImportOnlySubgridV1 { #[new] pub fn new_import_only_subgrid( array: PyReadonlyArray3, - q2_grid: PyReadonlyArray1, - x1_grid: PyReadonlyArray1, - x2_grid: PyReadonlyArray1, + q2_grid: Vec, + x1_grid: Vec, + x2_grid: Vec, ) -> Self { - let mut sparse_array = SparseArray3::new( - q2_grid.as_slice().unwrap().len(), - x1_grid.as_slice().unwrap().len(), - x2_grid.as_slice().unwrap().len(), - ); + let mut sparse_array = SparseArray3::new(q2_grid.len(), x1_grid.len(), x2_grid.len()); for ((iq2, ix1, ix2), value) in array .as_array() @@ -132,9 +124,9 @@ impl PyImportOnlySubgridV1 { Self::new(ImportOnlySubgridV1::new( sparse_array, - q2_grid.to_vec().unwrap(), - x1_grid.to_vec().unwrap(), - x2_grid.to_vec().unwrap(), + q2_grid, + x1_grid, + x2_grid, )) } From 1b5c6e5abeda19e235a9ce5b04dfbfd4e3c6209b Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Thu, 26 Sep 2024 10:27:38 +0200 Subject: [PATCH 6/6] remove deprecated TODOs --- pineappl_py/src/grid.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 86df8e33..2fb37c62 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -413,7 +413,6 @@ impl PyGrid { CowArray::from(op.as_array().to_owned()), )) }), - // TODO: make `order_mask` a `Vec` &order_mask, xi, &AlphasTable { ren1, alphas }, @@ -478,7 +477,6 @@ impl PyGrid { CowArray::from(op.as_array().to_owned()), )) }), - // TODO: make `order_mask` a `Vec` &order_mask, xi, &AlphasTable { ren1, alphas },