From 150fe23f6c9f5fbcd74d61ca86b11d8ffd9ef51e Mon Sep 17 00:00:00 2001 From: Cian Hughes Date: Thu, 31 Oct 2024 10:56:02 +0000 Subject: [PATCH] Fixed mistake in csv parser (was parsing as csv, not ssv) --- Cargo.toml | 1 + src/lib.rs | 6 ++++++ src/rust_fn/mod.rs | 40 ++++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b628a6e..34b21eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ rayon = "*" thiserror = "1.0.60" [dev-dependencies] +anyhow = "1.0.91" approx = "0.5.1" arbitrary = { version = "1.3.2", features = ["derive"] } arbtest = "0.3.1" diff --git a/src/lib.rs b/src/lib.rs index c1f5c96..f471887 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,9 +17,15 @@ impl From for PyErr { } rust_fn::ReadError::Io(e) => PyErr::new::(format!("{}", e)), rust_fn::ReadError::CSV(e) => PyErr::new::(format!("{}", e)), + rust_fn::ReadError::ParseIntError(e) => { + PyErr::new::(format!("{}", e)) + } rust_fn::ReadError::ParseFloatError(e) => { PyErr::new::(format!("{}", e)) } + rust_fn::ReadError::ShapeError(e) => { + PyErr::new::(format!("{}", e)) + } rust_fn::ReadError::MiscError(e) => PyErr::new::(e), } } diff --git a/src/rust_fn/mod.rs b/src/rust_fn/mod.rs index c5beeae..f6ef146 100644 --- a/src/rust_fn/mod.rs +++ b/src/rust_fn/mod.rs @@ -21,9 +21,15 @@ pub enum ReadError { #[error("CSV error: {0}")] CSV(#[from] csv::Error), - #[error("Float parse error: {0}")] + #[error("Float int error: {0}")] + ParseIntError(#[from] std::num::ParseIntError), + + #[error("Float float error: {0}")] ParseFloatError(#[from] std::num::ParseFloatError), + #[error("Shape error: {0}")] + ShapeError(#[from] ndarray::ShapeError), + #[error("Miscellaneous Error")] MiscError(String), } @@ -80,8 +86,7 @@ pub fn read_layers(folder: &str) -> Result> { .unwrap() .slice_axis(Axis(1), Slice::from(2..4)), ], - ) - .unwrap(); + )?; out_array.column_mut(0).par_map_inplace(correct_x); out_array.column_mut(1).par_map_inplace(correct_y); @@ -100,15 +105,16 @@ pub fn read_selected_layers(file_list: Vec) -> Result> { .zip(arrays.par_iter_mut()) .zip(z_vals.par_iter_mut()) .zip(z_lens.par_iter_mut()) - .for_each( - |(((filepath, array_element), z_vals_element), z_lens_element)| { - let (array, z, z_len) = read_file(filepath.to_path_buf()).unwrap(); + .try_for_each( + |(((filepath, array_element), z_vals_element), z_lens_element)| -> Result<()> { + let (array, z, z_len) = read_file(filepath.to_path_buf())?; *array_element = array; *z_vals_element = z; *z_lens_element = z_len; - bar.inc(1) + bar.inc(1); + Ok(()) }, - ); + )?; let mut padding_arrays: Vec> = Vec::>::new(); for (z, z_len) in z_vals.iter().zip(z_lens) { @@ -131,8 +137,7 @@ pub fn read_selected_layers(file_list: Vec) -> Result> { .unwrap() .slice_axis(Axis(1), Slice::from(2..4)), ], - ) - .unwrap(); + )?; out_array.column_mut(0).par_map_inplace(correct_x); out_array.column_mut(1).par_map_inplace(correct_y); @@ -141,12 +146,12 @@ pub fn read_selected_layers(file_list: Vec) -> Result> { } pub fn read_layer(file: &str) -> Result> { - let (array, z, z_len) = read_file(Path::new(file).to_path_buf()).unwrap(); + let (array, z, z_len) = read_file(Path::new(file).to_path_buf())?; let z_array: Array2 = Array2::from_elem((z_len, 1), z); let z_array_view: ArrayView2 = z_array.view(); let array_view: ArrayView2 = array.view(); - let mut out_array = concatenate(Axis(1), &[array_view, z_array_view]).unwrap(); + let mut out_array = concatenate(Axis(1), &[array_view, z_array_view])?; out_array.column_mut(0).par_map_inplace(correct_x); out_array.column_mut(1).par_map_inplace(correct_y); @@ -157,7 +162,10 @@ pub fn read_layer(file: &str) -> Result> { pub fn read_file(filepath: PathBuf) -> Result<(Array2, f64, usize)> { let z: f64 = get_z(&filepath)?; let file = File::open(filepath)?; - let mut rdr = ReaderBuilder::new().has_headers(false).from_reader(file); + let mut rdr = ReaderBuilder::new() + .delimiter(b' ') + .has_headers(false) + .from_reader(file); let data = rdr .records() .into_iter() @@ -165,8 +173,8 @@ pub fn read_file(filepath: PathBuf) -> Result<(Array2, f64, usize)> { .iter() .map(|x| { x.iter() - .map(|y| y.parse::().map_err(|e| ReadError::ParseFloatError(e))) - .collect::>>() + .map(|y| y.parse::().map_err(|e| ReadError::ParseIntError(e))) + .collect::>>() }) .collect::>>()?; let length = data.len(); @@ -174,7 +182,7 @@ pub fn read_file(filepath: PathBuf) -> Result<(Array2, f64, usize)> { let mut arr: Array2 = Array2::zeros((length, width)); for (data_row, mut arr_row) in data.iter().zip(arr.axis_iter_mut(Axis(0))) { for (data_i, arr_i) in data_row.iter().zip(arr_row.iter_mut()) { - *arr_i = *data_i + *arr_i = *data_i as f64 } }