Skip to content

Commit

Permalink
UPG: rust packages upgrades (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
attack68 authored Jan 26, 2025
1 parent 7303710 commit f32f8fc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 56 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ path = "rust/main.rs"
#pyo3 = { version = "0.20.3", features = ["abi3-py39", "extension-module"] }
serde = { version = "1.0", features = ["derive", "rc"] }
chrono = { version = "0.4", features = ["serde"] }
indexmap = { version = "2.0", features = ["serde"] }
indexmap = { version = "2.7", features = ["serde"] }
ndarray = { version = "0.16", features = ["serde"] }
internment = { version = "0.8", features = ["serde"] }
pyo3 = "0.22"
pyo3 = "0.23"
num-traits = "0.2"
auto_ops = "0.3"
numpy = "0.22"
itertools = "0.12"
statrs = "0.16"
numpy = "0.23"
itertools = "0.14"
statrs = "0.18"
bincode = "1.3"

serde_json = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions rust/calendars/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use chrono::prelude::*;
use chrono::Weekday;

Check warning on line 2 in rust/calendars/calendar.rs

View workflow job for this annotation

GitHub Actions / build (3.13)

Diff in /home/runner/work/rateslib/rateslib/rust/calendars/calendar.rs
use indexmap::set::IndexSet;
use pyo3::exceptions::PyValueError;
use pyo3::{pyclass, FromPyObject, PyErr};
use pyo3::{pyclass, FromPyObject, PyErr, IntoPyObject};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

use crate::calendars::dateroll::DateRoll;
use crate::calendars::named::get_calendar_by_name;

/// Container for calendar types.
#[derive(Debug, Clone, PartialEq, FromPyObject, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, FromPyObject, Serialize, Deserialize, IntoPyObject)]
pub enum CalType {
Cal(Cal),
UnionCal(UnionCal),
Expand Down
31 changes: 16 additions & 15 deletions rust/calendars/calendar_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ use pyo3::prelude::*;
use pyo3::types::PyBytes;
use std::collections::HashSet;

impl IntoPy<PyObject> for CalType {
fn into_py(self, py: Python<'_>) -> PyObject {
macro_rules! into_py {
($obj: ident) => {
Py::new(py, $obj).unwrap().to_object(py)
};
}

match self {
CalType::Cal(i) => into_py!(i),
CalType::UnionCal(i) => into_py!(i),
CalType::NamedCal(i) => into_py!(i),
}
}
}
// // removed when upgrading to py03 0.23, see https://pyo3.rs/v0.23.0/migration#intopyobject-and-intopyobjectref-derive-macros
// impl IntoPy<PyObject> for CalType {
// fn into_py(self, py: Python<'_>) -> PyObject {
// macro_rules! into_py {
// ($obj: ident) => {
// Py::new(py, $obj).unwrap().to_object(py)
// };
// }
//
// match self {
// CalType::Cal(i) => into_py!(i),
// CalType::UnionCal(i) => into_py!(i),
// CalType::NamedCal(i) => into_py!(i),
// }
// }
// }

#[pymethods]
impl Convention {
Expand Down
39 changes: 20 additions & 19 deletions rust/curves/curve_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use pyo3::types::PyBytes;
use serde::{Deserialize, Serialize};

/// Interpolation
#[derive(Debug, Clone, PartialEq, FromPyObject, Deserialize, Serialize)]
#[derive(Debug, Clone, PartialEq, FromPyObject, Deserialize, Serialize, IntoPyObject)]
pub(crate) enum CurveInterpolator {
LogLinear(LogLinearInterpolator),
Linear(LinearInterpolator),
Expand All @@ -29,24 +29,25 @@ pub(crate) enum CurveInterpolator {
Null(NullInterpolator),
}

impl IntoPy<PyObject> for CurveInterpolator {
fn into_py(self, py: Python<'_>) -> PyObject {
macro_rules! into_py {
($obj: ident) => {
Py::new(py, $obj).unwrap().to_object(py)
};
}

match self {
CurveInterpolator::LogLinear(i) => into_py!(i),
CurveInterpolator::Linear(i) => into_py!(i),
CurveInterpolator::LinearZeroRate(i) => into_py!(i),
CurveInterpolator::FlatForward(i) => into_py!(i),
CurveInterpolator::FlatBackward(i) => into_py!(i),
CurveInterpolator::Null(i) => into_py!(i),
}
}
}
// // removed upgrading to pyo3 0.23, see https://pyo3.rs/v0.23.0/migration#intopyobject-and-intopyobjectref-derive-macros
// impl IntoPy<PyObject> for CurveInterpolator {
// fn into_py(self, py: Python<'_>) -> PyObject {
// macro_rules! into_py {
// ($obj: ident) => {
// Py::new(py, $obj).unwrap().to_object(py)
// };
// }
//
// match self {
// CurveInterpolator::LogLinear(i) => into_py!(i),
// CurveInterpolator::Linear(i) => into_py!(i),
// CurveInterpolator::LinearZeroRate(i) => into_py!(i),
// CurveInterpolator::FlatForward(i) => into_py!(i),
// CurveInterpolator::FlatBackward(i) => into_py!(i),
// CurveInterpolator::Null(i) => into_py!(i),
// }
// }
// }

impl CurveInterpolation for CurveInterpolator {
fn interpolated_value(&self, nodes: &NodesTimestamp, date: &NaiveDateTime) -> Number {
Expand Down
31 changes: 18 additions & 13 deletions rust/dual/dual_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use numpy::{Element, PyArray1, PyArray2, PyArrayDescr, ToPyArray};

unsafe impl Element for Dual {
const IS_COPY: bool = false;
fn get_dtype_bound(py: Python<'_>) -> Bound<'_, PyArrayDescr> {
fn get_dtype(py: Python<'_>) -> Bound<'_, PyArrayDescr> {
PyArrayDescr::object_bound(py)
}

Expand All @@ -26,7 +26,7 @@ unsafe impl Element for Dual {
}
unsafe impl Element for Dual2 {
const IS_COPY: bool = false;
fn get_dtype_bound(py: Python<'_>) -> Bound<'_, PyArrayDescr> {
fn get_dtype(py: Python<'_>) -> Bound<'_, PyArrayDescr> {
PyArrayDescr::object_bound(py)
}

Expand All @@ -35,15 +35,16 @@ unsafe impl Element for Dual2 {
}
}

impl IntoPy<PyObject> for Number {
fn into_py(self, py: Python<'_>) -> PyObject {
match self {
Number::F64(f) => PyFloat::new_bound(py, f).to_object(py),
Number::Dual(d) => Py::new(py, d).unwrap().to_object(py),
Number::Dual2(d) => Py::new(py, d).unwrap().to_object(py),
}
}
}
// This was removed when upgrading to pyO3 0.23: see https://pyo3.rs/v0.23.0/migration#intopyobject-and-intopyobjectref-derive-macros
// impl IntoPy<PyObject> for Number {
// fn into_py(self, py: Python<'_>) -> PyObject {
// match self {
// Number::F64(f) => PyFloat::new_bound(py, f).to_object(py),
// Number::Dual(d) => Py::new(py, d).unwrap().to_object(py),
// Number::Dual2(d) => Py::new(py, d).unwrap().to_object(py),
// }
// }
// }

// https://github.com/PyO3/pyo3/discussions/3911
// #[derive(Debug, Clone, PartialEq, PartialOrd, FromPyObject)]
Expand Down Expand Up @@ -151,7 +152,7 @@ impl Dual {

#[getter]
#[pyo3(name = "dual2")]
fn dual2_py<'py>(&'py self, _py: Python<'py>) -> PyResult<&'py PyArray2<f64>> {
fn dual2_py<'py>(&'py self, _py: Python<'py>) -> PyResult<Bound<'py, PyArray2<f64>>> {
Err(PyValueError::new_err(
"`Dual` variable cannot possess `dual2` attribute.",
))
Expand All @@ -167,7 +168,11 @@ impl Dual {
}

#[pyo3(name = "grad2")]
fn grad2<'py>(&'py self, _py: Python<'py>, _vars: Vec<String>) -> PyResult<&'py PyArray2<f64>> {
fn grad2<'py>(

Check warning on line 171 in rust/dual/dual_py.rs

View workflow job for this annotation

GitHub Actions / build (3.13)

Diff in /home/runner/work/rateslib/rateslib/rust/dual/dual_py.rs
&'py self,
_py: Python<'py>,
_vars: Vec<String>
) -> PyResult<Bound<'py, PyArray2<f64>>> {
Err(PyValueError::new_err(
"Cannot evaluate second order derivative on a Dual.",
))
Expand Down
4 changes: 2 additions & 2 deletions rust/dual/enums.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dual::{Dual, Dual2};

Check warning on line 1 in rust/dual/enums.rs

View workflow job for this annotation

GitHub Actions / build (3.13)

Diff in /home/runner/work/rateslib/rateslib/rust/dual/enums.rs
use crate::splines::{PPSplineDual, PPSplineDual2, PPSplineF64};
use ndarray::{Array1, Array2};
use pyo3::{pyclass, FromPyObject, PyErr};
use pyo3::{pyclass, FromPyObject, PyErr, IntoPyObject};
use serde::{Deserialize, Serialize};

/// Defines the order of gradients available in a calculation with AD.
Expand All @@ -17,7 +17,7 @@ pub enum ADOrder {
}

/// Container for the three core numeric types; [f64], [Dual] and [Dual2].
#[derive(Debug, Clone, FromPyObject, Serialize, Deserialize)]
#[derive(Debug, Clone, FromPyObject, Serialize, Deserialize, IntoPyObject)]
pub enum Number {
Dual(Dual),
Dual2(Dual2),
Expand Down

0 comments on commit f32f8fc

Please sign in to comment.