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 Nov 11, 2024
1 parent 95da51b commit 348e8e5
Show file tree
Hide file tree
Showing 36 changed files with 30 additions and 3,873 deletions.
987 changes: 8 additions & 979 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]
members = [
"brro-compressor",
"optimizer",
"prometheus-remote",
"tools",
"wavbrro",
"vsri",
Expand Down
72 changes: 1 addition & 71 deletions brro-compressor/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,84 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use crate::{
compressor::{
constant::constant_compressor,
fft::fft,
polynomial::{polynomial, PolynomialType},
},
optimizer::utils::DataStats,
};
use std::thread;

/// Enum to represent the decision between compressors.
#[derive(PartialEq, Debug)]
enum CompressionDecision {
Constant,
Fft,
Polynomial,
}

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))?;
let result_fft = thread_fft
.join()
.map_err(|e| format!("FFT thread error: {:?}", e))?;
let result_polynomial = thread_polynomial
.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(),
result_polynomial.len(),
) {
(constant_len, fft_len, poly_len)
if constant_len < fft_len && constant_len < poly_len =>
{
CompressionDecision::Constant
}
(_, fft_len, poly_len) if fft_len < poly_len => CompressionDecision::Fft,
_ => CompressionDecision::Polynomial,
};

// Use the decision to perform further actions
match decision {
CompressionDecision::Constant => {
println!("Selected Constant Compressor");
}
CompressionDecision::Fft => {
println!("Selected FFT Compressor");
}
CompressionDecision::Polynomial => {
println!("Selected Polynomial Compressor");
}
}

Ok(())
}
}
impl CompressionDecision {}
fn get_compression_decision(
result_constant: &[f64],
result_fft: &[f64],
Expand Down
9 changes: 0 additions & 9 deletions brro-compressor/src/compressor/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 @@ -112,11 +111,6 @@ impl Constant {
}
}

/// This compressor is about having a single constant for the whole segment
pub fn set_constant(&mut self, constant_value: f64) {
self.constant = constant_value;
}

/// Receives a data stream and generates a Constant
pub fn decompress(data: &[u8]) -> Self {
let config = BinConfig::get();
Expand All @@ -126,7 +120,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 @@ -141,9 +134,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
33 changes: 1 addition & 32 deletions brro-compressor/src/compressor/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ pub struct FrequencyPoint {
}

impl FrequencyPoint {
pub fn new(real: f32, img: f32) -> Self {
FrequencyPoint {
pos: 0,
freq_real: real,
freq_img: img,
}
}

pub fn with_position(real: f32, img: f32, pos: u16) -> Self {
FrequencyPoint {
pos,
freq_real: real,
freq_img: img,
}
}

pub fn from_complex(complex: Complex<f32>) -> Self {
FrequencyPoint {
pos: 0,
freq_real: complex.re,
freq_img: complex.im,
}
}

pub fn from_complex_with_position(complex: Complex<f32>, pos: u16) -> Self {
FrequencyPoint {
pos,
Expand Down Expand Up @@ -132,15 +108,10 @@ impl Ord for FrequencyPoint {
/// FFT Compressor. Applies FFT to a signal, picks the N best frequencies, discards the rest. Always LOSSY
#[derive(PartialEq, Debug)]
pub struct FFT {
/// Compressor ID
pub id: u8,
/// Stored frequencies
pub frequencies: Vec<FrequencyPoint>,
/// The maximum numeric value of the points in the frame
pub max_value: f32,
/// The minimum numeric value of the points in the frame
pub min_value: f32,
/// Compression error
pub error: Option<f64>,
}

Expand Down Expand Up @@ -391,7 +362,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 All @@ -415,8 +386,6 @@ impl FFT {
buffer.truncate(size);
self.frequencies = FFT::fft_trim(&mut buffer, max_freq);
}

/// Decompresses data
pub fn decompress(data: &[u8]) -> Self {
let config = BinConfig::get();
let (fft, _) = bincode::decode_from_slice(data, config).unwrap();
Expand Down
6 changes: 1 addition & 5 deletions brro-compressor/src/compressor/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,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 @@ -67,7 +64,6 @@ impl Noop {
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 @@ -118,7 +114,7 @@ mod tests {
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
24 changes: 2 additions & 22 deletions brro-compressor/src/compressor/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,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 @@ -97,7 +92,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 @@ -205,10 +199,6 @@ impl Polynomial {
}
}

fn locate_in_data_points(&self, point: f64) -> bool {
self.data_points.iter().any(|&i| i == point)
}

fn get_method(&self) -> Method {
match self.id {
PolynomialType::Idw => Method::Idw,
Expand All @@ -223,7 +213,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 @@ -280,7 +270,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 @@ -314,7 +304,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 @@ -324,7 +313,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 @@ -336,7 +324,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 @@ -393,7 +380,6 @@ impl Polynomial {
.map(|&f| f as f64)
.collect();
let idw = IDW::new(points, self.data_points.clone());
// Build the data
(0..frame_size)
.map(|f| {
round_and_limit_f64(
Expand Down Expand Up @@ -421,11 +407,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 @@ -436,14 +419,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
3 changes: 0 additions & 3 deletions brro-compressor/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub struct CompressedStream {
}

impl CompressedStream {
/// Creates an empty compressor stream
pub fn new() -> Self {
CompressedStream {
header: CompressorHeader::new(),
Expand Down Expand Up @@ -87,8 +86,6 @@ impl CompressedStream {
let (compressed_stream, _) = bincode::decode_from_slice(data, config).unwrap();
compressed_stream
}

/// Decompresses all the frames and returns a vector with the data
pub fn decompress(&self) -> Vec<f64> {
self.data_frames
.iter()
Expand Down
4 changes: 1 addition & 3 deletions brro-compressor/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ const COMPRESSION_SPEED: [i32; 7] = [i32::MAX, 4096, 2048, 1024, 512, 256, 128];
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 @@ -48,7 +46,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 Down
1 change: 0 additions & 1 deletion brro-compressor/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.

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
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

#![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 348e8e5

Please sign in to comment.