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
//! Embedding callbacks for monitoring and customizing manifold learning
//!
//! This module provides a callback system that allows users to hook into the
//! training process of manifold learning algorithms to monitor progress,
//! save intermediate results, implement early stopping, or customize behavior.

use scirs2_core::ndarray::Array2;
use sklears_core::{error::Result as SklResult, types::Float};
use std::collections::HashMap;

/// Callback event that occurs during manifold learning
#[derive(Debug, Clone, PartialEq)]
pub enum CallbackEvent {
    /// Training started
    TrainingStarted,
    /// Iteration completed
    IterationCompleted { iteration: usize, loss: Float },
    /// Epoch completed (for algorithms with epochs)
    EpochCompleted { epoch: usize, loss: Float },
    /// Early stopping triggered
    EarlyStopping { reason: String },
    /// Training completed
    TrainingCompleted { final_loss: Float },
    /// Custom event with arbitrary data
    Custom {
        name: String,
        data: HashMap<String, Float>,
    },
}

/// Context information available to callbacks during training
#[derive(Debug, Clone)]
pub struct CallbackContext {
    /// Current iteration number
    pub iteration: usize,
    /// Current loss value
    pub loss: Float,
    /// Current embedding state
    pub embedding: Array2<Float>,
    /// Gradient norms (if available)
    pub gradient_norm: Option<Float>,
    /// Learning rate (if applicable)
    pub learning_rate: Option<Float>,
    /// Additional metrics
    pub metrics: HashMap<String, Float>,
}

/// Trait for implementing custom embedding callbacks
pub trait EmbeddingCallback: Send + Sync + std::fmt::Debug {
    /// Called when an event occurs during training
    fn on_event(&mut self, event: &CallbackEvent, context: &CallbackContext) -> SklResult<()>;

    /// Called to check if training should stop early
    fn should_stop(&self) -> bool {
        false
    }

    /// Get callback name for identification
    fn name(&self) -> &str;
}

/// Progress monitoring callback that prints training progress
#[derive(Debug, Clone)]
pub struct ProgressCallback {
    print_every: usize,
    last_printed: usize,
}

impl ProgressCallback {
    /// Create a new progress callback
    pub fn new(print_every: usize) -> Self {
        Self {
            print_every,
            last_printed: 0,
        }
    }
}

impl EmbeddingCallback for ProgressCallback {
    fn on_event(&mut self, event: &CallbackEvent, _context: &CallbackContext) -> SklResult<()> {
        match event {
            CallbackEvent::TrainingStarted => {
                println!("Training started...");
            }
            CallbackEvent::IterationCompleted { iteration, loss }
                if iteration - self.last_printed >= self.print_every =>
            {
                println!("Iteration {}: loss = {:.6}", iteration, loss);
                self.last_printed = *iteration;
            }
            CallbackEvent::TrainingCompleted { final_loss } => {
                println!("Training completed with final loss: {:.6}", final_loss);
            }
            _ => {}
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "ProgressCallback"
    }
}

/// Early stopping callback based on loss improvement
#[derive(Debug, Clone)]
pub struct EarlyStoppingCallback {
    patience: usize,
    min_delta: Float,
    best_loss: Float,
    wait: usize,
    stopped: bool,
}

impl EarlyStoppingCallback {
    /// Create a new early stopping callback
    pub fn new(patience: usize, min_delta: Float) -> Self {
        Self {
            patience,
            min_delta,
            best_loss: Float::INFINITY,
            wait: 0,
            stopped: false,
        }
    }
}

impl EmbeddingCallback for EarlyStoppingCallback {
    fn on_event(&mut self, event: &CallbackEvent, _context: &CallbackContext) -> SklResult<()> {
        if let CallbackEvent::IterationCompleted { loss, .. } = event {
            if *loss < self.best_loss - self.min_delta {
                self.best_loss = *loss;
                self.wait = 0;
            } else {
                self.wait += 1;
                if self.wait >= self.patience {
                    self.stopped = true;
                    println!(
                        "Early stopping triggered after {} iterations without improvement",
                        self.wait
                    );
                }
            }
        }
        Ok(())
    }

    fn should_stop(&self) -> bool {
        self.stopped
    }

    fn name(&self) -> &str {
        "EarlyStoppingCallback"
    }
}

/// Callback for saving intermediate embeddings
#[derive(Debug, Clone)]
pub struct SaveEmbeddingCallback {
    save_every: usize,
    save_path: String,
    last_saved: usize,
}

impl SaveEmbeddingCallback {
    /// Create a new save embedding callback
    pub fn new(save_every: usize, save_path: String) -> Self {
        Self {
            save_every,
            save_path,
            last_saved: 0,
        }
    }
}

impl EmbeddingCallback for SaveEmbeddingCallback {
    fn on_event(&mut self, event: &CallbackEvent, _context: &CallbackContext) -> SklResult<()> {
        if let CallbackEvent::IterationCompleted { iteration, .. } = event {
            if iteration - self.last_saved >= self.save_every {
                // Here you would implement actual saving logic
                // For now, just print a message
                println!(
                    "Would save embedding at iteration {} to {}",
                    iteration, self.save_path
                );
                self.last_saved = *iteration;
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "SaveEmbeddingCallback"
    }
}

/// Callback for collecting training metrics
#[derive(Debug, Clone)]
pub struct MetricsCallback {
    history: Vec<(usize, Float)>,
    custom_metrics: HashMap<String, Vec<Float>>,
}

impl MetricsCallback {
    /// Create a new metrics callback
    pub fn new() -> Self {
        Self {
            history: Vec::new(),
            custom_metrics: HashMap::new(),
        }
    }

    /// Get the loss history
    pub fn loss_history(&self) -> &[(usize, Float)] {
        &self.history
    }

    /// Get custom metrics history
    pub fn custom_metrics(&self) -> &HashMap<String, Vec<Float>> {
        &self.custom_metrics
    }
}

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

impl EmbeddingCallback for MetricsCallback {
    fn on_event(&mut self, event: &CallbackEvent, context: &CallbackContext) -> SklResult<()> {
        if let CallbackEvent::IterationCompleted { iteration, loss } = event {
            self.history.push((*iteration, *loss));

            // Collect custom metrics
            for (name, value) in &context.metrics {
                self.custom_metrics
                    .entry(name.clone())
                    .or_default()
                    .push(*value);
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "MetricsCallback"
    }
}

/// Manager for handling multiple callbacks
#[derive(Debug, Default)]
pub struct CallbackManager {
    callbacks: Vec<Box<dyn EmbeddingCallback>>,
}

impl CallbackManager {
    /// Create a new callback manager
    pub fn new() -> Self {
        Self {
            callbacks: Vec::new(),
        }
    }

    /// Add a callback to the manager
    pub fn add_callback(&mut self, callback: Box<dyn EmbeddingCallback>) {
        self.callbacks.push(callback);
    }

    /// Trigger an event for all callbacks
    pub fn trigger_event(
        &mut self,
        event: &CallbackEvent,
        context: &CallbackContext,
    ) -> SklResult<()> {
        for callback in &mut self.callbacks {
            callback.on_event(event, context)?;
        }
        Ok(())
    }

    /// Check if any callback requests early stopping
    pub fn should_stop(&self) -> bool {
        self.callbacks.iter().any(|callback| callback.should_stop())
    }

    /// Get names of all registered callbacks
    pub fn callback_names(&self) -> Vec<&str> {
        self.callbacks.iter().map(|c| c.name()).collect()
    }
}

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

    #[test]
    fn test_progress_callback() {
        let mut callback = ProgressCallback::new(10);
        let context = CallbackContext {
            iteration: 5,
            loss: 1.0,
            embedding: Array2::zeros((10, 2)),
            gradient_norm: Some(0.1),
            learning_rate: Some(0.01),
            metrics: HashMap::new(),
        };

        let event = CallbackEvent::IterationCompleted {
            iteration: 5,
            loss: 1.0,
        };
        assert!(callback.on_event(&event, &context).is_ok());
        assert!(!callback.should_stop());
        assert_eq!(callback.name(), "ProgressCallback");
    }

    #[test]
    fn test_early_stopping_callback() {
        let mut callback = EarlyStoppingCallback::new(3, 0.01);
        let context = CallbackContext {
            iteration: 1,
            loss: 1.0,
            embedding: Array2::zeros((10, 2)),
            gradient_norm: None,
            learning_rate: None,
            metrics: HashMap::new(),
        };

        // First iteration - should not stop
        let event = CallbackEvent::IterationCompleted {
            iteration: 1,
            loss: 1.0,
        };
        assert!(callback.on_event(&event, &context).is_ok());
        assert!(!callback.should_stop());

        // Multiple iterations without improvement
        for i in 2..=5 {
            let event = CallbackEvent::IterationCompleted {
                iteration: i,
                loss: 1.0,
            };
            assert!(callback.on_event(&event, &context).is_ok());
        }

        // Should trigger early stopping after patience iterations
        assert!(callback.should_stop());
    }

    #[test]
    fn test_metrics_callback() {
        let mut callback = MetricsCallback::new();
        let mut metrics = HashMap::new();
        metrics.insert("custom_metric".to_string(), 0.5);

        let context = CallbackContext {
            iteration: 1,
            loss: 1.0,
            embedding: Array2::zeros((10, 2)),
            gradient_norm: None,
            learning_rate: None,
            metrics,
        };

        let event = CallbackEvent::IterationCompleted {
            iteration: 1,
            loss: 1.0,
        };
        assert!(callback.on_event(&event, &context).is_ok());

        let history = callback.loss_history();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0], (1, 1.0));

        let custom_metrics = callback.custom_metrics();
        assert!(custom_metrics.contains_key("custom_metric"));
        assert_eq!(custom_metrics["custom_metric"][0], 0.5);
    }

    #[test]
    fn test_callback_manager() {
        let mut manager = CallbackManager::new();

        manager.add_callback(Box::new(ProgressCallback::new(10)));
        manager.add_callback(Box::new(EarlyStoppingCallback::new(5, 0.01)));

        let context = CallbackContext {
            iteration: 1,
            loss: 1.0,
            embedding: Array2::zeros((10, 2)),
            gradient_norm: None,
            learning_rate: None,
            metrics: HashMap::new(),
        };

        let event = CallbackEvent::IterationCompleted {
            iteration: 1,
            loss: 1.0,
        };
        assert!(manager.trigger_event(&event, &context).is_ok());
        assert!(!manager.should_stop());

        let names = manager.callback_names();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"ProgressCallback"));
        assert!(names.contains(&"EarlyStoppingCallback"));
    }
}