Skip to content

Commit

Permalink
add histogram printing, remove time information
Browse files Browse the repository at this point in the history
  • Loading branch information
jirigav committed May 13, 2024
1 parent 4090de1 commit 09de396
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
23 changes: 7 additions & 16 deletions src/bottomup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::{Duration, Instant};

use itertools::Itertools;
use rayon::iter::*;

Expand Down Expand Up @@ -73,7 +71,7 @@ impl Histogram {
}
}

pub(crate) fn evaluate(&self, data: &[Vec<u8>]) -> usize {
pub(crate) fn evaluate(&self, data: &[Vec<u8>]) -> (usize, Vec<usize>) {
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;
Expand All @@ -82,7 +80,7 @@ impl Histogram {
for k in 0..self.best_division {
count += hist2[self.sorted_indices[k]];
}
count
(count, hist2)
}
}

Expand Down Expand Up @@ -137,9 +135,8 @@ fn compute_bins(
hists: &[Vec<usize>],
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;

Expand All @@ -163,11 +160,9 @@ fn compute_bins(
}

fn brute_force(data: &Data, block_size: usize, deg: usize, k: usize) -> Vec<Histogram> {
let mut t = Duration::from_micros(0);

let mut hists: Vec<Vec<usize>> = 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])
}

Expand All @@ -176,7 +171,7 @@ fn brute_force(data: &Data, block_size: usize, deg: usize, k: usize) -> Vec<Hist

for bits in (0..block_size).combinations(d) {
let mut bins = vec![0; 2_usize.pow(d as u32)];
compute_bins(&bits, data, d, &hists, &mut bins, block_size, &mut t);
compute_bins(&bits, data, d, &hists, &mut bins, block_size);

new_hists.push(bins);
}
Expand All @@ -185,13 +180,13 @@ fn brute_force(data: &Data, block_size: usize, deg: usize, k: usize) -> Vec<Hist
let mut best_hists = vec![Histogram::from_bins(vec![0], &[1, 1]); k];
let mut bins = vec![0; 2_usize.pow(deg as u32)];
for bits in (0..block_size).combinations(deg) {
compute_bins(&bits, data, deg, &hists, &mut bins, block_size, &mut t);
compute_bins(&bits, data, deg, &hists, &mut bins, block_size);
let hist = Histogram::from_bins(bits, &bins);
best_hists.push(hist);
best_hists.sort_by(|a, b| b.z_score.abs().partial_cmp(&a.z_score.abs()).unwrap());
best_hists.pop();
}
println!("Stream operations: {:?}", t);

best_hists
}

Expand Down Expand Up @@ -219,18 +214,14 @@ pub(crate) fn bottomup(
max_bits: usize,
threads: usize,
) -> 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();
Expand Down
9 changes: 2 additions & 7 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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() {
Expand All @@ -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::<usize>();
*t += start.elapsed();
r
}

Expand Down
39 changes: 34 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) {
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."
Expand All @@ -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(),
Expand All @@ -52,7 +78,10 @@ fn run_bottomup(args: Args) {
),
z,
args.alpha,
)
hist,
bins
);

}

fn main() {
Expand Down

0 comments on commit 09de396

Please sign in to comment.