Skip to content

Commit

Permalink
Expose more Python methods that are directly requried by pineko
Browse files Browse the repository at this point in the history
  • Loading branch information
Radonirinaunimi committed Nov 23, 2024
1 parent d71ac51 commit 95ff7eb
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pineappl/src/empty_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl Subgrid for EmptySubgridV1 {
Vec::new()
}

fn shape(&mut self) -> &[usize] {
panic!("EmptySubgridV1 doesn't have a shape");
}

fn is_empty(&self) -> bool {
true
}
Expand Down
10 changes: 8 additions & 2 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,19 @@ impl Grid {
&mut self.pid_basis
}

/// TODO
/// Return a vector containing the interpolation specifications for this grid.
#[must_use]
pub fn interpolations(&self) -> &[Interp] {
&self.interps
}

/// Return a vector containing the kinematic specifications for this grid.
#[must_use]
pub fn kinematics(&self) -> &[Kinematics] {
&self.kinematics
}

/// TODO
/// Return a vector containg the scale specifications for this grid.
#[must_use]
pub const fn scales(&self) -> &Scales {
&self.scales
Expand Down
4 changes: 4 additions & 0 deletions pineappl/src/import_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ impl Subgrid for ImportSubgridV1 {
Box::new(self.array.indexed_iter())
}

fn shape(&mut self) -> &[usize] {
self.array.shape()
}

fn stats(&self) -> Stats {
Stats {
total: self.array.shape().iter().product(),
Expand Down
4 changes: 4 additions & 0 deletions pineappl/src/interp_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl Subgrid for InterpSubgridV1 {
self.array.is_empty()
}

fn shape(&mut self) -> &[usize] {
self.array.shape()
}

fn merge(&mut self, other: &SubgridEnum, transpose: Option<(usize, usize)>) {
// we cannot use `Self::indexed_iter` because it multiplies with `reweight`
if let SubgridEnum::InterpSubgridV1(other) = other {
Expand Down
3 changes: 3 additions & 0 deletions pineappl/src/subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub trait Subgrid {
/// Scale the subgrid by `factor`.
fn scale(&mut self, factor: f64);

/// Return the shape of the subgrid
fn shape(&mut self) -> &[usize];

/// Assume that the convolution functions for indices `a` and `b` for this grid are the same
/// and use this to optimize the size of the grid.
fn symmetrize(&mut self, a: usize, b: usize);
Expand Down
31 changes: 31 additions & 0 deletions pineappl_py/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use pineappl::grid::Grid;
use pineappl::pids::PidBasis;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
Expand Down Expand Up @@ -228,6 +229,19 @@ impl PyGrid {
.insert(key.to_owned(), value.to_owned());
}

/// Get metadata values stored in the grid.
///
///
/// Returns
/// -------
/// dict :
/// key, value map
#[getter]
#[must_use]
pub fn key_values(&self) -> BTreeMap<String, String> {
self.grid.metadata().clone()
}

/// Convolve the grid with as many distributions.
///
/// # Panics
Expand Down Expand Up @@ -651,6 +665,23 @@ impl PyGrid {
.collect()
}

/// Get the interpolation specifications for the current grid.
///
/// Returns
/// list(PyInterp):
/// list of interpolation specifications
#[getter]
#[must_use]
pub fn interpolations(&mut self) -> Vec<PyInterp> {
self.grid
.interpolations()
.iter()
.map(|interp| PyInterp {
interp: interp.clone(),
})
.collect()
}

/// Extract channels.
///
/// Returns
Expand Down
29 changes: 29 additions & 0 deletions pineappl_py/src/subgrid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Subgrid interface.
use ndarray::ArrayD;
use numpy::{IntoPyArray, PyArrayDyn};
use pineappl::subgrid::{Subgrid, SubgridEnum};
use pyo3::prelude::*;

Expand All @@ -23,6 +25,33 @@ impl PySubgridEnum {
self.subgrid_enum.scale(factor);
}

/// Get the values of nodes used for the subgrids
#[getter]
pub fn node_values(&mut self) -> Vec<Vec<f64>> {
self.subgrid_enum.node_values()
}

/// Get the shape of the subgrids
#[getter]
pub fn shape(&mut self) -> Vec<usize> {
self.subgrid_enum.shape().to_vec()
}

/// Return the dense array of the subgrid.
#[must_use]
pub fn to_array<'py>(
&mut self,
py: Python<'py>,
shape: Vec<usize>,
) -> Bound<'py, PyArrayDyn<f64>> {
let mut array_subgrid = ArrayD::<f64>::zeros(shape);

for (index, value) in self.subgrid_enum.indexed_iter() {
array_subgrid[index.as_slice()] = value;
}
array_subgrid.into_pyarray_bound(py)
}

/// Clone.
#[must_use]
pub fn into(&self) -> Self {
Expand Down

0 comments on commit 95ff7eb

Please sign in to comment.