Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support native pathlike objects #105

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pineappl_py/pineappl/fk_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def read(cls, path):

Parameters
----------
path : str
path : pathlike
file path

Returns
Expand Down
2 changes: 1 addition & 1 deletion pineappl_py/pineappl/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def read(cls, path):

Parameters
----------
path : str
path : pathlike
file path

Returns
Expand Down
11 changes: 7 additions & 4 deletions pineappl_py/src/fk_table.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use numpy::{IntoPyArray, PyArray1, PyArray4};
use pineappl::fk_table::FkTable;
use pineappl::grid::Grid;
use pineappl::lumi::LumiCache;

use numpy::{IntoPyArray, PyArray1, PyArray4};
use pyo3::prelude::*;

use std::collections::HashMap;
use std::convert::TryFrom;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;

/// PyO3 wrapper to :rustdoc:`pineappl::fk_table::FkTable <fk_table/struct.FkTable.html>`
///
Expand All @@ -20,7 +23,7 @@ pub struct PyFkTable {
#[pymethods]
impl PyFkTable {
#[staticmethod]
pub fn read(path: &str) -> Self {
pub fn read(path: PathBuf) -> Self {
Self {
fk_table: FkTable::try_from(
Grid::read(BufReader::new(File::open(path).unwrap())).unwrap(),
Expand Down Expand Up @@ -150,7 +153,7 @@ impl PyFkTable {
/// ----------
/// path : str
/// file path
pub fn write(&self, path: &str) {
pub fn write(&self, path: PathBuf) {
self.fk_table.write(File::create(path).unwrap()).unwrap();
}

Expand All @@ -162,7 +165,7 @@ impl PyFkTable {
/// ----------
/// path : str
/// file path
pub fn write_lz4(&self, path: &str) {
pub fn write_lz4(&self, path: PathBuf) {
self.fk_table
.write_lz4(File::create(path).unwrap())
.unwrap();
Expand Down
9 changes: 5 additions & 4 deletions pineappl_py/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use numpy::{IntoPyArray, PyArray1};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
Expand Down Expand Up @@ -357,7 +358,7 @@ impl PyGrid {
/// PyGrid :
/// grid
#[staticmethod]
pub fn read(path: &str) -> Self {
pub fn read(path: PathBuf) -> Self {
Self::new(Grid::read(BufReader::new(File::open(path).unwrap())).unwrap())
}

Expand All @@ -369,7 +370,7 @@ impl PyGrid {
/// ----------
/// path : str
/// file path
pub fn write(&self, path: &str) {
pub fn write(&self, path: PathBuf) {
self.grid.write(File::create(path).unwrap()).unwrap();
}

Expand All @@ -379,7 +380,7 @@ impl PyGrid {
/// ----------
/// path : str
/// file path
pub fn write_lz4(&self, path: &str) {
pub fn write_lz4(&self, path: PathBuf) {
self.grid.write_lz4(File::create(path).unwrap()).unwrap();
}

Expand All @@ -396,7 +397,7 @@ impl PyGrid {
/// ----
/// For a current limitation with the implementation of the bound object `Grid` is not possible
/// to operate with two `Grid`s in memory, since is not possible to pass a `Grid` by argument
pub fn merge_from_file(&mut self, path: &str) -> PyResult<()> {
pub fn merge_from_file(&mut self, path: PathBuf) -> PyResult<()> {
match self.grid.merge(Self::read(path).grid) {
Ok(()) => Ok(()),
Err(x) => Err(PyValueError::new_err(format!("{:?}", x))),
Expand Down
59 changes: 32 additions & 27 deletions pineappl_py/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_init(self):
assert isinstance(g.raw, pineappl.pineappl.PyGrid)
# orders
assert len(g.orders()) == 1
assert g.orders()[0].as_tuple() == (3,0,0,0)
assert g.orders()[0].as_tuple() == (3, 0, 0, 0)

def test_set_subgrid(self):
g = self.fake_grid()
Expand Down Expand Up @@ -57,28 +57,28 @@ def test_set_subgrid(self):
def test_set_key_value(self):
g = self.fake_grid()
g.set_key_value("bla", "blub")
g.set_key_value("\"", "'")
g.set_key_value('"', "'")
g.set_key_value("äöü", "ß\\")

def test_bins(self):
g = self.fake_grid()
# 1D
normalizations = [1.0] * 2
limits = [(1,1),(2,2)]
limits = [(1, 1), (2, 2)]
remapper = pineappl.bin.BinRemapper(normalizations, limits)
g.set_remapper(remapper)
assert g.bin_dimensions() == 1
np.testing.assert_allclose(g.bin_left(0), [1,2])
np.testing.assert_allclose(g.bin_right(0), [1,2])
np.testing.assert_allclose(g.bin_left(0), [1, 2])
np.testing.assert_allclose(g.bin_right(0), [1, 2])
# 2D
limits = [(1,2),(2,3),(2,4),(3,5)]
limits = [(1, 2), (2, 3), (2, 4), (3, 5)]
remapper = pineappl.bin.BinRemapper(normalizations, limits)
g.set_remapper(remapper)
assert g.bin_dimensions() == 2
np.testing.assert_allclose(g.bin_left(0), [1,2])
np.testing.assert_allclose(g.bin_right(0), [2,4])
np.testing.assert_allclose(g.bin_left(1), [2,3])
np.testing.assert_allclose(g.bin_right(1), [3,5])
np.testing.assert_allclose(g.bin_left(0), [1, 2])
np.testing.assert_allclose(g.bin_right(0), [2, 4])
np.testing.assert_allclose(g.bin_left(1), [2, 3])
np.testing.assert_allclose(g.bin_right(1), [3, 5])

def test_convolute_with_one(self):
g = self.fake_grid()
Expand All @@ -93,9 +93,18 @@ def test_convolute_with_one(self):
[1.0],
)
g.set_subgrid(0, 0, 0, subgrid)
np.testing.assert_allclose(g.convolute_with_one(2212, lambda pid,x,q2: 0., lambda q2: 0.), [0.]*2)
np.testing.assert_allclose(g.convolute_with_one(2212, lambda pid,x,q2: 1, lambda q2: 1.), [5e6/9999,0.])
np.testing.assert_allclose(g.convolute_with_one(2212, lambda pid,x,q2: 1, lambda q2: 2.), [2**3 * 5e6/9999,0.])
np.testing.assert_allclose(
g.convolute_with_one(2212, lambda pid, x, q2: 0.0, lambda q2: 0.0),
[0.0] * 2,
)
np.testing.assert_allclose(
g.convolute_with_one(2212, lambda pid, x, q2: 1, lambda q2: 1.0),
[5e6 / 9999, 0.0],
)
np.testing.assert_allclose(
g.convolute_with_one(2212, lambda pid, x, q2: 1, lambda q2: 2.0),
[2 ** 3 * 5e6 / 9999, 0.0],
)

def test_axes(self):
g = self.fake_grid()
Expand All @@ -105,15 +114,15 @@ def test_axes(self):
vs = np.random.rand(len(xs))
subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV1(
vs[np.newaxis, :, np.newaxis],
[90.],
[90.0],
xs,
[1.0],
)
g.set_subgrid(0, 0, 0, subgrid)
vs2 = np.random.rand(len(xs))
subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV1(
vs2[np.newaxis, :, np.newaxis],
[100.],
[100.0],
xs,
[1.0],
)
Expand All @@ -123,31 +132,27 @@ def test_axes(self):

np.testing.assert_allclose(ei[0], xs)
np.testing.assert_allclose(ei[1], [])
np.testing.assert_allclose(ei[2], [90., 100.])
np.testing.assert_allclose(ei[2], [90.0, 100.0])

def test_io(self, tmp_path):
g = self.fake_grid()
p = tmp_path / "test.pineappl"
p.write_text("")
g.write(str(p))
gg = pineappl.grid.Grid.read(str(p))
gg = pineappl.grid.Grid.read(p)
assert isinstance(gg, pineappl.grid.Grid)
ggg = pineappl.grid.Grid.read(str(p))

def test_convolute_eko(self):
g = self.fake_grid()
fake_eko = {
"q2_ref": 1.,
"q2_ref": 1.0,
"targetpids": [1],
"targetgrid": [.1,1.],
"targetgrid": [0.1, 1.0],
"inputpids": [1],
"inputgrid": [.1,1.],
"interpolation_xgrid": [.1,1.],
"Q2grid": {
90: {
"operators": np.random.rand(1,2,1,2),
"alphas": 1.
}
}
"inputgrid": [0.1, 1.0],
"interpolation_xgrid": [0.1, 1.0],
"Q2grid": {90: {"operators": np.random.rand(1, 2, 1, 2), "alphas": 1.0}},
}
g.set_key_value("lumi_id_types", "pdg_mc_ids")
fk = g.convolute_eko(fake_eko)
Expand Down