Adds parsing of Format16 bytedata
This commit is contained in:
@@ -155,7 +155,7 @@ enum AdcBlockKeys {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SignalSpec {
|
pub struct SignalSpec {
|
||||||
filename: String,
|
filename: String,
|
||||||
format: SignalFormat,
|
pub format: SignalFormat,
|
||||||
samples_frame: Option<u64>,
|
samples_frame: Option<u64>,
|
||||||
skew: Option<u64>,
|
skew: Option<u64>,
|
||||||
offset: Option<u64>,
|
offset: Option<u64>,
|
||||||
@@ -431,7 +431,17 @@ impl Header {
|
|||||||
for signal in &self.signal_specs {
|
for signal in &self.signal_specs {
|
||||||
map.entry(signal.filename.clone()).and_modify(|val: &mut u64| *val += 1 ).or_insert(1);
|
map.entry(signal.filename.clone()).and_modify(|val: &mut u64| *val += 1 ).or_insert(1);
|
||||||
}
|
}
|
||||||
return map;
|
map
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn signals_in_file(&self, filename: String) -> Vec<&SignalSpec> {
|
||||||
|
let mut buffer: Vec<&SignalSpec> = Vec::new();
|
||||||
|
for signal in &self.signal_specs {
|
||||||
|
if signal.filename == filename {
|
||||||
|
buffer.push(signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
80
src/main.rs
80
src/main.rs
@@ -1,16 +1,17 @@
|
|||||||
use std::{env::{self}, path::Path};
|
use std::{env::{self}, os::linux::raw, path::Path, str::FromStr};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub mod headparse; // The HEAder parsing
|
pub mod headparse; // The HEAder parsing
|
||||||
pub mod signal_data;
|
pub mod signal_data;
|
||||||
|
|
||||||
use crate::{headparse::Header, signal_data::SignalData};
|
use crate::{headparse::{Header, SignalSpec}, signal_data::SignalData};
|
||||||
|
|
||||||
/// Use for handling possible formats of the WFDB data
|
/// Use for handling possible formats of the WFDB data
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
enum SignalFormat {
|
enum SignalFormat {
|
||||||
Format16 = 16,
|
Format16 = 16,
|
||||||
Format212 = 212,
|
Format212 = 212,
|
||||||
|
Invalid = -1,
|
||||||
Unimpl = 0
|
Unimpl = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ fn main() -> Result<(), String>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsed_signals = get_all_data(header, path_parent);
|
let parsed_signal_result = get_all_data(header, path_parent);
|
||||||
|
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -75,14 +76,79 @@ fn help() {
|
|||||||
println!("\nUse in the format \"wfdb_corrosion (.hea filename)\"")
|
println!("\nUse in the format \"wfdb_corrosion (.hea filename)\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_signal_data(spec: headparse::SignalSpec, offset: u64, data_length: usize) -> SignalData {
|
fn get_signal_data(spec: &SignalSpec, offset: u64, data_length: usize, raw: &Vec<u64>) -> SignalData {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_all_data(header: Header, header_root: String) -> Vec<SignalData> {
|
fn get_all_data(header: Header, header_root: String) -> Result<Vec<SignalData>, String> {
|
||||||
let signal_counts = header.signals_per_file();
|
let signal_counts = header.signals_per_file();
|
||||||
for (filename, sigcount) in signal_counts {
|
for (fname, sigcount) in signal_counts {
|
||||||
|
let mut full = header_root.to_owned(); // Consider refactoring these to avoid this massive amount of ownership bs
|
||||||
|
full.push('/');
|
||||||
|
let fname = fname.to_owned();
|
||||||
|
|
||||||
|
full.push_str(&fname);
|
||||||
|
let filepath = Path::new(&full);
|
||||||
|
if !filepath.is_file() {
|
||||||
|
return Err("Error: Could not open signal .dat file. Check if the
|
||||||
|
header points to the right .dat file and if files are present".to_string());
|
||||||
|
}
|
||||||
|
if filepath.extension().unwrap() != "dat" {
|
||||||
|
println!("warn: Header file defines signal files with an extension
|
||||||
|
other than .dat.
|
||||||
|
Check if the header points to the right files, reading will continue
|
||||||
|
but correct data is not guaranteed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time to actually do the work
|
||||||
|
let signals = header.signals_in_file(fname);
|
||||||
|
let format = file_signal_format_check(signals);
|
||||||
|
if format == SignalFormat::Invalid {
|
||||||
|
return Err("Error: Signals in single file use multiple formats.
|
||||||
|
Is your header file correctly formatted?".to_string());
|
||||||
|
}
|
||||||
|
if format == SignalFormat::Unimpl {
|
||||||
|
println!("Warn: Signals in single file use an unimplemented format,
|
||||||
|
and will be ignored");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let raw_bytes = fs::read(filepath).unwrap();
|
||||||
|
|
||||||
|
match format {
|
||||||
|
SignalFormat::Format16 => {
|
||||||
|
let mut buffer: u64 = 0;
|
||||||
|
let mut do_append = false;
|
||||||
|
let mut raw_converted: Vec<u64> = Vec::with_capacity(raw_bytes.len() / 2);
|
||||||
|
|
||||||
|
for byte in raw_bytes {
|
||||||
|
if do_append {
|
||||||
|
buffer = (buffer & 0xFF) + (byte as u64) << 8;
|
||||||
|
raw_converted.push(buffer);
|
||||||
|
do_append = false;
|
||||||
|
} else {
|
||||||
|
buffer = byte as u64;
|
||||||
|
do_append = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dbg!(raw_converted);
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
panic!("Somehow reached actual file and format processing with
|
||||||
|
unimplemented format. Please make a bug report")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
todo!()
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_signal_format_check(signals: Vec<&SignalSpec>) -> SignalFormat {
|
||||||
|
let first_sig = signals[0];
|
||||||
|
let format = first_sig.format;
|
||||||
|
for sig in signals {
|
||||||
|
if sig.format != format {
|
||||||
|
return SignalFormat::Invalid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return format;
|
||||||
}
|
}
|
||||||
@@ -45,4 +45,9 @@ impl SignalData {
|
|||||||
let mut to_add: Vec<f64> = data;
|
let mut to_add: Vec<f64> = data;
|
||||||
to_add.append(&mut self.values);
|
to_add.append(&mut self.values);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SignalDataRaw {
|
||||||
|
name: String,
|
||||||
|
raw_data: Vec<u8>
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user