treeboost 0.1.0

High-performance Gradient Boosted Decision Tree engine for large-scale tabular data
Documentation
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
//! Python bindings for DataPipeline
//!
//! Provides the dirty data pipeline for handling messy real-world data:
//! 1. Count-Min Sketch filtering for rare categories
//! 2. Ordered Target Encoding with M-Estimate smoothing
//! 3. T-Digest quantile binning

use pyo3::prelude::*;

use crate::dataset::{DataPipeline, PipelineConfig, PipelineState};

use super::types::{PyBinnedDataset, PyFeatureInfo};

/// Python wrapper for PipelineConfig
///
/// Configuration for the dirty data pipeline.
///
/// Example:
/// ```python
/// from treeboost import PipelineConfig
///
/// config = (
///     PipelineConfig()
///     .with_num_bins(255)
///     .with_cms_params(eps=0.001, confidence=0.99, min_count=5)
///     .with_smoothing(10.0)
/// )
/// ```
#[pyclass(name = "PipelineConfig")]
#[derive(Clone)]
pub struct PyPipelineConfig {
    pub(crate) inner: PipelineConfig,
}

#[pymethods]
impl PyPipelineConfig {
    /// Create a new PipelineConfig with default settings
    #[new]
    fn new() -> Self {
        Self {
            inner: PipelineConfig::default(),
        }
    }

    /// Set the number of bins for quantile binning
    ///
    /// Args:
    ///     num_bins: Number of bins (1-255, default: 255)
    ///
    /// Returns:
    ///     Self for method chaining
    fn with_num_bins(&self, num_bins: usize) -> PyResult<Self> {
        if num_bins == 0 || num_bins > 255 {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "num_bins must be between 1 and 255",
            ));
        }
        Ok(Self {
            inner: self.inner.clone().with_num_bins(num_bins),
        })
    }

    /// Set Count-Min Sketch parameters for rare category filtering
    ///
    /// Args:
    ///     eps: Error tolerance (default: 0.001 = 0.1%)
    ///     confidence: Confidence level (default: 0.99)
    ///     min_count: Minimum count for a category to be kept (default: 5)
    ///
    /// Returns:
    ///     Self for method chaining
    #[pyo3(signature = (eps=0.001, confidence=0.99, min_count=5))]
    fn with_cms_params(&self, eps: f64, confidence: f64, min_count: u64) -> PyResult<Self> {
        if eps <= 0.0 || eps >= 1.0 {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "eps must be between 0 and 1",
            ));
        }
        if confidence <= 0.0 || confidence >= 1.0 {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "confidence must be between 0 and 1",
            ));
        }
        Ok(Self {
            inner: self
                .inner
                .clone()
                .with_cms_params(eps, confidence, min_count),
        })
    }

    /// Set the M-Estimate smoothing parameter for target encoding
    ///
    /// Args:
    ///     smoothing: Smoothing parameter (default: 10.0)
    ///         - Larger values bias towards global mean
    ///         - Smaller values trust category means more
    ///
    /// Returns:
    ///     Self for method chaining
    fn with_smoothing(&self, smoothing: f64) -> PyResult<Self> {
        if smoothing < 0.0 {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "smoothing must be non-negative",
            ));
        }
        Ok(Self {
            inner: self.inner.clone().with_smoothing(smoothing),
        })
    }

    /// Number of bins for quantile binning
    #[getter]
    fn num_bins(&self) -> usize {
        self.inner.num_bins
    }

    /// CMS error tolerance
    #[getter]
    fn cms_eps(&self) -> f64 {
        self.inner.cms_eps
    }

    /// CMS confidence level
    #[getter]
    fn cms_confidence(&self) -> f64 {
        self.inner.cms_confidence
    }

    /// Minimum category count
    #[getter]
    fn min_category_count(&self) -> u64 {
        self.inner.min_category_count
    }

    /// Target encoding smoothing parameter
    #[getter]
    fn target_encoding_smoothing(&self) -> f64 {
        self.inner.target_encoding_smoothing
    }

    fn __repr__(&self) -> String {
        format!(
            "PipelineConfig(num_bins={}, cms_eps={}, min_count={}, smoothing={})",
            self.inner.num_bins,
            self.inner.cms_eps,
            self.inner.min_category_count,
            self.inner.target_encoding_smoothing
        )
    }
}

/// Python wrapper for PipelineState
///
/// Learned state from training that is needed for inference.
/// Can be serialized and loaded for production deployment.
#[pyclass(name = "PipelineState")]
#[derive(Clone)]
pub struct PyPipelineState {
    pub(crate) inner: PipelineState,
}

#[pymethods]
impl PyPipelineState {
    /// Get feature info for all columns
    #[getter]
    fn feature_info(&self) -> Vec<PyFeatureInfo> {
        self.inner.feature_info.iter().map(|fi| fi.into()).collect()
    }

    /// Column names in order
    #[getter]
    fn column_order(&self) -> Vec<String> {
        self.inner.column_order.clone()
    }

    /// Indices of categorical columns
    #[getter]
    fn categorical_indices(&self) -> Vec<usize> {
        self.inner.categorical_indices.clone()
    }

    /// Number of categorical columns
    #[getter]
    fn num_categorical(&self) -> usize {
        self.inner.categorical_encodings.len()
    }

    /// Number of columns
    fn __len__(&self) -> usize {
        self.inner.column_order.len()
    }

    fn __repr__(&self) -> String {
        format!(
            "PipelineState(columns={}, categorical={})",
            self.inner.column_order.len(),
            self.inner.categorical_encodings.len()
        )
    }
}

impl From<PipelineState> for PyPipelineState {
    fn from(state: PipelineState) -> Self {
        Self { inner: state }
    }
}

/// Python wrapper for DataPipeline
///
/// The dirty data pipeline for handling messy real-world data:
/// 1. Count-Min Sketch filtering (rare categories -> "unknown")
/// 2. Ordered Target Encoding (with M-Estimate smoothing)
/// 3. T-Digest quantile binning
///
/// Example:
/// ```python
/// from treeboost import DataPipeline, PipelineConfig
///
/// # Create pipeline with config
/// config = PipelineConfig().with_num_bins(255).with_smoothing(10.0)
/// pipeline = DataPipeline(config)
///
/// # Train: fit and transform
/// dataset, state = pipeline.load_csv_for_training(
///     "train.csv",
///     target="price",
///     categoricals=["city", "type"]
/// )
///
/// # Inference: transform using learned state
/// test_dataset = pipeline.load_csv_for_inference("test.csv", state)
/// ```
#[pyclass(name = "DataPipeline")]
pub struct PyDataPipeline {
    inner: DataPipeline,
}

#[pymethods]
impl PyDataPipeline {
    /// Create a new DataPipeline
    ///
    /// Args:
    ///     config: Optional PipelineConfig. If None, uses default config.
    #[new]
    #[pyo3(signature = (config=None))]
    fn new(config: Option<PyPipelineConfig>) -> Self {
        let cfg = config.map(|c| c.inner).unwrap_or_default();
        Self {
            inner: DataPipeline::new(cfg),
        }
    }

    /// Create a DataPipeline with default configuration
    #[staticmethod]
    fn default() -> Self {
        Self {
            inner: DataPipeline::default_config(),
        }
    }

    /// Load and process a CSV file for training
    ///
    /// Applies the full pipeline:
    /// 1. CMS filtering for rare categories
    /// 2. Ordered target encoding
    /// 3. Quantile binning
    ///
    /// Args:
    ///     path: Path to the CSV file
    ///     target: Name of the target column
    ///     categoricals: Optional list of categorical column names.
    ///                   If None, auto-detects string columns.
    ///
    /// Returns:
    ///     Tuple of (BinnedDataset, PipelineState)
    #[pyo3(signature = (path, target, categoricals=None))]
    fn load_csv_for_training(
        &self,
        py: Python<'_>,
        path: &str,
        target: &str,
        categoricals: Option<Vec<String>>,
    ) -> PyResult<(PyBinnedDataset, PyPipelineState)> {
        let cat_refs: Option<Vec<&str>> = categoricals
            .as_ref()
            .map(|v| v.iter().map(|s| s.as_str()).collect());

        let result = py.allow_threads(|| {
            self.inner
                .load_csv_for_training(path, target, cat_refs.as_deref())
        });

        match result {
            Ok((dataset, state)) => {
                Ok((PyBinnedDataset::from(dataset), PyPipelineState::from(state)))
            }
            Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
                "Failed to process CSV for training: {}",
                e
            ))),
        }
    }

    /// Load and process a Parquet file for training
    ///
    /// Applies the full pipeline:
    /// 1. CMS filtering for rare categories
    /// 2. Ordered target encoding
    /// 3. Quantile binning
    ///
    /// Args:
    ///     path: Path to the Parquet file
    ///     target: Name of the target column
    ///     categoricals: Optional list of categorical column names.
    ///                   If None, auto-detects string columns.
    ///
    /// Returns:
    ///     Tuple of (BinnedDataset, PipelineState)
    #[pyo3(signature = (path, target, categoricals=None))]
    fn load_parquet_for_training(
        &self,
        py: Python<'_>,
        path: &str,
        target: &str,
        categoricals: Option<Vec<String>>,
    ) -> PyResult<(PyBinnedDataset, PyPipelineState)> {
        let cat_refs: Option<Vec<&str>> = categoricals
            .as_ref()
            .map(|v| v.iter().map(|s| s.as_str()).collect());

        let result = py.allow_threads(|| {
            self.inner
                .load_parquet_for_training(path, target, cat_refs.as_deref())
        });

        match result {
            Ok((dataset, state)) => {
                Ok((PyBinnedDataset::from(dataset), PyPipelineState::from(state)))
            }
            Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
                "Failed to process Parquet for training: {}",
                e
            ))),
        }
    }

    /// Load and process a CSV file for inference using learned state
    ///
    /// Applies encodings and binning learned during training.
    ///
    /// Args:
    ///     path: Path to the CSV file
    ///     state: PipelineState from training
    ///
    /// Returns:
    ///     BinnedDataset with consistent encoding/binning (dummy targets)
    fn load_csv_for_inference(
        &self,
        py: Python<'_>,
        path: &str,
        state: &PyPipelineState,
    ) -> PyResult<PyBinnedDataset> {
        let result = py.allow_threads(|| self.inner.load_csv_for_inference(path, &state.inner));

        match result {
            Ok(dataset) => Ok(PyBinnedDataset::from(dataset)),
            Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
                "Failed to process CSV for inference: {}",
                e
            ))),
        }
    }

    /// Load and process a Parquet file for inference using learned state
    ///
    /// Applies encodings and binning learned during training.
    ///
    /// Args:
    ///     path: Path to the Parquet file
    ///     state: PipelineState from training
    ///
    /// Returns:
    ///     BinnedDataset with consistent encoding/binning (dummy targets)
    fn load_parquet_for_inference(
        &self,
        py: Python<'_>,
        path: &str,
        state: &PyPipelineState,
    ) -> PyResult<PyBinnedDataset> {
        let result = py.allow_threads(|| self.inner.load_parquet_for_inference(path, &state.inner));

        match result {
            Ok(dataset) => Ok(PyBinnedDataset::from(dataset)),
            Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
                "Failed to process Parquet for inference: {}",
                e
            ))),
        }
    }

    fn __repr__(&self) -> &'static str {
        "DataPipeline(...)"
    }
}

/// Register pipeline classes with the module
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<PyPipelineConfig>()?;
    m.add_class::<PyPipelineState>()?;
    m.add_class::<PyDataPipeline>()?;
    Ok(())
}