-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jirigav/automated_tests
Automated tests
- Loading branch information
Showing
7 changed files
with
184 additions
and
94 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "cooltest" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
edition = "2021" | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::bottomup::bottomup; | ||
use crate::common::{prepare_data, Args}; | ||
use crate::results::results; | ||
use std::time::Instant; | ||
|
||
const GB: usize = 1000000000; | ||
const MB: usize = 1000000; | ||
|
||
fn choose_k(block_size: usize, data_size: usize) -> usize { | ||
if data_size <= 10 * MB && block_size < 128 { | ||
4 | ||
} else if data_size < 2 * GB && block_size < 256 { | ||
3 | ||
} else { | ||
2 | ||
} | ||
} | ||
|
||
pub(crate) fn autotest(mut args: Args) { | ||
let (training_data, testing_data) = prepare_data(&args.data_source, args.block, true); | ||
let mut testing_data = testing_data.unwrap(); | ||
let mut tested_cases = 0; | ||
let start = Instant::now(); | ||
let data_size = training_data.len(); | ||
|
||
let mut k = choose_k(args.block, data_size); | ||
|
||
tested_cases += 1; | ||
let mut hist = bottomup( | ||
&training_data, | ||
args.block, | ||
k, | ||
args.top, | ||
args.max_bits, | ||
args.threads, | ||
); | ||
let testing_data2; | ||
if args.block <= 256 { | ||
tested_cases += 1; | ||
let (training_data, testing_data_opt2) = | ||
prepare_data(&args.data_source, 2 * args.block, true); | ||
testing_data2 = testing_data_opt2.unwrap(); | ||
k = choose_k(2 * args.block, data_size); | ||
let hist2 = bottomup( | ||
&training_data, | ||
args.block * 2, | ||
k, | ||
args.top, | ||
args.max_bits, | ||
args.threads, | ||
); | ||
if hist2.z_score.abs() > hist.z_score.abs() { | ||
hist = hist2; | ||
testing_data = testing_data2; | ||
} | ||
} | ||
println!("training finished in {:?}", start.elapsed()); | ||
|
||
if tested_cases > 1 { | ||
let new_alpha = args.alpha / (tested_cases as f64); | ||
println!( | ||
"Adjusting significance level based on the number of tests from {} to {}", | ||
args.alpha, new_alpha | ||
); | ||
args.alpha = new_alpha; | ||
} | ||
|
||
results(hist, &testing_data, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::{ | ||
bottomup::Histogram, | ||
common::{p_value, z_score, Args}, | ||
}; | ||
use serde_json::json; | ||
use std::fs::File; | ||
use std::io::Write; | ||
|
||
pub(crate) fn results(hist: Histogram, testing_data: &[Vec<u8>], args: Args) { | ||
let (count, bins) = hist.evaluate(testing_data); | ||
let prob = 2.0_f64.powf(-(hist.bits.len() as f64)); | ||
let z = z_score( | ||
testing_data.len(), | ||
count, | ||
prob * (hist.best_division as f64), | ||
); | ||
let p_val = p_value( | ||
count, | ||
testing_data.len(), | ||
prob * (hist.best_division as f64), | ||
); | ||
print_results(p_val, z, args.alpha, &hist, bins); | ||
|
||
if let Some(path) = args.json.clone() { | ||
let mut file = | ||
File::create(&path).unwrap_or_else(|_| panic!("File {} couldn't be created", path)); | ||
|
||
let output = json!({ | ||
"args": args, | ||
"dis": hist, | ||
"result": if p_val < args.alpha {"random"} else {"non-random"}, | ||
"p-value": p_val | ||
}); | ||
|
||
file.write_all( | ||
serde_json::to_string_pretty(&output) | ||
.expect("Failed to produce json!") | ||
.as_bytes(), | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
fn print_results(p_value: f64, z_score: f64, alpha: f64, hist: &Histogram, bins: Vec<usize>) { | ||
println!("----------------------------------------------------------------------"); | ||
println!("RESULTS:\n"); | ||
|
||
println!("Histogram(the discovered Boolean function returns 1 for values before the separator and 0 for values after the separator.):\n"); | ||
let m = bins.iter().max().unwrap(); | ||
let unit = (m / 50).max(1); | ||
for (i, ind) in hist.sorted_indices.iter().enumerate() { | ||
for x in &hist.bits { | ||
print!("x{} ", x); | ||
} | ||
let mut j = *ind; | ||
print!("| ["); | ||
for _ in 0..hist.bits.len() { | ||
print!("{}", j % 2); | ||
j /= 2; | ||
} | ||
print!("] | "); | ||
for _ in 0..bins[*ind] / unit { | ||
print!("∎"); | ||
} | ||
println!(); | ||
if i == (hist.best_division - 1) { | ||
for _ in 0..80 { | ||
print!("—"); | ||
} | ||
println!(); | ||
} | ||
} | ||
println!(); | ||
println!("Z-score: {z_score}"); | ||
println!("P-value: {p_value:.0e}"); | ||
if p_value >= alpha { | ||
println!( | ||
"As the p-value >= alpha {alpha:.0e}, the randomness hypothesis cannot be rejected." | ||
); | ||
println!("= CoolTest could not find statistically significant non-randomness."); | ||
} else { | ||
println!("As the p-value < alpha {alpha:.0e}, the randomness hypothesis is REJECTED."); | ||
println!("= Data is not random."); | ||
} | ||
} |