ferrolearn-preprocess 0.3.0

Preprocessing transformers for the ferrolearn ML framework
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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! Statistical-test-based feature selectors.
//!
//! Three selectors that choose features based on p-values obtained from a
//! statistical test (e.g., ANOVA F-test, chi-squared test):
//!
//! - [`SelectFpr`] — **False Positive Rate**: selects every feature whose
//!   p-value is below `alpha`.
//! - [`SelectFdr`] — **False Discovery Rate**: applies the Benjamini-Hochberg
//!   procedure to control the expected proportion of false positives.
//! - [`SelectFwe`] — **Family-Wise Error**: applies the Bonferroni correction
//!   (`alpha / n_features`) to control the probability of any false positive.
//!
//! All three take a pre-computed vector of p-values (one per feature) at fit
//! time, allowing integration with any upstream scoring function.

use ferrolearn_core::error::FerroError;
use ferrolearn_core::traits::{Fit, Transform};
use ndarray::{Array1, Array2};
use num_traits::Float;

// ---------------------------------------------------------------------------
// Shared helper
// ---------------------------------------------------------------------------

/// Build a new `Array2<F>` containing only the columns listed in `indices`.
fn select_columns<F: Float>(x: &Array2<F>, indices: &[usize]) -> Array2<F> {
    let nrows = x.nrows();
    let ncols = indices.len();
    if ncols == 0 {
        return Array2::zeros((nrows, 0));
    }
    let mut out = Array2::zeros((nrows, ncols));
    for (new_j, &old_j) in indices.iter().enumerate() {
        for i in 0..nrows {
            out[[i, new_j]] = x[[i, old_j]];
        }
    }
    out
}

/// Validate common inputs for all three selectors.
fn validate_inputs(n_features: usize, alpha: f64) -> Result<(), FerroError> {
    if n_features == 0 {
        return Err(FerroError::InvalidParameter {
            name: "p_values".into(),
            reason: "p-value vector must not be empty".into(),
        });
    }
    if alpha <= 0.0 || alpha > 1.0 {
        return Err(FerroError::InvalidParameter {
            name: "alpha".into(),
            reason: format!("alpha must be in (0, 1], got {alpha}"),
        });
    }
    Ok(())
}

// ===========================================================================
// SelectFpr — False Positive Rate
// ===========================================================================

/// Select features with p-values below `alpha`.
///
/// A feature is selected if its p-value is strictly less than `alpha`.
/// This controls the per-feature false positive rate but does not adjust
/// for multiple comparisons.
///
/// # Examples
///
/// ```
/// use ferrolearn_preprocess::stat_selectors::SelectFpr;
/// use ferrolearn_core::traits::{Fit, Transform};
/// use ndarray::array;
///
/// let sel = SelectFpr::<f64>::new(0.05);
/// let p_values = array![0.01, 0.5, 0.03, 0.9];
/// let fitted = sel.fit(&p_values, &()).unwrap();
/// // Features 0 (p=0.01) and 2 (p=0.03) are below alpha=0.05
/// assert_eq!(fitted.selected_indices(), &[0, 2]);
/// ```
#[must_use]
#[derive(Debug, Clone)]
pub struct SelectFpr<F> {
    /// Significance threshold.
    alpha: f64,
    _marker: std::marker::PhantomData<F>,
}

impl<F: Float + Send + Sync + 'static> SelectFpr<F> {
    /// Create a new `SelectFpr` with the given significance level.
    pub fn new(alpha: f64) -> Self {
        Self {
            alpha,
            _marker: std::marker::PhantomData,
        }
    }

    /// Return the significance level.
    #[must_use]
    pub fn alpha(&self) -> f64 {
        self.alpha
    }
}

/// A fitted `SelectFpr` holding the selected indices.
#[derive(Debug, Clone)]
pub struct FittedSelectFpr<F> {
    /// Number of features seen during fitting.
    n_features_in: usize,
    /// P-values supplied during fitting.
    p_values: Array1<F>,
    /// Indices of selected columns (sorted).
    selected_indices: Vec<usize>,
}

impl<F: Float + Send + Sync + 'static> FittedSelectFpr<F> {
    /// Return the p-values.
    #[must_use]
    pub fn p_values(&self) -> &Array1<F> {
        &self.p_values
    }

    /// Return the indices of the selected columns.
    #[must_use]
    pub fn selected_indices(&self) -> &[usize] {
        &self.selected_indices
    }

    /// Return the number of selected features.
    #[must_use]
    pub fn n_features_selected(&self) -> usize {
        self.selected_indices.len()
    }
}

impl<F: Float + Send + Sync + 'static> Fit<Array1<F>, ()> for SelectFpr<F> {
    type Fitted = FittedSelectFpr<F>;
    type Error = FerroError;

    /// Fit by selecting features whose p-value is below `alpha`.
    ///
    /// # Errors
    ///
    /// - [`FerroError::InvalidParameter`] if p-values are empty or alpha is
    ///   not in `(0, 1]`.
    fn fit(&self, x: &Array1<F>, _y: &()) -> Result<FittedSelectFpr<F>, FerroError> {
        let n = x.len();
        validate_inputs(n, self.alpha)?;

        let alpha_f = F::from(self.alpha).unwrap_or_else(F::zero);
        let selected_indices: Vec<usize> = x
            .iter()
            .enumerate()
            .filter(|&(_, &p)| p < alpha_f)
            .map(|(j, _)| j)
            .collect();

        Ok(FittedSelectFpr {
            n_features_in: n,
            p_values: x.clone(),
            selected_indices,
        })
    }
}

impl<F: Float + Send + Sync + 'static> Transform<Array2<F>> for FittedSelectFpr<F> {
    type Output = Array2<F>;
    type Error = FerroError;

    /// Return a matrix containing only the selected columns.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::ShapeMismatch`] if column count does not match.
    fn transform(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
        if x.ncols() != self.n_features_in {
            return Err(FerroError::ShapeMismatch {
                expected: vec![x.nrows(), self.n_features_in],
                actual: vec![x.nrows(), x.ncols()],
                context: "FittedSelectFpr::transform".into(),
            });
        }
        Ok(select_columns(x, &self.selected_indices))
    }
}

// ===========================================================================
// SelectFdr — False Discovery Rate (Benjamini-Hochberg)
// ===========================================================================

/// Select features controlling the false discovery rate via the
/// Benjamini-Hochberg procedure.
///
/// Features are sorted by p-value. Feature *i* (0-indexed, sorted ascending)
/// is selected if `p_value[i] <= alpha * (i+1) / n_features`. All features
/// with rank at or below the highest qualifying rank are selected.
///
/// # Examples
///
/// ```
/// use ferrolearn_preprocess::stat_selectors::SelectFdr;
/// use ferrolearn_core::traits::{Fit, Transform};
/// use ndarray::array;
///
/// let sel = SelectFdr::<f64>::new(0.05);
/// let p_values = array![0.01, 0.5, 0.03, 0.9];
/// let fitted = sel.fit(&p_values, &()).unwrap();
/// assert!(fitted.selected_indices().contains(&0));
/// ```
#[must_use]
#[derive(Debug, Clone)]
pub struct SelectFdr<F> {
    /// Target false discovery rate.
    alpha: f64,
    _marker: std::marker::PhantomData<F>,
}

impl<F: Float + Send + Sync + 'static> SelectFdr<F> {
    /// Create a new `SelectFdr` with the given FDR level.
    pub fn new(alpha: f64) -> Self {
        Self {
            alpha,
            _marker: std::marker::PhantomData,
        }
    }

    /// Return the FDR level.
    #[must_use]
    pub fn alpha(&self) -> f64 {
        self.alpha
    }
}

/// A fitted `SelectFdr` holding the selected indices.
#[derive(Debug, Clone)]
pub struct FittedSelectFdr<F> {
    /// Number of features seen during fitting.
    n_features_in: usize,
    /// P-values supplied during fitting.
    p_values: Array1<F>,
    /// Indices of selected columns (sorted in original order).
    selected_indices: Vec<usize>,
}

impl<F: Float + Send + Sync + 'static> FittedSelectFdr<F> {
    /// Return the p-values.
    #[must_use]
    pub fn p_values(&self) -> &Array1<F> {
        &self.p_values
    }

    /// Return the indices of the selected columns.
    #[must_use]
    pub fn selected_indices(&self) -> &[usize] {
        &self.selected_indices
    }

    /// Return the number of selected features.
    #[must_use]
    pub fn n_features_selected(&self) -> usize {
        self.selected_indices.len()
    }
}

impl<F: Float + Send + Sync + 'static> Fit<Array1<F>, ()> for SelectFdr<F> {
    type Fitted = FittedSelectFdr<F>;
    type Error = FerroError;

    /// Fit using the Benjamini-Hochberg procedure.
    ///
    /// # Errors
    ///
    /// - [`FerroError::InvalidParameter`] if p-values are empty or alpha is
    ///   not in `(0, 1]`.
    fn fit(&self, x: &Array1<F>, _y: &()) -> Result<FittedSelectFdr<F>, FerroError> {
        let n = x.len();
        validate_inputs(n, self.alpha)?;

        let alpha_f = F::from(self.alpha).unwrap_or_else(F::zero);
        let n_f = F::from(n).unwrap_or_else(F::one);

        // Sort features by p-value (ascending), keeping original indices
        let mut ranked: Vec<(usize, F)> = x.iter().copied().enumerate().collect();
        ranked.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Find the largest rank k where p_(k) <= alpha * (k+1) / n
        let mut max_qualifying_rank: Option<usize> = None;
        for (rank, &(_, p_val)) in ranked.iter().enumerate() {
            let bh_threshold = alpha_f * F::from(rank + 1).unwrap_or_else(F::one) / n_f;
            if p_val <= bh_threshold {
                max_qualifying_rank = Some(rank);
            }
        }

        // Select all features at or below the max qualifying rank
        let mut selected_indices: Vec<usize> = match max_qualifying_rank {
            Some(max_rank) => ranked[..=max_rank]
                .iter()
                .map(|&(idx, _)| idx)
                .collect(),
            None => Vec::new(),
        };
        selected_indices.sort_unstable();

        Ok(FittedSelectFdr {
            n_features_in: n,
            p_values: x.clone(),
            selected_indices,
        })
    }
}

impl<F: Float + Send + Sync + 'static> Transform<Array2<F>> for FittedSelectFdr<F> {
    type Output = Array2<F>;
    type Error = FerroError;

    /// Return a matrix containing only the selected columns.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::ShapeMismatch`] if column count does not match.
    fn transform(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
        if x.ncols() != self.n_features_in {
            return Err(FerroError::ShapeMismatch {
                expected: vec![x.nrows(), self.n_features_in],
                actual: vec![x.nrows(), x.ncols()],
                context: "FittedSelectFdr::transform".into(),
            });
        }
        Ok(select_columns(x, &self.selected_indices))
    }
}

// ===========================================================================
// SelectFwe — Family-Wise Error (Bonferroni)
// ===========================================================================

/// Select features controlling the family-wise error rate via the
/// Bonferroni correction.
///
/// A feature is selected if its p-value is strictly less than
/// `alpha / n_features`.
///
/// # Examples
///
/// ```
/// use ferrolearn_preprocess::stat_selectors::SelectFwe;
/// use ferrolearn_core::traits::{Fit, Transform};
/// use ndarray::array;
///
/// let sel = SelectFwe::<f64>::new(0.05);
/// let p_values = array![0.001, 0.5, 0.03, 0.9];
/// let fitted = sel.fit(&p_values, &()).unwrap();
/// // Bonferroni threshold = 0.05/4 = 0.0125; only feature 0 qualifies
/// assert_eq!(fitted.selected_indices(), &[0]);
/// ```
#[must_use]
#[derive(Debug, Clone)]
pub struct SelectFwe<F> {
    /// Significance level before Bonferroni correction.
    alpha: f64,
    _marker: std::marker::PhantomData<F>,
}

impl<F: Float + Send + Sync + 'static> SelectFwe<F> {
    /// Create a new `SelectFwe` with the given significance level.
    pub fn new(alpha: f64) -> Self {
        Self {
            alpha,
            _marker: std::marker::PhantomData,
        }
    }

    /// Return the significance level.
    #[must_use]
    pub fn alpha(&self) -> f64 {
        self.alpha
    }
}

/// A fitted `SelectFwe` holding the selected indices.
#[derive(Debug, Clone)]
pub struct FittedSelectFwe<F> {
    /// Number of features seen during fitting.
    n_features_in: usize,
    /// P-values supplied during fitting.
    p_values: Array1<F>,
    /// Indices of selected columns (sorted).
    selected_indices: Vec<usize>,
}

impl<F: Float + Send + Sync + 'static> FittedSelectFwe<F> {
    /// Return the p-values.
    #[must_use]
    pub fn p_values(&self) -> &Array1<F> {
        &self.p_values
    }

    /// Return the indices of the selected columns.
    #[must_use]
    pub fn selected_indices(&self) -> &[usize] {
        &self.selected_indices
    }

    /// Return the number of selected features.
    #[must_use]
    pub fn n_features_selected(&self) -> usize {
        self.selected_indices.len()
    }
}

impl<F: Float + Send + Sync + 'static> Fit<Array1<F>, ()> for SelectFwe<F> {
    type Fitted = FittedSelectFwe<F>;
    type Error = FerroError;

    /// Fit using the Bonferroni correction: `p < alpha / n_features`.
    ///
    /// # Errors
    ///
    /// - [`FerroError::InvalidParameter`] if p-values are empty or alpha is
    ///   not in `(0, 1]`.
    fn fit(&self, x: &Array1<F>, _y: &()) -> Result<FittedSelectFwe<F>, FerroError> {
        let n = x.len();
        validate_inputs(n, self.alpha)?;

        let adjusted_alpha = self.alpha / n as f64;
        let adjusted_alpha_f = F::from(adjusted_alpha).unwrap_or_else(F::zero);

        let selected_indices: Vec<usize> = x
            .iter()
            .enumerate()
            .filter(|&(_, &p)| p < adjusted_alpha_f)
            .map(|(j, _)| j)
            .collect();

        Ok(FittedSelectFwe {
            n_features_in: n,
            p_values: x.clone(),
            selected_indices,
        })
    }
}

impl<F: Float + Send + Sync + 'static> Transform<Array2<F>> for FittedSelectFwe<F> {
    type Output = Array2<F>;
    type Error = FerroError;

    /// Return a matrix containing only the selected columns.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::ShapeMismatch`] if column count does not match.
    fn transform(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
        if x.ncols() != self.n_features_in {
            return Err(FerroError::ShapeMismatch {
                expected: vec![x.nrows(), self.n_features_in],
                actual: vec![x.nrows(), x.ncols()],
                context: "FittedSelectFwe::transform".into(),
            });
        }
        Ok(select_columns(x, &self.selected_indices))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::array;

    // ========================================================================
    // SelectFpr tests
    // ========================================================================

    #[test]
    fn test_fpr_selects_below_alpha() {
        let sel = SelectFpr::<f64>::new(0.05);
        let p = array![0.01, 0.5, 0.03, 0.9];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.selected_indices(), &[0, 2]);
    }

    #[test]
    fn test_fpr_none_below_alpha() {
        let sel = SelectFpr::<f64>::new(0.001);
        let p = array![0.01, 0.5, 0.03];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.n_features_selected(), 0);
    }

    #[test]
    fn test_fpr_all_below_alpha() {
        let sel = SelectFpr::<f64>::new(0.99);
        let p = array![0.01, 0.5, 0.03];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.n_features_selected(), 3);
    }

    #[test]
    fn test_fpr_transform() {
        let sel = SelectFpr::<f64>::new(0.05);
        let p = array![0.01, 0.5, 0.03];
        let fitted = sel.fit(&p, &()).unwrap();
        let x = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let out = fitted.transform(&x).unwrap();
        assert_eq!(out.ncols(), 2); // features 0 and 2
        assert_eq!(out[[0, 0]], 1.0);
        assert_eq!(out[[0, 1]], 3.0);
    }

    #[test]
    fn test_fpr_empty_error() {
        let sel = SelectFpr::<f64>::new(0.05);
        let p: Array1<f64> = Array1::zeros(0);
        assert!(sel.fit(&p, &()).is_err());
    }

    #[test]
    fn test_fpr_invalid_alpha() {
        let sel = SelectFpr::<f64>::new(0.0);
        let p = array![0.01];
        assert!(sel.fit(&p, &()).is_err());

        let sel2 = SelectFpr::<f64>::new(1.5);
        assert!(sel2.fit(&p, &()).is_err());
    }

    #[test]
    fn test_fpr_shape_mismatch() {
        let sel = SelectFpr::<f64>::new(0.05);
        let p = array![0.01, 0.5];
        let fitted = sel.fit(&p, &()).unwrap();
        let x_bad = array![[1.0, 2.0, 3.0]];
        assert!(fitted.transform(&x_bad).is_err());
    }

    #[test]
    fn test_fpr_accessor() {
        let sel = SelectFpr::<f64>::new(0.05);
        assert_eq!(sel.alpha(), 0.05);
    }

    #[test]
    fn test_fpr_p_values_accessor() {
        let sel = SelectFpr::<f64>::new(0.05);
        let p = array![0.01, 0.5];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.p_values().len(), 2);
    }

    // ========================================================================
    // SelectFdr tests (Benjamini-Hochberg)
    // ========================================================================

    #[test]
    fn test_fdr_basic() {
        let sel = SelectFdr::<f64>::new(0.05);
        // Sorted p-values: 0.01 (feat 0), 0.03 (feat 2), 0.5 (feat 1), 0.9 (feat 3)
        // BH thresholds: 0.05*1/4=0.0125, 0.05*2/4=0.025, 0.05*3/4=0.0375, 0.05*4/4=0.05
        // 0.01 <= 0.0125 ✓ (rank 0)
        // 0.03 <= 0.025  ✗ → but check all: max qualifying rank = 0
        let p = array![0.01, 0.5, 0.03, 0.9];
        let fitted = sel.fit(&p, &()).unwrap();
        assert!(fitted.selected_indices().contains(&0));
    }

    #[test]
    fn test_fdr_multiple_pass() {
        let sel = SelectFdr::<f64>::new(0.10);
        // Sorted: 0.005 (rank 0), 0.02 (rank 1), 0.04 (rank 2), 0.5 (rank 3)
        // BH: 0.1*1/4=0.025, 0.1*2/4=0.05, 0.1*3/4=0.075, 0.1*4/4=0.1
        // 0.005 <= 0.025 ✓
        // 0.02  <= 0.05  ✓
        // 0.04  <= 0.075 ✓ → max rank = 2 → select rank 0,1,2
        let p = array![0.02, 0.5, 0.005, 0.04];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.n_features_selected(), 3);
        assert!(fitted.selected_indices().contains(&0)); // 0.02
        assert!(fitted.selected_indices().contains(&2)); // 0.005
        assert!(fitted.selected_indices().contains(&3)); // 0.04
    }

    #[test]
    fn test_fdr_none_selected() {
        let sel = SelectFdr::<f64>::new(0.001);
        let p = array![0.01, 0.5, 0.03];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.n_features_selected(), 0);
    }

    #[test]
    fn test_fdr_transform() {
        let sel = SelectFdr::<f64>::new(0.10);
        let p = array![0.001, 0.5, 0.9];
        let fitted = sel.fit(&p, &()).unwrap();
        let x = array![[1.0, 2.0, 3.0]];
        let out = fitted.transform(&x).unwrap();
        // Feature 0 (p=0.001) selected: BH threshold = 0.1*1/3 ≈ 0.033
        assert!(out.ncols() >= 1);
    }

    #[test]
    fn test_fdr_empty_error() {
        let sel = SelectFdr::<f64>::new(0.05);
        let p: Array1<f64> = Array1::zeros(0);
        assert!(sel.fit(&p, &()).is_err());
    }

    #[test]
    fn test_fdr_invalid_alpha() {
        let sel = SelectFdr::<f64>::new(0.0);
        let p = array![0.01];
        assert!(sel.fit(&p, &()).is_err());
    }

    #[test]
    fn test_fdr_shape_mismatch() {
        let sel = SelectFdr::<f64>::new(0.05);
        let p = array![0.01, 0.5];
        let fitted = sel.fit(&p, &()).unwrap();
        let x_bad = array![[1.0, 2.0, 3.0]];
        assert!(fitted.transform(&x_bad).is_err());
    }

    #[test]
    fn test_fdr_accessor() {
        let sel = SelectFdr::<f64>::new(0.05);
        assert_eq!(sel.alpha(), 0.05);
    }

    // ========================================================================
    // SelectFwe tests (Bonferroni)
    // ========================================================================

    #[test]
    fn test_fwe_basic() {
        let sel = SelectFwe::<f64>::new(0.05);
        // Bonferroni threshold = 0.05/4 = 0.0125
        let p = array![0.001, 0.5, 0.03, 0.9];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.selected_indices(), &[0]);
    }

    #[test]
    fn test_fwe_two_features() {
        let sel = SelectFwe::<f64>::new(0.10);
        // Bonferroni: 0.1/3 ≈ 0.0333
        let p = array![0.01, 0.02, 0.5];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.selected_indices(), &[0, 1]);
    }

    #[test]
    fn test_fwe_none_selected() {
        let sel = SelectFwe::<f64>::new(0.01);
        // Bonferroni: 0.01/3 ≈ 0.00333
        let p = array![0.005, 0.5, 0.03];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.n_features_selected(), 0);
    }

    #[test]
    fn test_fwe_transform() {
        let sel = SelectFwe::<f64>::new(0.05);
        let p = array![0.001, 0.5, 0.9];
        let fitted = sel.fit(&p, &()).unwrap();
        let x = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let out = fitted.transform(&x).unwrap();
        assert_eq!(out.ncols(), 1);
        assert_eq!(out[[0, 0]], 1.0);
    }

    #[test]
    fn test_fwe_empty_error() {
        let sel = SelectFwe::<f64>::new(0.05);
        let p: Array1<f64> = Array1::zeros(0);
        assert!(sel.fit(&p, &()).is_err());
    }

    #[test]
    fn test_fwe_invalid_alpha() {
        let sel = SelectFwe::<f64>::new(0.0);
        let p = array![0.01];
        assert!(sel.fit(&p, &()).is_err());
    }

    #[test]
    fn test_fwe_shape_mismatch() {
        let sel = SelectFwe::<f64>::new(0.05);
        let p = array![0.01, 0.5];
        let fitted = sel.fit(&p, &()).unwrap();
        let x_bad = array![[1.0, 2.0, 3.0]];
        assert!(fitted.transform(&x_bad).is_err());
    }

    #[test]
    fn test_fwe_accessor() {
        let sel = SelectFwe::<f64>::new(0.05);
        assert_eq!(sel.alpha(), 0.05);
    }

    #[test]
    fn test_fwe_single_feature() {
        let sel = SelectFwe::<f64>::new(0.05);
        // Bonferroni: 0.05/1 = 0.05; p=0.01 < 0.05 ✓
        let p = array![0.01];
        let fitted = sel.fit(&p, &()).unwrap();
        assert_eq!(fitted.selected_indices(), &[0]);
    }

    #[test]
    fn test_fwe_f32() {
        let sel = SelectFwe::<f32>::new(0.05);
        let p: Array1<f32> = array![0.001f32, 0.5];
        let fitted = sel.fit(&p, &()).unwrap();
        // Bonferroni: 0.05/2 = 0.025; p=0.001 < 0.025 ✓
        assert_eq!(fitted.selected_indices(), &[0]);
    }
}