sklears-manifold 0.1.2

Manifold learning algorithms (t-SNE, Isomap, etc.)
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
//! Trait-based manifold learning framework
//!
//! This module provides a unified trait system for manifold learning algorithms,
//! enabling composable and extensible manifold learning pipelines.

use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
/// Core trait for manifold learning algorithms
use sklears_core::error::Result as SklResult;
use std::collections::HashMap;
pub trait ManifoldLearning {
    /// Get the intrinsic dimensionality of the learned manifold
    fn intrinsic_dimension(&self) -> Option<usize>;

    /// Get the embedding dimension
    fn embedding_dimension(&self) -> usize;

    /// Get algorithm-specific parameters
    fn parameters(&self) -> HashMap<String, f64>;

    /// Get algorithm name
    fn algorithm_name(&self) -> &'static str;

    /// Check if the algorithm supports out-of-sample extension
    fn supports_transform(&self) -> bool;

    /// Get the computational complexity
    fn complexity(&self) -> ManifoldComplexity;
}

/// Trait for algorithms that support distance metrics
pub trait DistanceMetric {
    /// Compute pairwise distances between points
    fn pairwise_distances(
        &self,
        x: ArrayView2<f64>,
        y: Option<ArrayView2<f64>>,
    ) -> SklResult<Array2<f64>>;

    /// Get the name of the distance metric
    fn metric_name(&self) -> &'static str;

    /// Check if the metric satisfies the triangle inequality
    fn is_metric(&self) -> bool;
}

/// Trait for neighborhood-based algorithms
pub trait NeighborhoodBased {
    /// Get the number of neighbors used
    fn n_neighbors(&self) -> usize;

    /// Set the number of neighbors
    fn set_n_neighbors(&mut self, n_neighbors: usize);

    /// Get the neighborhood connectivity matrix
    fn neighborhood_graph(&self) -> Option<Array2<f64>>;
}

/// Trait for algorithms with random initialization
pub trait RandomizedAlgorithm {
    /// Get the random state
    fn random_state(&self) -> Option<u64>;

    /// Set the random state for reproducibility
    fn set_random_state(&mut self, random_state: Option<u64>);
}

/// Trait for iterative optimization algorithms
pub trait IterativeOptimization {
    /// Get the number of iterations performed
    fn n_iter(&self) -> usize;

    /// Get the maximum number of iterations
    fn max_iter(&self) -> usize;

    /// Set the maximum number of iterations
    fn set_max_iter(&mut self, max_iter: usize);

    /// Get the convergence tolerance
    fn tolerance(&self) -> f64;

    /// Set the convergence tolerance
    fn set_tolerance(&mut self, tolerance: f64);

    /// Get the optimization history
    fn optimization_history(&self) -> Option<Vec<f64>>;
}

/// Trait for spectral embedding algorithms
pub trait SpectralEmbedding {
    /// Get the eigenvalues from the spectral decomposition
    fn eigenvalues(&self) -> Option<Array1<f64>>;

    /// Get the eigenvectors from the spectral decomposition
    fn eigenvectors(&self) -> Option<Array2<f64>>;

    /// Get the number of components to keep
    fn n_components(&self) -> usize;

    /// Set the number of components to keep
    fn set_n_components(&mut self, n_components: usize);
}

/// Trait for kernel-based methods
pub trait KernelBased {
    /// Get the kernel matrix
    fn kernel_matrix(&self) -> Option<Array2<f64>>;

    /// Get the kernel parameters
    fn kernel_params(&self) -> HashMap<String, f64>;

    /// Compute kernel values between new points and training data
    fn kernel_transform(&self, x: ArrayView2<f64>) -> SklResult<Array2<f64>>;
}

/// Trait for probabilistic manifold learning
pub trait ProbabilisticEmbedding {
    /// Get the joint probabilities in high-dimensional space
    fn high_dim_probabilities(&self) -> Option<Array2<f64>>;

    /// Get the joint probabilities in low-dimensional space
    fn low_dim_probabilities(&self) -> Option<Array2<f64>>;

    /// Get the perplexity or equivalent parameter
    fn perplexity(&self) -> f64;

    /// Set the perplexity
    fn set_perplexity(&mut self, perplexity: f64);
}

/// Trait for manifold learning quality assessment
pub trait EmbeddingQuality {
    /// Compute trustworthiness of the embedding
    fn trustworthiness(
        &self,
        x: ArrayView2<f64>,
        x_embedded: ArrayView2<f64>,
        k: usize,
    ) -> SklResult<f64>;

    /// Compute continuity of the embedding
    fn continuity(
        &self,
        x: ArrayView2<f64>,
        x_embedded: ArrayView2<f64>,
        k: usize,
    ) -> SklResult<f64>;

    /// Compute neighborhood hit rate
    fn neighborhood_hit_rate(
        &self,
        x: ArrayView2<f64>,
        x_embedded: ArrayView2<f64>,
        k: usize,
    ) -> SklResult<f64>;

    /// Compute reconstruction error
    fn reconstruction_error(
        &self,
        x: ArrayView2<f64>,
        x_embedded: ArrayView2<f64>,
    ) -> SklResult<f64>;
}

/// Computational complexity of manifold learning algorithms
#[derive(Debug, Clone, PartialEq)]
pub enum ManifoldComplexity {
    /// Linear complexity O(n)
    Linear,
    /// Quadratic complexity O(n²)
    Quadratic,
    /// Cubic complexity O(n³)
    Cubic,
    /// Log-linear complexity O(n log n)
    LogLinear,
    /// Custom complexity with description
    Custom(String),
}

impl ManifoldComplexity {
    /// Get a human-readable description of the complexity
    pub fn description(&self) -> &str {
        match self {
            ManifoldComplexity::Linear => "O(n) - Linear time complexity",
            ManifoldComplexity::Quadratic => "O(n²) - Quadratic time complexity",
            ManifoldComplexity::Cubic => "O(n³) - Cubic time complexity",
            ManifoldComplexity::LogLinear => "O(n log n) - Log-linear time complexity",
            ManifoldComplexity::Custom(desc) => desc,
        }
    }
}

/// Configuration builder for manifold learning algorithms
#[derive(Debug, Clone)]
pub struct ManifoldConfig {
    /// Number of components in the embedding
    pub n_components: usize,
    /// Number of neighbors for neighborhood-based algorithms
    pub n_neighbors: Option<usize>,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
    /// Maximum number of iterations
    pub max_iter: Option<usize>,
    /// Convergence tolerance
    pub tolerance: Option<f64>,
    /// Distance metric name
    pub metric: Option<String>,
    /// Additional algorithm-specific parameters
    pub params: HashMap<String, f64>,
}

impl Default for ManifoldConfig {
    fn default() -> Self {
        Self {
            n_components: 2,
            n_neighbors: None,
            random_state: None,
            max_iter: None,
            tolerance: None,
            metric: None,
            params: HashMap::new(),
        }
    }
}

impl ManifoldConfig {
    /// Create a new configuration with default values
    pub fn new() -> Self {
        Self::default()
    }

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

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

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

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

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

    /// Set the distance metric
    pub fn metric(mut self, metric: impl Into<String>) -> Self {
        self.metric = Some(metric.into());
        self
    }

    /// Add a custom parameter
    pub fn param(mut self, key: impl Into<String>, value: f64) -> Self {
        self.params.insert(key.into(), value);
        self
    }
}

/// Preset configurations for common use cases
pub struct ManifoldPresets;

impl ManifoldPresets {
    /// Configuration for fast visualization (t-SNE with reduced iterations)
    pub fn fast_visualization() -> ManifoldConfig {
        ManifoldConfig::new()
            .n_components(2)
            .max_iter(250)
            .param("perplexity", 30.0)
            .param("learning_rate", 200.0)
    }

    /// Configuration for high-quality visualization (t-SNE with more iterations)
    pub fn high_quality_visualization() -> ManifoldConfig {
        ManifoldConfig::new()
            .n_components(2)
            .max_iter(1000)
            .param("perplexity", 30.0)
            .param("learning_rate", 200.0)
    }

    /// Configuration for clustering preprocessing (PCA-like)
    pub fn clustering_preprocessing() -> ManifoldConfig {
        ManifoldConfig::new()
            .n_components(50)
            .metric("euclidean".to_string())
    }

    /// Configuration for nonlinear dimensionality reduction (Isomap)
    pub fn nonlinear_reduction() -> ManifoldConfig {
        ManifoldConfig::new()
            .n_neighbors(12)
            .n_components(10)
            .metric("euclidean".to_string())
    }

    /// Configuration for local structure preservation (LLE)
    pub fn local_structure() -> ManifoldConfig {
        ManifoldConfig::new().n_neighbors(12).n_components(2)
    }

    /// Configuration for global structure preservation (MDS)
    pub fn global_structure() -> ManifoldConfig {
        ManifoldConfig::new()
            .n_components(2)
            .metric("euclidean".to_string())
            .max_iter(300)
    }
}

/// Factory trait for creating manifold learning algorithms
pub trait ManifoldFactory {
    /// The type of manifold algorithm this factory creates
    type Algorithm: ManifoldLearning;

    /// Create a new instance with default configuration
    fn default() -> Self::Algorithm;

    /// Create a new instance with custom configuration
    fn with_config(config: ManifoldConfig) -> Self::Algorithm;

    /// Create a new instance with a preset configuration
    fn with_preset(preset: fn() -> ManifoldConfig) -> Self::Algorithm {
        Self::with_config(preset())
    }
}

/// Pipeline for composing multiple manifold learning steps
/// This is a simplified version that stores step names and configurations
/// rather than trait objects for now
#[derive(Debug, Clone)]
pub struct ManifoldPipeline {
    step_configs: Vec<(String, ManifoldConfig)>,
}

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

impl ManifoldPipeline {
    /// Create a new empty pipeline
    pub fn new() -> Self {
        Self {
            step_configs: Vec::new(),
        }
    }

    /// Add a step to the pipeline
    pub fn add_step(mut self, name: impl Into<String>, config: ManifoldConfig) -> Self {
        self.step_configs.push((name.into(), config));
        self
    }

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

    /// Check if the pipeline is empty
    pub fn is_empty(&self) -> bool {
        self.step_configs.is_empty()
    }

    /// Get the names of all steps
    pub fn step_names(&self) -> Vec<&str> {
        self.step_configs
            .iter()
            .map(|(name, _)| name.as_str())
            .collect()
    }

    /// Get all step configurations
    pub fn step_configs(&self) -> &[(String, ManifoldConfig)] {
        &self.step_configs
    }
}

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

    #[test]
    fn test_manifold_config_builder() {
        let config = ManifoldConfig::new()
            .n_components(3)
            .n_neighbors(10)
            .random_state(42)
            .max_iter(500)
            .tolerance(1e-6)
            .metric("euclidean")
            .param("perplexity", 50.0);

        assert_eq!(config.n_components, 3);
        assert_eq!(config.n_neighbors, Some(10));
        assert_eq!(config.random_state, Some(42));
        assert_eq!(config.max_iter, Some(500));
        assert_eq!(config.tolerance, Some(1e-6));
        assert_eq!(config.metric, Some("euclidean".to_string()));
        assert_eq!(config.params.get("perplexity"), Some(&50.0));
    }

    #[test]
    fn test_manifold_presets() {
        let fast_config = ManifoldPresets::fast_visualization();
        assert_eq!(fast_config.n_components, 2);
        assert_eq!(fast_config.max_iter, Some(250));

        let quality_config = ManifoldPresets::high_quality_visualization();
        assert_eq!(quality_config.n_components, 2);
        assert_eq!(quality_config.max_iter, Some(1000));

        let clustering_config = ManifoldPresets::clustering_preprocessing();
        assert_eq!(clustering_config.n_components, 50);

        let nonlinear_config = ManifoldPresets::nonlinear_reduction();
        assert_eq!(nonlinear_config.n_neighbors, Some(12));
        assert_eq!(nonlinear_config.n_components, 10);

        let local_config = ManifoldPresets::local_structure();
        assert_eq!(local_config.n_neighbors, Some(12));
        assert_eq!(local_config.n_components, 2);

        let global_config = ManifoldPresets::global_structure();
        assert_eq!(global_config.n_components, 2);
        assert_eq!(global_config.max_iter, Some(300));
    }

    #[test]
    fn test_manifold_complexity() {
        let linear = ManifoldComplexity::Linear;
        assert_eq!(linear.description(), "O(n) - Linear time complexity");

        let quadratic = ManifoldComplexity::Quadratic;
        assert_eq!(quadratic.description(), "O(n²) - Quadratic time complexity");

        let custom = ManifoldComplexity::Custom("O(n^1.5)".to_string());
        assert_eq!(custom.description(), "O(n^1.5)");
    }

    #[test]
    fn test_manifold_pipeline() {
        let pipeline = ManifoldPipeline::new();
        assert!(pipeline.is_empty());
        assert_eq!(pipeline.len(), 0);

        let config1 = ManifoldConfig::new().n_components(2);
        let config2 = ManifoldConfig::new().n_components(10);

        let pipeline = pipeline.add_step("tsne", config1).add_step("pca", config2);

        assert!(!pipeline.is_empty());
        assert_eq!(pipeline.len(), 2);
        assert_eq!(pipeline.step_names(), vec!["tsne", "pca"]);
        assert_eq!(pipeline.step_configs().len(), 2);
    }
}