scirs2-cluster 0.5.0

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
//! Data types for native plotting — structs, enums, and their Default impls.

use scirs2_core::ndarray::Array2;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration for native plotting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NativePlotConfig {
    /// Canvas width in pixels
    pub width: usize,
    /// Canvas height in pixels
    pub height: usize,
    /// Enable interactive features
    pub enable_interactivity: bool,
    /// Enable animations
    pub enable_animations: bool,
    /// Animation frame rate (FPS)
    pub animation_fps: f64,
    /// Color scheme
    pub color_scheme: PlotColorScheme,
    /// Export quality
    pub export_quality: ExportQuality,
}

/// Color schemes for native plotting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PlotColorScheme {
    /// Quantum theme (blue-cyan-purple)
    Quantum,
    /// Neuromorphic theme (green-yellow-red)
    Neuromorphic,
    /// AI theme (gold-orange-red)
    AI,
    /// Scientific theme (grayscale with highlights)
    Scientific,
    /// Custom color palette
    Custom(Vec<[u8; 3]>),
}

/// Export quality settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExportQuality {
    /// Draft quality (fast rendering)
    Draft,
    /// Standard quality
    Standard,
    /// High quality (detailed rendering)
    High,
    /// Publication quality (maximum detail)
    Publication,
}

/// SVG canvas for native rendering
#[derive(Debug)]
pub struct SvgCanvas {
    /// Canvas dimensions
    pub(crate) width: usize,
    pub(crate) height: usize,
    /// SVG elements
    pub(crate) elements: Vec<SvgElement>,
    /// Style definitions
    pub(crate) styles: HashMap<String, String>,
}

/// SVG element types
#[derive(Debug, Clone)]
pub enum SvgElement {
    /// Circle element
    Circle {
        cx: f64,
        cy: f64,
        r: f64,
        fill: String,
        stroke: String,
        stroke_width: f64,
        opacity: f64,
    },
    /// Line element
    Line {
        x1: f64,
        y1: f64,
        x2: f64,
        y2: f64,
        stroke: String,
        stroke_width: f64,
        opacity: f64,
    },
    /// Path element (for complex shapes)
    Path {
        d: String,
        fill: String,
        stroke: String,
        stroke_width: f64,
        opacity: f64,
    },
    /// Text element
    Text {
        x: f64,
        y: f64,
        content: String,
        font_size: f64,
        fill: String,
        text_anchor: String,
    },
    /// Group element (for hierarchical organization)
    Group {
        id: String,
        elements: Vec<SvgElement>,
        transform: String,
    },
}

/// Animation engine for dynamic visualizations
#[derive(Debug)]
pub struct AnimationEngine {
    /// Animation frames
    pub(crate) frames: Vec<AnimationFrame>,
    /// Current frame index
    pub(crate) current_frame: usize,
    /// Frame duration in milliseconds
    pub(crate) frame_duration: f64,
    /// Total animation duration
    pub(crate) total_duration: f64,
}

/// Animation frame data
#[derive(Debug, Clone)]
pub struct AnimationFrame {
    /// Frame timestamp
    pub timestamp: f64,
    /// Frame elements
    pub elements: Vec<SvgElement>,
    /// Frame-specific transformations
    pub transformations: Vec<Transformation>,
}

/// Animation transformations
#[derive(Debug, Clone)]
pub enum Transformation {
    /// Translation
    Translate { dx: f64, dy: f64 },
    /// Rotation
    Rotate { angle: f64, cx: f64, cy: f64 },
    /// Scale
    Scale { sx: f64, sy: f64 },
    /// Opacity fade
    Fade { from: f64, to: f64 },
    /// Color transition
    ColorTransition { from: String, to: String },
}

/// Interactive controller for user interaction
#[derive(Debug)]
pub struct InteractiveController {
    /// Zoom level
    pub(crate) zoom_level: f64,
    /// Pan offset
    pub(crate) pan_offset: (f64, f64),
    /// Selected elements
    pub(crate) selected_elements: Vec<String>,
    /// Hover state
    pub(crate) hover_element: Option<String>,
}

/// Native dendrogram plot
#[derive(Debug, Serialize, Deserialize)]
pub struct NativeDendrogramPlot {
    /// Dendrogram tree structure
    pub tree: DendrogramTree,
    /// Node positions
    pub node_positions: HashMap<String, (f64, f64)>,
    /// Branch lengths
    pub branch_lengths: HashMap<String, f64>,
    /// Quantum enhancement data
    pub quantum_enhancements: HashMap<String, f64>,
    /// Interactive features
    pub interactive_features: Vec<InteractiveFeature>,
}

/// Dendrogram tree structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DendrogramTree {
    /// Root node
    pub root: DendrogramNode,
    /// Total height
    pub height: f64,
    /// Leaf count
    pub leaf_count: usize,
}

/// Dendrogram node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DendrogramNode {
    /// Node ID
    pub id: String,
    /// Node height
    pub height: f64,
    /// Child nodes
    pub children: Vec<DendrogramNode>,
    /// Data point indices (for leaf nodes)
    pub data_indices: Vec<usize>,
    /// Quantum coherence at this node
    pub quantum_coherence: f64,
    /// Neuromorphic activity
    pub neuromorphic_activity: f64,
}

/// Interactive features for plots
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InteractiveFeature {
    /// Zoom and pan
    ZoomPan,
    /// Node selection
    NodeSelection,
    /// Tooltip on hover
    Tooltip,
    /// Real-time filtering
    RealTimeFilter,
    /// Animation controls
    AnimationControls,
    /// Export options
    ExportOptions,
}

/// 3D cluster plot for high-dimensional visualization
#[derive(Debug, Serialize, Deserialize)]
pub struct Native3DClusterPlot {
    /// 3D data points
    pub points_3d: Array2<f64>,
    /// Point colors based on clustering
    pub point_colors: Vec<[u8; 3]>,
    /// 3D centroids
    pub centroids_3d: Array2<f64>,
    /// Camera position
    pub camera: Camera3D,
    /// Lighting setup
    pub lighting: Lighting3D,
    /// Quantum field visualization
    pub quantum_field: QuantumField3D,
}

/// 3D camera configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Camera3D {
    /// Camera position
    pub position: [f64; 3],
    /// Look-at target
    pub target: [f64; 3],
    /// Up vector
    pub up: [f64; 3],
    /// Field of view
    pub fov: f64,
    /// Near and far clipping planes
    pub near: f64,
    pub far: f64,
}

/// 3D lighting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lighting3D {
    /// Ambient light intensity
    pub ambient: f64,
    /// Directional lights
    pub directional_lights: Vec<DirectionalLight>,
    /// Point lights
    pub point_lights: Vec<PointLight>,
}

/// Directional light
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirectionalLight {
    /// Light direction
    pub direction: [f64; 3],
    /// Light intensity
    pub intensity: f64,
    /// Light color
    pub color: [f64; 3],
}

/// Point light
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PointLight {
    /// Light position
    pub position: [f64; 3],
    /// Light intensity
    pub intensity: f64,
    /// Light color
    pub color: [f64; 3],
    /// Attenuation
    pub attenuation: f64,
}

/// 3D quantum field visualization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumField3D {
    /// Field strength at grid points
    pub field_strength: Array2<f64>,
    /// Field coherence
    pub coherence: Array2<f64>,
    /// Phase information
    pub phase: Array2<f64>,
    /// Entanglement connections
    pub entanglement_lines: Vec<([f64; 3], [f64; 3], f64)>,
}

// Supporting output data structures

/// Native cluster plot output
#[derive(Debug)]
pub struct NativeClusterPlot {
    /// Plot data
    pub data: Array2<f64>,
    /// Point elements
    pub point_elements: Vec<SvgElement>,
    /// Centroid elements
    pub centroid_elements: Vec<SvgElement>,
    /// Quantum enhancements per point
    pub quantum_enhancements: Vec<f64>,
    /// Plot bounds
    pub bounds: (f64, f64, f64, f64),
    /// Scale factors
    pub scale: (f64, f64),
}

/// Complete native visualization output
#[derive(Debug)]
pub struct NativeVisualizationOutput {
    /// Main cluster plot
    pub cluster_plot: NativeClusterPlot,
    /// Dendrogram (if applicable)
    pub dendrogram: Option<NativeDendrogramPlot>,
    /// 3D plot (if applicable)
    pub plot_3d: Option<Native3DClusterPlot>,
    /// Quantum coherence animation
    pub quantum_animation: Option<QuantumCoherenceAnimation>,
    /// Neuromorphic activity plot
    pub neuromorphic_plot: NeuromorphicActivityPlot,
    /// Interactive performance dashboard
    pub performance_dashboard: InteractivePerformanceDashboard,
    /// SVG content as string
    pub svg_content: String,
    /// Interactive JavaScript
    pub interactive_script: String,
}

/// Quantum coherence animation data
#[derive(Debug)]
pub struct QuantumCoherenceAnimation {
    /// Animation frames
    pub frames: Vec<QuantumCoherenceFrame>,
    /// Total duration in seconds
    pub duration: f64,
    /// Frames per second
    pub fps: f64,
}

/// Single quantum coherence frame
#[derive(Debug, Clone)]
pub struct QuantumCoherenceFrame {
    /// Frame timestamp
    pub timestamp: f64,
    /// Coherence visualization elements
    pub elements: Vec<SvgElement>,
    /// Quantum field strength
    pub field_strength: Array2<f64>,
}

/// Neuromorphic activity plot
#[derive(Debug)]
pub struct NeuromorphicActivityPlot {
    /// Activity matrix (time x neurons)
    pub activity_matrix: Array2<f64>,
    /// Spike trains
    pub spike_trains: Array2<f64>,
    /// Plasticity changes
    pub plasticity_changes: Array2<f64>,
    /// Time resolution
    pub time_resolution: f64,
}

/// Interactive performance dashboard
#[derive(Debug)]
pub struct InteractivePerformanceDashboard {
    /// Performance metrics
    pub performance_metrics: HashMap<String, f64>,
    /// Improvement factors
    pub improvements: HashMap<String, f64>,
    /// Metrics timeline
    pub metrics_timeline: Vec<MetricTimelinePoint>,
    /// Execution summary
    pub execution_summary: ExecutionSummary,
}

/// Timeline point for metrics
#[derive(Debug, Clone)]
pub struct MetricTimelinePoint {
    /// Timestamp
    pub timestamp: f64,
    /// Quantum coherence at this time
    pub quantum_coherence: f64,
    /// Neural adaptation rate
    pub neural_adaptation: f64,
    /// AI confidence
    pub ai_confidence: f64,
}

/// Execution summary
#[derive(Debug, Clone)]
pub struct ExecutionSummary {
    /// Total execution time
    pub total_time: f64,
    /// Memory usage
    pub memory_usage: f64,
    /// Number of iterations
    pub iterations: usize,
    /// Selected algorithm
    pub algorithm: String,
    /// Final confidence
    pub confidence: f64,
}

impl Default for NativePlotConfig {
    fn default() -> Self {
        Self {
            width: 1200,
            height: 800,
            enable_interactivity: true,
            enable_animations: true,
            animation_fps: 30.0,
            color_scheme: PlotColorScheme::Quantum,
            export_quality: ExportQuality::High,
        }
    }
}