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

Add __repr__ for pyaugurs structs #21

Merged
merged 1 commit into from
Jun 15, 2023
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
10 changes: 10 additions & 0 deletions crates/augurs-ets/src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ impl AutoETS {
}
}

/// Get the season length of the model.
pub fn season_length(&self) -> usize {
self.season_length
}

/// Get the search specification.
pub fn spec(&self) -> AutoSpec {
self.spec
}

/// Create a new `AutoETS` model search without any seasonal components.
///
/// Equivalent to `AutoETS::new(1, "ZZN")`.
Expand Down
8 changes: 8 additions & 0 deletions crates/pyaugurs/src/ets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ impl AutoETS {
Ok(Self { inner })
}

fn __repr__(&self) -> String {
format!(
"AutoETS(spec=\"{}\", season_length={})",
self.inner.spec(),
self.inner.season_length()
)
}

/// Search for the best model, fitting it to the data.
///
/// The model will be stored on the inner `AutoETS` instance, after which
Expand Down
12 changes: 11 additions & 1 deletion crates/pyaugurs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl From<Forecast> for augurs_core::Forecast {
}
}

// TODO: __repr__, etc.
#[pymethods]
impl Forecast {
#[new]
Expand Down Expand Up @@ -66,6 +65,17 @@ impl Forecast {
})
}

fn __repr__(&self) -> String {
let intervals = self.inner.intervals.as_ref();
format!(
"Forecast(point={:?}, level={:?}, lower={:?}, upper={:?})",
self.inner.point,
intervals.map(|x| x.level),
intervals.map(|x| &x.lower),
intervals.map(|x| &x.upper)
)
}

/// Get the point forecast.
fn point(&self, py: Python<'_>) -> Py<PyArray1<f64>> {
// Use `to_pyarray` to allocate a new array on the Python heap.
Expand Down
4 changes: 4 additions & 0 deletions crates/pyaugurs/src/trend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct PyTrendModel {

#[pymethods]
impl PyTrendModel {
fn __repr__(&self) -> String {
format!("PyTrendModel(model=\"{}\")", self.name())
}

/// Wrap a trend model implemented in Python into a PyTrendModel.
///
/// The returned PyTrendModel can be used in MSTL models using the
Expand Down