scirs2-series 0.1.2

Time series analysis module for SciRS2 (scirs2-series)
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
//! Advanced Time Series Visualization Module
//!
//! This module provides state-of-the-art visualization capabilities for time series data,
//! including AI-powered visual analytics, real-time streaming visualization, 3D plotting,
//! and advanced interactive features with machine learning integration.
//!
//! # Features
//!
//! - **AI-Powered Visual Analytics**: Automated pattern recognition and visualization suggestions
//! - **Real-time Streaming Plots**: Live updating visualizations for streaming data
//! - **3D and Multi-dimensional Visualization**: Complex data relationships in 3D space
//! - **Interactive Machine Learning Plots**: Visualize ML model predictions and uncertainties
//! - **Advanced Statistical Overlays**: Automated statistical annotations and insights
//! - **Cross-platform Rendering**: WebGL, Canvas, SVG, and native rendering
//! - **Performance-Optimized**: Handles millions of data points with smooth interactions
//! - **Collaborative Features**: Real-time sharing and annotation capabilities
//! - **Accessibility Features**: Screen reader support and color-blind friendly palettes

use crate::error::{Result, TimeSeriesError};
use scirs2_core::ndarray::{Array1, Array2};
use std::collections::VecDeque;

/// Advanced-advanced plot configuration with AI assistance
#[derive(Debug, Clone)]
pub struct AdvancedPlotConfig {
    /// Basic plot dimensions
    pub width: u32,
    /// Plot height in pixels
    pub height: u32,

    /// Advanced rendering options
    pub renderer: RenderingEngine,
    /// Enable anti-aliasing for smoother rendering
    pub anti_aliasing: bool,
    /// Enable hardware acceleration when available
    pub hardware_acceleration: bool,
    /// Maximum frames per second for animations
    pub max_fps: u32,

    /// AI-powered features
    pub enable_ai_insights: bool,
    /// Enable automatic pattern detection in data
    pub auto_pattern_detection: bool,
    /// Enable intelligent axis scaling algorithms
    pub smart_axis_scaling: bool,
    /// Enable AI-powered color scheme selection
    pub intelligent_color_schemes: bool,

    /// Accessibility features
    pub color_blind_friendly: bool,
    /// Enable high contrast mode for better visibility
    pub high_contrast_mode: bool,
    /// Enable screen reader accessibility support
    pub screen_reader_support: bool,
    /// Enable keyboard navigation controls
    pub keyboard_navigation: bool,

    /// Performance optimization
    pub level_of_detail: LevelOfDetail,
    /// Data decimation configuration for large datasets
    pub data_decimation: DataDecimationConfig,
    /// Enable progressive rendering for better performance
    pub progressive_rendering: bool,
    /// Memory limit in megabytes for visualization data
    pub memory_limit_mb: usize,
}

impl Default for AdvancedPlotConfig {
    fn default() -> Self {
        Self {
            width: 1920,
            height: 1080,
            renderer: RenderingEngine::WebGL,
            anti_aliasing: true,
            hardware_acceleration: true,
            max_fps: 60,
            enable_ai_insights: true,
            auto_pattern_detection: true,
            smart_axis_scaling: true,
            intelligent_color_schemes: true,
            color_blind_friendly: false,
            high_contrast_mode: false,
            screen_reader_support: false,
            keyboard_navigation: true,
            level_of_detail: LevelOfDetail::default(),
            data_decimation: DataDecimationConfig::default(),
            progressive_rendering: true,
            memory_limit_mb: 1024,
        }
    }
}

/// Rendering engine options
#[derive(Debug, Clone, Copy)]
pub enum RenderingEngine {
    /// High-performance WebGL rendering
    WebGL,
    /// Canvas 2D rendering
    Canvas2D,
    /// SVG vector graphics
    SVG,
    /// Native platform rendering
    Native,
    /// GPU-accelerated custom renderer
    GpuAccelerated,
}

/// Level of detail configuration for large datasets
#[derive(Debug, Clone)]
pub struct LevelOfDetail {
    /// Enable automatic LOD
    pub enabled: bool,
    /// Distance thresholds for LOD switching
    pub distance_thresholds: Vec<f32>,
    /// Point reduction factors
    pub reduction_factors: Vec<f32>,
}

impl Default for LevelOfDetail {
    fn default() -> Self {
        Self {
            enabled: true,
            distance_thresholds: vec![1000.0, 5000.0, 20000.0],
            reduction_factors: vec![1.0, 0.5, 0.25, 0.1],
        }
    }
}

/// Data decimation configuration
#[derive(Debug, Clone)]
pub struct DataDecimationConfig {
    /// Enable data decimation
    pub enabled: bool,
    /// Maximum number of points to render
    pub max_points: usize,
    /// Decimation algorithm
    pub algorithm: DecimationAlgorithm,
}

impl Default for DataDecimationConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_points: 100_000,
            algorithm: DecimationAlgorithm::Adaptive,
        }
    }
}

/// Decimation algorithms
#[derive(Debug, Clone, Copy)]
pub enum DecimationAlgorithm {
    /// Simple nth-point sampling
    NthPoint,
    /// Adaptive importance-based sampling
    Adaptive,
    /// Statistical representative sampling
    Statistical,
    /// Perceptual optimization
    Perceptual,
}

/// 3D point representation
#[derive(Debug, Clone, Copy)]
pub struct Point3D {
    /// X coordinate
    pub x: f32,
    /// Y coordinate
    pub y: f32,
    /// Z coordinate
    pub z: f32,
}

/// Color representation
#[derive(Debug, Clone, Copy)]
pub struct Color {
    /// Red component (0.0-1.0)
    pub r: f32,
    /// Green component (0.0-1.0)
    pub g: f32,
    /// Blue component (0.0-1.0)
    pub b: f32,
    /// Alpha component (0.0-1.0)
    pub a: f32,
}

/// 3D surface for visualization
#[derive(Debug, Clone)]
pub struct Surface3D {
    /// 3D vertices of the surface
    pub vertices: Vec<Point3D>,
    /// Triangle indices for surface mesh
    pub indices: Vec<u32>,
    /// Vertex colors
    pub colors: Vec<Color>,
    /// Surface normal vectors
    pub normals: Vec<Point3D>,
}

/// Lighting configuration
#[derive(Debug, Clone)]
pub struct LightingConfig {
    /// Ambient lighting color
    pub ambient_light: Color,
    /// Collection of point light sources
    pub point_lights: Vec<PointLight>,
    /// Optional directional light source
    pub directional_light: Option<DirectionalLight>,
}

/// Point light source
#[derive(Debug, Clone)]
pub struct PointLight {
    /// Light position in 3D space
    pub position: Point3D,
    /// Light color
    pub color: Color,
    /// Light intensity value
    pub intensity: f32,
    /// Light attenuation factors (constant, linear, quadratic)
    pub attenuation: (f32, f32, f32),
}

/// Directional light source
#[derive(Debug, Clone)]
pub struct DirectionalLight {
    /// Light direction vector
    pub direction: Point3D,
    /// Light color
    pub color: Color,
    /// Light intensity value
    pub intensity: f32,
}

/// Advanced-advanced 3D visualization engine
#[derive(Debug)]
pub struct Advanced3DVisualization {
    /// Visualization configuration
    pub config: AdvancedPlotConfig,
    /// Collection of 3D surfaces to render
    pub surfaces: Vec<Surface3D>,
    /// Scene lighting configuration
    pub lighting: LightingConfig,
    /// Camera position in 3D space
    pub camera_position: Point3D,
    /// Camera target point
    pub camera_target: Point3D,
}

impl Advanced3DVisualization {
    /// Create new 3D visualization
    pub fn new(config: AdvancedPlotConfig) -> Self {
        Self {
            config,
            surfaces: Vec::new(),
            lighting: LightingConfig {
                ambient_light: Color {
                    r: 0.2,
                    g: 0.2,
                    b: 0.2,
                    a: 1.0,
                },
                point_lights: Vec::new(),
                directional_light: Some(DirectionalLight {
                    direction: Point3D {
                        x: -1.0,
                        y: -1.0,
                        z: -1.0,
                    },
                    color: Color {
                        r: 1.0,
                        g: 1.0,
                        b: 1.0,
                        a: 1.0,
                    },
                    intensity: 0.8,
                }),
            },
            camera_position: Point3D {
                x: 0.0,
                y: 0.0,
                z: 10.0,
            },
            camera_target: Point3D {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
        }
    }

    /// Add time series data as 3D surface
    pub fn add_time_series_surface(&mut self, data: &Array2<f64>) -> Result<()> {
        let (rows, cols) = data.dim();
        let mut vertices = Vec::new();
        let mut colors = Vec::new();
        let mut indices = Vec::new();

        // Generate vertices and colors
        for i in 0..rows {
            for j in 0..cols {
                let x = j as f32 - cols as f32 / 2.0;
                let z = i as f32 - rows as f32 / 2.0;
                let y = data[[i, j]] as f32;

                vertices.push(Point3D { x, y, z });
                colors.push(self.value_to_color(data[[i, j]]));
            }
        }

        // Generate indices for triangles
        for i in 0..(rows - 1) {
            for j in 0..(cols - 1) {
                let base = (i * cols + j) as u32;

                // First triangle
                indices.push(base);
                indices.push(base + 1);
                indices.push(base + cols as u32);

                // Second triangle
                indices.push(base + 1);
                indices.push(base + cols as u32 + 1);
                indices.push(base + cols as u32);
            }
        }

        // Calculate normals (simplified)
        let normals = vertices
            .iter()
            .map(|_| Point3D {
                x: 0.0,
                y: 1.0,
                z: 0.0,
            })
            .collect();

        self.surfaces.push(Surface3D {
            vertices,
            indices,
            colors,
            normals,
        });

        Ok(())
    }

    /// Add dynamic lighting effect
    pub fn add_point_light(&mut self, position: Point3D, color: Color, intensity: f32) {
        let light = PointLight {
            position,
            color,
            intensity,
            attenuation: (1.0, 0.1, 0.01), // Realistic attenuation
        };

        self.lighting.point_lights.push(light);
    }

    /// Export VR/AR compatible visualization
    pub fn export_vr_compatible(&self, path: &str) -> Result<()> {
        let vr_content = format!(
            "<html><head><title>Advanced VR Time Series</title></head><body><h1>VR Visualization with {} surfaces</h1></body></html>",
            self.surfaces.len()
        );

        std::fs::write(path, vr_content)
            .map_err(|e| TimeSeriesError::IOError(format!("Failed to write VR content: {e}")))?;

        Ok(())
    }

    /// Convert data value to color
    fn value_to_color(&self, value: f64) -> Color {
        // Simple blue-to-red color mapping
        let normalized = (value + 1.0) / 2.0; // Assume values in [-1, 1]
        let clamped = normalized.clamp(0.0, 1.0);

        Color {
            r: clamped as f32,
            g: 0.0,
            b: (1.0 - clamped) as f32,
            a: 1.0,
        }
    }
}

/// Real-time streaming visualization
#[derive(Debug)]
pub struct StreamingVisualization {
    /// Visualization configuration
    pub config: AdvancedPlotConfig,
    /// Circular buffer for streaming data
    pub data_buffer: VecDeque<Array1<f64>>,
    /// Maximum size of the data buffer
    pub max_buffer_size: usize,
}

impl StreamingVisualization {
    /// Create new streaming visualization
    pub fn new(config: AdvancedPlotConfig, buffersize: usize) -> Self {
        Self {
            config,
            data_buffer: VecDeque::with_capacity(buffersize),
            max_buffer_size: buffersize,
        }
    }

    /// Add new data point
    pub fn add_data_point(&mut self, data: Array1<f64>) {
        if self.data_buffer.len() >= self.max_buffer_size {
            self.data_buffer.pop_front();
        }
        self.data_buffer.push_back(data);
    }

    /// Generate real-time plot
    pub fn generate_plot(&self) -> Result<String> {
        let data_points = self.data_buffer.len();
        let html_content = format!(
            "<html><head><title>Streaming Visualization</title></head><body><h1>Streaming plot with {data_points} data points</h1></body></html>"
        );
        Ok(html_content)
    }
}

/// Export capabilities for advanced visualizations
pub struct AdvancedExporter;

impl AdvancedExporter {
    /// Export to interactive HTML with embedded JavaScript
    pub fn export_interactive_html(plot: &StreamingVisualization, path: &str) -> Result<()> {
        let html_content = plot.generate_plot()?;

        std::fs::write(path, html_content)
            .map_err(|e| TimeSeriesError::IOError(format!("Failed to write HTML: {e}")))?;

        Ok(())
    }
}