From 0db89d258c1b9b367cfeaa6b9024cb444669e5cb Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Mon, 8 Feb 2021 12:05:27 +0100 Subject: [PATCH 1/3] Start moving pineappl_py into main pineappl crate --- Cargo.toml | 7 +- pineappl/.gitignore | 145 ++++++++++++++++++ pineappl/Cargo.toml | 8 + .../dev.py.requirements.txt | 0 .../examples/import-yadism-yaml.py | 0 .../examples/py.requirements.txt | 0 .../pineappl/__init__.py | 0 {pineappl_py => pineappl}/pineappl/bin.py | 0 {pineappl_py => pineappl}/pineappl/grid.py | 0 .../pineappl/lagrange_subgrid.py | 0 {pineappl_py => pineappl}/pineappl/lumi.py | 0 {pineappl_py => pineappl}/pineappl/subgrid.py | 0 {pineappl_py => pineappl}/pineappl/utils.py | 0 pineappl/src/bin.rs | 4 + pineappl/src/lib.rs | 17 ++ .../tests/python}/test_bin.py | 0 .../tests/python}/test_grid.py | 0 .../tests/python}/test_lagrange_subgrid.py | 0 .../tests/python}/test_lumi.py | 0 .../tests/python}/test_sugrid.py | 0 pineappl_py/Cargo.toml | 23 --- pineappl_py/src/bin.rs | 3 - pineappl_py/src/lib.rs | 22 --- 23 files changed, 175 insertions(+), 54 deletions(-) create mode 100644 pineappl/.gitignore rename pineappl_py/dev.requirements.txt => pineappl/dev.py.requirements.txt (100%) rename {pineappl_py => pineappl}/examples/import-yadism-yaml.py (100%) rename pineappl_py/examples/requirements.txt => pineappl/examples/py.requirements.txt (100%) rename {pineappl_py => pineappl}/pineappl/__init__.py (100%) rename {pineappl_py => pineappl}/pineappl/bin.py (100%) rename {pineappl_py => pineappl}/pineappl/grid.py (100%) rename {pineappl_py => pineappl}/pineappl/lagrange_subgrid.py (100%) rename {pineappl_py => pineappl}/pineappl/lumi.py (100%) rename {pineappl_py => pineappl}/pineappl/subgrid.py (100%) rename {pineappl_py => pineappl}/pineappl/utils.py (100%) rename {pineappl_py/tests => pineappl/tests/python}/test_bin.py (100%) rename {pineappl_py/tests => pineappl/tests/python}/test_grid.py (100%) rename {pineappl_py/tests => pineappl/tests/python}/test_lagrange_subgrid.py (100%) rename {pineappl_py/tests => pineappl/tests/python}/test_lumi.py (100%) rename {pineappl_py/tests => pineappl/tests/python}/test_sugrid.py (100%) delete mode 100644 pineappl_py/Cargo.toml delete mode 100644 pineappl_py/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 0d6dbe6ff..e446f6df7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,5 @@ [workspace] -members = [ - "pineappl", - "pineappl_capi", - "pineappl_py", - "pineappl_cli", -] +members = ["pineappl", "pineappl_capi", "pineappl_cli"] [profile.release] lto = true diff --git a/pineappl/.gitignore b/pineappl/.gitignore new file mode 100644 index 000000000..bfbb67c10 --- /dev/null +++ b/pineappl/.gitignore @@ -0,0 +1,145 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/python +# Edit at https://www.toptal.com/developers/gitignore?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +pytestdebug.log + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# profiling data +.prof + +# End of https://www.toptal.com/developers/gitignore/api/python diff --git a/pineappl/Cargo.toml b/pineappl/Cargo.toml index 35ce0d64e..5bcb3f2c5 100644 --- a/pineappl/Cargo.toml +++ b/pineappl/Cargo.toml @@ -24,6 +24,14 @@ rustc-hash = "1.1.0" serde = { features = ["derive"], version = "1.0" } thiserror = "1.0" +[lib] +name = "pineappl" +crate-type = ["cdylib"] + +[dependencies.pyo3] +version = "0.12.4" +features = ["extension-module"] + [dev-dependencies] lhapdf = "0.1.8" rand = { default-features = false, version = "0.7" } diff --git a/pineappl_py/dev.requirements.txt b/pineappl/dev.py.requirements.txt similarity index 100% rename from pineappl_py/dev.requirements.txt rename to pineappl/dev.py.requirements.txt diff --git a/pineappl_py/examples/import-yadism-yaml.py b/pineappl/examples/import-yadism-yaml.py similarity index 100% rename from pineappl_py/examples/import-yadism-yaml.py rename to pineappl/examples/import-yadism-yaml.py diff --git a/pineappl_py/examples/requirements.txt b/pineappl/examples/py.requirements.txt similarity index 100% rename from pineappl_py/examples/requirements.txt rename to pineappl/examples/py.requirements.txt diff --git a/pineappl_py/pineappl/__init__.py b/pineappl/pineappl/__init__.py similarity index 100% rename from pineappl_py/pineappl/__init__.py rename to pineappl/pineappl/__init__.py diff --git a/pineappl_py/pineappl/bin.py b/pineappl/pineappl/bin.py similarity index 100% rename from pineappl_py/pineappl/bin.py rename to pineappl/pineappl/bin.py diff --git a/pineappl_py/pineappl/grid.py b/pineappl/pineappl/grid.py similarity index 100% rename from pineappl_py/pineappl/grid.py rename to pineappl/pineappl/grid.py diff --git a/pineappl_py/pineappl/lagrange_subgrid.py b/pineappl/pineappl/lagrange_subgrid.py similarity index 100% rename from pineappl_py/pineappl/lagrange_subgrid.py rename to pineappl/pineappl/lagrange_subgrid.py diff --git a/pineappl_py/pineappl/lumi.py b/pineappl/pineappl/lumi.py similarity index 100% rename from pineappl_py/pineappl/lumi.py rename to pineappl/pineappl/lumi.py diff --git a/pineappl_py/pineappl/subgrid.py b/pineappl/pineappl/subgrid.py similarity index 100% rename from pineappl_py/pineappl/subgrid.py rename to pineappl/pineappl/subgrid.py diff --git a/pineappl_py/pineappl/utils.py b/pineappl/pineappl/utils.py similarity index 100% rename from pineappl_py/pineappl/utils.py rename to pineappl/pineappl/utils.py diff --git a/pineappl/src/bin.rs b/pineappl/src/bin.rs index e4bb8111c..063e7f047 100644 --- a/pineappl/src/bin.rs +++ b/pineappl/src/bin.rs @@ -2,6 +2,7 @@ use super::convert::{f64_from_usize, usize_from_f64}; use float_cmp::approx_eq; +use pyo3::prelude::*; use serde::{Deserialize, Serialize}; use std::f64; use thiserror::Error; @@ -47,6 +48,7 @@ pub enum BinRemapperNewError { } /// Structure for remapping bin limits. +#[pyclass] #[derive(Clone, Debug, Deserialize, Serialize)] pub struct BinRemapper { normalizations: Vec, @@ -148,6 +150,7 @@ impl PartialEq> for BinInfo<'_> { } } +#[pymethods] impl BinRemapper { /// Create a new `BinRemapper` object with the specified number of bins and dimensions and /// limits. @@ -156,6 +159,7 @@ impl BinRemapper { /// /// Returns an error if the length of `limits` is not a multiple of the length of /// `normalizations`. + #[new] pub fn new( normalizations: Vec, limits: Vec<(f64, f64)>, diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index 745f2525e..deec4d5c9 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -4,6 +4,8 @@ //! `PineAPPL` is not an extension of `APPLgrid`. +use pyo3::prelude::*; + mod convert; pub mod bin; @@ -13,3 +15,18 @@ pub mod lumi; pub mod ntuple_subgrid; pub mod sparse_array3; pub mod subgrid; + +/// A Python module implemented in Rust. +/// NOTE: this name has to match the one in Cargo.toml 'lib.name' +#[pymodule] +fn pineappl(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_class::()?; + //m.add_class::()?; + //m.add_class::()?; + //m.add_class::()?; + //m.add_class::()?; + //m.add_class::()?; + //m.add_class::()?; + + Ok(()) +} diff --git a/pineappl_py/tests/test_bin.py b/pineappl/tests/python/test_bin.py similarity index 100% rename from pineappl_py/tests/test_bin.py rename to pineappl/tests/python/test_bin.py diff --git a/pineappl_py/tests/test_grid.py b/pineappl/tests/python/test_grid.py similarity index 100% rename from pineappl_py/tests/test_grid.py rename to pineappl/tests/python/test_grid.py diff --git a/pineappl_py/tests/test_lagrange_subgrid.py b/pineappl/tests/python/test_lagrange_subgrid.py similarity index 100% rename from pineappl_py/tests/test_lagrange_subgrid.py rename to pineappl/tests/python/test_lagrange_subgrid.py diff --git a/pineappl_py/tests/test_lumi.py b/pineappl/tests/python/test_lumi.py similarity index 100% rename from pineappl_py/tests/test_lumi.py rename to pineappl/tests/python/test_lumi.py diff --git a/pineappl_py/tests/test_sugrid.py b/pineappl/tests/python/test_sugrid.py similarity index 100% rename from pineappl_py/tests/test_sugrid.py rename to pineappl/tests/python/test_sugrid.py diff --git a/pineappl_py/Cargo.toml b/pineappl_py/Cargo.toml deleted file mode 100644 index be69aa347..000000000 --- a/pineappl_py/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "pineappl_py" -version = "0.3.0" -authors = ["Christopher Schwan ", "Alessandro Candido "] -edition = "2018" -license = "GPL-3.0-or-later" -repository = "https://github.com/N3PDF/pineappl" -readme = "README.md" -keywords = ["high-energy-physics", "physics"] -categories = ["science"] -description = "Python bindings to PineAPPL" - -[lib] -name = "pineappl" -crate-type = ["cdylib"] - -[dependencies] -pineappl = { path = "../pineappl", version = "0.3.0" } -itertools = "0.9" - -[dependencies.pyo3] -version = "0.12.4" -features = ["extension-module"] diff --git a/pineappl_py/src/bin.rs b/pineappl_py/src/bin.rs index 9a29e8f92..83189b6b7 100644 --- a/pineappl_py/src/bin.rs +++ b/pineappl_py/src/bin.rs @@ -1,11 +1,8 @@ use pineappl::bin::BinRemapper; -use pyo3::prelude::*; //use pyo3::types::{PyList, PyTuple}; //use pyo3::{exceptions::PyRuntimeError, Python}; -#[pyclass] -#[derive(Clone)] #[repr(transparent)] pub struct PyBinRemapper { pub bin_remapper: BinRemapper, diff --git a/pineappl_py/src/lib.rs b/pineappl_py/src/lib.rs deleted file mode 100644 index eba581cef..000000000 --- a/pineappl_py/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -use pyo3::prelude::*; - -pub mod bin; -pub mod grid; -pub mod lagrange_subgrid; -pub mod lumi; -pub mod subgrid; - -/// A Python module implemented in Rust. -/// NOTE: this name has to match the one in Cargo.toml 'lib.name' -#[pymodule] -fn pineappl(_py: Python, m: &PyModule) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - - Ok(()) -} From bf7136e839ab4090d05590ae35b4439493eb1b01 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 8 Feb 2021 15:20:10 +0100 Subject: [PATCH 2/3] Use pyo3 conditionally --- pineappl/Cargo.toml | 8 ++++---- pineappl/src/bin.rs | 10 ++++++---- pineappl/src/lib.rs | 6 ++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pineappl/Cargo.toml b/pineappl/Cargo.toml index 5bcb3f2c5..7828559e2 100644 --- a/pineappl/Cargo.toml +++ b/pineappl/Cargo.toml @@ -20,17 +20,17 @@ float-cmp = "0.8" itertools = "0.9" lz-fear = "0.1" ndarray = { features = ["serde"], version = "0.13.1" } +pyo3 = { features = ["extension-module"], optional = true, version = "0.12.4" } rustc-hash = "1.1.0" serde = { features = ["derive"], version = "1.0" } thiserror = "1.0" [lib] name = "pineappl" -crate-type = ["cdylib"] +crate-type = ["cdylib", "lib"] -[dependencies.pyo3] -version = "0.12.4" -features = ["extension-module"] +[features] +python = ["pyo3"] [dev-dependencies] lhapdf = "0.1.8" diff --git a/pineappl/src/bin.rs b/pineappl/src/bin.rs index 063e7f047..e80eda42c 100644 --- a/pineappl/src/bin.rs +++ b/pineappl/src/bin.rs @@ -1,8 +1,10 @@ //! Module that contains helpers for binning observables +#[cfg(feature = "python")] +use pyo3::prelude::*; + use super::convert::{f64_from_usize, usize_from_f64}; use float_cmp::approx_eq; -use pyo3::prelude::*; use serde::{Deserialize, Serialize}; use std::f64; use thiserror::Error; @@ -48,7 +50,7 @@ pub enum BinRemapperNewError { } /// Structure for remapping bin limits. -#[pyclass] +#[cfg_attr(feature = "python", pyclass)] #[derive(Clone, Debug, Deserialize, Serialize)] pub struct BinRemapper { normalizations: Vec, @@ -150,7 +152,7 @@ impl PartialEq> for BinInfo<'_> { } } -#[pymethods] +#[cfg_attr(feature = "python", pymethods)] impl BinRemapper { /// Create a new `BinRemapper` object with the specified number of bins and dimensions and /// limits. @@ -159,7 +161,7 @@ impl BinRemapper { /// /// Returns an error if the length of `limits` is not a multiple of the length of /// `normalizations`. - #[new] + #[cfg_attr(feature = "python", new)] pub fn new( normalizations: Vec, limits: Vec<(f64, f64)>, diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index deec4d5c9..f816e6f05 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -4,8 +4,6 @@ //! `PineAPPL` is not an extension of `APPLgrid`. -use pyo3::prelude::*; - mod convert; pub mod bin; @@ -18,6 +16,10 @@ pub mod subgrid; /// A Python module implemented in Rust. /// NOTE: this name has to match the one in Cargo.toml 'lib.name' +#[cfg(feature = "python")] +use pyo3::prelude::*; + +#[cfg(feature = "python")] #[pymodule] fn pineappl(_py: Python, m: &PyModule) -> PyResult<()> { m.add_class::()?; From 10944aada7e36896549e413c131e57b6713051d9 Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Tue, 9 Feb 2021 10:33:37 +0100 Subject: [PATCH 3/3] Update pyo3 version, add bindings to grid --- pineappl/Cargo.toml | 2 +- pineappl/src/grid.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pineappl/Cargo.toml b/pineappl/Cargo.toml index 7828559e2..755087920 100644 --- a/pineappl/Cargo.toml +++ b/pineappl/Cargo.toml @@ -20,7 +20,7 @@ float-cmp = "0.8" itertools = "0.9" lz-fear = "0.1" ndarray = { features = ["serde"], version = "0.13.1" } -pyo3 = { features = ["extension-module"], optional = true, version = "0.12.4" } +pyo3 = { features = ["extension-module"], optional = true, version = "0.13.1" } rustc-hash = "1.1.0" serde = { features = ["derive"], version = "1.0" } thiserror = "1.0" diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index c85f77f2d..ae676e857 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -175,6 +175,7 @@ pub enum GridSetBinRemapperError { /// Main data structure of `PineAPPL`. This structure contains a `Subgrid` for each `LumiEntry`, /// bin, and coupling order it was created with. +#[cfg_attr(feature = "python", pyclass)] #[derive(Deserialize, Serialize)] pub struct Grid { subgrids: Array3, @@ -185,8 +186,10 @@ pub struct Grid { more_members: MoreMembers, } +#[cfg_attr(feature = "python", pymethods)] impl Grid { /// Constructor. + #[cfg_attr(feature = "python", new)] #[must_use] pub fn new( lumi: Vec,