From 09de3968fd9222db7ca8edf2e53ae5d64b1616cd Mon Sep 17 00:00:00 2001 From: jirigav Date: Mon, 13 May 2024 17:41:28 +0200 Subject: [PATCH] add histogram printing, remove time information --- src/bottomup.rs | 23 +++++++---------------- src/common.rs | 9 ++------- src/main.rs | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/bottomup.rs b/src/bottomup.rs index f9fb27d..b55ca5b 100644 --- a/src/bottomup.rs +++ b/src/bottomup.rs @@ -1,5 +1,3 @@ -use std::time::{Duration, Instant}; - use itertools::Itertools; use rayon::iter::*; @@ -73,7 +71,7 @@ impl Histogram { } } - pub(crate) fn evaluate(&self, data: &[Vec]) -> usize { + pub(crate) fn evaluate(&self, data: &[Vec]) -> (usize, Vec) { let mut hist2 = vec![0; 2_usize.pow(self.bits.len() as u32)]; for block in data { hist2[bits_block_eval(&self.bits, block)] += 1; @@ -82,7 +80,7 @@ impl Histogram { for k in 0..self.best_division { count += hist2[self.sorted_indices[k]]; } - count + (count, hist2) } } @@ -137,9 +135,8 @@ fn compute_bins( hists: &[Vec], bins: &mut [usize], block_size: usize, - t: &mut Duration, ) { - let ones = multi_eval(bits, data, t); + let ones = multi_eval(bits, data); let value = 2_usize.pow(d as u32) - 1; @@ -163,11 +160,9 @@ fn compute_bins( } fn brute_force(data: &Data, block_size: usize, deg: usize, k: usize) -> Vec { - let mut t = Duration::from_micros(0); - let mut hists: Vec> = Vec::new(); for i in 0..block_size { - let ones = multi_eval(&[i], data, &mut t); + let ones = multi_eval(&[i], data); hists.push(vec![(data._num_of_blocks as usize) - ones, ones]) } @@ -176,7 +171,7 @@ fn brute_force(data: &Data, block_size: usize, deg: usize, k: usize) -> Vec Vec Histogram { - let mut start = Instant::now(); let mut top_k = if threads == 0 { brute_force(&transform_data(data), block_size, base_degree, k) } else { brute_force_threads(&transform_data(data), block_size, base_degree, k, threads) }; - println!("Brute-force finished in {:?}", start.elapsed()); if max_bits > base_degree { - start = Instant::now(); top_k = phase_two(data, block_size, top_k, max_bits); - println!("Heuristic search finished in {:?}", start.elapsed()); } let res = top_k[0].clone(); diff --git a/src/common.rs b/src/common.rs index bde8e82..6834317 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,9 +1,6 @@ use clap::Parser; use pyo3::prelude::*; -use std::{ - fs, - time::{Duration, Instant}, -}; +use std::fs; pub(crate) fn z_score(sample_size: usize, positive: usize, p: f64) -> f64 { ((positive as f64) - p * (sample_size as f64)) / f64::sqrt(p * (1.0 - p) * (sample_size as f64)) @@ -63,8 +60,7 @@ pub(crate) struct Data { pub(crate) _num_of_blocks: u32, } -pub(crate) fn multi_eval(bits: &[usize], data: &Data, t: &mut Duration) -> usize { - let start = Instant::now(); +pub(crate) fn multi_eval(bits: &[usize], data: &Data) -> usize { let mut result = vec![u128::MAX; data.data[0].len()]; for b in bits.iter() { @@ -79,7 +75,6 @@ pub(crate) fn multi_eval(bits: &[usize], data: &Data, t: &mut Duration) -> usize .iter() .map(|x| x.count_ones() as usize) .sum::(); - *t += start.elapsed(); r } diff --git a/src/main.rs b/src/main.rs index dd8e0a1..f6db558 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,41 @@ mod common; use crate::bottomup::bottomup; use crate::common::{p_value, z_score, Args}; +use bottomup::Histogram; use clap::Parser; use common::prepare_data; use std::time::Instant; -fn print_results(p_value: f64, z_score: f64, alpha: f64) { +fn print_results(p_value: f64, z_score: f64, alpha: f64, hist: Histogram, bins: Vec) { println!("----------------------------------------------------------------------"); println!("RESULTS:"); - println!("z-score: {z_score}"); - println!("p-value: {p_value:.0e}"); + 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." @@ -37,7 +63,7 @@ fn run_bottomup(args: Args) { ); println!("training finished in {:?}", start.elapsed()); - let count = hist.evaluate(&testing_data); + 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(), @@ -52,7 +78,10 @@ fn run_bottomup(args: Args) { ), z, args.alpha, - ) + hist, + bins + ); + } fn main() {