use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use ndarray::{Array2, Array3};
use crate::dataset::TSDataset;
use crate::error::{DataError, Result};
pub fn read_npy<P: AsRef<Path>>(path: P) -> Result<Array3<f32>> {
use ndarray_npy::ReadNpyExt;
let file = std::fs::File::open(path.as_ref())?;
let reader = std::io::BufReader::new(file);
match Array3::<f32>::read_npy(reader) {
Ok(arr) => Ok(arr),
Err(e) => {
let file = std::fs::File::open(path.as_ref())?;
let reader = std::io::BufReader::new(file);
let arr_f64: Array3<f64> = Array3::<f64>::read_npy(reader)
.map_err(|_| DataError::FormatError(format!("Failed to read npy file: {}", e)))?;
Ok(arr_f64.mapv(|x| x as f32))
}
}
}
pub fn read_npz<P: AsRef<Path>>(path: P) -> Result<(Array3<f32>, Option<Array2<f32>>)> {
let file = std::fs::File::open(path.as_ref())?;
let mut npz = ndarray_npy::NpzReader::new(file)
.map_err(|e| DataError::FormatError(format!("Failed to read npz file: {}", e)))?;
let x: Array3<f32> = npz
.by_name("x")
.map_err(|e| DataError::FormatError(format!("Failed to read 'x' from npz: {}", e)))?;
let y: Option<Array2<f32>> = npz.by_name("y").ok();
Ok((x, y))
}
pub fn read_csv<P: AsRef<Path>>(
path: P,
n_vars: usize,
seq_len: usize,
has_labels: bool,
) -> Result<TSDataset> {
let file = File::open(path.as_ref())?;
let mut reader = csv::ReaderBuilder::new()
.has_headers(true)
.from_reader(BufReader::new(file));
let expected_data_cols = n_vars * seq_len;
let mut rows: Vec<Vec<f32>> = Vec::new();
let mut labels: Vec<f32> = Vec::new();
for result in reader.records() {
let record = result.map_err(|e| DataError::FormatError(format!("CSV parse error: {}", e)))?;
let n_cols = record.len();
let total_expected = if has_labels {
expected_data_cols + 1
} else {
expected_data_cols
};
if n_cols != total_expected {
return Err(DataError::FormatError(format!(
"Expected {} columns, got {}",
total_expected, n_cols
)));
}
let data_cols = if has_labels { n_cols - 1 } else { n_cols };
let mut row_data: Vec<f32> = Vec::with_capacity(data_cols);
for i in 0..data_cols {
let val: f32 = record.get(i)
.ok_or_else(|| DataError::FormatError("Missing column".to_string()))?
.parse()
.unwrap_or(0.0);
row_data.push(val);
}
rows.push(row_data);
if has_labels {
let label: f32 = record.get(n_cols - 1)
.ok_or_else(|| DataError::FormatError("Missing label column".to_string()))?
.parse()
.unwrap_or(0.0);
labels.push(label);
}
}
let n_samples = rows.len();
if n_samples == 0 {
return Err(DataError::InvalidInput("Empty CSV file".to_string()));
}
let mut x = Array3::<f32>::zeros((n_samples, n_vars, seq_len));
for (row_idx, row_data) in rows.iter().enumerate() {
for (col_idx, &val) in row_data.iter().enumerate() {
let var_idx = col_idx / seq_len;
let step_idx = col_idx % seq_len;
x[[row_idx, var_idx, step_idx]] = val;
}
}
let y = if has_labels {
let mut y_arr = Array2::<f32>::zeros((n_samples, 1));
for (i, &label) in labels.iter().enumerate() {
y_arr[[i, 0]] = label;
}
Some(y_arr)
} else {
None
};
TSDataset::from_arrays(x, y)
}
pub fn read_parquet<P: AsRef<Path>>(
path: P,
x_columns: &[&str],
y_column: Option<&str>,
n_vars: usize,
seq_len: usize,
) -> Result<TSDataset> {
use arrow_array::cast::AsArray;
use arrow_array::types::Float32Type;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
if x_columns.len() != n_vars * seq_len {
return Err(DataError::FormatError(format!(
"Expected {} x columns, got {}",
n_vars * seq_len,
x_columns.len()
)));
}
let file = File::open(path.as_ref())?;
let builder = ParquetRecordBatchReaderBuilder::try_new(file)
.map_err(|e| DataError::FormatError(format!("Failed to read Parquet: {}", e)))?;
let reader = builder.build()
.map_err(|e| DataError::FormatError(format!("Failed to build Parquet reader: {}", e)))?;
let mut all_x_data: Vec<Vec<f32>> = Vec::new();
let mut all_y_data: Vec<f32> = Vec::new();
let mut n_samples = 0;
for batch_result in reader {
let batch = batch_result
.map_err(|e| DataError::FormatError(format!("Failed to read batch: {}", e)))?;
let batch_size = batch.num_rows();
n_samples += batch_size;
let mut batch_x: Vec<Vec<f32>> = vec![vec![0.0; x_columns.len()]; batch_size];
for (col_idx, col_name) in x_columns.iter().enumerate() {
let col = batch.column_by_name(col_name)
.ok_or_else(|| DataError::FormatError(format!("Column '{}' not found", col_name)))?;
let float_arr = col.as_primitive_opt::<Float32Type>()
.ok_or_else(|| DataError::FormatError(format!(
"Column '{}' is not a float type", col_name
)))?;
for (row_idx, val) in float_arr.iter().enumerate() {
batch_x[row_idx][col_idx] = val.unwrap_or(0.0);
}
}
all_x_data.extend(batch_x);
if let Some(y_col) = y_column {
let col = batch.column_by_name(y_col)
.ok_or_else(|| DataError::FormatError(format!("Column '{}' not found", y_col)))?;
let float_arr = col.as_primitive_opt::<Float32Type>()
.ok_or_else(|| DataError::FormatError(format!(
"Column '{}' is not a float type", y_col
)))?;
for val in float_arr.iter() {
all_y_data.push(val.unwrap_or(0.0));
}
}
}
if n_samples == 0 {
return Err(DataError::InvalidInput("Empty Parquet file".to_string()));
}
let mut x = Array3::<f32>::zeros((n_samples, n_vars, seq_len));
for (row_idx, row_data) in all_x_data.iter().enumerate() {
for (col_idx, &val) in row_data.iter().enumerate() {
let var_idx = col_idx / seq_len;
let step_idx = col_idx % seq_len;
x[[row_idx, var_idx, step_idx]] = val;
}
}
let y = if y_column.is_some() && !all_y_data.is_empty() {
let mut y_arr = Array2::<f32>::zeros((n_samples, 1));
for (i, &label) in all_y_data.iter().enumerate() {
y_arr[[i, 0]] = label;
}
Some(y_arr)
} else {
None
};
TSDataset::from_arrays(x, y)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dataset_creation() {
let x = Array3::<f32>::zeros((100, 3, 50));
let y = Array2::<f32>::zeros((100, 1));
let ds = TSDataset::from_arrays(x, Some(y)).unwrap();
assert_eq!(ds.len(), 100);
assert_eq!(ds.n_vars(), 3);
assert_eq!(ds.seq_len(), 50);
}
}