~mht/cra

933b27350404d77d37a8f2b73e9ae8bc2139e4a5 — Martin Hafskjold Thoresen 5 months ago 175154e
Fix warnings and remove some dead code
3 files changed, 34 insertions(+), 50 deletions(-)

M cra/src/log.rs
M cra/src/main.rs
M cra/src/output.rs
M cra/src/log.rs => cra/src/log.rs +6 -7
@@ 1,7 1,6 @@
//! This module contains a log of all actions that happens during the run of the algorithms.
//! It is meant to be used by constructing an `Entry`, and operate on it during the course of the algorithm.

use std::io::Write;
use std::collections::HashMap;

use crate::serde::Serialize;


@@ 97,7 96,7 @@ impl Reduction {
        let mut add_size_by_dim = HashMap::<isize, Vec<(usize, usize)>>::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            let mut v = add_size_by_dim.entry(r.dim).or_insert(Vec::new());
            let v = add_size_by_dim.entry(r.dim).or_insert(Vec::new());
            for a in r.adds.iter() {
                v.push((a.this_size, a.other_size));
            }


@@ 106,15 105,15 @@ impl Reduction {
        let mut avg_add_size_by_dim = HashMap::new();
        for (&dim, adds) in add_size_by_dim.iter() {
            if adds.len() == 0 { continue; }
            let avg_f = adds.iter().map(|(f, s)| *f).sum::<usize>() as f64 / adds.len() as f64;
            let avg_s = adds.iter().map(|(f, s)| *s).sum::<usize>() as f64 / adds.len() as f64;
            let avg_f = adds.iter().map(|(f, _s)| *f).sum::<usize>() as f64 / adds.len() as f64;
            let avg_s = adds.iter().map(|(_f, s)| *s).sum::<usize>() as f64 / adds.len() as f64;
            avg_add_size_by_dim.insert(dim, (avg_f, avg_s));
       	}

       	let mut histogram_add_size_by_dim = HashMap::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            let mut histogram: &mut HashMap<usize, (usize, usize)> = histogram_add_size_by_dim.entry(r.dim).or_insert(HashMap::new());
            let histogram: &mut HashMap<usize, (usize, usize)> = histogram_add_size_by_dim.entry(r.dim).or_insert(HashMap::new());
            for add in r.adds.iter() {
                histogram.entry(add.this_size).or_insert((0, 0)).0 += 1;
                histogram.entry(add.other_size).or_insert((0, 0)).1 += 1;


@@ 124,7 123,7 @@ impl Reduction {
        let mut add_cost_by_dim = HashMap::<isize, Vec<usize>>::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            let mut v = add_cost_by_dim.entry(r.dim).or_insert(Vec::new());
            let v = add_cost_by_dim.entry(r.dim).or_insert(Vec::new());
            for a in r.adds.iter() {
                v.push(a.this_size + a.other_size);
            }


@@ 140,7 139,7 @@ impl Reduction {
       	let mut histogram_add_cost_by_dim = HashMap::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            let mut histogram: &mut HashMap<usize, usize> = histogram_add_cost_by_dim.entry(r.dim).or_insert(HashMap::new());
            let histogram: &mut HashMap<usize, usize> = histogram_add_cost_by_dim.entry(r.dim).or_insert(HashMap::new());
            for add in r.adds.iter() {
                *histogram.entry(add.this_size + add.other_size).or_insert(0) += 1;
            }

M cra/src/main.rs => cra/src/main.rs +5 -19
@@ 299,7 299,6 @@ fn main() {
        (@arg stats: -s --stats[FILE] "Print out statistics.")
        (@arg graphviz: --graphviz[FILE] "Use `graphviz` to graph out which simplices are added together.")
        (@arg diagram: --diagram[FILE] "Use `gnuplot` to print out the persistence diagram.")
        (@arg textable: --("tex-table")[FILE] "Output a `tex` formatted table of statistics.")
        (@arg points: --points[FILE] "Input the location data for the 1-simplices.")
        (@arg debug: -d "Print debug information to `stdout`")
    ).arg(Arg::from_usage(


@@ 347,17 346,6 @@ the first boundaries in the list should be that of the 1-simplices.\n "),
    };
    let reduction = reduce(&persistence, variant);



	// println!("{:?}", reduction);
	let aggr = reduction.log.aggregate();
    // println!("{:?}", aggr);
    println!("{}", serde_json::to_string(&aggr).unwrap());
    return;




    if debug {
        let mut out = std::io::stdout();
        reduction


@@ 367,17 355,15 @@ the first boundaries in the list should be that of the 1-simplices.\n "),

    if let Some(path) = matches.value_of("stats") {
        let mut stats_file = open_file_or_stdout(path).unwrap();
        write!(stats_file, "# STATISTICS\n").unwrap();
        // TODO: FIXME
        // output::stats(&stats, &mut stats_file).unwrap();
        write!(stats_file, "# STATISTICS end\n").unwrap();
        let json = serde_json::to_string(&reduction.log.aggregate()).expect("failed to convert stats to .json");
        write!(stats_file, "{}", json).expect("failed to write stats to file");
    }

    if let Some(path) = matches.value_of("textable") {
        let mut tex_file = File::create(Path::new(path)).unwrap();
    // if let Some(path) = matches.value_of("textable") {
        // let mut tex_file = File::create(Path::new(path)).unwrap();
        // TODO: FIXME
        // output::tex_table(&stats, &stats, &mut tex_file).unwrap();
    }
    // }

    if let Some(path) = matches.value_of("graphviz") {
        let mut f = File::create(Path::new(path)).unwrap();

M cra/src/output.rs => cra/src/output.rs +23 -24
@@ 1,4 1,3 @@
use std::fmt::Write as FmtWrite;
use std::fs::File;
use std::io::Write;
use std::path::Path;


@@ 14,29 13,29 @@ use persistence::{Persistence, Reduction};
/// ```
/// assert_eq!(thousand_split(1234567), "1_234_567");
/// ```
fn thousand_split(mut num: usize) -> String {
    let mut out = String::new();
    let mut nums = vec![];
    while num > 0 {
        nums.push(num % 1_000);
        num /= 1_000;
    }
    for i in (1..nums.len()).rev() {
        if i == nums.len() - 1 {
            write!(out, "{},", nums[i]).unwrap();
        } else {
            write!(out, "{:03},", nums[i]).unwrap();
        }
    }
    if nums.len() == 0 {
        write!(out, "0").unwrap();
    } else if nums.len() == 1 {
        write!(out, "{}", nums[0]).unwrap();
    } else {
        write!(out, "{:03}", nums[0]).unwrap();
    }
    out
}
// fn thousand_split(mut num: usize) -> String {
//     let mut out = String::new();
//     let mut nums = vec![];
//     while num > 0 {
//         nums.push(num % 1_000);
//         num /= 1_000;
//     }
//     for i in (1..nums.len()).rev() {
//         if i == nums.len() - 1 {
//             write!(out, "{},", nums[i]).unwrap();
//         } else {
//             write!(out, "{:03},", nums[i]).unwrap();
//         }
//     }
//     if nums.len() == 0 {
//         write!(out, "0").unwrap();
//     } else if nums.len() == 1 {
//         write!(out, "{}", nums[0]).unwrap();
//     } else {
//         write!(out, "{:03}", nums[0]).unwrap();
//     }
//     out
// }

/// Write an `svg` file to the file `out_path` showing the simplicial complex.
///