use crate::{Error, Result, canonical_zero};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum IntervalEnd {
Finite(f64),
Essential,
RightCensored {
through: f64,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PersistenceInterval {
dimension: usize,
birth: f64,
end: IntervalEnd,
}
impl PersistenceInterval {
pub fn new(dimension: usize, birth: f64, end: IntervalEnd) -> Result<Self> {
let birth = finite_scale(birth, "birth")?;
let end = match end {
IntervalEnd::Finite(death) => {
let death = finite_scale(death, "death")?;
if death <= birth {
return Err(Error::InvalidInterval {
reason: "finite death must be strictly after birth",
});
}
IntervalEnd::Finite(death)
}
IntervalEnd::Essential => IntervalEnd::Essential,
IntervalEnd::RightCensored { through } => {
let through = finite_scale(through, "through")?;
if through < birth {
return Err(Error::InvalidInterval {
reason: "censoring cutoff must be at or after birth",
});
}
IntervalEnd::RightCensored { through }
}
};
Ok(Self {
dimension,
birth,
end,
})
}
pub fn dimension(&self) -> usize {
self.dimension
}
pub fn birth(&self) -> f64 {
self.birth
}
pub fn end(&self) -> IntervalEnd {
self.end
}
}
pub(super) fn finite_scale(value: f64, field: &'static str) -> Result<f64> {
if !value.is_finite() {
Err(Error::NonFiniteValue { field, index: None })
} else {
Ok(canonical_zero(value))
}
}