~mht/cra

50ab1601203ef5044b9fcf3eb7a01525c52a074c — Martin Hafskjold Thoresen 8 months ago 71377f3
Add simplex count output in Aggregate
2 files changed, 25 insertions(+), 11 deletions(-)

M src/log.rs
M src/persistence.rs
M src/log.rs => src/log.rs +19 -5
@@ 5,7 5,7 @@ use std::collections::HashMap;

use crate::serde::Serialize;

use crate::persistence::Simplex;
use crate::persistence::{Simplex, Persistence};

/// Stats for a single `simplex_reduce` call.
#[derive(Debug, Copy, Clone)]


@@ 36,9 36,9 @@ pub struct SimplexReduction {
}

/// Stats for a whole run of the reduction algorithm.
#[derive(Debug)]
pub struct Reduction {
pub struct Reduction<'a> {
    pub reductions: Vec<SimplexReduction>,
    pub persistence: &'a Persistence,
}

/// This is the full data that we output.


@@ 63,12 63,15 @@ pub struct Aggregate {
    add_cost_by_dim: HashMap<isize, Vec<usize>>,
    avg_add_cost_by_dim: HashMap<isize, f64>,
    histogram_add_cost_by_dim: HashMap<isize, HashMap<usize, usize>>,

    num_simplices: HashMap<isize, usize>,
}

impl Reduction {
    pub fn new() -> Self {
impl<'a> Reduction<'a> {
    pub fn new(persistence: &'a Persistence) -> Self {
        Self {
            reductions: Vec::new(),
            persistence
        }
    }



@@ 77,6 80,16 @@ impl Reduction {
    }

    pub fn aggregate(&self) -> Aggregate {
        let mut simplices2dim = HashMap::new();
        for s in &self.persistence.simplices {
            simplices2dim.insert(s.j, s.dim);
        }

        let mut num_simplices = HashMap::new();
        for (&j, &dim) in simplices2dim.iter() {
            *num_simplices.entry(dim).or_insert(0) += 1;
        }

        let mut adds = Vec::new();
        for r in self.reductions.iter() {
            for a in r.adds.iter() {


@@ 174,6 187,7 @@ impl Reduction {
            add_cost_by_dim,
            avg_add_cost_by_dim,
            histogram_add_cost_by_dim,
            num_simplices,
        }
    }
}

M src/persistence.rs => src/persistence.rs +6 -6
@@ 52,8 52,7 @@ pub struct Persistence {
}

/// The result from reducing.
#[derive(Debug)]
pub struct Reduction {
pub struct Reduction<'a> {
    /// When we reduce a simplex we might finding that it's `low` is unique (that `simplex_with_low[low] == None`).
    /// We then store the reduced simplex in that slot. This corresponds to pairing `low` with `j`.
    pub pairings: Vec<(usize, usize)>,


@@ 63,10 62,10 @@ pub struct Reduction {
    /// This was probably used for the output?
    pub additions: Vec<(usize, usize)>,

    pub log: log::Reduction,
    pub log: log::Reduction<'a>,
}

impl Reduction {
impl<'a> Reduction<'a> {
    /// Write debug output for this reduction to `w`. This includes how each simplex was reduced.
    pub fn debug_output<W: Write>(
        &self,


@@ 89,7 88,8 @@ impl Reduction {
        for s in simplices {
            let empty_vec = Vec::new();
            let j = s.j;
            let initial_faces = &s.faces;
            let mut initial_faces = s.faces.clone();
            initial_faces.sort();
            let reduced_faces = if let Some(&place) = killed.get(&j) {
                let v = self.simplex_with_low[place]
                    .as_ref()


@@ 117,7 117,7 @@ pub fn reduce(p: &Persistence, variant: Variant) -> Reduction {
    let mut additions = Vec::new();
    let mut reduced_with_low: Vec<Option<Simplex>> = vec![None; p.simplices.len()];

    let mut reduction_log = log::Reduction::new();
    let mut reduction_log = log::Reduction::new(p);

    // We're reducing each simplex.
    for simplex in &p.simplices {