scirs2-cluster 0.3.4

Clustering algorithms module for SciRS2 (scirs2-cluster)
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
//! Advanced export functionality for clustering models
//!
//! This module provides sophisticated export capabilities including
//! multiple formats, metadata enrichment, and cross-platform compatibility.

use crate::error::{ClusteringError, Result};
use scirs2_core::ndarray::{Array1, Array2};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;

#[cfg(feature = "yaml")]
use serde_yaml;

use super::core::{EnhancedModelMetadata, PlatformInfo, SerializableModel};
use super::models::*;

/// Export formats supported by the serialization system
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExportFormat {
    /// JSON format with full metadata
    Json,
    /// Compressed JSON format
    JsonGz,
    /// Binary format (MessagePack)
    Binary,
    /// CSV format (for simple models)
    Csv,
    /// YAML format
    Yaml,
    /// XML format
    Xml,
    /// HDF5 format (for large datasets)
    Hdf5,
    /// Custom format with user-defined structure
    Custom(String),
}

/// Trait for advanced export capabilities
pub trait AdvancedExport {
    /// Export model to specified format with metadata
    fn export_with_metadata(
        &self,
        format: ExportFormat,
        metadata: Option<ModelMetadata>,
    ) -> Result<Vec<u8>>;

    /// Export to file with automatic format detection
    fn export_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>;

    /// Export model summary for quick inspection
    fn export_summary(&self) -> Result<String>;

    /// Export model in a format compatible with other libraries
    fn export_compatible(&self, target_library: &str) -> Result<Value>;

    /// Validate model before export
    fn validate_for_export(&self) -> Result<()>;
}

/// Comprehensive model metadata for exports
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ModelMetadata {
    /// Model name and version
    pub model_info: ModelInfo,
    /// Algorithm configuration
    pub algorithm_config: AlgorithmConfig,
    /// Performance metrics
    pub performance_metrics: PerformanceMetrics,
    /// Data characteristics
    pub data_characteristics: ModelDataCharacteristics,
    /// Export settings
    pub export_settings: ExportSettings,
}

/// Model information
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ModelInfo {
    /// Model name
    pub name: String,
    /// Model version
    pub version: String,
    /// Creation timestamp
    pub created_at: String,
    /// Author/creator
    pub author: Option<String>,
    /// Description
    pub description: Option<String>,
}

/// Algorithm configuration details
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AlgorithmConfig {
    /// Algorithm name
    pub algorithm: String,
    /// Hyperparameters used
    pub hyperparameters: HashMap<String, Value>,
    /// Preprocessing steps applied
    pub preprocessing: Vec<String>,
    /// Random seed used
    pub random_seed: Option<u64>,
    /// Convergence criteria
    pub convergence_criteria: Option<HashMap<String, f64>>,
}

/// Performance metrics collected during training
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PerformanceMetrics {
    /// Training time in seconds
    pub training_time_seconds: f64,
    /// Memory usage in MB
    pub peak_memory_mb: f64,
    /// CPU utilization percentage
    pub cpu_utilization: f64,
    /// Model quality metrics
    pub quality_metrics: HashMap<String, f64>,
    /// Convergence information
    pub convergence_info: Option<ConvergenceInfo>,
}

/// Convergence information
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ConvergenceInfo {
    /// Whether algorithm converged
    pub converged: bool,
    /// Number of iterations to convergence
    pub iterations: usize,
    /// Final objective value
    pub final_objective: f64,
    /// Convergence tolerance achieved
    pub tolerance_achieved: f64,
}

/// Data characteristics for model validation
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ModelDataCharacteristics {
    /// Number of samples
    pub n_samples: usize,
    /// Number of features
    pub n_features: usize,
    /// Feature names (if available)
    pub feature_names: Option<Vec<String>>,
    /// Data types for each feature
    pub feature_types: Option<Vec<String>>,
    /// Statistical summaries
    pub feature_statistics: Option<HashMap<String, FeatureStats>>,
}

/// Statistical summary for a feature
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FeatureStats {
    /// Mean value
    pub mean: f64,
    /// Standard deviation
    pub std: f64,
    /// Minimum value
    pub min: f64,
    /// Maximum value
    pub max: f64,
    /// Missing value count
    pub missing_count: usize,
}

/// Export settings and options
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExportSettings {
    /// Include raw model data
    pub include_raw_data: bool,
    /// Include training data
    pub include_training_data: bool,
    /// Compression level (0-9)
    pub compression_level: Option<u8>,
    /// Precision for floating point values
    pub float_precision: Option<usize>,
    /// Custom export options
    pub custom_options: HashMap<String, Value>,
}

impl Default for ExportSettings {
    fn default() -> Self {
        Self {
            include_raw_data: true,
            include_training_data: false,
            compression_level: None,
            float_precision: Some(6),
            custom_options: HashMap::new(),
        }
    }
}

/// Implementation of AdvancedExport for KMeansModel
impl AdvancedExport for KMeansModel {
    fn export_with_metadata(
        &self,
        format: ExportFormat,
        metadata: Option<ModelMetadata>,
    ) -> Result<Vec<u8>> {
        let export_data = KMeansExportData {
            model: self.clone(),
            metadata,
            format_version: "1.0".to_string(),
            export_timestamp: chrono::Utc::now().to_rfc3339(),
        };

        match format {
            ExportFormat::Json => {
                let json = serde_json::to_string_pretty(&export_data)
                    .map_err(|e| ClusteringError::InvalidInput(e.to_string()))?;
                Ok(json.into_bytes())
            }
            ExportFormat::JsonGz => {
                let json = serde_json::to_string(&export_data)
                    .map_err(|e| ClusteringError::InvalidInput(e.to_string()))?;

                oxiarc_deflate::gzip_compress(json.as_bytes(), 6)
                    .map_err(|e| ClusteringError::InvalidInput(e.to_string()))
            }
            #[cfg(feature = "yaml")]
            ExportFormat::Yaml => {
                let yaml = serde_yaml::to_string(&export_data)
                    .map_err(|e| ClusteringError::InvalidInput(e.to_string()))?;
                Ok(yaml.into_bytes())
            }
            #[cfg(not(feature = "yaml"))]
            ExportFormat::Yaml => Err(ClusteringError::InvalidInput(
                "YAML support not enabled. Enable the 'yaml' feature".to_string(),
            )),
            ExportFormat::Csv => self.export_csv(),
            _ => Err(ClusteringError::InvalidInput(format!(
                "Unsupported export format: {:?}",
                format
            ))),
        }
    }

    fn export_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let path = path.as_ref();
        let format = detect_format_from_extension(path)?;
        let data = self.export_with_metadata(format, None)?;

        std::fs::write(path, data)
            .map_err(|e| ClusteringError::InvalidInput(format!("Failed to write file: {}", e)))
    }

    fn export_summary(&self) -> Result<String> {
        let summary = KMeansSummary {
            algorithm: "K-Means".to_string(),
            n_clusters: self.n_clusters,
            n_features: self.centroids.ncols(),
            n_iterations: self.n_iter,
            inertia: self.inertia,
            has_labels: self.labels.is_some(),
        };

        serde_json::to_string_pretty(&summary)
            .map_err(|e| ClusteringError::InvalidInput(e.to_string()))
    }

    fn export_compatible(&self, target_library: &str) -> Result<Value> {
        match target_library.to_lowercase().as_str() {
            "sklearn" | "scikit-learn" => self.to_sklearn_format(),
            "tensorflow" | "tf" => self.to_tensorflow_format(),
            "pytorch" => self.to_pytorch_format(),
            _ => Err(ClusteringError::InvalidInput(format!(
                "Unsupported target library: {}",
                target_library
            ))),
        }
    }

    fn validate_for_export(&self) -> Result<()> {
        if self.centroids.is_empty() {
            return Err(ClusteringError::InvalidInput(
                "Cannot export model with empty centroids".to_string(),
            ));
        }

        if self.n_clusters == 0 {
            return Err(ClusteringError::InvalidInput(
                "Cannot export model with zero clusters".to_string(),
            ));
        }

        if self.centroids.nrows() != self.n_clusters {
            return Err(ClusteringError::InvalidInput(
                "Centroids shape inconsistent with n_clusters".to_string(),
            ));
        }

        Ok(())
    }
}

/// Export data structure for K-Means
#[derive(Serialize, Deserialize, Debug, Clone)]
struct KMeansExportData {
    model: KMeansModel,
    metadata: Option<ModelMetadata>,
    format_version: String,
    export_timestamp: String,
}

/// Summary structure for K-Means
#[derive(Serialize, Deserialize, Debug, Clone)]
struct KMeansSummary {
    algorithm: String,
    n_clusters: usize,
    n_features: usize,
    n_iterations: usize,
    inertia: f64,
    has_labels: bool,
}

impl KMeansModel {
    /// Export as CSV format
    fn export_csv(&self) -> Result<Vec<u8>> {
        let mut csv_content = String::new();

        // Header
        csv_content.push_str("cluster_id");
        for i in 0..self.centroids.ncols() {
            csv_content.push_str(&format!(",feature_{}", i));
        }
        csv_content.push('\n');

        // Centroids data
        for (cluster_id, centroid) in self.centroids.rows().into_iter().enumerate() {
            csv_content.push_str(&cluster_id.to_string());
            for value in centroid {
                csv_content.push_str(&format!(",{:.6}", value));
            }
            csv_content.push('\n');
        }

        Ok(csv_content.into_bytes())
    }

    /// Convert to scikit-learn compatible format
    fn to_sklearn_format(&self) -> Result<Value> {
        use serde_json::json;

        Ok(json!({
            "cluster_centers_": self.centroids.as_slice().expect("Operation failed"),
            "labels_": self.labels.as_ref().map(|l| l.as_slice().expect("Operation failed")),
            "inertia_": self.inertia,
            "n_iter_": self.n_iter,
            "n_clusters": self.n_clusters,
            "_sklearn_version": "1.0.0"
        }))
    }

    /// Convert to TensorFlow compatible format
    fn to_tensorflow_format(&self) -> Result<Value> {
        use serde_json::json;

        Ok(json!({
            "centroids": {
                "data": self.centroids.as_slice().expect("Operation failed"),
                "shape": [self.centroids.nrows(), self.centroids.ncols()],
                "dtype": "float64"
            },
            "metadata": {
                "n_clusters": self.n_clusters,
                "inertia": self.inertia,
                "iterations": self.n_iter
            }
        }))
    }

    /// Convert to PyTorch compatible format
    fn to_pytorch_format(&self) -> Result<Value> {
        use serde_json::json;

        Ok(json!({
            "state_dict": {
                "centroids": self.centroids.as_slice().expect("Operation failed")
            },
            "hyperparameters": {
                "n_clusters": self.n_clusters
            },
            "metrics": {
                "inertia": self.inertia,
                "n_iter": self.n_iter
            }
        }))
    }
}

/// Detect export format from file extension
fn detect_format_from_extension<P: AsRef<Path>>(path: P) -> Result<ExportFormat> {
    let path = path.as_ref();
    let extension = path
        .extension()
        .and_then(|ext| ext.to_str())
        .unwrap_or("")
        .to_lowercase();

    match extension.as_str() {
        "json" => Ok(ExportFormat::Json),
        "gz" | "json.gz" => Ok(ExportFormat::JsonGz),
        "yaml" | "yml" => Ok(ExportFormat::Yaml),
        "csv" => Ok(ExportFormat::Csv),
        "xml" => Ok(ExportFormat::Xml),
        "h5" | "hdf5" => Ok(ExportFormat::Hdf5),
        _ => Err(ClusteringError::InvalidInput(format!(
            "Unknown file extension: {}",
            extension
        ))),
    }
}

/// Export utility functions
pub mod utils {
    use super::*;

    /// Create default metadata for a model
    pub fn create_default_metadata(algorithm_name: &str) -> ModelMetadata {
        ModelMetadata {
            model_info: ModelInfo {
                name: format!("{}_model", algorithm_name),
                version: "1.0.0".to_string(),
                created_at: chrono::Utc::now().to_rfc3339(),
                author: None,
                description: None,
            },
            algorithm_config: AlgorithmConfig {
                algorithm: algorithm_name.to_string(),
                hyperparameters: HashMap::new(),
                preprocessing: Vec::new(),
                random_seed: None,
                convergence_criteria: None,
            },
            performance_metrics: PerformanceMetrics {
                training_time_seconds: 0.0,
                peak_memory_mb: 0.0,
                cpu_utilization: 0.0,
                quality_metrics: HashMap::new(),
                convergence_info: None,
            },
            data_characteristics: ModelDataCharacteristics {
                n_samples: 0,
                n_features: 0,
                feature_names: None,
                feature_types: None,
                feature_statistics: None,
            },
            export_settings: ExportSettings::default(),
        }
    }

    /// Compress data using gzip
    pub fn compress_data(data: &[u8]) -> Result<Vec<u8>> {
        oxiarc_deflate::gzip_compress(data, 6)
            .map_err(|e| ClusteringError::InvalidInput(e.to_string()))
    }

    /// Decompress gzip data
    pub fn decompress_data(compressed: &[u8]) -> Result<Vec<u8>> {
        oxiarc_deflate::gzip_decompress(compressed)
            .map_err(|e| ClusteringError::InvalidInput(e.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_kmeans_export_summary() {
        let centroids =
            Array2::from_shape_vec((2, 2), vec![0.0, 0.0, 1.0, 1.0]).expect("Operation failed");
        let model = KMeansModel::new(centroids, 2, 10, 0.5, None);

        let summary = model.export_summary().expect("Operation failed");
        assert!(summary.contains("K-Means"));
        assert!(summary.contains("\"n_clusters\": 2"));
    }

    #[test]
    fn test_format_detection() {
        assert_eq!(
            detect_format_from_extension("model.json").expect("Operation failed"),
            ExportFormat::Json
        );
        assert_eq!(
            detect_format_from_extension("model.yaml").expect("Operation failed"),
            ExportFormat::Yaml
        );
        assert_eq!(
            detect_format_from_extension("model.csv").expect("Operation failed"),
            ExportFormat::Csv
        );
    }

    #[test]
    fn test_sklearn_compatibility() {
        let centroids =
            Array2::from_shape_vec((2, 2), vec![0.0, 0.0, 1.0, 1.0]).expect("Operation failed");
        let model = KMeansModel::new(centroids, 2, 10, 0.5, None);

        let sklearn_format = model
            .export_compatible("sklearn")
            .expect("Operation failed");
        assert!(sklearn_format.get("cluster_centers_").is_some());
        assert!(sklearn_format.get("_sklearn_version").is_some());
    }
}