sklears-semi-supervised 0.2.0

Semi-supervised learning algorithms
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
//! Streaming Graph Learning for Dynamic Semi-Supervised Learning
//!
//! This module provides algorithms for learning and updating graph structures
//! incrementally as new data arrives in streaming scenarios.

use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Predict, PredictProba, Untrained},
    types::Float,
};
use std::collections::{HashMap, VecDeque};

/// Streaming Graph Learning for Dynamic Semi-Supervised Learning
///
/// This method continuously updates graph structures as new data points arrive,
/// making it suitable for dynamic environments where the data distribution
/// may change over time. It maintains a sliding window of recent data points
/// and efficiently updates the graph structure and label propagation.
///
/// # Parameters
///
/// * `window_size` - Size of the sliding window for maintaining recent data
/// * `lambda_sparse` - Sparsity regularization parameter for graph learning
/// * `alpha_decay` - Decay factor for edge weights over time
/// * `update_frequency` - Frequency of full graph reconstruction
/// * `forgetting_factor` - Factor for exponential forgetting of old connections
/// * `adaptive_threshold` - Whether to use adaptive thresholds for edge addition
/// * `min_samples_update` - Minimum samples required before updating the graph
///
/// # Examples
///
/// ```
/// use scirs2_core::array;
/// use sklears_semi_supervised::StreamingGraphLearning;
/// use sklears_core::traits::{Predict, Fit};
///
///
/// let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
/// let y = array![0, 1, -1, -1]; // -1 indicates unlabeled
///
/// let mut sgl = StreamingGraphLearning::new()
///     .window_size(100)
///     .lambda_sparse(0.1)
///     .alpha_decay(0.95);
///
/// let mut fitted = sgl.fit(&X.view(), &y.view()).unwrap();
/// let predictions = fitted.predict(&X.view()).unwrap();
///
/// // Update with new data
/// let X_new = array![[5.0, 6.0], [6.0, 7.0]];
/// let y_new = array![-1, 0];
/// let updated = fitted.update(&X_new.view(), &y_new.view()).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct StreamingGraphLearning<S = Untrained> {
    state: S,
    window_size: usize,
    lambda_sparse: f64,
    alpha_decay: f64,
    update_frequency: usize,
    forgetting_factor: f64,
    adaptive_threshold: bool,
    min_samples_update: usize,
    k_neighbors: usize,
    similarity_threshold: f64,
}

impl StreamingGraphLearning<Untrained> {
    /// Create a new StreamingGraphLearning instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            window_size: 1000,
            lambda_sparse: 0.1,
            alpha_decay: 0.95,
            update_frequency: 50,
            forgetting_factor: 0.99,
            adaptive_threshold: true,
            min_samples_update: 10,
            k_neighbors: 5,
            similarity_threshold: 0.5,
        }
    }

    /// Set the sliding window size
    pub fn window_size(mut self, window_size: usize) -> Self {
        self.window_size = window_size;
        self
    }

    /// Set the sparsity regularization parameter
    pub fn lambda_sparse(mut self, lambda_sparse: f64) -> Self {
        self.lambda_sparse = lambda_sparse;
        self
    }

    /// Set the decay factor for edge weights
    pub fn alpha_decay(mut self, alpha_decay: f64) -> Self {
        self.alpha_decay = alpha_decay;
        self
    }

    /// Set the frequency of full graph reconstruction
    pub fn update_frequency(mut self, frequency: usize) -> Self {
        self.update_frequency = frequency;
        self
    }

    /// Set the forgetting factor for old connections
    pub fn forgetting_factor(mut self, factor: f64) -> Self {
        self.forgetting_factor = factor;
        self
    }

    /// Enable/disable adaptive threshold for edge addition
    pub fn adaptive_threshold(mut self, adaptive: bool) -> Self {
        self.adaptive_threshold = adaptive;
        self
    }

    /// Set minimum samples required before updating the graph
    pub fn min_samples_update(mut self, min_samples: usize) -> Self {
        self.min_samples_update = min_samples;
        self
    }

    /// Set the number of nearest neighbors to consider
    pub fn k_neighbors(mut self, k: usize) -> Self {
        self.k_neighbors = k;
        self
    }

    /// Set the similarity threshold for edge creation
    pub fn similarity_threshold(mut self, threshold: f64) -> Self {
        self.similarity_threshold = threshold;
        self
    }

    fn compute_similarity(&self, x1: &ArrayView1<f64>, x2: &ArrayView1<f64>) -> f64 {
        let diff = x1 - x2;
        let dist = diff.mapv(|x: f64| x * x).sum().sqrt();
        (-dist / (2.0 * 1.0_f64.powi(2))).exp()
    }

    #[allow(non_snake_case)] // standard ML notation
    fn build_initial_graph(&self, X: &Array2<f64>) -> Array2<f64> {
        let n_samples = X.nrows();
        let mut W = Array2::zeros((n_samples, n_samples));

        for i in 0..n_samples {
            let mut similarities: Vec<(usize, f64)> = Vec::new();

            for j in 0..n_samples {
                if i != j {
                    let sim = self.compute_similarity(&X.row(i), &X.row(j));
                    similarities.push((j, sim));
                }
            }

            // Sort by similarity (descending)
            similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed"));

            // Connect to k nearest neighbors
            for &(j, sim) in similarities.iter().take(self.k_neighbors) {
                if sim > self.similarity_threshold {
                    W[[i, j]] = sim;
                    W[[j, i]] = sim; // Ensure symmetry
                }
            }
        }

        // Apply sparsity threshold
        let threshold = self.lambda_sparse;
        W.mapv_inplace(|x| if x > threshold { x - threshold } else { 0.0 });
        W.mapv_inplace(|x| x.max(0.0));

        // Zero diagonal
        for i in 0..n_samples {
            W[[i, i]] = 0.0;
        }

        W
    }

    #[allow(non_snake_case)]
    fn propagate_labels(&self, W: &Array2<f64>, Y_init: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n_samples = W.nrows();

        // Compute transition matrix
        let D = W.sum_axis(Axis(1));
        let mut P = Array2::zeros((n_samples, n_samples));
        for i in 0..n_samples {
            if D[i] > 0.0 {
                for j in 0..n_samples {
                    P[[i, j]] = W[[i, j]] / D[i];
                }
            }
        }

        let mut Y = Y_init.clone();
        let Y_static = Y_init.clone();

        // Label propagation iterations
        for _iter in 0..30 {
            let prev_Y = Y.clone();
            Y = 0.8 * P.dot(&Y) + 0.2 * &Y_static;

            // Check convergence
            let diff = (&Y - &prev_Y).mapv(|x| x.abs()).sum();
            if diff < 1e-6 {
                break;
            }
        }

        Ok(Y)
    }
}

impl Default for StreamingGraphLearning<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl Estimator for StreamingGraphLearning<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = Float;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<ArrayView2<'_, Float>, ArrayView1<'_, i32>> for StreamingGraphLearning<Untrained> {
    type Fitted = StreamingGraphLearning<StreamingGraphLearningTrained>;

    #[allow(non_snake_case)]
    fn fit(self, X: &ArrayView2<'_, Float>, y: &ArrayView1<'_, i32>) -> SklResult<Self::Fitted> {
        let X = X.to_owned();
        let y = y.to_owned();
        let (n_samples, _n_features) = X.dim();

        // Identify labeled samples and classes
        let mut labeled_indices = Vec::new();
        let mut classes = std::collections::HashSet::new();

        for (i, &label) in y.iter().enumerate() {
            if label != -1 {
                labeled_indices.push(i);
                classes.insert(label);
            }
        }

        if labeled_indices.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No labeled samples provided".to_string(),
            ));
        }

        let classes: Vec<i32> = classes.into_iter().collect();
        let n_classes = classes.len();

        // Build initial graph
        let W = self.build_initial_graph(&X);

        // Initialize label matrix
        let mut Y = Array2::zeros((n_samples, n_classes));
        for &idx in &labeled_indices {
            if let Some(class_idx) = classes.iter().position(|&c| c == y[idx]) {
                Y[[idx, class_idx]] = 1.0;
            }
        }

        // Perform initial label propagation
        let Y_final = self.propagate_labels(&W, &Y)?;

        // Initialize sliding window with current data
        let mut data_window = VecDeque::with_capacity(self.window_size);
        let mut label_window = VecDeque::with_capacity(self.window_size);

        for i in 0..n_samples {
            data_window.push_back(X.row(i).to_owned());
            label_window.push_back(y[i]);
        }

        Ok(StreamingGraphLearning {
            state: StreamingGraphLearningTrained {
                X_train: X,
                y_train: y,
                classes: Array1::from(classes),
                current_graph: W,
                label_distributions: Y_final,
                data_window,
                label_window,
                update_count: 0,
                edge_ages: HashMap::new(),
                adaptive_threshold_value: self.similarity_threshold,
            },
            window_size: self.window_size,
            lambda_sparse: self.lambda_sparse,
            alpha_decay: self.alpha_decay,
            update_frequency: self.update_frequency,
            forgetting_factor: self.forgetting_factor,
            adaptive_threshold: self.adaptive_threshold,
            min_samples_update: self.min_samples_update,
            k_neighbors: self.k_neighbors,
            similarity_threshold: self.similarity_threshold,
        })
    }
}

impl StreamingGraphLearning<StreamingGraphLearningTrained> {
    fn compute_similarity(&self, x1: &ArrayView1<f64>, x2: &ArrayView1<f64>) -> f64 {
        let diff = x1 - x2;
        let dist = diff.mapv(|x: f64| x * x).sum().sqrt();
        (-dist / (2.0 * 1.0_f64.powi(2))).exp()
    }

    #[allow(non_snake_case)] // standard ML notation
    fn build_initial_graph(&self, X: &Array2<f64>) -> Array2<f64> {
        let n_samples = X.nrows();
        let mut W = Array2::zeros((n_samples, n_samples));

        for i in 0..n_samples {
            let mut similarities: Vec<(usize, f64)> = Vec::new();

            for j in 0..n_samples {
                if i != j {
                    let sim = self.compute_similarity(&X.row(i), &X.row(j));
                    similarities.push((j, sim));
                }
            }

            // Sort by similarity (descending)
            similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed"));

            // Connect to k nearest neighbors
            for &(j, sim) in similarities.iter().take(self.k_neighbors) {
                if sim > self.similarity_threshold {
                    W[[i, j]] = sim;
                    W[[j, i]] = sim; // Ensure symmetry
                }
            }
        }

        // Apply sparsity threshold
        let threshold = self.lambda_sparse;
        W.mapv_inplace(|x| if x > threshold { x - threshold } else { 0.0 });
        W.mapv_inplace(|x| x.max(0.0));

        // Zero diagonal
        for i in 0..n_samples {
            W[[i, i]] = 0.0;
        }

        W
    }

    #[allow(non_snake_case)]
    fn propagate_labels(&self, W: &Array2<f64>, Y_init: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n_samples = W.nrows();

        // Compute transition matrix
        let D = W.sum_axis(Axis(1));
        let mut P = Array2::zeros((n_samples, n_samples));
        for i in 0..n_samples {
            if D[i] > 0.0 {
                for j in 0..n_samples {
                    P[[i, j]] = W[[i, j]] / D[i];
                }
            }
        }

        let mut Y = Y_init.clone();
        let Y_static = Y_init.clone();

        // Label propagation iterations
        for _iter in 0..30 {
            let prev_Y = Y.clone();
            Y = 0.8 * P.dot(&Y) + 0.2 * &Y_static;

            // Check convergence
            let diff = (&Y - &prev_Y).mapv(|x| x.abs()).sum();
            if diff < 1e-6 {
                break;
            }
        }

        Ok(Y)
    }
    /// Update the model with new streaming data
    #[allow(non_snake_case)]
    pub fn update(
        &mut self,
        X_new: &ArrayView2<'_, Float>,
        y_new: &ArrayView1<'_, i32>,
    ) -> SklResult<()> {
        let X_new = X_new.to_owned();
        let y_new = y_new.to_owned();
        let (n_new, _) = X_new.dim();

        // Add new data to sliding window
        for i in 0..n_new {
            // Remove oldest data if window is full
            if self.state.data_window.len() >= self.window_size {
                self.state.data_window.pop_front();
                self.state.label_window.pop_front();
            }

            self.state.data_window.push_back(X_new.row(i).to_owned());
            self.state.label_window.push_back(y_new[i]);
        }

        self.state.update_count += n_new;

        // Decay existing edge weights
        self.state
            .current_graph
            .mapv_inplace(|x| x * self.alpha_decay);

        // Update adaptive threshold if enabled
        if self.adaptive_threshold {
            self.update_adaptive_threshold();
        }

        // Age all edges
        let mut aged_edges = HashMap::new();
        for ((i, j), age) in &self.state.edge_ages {
            aged_edges.insert((*i, *j), age + 1);
        }
        self.state.edge_ages = aged_edges;

        // Incremental graph update
        self.incremental_graph_update(&X_new, &y_new)?;

        // Full reconstruction if update frequency is reached
        if self
            .state
            .update_count
            .is_multiple_of(self.update_frequency)
        {
            self.full_graph_reconstruction()?;
        }

        Ok(())
    }

    fn update_adaptive_threshold(&mut self) {
        let current_data: Vec<Array1<f64>> = self.state.data_window.iter().cloned().collect();
        if current_data.len() < 2 {
            return;
        }

        let mut similarities = Vec::new();
        for i in 0..current_data.len().min(100) {
            for j in (i + 1)..current_data.len().min(100) {
                let sim = self.compute_similarity(&current_data[i].view(), &current_data[j].view());
                similarities.push(sim);
            }
        }

        if !similarities.is_empty() {
            similarities.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));
            let median_idx = similarities.len() / 2;
            self.state.adaptive_threshold_value = similarities[median_idx] * 0.8;
        }
    }

    #[allow(non_snake_case)] // standard ML notation
    fn incremental_graph_update(
        &mut self,
        X_new: &Array2<f64>,
        _y_new: &Array1<i32>,
    ) -> SklResult<()> {
        let current_data: Vec<Array1<f64>> = self.state.data_window.iter().cloned().collect();
        let current_labels: Vec<i32> = self.state.label_window.iter().cloned().collect();
        let n_current = current_data.len();
        let n_new = X_new.nrows();

        // Extend current graph to accommodate new nodes
        let mut new_graph = Array2::zeros((n_current, n_current));

        // Copy existing graph (with aging applied)
        let old_size = self.state.current_graph.nrows().min(n_current);
        for i in 0..old_size {
            for j in 0..old_size {
                new_graph[[i, j]] = self.state.current_graph[[i, j]];
            }
        }

        // Add connections for new nodes
        let start_idx = n_current - n_new;
        for i in start_idx..n_current {
            let mut similarities: Vec<(usize, f64)> = Vec::new();

            for j in 0..n_current {
                if i != j {
                    let sim =
                        self.compute_similarity(&current_data[i].view(), &current_data[j].view());
                    similarities.push((j, sim));
                }
            }

            // Sort by similarity (descending)
            similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed"));

            // Connect to k nearest neighbors
            let threshold = if self.adaptive_threshold {
                self.state.adaptive_threshold_value
            } else {
                self.similarity_threshold
            };

            for &(j, sim) in similarities.iter().take(self.k_neighbors) {
                if sim > threshold {
                    new_graph[[i, j]] = sim;
                    new_graph[[j, i]] = sim; // Ensure symmetry

                    // Track edge age
                    self.state.edge_ages.insert((i, j), 0);
                    self.state.edge_ages.insert((j, i), 0);
                }
            }
        }

        // Apply forgetting to old edges
        for ((i, j), age) in &self.state.edge_ages {
            if *i < n_current && *j < n_current {
                let forgetting_weight = self.forgetting_factor.powi(*age as i32);
                new_graph[[*i, *j]] *= forgetting_weight;
            }
        }

        // Apply sparsity threshold
        let threshold = self.lambda_sparse;
        new_graph.mapv_inplace(|x| if x > threshold { x - threshold } else { 0.0 });
        new_graph.mapv_inplace(|x| x.max(0.0));

        // Zero diagonal
        for i in 0..n_current {
            new_graph[[i, i]] = 0.0;
        }

        self.state.current_graph = new_graph;

        // Update label propagation
        self.update_label_propagation(&current_data, &current_labels)?;

        Ok(())
    }

    #[allow(non_snake_case)] // standard ML notation
    fn full_graph_reconstruction(&mut self) -> SklResult<()> {
        let current_data: Vec<Array1<f64>> = self.state.data_window.iter().cloned().collect();
        let current_labels: Vec<i32> = self.state.label_window.iter().cloned().collect();

        if current_data.is_empty() {
            return Ok(());
        }

        let n_samples = current_data.len();

        // Convert data to Array2
        let mut X = Array2::zeros((n_samples, current_data[0].len()));
        for (i, data_point) in current_data.iter().enumerate() {
            X.row_mut(i).assign(data_point);
        }

        // Rebuild graph from scratch
        self.state.current_graph = self.build_initial_graph(&X);

        // Clear edge ages
        self.state.edge_ages.clear();

        // Update label propagation
        self.update_label_propagation(&current_data, &current_labels)?;

        Ok(())
    }

    #[allow(non_snake_case)]
    fn update_label_propagation(
        &mut self,
        current_data: &[Array1<f64>],
        current_labels: &[i32],
    ) -> SklResult<()> {
        let n_samples = current_data.len();
        let n_classes = self.state.classes.len();

        if n_samples == 0 {
            return Ok(());
        }

        // Initialize label matrix
        let mut Y = Array2::zeros((n_samples, n_classes));
        for (i, &label) in current_labels.iter().enumerate() {
            if label != -1 {
                if let Some(class_idx) = self.state.classes.iter().position(|&c| c == label) {
                    Y[[i, class_idx]] = 1.0;
                }
            }
        }

        // Perform label propagation
        let Y_final = self.propagate_labels(&self.state.current_graph, &Y)?;
        self.state.label_distributions = Y_final;

        Ok(())
    }
}

impl Predict<ArrayView2<'_, Float>, Array1<i32>>
    for StreamingGraphLearning<StreamingGraphLearningTrained>
{
    #[allow(non_snake_case)]
    fn predict(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array1<i32>> {
        let X = X.to_owned();
        let n_test = X.nrows();
        let mut predictions = Array1::zeros(n_test);

        let current_data: Vec<Array1<f64>> = self.state.data_window.iter().cloned().collect();

        for i in 0..n_test {
            let mut max_sim = -1.0;
            let mut best_idx = 0;

            // Find most similar sample in current window
            for (j, data_point) in current_data.iter().enumerate() {
                let sim = self.compute_similarity(&X.row(i), &data_point.view());
                if sim > max_sim {
                    max_sim = sim;
                    best_idx = j;
                }
            }

            // Use the label distribution of the most similar sample
            if best_idx < self.state.label_distributions.nrows() {
                let distributions = self.state.label_distributions.row(best_idx);
                let max_idx = distributions
                    .iter()
                    .enumerate()
                    .max_by(|a, b| a.1.partial_cmp(b.1).expect("operation should succeed"))
                    .expect("operation should succeed")
                    .0;

                predictions[i] = self.state.classes[max_idx];
            }
        }

        Ok(predictions)
    }
}

impl PredictProba<ArrayView2<'_, Float>, Array2<f64>>
    for StreamingGraphLearning<StreamingGraphLearningTrained>
{
    #[allow(non_snake_case)]
    fn predict_proba(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array2<f64>> {
        let X = X.to_owned();
        let n_test = X.nrows();
        let n_classes = self.state.classes.len();
        let mut probas = Array2::zeros((n_test, n_classes));

        let current_data: Vec<Array1<f64>> = self.state.data_window.iter().cloned().collect();

        for i in 0..n_test {
            let mut max_sim = -1.0;
            let mut best_idx = 0;

            // Find most similar sample in current window
            for (j, data_point) in current_data.iter().enumerate() {
                let sim = self.compute_similarity(&X.row(i), &data_point.view());
                if sim > max_sim {
                    max_sim = sim;
                    best_idx = j;
                }
            }

            // Copy the label distribution
            if best_idx < self.state.label_distributions.nrows() {
                for k in 0..n_classes {
                    probas[[i, k]] = self.state.label_distributions[[best_idx, k]];
                }
            }
        }

        Ok(probas)
    }
}

/// Trained state for StreamingGraphLearning
#[derive(Debug, Clone)]
#[allow(non_snake_case)] // standard ML notation
pub struct StreamingGraphLearningTrained {
    /// X_train
    pub X_train: Array2<f64>,
    /// y_train
    pub y_train: Array1<i32>,
    /// classes
    pub classes: Array1<i32>,
    /// current_graph
    pub current_graph: Array2<f64>,
    /// label_distributions
    pub label_distributions: Array2<f64>,
    /// data_window
    pub data_window: VecDeque<Array1<f64>>,
    /// label_window
    pub label_window: VecDeque<i32>,
    /// update_count
    pub update_count: usize,
    /// edge_ages
    pub edge_ages: HashMap<(usize, usize), usize>,
    /// adaptive_threshold_value
    pub adaptive_threshold_value: f64,
}

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

    #[test]
    #[allow(non_snake_case)]
    fn test_streaming_graph_learning_basic() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let sgl = StreamingGraphLearning::new()
            .window_size(10)
            .lambda_sparse(0.1)
            .alpha_decay(0.9)
            .update_frequency(5);
        let fitted = sgl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));

        // Check that labeled samples maintain their labels
        assert_eq!(predictions[0], 0);
        assert_eq!(predictions[1], 1);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_streaming_graph_learning_update() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1];

        let sgl = StreamingGraphLearning::new()
            .window_size(10)
            .update_frequency(3)
            .alpha_decay(0.95);
        let mut fitted = sgl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        // Initial graph size
        let initial_graph_size = fitted.state.current_graph.dim();
        assert_eq!(initial_graph_size, (4, 4));

        // Add new streaming data
        let X_new = array![[5.0, 6.0], [6.0, 7.0]];
        let y_new = array![-1, 0];
        fitted
            .update(&X_new.view(), &y_new.view())
            .expect("operation should succeed");

        // Check that data window is updated
        assert_eq!(fitted.state.data_window.len(), 6);
        assert_eq!(fitted.state.label_window.len(), 6);

        // Graph should be updated to accommodate new data
        let updated_graph_size = fitted.state.current_graph.dim();
        assert_eq!(updated_graph_size, (6, 6));

        // Test predictions with updated model
        let predictions = fitted
            .predict(&X_new.view())
            .expect("operation should succeed");
        assert_eq!(predictions.len(), 2);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_streaming_graph_learning_window_overflow() {
        let X = array![[1.0, 2.0], [2.0, 3.0]];
        let y = array![0, 1];

        let sgl = StreamingGraphLearning::new()
            .window_size(3) // Small window size
            .update_frequency(2);
        let mut fitted = sgl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        // Add more data than window size
        let X_new1 = array![[3.0, 4.0]];
        let y_new1 = array![-1];
        fitted
            .update(&X_new1.view(), &y_new1.view())
            .expect("operation should succeed");

        let X_new2 = array![[4.0, 5.0]];
        let y_new2 = array![0];
        fitted
            .update(&X_new2.view(), &y_new2.view())
            .expect("operation should succeed");

        // Window should maintain size limit
        assert_eq!(fitted.state.data_window.len(), 3);
        assert_eq!(fitted.state.label_window.len(), 3);

        // Should still be able to make predictions
        let predictions = fitted
            .predict(&X_new2.view())
            .expect("operation should succeed");
        assert_eq!(predictions.len(), 1);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_streaming_graph_learning_adaptive_threshold() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1];

        let sgl = StreamingGraphLearning::new()
            .window_size(10)
            .adaptive_threshold(true)
            .similarity_threshold(0.5);
        let mut fitted = sgl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let _initial_threshold = fitted.state.adaptive_threshold_value;

        // Add new data with different characteristics
        let X_new = array![[10.0, 20.0], [20.0, 30.0]];
        let y_new = array![-1, 1];
        fitted
            .update(&X_new.view(), &y_new.view())
            .expect("operation should succeed");

        // Adaptive threshold should potentially change
        // (depends on the similarity distribution)
        assert!(fitted.state.adaptive_threshold_value > 0.0);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_streaming_graph_learning_edge_aging() {
        let X = array![[1.0, 2.0], [2.0, 3.0]];
        let y = array![0, 1];

        let sgl = StreamingGraphLearning::new()
            .window_size(10)
            .forgetting_factor(0.8)
            .alpha_decay(0.9);
        let mut fitted = sgl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        // Check initial state
        assert_eq!(fitted.state.update_count, 0);

        // Add new data multiple times to age edges
        for i in 0..3 {
            let X_new = array![[3.0 + i as f64, 4.0 + i as f64]];
            let y_new = array![-1];
            fitted
                .update(&X_new.view(), &y_new.view())
                .expect("operation should succeed");
        }

        // Update count should be incremented
        assert_eq!(fitted.state.update_count, 3);

        // Some edges should have aged
        assert!(!fitted.state.edge_ages.is_empty());
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_streaming_graph_learning_full_reconstruction() {
        let X = array![[1.0, 2.0], [2.0, 3.0]];
        let y = array![0, 1];

        let sgl = StreamingGraphLearning::new()
            .window_size(10)
            .update_frequency(2); // Trigger full reconstruction frequently
        let mut fitted = sgl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        // Add data to trigger full reconstruction
        let X_new1 = array![[3.0, 4.0]];
        let y_new1 = array![-1];
        fitted
            .update(&X_new1.view(), &y_new1.view())
            .expect("operation should succeed");

        let X_new2 = array![[4.0, 5.0]];
        let y_new2 = array![0];
        fitted
            .update(&X_new2.view(), &y_new2.view())
            .expect("operation should succeed");

        // Full reconstruction should have been triggered
        // Edge ages should be cleared
        assert!(
            fitted.state.edge_ages.is_empty()
                || fitted.state.edge_ages.values().all(|&age| age == 0)
        );

        // Should still be able to make predictions
        let predictions = fitted
            .predict(&X_new2.view())
            .expect("operation should succeed");
        assert_eq!(predictions.len(), 1);
    }
}