Skip to content

Commit

Permalink
feat: bump versions on ganesh, pyo3, and (rust) numpy and make …
Browse files Browse the repository at this point in the history
…appropriate updates for each
  • Loading branch information
denehoffman committed Dec 14, 2024
1 parent 5e43680 commit 3f2d67a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 63 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ auto_ops = "0.3.0"
rand = "0.8.5"
rand_chacha = "0.3.1"
rayon = { version = "1.10.0", optional = true }
pyo3 = { version = "0.22.6", optional = true, features = [
pyo3 = { version = "0.23.3", optional = true, features = [
"num-complex",
"abi3-py37",
] }
numpy = { version = "0.22.1", optional = true, features = ["nalgebra"] }
ganesh = "0.13.1"
numpy = { version = "0.23.0", optional = true, features = ["nalgebra"] }
ganesh = "0.14.1"
thiserror = "2.0.3"
shellexpand = "3.1.0"
accurate = "0.4.1"
Expand Down Expand Up @@ -64,7 +64,7 @@ harness = false
default = ["rayon", "python"]
extension-module = ["pyo3/extension-module"]
rayon = ["dep:rayon"]
f32 = []
f32 = ["ganesh/f32"]
python = ["pyo3", "numpy", "extension-module"]
pyo3 = ["dep:pyo3"]
numpy = ["dep:numpy"]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ pub trait ReadWrite: Serialize + DeserializeOwned {
}
}

impl ReadWrite for Status<Float> {
impl ReadWrite for Status {
fn create_null() -> Self {
Status::default()
}
Expand Down
23 changes: 10 additions & 13 deletions src/likelihoods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ impl LikelihoodTerm for NLL {
}
}

impl Function<Float, (), Infallible> for NLL {
impl Function<(), Infallible> for NLL {
fn evaluate(&self, parameters: &[Float], _user_data: &mut ()) -> Result<Float, Infallible> {
Ok(LikelihoodTerm::evaluate(self, parameters))
}
Expand All @@ -581,8 +581,8 @@ impl Function<Float, (), Infallible> for NLL {

/// A set of options that are used when minimizations are performed.
pub struct MinimizerOptions {
algorithm: Box<dyn ganesh::Algorithm<Float, (), Infallible>>,
observers: Vec<Box<dyn Observer<Float, ()>>>,
algorithm: Box<dyn ganesh::Algorithm<(), Infallible>>,
observers: Vec<Box<dyn Observer<()>>>,
max_steps: usize,
}

Expand All @@ -601,8 +601,8 @@ struct VerboseObserver {
show_x: bool,
show_fx: bool,
}
impl Observer<Float, ()> for VerboseObserver {
fn callback(&mut self, step: usize, status: &mut Status<Float>, _user_data: &mut ()) -> bool {
impl Observer<()> for VerboseObserver {
fn callback(&mut self, step: usize, status: &mut Status, _user_data: &mut ()) -> bool {
if self.show_step {
println!("Step: {}", step);
}
Expand Down Expand Up @@ -643,18 +643,15 @@ impl MinimizerOptions {
}
/// Set the [`Algorithm`] to be used in the minimization (default: [`LBFGSB`] with default
/// settings).
pub fn with_algorithm<A: Algorithm<Float, (), Infallible> + 'static>(
self,
algorithm: A,
) -> Self {
pub fn with_algorithm<A: Algorithm<(), Infallible> + 'static>(self, algorithm: A) -> Self {
Self {
algorithm: Box::new(algorithm),
observers: self.observers,
max_steps: self.max_steps,
}
}
/// Add an [`Observer`] to the list of [`Observer`]s used in the minimization.
pub fn with_observer<O: Observer<Float, ()> + 'static>(self, observer: O) -> Self {
pub fn with_observer<O: Observer<()> + 'static>(self, observer: O) -> Self {
let mut observers = self.observers;
observers.push(Box::new(observer));
Self {
Expand Down Expand Up @@ -682,7 +679,7 @@ impl NLL {
p0: &[Float],
bounds: Option<Vec<(Float, Float)>>,
options: Option<MinimizerOptions>,
) -> Status<Float> {
) -> Status {
let options = options.unwrap_or_default();
let mut m = Minimizer::new_from_box(options.algorithm, self.parameters().len())
.with_bounds(bounds)
Expand Down Expand Up @@ -881,7 +878,7 @@ pub struct LikelihoodEvaluator {
likelihood_expression: LikelihoodExpression,
}

impl Function<Float, (), Infallible> for LikelihoodEvaluator {
impl Function<(), Infallible> for LikelihoodEvaluator {
fn evaluate(&self, parameters: &[Float], _user_data: &mut ()) -> Result<Float, Infallible> {
let mut param_buffers: Vec<Vec<Float>> = self
.likelihood_manager
Expand Down Expand Up @@ -981,7 +978,7 @@ impl LikelihoodEvaluator {
p0: &[Float],
bounds: Option<Vec<(Float, Float)>>,
options: Option<MinimizerOptions>,
) -> Status<Float> {
) -> Status {
let options = options.unwrap_or_default();
let mut m = Minimizer::new_from_box(options.algorithm, self.parameters().len())
.with_bounds(bounds)
Expand Down
72 changes: 27 additions & 45 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use pyo3::{
types::{PyTuple, PyTupleMethods},
};

use crate::Float;

#[pymodule]
#[allow(non_snake_case, clippy::upper_case_acronyms)]
pub(crate) mod laddu {
Expand Down Expand Up @@ -387,7 +385,7 @@ pub(crate) mod laddu {
/// A ``numpy`` array built from the components of this ``Vector3``
///
fn to_numpy<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, self.0.as_slice())
PyArray1::from_slice(py, self.0.as_slice())
}
/// Convert an array into a 3-vector
///
Expand Down Expand Up @@ -796,7 +794,7 @@ pub(crate) mod laddu {
/// A ``numpy`` array built from the components of this ``Vector4``
///
fn to_numpy<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, self.0.as_slice())
PyArray1::from_slice(py, self.0.as_slice())
}
/// Convert an array into a 4-vector
///
Expand Down Expand Up @@ -955,7 +953,7 @@ pub(crate) mod laddu {
///
#[getter]
fn weights<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.weights())
PyArray1::from_slice(py, &self.0.weights())
}
/// The internal list of Events stored in the Dataset
///
Expand Down Expand Up @@ -1080,7 +1078,7 @@ pub(crate) mod laddu {
///
#[getter]
fn edges<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.edges())
PyArray1::from_slice(py, &self.0.edges())
}
fn __getitem__(&self, index: usize) -> PyResult<Dataset> {
self.0
Expand Down Expand Up @@ -1165,7 +1163,7 @@ pub(crate) mod laddu {
/// The values of the Variable for each Event in the given `dataset`
///
fn value_on<'py>(&self, py: Python<'py>, dataset: &Dataset) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.value_on(&dataset.0))
PyArray1::from_slice(py, &self.0.value_on(&dataset.0))
}
}

Expand Down Expand Up @@ -1258,7 +1256,7 @@ pub(crate) mod laddu {
/// The values of the Variable for each Event in the given `dataset`
///
fn value_on<'py>(&self, py: Python<'py>, dataset: &Dataset) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.value_on(&dataset.0))
PyArray1::from_slice(py, &self.0.value_on(&dataset.0))
}
}

Expand Down Expand Up @@ -1351,7 +1349,7 @@ pub(crate) mod laddu {
/// The values of the Variable for each Event in the given `dataset`
///
fn value_on<'py>(&self, py: Python<'py>, dataset: &Dataset) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.value_on(&dataset.0))
PyArray1::from_slice(py, &self.0.value_on(&dataset.0))
}
}

Expand Down Expand Up @@ -1475,7 +1473,7 @@ pub(crate) mod laddu {
/// The values of the Variable for each Event in the given `dataset`
///
fn value_on<'py>(&self, py: Python<'py>, dataset: &Dataset) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.value_on(&dataset.0))
PyArray1::from_slice(py, &self.0.value_on(&dataset.0))
}
}

Expand Down Expand Up @@ -1531,7 +1529,7 @@ pub(crate) mod laddu {
/// The values of the Variable for each Event in the given `dataset`
///
fn value_on<'py>(&self, py: Python<'py>, dataset: &Dataset) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.value_on(&dataset.0))
PyArray1::from_slice(py, &self.0.value_on(&dataset.0))
}
}

Expand Down Expand Up @@ -1670,7 +1668,7 @@ pub(crate) mod laddu {
/// The values of the Variable for each Event in the given `dataset`
///
fn value_on<'py>(&self, py: Python<'py>, dataset: &Dataset) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, &self.0.value_on(&dataset.0))
PyArray1::from_slice(py, &self.0.value_on(&dataset.0))
}
}

Expand Down Expand Up @@ -2050,10 +2048,7 @@ pub(crate) mod laddu {
Model(crate::amplitudes::Model::create_null())
}
fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
Ok(PyBytes::new_bound(
py,
serialize(&self.0).unwrap().as_slice(),
))
Ok(PyBytes::new(py, serialize(&self.0).unwrap().as_slice()))
}
fn __setstate__(&mut self, state: Bound<'_, PyBytes>) -> PyResult<()> {
*self = Model(deserialize(state.as_bytes()).unwrap());
Expand Down Expand Up @@ -2199,7 +2194,7 @@ pub(crate) mod laddu {
py: Python<'py>,
parameters: Vec<Float>,
) -> Bound<'py, PyArray1<Complex<Float>>> {
PyArray1::from_slice_bound(py, &self.0.evaluate(&parameters))
PyArray1::from_slice(py, &self.0.evaluate(&parameters))
}
/// Evaluate the gradient of the stored Expression over the stored Dataset
///
Expand All @@ -2218,7 +2213,7 @@ pub(crate) mod laddu {
py: Python<'py>,
parameters: Vec<Float>,
) -> Bound<'py, PyArray2<Complex<Float>>> {
PyArray2::from_vec2_bound(
PyArray2::from_vec2(
py,
&self
.0
Expand Down Expand Up @@ -2587,7 +2582,7 @@ pub(crate) mod laddu {
py: Python<'py>,
parameters: Vec<Float>,
) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, self.0.evaluate_gradient(&parameters).as_slice())
PyArray1::from_slice(py, self.0.evaluate_gradient(&parameters).as_slice())
}
/// Project the model over the Monte Carlo dataset with the given parameter values
///
Expand All @@ -2614,7 +2609,7 @@ pub(crate) mod laddu {
parameters: Vec<Float>,
mc_evaluator: Option<Evaluator>,
) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(
PyArray1::from_slice(
py,
&self
.0
Expand Down Expand Up @@ -2667,7 +2662,7 @@ pub(crate) mod laddu {
"Argument must be either a string or a list of strings",
));
};
Ok(PyArray1::from_slice_bound(
Ok(PyArray1::from_slice(
py,
&self.0.project_with(
&parameters,
Expand Down Expand Up @@ -3150,7 +3145,7 @@ pub(crate) mod laddu {
///
#[pyclass]
#[derive(Clone)]
pub(crate) struct Status(pub(crate) ganesh::Status<Float>);
pub(crate) struct Status(pub(crate) ganesh::Status);
#[pymethods]
impl Status {
/// The current best position in parameter space
Expand All @@ -3161,7 +3156,7 @@ pub(crate) mod laddu {
///
#[getter]
fn x<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, self.0.x.as_slice())
PyArray1::from_slice(py, self.0.x.as_slice())
}
/// The uncertainty on each parameter (``None`` if it wasn't calculated)
///
Expand All @@ -3174,7 +3169,7 @@ pub(crate) mod laddu {
self.0
.err
.clone()
.map(|err| PyArray1::from_slice_bound(py, err.as_slice()))
.map(|err| PyArray1::from_slice(py, err.as_slice()))
}
/// The initial position at the start of the minimization
///
Expand All @@ -3184,7 +3179,7 @@ pub(crate) mod laddu {
///
#[getter]
fn x0<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Float>> {
PyArray1::from_slice_bound(py, self.0.x0.as_slice())
PyArray1::from_slice(py, self.0.x0.as_slice())
}
/// The optimized value of the objective function
///
Expand All @@ -3205,7 +3200,7 @@ pub(crate) mod laddu {
#[getter]
fn cov<'py>(&self, py: Python<'py>) -> Option<Bound<'py, PyArray2<Float>>> {
self.0.cov.clone().map(|cov| {
PyArray2::from_vec2_bound(
PyArray2::from_vec2(
py,
&cov.row_iter()
.map(|row| row.iter().cloned().collect())
Expand All @@ -3223,7 +3218,7 @@ pub(crate) mod laddu {
#[getter]
fn hess<'py>(&self, py: Python<'py>) -> Option<Bound<'py, PyArray2<Float>>> {
self.0.hess.clone().map(|hess| {
PyArray2::from_vec2_bound(
PyArray2::from_vec2(
py,
&hess
.row_iter()
Expand Down Expand Up @@ -3342,10 +3337,7 @@ pub(crate) mod laddu {
Status(ganesh::Status::create_null())
}
fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
Ok(PyBytes::new_bound(
py,
serialize(&self.0).unwrap().as_slice(),
))
Ok(PyBytes::new(py, serialize(&self.0).unwrap().as_slice()))
}
fn __setstate__(&mut self, state: Bound<'_, PyBytes>) -> PyResult<()> {
*self = Status(deserialize(state.as_bytes()).unwrap());
Expand All @@ -3358,7 +3350,7 @@ pub(crate) mod laddu {
/// dict
///
fn as_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
dict.set_item("x", self.x(py))?;
dict.set_item("err", self.err(py))?;
dict.set_item("x0", self.x0(py))?;
Expand All @@ -3379,7 +3371,7 @@ pub(crate) mod laddu {
#[pyclass]
#[derive(Clone)]
#[pyo3(name = "Bound")]
pub(crate) struct ParameterBound(pub(crate) ganesh::Bound<Float>);
pub(crate) struct ParameterBound(pub(crate) ganesh::Bound);
#[pymethods]
impl ParameterBound {
/// The lower bound
Expand Down Expand Up @@ -4037,13 +4029,8 @@ pub(crate) mod laddu {
}
}

impl Observer<Float, ()> for crate::python::laddu::PyObserver {
fn callback(
&mut self,
step: usize,
status: &mut ganesh::Status<Float>,
_user_data: &mut (),
) -> bool {
impl Observer<()> for crate::python::laddu::PyObserver {
fn callback(&mut self, step: usize, status: &mut ganesh::Status, _user_data: &mut ()) -> bool {
let (new_status, result) = Python::with_gil(|py| {
let res = self
.0
Expand Down Expand Up @@ -4073,8 +4060,3 @@ impl FromPyObject<'_> for crate::python::laddu::PyObserver {
Ok(crate::python::laddu::PyObserver(ob.clone().into()))
}
}
impl ToPyObject for crate::python::laddu::ParameterBound {
fn to_object(&self, py: Python<'_>) -> PyObject {
PyTuple::new_bound(py, vec![self.0.lower(), self.0.upper()]).into()
}
}

0 comments on commit 3f2d67a

Please sign in to comment.