| use super::AnalyticsDirectory; |
| use loda_rust_core::util::BigIntVec; |
| use loda_rust_core::oeis::OeisIdHashSet; |
| use crate::mine::FunnelConfig; |
| use crate::config::Config; |
| use crate::common::{create_csv_file, SimpleLog}; |
| use crate::oeis::{ProcessStrippedFile, StrippedRow}; |
| use num_bigint::{BigInt, ToBigInt}; |
| use std::convert::TryFrom; |
| use std::io; |
| use std::path::PathBuf; |
| use std::fs::File; |
| use std::io::BufReader; |
| use std::collections::HashMap; |
| use std::time::Instant; |
| use serde::Serialize; |
| use console::Style; |
| use indicatif::{HumanDuration, ProgressBar}; |
|
|
| static DISCARD_EXTREME_VALUES_BEYOND_THIS_LIMIT: i64 = 400; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub struct HistogramStrippedFile { |
| analytics_directory: AnalyticsDirectory, |
| config: Config, |
| simple_log: SimpleLog, |
| histogram: HashMap<i64,u32>, |
| } |
|
|
| impl HistogramStrippedFile { |
| pub fn run(analytics_directory: AnalyticsDirectory, simple_log: SimpleLog) -> anyhow::Result<()> { |
| let config = Config::load(); |
| let mut instance = Self { |
| analytics_directory, |
| config, |
| simple_log, |
| histogram: HashMap::new(), |
| }; |
| instance.run_inner()?; |
| Ok(()) |
| } |
|
|
| fn run_inner(&mut self) -> anyhow::Result<()> { |
| self.simple_log.println("\nHistogram of OEIS 'stripped' file"); |
| println!("Histogram of OEIS 'stripped' file"); |
|
|
| let oeis_stripped_file: PathBuf = self.config.oeis_stripped_file(); |
| assert!(oeis_stripped_file.is_absolute()); |
| assert!(oeis_stripped_file.is_file()); |
|
|
| let file = File::open(oeis_stripped_file).unwrap(); |
| let filesize: usize = file.metadata().unwrap().len() as usize; |
| let mut reader = BufReader::new(file); |
| Self::histogram_of_terms_in_oeis_stripped_file( |
| self.simple_log.clone(), |
| &mut reader, |
| filesize, |
| &mut self.histogram, |
| )?; |
| self.save()?; |
| Ok(()) |
| } |
|
|
| fn histogram_of_terms_in_oeis_stripped_file( |
| simple_log: SimpleLog, |
| oeis_stripped_file_reader: &mut dyn io::BufRead, |
| filesize: usize, |
| histogram: &mut HashMap::<i64,u32>, |
| ) -> anyhow::Result<()> { |
| let start = Instant::now(); |
| let mut count_big: u32 = 0; |
| let mut count_small: u32 = 0; |
| let mut count_wildcard: u32 = 0; |
| let pb = ProgressBar::new(filesize as u64); |
| let padding_value_i64: i64 = 0xC0FFEE; |
| let padding_value: BigInt = padding_value_i64.to_bigint().unwrap(); |
| let process_callback = |stripped_sequence: &StrippedRow, count_bytes: usize| { |
| pb.set_position(count_bytes as u64); |
| let all_vec: &BigIntVec = stripped_sequence.terms(); |
| for value in all_vec { |
| let key: i64 = match i64::try_from(value).ok() { |
| Some(value) => value, |
| None => { |
| count_big += 1; |
| continue; |
| } |
| }; |
| if key == padding_value_i64 { |
| count_wildcard += 1; |
| continue; |
| } |
| if key.abs() > DISCARD_EXTREME_VALUES_BEYOND_THIS_LIMIT { |
| count_big += 1; |
| continue; |
| } |
| let counter = histogram.entry(key).or_insert(0); |
| *counter += 1; |
| count_small += 1; |
| } |
| }; |
| let oeis_ids_to_ignore = OeisIdHashSet::new(); |
| let mut stripped_sequence_processor = ProcessStrippedFile::new(); |
| stripped_sequence_processor.execute( |
| oeis_stripped_file_reader, |
| FunnelConfig::MINIMUM_NUMBER_OF_REQUIRED_TERMS, |
| FunnelConfig::TERM_COUNT, |
| &oeis_ids_to_ignore, |
| &padding_value, |
| true, |
| process_callback |
| ); |
| pb.finish_and_clear(); |
| |
| let green_bold = Style::new().green().bold(); |
| println!( |
| "{:>12} Histogram of OEIS 'stripped' file, in {}", |
| green_bold.apply_to("Finished"), |
| HumanDuration(start.elapsed()) |
| ); |
| |
| simple_log.println(format!("number of small values: {}", count_small)); |
| simple_log.println(format!("number of big values: {}", count_big)); |
| simple_log.println(format!("number of wildcard values: {}", count_wildcard)); |
| Ok(()) |
| } |
|
|
| fn save(&self) -> anyhow::Result<()> { |
| let mut records = Vec::<Record>::new(); |
| for (histogram_key, histogram_count) in &self.histogram { |
| let record = Record { |
| count: *histogram_count, |
| value: *histogram_key, |
| }; |
| records.push(record); |
| } |
| for i in -DISCARD_EXTREME_VALUES_BEYOND_THIS_LIMIT..(DISCARD_EXTREME_VALUES_BEYOND_THIS_LIMIT+1) { |
| if !self.histogram.contains_key(&i) { |
| let record = Record { |
| count: 0, |
| value: i, |
| }; |
| records.push(record); |
| } |
| } |
| |
| |
| |
| records.sort_unstable_by_key(|item| (item.count, item.value.clone())); |
| records.reverse(); |
| |
| |
| let output_path: PathBuf = self.analytics_directory.histogram_oeis_stripped_file(); |
| create_csv_file(&records, &output_path) |
| .map_err(|e| anyhow::anyhow!("HistogramStrippedFile.save - create_csv_file error: {:?}", e))?; |
| Ok(()) |
| } |
| } |
|
|
| #[derive(Debug, Serialize)] |
| struct Record { |
| count: u32, |
| value: i64, |
| } |
|
|