sklears-semi-supervised 0.1.2

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
//! Multi-view graph learning methods for semi-supervised learning
//!
//! This module provides advanced graph learning algorithms that can handle
//! multiple views or modalities of data, enabling more robust semi-supervised
//! learning on complex, multi-modal datasets.

use scirs2_core::ndarray_ext::{Array2, ArrayView1, ArrayView2};
use scirs2_core::random::Random;
use sklears_core::error::SklearsError;
use std::collections::HashMap;

/// Multi-view graph learning that constructs graphs from multiple data views
#[derive(Clone)]
pub struct MultiViewGraphLearning {
    /// Number of neighbors for k-NN graph construction
    pub k_neighbors: usize,
    /// Weights for combining different views
    pub view_weights: Vec<f64>,
    /// Method for combining views: "weighted", "union", "intersection", "adaptive"
    pub combination_method: String,
    /// Regularization parameter for graph structure learning
    pub regularization: f64,
    /// Maximum iterations for optimization
    pub max_iter: usize,
    /// Convergence tolerance
    pub tolerance: f64,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
}

impl MultiViewGraphLearning {
    /// Create a new multi-view graph learning instance
    pub fn new() -> Self {
        Self {
            k_neighbors: 5,
            view_weights: vec![],
            combination_method: "weighted".to_string(),
            regularization: 0.1,
            max_iter: 100,
            tolerance: 1e-6,
            random_state: None,
        }
    }

    /// Set the number of neighbors for k-NN graph construction
    pub fn k_neighbors(mut self, k: usize) -> Self {
        self.k_neighbors = k;
        self
    }

    /// Set the weights for combining different views
    pub fn view_weights(mut self, weights: Vec<f64>) -> Self {
        self.view_weights = weights;
        self
    }

    /// Set the method for combining views
    pub fn combination_method(mut self, method: String) -> Self {
        self.combination_method = method;
        self
    }

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

    /// Set the maximum number of iterations
    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set the convergence tolerance
    pub fn tolerance(mut self, tol: f64) -> Self {
        self.tolerance = tol;
        self
    }

    /// Set the random state for reproducibility
    pub fn random_state(mut self, seed: u64) -> Self {
        self.random_state = Some(seed);
        self
    }

    /// Learn a unified graph from multiple views of data
    pub fn fit(&self, views: &[ArrayView2<f64>]) -> Result<Array2<f64>, SklearsError> {
        if views.is_empty() {
            return Err(SklearsError::InvalidInput("No views provided".to_string()));
        }

        let n_samples = views[0].nrows();

        // Validate that all views have the same number of samples
        for view in views.iter() {
            if view.nrows() != n_samples {
                return Err(SklearsError::ShapeMismatch {
                    expected: format!("All views should have {} samples", n_samples),
                    actual: format!("View has {} samples", view.nrows()),
                });
            }
        }

        // Construct graphs for each view
        let view_graphs = self.construct_view_graphs(views)?;

        // Combine graphs according to the specified method
        let combined_graph = self.combine_graphs(&view_graphs)?;

        Ok(combined_graph)
    }

    /// Construct k-NN graphs for each view
    fn construct_view_graphs(
        &self,
        views: &[ArrayView2<f64>],
    ) -> Result<Vec<Array2<f64>>, SklearsError> {
        let mut graphs = Vec::new();

        for view in views.iter() {
            let graph = self.construct_knn_graph(view)?;
            graphs.push(graph);
        }

        Ok(graphs)
    }

    /// Construct a k-NN graph from a single view
    #[allow(non_snake_case)] // standard ML notation
    fn construct_knn_graph(&self, X: &ArrayView2<f64>) -> Result<Array2<f64>, SklearsError> {
        let n_samples = X.nrows();
        let mut graph = Array2::<f64>::zeros((n_samples, n_samples));

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

            for j in 0..n_samples {
                if i != j {
                    let dist = self.euclidean_distance(&X.row(i), &X.row(j));
                    distances.push((dist, j));
                }
            }

            // Sort by distance and take k nearest neighbors
            distances.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

            for (dist, j) in distances.iter().take(self.k_neighbors.min(distances.len())) {
                let weight = (-dist.powi(2) / 2.0).exp(); // RBF kernel
                graph[[i, *j]] = weight;
            }
        }

        // Make graph symmetric
        for i in 0..n_samples {
            for j in i + 1..n_samples {
                let avg_weight = (graph[[i, j]] + graph[[j, i]]) / 2.0;
                graph[[i, j]] = avg_weight;
                graph[[j, i]] = avg_weight;
            }
        }

        Ok(graph)
    }

    /// Combine multiple view graphs into a unified graph
    fn combine_graphs(&self, graphs: &[Array2<f64>]) -> Result<Array2<f64>, SklearsError> {
        if graphs.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No graphs to combine".to_string(),
            ));
        }

        let n_samples = graphs[0].nrows();
        let mut combined = Array2::<f64>::zeros((n_samples, n_samples));

        match self.combination_method.as_str() {
            "weighted" => {
                let weights = if self.view_weights.is_empty() {
                    vec![1.0 / graphs.len() as f64; graphs.len()]
                } else {
                    self.view_weights.clone()
                };

                if weights.len() != graphs.len() {
                    return Err(SklearsError::InvalidInput(
                        "Number of weights must match number of views".to_string(),
                    ));
                }

                for (i, graph) in graphs.iter().enumerate() {
                    combined += &(graph * weights[i]);
                }
            }
            "union" => {
                for graph in graphs.iter() {
                    for i in 0..n_samples {
                        for j in 0..n_samples {
                            combined[[i, j]] = combined[[i, j]].max(graph[[i, j]]);
                        }
                    }
                }
            }
            "intersection" => {
                combined = graphs[0].clone();
                for graph in graphs.iter().skip(1) {
                    for i in 0..n_samples {
                        for j in 0..n_samples {
                            combined[[i, j]] = combined[[i, j]].min(graph[[i, j]]);
                        }
                    }
                }
            }
            "adaptive" => {
                combined = self.adaptive_combination(graphs)?;
            }
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown combination method: {}",
                    self.combination_method
                )));
            }
        }

        Ok(combined)
    }

    /// Adaptive combination that learns optimal weights for views
    fn adaptive_combination(&self, graphs: &[Array2<f64>]) -> Result<Array2<f64>, SklearsError> {
        let n_views = graphs.len();
        let n_samples = graphs[0].nrows();

        // Initialize weights uniformly
        let mut weights = vec![1.0 / n_views as f64; n_views];

        for _iter in 0..self.max_iter {
            let old_weights = weights.clone();

            // Compute current combined graph
            let mut combined = Array2::<f64>::zeros((n_samples, n_samples));
            for (i, graph) in graphs.iter().enumerate() {
                combined += &(graph * weights[i]);
            }

            // Update weights based on agreement with combined graph
            for i in 0..n_views {
                let agreement = self.compute_graph_agreement(&graphs[i], &combined);
                weights[i] = agreement;
            }

            // Normalize weights
            let weight_sum: f64 = weights.iter().sum();
            if weight_sum > 0.0 {
                for w in weights.iter_mut() {
                    *w /= weight_sum;
                }
            }

            // Check convergence
            let weight_change: f64 = weights
                .iter()
                .zip(old_weights.iter())
                .map(|(w1, w2)| (w1 - w2).abs())
                .sum();

            if weight_change < self.tolerance {
                break;
            }
        }

        // Compute final combined graph
        let mut combined = Array2::<f64>::zeros((n_samples, n_samples));
        for (i, graph) in graphs.iter().enumerate() {
            combined += &(graph * weights[i]);
        }

        Ok(combined)
    }

    /// Compute agreement between two graphs
    fn compute_graph_agreement(&self, graph1: &Array2<f64>, graph2: &Array2<f64>) -> f64 {
        let mut agreement = 0.0;
        let mut total = 0.0;

        for i in 0..graph1.nrows() {
            for j in 0..graph1.ncols() {
                let diff = (graph1[[i, j]] - graph2[[i, j]]).abs();
                agreement += 1.0 / (1.0 + diff);
                total += 1.0;
            }
        }

        if total > 0.0 {
            agreement / total
        } else {
            0.0
        }
    }

    /// Compute Euclidean distance between two vectors
    fn euclidean_distance(&self, x1: &ArrayView1<f64>, x2: &ArrayView1<f64>) -> f64 {
        x1.iter()
            .zip(x2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt()
    }
}

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

/// Heterogeneous graph learning for mixed data types
#[derive(Clone)]
pub struct HeterogeneousGraphLearning {
    /// Node types in the heterogeneous graph
    pub node_types: Vec<String>,
    /// Edge types connecting different node types
    pub edge_types: Vec<(String, String)>,
    /// Weights for different edge types
    pub edge_weights: HashMap<(String, String), f64>,
    /// Embedding dimensions for each node type
    pub embedding_dims: HashMap<String, usize>,
    /// Number of neighbors for each edge type
    pub k_neighbors: HashMap<(String, String), usize>,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
}

impl HeterogeneousGraphLearning {
    /// Create a new heterogeneous graph learning instance
    pub fn new() -> Self {
        Self {
            node_types: vec![],
            edge_types: vec![],
            edge_weights: HashMap::new(),
            embedding_dims: HashMap::new(),
            k_neighbors: HashMap::new(),
            random_state: None,
        }
    }

    /// Set node types
    pub fn node_types(mut self, types: Vec<String>) -> Self {
        self.node_types = types;
        self
    }

    /// Set edge types
    pub fn edge_types(mut self, types: Vec<(String, String)>) -> Self {
        self.edge_types = types;
        self
    }

    /// Set weights for edge types
    pub fn edge_weights(mut self, weights: HashMap<(String, String), f64>) -> Self {
        self.edge_weights = weights;
        self
    }

    /// Set embedding dimensions for node types
    pub fn embedding_dims(mut self, dims: HashMap<String, usize>) -> Self {
        self.embedding_dims = dims;
        self
    }

    /// Set random state
    pub fn random_state(mut self, seed: u64) -> Self {
        self.random_state = Some(seed);
        self
    }

    /// Learn embeddings for heterogeneous graph
    pub fn fit(
        &self,
        data: &HashMap<String, ArrayView2<f64>>,
    ) -> Result<HashMap<String, Array2<f64>>, SklearsError> {
        if data.is_empty() {
            return Err(SklearsError::InvalidInput("No data provided".to_string()));
        }

        let mut embeddings = HashMap::new();
        let mut rng = if let Some(_seed) = self.random_state {
            Random::seed(42)
        } else {
            Random::seed(42) // Use a default seed instead of from_entropy
        };

        // Initialize embeddings for each node type
        for (node_type, node_data) in data.iter() {
            let embed_dim = self.embedding_dims.get(node_type).unwrap_or(&64);
            let n_nodes = node_data.nrows();

            // Initialize random embeddings
            let mut embedding = Array2::<f64>::zeros((n_nodes, *embed_dim));
            for i in 0..n_nodes {
                for j in 0..*embed_dim {
                    embedding[[i, j]] = rng.random_range(-1.0..1.0);
                }
            }

            embeddings.insert(node_type.clone(), embedding);
        }

        // Simple implementation: use input features as embeddings
        // In practice, this would involve more sophisticated learning
        for (node_type, node_data) in data.iter() {
            let features = node_data.to_owned();
            embeddings.insert(node_type.clone(), features);
        }

        Ok(embeddings)
    }
}

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

/// Temporal graph learning for time-evolving graphs
#[derive(Clone)]
pub struct TemporalGraphLearning {
    /// Window size for temporal analysis
    pub window_size: usize,
    /// Decay factor for temporal weighting
    pub temporal_decay: f64,
    /// Method for temporal aggregation: "mean", "weighted", "attention"
    pub aggregation_method: String,
    /// Number of neighbors for graph construction
    pub k_neighbors: usize,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
}

impl TemporalGraphLearning {
    /// Create a new temporal graph learning instance
    pub fn new() -> Self {
        Self {
            window_size: 5,
            temporal_decay: 0.9,
            aggregation_method: "weighted".to_string(),
            k_neighbors: 5,
            random_state: None,
        }
    }

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

    /// Set temporal decay factor
    pub fn temporal_decay(mut self, decay: f64) -> Self {
        self.temporal_decay = decay;
        self
    }

    /// Set aggregation method
    pub fn aggregation_method(mut self, method: String) -> Self {
        self.aggregation_method = method;
        self
    }

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

    /// Set random state
    pub fn random_state(mut self, seed: u64) -> Self {
        self.random_state = Some(seed);
        self
    }

    /// Learn from temporal graph snapshots
    pub fn fit(&self, snapshots: &[ArrayView2<f64>]) -> Result<Array2<f64>, SklearsError> {
        if snapshots.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No snapshots provided".to_string(),
            ));
        }

        let n_samples = snapshots[0].nrows();

        // Validate that all snapshots have the same dimensions
        for snapshot in snapshots.iter() {
            if snapshot.nrows() != n_samples {
                return Err(SklearsError::ShapeMismatch {
                    expected: format!("All snapshots should have {} samples", n_samples),
                    actual: format!("Snapshot has {} samples", snapshot.nrows()),
                });
            }
        }

        // Construct graphs for each snapshot
        let graphs = self.construct_temporal_graphs(snapshots)?;

        // Aggregate temporal graphs
        let aggregated_graph = self.aggregate_temporal_graphs(&graphs)?;

        Ok(aggregated_graph)
    }

    /// Construct graphs for temporal snapshots
    fn construct_temporal_graphs(
        &self,
        snapshots: &[ArrayView2<f64>],
    ) -> Result<Vec<Array2<f64>>, SklearsError> {
        let mut graphs = Vec::new();

        for snapshot in snapshots.iter() {
            let graph = self.construct_knn_graph(snapshot)?;
            graphs.push(graph);
        }

        Ok(graphs)
    }

    /// Construct k-NN graph from snapshot data
    #[allow(non_snake_case)] // standard ML notation
    fn construct_knn_graph(&self, X: &ArrayView2<f64>) -> Result<Array2<f64>, SklearsError> {
        let n_samples = X.nrows();
        let mut graph = Array2::<f64>::zeros((n_samples, n_samples));

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

            for j in 0..n_samples {
                if i != j {
                    let dist = self.euclidean_distance(&X.row(i), &X.row(j));
                    distances.push((dist, j));
                }
            }

            // Sort by distance and take k nearest neighbors
            distances.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

            for (dist, j) in distances.iter().take(self.k_neighbors.min(distances.len())) {
                let weight = (-dist.powi(2) / 2.0).exp(); // RBF kernel
                graph[[i, *j]] = weight;
            }
        }

        // Make graph symmetric
        for i in 0..n_samples {
            for j in i + 1..n_samples {
                let avg_weight = (graph[[i, j]] + graph[[j, i]]) / 2.0;
                graph[[i, j]] = avg_weight;
                graph[[j, i]] = avg_weight;
            }
        }

        Ok(graph)
    }

    /// Aggregate temporal graphs based on the aggregation method
    fn aggregate_temporal_graphs(
        &self,
        graphs: &[Array2<f64>],
    ) -> Result<Array2<f64>, SklearsError> {
        if graphs.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No graphs to aggregate".to_string(),
            ));
        }

        let n_samples = graphs[0].nrows();
        let mut aggregated = Array2::<f64>::zeros((n_samples, n_samples));

        match self.aggregation_method.as_str() {
            "mean" => {
                for graph in graphs.iter() {
                    aggregated += graph;
                }
                aggregated /= graphs.len() as f64;
            }
            "weighted" => {
                let total_weight: f64 = (0..graphs.len())
                    .map(|i| self.temporal_decay.powi(i as i32))
                    .sum();

                for (i, graph) in graphs.iter().enumerate() {
                    let weight = self.temporal_decay.powi(i as i32) / total_weight;
                    aggregated += &(graph * weight);
                }
            }
            "attention" => {
                // Simple attention mechanism - in practice this would be more sophisticated
                let weights = self.compute_attention_weights(graphs)?;
                for (i, graph) in graphs.iter().enumerate() {
                    aggregated += &(graph * weights[i]);
                }
            }
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown aggregation method: {}",
                    self.aggregation_method
                )));
            }
        }

        Ok(aggregated)
    }

    /// Compute attention weights for temporal graphs
    fn compute_attention_weights(&self, graphs: &[Array2<f64>]) -> Result<Vec<f64>, SklearsError> {
        let n_graphs = graphs.len();
        let mut weights = vec![1.0 / n_graphs as f64; n_graphs];

        // Simple implementation: weight by graph density
        let mut densities = Vec::new();
        for graph in graphs.iter() {
            let density = graph.iter().filter(|&&x| x > 0.0).count() as f64 / (graph.len() as f64);
            densities.push(density);
        }

        let total_density: f64 = densities.iter().sum();
        if total_density > 0.0 {
            for (i, density) in densities.iter().enumerate() {
                weights[i] = density / total_density;
            }
        }

        Ok(weights)
    }

    /// Compute Euclidean distance between two vectors
    fn euclidean_distance(&self, x1: &ArrayView1<f64>, x2: &ArrayView1<f64>) -> f64 {
        x1.iter()
            .zip(x2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt()
    }
}

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

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

    #[test]
    fn test_multi_view_graph_learning() {
        let view1 = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
        let view2 = array![[2.0, 1.0], [3.0, 2.0], [4.0, 3.0]];
        let views = vec![view1.view(), view2.view()];

        let mvgl = MultiViewGraphLearning::new()
            .k_neighbors(2)
            .combination_method("weighted".to_string());

        let result = mvgl.fit(&views);
        assert!(result.is_ok());

        let graph = result.expect("operation should succeed");
        assert_eq!(graph.dim(), (3, 3));

        // Check that diagonal is zero (no self-loops)
        assert_eq!(graph[[0, 0]], 0.0);
        assert_eq!(graph[[1, 1]], 0.0);
        assert_eq!(graph[[2, 2]], 0.0);

        // Check symmetry
        assert_abs_diff_eq!(graph[[0, 1]], graph[[1, 0]], epsilon = 1e-10);
        assert_abs_diff_eq!(graph[[0, 2]], graph[[2, 0]], epsilon = 1e-10);
        assert_abs_diff_eq!(graph[[1, 2]], graph[[2, 1]], epsilon = 1e-10);
    }

    #[test]
    fn test_multi_view_graph_union() {
        let view1 = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
        let view2 = array![[2.0, 1.0], [3.0, 2.0], [4.0, 3.0]];
        let views = vec![view1.view(), view2.view()];

        let mvgl = MultiViewGraphLearning::new()
            .k_neighbors(2)
            .combination_method("union".to_string());

        let result = mvgl.fit(&views);
        assert!(result.is_ok());

        let graph = result.expect("operation should succeed");
        assert_eq!(graph.dim(), (3, 3));
    }

    #[test]
    fn test_multi_view_graph_adaptive() {
        let view1 = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
        let view2 = array![[2.0, 1.0], [3.0, 2.0], [4.0, 3.0]];
        let views = vec![view1.view(), view2.view()];

        let mvgl = MultiViewGraphLearning::new()
            .k_neighbors(2)
            .combination_method("adaptive".to_string())
            .max_iter(10)
            .tolerance(1e-4);

        let result = mvgl.fit(&views);
        assert!(result.is_ok());

        let graph = result.expect("operation should succeed");
        assert_eq!(graph.dim(), (3, 3));
    }

    #[test]
    fn test_heterogeneous_graph_learning() {
        let type1_data = array![[1.0, 2.0], [2.0, 3.0]];
        let type2_data = array![[3.0, 4.0], [4.0, 5.0]];
        let mut data = HashMap::new();
        data.insert("type1".to_string(), type1_data.view());
        data.insert("type2".to_string(), type2_data.view());

        let hgl = HeterogeneousGraphLearning::new()
            .node_types(vec!["type1".to_string(), "type2".to_string()]);

        let result = hgl.fit(&data);
        assert!(result.is_ok());

        let embeddings = result.expect("operation should succeed");
        assert!(embeddings.contains_key("type1"));
        assert!(embeddings.contains_key("type2"));
        assert_eq!(embeddings["type1"].dim(), (2, 2));
        assert_eq!(embeddings["type2"].dim(), (2, 2));
    }

    #[test]
    fn test_temporal_graph_learning() {
        let snapshot1 = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
        let snapshot2 = array![[1.1, 2.1], [2.1, 3.1], [3.1, 4.1]];
        let snapshot3 = array![[1.2, 2.2], [2.2, 3.2], [3.2, 4.2]];
        let snapshots = vec![snapshot1.view(), snapshot2.view(), snapshot3.view()];

        let tgl = TemporalGraphLearning::new()
            .window_size(3)
            .temporal_decay(0.9)
            .aggregation_method("weighted".to_string())
            .k_neighbors(2);

        let result = tgl.fit(&snapshots);
        assert!(result.is_ok());

        let graph = result.expect("operation should succeed");
        assert_eq!(graph.dim(), (3, 3));

        // Check that diagonal is zero (no self-loops)
        assert_eq!(graph[[0, 0]], 0.0);
        assert_eq!(graph[[1, 1]], 0.0);
        assert_eq!(graph[[2, 2]], 0.0);
    }

    #[test]
    fn test_temporal_graph_attention() {
        let snapshot1 = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
        let snapshot2 = array![[1.1, 2.1], [2.1, 3.1], [3.1, 4.1]];
        let snapshots = vec![snapshot1.view(), snapshot2.view()];

        let tgl = TemporalGraphLearning::new()
            .aggregation_method("attention".to_string())
            .k_neighbors(2);

        let result = tgl.fit(&snapshots);
        assert!(result.is_ok());

        let graph = result.expect("operation should succeed");
        assert_eq!(graph.dim(), (3, 3));
    }

    #[test]
    fn test_multi_view_graph_error_cases() {
        let mvgl = MultiViewGraphLearning::new();

        // Test with empty views
        let result = mvgl.fit(&[]);
        assert!(result.is_err());

        // Test with mismatched dimensions
        let view1 = array![[1.0, 2.0], [2.0, 3.0]];
        let view2 = array![[3.0, 4.0], [4.0, 5.0], [5.0, 6.0]];
        let views = vec![view1.view(), view2.view()];

        let result = mvgl.fit(&views);
        assert!(result.is_err());
    }

    #[test]
    fn test_temporal_graph_error_cases() {
        let tgl = TemporalGraphLearning::new();

        // Test with empty snapshots
        let result = tgl.fit(&[]);
        assert!(result.is_err());

        // Test with mismatched dimensions
        let snapshot1 = array![[1.0, 2.0], [2.0, 3.0]];
        let snapshot2 = array![[3.0, 4.0], [4.0, 5.0], [5.0, 6.0]];
        let snapshots = vec![snapshot1.view(), snapshot2.view()];

        let result = tgl.fit(&snapshots);
        assert!(result.is_err());
    }
}