Skip to content

Commit

Permalink
issue-108, unnecessary comments and code were removed
Browse files Browse the repository at this point in the history
  • Loading branch information
DoodgeMatvey committed Sep 16, 2024
1 parent a29a36a commit 662aca2
Show file tree
Hide file tree
Showing 30 changed files with 43 additions and 388 deletions.
8 changes: 0 additions & 8 deletions brro-compressor/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
};
use std::thread;

/// Enum to represent the decision between compressors.
#[derive(PartialEq, Debug)]
enum CompressionDecision {
Constant,
Expand All @@ -17,24 +16,19 @@ enum CompressionDecision {
}

impl CompressionDecision {
/// Function to perform compression and make a decision based on the results.
pub fn compress_and_decide() -> Result<(), Box<dyn std::error::Error>> {
// Sample data for testing
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let stats = DataStats::new(&data);

// Clone data for each compressor
let data_constant = data.clone();
let data_fft = data.clone();
let data_polynomial = data.clone();

// Create threads for each compressor
let thread_constant = thread::spawn(move || constant_compressor(&data_constant, stats));
let thread_fft = thread::spawn(move || fft(&data_fft));
let thread_polynomial =
thread::spawn(move || polynomial(&data_polynomial, PolynomialType::Polynomial));

// Wait for threads to finish and collect their results with error handling
let result_constant = thread_constant
.join()
.map_err(|e| format!("Constant thread error: {:?}", e))?;
Expand All @@ -45,7 +39,6 @@ impl CompressionDecision {
.join()
.map_err(|e| format!("Polynomial thread error: {:?}", e))?;

// Use the decision logic to determine the compression decision
let decision = match (
result_constant.compressed_data.len(),
result_fft.len(),
Expand All @@ -60,7 +53,6 @@ impl CompressionDecision {
_ => CompressionDecision::Polynomial,
};

// Use the decision to perform further actions
match decision {
CompressionDecision::Constant => {
println!("Selected Constant Compressor");
Expand Down
4 changes: 0 additions & 4 deletions brro-compressor/src/compressor/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl Decode for Constant {
) -> Result<Self, ::bincode::error::DecodeError> {
let id = Decode::decode(decoder)?;
let bitdepth = Decode::decode(decoder)?;
// Here is where the pig twists the tail
let constant: f64 = match bitdepth {
Bitdepth::U8 => {
debug!("Decoding as u8");
Expand Down Expand Up @@ -110,7 +109,6 @@ impl Constant {

/// This function transforms the structure into a Binary stream
pub fn to_bytes(&self) -> Vec<u8> {
// Use Bincode and flate2-rs? Do this at the Stream Level?
let config = BinConfig::get();
bincode::encode_to_vec(self, config).unwrap()
}
Expand All @@ -125,9 +123,7 @@ impl Constant {

pub fn constant_compressor(data: &[f64], stats: DataStats) -> CompressorResult {
debug!("Initializing Constant Compressor. Error and Stats provided");
// Initialize the compressor
let c = Constant::new(data.len(), stats.min, stats.bitdepth);
// Convert to bytes
CompressorResult::new(c.to_bytes(), 0.0)
}

Expand Down
2 changes: 1 addition & 1 deletion brro-compressor/src/compressor/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl FFT {
}

/// Compresses data via FFT
/// The set of frequencies to store is 1/100 of the data lenght OR 3, which is bigger.
/// The set of frequencies to store is 1/100 of the data length OR 3, which is bigger.
pub fn compress(&mut self, data: &[f64]) {
if self.max_value == self.min_value {
debug!("Same max and min, we're done here!");
Expand Down
1 change: 0 additions & 1 deletion brro-compressor/src/compressor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ pub struct BinConfig {

impl BinConfig {
pub fn get() -> Configuration {
// Little endian and Variable int encoding
config::standard()
}
}
9 changes: 1 addition & 8 deletions brro-compressor/src/compressor/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ impl Noop {
data: Vec::with_capacity(sample_count),
}
}
///Optimize
pub fn optimize(data: &[f64]) -> Vec<i64> {
let mut out_vec = Vec::with_capacity(data.len());
for &element in data {
// Round the floating-point number before casting to i64
out_vec.push(element.round() as i64);
}
out_vec
}

/// "Compress"
pub fn compress(&mut self, data: &[f64]) {
self.data = Noop::optimize(data);
debug!(
Expand All @@ -38,20 +35,17 @@ impl Noop {
);
}

/// Receives a data stream and generates a Noop
pub fn decompress(data: &[u8]) -> Self {
let config = BinConfig::get();
let (noop, _) = bincode::decode_from_slice(data, config).unwrap();
noop
}

/// This function transforms the structure in a Binary stream to be appended to the frame
pub fn to_bytes(&self) -> Vec<u8> {
let config = BinConfig::get();
bincode::encode_to_vec(self, config).unwrap()
}

/// Returns an array of data
pub fn to_data(&self, _frame_size: usize) -> Vec<i64> {
self.data.clone()
}
Expand Down Expand Up @@ -100,9 +94,8 @@ mod tests {

#[test]
fn test_optimize() {
// Test case with floating-point numbers that have fractional parts
let input_data = [1.5, 2.7, 3.3, 4.9];
let expected_output = [2, 3, 3, 5]; // Rounded to the nearest integer
let expected_output = [2, 3, 3, 5];

let result = Noop::optimize(&input_data);
assert_eq!(result, expected_output);
Expand Down
19 changes: 2 additions & 17 deletions brro-compressor/src/compressor/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ pub enum Method {

#[derive(PartialEq, Debug, Clone)]
pub struct Polynomial {
/// Compressor ID
pub id: PolynomialType,
/// Stored Points
pub data_points: Vec<f64>,
pub min: f64,
pub max: f64,
/// What is the base step between points
pub point_step: u8,
/// Compression error
pub error: Option<f64>,
/// Target bitdepth
pub bitdepth: Bitdepth,
}

Expand Down Expand Up @@ -81,7 +76,6 @@ impl Decode for Polynomial {
) -> Result<Self, ::bincode::error::DecodeError> {
let id = Decode::decode(decoder)?;
let bitdepth = Decode::decode(decoder)?;
// Here is where the pig twists the tail
let data_points: Vec<f64> = match bitdepth {
Bitdepth::U8 => {
debug!("Decoding as u8");
Expand Down Expand Up @@ -207,7 +201,7 @@ impl Polynomial {
}
// TODO: Big one, read below
// To reduce error we add more points to the polynomial, but, we also might add residuals
// each residual is 1/data_lenght * 100% less compression, each jump is 5% less compression.
// each residual is 1/data_length * 100% less compression, each jump is 5% less compression.
// We can do the math and pick the one which fits better.
let method = self.get_method();
let data_len = data.len();
Expand Down Expand Up @@ -264,7 +258,7 @@ impl Polynomial {
}
self.error = Some(current_err);
debug!(
"Final Stored Data Lenght: {} Iterations: {}",
"Final Stored Data Length: {} Iterations: {}",
self.data_points.len(),
iterations
);
Expand Down Expand Up @@ -298,7 +292,6 @@ impl Polynomial {
self.point_step = step as u8;
}

// --- MANDATORY METHODS ---
pub fn compress(&mut self, data: &[f64]) {
let points = if 3 >= (data.len() / 100) {
3
Expand All @@ -308,7 +301,6 @@ impl Polynomial {
self.compress_hinted(data, points)
}

/// Decompresses data
pub fn decompress(data: &[u8]) -> Self {
let config = BinConfig::get();
let (poly, _) = bincode::decode_from_slice(data, config).unwrap();
Expand All @@ -320,7 +312,6 @@ impl Polynomial {
bincode::encode_to_vec(self, config).unwrap()
}

// --- END OF MANDATORY METHODS ---
/// Since IDW and Polynomial are the same code everywhere, this function prepares the data
/// to be used by one of the polynomial decompression methods
fn get_positions(&self, frame_size: usize) -> Vec<usize> {
Expand Down Expand Up @@ -405,11 +396,8 @@ impl Polynomial {
pub fn polynomial(data: &[f64], p_type: PolynomialType) -> Vec<u8> {
info!("Initializing Polynomial Compressor");
let stats = DataStats::new(data);
// Initialize the compressor
let mut c = Polynomial::new(data.len(), stats.min, stats.max, p_type, stats.bitdepth);
// Convert the data
c.compress(data);
// Convert to bytes
c.to_bytes()
}

Expand All @@ -420,14 +408,11 @@ pub fn polynomial_allowed_error(
) -> CompressorResult {
info!("Initializing Polynomial Compressor");
let stats = DataStats::new(data);
// Initialize the compressor
let mut c = Polynomial::new(data.len(), stats.min, stats.max, p_type, stats.bitdepth);
// Convert the data
c.compress_bounded(data, allowed_error);
CompressorResult::new(c.to_bytes(), c.error.unwrap_or(0.0))
}

/// Uncompress
pub fn to_data(sample_number: usize, compressed_data: &[u8]) -> Vec<f64> {
let c = Polynomial::decompress(compressed_data);
c.to_data(sample_number)
Expand Down
1 change: 0 additions & 1 deletion brro-compressor/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct CompressedStream {
}

impl CompressedStream {
/// Creates an empty compressor stream
pub fn new() -> Self {
CompressedStream {
header: CompressorHeader::new(),
Expand Down
13 changes: 1 addition & 12 deletions brro-compressor/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ use std::mem::size_of_val;

const COMPRESSION_SPEED: [i32; 7] = [i32::MAX, 4096, 2048, 1024, 512, 256, 128];

/// This is the structure of a compressor frame
#[derive(Encode, Decode, Debug, Clone)]
pub struct CompressorFrame {
/// The frame size in bytes,
frame_size: usize,
/// The number of samples in this frame,
sample_count: usize,
/// The compressor used in the current frame
compressor: Compressor,
/// Output from the compressor
data: Vec<u8>,
Expand All @@ -32,7 +29,7 @@ impl CompressorFrame {
}

/// Calculates the size of the Frame and "closes it"
// TODO this is probably wrong, so we have to use the write stream to dump the bytes writen
// TODO this is probably wrong, so we have to use the write stream to dump the bytes written
pub fn close(&mut self) {
let size = size_of_val(&self.sample_count)
+ size_of_val(&self.compressor)
Expand All @@ -41,13 +38,11 @@ impl CompressorFrame {
self.frame_size = size;
}

/// Compress a data and stores the result in the frame
pub fn compress(&mut self, data: &[f64]) {
self.sample_count = data.len();
self.data = self.compressor.compress(data);
}

/// Compress a data and stores the result in the frame
pub fn compress_bounded(&mut self, data: &[f64], max_error: f32) {
self.sample_count = data.len();
self.data = self.compressor.compress_bounded(data, max_error as f64);
Expand All @@ -59,19 +54,16 @@ impl CompressorFrame {
// Speed factor limits the amount of data that is sampled to calculate the best compressor.
// We need enough samples to do decent compression, minimum is 128 (2^7)
let data_sample = COMPRESSION_SPEED[compression_speed] as usize;
// Eligible compressors for use
let compressor_list = [
Compressor::Constant,
Compressor::FFT,
Compressor::Polynomial,
];
// Do a statistical analysis of the data, let's see if we can pick a compressor out of this.
let stats = DataStats::new(data);
// Checking the statistical analysis and chose, if possible, a compressor
// If the data is constant, well, constant frame
if stats.min == stats.max {
self.compressor = Compressor::Constant;
// Now do the full data compression
self.data = self
.compressor
.get_compress_bounded_results(data, max_error as f64)
Expand All @@ -93,13 +85,11 @@ impl CompressorFrame {
.min_by_key(|x| x.0.compressed_data.len())
.unwrap();
self.compressor = *chosen_compressor;
// Now do the full data compression
self.data = self
.compressor
.get_compress_bounded_results(data, max_error as f64)
.compressed_data;
} else {
// Run all the eligible compressors and choose smallest
let (smallest_result, chosen_compressor) = compressor_list
.iter()
.map(|compressor| {
Expand All @@ -117,7 +107,6 @@ impl CompressorFrame {
debug!("Auto Compressor Selection: {:?}", self.compressor);
}

/// Decompresses a frame and returns the resulting data array
pub fn decompress(&self) -> Vec<f64> {
debug!(
"Decompressing Frame. Size: {}, Samples: {}",
Expand Down
1 change: 0 additions & 1 deletion brro-compressor/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bincode::{Decode, Encode};

/// This will write the file headers
#[derive(Encode, Decode, Debug, Clone)]
pub struct CompressorHeader {
initial_segment: [u8; 4],
Expand Down
2 changes: 1 addition & 1 deletion brro-compressor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::new_without_default)]
// Lucas - Once the project is far enough along I strongly reccomend reenabling dead code checks
// TODO: re-enable dead code checks
#![allow(dead_code)]

pub mod compare;
Expand Down
Loading

0 comments on commit 662aca2

Please sign in to comment.