1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! The `Frame` data model — the boundary type the whole public API speaks.
//!
//! A [`Frame`] is a contiguous, row-major `f64` buffer plus a schema (column
//! names). It is the one type users pass around; backend adapters convert it
//! to their native array type *at the edge only* (see the smartcore adapter's
//! `as_dense`). This is how Millwright keeps the two-`ndarray`-worlds problem
//! out of user code — exactly how pandas/NumPy sit under scikit-learn.
//!
//! A [`Dataset`] is a `Frame` paired with a target column: what an
//! [`Estimator`](crate::traits::Estimator) fits on.
use std::path::Path;
use crate::error::{Error, Result};
/// The role of a column, so schema-aware steps (encoders, profiling) need not
/// guess. Defaults to [`Numeric`](Dtype::Numeric); [`Table`](crate::table::Table)
/// marks the columns it knows are [`Categorical`](Dtype::Categorical) as it
/// lowers into a `Frame`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Dtype {
/// A continuous or already-numeric column.
Numeric,
/// A nominal category, carried as an integer code.
Categorical,
}
/// A contiguous, row-major table of `f64` with named columns.
///
/// Layout: element `(r, c)` lives at `buf[r * ncols + c]`. Each column also
/// carries a [`Dtype`] (defaulting to `Numeric`) so downstream steps know which
/// columns are truly categorical rather than inferring it from the values.
#[derive(Clone, Debug, PartialEq)]
pub struct Frame {
buf: Vec<f64>,
nrows: usize,
ncols: usize,
columns: Vec<String>,
dtypes: Vec<Dtype>,
}
impl Frame {
/// Build a frame from a flat row-major buffer.
///
/// Fails if `buf.len() != nrows * ncols` or if `columns.len() != ncols`.
pub fn new(buf: Vec<f64>, nrows: usize, ncols: usize, columns: Vec<String>) -> Result<Self> {
if buf.len() != nrows * ncols {
return Err(Error::Shape(format!(
"buffer has {} elements but shape {}x{} needs {}",
buf.len(),
nrows,
ncols,
nrows * ncols
)));
}
if columns.len() != ncols {
return Err(Error::Schema(format!(
"{} column names for {} columns",
columns.len(),
ncols
)));
}
Ok(Frame {
buf,
nrows,
ncols,
columns,
dtypes: vec![Dtype::Numeric; ncols],
})
}
/// Set the per-column dtypes (builder). Fails if the count disagrees with
/// the number of columns.
pub fn with_dtypes(mut self, dtypes: Vec<Dtype>) -> Result<Self> {
if dtypes.len() != self.ncols {
return Err(Error::Schema(format!(
"{} dtypes for {} columns",
dtypes.len(),
self.ncols
)));
}
self.dtypes = dtypes;
Ok(self)
}
/// Build a frame from a vector of equal-length rows.
pub fn from_rows(rows: Vec<Vec<f64>>, columns: Vec<String>) -> Result<Self> {
let nrows = rows.len();
let ncols = columns.len();
let mut buf = Vec::with_capacity(nrows * ncols);
for (r, row) in rows.into_iter().enumerate() {
if row.len() != ncols {
return Err(Error::Shape(format!(
"row {r} has {} values, expected {ncols}",
row.len()
)));
}
buf.extend(row);
}
Frame::new(buf, nrows, ncols, columns)
}
/// Read an all-numeric CSV (a header row, then `f64` rows) into a frame.
///
/// Empty cells become `NaN` (ready for a [`SimpleImputer`]). This is the
/// dependency-free fast path for already-numeric data; for typed data
/// (strings, categories, dates) use [`Table`](crate::table::Table) behind
/// the `eda` feature.
///
/// [`SimpleImputer`]: crate::transform::SimpleImputer
pub fn from_csv(path: impl AsRef<Path>) -> Result<Frame> {
let text = std::fs::read_to_string(path.as_ref())
.map_err(|e| Error::Backend(format!("read csv: {e}")))?;
let mut lines = text.lines().filter(|l| !l.trim().is_empty());
let header = lines
.next()
.ok_or_else(|| Error::Schema("empty CSV".into()))?;
let columns: Vec<String> = header.split(',').map(|s| s.trim().to_string()).collect();
let ncols = columns.len();
let mut rows = Vec::new();
for (i, line) in lines.enumerate() {
let mut row = Vec::with_capacity(ncols);
for cell in line.split(',') {
let t = cell.trim();
row.push(if t.is_empty() {
f64::NAN
} else {
t.parse::<f64>().map_err(|_| {
Error::Schema(format!("row {}: '{t}' is not a number", i + 2))
})?
});
}
rows.push(row);
}
Frame::from_rows(rows, columns)
}
/// Number of rows.
pub fn nrows(&self) -> usize {
self.nrows
}
/// Number of columns.
pub fn ncols(&self) -> usize {
self.ncols
}
/// `(nrows, ncols)`.
pub fn shape(&self) -> (usize, usize) {
(self.nrows, self.ncols)
}
/// The column names, in order.
pub fn columns(&self) -> &[String] {
&self.columns
}
/// The per-column dtypes (all [`Dtype::Numeric`] unless set).
pub fn dtypes(&self) -> &[Dtype] {
&self.dtypes
}
/// The dtype of column `c`.
pub fn dtype(&self, c: usize) -> Dtype {
self.dtypes[c]
}
/// The indices of columns marked [`Dtype::Categorical`].
pub fn categorical_columns(&self) -> Vec<usize> {
(0..self.ncols)
.filter(|&c| self.dtypes[c] == Dtype::Categorical)
.collect()
}
/// The flat row-major backing buffer.
pub fn buf(&self) -> &[f64] {
&self.buf
}
/// Index of a column by name, if present.
pub fn column_index(&self, name: &str) -> Option<usize> {
self.columns.iter().position(|c| c == name)
}
/// A single row as a contiguous slice.
pub fn row(&self, r: usize) -> &[f64] {
let start = r * self.ncols;
&self.buf[start..start + self.ncols]
}
/// Element at `(r, c)`.
pub fn get(&self, r: usize, c: usize) -> f64 {
self.buf[r * self.ncols + c]
}
/// Extract one column as an owned vector.
pub fn column(&self, c: usize) -> Vec<f64> {
(0..self.nrows).map(|r| self.get(r, c)).collect()
}
/// Materialize the frame as a vector of rows.
///
/// This is the shape most backend adapters want at the conversion edge.
pub fn as_rows(&self) -> Vec<Vec<f64>> {
(0..self.nrows).map(|r| self.row(r).to_vec()).collect()
}
/// A new frame holding only the given rows, in the given order.
///
/// Used to materialize CV folds and bootstrap samples. Row indices are
/// assumed in-range.
pub fn select_rows(&self, idx: &[usize]) -> Frame {
let mut buf = Vec::with_capacity(idx.len() * self.ncols);
for &r in idx {
buf.extend_from_slice(self.row(r));
}
Frame {
buf,
nrows: idx.len(),
ncols: self.ncols,
columns: self.columns.clone(),
dtypes: self.dtypes.clone(),
}
}
/// Assert that another frame has the same columns (used when a fitted step
/// is applied to new data).
pub(crate) fn require_columns(&self, expected: &[String]) -> Result<()> {
if self.columns != expected {
return Err(Error::Schema(format!(
"expected columns {expected:?}, got {:?}",
self.columns
)));
}
Ok(())
}
}
/// A training dataset: a feature [`Frame`] plus a target column.
#[derive(Clone, Debug, PartialEq)]
pub struct Dataset {
features: Frame,
target: Vec<f64>,
}
impl Dataset {
/// Pair features with a target. Fails if the lengths disagree.
pub fn new(features: Frame, target: Vec<f64>) -> Result<Self> {
if target.len() != features.nrows() {
return Err(Error::Shape(format!(
"target has {} values but frame has {} rows",
target.len(),
features.nrows()
)));
}
Ok(Dataset { features, target })
}
/// The feature frame.
pub fn features(&self) -> &Frame {
&self.features
}
/// The target column.
pub fn target(&self) -> &[f64] {
&self.target
}
/// Replace the feature frame, keeping the target (used by `Pipeline::fit`
/// as it threads a frame through its transformers).
pub(crate) fn with_features(&self, features: Frame) -> Dataset {
Dataset {
features,
target: self.target.clone(),
}
}
/// A new dataset holding only the given rows, in the given order — a CV
/// fold or a bootstrap sample.
pub fn select(&self, idx: &[usize]) -> Dataset {
Dataset {
features: self.features.select_rows(idx),
target: idx.iter().map(|&i| self.target[i]).collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_mismatched_buffer() {
assert!(Frame::new(vec![1.0, 2.0], 2, 2, vec!["a".into(), "b".into()]).is_err());
}
#[test]
fn round_trips_rows() {
let f = Frame::from_rows(
vec![vec![1.0, 2.0], vec![3.0, 4.0]],
vec!["a".into(), "b".into()],
)
.unwrap();
assert_eq!(f.shape(), (2, 2));
assert_eq!(f.get(1, 0), 3.0);
assert_eq!(f.column(1), vec![2.0, 4.0]);
assert_eq!(f.as_rows(), vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
}
#[test]
fn reads_numeric_csv_with_blanks_as_nan() {
let path = std::env::temp_dir().join("mw_frame_from_csv.csv");
std::fs::write(&path, "a,b\n1,2\n3,\n").unwrap();
let f = Frame::from_csv(&path).unwrap();
assert_eq!(f.shape(), (2, 2));
assert_eq!(f.get(0, 1), 2.0);
assert!(f.get(1, 1).is_nan());
let _ = std::fs::remove_file(&path);
}
#[test]
fn dtypes_default_numeric_and_survive_selection() {
let f = Frame::from_rows(
vec![vec![1.0, 2.0], vec![3.0, 4.0]],
vec!["a".into(), "b".into()],
)
.unwrap();
assert_eq!(f.dtypes(), &[Dtype::Numeric, Dtype::Numeric]);
let typed = f
.with_dtypes(vec![Dtype::Categorical, Dtype::Numeric])
.unwrap();
assert_eq!(typed.categorical_columns(), vec![0]);
assert_eq!(typed.dtype(0), Dtype::Categorical);
// dtypes are preserved through a CV-style row selection
assert_eq!(
typed.select_rows(&[1]).dtypes(),
&[Dtype::Categorical, Dtype::Numeric]
);
}
#[test]
fn dataset_checks_lengths() {
let f = Frame::from_rows(vec![vec![1.0], vec![2.0]], vec!["a".into()]).unwrap();
assert!(Dataset::new(f.clone(), vec![0.0]).is_err());
assert!(Dataset::new(f, vec![0.0, 1.0]).is_ok());
}
}