Skip to content

Commit

Permalink
add option to run multiple experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
jirigav committed Sep 29, 2023
1 parent cd80849 commit 7798836
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/data/*
config
4 changes: 4 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub(crate) struct Args {
/// Option whether histogram should be used as an alternative evaluation method.
#[arg(long)]
pub(crate) hist: bool,

/// Option whether histogram should be used as an alternative evaluation method.
#[arg(long, short)]
pub(crate) config: bool,
}

pub(crate) fn bit_value_in_block(bit: usize, block: &[u8]) -> bool {
Expand Down
70 changes: 55 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::distinguishers::{

use clap::Parser;
use common::{prepare_data, Data};
use itertools::Itertools;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::time::Instant;

fn print_results(p_value: f64, z_score: f64) {
Expand All @@ -24,44 +27,51 @@ fn results(
testing_data: &Data,
patterns_combined: usize,
hist: bool,
) {
) -> (f64, f64) {
final_patterns.sort_by(|a, b| {
f64::abs(b.z_score.unwrap())
.partial_cmp(&f64::abs(a.z_score.unwrap()))
.unwrap()
});
let mut best_mp = best_multi_pattern(training_data, &final_patterns, patterns_combined);

println!("trained in {:.2?}", start.elapsed());

println!("training z-score: {}", best_mp.z_score.unwrap());
println!("best multi-pattern: {best_mp:?}");

if hist {
hist_result(final_patterns, training_data, testing_data);
hist_result(final_patterns, training_data, testing_data, start)
} else {
let mut best_mp = best_multi_pattern(training_data, &final_patterns, patterns_combined);

println!("trained in {:.2?}", start.elapsed());

println!("training z-score: {}", best_mp.z_score.unwrap());
println!("best multi-pattern: {best_mp:?}");
let z_score = evaluate_distinguisher(&mut best_mp, testing_data);
let p_value = p_value(
best_mp.get_count(),
testing_data.num_of_blocks,
best_mp.probability,
);
print_results(p_value, z_score);
(p_value, z_score)
}
}

fn hist_result(final_patterns: Vec<Pattern>, training_data: &Data, testing_data: &Data) {
fn hist_result(
final_patterns: Vec<Pattern>,
training_data: &Data,
testing_data: &Data,
start: Instant,
) -> (f64, f64) {
let bits = final_patterns[0].bits.clone();

println!("number of bits: {}", bits.len());

if bits.len() > 20 {
println!("Too many bits in pattern, can't produce hist result.");
return;
return (1.0, 0.0);
}

let hist = Histogram::get_hist(&bits, training_data);

println!("trained in {:.2?}", start.elapsed());
println!("training z-score: {}", hist.z_score);

let count = hist.evaluate(testing_data);
Expand All @@ -71,9 +81,10 @@ fn hist_result(final_patterns: Vec<Pattern>, training_data: &Data, testing_data:
let p_val = p_value(count, testing_data.num_of_blocks, prob);

print_results(p_val, z);
(p_val, z)
}

fn run_bottomup(args: Args) {
fn run_bottomup(args: Args) -> (f64, f64) {
let s = Instant::now();
let (training_data, validation_data_option, testing_data_option) = prepare_data(
&args.data_source,
Expand All @@ -92,12 +103,41 @@ fn run_bottomup(args: Args) {
&testing_data_option.unwrap(),
args.patterns_combined,
args.hist,
);
)
}

fn parse_args(s: Vec<&str>) -> Args {
Args {
data_source: s[0].to_string(),
block_size: s[1].trim().parse().unwrap(),
k: s[2].trim().parse().unwrap(),
min_difference: s[3].trim().parse().unwrap(),
patterns_combined: s[4].trim().parse().unwrap(),
base_pattern_size: s[5].trim().parse().unwrap(),
validation_and_testing_split: s[6].trim().parse().unwrap(),
hist: s[7].trim().parse().unwrap(),
config: false,
}
}

fn main() {
let args = Args::parse();
println!("\n{args:?}\n");

run_bottomup(args);
if args.config {
let file = File::open(args.data_source).unwrap();

let reader = BufReader::new(file);
let mut results = Vec::new();
for line in reader.lines() {
let l = line.unwrap();
println!("config: {l}");
let splitted = l.split(',').collect_vec();
let args = parse_args(splitted);
results.push(run_bottomup(args));
println!();
}
println!("{results:?}");
} else {
println!("\n{args:?}\n");
run_bottomup(args);
}
}

0 comments on commit 7798836

Please sign in to comment.