scirs2-transform 0.4.1

Data transformation module for SciRS2 (scirs2-transform)
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Pipeline API for chaining transformations
//!
//! This module provides utilities for creating pipelines of transformations
//! that can be applied sequentially, similar to scikit-learn's Pipeline.

// mod adapters;

// pub use adapters::boxed;

use scirs2_core::ndarray::{Array2, ArrayBase, Data, Ix2};
use scirs2_core::numeric::{Float, NumCast};
use std::any::Any;

use crate::error::{Result, TransformError};

/// Trait for all transformers that can be used in pipelines
pub trait Transformer: Send + Sync {
    /// Fits the transformer to the input data
    fn fit(&mut self, x: &Array2<f64>) -> Result<()>;

    /// Transforms the input data
    fn transform(&self, x: &Array2<f64>) -> Result<Array2<f64>>;

    /// Fits and transforms the data in one step
    fn fit_transform(&mut self, x: &Array2<f64>) -> Result<Array2<f64>> {
        self.fit(x)?;
        self.transform(x)
    }

    /// Returns a boxed clone of the transformer
    fn clone_box(&self) -> Box<dyn Transformer>;

    /// Returns the transformer as Any for downcasting
    fn as_any(&self) -> &dyn Any;

    /// Returns the transformer as mutable Any for downcasting
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// A pipeline of transformations to be applied sequentially
pub struct Pipeline {
    /// List of named steps in the pipeline
    steps: Vec<(String, Box<dyn Transformer>)>,
    /// Whether the pipeline has been fitted
    fitted: bool,
}

impl Pipeline {
    /// Creates a new empty pipeline
    pub fn new() -> Self {
        Pipeline {
            steps: Vec::new(),
            fitted: false,
        }
    }

    /// Adds a step to the pipeline
    ///
    /// # Arguments
    /// * `name` - Name of the step
    /// * `transformer` - The transformer to add
    ///
    /// # Returns
    /// * `Self` - The pipeline for chaining
    pub fn add_step(mut self, name: impl Into<String>, transformer: Box<dyn Transformer>) -> Self {
        self.steps.push((name.into(), transformer));
        self
    }

    /// Fits all steps in the pipeline
    ///
    /// # Arguments
    /// * `x` - The input data
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, Err otherwise
    pub fn fit<S>(&mut self, x: &ArrayBase<S, Ix2>) -> Result<()>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        let mut x_transformed = x.mapv(|x| NumCast::from(x).unwrap_or(0.0));

        for (name, transformer) in &mut self.steps {
            transformer.fit(&x_transformed).map_err(|e| {
                TransformError::TransformationError(format!("Failed to fit step '{name}': {e}"))
            })?;

            x_transformed = transformer.transform(&x_transformed).map_err(|e| {
                TransformError::TransformationError(format!(
                    "Failed to transform in step '{name}': {e}"
                ))
            })?;
        }

        self.fitted = true;
        Ok(())
    }

    /// Transforms data through all steps in the pipeline
    ///
    /// # Arguments
    /// * `x` - The input data
    ///
    /// # Returns
    /// * `Result<Array2<f64>>` - The transformed data
    pub fn transform<S>(&self, x: &ArrayBase<S, Ix2>) -> Result<Array2<f64>>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        if !self.fitted {
            return Err(TransformError::TransformationError(
                "Pipeline has not been fitted".to_string(),
            ));
        }

        let mut x_transformed = x.mapv(|x| NumCast::from(x).unwrap_or(0.0));

        for (name, transformer) in &self.steps {
            x_transformed = transformer.transform(&x_transformed).map_err(|e| {
                TransformError::TransformationError(format!(
                    "Failed to transform in step '{name}': {e}"
                ))
            })?;
        }

        Ok(x_transformed)
    }

    /// Fits and transforms data through all steps in the pipeline
    ///
    /// # Arguments
    /// * `x` - The input data
    ///
    /// # Returns
    /// * `Result<Array2<f64>>` - The transformed data
    pub fn fit_transform<S>(&mut self, x: &ArrayBase<S, Ix2>) -> Result<Array2<f64>>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        self.fit(x)?;
        self.transform(x)
    }

    /// Returns the number of steps in the pipeline
    pub fn len(&self) -> usize {
        self.steps.len()
    }

    /// Returns whether the pipeline is empty
    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }

    /// Gets a reference to a step by name
    pub fn get_step(&self, name: &str) -> Option<&dyn Transformer> {
        self.steps
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, t)| t.as_ref())
    }

    /// Gets a mutable reference to a step by name
    pub fn get_step_mut(&mut self, name: &str) -> Option<&mut Box<dyn Transformer>> {
        self.steps
            .iter_mut()
            .find(|(n, _)| n == name)
            .map(|(_, t)| t)
    }
}

impl Default for Pipeline {
    fn default() -> Self {
        Self::new()
    }
}

/// ColumnTransformer applies different transformers to different columns
pub struct ColumnTransformer {
    /// List of transformers with their column indices
    transformers: Vec<(String, Box<dyn Transformer>, Vec<usize>)>,
    /// Whether to pass through columns not specified
    remainder: RemainderOption,
    /// Whether the transformer has been fitted
    fitted: bool,
}

/// Options for handling columns not specified in transformers
#[derive(Debug, Clone, Copy)]
pub enum RemainderOption {
    /// Drop unspecified columns
    Drop,
    /// Pass through unspecified columns unchanged
    Passthrough,
}

impl ColumnTransformer {
    /// Creates a new ColumnTransformer
    ///
    /// # Arguments
    /// * `remainder` - How to handle unspecified columns
    pub fn new(remainder: RemainderOption) -> Self {
        ColumnTransformer {
            transformers: Vec::new(),
            remainder,
            fitted: false,
        }
    }

    /// Adds a transformer for specific columns
    ///
    /// # Arguments
    /// * `name` - Name of the transformer
    /// * `transformer` - The transformer to apply
    /// * `columns` - Column indices to apply the transformer to
    ///
    /// # Returns
    /// * `Self` - The ColumnTransformer for chaining
    pub fn add_transformer(
        mut self,
        name: impl Into<String>,
        transformer: Box<dyn Transformer>,
        columns: Vec<usize>,
    ) -> Self {
        self.transformers.push((name.into(), transformer, columns));
        self
    }

    /// Fits all transformers to their respective columns
    ///
    /// # Arguments
    /// * `x` - The input data
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, Err otherwise
    pub fn fit<S>(&mut self, x: &ArrayBase<S, Ix2>) -> Result<()>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        let x_f64 = x.mapv(|x| NumCast::from(x).unwrap_or(0.0));
        let n_features = x_f64.shape()[1];

        // Validate column indices
        for (name_, transformer, columns) in &self.transformers {
            for &col in columns {
                if col >= n_features {
                    return Err(TransformError::InvalidInput(format!(
                        "Column index {col} in transformer '{name_}' exceeds number of features {n_features}"
                    )));
                }
            }
        }

        // Fit each transformer on its columns
        for (name, transformer, columns) in &mut self.transformers {
            // Extract relevant columns
            let subset = extract_columns(&x_f64, columns);

            transformer.fit(&subset).map_err(|e| {
                TransformError::TransformationError(format!(
                    "Failed to fit transformer '{name}': {e}"
                ))
            })?;
        }

        self.fitted = true;
        Ok(())
    }

    /// Transforms data using all configured transformers
    ///
    /// # Arguments
    /// * `x` - The input data
    ///
    /// # Returns
    /// * `Result<Array2<f64>>` - The transformed data
    pub fn transform<S>(&self, x: &ArrayBase<S, Ix2>) -> Result<Array2<f64>>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        if !self.fitted {
            return Err(TransformError::TransformationError(
                "ColumnTransformer has not been fitted".to_string(),
            ));
        }

        let x_f64 = x.mapv(|x| NumCast::from(x).unwrap_or(0.0));
        let n_samples = x_f64.shape()[0];
        let n_features = x_f64.shape()[1];

        // Track which columns have been transformed
        let mut used_columns = vec![false; n_features];
        let mut transformed_parts = Vec::new();

        // Transform each group of columns
        for (name, transformer, columns) in &self.transformers {
            // Mark columns as used
            for &col in columns {
                used_columns[col] = true;
            }

            // Extract and transform columns
            let subset = extract_columns(&x_f64, columns);
            let transformed = transformer.transform(&subset).map_err(|e| {
                TransformError::TransformationError(format!(
                    "Failed to transform with '{name}': {e}"
                ))
            })?;

            transformed_parts.push(transformed);
        }

        // Handle remainder columns
        match self.remainder {
            RemainderOption::Passthrough => {
                // Collect unused columns
                let unused_columns: Vec<usize> =
                    (0..n_features).filter(|&i| !used_columns[i]).collect();

                if !unused_columns.is_empty() {
                    let remainder = extract_columns(&x_f64, &unused_columns);
                    transformed_parts.push(remainder);
                }
            }
            RemainderOption::Drop => {
                // Do nothing - unused columns are dropped
            }
        }

        // Concatenate all parts horizontally
        if transformed_parts.is_empty() {
            return Ok(Array2::zeros((n_samples, 0)));
        }

        concatenate_horizontal(&transformed_parts)
    }

    /// Fits and transforms data in one step
    pub fn fit_transform<S>(&mut self, x: &ArrayBase<S, Ix2>) -> Result<Array2<f64>>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        self.fit(x)?;
        self.transform(x)
    }
}

/// Extracts specific columns from a 2D array
#[allow(dead_code)]
fn extract_columns(data: &Array2<f64>, columns: &[usize]) -> Array2<f64> {
    let n_samples = data.shape()[0];
    let n_cols = columns.len();

    let mut result = Array2::zeros((n_samples, n_cols));

    for (j, &col_idx) in columns.iter().enumerate() {
        for i in 0..n_samples {
            result[[i, j]] = data[[i, col_idx]];
        }
    }

    result
}

/// Concatenates arrays horizontally
#[allow(dead_code)]
fn concatenate_horizontal(arrays: &[Array2<f64>]) -> Result<Array2<f64>> {
    if arrays.is_empty() {
        return Err(TransformError::InvalidInput(
            "Cannot concatenate empty array list".to_string(),
        ));
    }

    let n_samples = arrays[0].shape()[0];
    let total_features: usize = arrays.iter().map(|a| a.shape()[1]).sum();

    // Verify all _arrays have the same number of samples
    for arr in arrays {
        if arr.shape()[0] != n_samples {
            return Err(TransformError::InvalidInput(
                "All _arrays must have the same number of samples".to_string(),
            ));
        }
    }

    let mut result = Array2::zeros((n_samples, total_features));
    let mut col_offset = 0;

    for arr in arrays {
        let n_cols = arr.shape()[1];
        for i in 0..n_samples {
            for j in 0..n_cols {
                result[[i, col_offset + j]] = arr[[i, j]];
            }
        }
        col_offset += n_cols;
    }

    Ok(result)
}

/// Make a pipeline from a list of (name, transformer) tuples
#[allow(dead_code)]
pub fn make_pipeline(steps: Vec<(&str, Box<dyn Transformer>)>) -> Pipeline {
    let mut pipeline = Pipeline::new();
    for (name, transformer) in steps {
        pipeline = pipeline.add_step(name, transformer);
    }
    pipeline
}

/// Make a column transformer from a list of (name, transformer, columns) tuples
#[allow(dead_code)]
pub fn make_column_transformer(
    transformers: Vec<(&str, Box<dyn Transformer>, Vec<usize>)>,
    remainder: RemainderOption,
) -> ColumnTransformer {
    let mut ct = ColumnTransformer::new(remainder);
    for (name, transformer, columns) in transformers {
        ct = ct.add_transformer(name, transformer, columns);
    }
    ct
}