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
//! Visualization integration utilities for manifold learning
//!
//! This module provides utilities for exporting and preparing manifold learning
//! embeddings for visualization with common plotting libraries and tools.

use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use std::fs::File;
use std::io::Write;

/// Visualization backend type
use sklears_core::error::{Result as SklResult, SklearsError};
use std::collections::HashMap;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VisualizationBackend {
    /// matplotlib/seaborn compatible CSV format
    Matplotlib,
    /// plotly compatible JSON format
    Plotly,
    /// D3.js compatible JSON format
    D3,
    /// Generic CSV format
    CSV,
    /// Generic JSON format
    JSON,
}

/// Visualization export configuration
#[derive(Debug, Clone)]
pub struct VisualizationConfig {
    /// Backend to export for
    pub backend: VisualizationBackend,
    /// Include metadata in the export
    pub include_metadata: bool,
    /// Include original data if available
    pub include_original: bool,
    /// Additional metadata to include
    pub metadata: HashMap<String, String>,
}

impl Default for VisualizationConfig {
    fn default() -> Self {
        Self {
            backend: VisualizationBackend::CSV,
            include_metadata: true,
            include_original: false,
            metadata: HashMap::new(),
        }
    }
}

/// Visualization data structure
#[derive(Debug, Clone)]
pub struct VisualizationData {
    /// Embedded coordinates
    pub embedding: Array2<f64>,
    /// Optional labels for points
    pub labels: Option<Array1<String>>,
    /// Optional colors for points
    pub colors: Option<Array1<f64>>,
    /// Optional sizes for points  
    pub sizes: Option<Array1<f64>>,
    /// Original high-dimensional data
    pub original: Option<Array2<f64>>,
    /// Metadata about the embedding
    pub metadata: HashMap<String, String>,
}

impl VisualizationData {
    /// Create new visualization data from embedding
    pub fn new(embedding: Array2<f64>) -> Self {
        Self {
            embedding,
            labels: None,
            colors: None,
            sizes: None,
            original: None,
            metadata: HashMap::new(),
        }
    }

    /// Add labels to the visualization data
    pub fn with_labels(mut self, labels: Array1<String>) -> SklResult<Self> {
        if labels.len() != self.embedding.shape()[0] {
            return Err(SklearsError::InvalidInput(
                "Number of labels must match number of points".to_string(),
            ));
        }
        self.labels = Some(labels);
        Ok(self)
    }

    /// Add colors to the visualization data
    pub fn with_colors(mut self, colors: Array1<f64>) -> SklResult<Self> {
        if colors.len() != self.embedding.shape()[0] {
            return Err(SklearsError::InvalidInput(
                "Number of colors must match number of points".to_string(),
            ));
        }
        self.colors = Some(colors);
        Ok(self)
    }

    /// Add sizes to the visualization data
    pub fn with_sizes(mut self, sizes: Array1<f64>) -> SklResult<Self> {
        if sizes.len() != self.embedding.shape()[0] {
            return Err(SklearsError::InvalidInput(
                "Number of sizes must match number of points".to_string(),
            ));
        }
        self.sizes = Some(sizes);
        Ok(self)
    }

    /// Add original high-dimensional data
    pub fn with_original(mut self, original: Array2<f64>) -> SklResult<Self> {
        if original.shape()[0] != self.embedding.shape()[0] {
            return Err(SklearsError::InvalidInput(
                "Number of original points must match number of embedded points".to_string(),
            ));
        }
        self.original = Some(original);
        Ok(self)
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Export to file
    pub fn export(&self, path: impl AsRef<Path>, config: &VisualizationConfig) -> SklResult<()> {
        match config.backend {
            VisualizationBackend::CSV | VisualizationBackend::Matplotlib => {
                self.export_csv(path, config)
            }
            VisualizationBackend::JSON
            | VisualizationBackend::Plotly
            | VisualizationBackend::D3 => self.export_json(path, config),
        }
    }

    /// Export to CSV format
    fn export_csv(&self, path: impl AsRef<Path>, config: &VisualizationConfig) -> SklResult<()> {
        let mut file = File::create(path)
            .map_err(|e| SklearsError::InvalidInput(format!("Failed to create file: {}", e)))?;

        let n_points = self.embedding.shape()[0];
        let n_dims = self.embedding.shape()[1];

        // Write header
        let mut header = Vec::new();
        for i in 0..n_dims {
            header.push(format!("dim_{}", i));
        }

        if self.labels.is_some() {
            header.push("label".to_string());
        }
        if self.colors.is_some() {
            header.push("color".to_string());
        }
        if self.sizes.is_some() {
            header.push("size".to_string());
        }

        writeln!(file, "{}", header.join(","))
            .map_err(|e| SklearsError::InvalidInput(format!("Failed to write header: {}", e)))?;

        // Write data
        for i in 0..n_points {
            let mut row = Vec::new();

            // Add embedding coordinates
            for j in 0..n_dims {
                row.push(self.embedding[[i, j]].to_string());
            }

            // Add optional data
            if let Some(labels) = &self.labels {
                row.push(labels[i].clone());
            }
            if let Some(colors) = &self.colors {
                row.push(colors[i].to_string());
            }
            if let Some(sizes) = &self.sizes {
                row.push(sizes[i].to_string());
            }

            writeln!(file, "{}", row.join(",")).map_err(|e| {
                SklearsError::InvalidInput(format!("Failed to write row {}: {}", i, e))
            })?;
        }

        // Write metadata as comments if requested
        if config.include_metadata {
            writeln!(file, "# Metadata:").map_err(|e| {
                SklearsError::InvalidInput(format!("Failed to write metadata header: {}", e))
            })?;

            for (key, value) in &self.metadata {
                writeln!(file, "# {}: {}", key, value).map_err(|e| {
                    SklearsError::InvalidInput(format!("Failed to write metadata: {}", e))
                })?;
            }
        }

        Ok(())
    }

    /// Export to JSON format
    fn export_json(&self, path: impl AsRef<Path>, config: &VisualizationConfig) -> SklResult<()> {
        let mut file = File::create(path)
            .map_err(|e| SklearsError::InvalidInput(format!("Failed to create file: {}", e)))?;

        let n_points = self.embedding.shape()[0];
        let n_dims = self.embedding.shape()[1];

        // Create JSON structure
        let mut json = String::new();
        json.push_str("{\n");
        json.push_str("  \"data\": [\n");

        for i in 0..n_points {
            json.push_str("    {\n");

            // Add embedding coordinates
            json.push_str("      \"coordinates\": [");
            for j in 0..n_dims {
                if j > 0 {
                    json.push_str(", ");
                }
                json.push_str(&self.embedding[[i, j]].to_string());
            }
            json.push_str("],\n");

            // Add optional data
            if let Some(labels) = &self.labels {
                json.push_str(&format!("      \"label\": \"{}\",\n", labels[i]));
            }
            if let Some(colors) = &self.colors {
                json.push_str(&format!("      \"color\": {},\n", colors[i]));
            }
            if let Some(sizes) = &self.sizes {
                json.push_str(&format!("      \"size\": {},\n", sizes[i]));
            }

            // Remove trailing comma
            if json.ends_with(",\n") {
                json.truncate(json.len() - 2);
                json.push('\n');
            }

            json.push_str("    }");
            if i < n_points - 1 {
                json.push(',');
            }
            json.push('\n');
        }

        json.push_str("  ]");

        // Add metadata if requested
        if config.include_metadata && !self.metadata.is_empty() {
            json.push_str(",\n  \"metadata\": {\n");
            let mut first = true;
            for (key, value) in &self.metadata {
                if !first {
                    json.push_str(",\n");
                }
                json.push_str(&format!("    \"{}\": \"{}\"", key, value));
                first = false;
            }
            json.push_str("\n  }");
        }

        json.push_str("\n}");

        file.write_all(json.as_bytes())
            .map_err(|e| SklearsError::InvalidInput(format!("Failed to write JSON: {}", e)))?;

        Ok(())
    }
}

/// Quick visualization export for common use cases
pub struct QuickVisualization;

impl QuickVisualization {
    /// Export 2D embedding to CSV for matplotlib
    pub fn to_matplotlib_csv(
        embedding: ArrayView2<f64>,
        path: impl AsRef<Path>,
        labels: Option<Array1<String>>,
    ) -> SklResult<()> {
        let viz_data = VisualizationData::new(embedding.to_owned());
        let viz_data = if let Some(labels) = labels {
            viz_data.with_labels(labels)?
        } else {
            viz_data
        };

        let config = VisualizationConfig {
            backend: VisualizationBackend::Matplotlib,
            include_metadata: true,
            include_original: false,
            metadata: HashMap::new(),
        };

        viz_data.export(path, &config)
    }

    /// Export embedding to JSON for plotly
    pub fn to_plotly_json(
        embedding: ArrayView2<f64>,
        path: impl AsRef<Path>,
        labels: Option<Array1<String>>,
        colors: Option<Array1<f64>>,
    ) -> SklResult<()> {
        let mut viz_data = VisualizationData::new(embedding.to_owned());

        if let Some(labels) = labels {
            viz_data = viz_data.with_labels(labels)?;
        }
        if let Some(colors) = colors {
            viz_data = viz_data.with_colors(colors)?;
        }

        let config = VisualizationConfig {
            backend: VisualizationBackend::Plotly,
            include_metadata: true,
            include_original: false,
            metadata: HashMap::new(),
        };

        viz_data.export(path, &config)
    }

    /// Export embedding to JSON for D3.js
    pub fn to_d3_json(
        embedding: ArrayView2<f64>,
        path: impl AsRef<Path>,
        labels: Option<Array1<String>>,
    ) -> SklResult<()> {
        let viz_data = VisualizationData::new(embedding.to_owned());
        let viz_data = if let Some(labels) = labels {
            viz_data.with_labels(labels)?
        } else {
            viz_data
        };

        let config = VisualizationConfig {
            backend: VisualizationBackend::D3,
            include_metadata: false,
            include_original: false,
            metadata: HashMap::new(),
        };

        viz_data.export(path, &config)
    }
}

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

    #[test]
    fn test_visualization_data_creation() {
        let embedding = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let viz_data = VisualizationData::new(embedding.clone());

        assert_eq!(viz_data.embedding, embedding);
        assert!(viz_data.labels.is_none());
        assert!(viz_data.colors.is_none());
        assert!(viz_data.sizes.is_none());
    }

    #[test]
    fn test_visualization_data_with_labels() {
        let embedding = array![[1.0, 2.0], [3.0, 4.0]];
        let labels = array!["A".to_string(), "B".to_string()];

        let viz_data = VisualizationData::new(embedding)
            .with_labels(labels.clone())
            .expect("operation should succeed");

        assert_eq!(viz_data.labels.expect("operation should succeed"), labels);
    }

    #[test]
    fn test_csv_export() {
        let embedding = array![[1.0, 2.0], [3.0, 4.0]];
        let labels = array!["A".to_string(), "B".to_string()];

        let viz_data = VisualizationData::new(embedding)
            .with_labels(labels)
            .expect("operation should succeed");

        let config = VisualizationConfig::default();
        let temp_file = NamedTempFile::new().expect("operation should succeed");

        viz_data
            .export(temp_file.path(), &config)
            .expect("operation should succeed");

        let content = std::fs::read_to_string(temp_file.path()).expect("operation should succeed");
        assert!(content.contains("dim_0,dim_1,label"));
        assert!(content.contains("1,2,A"));
        assert!(content.contains("3,4,B"));
    }

    #[test]
    fn test_json_export() {
        let embedding = array![[1.0, 2.0], [3.0, 4.0]];

        let viz_data = VisualizationData::new(embedding);

        let config = VisualizationConfig {
            backend: VisualizationBackend::JSON,
            include_metadata: false,
            include_original: false,
            metadata: HashMap::new(),
        };

        let temp_file = NamedTempFile::new().expect("operation should succeed");
        viz_data
            .export(temp_file.path(), &config)
            .expect("operation should succeed");

        let content = std::fs::read_to_string(temp_file.path()).expect("operation should succeed");
        assert!(content.contains("\"coordinates\""));
        assert!(content.contains("[1, 2]"));
        assert!(content.contains("[3, 4]"));
    }

    #[test]
    fn test_quick_matplotlib_export() {
        let embedding = array![[1.0, 2.0], [3.0, 4.0]];
        let labels = array!["A".to_string(), "B".to_string()];

        let temp_file = NamedTempFile::new().expect("operation should succeed");

        QuickVisualization::to_matplotlib_csv(embedding.view(), temp_file.path(), Some(labels))
            .expect("operation should succeed");

        let content = std::fs::read_to_string(temp_file.path()).expect("operation should succeed");
        assert!(content.contains("dim_0,dim_1,label"));
    }
}