scirs2-metrics 0.3.2

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! Specialized clustering validation metrics
//!
//! This module contains metrics for evaluating clustering results using
//! specialized validation techniques such as stability measures, consensus
//! metrics, and similarity indices.

use scirs2_core::ndarray::{Array1, Array2, ArrayBase, Data, Dimension, Ix2};
use scirs2_core::numeric::{Float, NumCast};
use scirs2_core::random::{rngs::StdRng, Rng, RngExt, SeedableRng};
use std::collections::HashMap;
use std::ops::{AddAssign, DivAssign};

use crate::clustering::adjusted_rand_index;
use crate::error::{MetricsError, Result};

/// Calculate Jaccard Similarity index between two clusterings
///
/// The Jaccard index measures the similarity between two clusterings by
/// calculating the ratio of pair agreements to the total number of pairs.
/// Values range from 0 (no similarity) to 1 (identical clusterings).
///
/// # Arguments
///
/// * `labels_true` - Array of shape (n_samples,) with true cluster labels
/// * `labels_pred` - Array of shape (n_samples,) with predicted cluster labels
///
/// # Returns
///
/// * Jaccard similarity index (between 0 and 1)
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_metrics::clustering::validation::jaccard_similarity;
///
/// let labels_true = array![0, 0, 1, 1, 2, 2];
/// let labels_pred = array![1, 1, 0, 0, 2, 2];
///
/// let similarity = jaccard_similarity(&labels_true, &labels_pred).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn jaccard_similarity<S1, S2, D1, D2>(
    labels_true: &ArrayBase<S1, D1>,
    labels_pred: &ArrayBase<S2, D2>,
) -> Result<f64>
where
    S1: Data<Elem = usize>,
    S2: Data<Elem = usize>,
    D1: Dimension,
    D2: Dimension,
{
    // Check that labels_true and labels_pred have the same number of samples
    if labels_true.len() != labels_pred.len() {
        return Err(MetricsError::InvalidInput(format!(
            "labels_true has {} samples, but labels_pred has {} samples",
            labels_true.len(),
            labels_pred.len()
        )));
    }

    let n_samples = labels_true.len();
    if n_samples <= 1 {
        return Err(MetricsError::InvalidInput(
            "Number of samples must be greater than 1".to_string(),
        ));
    }

    // Generate all pairs
    let mut same_true = 0;
    let mut same_pred = 0;
    let mut same_both = 0;

    for i in 0..n_samples {
        for j in (i + 1)..n_samples {
            let true_i = labels_true.iter().nth(i).expect("Operation failed");
            let true_j = labels_true.iter().nth(j).expect("Operation failed");
            let pred_i = labels_pred.iter().nth(i).expect("Operation failed");
            let pred_j = labels_pred.iter().nth(j).expect("Operation failed");

            let same_in_true = true_i == true_j;
            let same_in_pred = pred_i == pred_j;

            if same_in_true {
                same_true += 1;
            }

            if same_in_pred {
                same_pred += 1;
            }

            if same_in_true && same_in_pred {
                same_both += 1;
            }
        }
    }

    // Calculate Jaccard similarity
    // Jaccard index = |A ∩ B| / |A ∪ B|
    // Here, A is the set of pairs in the same cluster in _true labels
    // B is the set of pairs in the same cluster in predicted labels
    let union_size = same_true + same_pred - same_both;

    let jaccard = if union_size > 0 {
        same_both as f64 / union_size as f64
    } else {
        1.0 // If both clusterings have all points in separate clusters
    };

    Ok(jaccard)
}

/// Calculate Cluster Stability index
///
/// Measures the stability of clustering by comparing multiple runs with
/// perturbed data. Higher values indicate more stable clustering.
///
/// # Arguments
///
/// * `x` - Array of shape (n_samples, n_features) - The data
/// * `labels` - Array of shape (n_samples,) - Predicted cluster labels
/// * `n_runs` - Number of bootstrap samples to generate (default: 10)
/// * `perturbation_scale` - Scale of Gaussian noise to add (default: 0.1)
/// * `random_seed` - Optional seed for reproducibility
///
/// # Returns
///
/// * Stability index (between 0 and 1)
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::{array, Array2};
/// use scirs2_metrics::clustering::validation::cluster_stability;
///
/// let x = Array2::from_shape_vec((6, 2), vec![
///     1.0, 2.0, 1.5, 1.8, 1.2, 2.2,
///     5.0, 6.0, 5.2, 5.8, 5.5, 6.2,
/// ]).expect("Operation failed");
///
/// let labels = array![0, 0, 0, 1, 1, 1];
///
/// let stability = cluster_stability(&x, &labels, None, None, None).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn cluster_stability<F, S1, S2, D>(
    x: &ArrayBase<S1, Ix2>,
    labels: &ArrayBase<S2, D>,
    n_runs: Option<usize>,
    perturbation_scale: Option<F>,
    random_seed: Option<u64>,
) -> Result<F>
where
    F: Float
        + NumCast
        + std::fmt::Debug
        + scirs2_core::ndarray::ScalarOperand
        + AddAssign
        + DivAssign,
    S1: Data<Elem = F>,
    S2: Data<Elem = usize>,
    D: Dimension,
{
    // Check that x and labels have the same number of samples
    let n_samples = x.shape()[0];
    if n_samples != labels.len() {
        return Err(MetricsError::InvalidInput(format!(
            "x has {} samples, but labels has {} samples",
            n_samples,
            labels.len()
        )));
    }

    // Default parameters
    let n_runs = n_runs.unwrap_or(10);
    let perturbation_scale = perturbation_scale
        .unwrap_or_else(|| F::from(0.1).expect("Failed to convert constant to float"));

    if n_runs < 2 {
        return Err(MetricsError::InvalidInput(
            "Number of _runs must be at least 2".to_string(),
        ));
    }

    // Setup RNG
    let _seed = random_seed.unwrap_or_else(scirs2_core::random::random::<u64>);
    let mut rng = StdRng::seed_from_u64(_seed);

    // Get unique clusters
    let mut unique_labels = Vec::new();
    for &label in labels.iter() {
        if !unique_labels.contains(&label) {
            unique_labels.push(label);
        }
    }

    // Create label mapping for consistency across perturbed clusterings
    let mut label_indices = HashMap::new();
    for (i, &label) in unique_labels.iter().enumerate() {
        label_indices.insert(label, i);
    }

    // Create a matrix to store remapped original labels
    let mut original_labels = Array1::zeros(n_samples);
    for (i, &label) in labels.iter().enumerate() {
        original_labels[i] = *label_indices.get(&label).expect("Operation failed");
    }

    // Generate perturbed datasets and calculate stability
    let mut stability_scores = Vec::new();

    for _ in 0..n_runs {
        // Create perturbed dataset
        let mut perturbed_data = Array2::zeros((n_samples, x.ncols()));

        // Add Gaussian noise
        for i in 0..n_samples {
            for j in 0..x.ncols() {
                let noise: f64 = rng.random_range(-1.0..1.0);
                let noise_value =
                    F::from(noise).expect("Failed to convert to float") * perturbation_scale;
                perturbed_data[[i, j]] = x[[i, j]] + noise_value;
            }
        }

        // Recluster the perturbed data using the existing labels and nearest centroids
        // Note: This is a simple approach - in practice, you might want to re-run the clustering algorithm

        // Calculate centroids for each cluster
        let mut centroids = Array2::zeros((unique_labels.len(), x.ncols()));
        let mut counts = vec![0; unique_labels.len()];

        for i in 0..n_samples {
            let label_idx = original_labels[i];
            for j in 0..x.ncols() {
                centroids[[label_idx, j]] += x[[i, j]];
            }
            counts[label_idx] += 1;
        }

        // Normalize centroids
        for i in 0..unique_labels.len() {
            if counts[i] > 0 {
                for j in 0..x.ncols() {
                    centroids[[i, j]] /= F::from(counts[i]).expect("Failed to convert to float");
                }
            }
        }

        // Assign perturbed data to nearest centroid
        let mut perturbed_labels = Array1::zeros(n_samples);

        for i in 0..n_samples {
            let mut min_dist = F::infinity();
            let mut best_label = 0;

            for (label_idx, _) in unique_labels.iter().enumerate() {
                let mut dist = F::zero();
                for j in 0..x.ncols() {
                    let diff = perturbed_data[[i, j]] - centroids[[label_idx, j]];
                    dist += diff * diff;
                }

                if dist < min_dist {
                    min_dist = dist;
                    best_label = label_idx;
                }
            }

            perturbed_labels[i] = best_label;
        }

        // Calculate similarity between original and perturbed clustering
        let mut ari_input_true = Vec::new();
        let mut ari_input_pred = Vec::new();

        for i in 0..n_samples {
            ari_input_true.push(original_labels[i]);
            ari_input_pred.push(perturbed_labels[i]);
        }

        let ari_true = scirs2_core::ndarray::Array1::from_vec(ari_input_true);
        let ari_pred = scirs2_core::ndarray::Array1::from_vec(ari_input_pred);

        let ari = adjusted_rand_index(&ari_true, &ari_pred).expect("Operation failed");
        stability_scores.push(F::from(ari).expect("Failed to convert to float"));
    }

    // Calculate mean stability score
    let sum = stability_scores.iter().fold(F::zero(), |acc, &x| acc + x);
    let mean = sum / F::from(stability_scores.len()).expect("Operation failed");

    Ok(mean)
}

/// Calculate Consensus Score for multiple clusterings
///
/// Measures the agreement among multiple clusterings of the same dataset.
/// Higher values indicate stronger consensus.
///
/// # Arguments
///
/// * `alllabels` - Vector of arrays, each containing a clustering result
///
/// # Returns
///
/// * Consensus score (between 0 and 1)
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_metrics::clustering::validation::consensus_score;
///
/// let clustering1 = array![0, 0, 0, 1, 1, 1];
/// let clustering2 = array![1, 1, 1, 0, 0, 0];  // Same as clustering1 but with inverted labels
/// let clustering3 = array![0, 0, 1, 1, 2, 2];  // Different clustering
///
/// let all_clusterings = vec![&clustering1, &clustering2, &clustering3];
/// let score = consensus_score(&all_clusterings).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn consensus_score<S, D>(alllabels: &[&ArrayBase<S, D>]) -> Result<f64>
where
    S: Data<Elem = usize>,
    D: Dimension,
{
    if alllabels.is_empty() {
        return Err(MetricsError::InvalidInput(
            "At least one clustering result is required".to_string(),
        ));
    }

    let n_clusterings = alllabels.len();
    if n_clusterings < 2 {
        return Err(MetricsError::InvalidInput(
            "At least two clusterings are required for consensus score".to_string(),
        ));
    }

    let n_samples = alllabels[0].len();

    // Check that all clusterings have the same number of samples
    for labels in alllabels.iter().skip(1) {
        if labels.len() != n_samples {
            return Err(MetricsError::InvalidInput(
                "All clusterings must have the same number of samples".to_string(),
            ));
        }
    }

    if n_samples <= 1 {
        return Err(MetricsError::InvalidInput(
            "Number of samples must be greater than 1".to_string(),
        ));
    }

    // Use a 2D array to store consensus values
    let mut consensus_values = vec![vec![0.0; n_samples]; n_samples];

    for labels in alllabels {
        for i in 0..n_samples {
            for j in i..n_samples {
                let label_i = labels.iter().nth(i).expect("Operation failed");
                let label_j = labels.iter().nth(j).expect("Operation failed");

                if label_i == label_j {
                    consensus_values[i][j] += 1.0;
                    if i != j {
                        consensus_values[j][i] += 1.0; // Symmetric
                    }
                }
            }
        }
    }

    // Normalize consensus values
    for i in 0..n_samples {
        for j in 0..n_samples {
            consensus_values[i][j] /= n_clusterings as f64;
        }
    }

    // Calculate consensus score as the average pairwise agreement
    let mut total = 0.0;
    let mut count = 0;

    for i in 0..n_samples {
        for j in (i + 1)..n_samples {
            total += consensus_values[i][j];
            count += 1;
        }
    }

    let consensus = if count > 0 {
        total / count as f64
    } else {
        1.0 // Default for a single sample
    };

    Ok(consensus)
}

/// Calculate fold stability of a clustering algorithm
///
/// Measures how stable a clustering is when applied to different subsets of data.
/// Higher values indicate more robust clustering.
///
/// # Arguments
///
/// * `x` - Array of shape (n_samples, n_features) - The data
/// * `labels` - Array of shape (n_samples,) - Predicted cluster labels
/// * `n_folds` - Number of folds to split the data into (default: 5)
/// * `fold_size` - Fraction of data to include in each fold (default: 0.8)
/// * `random_seed` - Optional seed for reproducibility
///
/// # Returns
///
/// * Fold stability index (between 0 and 1)
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::{array, Array2};
/// use scirs2_metrics::clustering::validation::fold_stability;
///
/// let x = Array2::from_shape_vec((10, 2), vec![
///     1.0, 2.0, 1.5, 1.8, 1.2, 2.2, 1.3, 2.1, 1.4, 1.9,
///     5.0, 6.0, 5.2, 5.8, 5.5, 6.2, 5.3, 6.1, 5.4, 5.9,
/// ]).expect("Operation failed");
///
/// let labels = array![0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
///
/// let stability = fold_stability(&x, &labels, None, None, None).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn fold_stability<F, S1, S2, D>(
    x: &ArrayBase<S1, Ix2>,
    labels: &ArrayBase<S2, D>,
    n_folds: Option<usize>,
    fold_size: Option<f64>,
    random_seed: Option<u64>,
) -> Result<F>
where
    F: Float
        + NumCast
        + std::fmt::Debug
        + scirs2_core::ndarray::ScalarOperand
        + AddAssign
        + DivAssign,
    S1: Data<Elem = F>,
    S2: Data<Elem = usize>,
    D: Dimension,
{
    // Check that x and labels have the same number of samples
    let n_samples = x.shape()[0];
    if n_samples != labels.len() {
        return Err(MetricsError::InvalidInput(format!(
            "x has {} samples, but labels has {} samples",
            n_samples,
            labels.len()
        )));
    }

    // Default parameters
    let n_folds = n_folds.unwrap_or(5);
    let fold_size = fold_size.unwrap_or(0.8);

    if n_folds < 2 {
        return Err(MetricsError::InvalidInput(
            "Number of _folds must be at least 2".to_string(),
        ));
    }

    if fold_size <= 0.0 || fold_size >= 1.0 {
        return Err(MetricsError::InvalidInput(
            "Fold _size must be between 0 and 1 (exclusive)".to_string(),
        ));
    }

    // Setup RNG
    let _seed = random_seed.unwrap_or_else(scirs2_core::random::random::<u64>);
    let mut rng = StdRng::seed_from_u64(_seed);

    // Get unique clusters
    let mut unique_labels = Vec::new();
    for &label in labels.iter() {
        if !unique_labels.contains(&label) {
            unique_labels.push(label);
        }
    }

    // Create indices for each fold
    let fold_sample_count = (n_samples as f64 * fold_size) as usize;
    let mut fold_indices = Vec::new();

    for _ in 0..n_folds {
        let mut indices = Vec::new();
        let mut available_indices: Vec<usize> = (0..n_samples).collect();

        // Shuffle available indices
        for i in (1..available_indices.len()).rev() {
            let j = rng.random_range(0..=i);
            available_indices.swap(i, j);
        }

        // Take the first fold_sample_count indices
        for i in 0..fold_sample_count.min(available_indices.len()) {
            indices.push(available_indices[i]);
        }

        fold_indices.push(indices);
    }

    // Calculate centroids from the original clustering
    let mut centroids = HashMap::new();
    let mut counts = HashMap::new();

    for (i, &label) in labels.iter().enumerate() {
        // Initialize centroid if not already present
        if !centroids.contains_key(&label) {
            let centroid = Array1::zeros(x.ncols());
            centroids.insert(label, centroid);
            counts.insert(label, 0);
        }

        // Add this point to the centroid
        let centroid = centroids.get_mut(&label).expect("Operation failed");
        for j in 0..x.ncols() {
            centroid[j] += x[[i, j]];
        }

        // Increment count
        *counts.get_mut(&label).expect("Operation failed") += 1;
    }

    // Normalize centroids
    for (&label, centroid) in centroids.iter_mut() {
        let count = *counts.get(&label).expect("Operation failed");
        if count > 0 {
            for j in 0..centroid.len() {
                centroid[j] /= F::from(count).expect("Failed to convert to float");
            }
        }
    }

    // Calculate stability for each fold
    let mut stability_scores = Vec::new();

    for fold_idx in &fold_indices {
        // Extract fold data
        let fold_size = fold_idx.len();
        let mut fold_data = Array2::zeros((fold_size, x.ncols()));
        let mut fold_labels = Vec::new();

        for (i, &idx) in fold_idx.iter().enumerate() {
            for j in 0..x.ncols() {
                fold_data[[i, j]] = x[[idx, j]];
            }
            fold_labels.push(*labels.iter().nth(idx).expect("Operation failed"));
        }

        // Assign points to nearest centroid
        let mut predicted_labels = Vec::new();

        for i in 0..fold_size {
            let mut min_dist = F::infinity();
            let mut best_label = 0;

            for &label in &unique_labels {
                let centroid = centroids.get(&label).expect("Operation failed");

                let mut dist = F::zero();
                for j in 0..x.ncols() {
                    let diff = fold_data[[i, j]] - centroid[j];
                    dist += diff * diff;
                }

                if dist < min_dist {
                    min_dist = dist;
                    best_label = label;
                }
            }

            predicted_labels.push(best_label);
        }

        // Calculate similarity between original and predicted labels
        let true_labels = scirs2_core::ndarray::Array1::from_vec(fold_labels);
        let pred_labels = scirs2_core::ndarray::Array1::from_vec(predicted_labels);

        let jaccard = jaccard_similarity(&true_labels, &pred_labels).expect("Operation failed");
        stability_scores.push(F::from(jaccard).expect("Failed to convert to float"));
    }

    // Calculate mean stability score
    let sum = stability_scores.iter().fold(F::zero(), |acc, &x| acc + x);
    let mean = sum / F::from(stability_scores.len()).expect("Operation failed");

    Ok(mean)
}

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

    #[test]
    fn test_jaccard_similarity() {
        // Identical clusterings
        let labels1 = array![0, 0, 1, 1, 2, 2];
        let labels2 = array![0, 0, 1, 1, 2, 2];

        let similarity = jaccard_similarity(&labels1, &labels2).expect("Operation failed");
        assert_abs_diff_eq!(similarity, 1.0, epsilon = 1e-10);

        // Same clustering but with different label values
        let labels3 = array![0, 0, 1, 1, 2, 2];
        let labels4 = array![1, 1, 0, 0, 2, 2];

        let similarity = jaccard_similarity(&labels3, &labels4).expect("Operation failed");
        assert!(similarity > 0.5); // Should be high but not necessarily 1.0

        // Different clusterings
        let labels5 = array![0, 0, 0, 1, 1, 1];
        let labels6 = array![0, 0, 1, 1, 2, 2];

        let similarity = jaccard_similarity(&labels5, &labels6).expect("Operation failed");
        assert!(similarity < 1.0); // Should be less than 1.0

        // Completely different clusterings
        let labels7 = array![0, 0, 0, 0, 0, 0];
        let labels8 = array![0, 1, 2, 3, 4, 5];

        let similarity = jaccard_similarity(&labels7, &labels8).expect("Operation failed");
        assert!(similarity < 0.5); // Should be low
    }

    #[test]
    fn test_consensus_score() {
        // Two identical clusterings
        let clustering1 = array![0, 0, 0, 1, 1, 1];
        let clustering2 = array![0, 0, 0, 1, 1, 1];

        let all_clusterings = vec![&clustering1, &clustering2];
        let score = consensus_score(&all_clusterings).expect("Operation failed");
        assert!(score > 0.0); // Just check it's positive for identical clusterings

        // Two similar clusterings (same structure, different labels)
        let clustering3 = array![0, 0, 0, 1, 1, 1];
        let clustering4 = array![1, 1, 1, 0, 0, 0];

        let all_clusterings = vec![&clustering3, &clustering4];
        let score = consensus_score(&all_clusterings).expect("Operation failed");
        assert!(score > 0.0); // Just check it's positive for identical clusterings

        // Three clusterings, two similar and one different
        let clustering5 = array![0, 0, 0, 1, 1, 1];
        let clustering6 = array![1, 1, 1, 0, 0, 0]; // Same as clustering5
        let clustering7 = array![0, 0, 1, 1, 2, 2]; // Different

        let all_clusterings = vec![&clustering5, &clustering6, &clustering7];
        let score = consensus_score(&all_clusterings).expect("Operation failed");

        // Score should be positive but less than 1.0
        assert!(score > 0.0);
        assert!(score < 1.0);
    }

    #[test]
    fn test_cluster_stability() {
        // Create a simple dataset with well-separated clusters
        let x = Array2::from_shape_vec(
            (6, 2),
            vec![1.0, 2.0, 1.5, 1.8, 1.2, 2.2, 5.0, 6.0, 5.2, 5.8, 5.5, 6.2],
        )
        .expect("Operation failed");

        let labels = array![0, 0, 0, 1, 1, 1];

        // Test stability with default parameters
        let stability =
            cluster_stability(&x, &labels, Some(3), None, Some(42)).expect("Operation failed");

        // Well-separated clusters should have high stability
        assert!(stability > 0.5);

        // Create a dataset with less separated clusters
        let less_separated = Array2::from_shape_vec(
            (6, 2),
            vec![1.0, 2.0, 1.5, 1.8, 2.8, 3.0, 3.2, 3.5, 4.0, 4.2, 4.5, 5.0],
        )
        .expect("Operation failed");

        // Test stability with less separated clusters
        let stability_less = cluster_stability(&less_separated, &labels, Some(3), None, Some(42))
            .expect("Operation failed");

        // Less separated clusters should have lower stability
        // But with small datasets and few runs, this might not always be true
        assert!(stability_less >= 0.0);
        assert!(stability_less <= 1.0);
    }

    #[test]
    fn test_fold_stability() {
        // Create a larger dataset with well-separated clusters
        let mut x_data = Vec::new();
        let mut labels_data = Vec::new();

        // Add 20 points in cluster 0
        for i in 0..20 {
            x_data.push(1.0 + (i as f64 % 5.0) * 0.1); // x between 1.0 and 1.4
            x_data.push(2.0 + (i as f64 % 4.0) * 0.1); // y between 2.0 and 2.3
            labels_data.push(0);
        }

        // Add 20 points in cluster 1
        for i in 0..20 {
            x_data.push(5.0 + (i as f64 % 5.0) * 0.1); // x between 5.0 and 5.4
            x_data.push(6.0 + (i as f64 % 4.0) * 0.1); // y between 6.0 and 6.3
            labels_data.push(1);
        }

        let x = Array2::from_shape_vec((40, 2), x_data).expect("Operation failed");
        let labels = Array1::from_vec(labels_data);

        // Test fold stability
        let stability =
            fold_stability(&x, &labels, Some(3), Some(0.7), Some(42)).expect("Operation failed");

        // Well-separated clusters should have high fold stability
        assert!(stability > 0.7);

        // Invalid parameters
        let invalid_folds = fold_stability(&x, &labels, Some(1), None, None);
        assert!(invalid_folds.is_err());

        let invalid_fold_size = fold_stability(&x, &labels, None, Some(1.2), None);
        assert!(invalid_fold_size.is_err());
    }

    #[test]
    fn test_with_invalid_inputs() {
        // Mismatched sample counts
        let labels1 = array![0, 0, 1, 1, 2, 2];
        let labels2 = array![0, 0, 1, 1];

        let result = jaccard_similarity(&labels1, &labels2);
        assert!(result.is_err());

        // Empty or singleton inputs
        let empty = array![] as scirs2_core::ndarray::Array1<usize>;
        let singleton = array![0];

        let result = jaccard_similarity(&empty, &empty);
        assert!(result.is_err());

        let result = jaccard_similarity(&singleton, &singleton);
        assert!(result.is_err());

        // Test consensus score with invalid inputs
        let empty_array: scirs2_core::ndarray::Array1<usize> =
            scirs2_core::ndarray::Array1::zeros(0);
        let result = consensus_score(&[&empty_array]);
        assert!(result.is_err());

        let result = consensus_score(&[&labels1]);
        assert!(result.is_err());

        let result = consensus_score(&[&labels1, &labels2]);
        assert!(result.is_err());
    }
}