~mht/cra

a3f4797faa2ae9132d5c562127285d8c56e01149 — Martin Hafskjold Thoresen 2 years ago 7e7ed3d
Output all the pdfs
1 files changed, 77 insertions(+), 89 deletions(-)

M cra/src/main.rs
M cra/src/main.rs => cra/src/main.rs +77 -89
@@ 444,8 444,9 @@ pub fn reduce(p: &Persistence, exhaustive: bool, stats: &mut Statistics) -> Vec<
                        // from the back and check if there is a simplex with that low.
                        let mut iter = 0;
                        'search: loop {
                            let list_len = simplex_with_low[low].get().len();
                            for (iter_i, face_i) in
                                (0..simplex_with_low[low].get().len()).rev().enumerate()
                                (0..list_len).rev().enumerate()
                            {
                                let this_index = simplex_with_low[low].get().0[face_i];
                                let other = &simplex_with_low[this_index];


@@ 464,6 465,7 @@ pub fn reduce(p: &Persistence, exhaustive: bool, stats: &mut Statistics) -> Vec<
                                // if ours.is_empty() { break; }
                                continue 'search;
                            }
                            stats.ex_search(list_len);
                            stats.ex_reductions(iter);
                            break;
                        }


@@ 757,10 759,22 @@ fn main() {
    e_stats.eprint_avg();
    eprintln!("\n");

    // output_histogram(&r_stats.num_iters, "Iters for reducing one column");

    output_2histogram(&r_stats.add_iters, &e_stats.add_iters, "Iters for reducing one column");
    // output_2histogram( &r_stats.add_size_sum, &e_stats.add_size_sum, "Cost estimate for column addition");
    output_histogram(&e_stats.ex_searches,
                     "Iters for finding k=low(i) for any i",
                     "ex_searches.pdf");
    output_histogram(&e_stats.ex_reductions,
                     "Iters for exhaustively reducing a column",
                     "ex_reductions.pdf");

    output_2histogram(&r_stats.add_iters, &e_stats.add_iters, 
                      "Loop iterations in 'xor'",
                      "add_iters.pdf");
    output_2histogram(&r_stats.add_size_sum, &e_stats.add_size_sum,
                      "Cost estimate for column addition",
                      "add_size_sum.pdf");
    output_2histogram(&r_stats.num_iters, &e_stats.num_iters,
                      "Number of iterations for reducing a column",
                      "num_iters.pdf");

    // eprintln!("{:#?}", persistence.simplices);
    // eprintln!("{:?}", pairings);


@@ 929,26 943,52 @@ fn output_svg(persistence: &Persistence) {
    f.write_all(b"</svg>").unwrap();
}

fn output_histogram(data: &[usize], label: &str) {
// Run `gnuplot` with the given script and out file for output.
fn run_gnuplot(script: &str, out_file: &str) {
    let mut gnuplot = Command::new("gnuplot")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Failed to run `gnuplot`");
    {
        let stdin = gnuplot
            .stdin
            .as_mut()
            .expect("Failed to open stdin for `gnuplot`");
        stdin
            .write_all(script.as_bytes())
            .expect("Failed to write to stdin");
    }
    let output = gnuplot
        .wait_with_output()
        .expect("`wait_for_output` failed!");
    match output.status.code() {
        Some(0) => {}
        other => panic!("`gnuplot` process returned error: {:?}", other),
    }

    let mut f = File::create(out_file).unwrap();
    f.write_all(&output.stdout).unwrap();
}

fn histogram(data: &[usize]) -> Vec<usize> {
    if data.len() == 0 {
        panic!("output_histogram: `data` cannot be empty!");
        panic!("Trying to create histogram of empty data set");
    }
    let max = data.iter().max().unwrap();
    let mut freq = iter::repeat(0).take(max + 1).collect::<Vec<_>>();
    for &d in data.iter() {
        freq[d] += 1;
    }
    freq
}

    let mut data: Vec<usize> = data.iter().cloned().collect();
    data.sort();
fn output_histogram(data: &[usize], label: &str, out_file: &str) {
    if data.len() == 0 {
        panic!("output_histogram: `data` cannot be empty!");
    }

    let frequency = {
        let min = data[0];
        let max = data[data.len() - 1];
        let range = 1 + max - min;
        let mut freq = std::iter::repeat(0).take(range).collect::<Vec<_>>();
        for d in &data {
            freq[d - min] += 1;
        }
        freq
    };
    use std::fs::File;
    use std::process::{Command, Stdio};
    let frequency = histogram(data);

    let mut file = File::create("kek.freq").unwrap();
    for (ind, freq) in frequency.iter().enumerate() {


@@ 957,59 997,30 @@ fn output_histogram(data: &[usize], label: &str) {

    let plot_script = format!(
        r#"
stats 'kek.freq' 
set grid
set terminal pdf size 12cm,10cm
set logscale y
# set xtics out offset 1 1
set xtics out
set ytics out
set xrange [-0.5:STATS_max_x + 0.5]
set xlabel "{}"
set ylabel "Frequency"
set style fill solid 0.7
plot 'kek.freq' using ($1 + 0.5):($2) with boxes lc rgb"black" notitle
set style fill solid 1.0 noborder
set boxwidth 1.1
plot 'kek.freq' using ($1):($2 + 1) with boxes lc rgb"gray40" notitle
"#,
        label
    );
    //
    //
    let mut gnuplot = Command::new("gnuplot")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Failed to run `gnuplot`");
    {
        let stdin = gnuplot
            .stdin
            .as_mut()
            .expect("Failed to open stdin for `gnuplot`");
        stdin
            .write_all(plot_script.as_bytes())
            .expect("Failed to write to stdin");
    }
    let output = gnuplot
        .wait_with_output()
        .expect("`wait_for_output` failed!");
    eprintln!("{:?}", output.status);
    eprintln!("{:?}", output.stderr);

    let mut f = File::create("kek-hist.pdf").unwrap();
    f.write_all(&output.stdout).unwrap();
    run_gnuplot(&plot_script, out_file);
}

fn output_2histogram(data_a: &[usize], data_b: &[usize], label: &str) {
fn output_2histogram(data_a: &[usize], data_b: &[usize], label: &str, out_file: &str) {
    if data_a.len() == 0 || data_b.len() == 0 {
        panic!("output_histogram: `data` cannot be empty!");
    }

    fn histogram(data: &[usize]) -> Vec<usize> {
        if data.len() == 0 {
            panic!("Trying to create histogram of empty data set");
        }
        let max = data.iter().max().unwrap();
        let mut freq = iter::repeat(0).take(max + 1).collect::<Vec<_>>();
        for &d in data.iter() {
            freq[d] += 1;
        }
        freq
    }

    let a_hist = histogram(data_a);
    let b_hist = histogram(data_b);



@@ 1021,45 1032,22 @@ fn output_2histogram(data_a: &[usize], data_b: &[usize], label: &str) {

    let plot_script = format!(
        r#"
set terminal pdf size 22cm,20cm
set terminal pdf size 18cm,14cm
set logscale y
set xtics out
set ytics out
set xrange [-1:]
set xlabel "{}"
set ylabel "Frequency"
set boxwidth 0.4
set boxwidth 0.5
set style fill solid 1.0 noborder
plot 'kek.freq' using ($0 - 0.2):($1) with boxes lc rgb"gray20" title "Regular",\
     'kek.freq' using ($0 + 0.2):($2) with boxes lc rgb"gray50" title "Exhaustive"
plot 'kek.freq' using ($0 - 0.25):($1+1) with boxes lc rgb"gray20" title "Regular",\
     'kek.freq' using ($0 + 0.25):($2+1) with boxes lc rgb"0x88bb88" title "Exhaustive"
"#,
        label
    );
    //
    //
    let mut gnuplot = Command::new("gnuplot")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Failed to run `gnuplot`");
    {
        let stdin = gnuplot
            .stdin
            .as_mut()
            .expect("Failed to open stdin for `gnuplot`");
        stdin
            .write_all(plot_script.as_bytes())
            .expect("Failed to write to stdin");
    }
    let output = gnuplot
        .wait_with_output()
        .expect("`wait_for_output` failed!");
    match output.status.code() {
        Some(0) => {}
        other => panic!("`gnuplot` process returned error: {:?}", other),
    }

    let mut f = File::create("kek-hist.pdf").unwrap();
    f.write_all(&output.stdout).unwrap();
    run_gnuplot(&plot_script, out_file);
}

#[cfg(test)]