use crate::complex::{WeightedEdge, WeightedGraph};
use crate::filtration::Coverage;
use crate::geometry::distance::{cutoff, nonnegative};
use crate::geometry::{DissimilarityMatrixView, PointCloudView, euclidean_distance};
use crate::{Error, Result};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RipsInputKind {
Dissimilarities,
Euclidean,
Custom,
}
#[derive(Clone, Debug)]
pub struct ThresholdRips {
graph: WeightedGraph,
coverage: Coverage,
kind: RipsInputKind,
requested_cutoff: Option<f64>,
}
impl ThresholdRips {
pub fn graph(&self) -> &WeightedGraph {
&self.graph
}
pub fn coverage(&self) -> Coverage {
self.coverage
}
pub fn input_kind(&self) -> RipsInputKind {
self.kind
}
pub fn requested_cutoff(&self) -> Option<f64> {
self.requested_cutoff
}
}
pub fn threshold_rips_from_distances(
input: DissimilarityMatrixView<'_>,
max_edge: Option<f64>,
) -> Result<ThresholdRips> {
build(
input.len(),
max_edge,
RipsInputKind::Dissimilarities,
|a, b| Ok(input.get(a, b).unwrap()),
)
}
pub fn threshold_rips_from_points(
input: PointCloudView<'_>,
max_edge: Option<f64>,
) -> Result<ThresholdRips> {
build(input.len(), max_edge, RipsInputKind::Euclidean, |a, b| {
euclidean_distance(input, a, b)
})
}
pub fn threshold_rips_with_distance<P>(
points: &[P],
max_edge: Option<f64>,
mut distance: impl FnMut(&P, &P) -> Result<f64>,
) -> Result<ThresholdRips> {
build(points.len(), max_edge, RipsInputKind::Custom, |a, b| {
distance(&points[a], &points[b])
})
}
fn build(
n: usize,
max_edge: Option<f64>,
kind: RipsInputKind,
mut distance: impl FnMut(usize, usize) -> Result<f64>,
) -> Result<ThresholdRips> {
build_with(n, max_edge, kind, &mut distance, &mut || Ok(()))
}
pub(super) fn build_with(
n: usize,
max_edge: Option<f64>,
kind: RipsInputKind,
mut distance: impl FnMut(usize, usize) -> Result<f64>,
checkpoint: &mut impl FnMut() -> Result<()>,
) -> Result<ThresholdRips> {
let max_edge = cutoff(max_edge)?;
n.checked_add(1).ok_or(Error::SizeOverflow {
operation: "graph offsets",
})?;
checkpoint()?;
let mut diameter: f64 = 0.0;
let mut edges = Vec::new();
for b in 0..n {
for a in 0..b {
checkpoint()?;
let value = nonnegative(distance(a, b)?, "distance callback", None)?;
checkpoint()?;
diameter = diameter.max(value);
if max_edge.is_none_or(|t| value <= t) {
edges.try_reserve(1).map_err(|_| Error::AllocationFailed {
context: "threshold graph edges",
})?;
edges.push(WeightedEdge {
vertices: [a, b],
value,
});
}
}
}
let coverage = match max_edge {
Some(t) if t < diameter => Coverage::Through(t),
_ => Coverage::Complete,
};
checkpoint()?;
let graph = WeightedGraph::new(n, edges)?;
checkpoint()?;
Ok(ThresholdRips {
graph,
coverage,
kind,
requested_cutoff: max_edge,
})
}
pub(crate) fn cone_radius(
input: DissimilarityMatrixView<'_>,
checkpoint: &mut impl FnMut() -> Result<()>,
) -> Result<f64> {
let mut radius: f64 = input.diameter();
for a in 0..input.len() {
let mut row: f64 = 0.0;
for b in 0..input.len() {
checkpoint()?;
row = row.max(input.get(a, b).unwrap());
}
radius = radius.min(row);
}
Ok(radius)
}