Skip to content

Commit

Permalink
1. Exposed csv samples reading API
Browse files Browse the repository at this point in the history
2. Added -csv command to process csv, --no_header and --fields options to customize its processing
3. cargo fmt
  • Loading branch information
worryg0d committed Nov 14, 2024
1 parent 4a3b4db commit 2f0088e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
40 changes: 31 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions atsc/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::{File, OpenOptions};
use std::path::Path;

#[derive(Debug, thiserror::Error)]
enum Error {
pub enum Error {
#[error("Failed to open csv file")]
OpenFileFailed,

Expand All @@ -25,15 +25,15 @@ enum Error {
type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, PartialEq)]
struct Sample {
timestamp: i64,
value: f64,
pub struct Sample {
pub timestamp: i64,
pub value: f64,
}

/// read_samples_with_headers reads samples from the given file.
/// It expects that timestamps are stored under timestamp_field field header
/// and values are stored under value_field.
fn read_samples_with_headers(
pub fn read_samples_with_headers(
filepath: &Path,
timestamp_field: &str,
value_field: &str,
Expand Down Expand Up @@ -75,9 +75,9 @@ fn read_samples_with_headers(
/// read_samples reads samples from the given file.
/// It assumes that file contains no headers and
/// consists of only a single field with values.
fn read_samples(filepath: &Path) -> Result<Vec<Sample>> {
pub fn read_samples(filepath: &Path) -> Result<Vec<Sample>> {
let mut reader = open_csv_reader(filepath, false)?;

// Collect samples
let mut samples = Vec::new();
for record in reader.records() {
Expand All @@ -93,7 +93,7 @@ fn read_samples(filepath: &Path) -> Result<Vec<Sample>> {
value,
});
}

Ok(samples)
}

Expand Down
2 changes: 1 addition & 1 deletion atsc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ pub mod frame;
pub mod header;
pub mod utils;

mod csv;
pub mod csv;
pub mod optimizer;
40 changes: 40 additions & 0 deletions atsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,37 @@ fn process_single_file(mut file_path: PathBuf, arguments: &Args) -> Result<(), B
file_path.set_extension("wbro");
WavBrro::to_file_with_data(&file_path, &decompressed_data)
}
} else if arguments.csv {
// Read samples from csv and compress it
let samples = if arguments.no_header {
debug!("Reading samples from csv with no header");
read_samples(&file_path)?
} else {
debug!("Reading samples from csv with headers");
let headers: Vec<&str> = arguments.fields.split(",").collect();
// Assuming that header[0] is a time field and header[1] is value field
read_samples_with_headers(&file_path, headers[0], headers[1])?
};

let data: Vec<f64> = samples.into_iter().map(|sample| sample.value).collect();

if arguments.verbose {
println!("Input={:?}", data);
}

// Compress
let compressed_data = compress_data(&data, arguments);

// Write
file_path.set_extension("bro");
std::fs::write(file_path, compressed_data)?;
} else {
// Read an WavBRRO file and compress it
let data = WavBrro::from_file(&file_path)?;
if arguments.verbose {
println!("Input={:?}", data);
}

//compress
let compressed_data = compress_data(&data, arguments);

Expand Down Expand Up @@ -169,6 +194,21 @@ struct Args {
/// Verbose output, dumps everysample in the input file (for compression) and in the ouput file (for decompression)
#[arg(long, action)]
verbose: bool,

/// Defines user input as a CSV file
#[arg(long, action)]
csv: bool,

/// Defines if the CSV has no header
#[arg(long, action)]
no_header: bool,

/// Defines names of fields in CSV file. It should follow this format:
/// --fields=TIME_FIELD_NAME,VALUE_FIELD_NAME
/// It assumes that the one before comma is a name of time field and the one
/// after comma is value field.
#[arg(long)]
fields: String,
}

#[derive(clap::ValueEnum, Default, Clone, Debug)]
Expand Down

0 comments on commit 2f0088e

Please sign in to comment.