pub(crate) mod cost;
pub(crate) mod dim;
pub(crate) mod error;
pub(crate) mod predict;
#[cfg(feature = "python")]
mod python;
use std::num::NonZero;
pub use cost::SegmentCostFunction;
#[doc(hidden)]
pub use cost::l2::{L2Cost1D, L2Cost2D};
pub use dim::OneOrTwoDimensions;
pub use error::Error;
use ndarray::{AsArray, Dimension};
use predict::PredictImpl;
#[derive(Debug, Clone)]
pub struct Pelt {
segment_cost_function: SegmentCostFunction,
jump: usize,
minimum_segment_length: usize,
}
impl Pelt {
#[must_use]
pub const fn new() -> Self {
Self {
segment_cost_function: SegmentCostFunction::L1,
jump: 5,
minimum_segment_length: 2,
}
}
#[must_use]
pub const fn with_segment_cost_function(mut self, model: SegmentCostFunction) -> Self {
self.segment_cost_function = model;
self
}
#[must_use]
pub const fn with_jump(mut self, jump: NonZero<usize>) -> Self {
self.jump = jump.get();
self
}
#[must_use]
pub const fn with_minimum_segment_length(
mut self,
minimum_segment_length: NonZero<usize>,
) -> Self {
self.minimum_segment_length = minimum_segment_length.get();
self
}
pub fn predict<'a, D>(
&self,
signal: impl AsArray<'a, f64, D>,
penalty: f64,
) -> Result<Vec<usize>, Error>
where
D: OneOrTwoDimensions + Dimension,
D::PrecalculationOutput: Sync,
{
let signal_view = signal.into();
D::try_as_1d(&signal_view).map_or_else(
|| PredictImpl::new(self.clone()).predict(&signal_view, penalty),
|signal_1d| PredictImpl::new(self.clone()).predict(&signal_1d, penalty),
)
}
}
impl Default for Pelt {
fn default() -> Self {
Self::new()
}
}