irithyll 10.0.0

Streaming ML in Rust -- gradient boosted trees, neural architectures (TTT/KAN/MoE/Mamba/SNN), AutoML, kernel methods, and composable pipelines
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Arrow and Parquet integration for zero-copy data ingestion.
//!
//! Provides functions to train SGBT models directly from Arrow `RecordBatch`
//! and Parquet files, avoiding intermediate data copies.

#[cfg(feature = "arrow")]
use arrow::array::{AsArray, Float64Array, RecordBatch};
#[cfg(feature = "arrow")]
use arrow::datatypes::DataType;

/// Train an SGBT model from an Arrow RecordBatch.
///
/// Iterates rows of the batch, treating all Float64 columns (except the target)
/// as features. Columns that are not Float64 are silently skipped.
///
/// The model's loss function is used for gradient/hessian computation
/// (monomorphized -- no `&dyn Loss` parameter needed).
///
/// # Arguments
/// * `model` - The SGBT model to train (generic over loss `L`)
/// * `batch` - Arrow RecordBatch containing feature and target columns
/// * `target_col` - Name of the target column
///
/// # Errors
/// Returns error if `target_col` is not found or is not Float64.
#[cfg(feature = "arrow")]
#[cfg_attr(docsrs, doc(cfg(feature = "arrow")))]
pub fn train_from_record_batch<L: crate::loss::Loss>(
    model: &mut crate::ensemble::SGBT<L>,
    batch: &RecordBatch,
    target_col: &str,
) -> crate::error::Result<()> {
    let schema = batch.schema();

    // Find target column index by name.
    let target_idx = schema.index_of(target_col).map_err(|_| {
        crate::error::IrithyllError::Serialization(format!(
            "target column '{}' not found in RecordBatch schema",
            target_col
        ))
    })?;

    // Verify target column is Float64.
    if schema.field(target_idx).data_type() != &DataType::Float64 {
        return Err(crate::error::IrithyllError::Serialization(format!(
            "target column '{}' must be Float64, got {:?}",
            target_col,
            schema.field(target_idx).data_type()
        )));
    }

    let target_array: &Float64Array = batch.column(target_idx).as_primitive();

    // Collect feature column indices: all Float64 columns except the target.
    let feature_indices: Vec<usize> = schema
        .fields()
        .iter()
        .enumerate()
        .filter(|(i, f)| *i != target_idx && f.data_type() == &DataType::Float64)
        .map(|(i, _)| i)
        .collect();

    // Downcast feature columns to Float64Array slices.
    let feature_columns: Vec<&Float64Array> = feature_indices
        .iter()
        .map(|&i| {
            batch
                .column(i)
                .as_primitive::<arrow::datatypes::Float64Type>()
        })
        .collect();

    let n_features = feature_columns.len();
    let n_rows = batch.num_rows();

    // Pre-allocate feature buffer to avoid per-row allocation.
    let mut features = vec![0.0_f64; n_features];

    for row in 0..n_rows {
        let target = target_array.values()[row];
        if !target.is_finite() {
            continue;
        }
        let mut has_non_finite = false;
        for (j, col) in feature_columns.iter().enumerate() {
            let v = col.values()[row];
            if !v.is_finite() {
                has_non_finite = true;
                break;
            }
            features[j] = v;
        }
        if has_non_finite {
            continue;
        }
        model.train_one(&crate::sample::SampleRef::new(&features, target));
    }

    Ok(())
}

/// Predict from an Arrow RecordBatch, returning a Float64Array of predictions.
///
/// All Float64 columns are treated as features (in schema order).
/// Non-Float64 columns are silently skipped.
#[cfg(feature = "arrow")]
#[cfg_attr(docsrs, doc(cfg(feature = "arrow")))]
pub fn predict_from_record_batch<L: crate::loss::Loss>(
    model: &crate::ensemble::SGBT<L>,
    batch: &RecordBatch,
) -> Float64Array {
    let schema = batch.schema();

    // Find all Float64 columns.
    let feature_columns: Vec<&Float64Array> = schema
        .fields()
        .iter()
        .enumerate()
        .filter(|(_, f)| f.data_type() == &DataType::Float64)
        .map(|(i, _)| {
            batch
                .column(i)
                .as_primitive::<arrow::datatypes::Float64Type>()
        })
        .collect();

    let n_features = feature_columns.len();
    let n_rows = batch.num_rows();

    // Pre-allocate feature buffer.
    let mut features = vec![0.0_f64; n_features];
    let mut predictions = Vec::with_capacity(n_rows);

    for row in 0..n_rows {
        for (j, col) in feature_columns.iter().enumerate() {
            features[j] = col.values()[row];
        }
        predictions.push(model.predict(&features));
    }

    Float64Array::from(predictions)
}

/// Extract feature/target data from a RecordBatch as vectors of (features, target) pairs.
///
/// Useful for inspection or when you need the raw data.
///
/// # Errors
/// Returns error if `target_col` is not found or is not Float64.
#[cfg(feature = "arrow")]
#[cfg_attr(docsrs, doc(cfg(feature = "arrow")))]
pub fn record_batch_to_samples(
    batch: &RecordBatch,
    target_col: &str,
) -> crate::error::Result<Vec<(Vec<f64>, f64)>> {
    let schema = batch.schema();

    let target_idx = schema.index_of(target_col).map_err(|_| {
        crate::error::IrithyllError::Serialization(format!(
            "target column '{}' not found in RecordBatch schema",
            target_col
        ))
    })?;

    if schema.field(target_idx).data_type() != &DataType::Float64 {
        return Err(crate::error::IrithyllError::Serialization(format!(
            "target column '{}' must be Float64, got {:?}",
            target_col,
            schema.field(target_idx).data_type()
        )));
    }

    let target_array: &Float64Array = batch.column(target_idx).as_primitive();

    let feature_columns: Vec<&Float64Array> = schema
        .fields()
        .iter()
        .enumerate()
        .filter(|(i, f)| *i != target_idx && f.data_type() == &DataType::Float64)
        .map(|(i, _)| {
            batch
                .column(i)
                .as_primitive::<arrow::datatypes::Float64Type>()
        })
        .collect();

    let n_features = feature_columns.len();
    let n_rows = batch.num_rows();
    let mut samples = Vec::with_capacity(n_rows);

    for row in 0..n_rows {
        let target = target_array.values()[row];
        if !target.is_finite() {
            continue;
        }
        let mut features = Vec::with_capacity(n_features);
        let mut has_non_finite = false;
        for col in &feature_columns {
            let v = col.values()[row];
            if !v.is_finite() {
                has_non_finite = true;
                break;
            }
            features.push(v);
        }
        if has_non_finite {
            continue;
        }
        samples.push((features, target));
    }

    Ok(samples)
}

/// Read all RecordBatches from a Parquet file.
///
/// # Errors
/// Returns error if the file cannot be opened or read.
#[cfg(feature = "parquet")]
#[cfg_attr(docsrs, doc(cfg(feature = "parquet")))]
pub fn read_parquet_batches(path: &std::path::Path) -> crate::error::Result<Vec<RecordBatch>> {
    let file = std::fs::File::open(path).map_err(|e| {
        crate::error::IrithyllError::Serialization(format!(
            "failed to open parquet file '{}': {}",
            path.display(),
            e
        ))
    })?;

    let reader = parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder::try_new(file)
        .map_err(|e| {
            crate::error::IrithyllError::Serialization(format!(
                "failed to create parquet reader for '{}': {}",
                path.display(),
                e
            ))
        })?
        .build()
        .map_err(|e| {
            crate::error::IrithyllError::Serialization(format!(
                "failed to build parquet reader for '{}': {}",
                path.display(),
                e
            ))
        })?;

    let mut batches = Vec::new();
    for batch_result in reader {
        let batch = batch_result.map_err(|e| {
            crate::error::IrithyllError::Serialization(format!(
                "failed to read parquet batch from '{}': {}",
                path.display(),
                e
            ))
        })?;
        batches.push(batch);
    }

    Ok(batches)
}

/// Stream-train an SGBT model from a Parquet file.
///
/// Reads the file in batches and trains incrementally, avoiding loading the
/// entire dataset into memory at once. The model's loss function is used
/// for gradient/hessian computation (monomorphized).
///
/// # Errors
/// Returns error if the file cannot be read or if the target column is invalid.
#[cfg(feature = "parquet")]
#[cfg_attr(docsrs, doc(cfg(feature = "parquet")))]
pub fn train_from_parquet<L: crate::loss::Loss>(
    model: &mut crate::ensemble::SGBT<L>,
    path: &std::path::Path,
    target_col: &str,
) -> crate::error::Result<()> {
    let file = std::fs::File::open(path).map_err(|e| {
        crate::error::IrithyllError::Serialization(format!(
            "failed to open parquet file '{}': {}",
            path.display(),
            e
        ))
    })?;

    let reader = parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder::try_new(file)
        .map_err(|e| {
            crate::error::IrithyllError::Serialization(format!(
                "failed to create parquet reader for '{}': {}",
                path.display(),
                e
            ))
        })?
        .build()
        .map_err(|e| {
            crate::error::IrithyllError::Serialization(format!(
                "failed to build parquet reader for '{}': {}",
                path.display(),
                e
            ))
        })?;

    for batch_result in reader {
        let batch = batch_result.map_err(|e| {
            crate::error::IrithyllError::Serialization(format!(
                "failed to read parquet batch from '{}': {}",
                path.display(),
                e
            ))
        })?;
        train_from_record_batch(model, &batch, target_col)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    // Only run tests when the arrow feature is enabled.
    #[cfg(feature = "arrow")]
    mod arrow_tests {
        use super::super::*;
        use arrow::array::{Float64Array, RecordBatch};
        use arrow::datatypes::{DataType, Field, Schema};
        use std::sync::Arc;

        fn make_test_batch() -> RecordBatch {
            let schema = Arc::new(Schema::new(vec![
                Field::new("x1", DataType::Float64, false),
                Field::new("x2", DataType::Float64, false),
                Field::new("target", DataType::Float64, false),
            ]));
            let x1 = Arc::new(Float64Array::from(vec![1.0, 2.0, 3.0, 4.0, 5.0]));
            let x2 = Arc::new(Float64Array::from(vec![0.5, 1.0, 1.5, 2.0, 2.5]));
            let target = Arc::new(Float64Array::from(vec![2.5, 5.0, 7.5, 10.0, 12.5]));
            RecordBatch::try_new(schema, vec![x1, x2, target]).unwrap()
        }

        #[test]
        fn train_from_batch_does_not_panic() {
            use crate::ensemble::config::SGBTConfig;

            let config = SGBTConfig::builder()
                .n_steps(5)
                .learning_rate(0.1)
                .grace_period(2)
                .build()
                .unwrap();
            let mut model = crate::ensemble::SGBT::new(config);
            let batch = make_test_batch();

            let result = train_from_record_batch(&mut model, &batch, "target");
            assert!(result.is_ok());
            assert_eq!(model.n_samples_seen(), 5);
        }

        #[test]
        fn predict_from_batch_returns_correct_count() {
            use crate::ensemble::config::SGBTConfig;

            let config = SGBTConfig::builder()
                .n_steps(3)
                .learning_rate(0.1)
                .grace_period(2)
                .build()
                .unwrap();
            let model = crate::ensemble::SGBT::new(config);

            // Create a batch without target column for prediction.
            let schema = Arc::new(Schema::new(vec![
                Field::new("x1", DataType::Float64, false),
                Field::new("x2", DataType::Float64, false),
            ]));
            let x1 = Arc::new(Float64Array::from(vec![1.0, 2.0, 3.0]));
            let x2 = Arc::new(Float64Array::from(vec![0.5, 1.0, 1.5]));
            let batch = RecordBatch::try_new(schema, vec![x1, x2]).unwrap();

            let preds = predict_from_record_batch(&model, &batch);
            assert_eq!(preds.len(), 3);
            for i in 0..3 {
                assert!(preds.value(i).is_finite());
            }
        }

        #[test]
        fn missing_target_column_returns_error() {
            use crate::ensemble::config::SGBTConfig;

            let config = SGBTConfig::builder().n_steps(3).build().unwrap();
            let mut model = crate::ensemble::SGBT::new(config);
            let batch = make_test_batch();

            let result = train_from_record_batch(&mut model, &batch, "nonexistent");
            assert!(result.is_err());
        }

        #[test]
        fn record_batch_to_samples_works() {
            let batch = make_test_batch();
            let samples = record_batch_to_samples(&batch, "target").unwrap();
            assert_eq!(samples.len(), 5);
            assert_eq!(samples[0].0.len(), 2); // 2 feature columns
            assert!((samples[0].1 - 2.5).abs() < 1e-10); // first target
        }

        #[test]
        fn nan_inf_rows_are_skipped_in_training() {
            use crate::ensemble::config::SGBTConfig;

            let schema = Arc::new(Schema::new(vec![
                Field::new("x1", DataType::Float64, false),
                Field::new("target", DataType::Float64, false),
            ]));
            let x1 = Arc::new(Float64Array::from(vec![
                1.0,
                f64::NAN,
                3.0,
                f64::INFINITY,
                5.0,
            ]));
            let target = Arc::new(Float64Array::from(vec![2.0, 4.0, f64::NAN, 8.0, 10.0]));
            let batch = RecordBatch::try_new(schema, vec![x1, target]).unwrap();

            let config = SGBTConfig::builder()
                .n_steps(3)
                .learning_rate(0.1)
                .grace_period(2)
                .build()
                .unwrap();
            let mut model = crate::ensemble::SGBT::new(config);

            let result = train_from_record_batch(&mut model, &batch, "target");
            assert!(result.is_ok());
            // Rows 0 (1.0, 2.0) and 4 (5.0, 10.0) are clean. Rows 1,2,3 have NaN/Inf.
            assert_eq!(model.n_samples_seen(), 2);
        }

        #[test]
        fn nan_inf_rows_are_skipped_in_samples() {
            let schema = Arc::new(Schema::new(vec![
                Field::new("x1", DataType::Float64, false),
                Field::new("target", DataType::Float64, false),
            ]));
            let x1 = Arc::new(Float64Array::from(vec![1.0, f64::NAN, 3.0]));
            let target = Arc::new(Float64Array::from(vec![2.0, 4.0, f64::NEG_INFINITY]));
            let batch = RecordBatch::try_new(schema, vec![x1, target]).unwrap();

            let samples = record_batch_to_samples(&batch, "target").unwrap();
            // Only row 0 is fully finite.
            assert_eq!(samples.len(), 1);
            assert!((samples[0].0[0] - 1.0).abs() < 1e-10);
            assert!((samples[0].1 - 2.0).abs() < 1e-10);
        }
    }
}