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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
//! IoT sensor data analysis for time series
//!
//! This module provides specialized functionality for analyzing time series data
//! from Internet of Things (IoT) sensors including temperature, humidity, motion,
//! GPS, accelerometer, and other sensor data streams.

use crate::error::{Result, TimeSeriesError};
use scirs2_core::ndarray::{Array1, Array2, ArrayStatCompat};
use scirs2_core::validation::check_positive;
use statrs::statistics::Statistics;
use std::collections::HashMap;

/// IoT sensor data types
#[derive(Debug, Clone)]
pub enum SensorType {
    /// Temperature sensor (°C)
    Temperature,
    /// Humidity sensor (%)
    Humidity,
    /// Pressure sensor (hPa)
    Pressure,
    /// Light intensity sensor (lux)
    Light,
    /// Motion/PIR sensor (binary)
    Motion,
    /// Accelerometer (3-axis: x, y, z in m/s²)
    Accelerometer,
    /// GPS coordinates (latitude, longitude)
    GPS,
    /// Sound level sensor (dB)
    Sound,
    /// Air quality sensor (AQI or ppm)
    AirQuality,
    /// Energy consumption (W or kWh)
    Energy,
}

/// Sensor data quality assessment
#[derive(Debug, Clone)]
pub struct DataQuality {
    /// Missing data percentage
    pub missing_percentage: f64,
    /// Outlier percentage
    pub outlier_percentage: f64,
    /// Signal-to-noise ratio
    pub snr: f64,
    /// Data consistency score
    pub consistency_score: f64,
}

/// Environmental sensor data analysis
pub struct EnvironmentalSensorAnalysis {
    /// Temperature readings
    pub temperature: Option<Array1<f64>>,
    /// Humidity readings
    pub humidity: Option<Array1<f64>>,
    /// Pressure readings
    pub pressure: Option<Array1<f64>>,
    /// Light readings
    pub light: Option<Array1<f64>>,
    /// Time stamps
    pub timestamps: Array1<i64>,
    /// Sampling interval in seconds
    pub sampling_interval: f64,
}

impl EnvironmentalSensorAnalysis {
    /// Create new environmental sensor analysis
    pub fn new(_timestamps: Array1<i64>, samplinginterval: f64) -> Result<Self> {
        check_positive(samplinginterval, "sampling_interval")?;

        Ok(Self {
            temperature: None,
            humidity: None,
            pressure: None,
            light: None,
            timestamps: _timestamps,
            sampling_interval: samplinginterval,
        })
    }

    /// Add temperature data
    pub fn with_temperature(mut self, data: Array1<f64>) -> Result<Self> {
        if data.iter().any(|x| !x.is_finite()) {
            return Err(TimeSeriesError::InvalidInput(
                "Temperature data contains non-finite values".to_string(),
            ));
        }
        if data.len() != self.timestamps.len() {
            return Err(TimeSeriesError::InvalidInput(
                "Temperature data length must match timestamps".to_string(),
            ));
        }
        self.temperature = Some(data);
        Ok(self)
    }

    /// Add humidity data
    pub fn with_humidity(mut self, data: Array1<f64>) -> Result<Self> {
        if data.iter().any(|x| !x.is_finite()) {
            return Err(TimeSeriesError::InvalidInput(
                "Humidity data contains non-finite values".to_string(),
            ));
        }

        // Validate humidity range (0-100%)
        if data.iter().any(|&x| !(0.0..=100.0).contains(&x)) {
            return Err(TimeSeriesError::InvalidInput(
                "Humidity values must be between 0 and 100%".to_string(),
            ));
        }

        self.humidity = Some(data);
        Ok(self)
    }

    /// Detect sensor malfunctions using multiple criteria
    pub fn detect_sensor_malfunctions(&self) -> Result<HashMap<String, Vec<usize>>> {
        let mut malfunctions = HashMap::new();

        // Temperature sensor malfunction detection
        if let Some(ref temp_data) = self.temperature {
            let mut temp_issues = Vec::new();

            // Stuck sensor (same value for extended period)
            let mut consecutive_count = 1;
            for i in 1..temp_data.len() {
                if (temp_data[i] - temp_data[i - 1]).abs() < 0.01 {
                    consecutive_count += 1;
                } else {
                    if consecutive_count > 20 {
                        // 20 consecutive identical readings
                        for j in (i - consecutive_count)..i {
                            temp_issues.push(j);
                        }
                    }
                    consecutive_count = 1;
                }
            }

            // Impossible values (temperature outside reasonable range)
            for (i, &temp) in temp_data.iter().enumerate() {
                if !(-50.0..=100.0).contains(&temp) {
                    temp_issues.push(i);
                }
            }

            // Sudden jumps (> 10°C change in one reading)
            for i in 1..temp_data.len() {
                if (temp_data[i] - temp_data[i - 1]).abs() > 10.0 {
                    temp_issues.push(i);
                }
            }

            malfunctions.insert("Temperature".to_string(), temp_issues);
        }

        // Humidity sensor malfunction detection
        if let Some(ref humidity_data) = self.humidity {
            let mut humidity_issues = Vec::new();

            // Stuck at 0% or 100%
            for (i, &humidity) in humidity_data.iter().enumerate() {
                if humidity == 0.0 || humidity == 100.0 {
                    humidity_issues.push(i);
                }
            }

            // Sudden changes > 20%
            for i in 1..humidity_data.len() {
                if (humidity_data[i] - humidity_data[i - 1]).abs() > 20.0 {
                    humidity_issues.push(i);
                }
            }

            malfunctions.insert("Humidity".to_string(), humidity_issues);
        }

        Ok(malfunctions)
    }

    /// Calculate comfort index from temperature and humidity
    pub fn comfort_index(&self) -> Result<Array1<f64>> {
        let temp_data = self.temperature.as_ref().ok_or_else(|| {
            TimeSeriesError::InvalidInput("Temperature data required".to_string())
        })?;
        let humidity_data = self
            .humidity
            .as_ref()
            .ok_or_else(|| TimeSeriesError::InvalidInput("Humidity data required".to_string()))?;

        let mut comfort = Array1::zeros(temp_data.len());

        for i in 0..comfort.len() {
            let temp = temp_data[i];
            let rh = humidity_data[i];

            // Heat Index calculation (simplified)
            let heat_index = if temp >= 27.0 && rh >= 40.0 {
                -42.379 + 2.04901523 * temp + 10.14333127 * rh
                    - 0.22475541 * temp * rh
                    - 0.00683783 * temp * temp
                    - 0.05481717 * rh * rh
                    + 0.00122874 * temp * temp * rh
                    + 0.00085282 * temp * rh * rh
                    - 0.00000199 * temp * temp * rh * rh
            } else {
                temp
            };

            // Comfort score (0-100, higher is more comfortable)
            comfort[i] = if heat_index <= 27.0 && (30.0..=60.0).contains(&rh) {
                100.0 - (heat_index - 22.0).abs() * 5.0 - (rh - 45.0).abs() * 0.5
            } else {
                50.0 - (heat_index - 22.0).abs() * 2.0 - (rh - 45.0).abs() * 0.3
            }
            .clamp(0.0, 100.0);
        }

        Ok(comfort)
    }

    /// Energy optimization recommendations based on environmental data
    pub fn energy_optimization_recommendations(&self) -> Result<Vec<String>> {
        let mut recommendations = Vec::new();

        if let Some(ref temp_data) = self.temperature {
            let avg_temp = temp_data.mean_or(0.0);

            if avg_temp > 25.0 {
                recommendations
                    .push("Consider increasing cooling setpoint during peak hours".to_string());
            } else if avg_temp < 18.0 {
                recommendations
                    .push("Consider decreasing heating setpoint during off-peak hours".to_string());
            }

            // Temperature stability analysis
            let temp_std = temp_data.std(0.0);
            if temp_std > 3.0 {
                recommendations.push(
                    "High temperature variation detected - check HVAC efficiency".to_string(),
                );
            }
        }

        if let Some(ref humidity_data) = self.humidity {
            let avg_humidity = humidity_data.mean_or(0.0);

            if avg_humidity > 70.0 {
                recommendations
                    .push("High humidity detected - consider dehumidification".to_string());
            } else if avg_humidity < 30.0 {
                recommendations.push("Low humidity detected - consider humidification".to_string());
            }
        }

        Ok(recommendations)
    }
}

/// Motion and acceleration sensor analysis
pub struct MotionSensorAnalysis {
    /// Accelerometer data (3-axis)
    pub acceleration: Option<Array2<f64>>,
    /// Motion detection data (binary)
    pub motion: Option<Array1<f64>>,
    /// GPS coordinates [latitude, longitude]
    pub gps: Option<Array2<f64>>,
    /// Time stamps
    pub timestamps: Array1<i64>,
    /// Sampling frequency (Hz)
    pub fs: f64,
}

impl MotionSensorAnalysis {
    /// Create new motion sensor analysis
    pub fn new(timestamps: Array1<i64>, fs: f64) -> Result<Self> {
        check_positive(fs, "sampling_frequency")?;

        Ok(Self {
            acceleration: None,
            motion: None,
            gps: None,
            timestamps,
            fs,
        })
    }

    /// Add accelerometer data (3-axis: x, y, z)
    pub fn with_accelerometer(mut self, data: Array2<f64>) -> Result<Self> {
        if data.iter().any(|x| !x.is_finite()) {
            return Err(TimeSeriesError::InvalidInput(
                "Acceleration data contains non-finite values".to_string(),
            ));
        }

        if data.ncols() != 3 {
            return Err(TimeSeriesError::InvalidInput(
                "Accelerometer data must have 3 columns (x, y, z)".to_string(),
            ));
        }

        self.acceleration = Some(data);
        Ok(self)
    }

    /// Add GPS data
    pub fn with_gps(mut self, data: Array2<f64>) -> Result<Self> {
        if data.iter().any(|x| !x.is_finite()) {
            return Err(TimeSeriesError::InvalidInput(
                "GPS data contains non-finite values".to_string(),
            ));
        }

        if data.ncols() != 2 {
            return Err(TimeSeriesError::InvalidInput(
                "GPS data must have 2 columns (latitude, longitude)".to_string(),
            ));
        }

        // Validate GPS coordinates
        for row in data.outer_iter() {
            let lat = row[0];
            let lon = row[1];
            if !(-90.0..=90.0).contains(&lat) || !(-180.0..=180.0).contains(&lon) {
                return Err(TimeSeriesError::InvalidInput(
                    "Invalid GPS coordinates".to_string(),
                ));
            }
        }

        self.gps = Some(data);
        Ok(self)
    }

    /// Detect different activity types from accelerometer data
    pub fn activity_recognition(&self) -> Result<Vec<(usize, String)>> {
        let accel_data = self.acceleration.as_ref().ok_or_else(|| {
            TimeSeriesError::InvalidInput("Accelerometer data required".to_string())
        })?;

        let window_size = (2.0 * self.fs) as usize; // 2-second windows
        let mut activities = Vec::new();

        for start in (0..accel_data.nrows()).step_by(window_size) {
            let end = (start + window_size).min(accel_data.nrows());
            if end - start < window_size / 2 {
                break; // Skip incomplete windows
            }

            let window = accel_data.slice(scirs2_core::ndarray::s![start..end, ..]);

            // Calculate features
            let magnitude: Array1<f64> = window
                .outer_iter()
                .map(|row| (row[0] * row[0] + row[1] * row[1] + row[2] * row[2]).sqrt())
                .collect();

            let mean_magnitude = magnitude.clone().mean();
            let std_magnitude = magnitude.std(0.0);
            let max_magnitude = magnitude.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

            // Simple activity classification based on acceleration patterns
            let activity = if std_magnitude > 3.0 && max_magnitude > 15.0 {
                "Running"
            } else if std_magnitude > 1.5 && mean_magnitude > 10.0 {
                "Walking"
            } else if std_magnitude < 0.5 && mean_magnitude < 10.5 {
                "Stationary"
            } else if max_magnitude > 20.0 {
                "High Impact Activity"
            } else {
                "Light Activity"
            };

            activities.push((start, activity.to_string()));
        }

        Ok(activities)
    }

    /// Calculate distance traveled from GPS data
    pub fn distance_traveled(&self) -> Result<f64> {
        let gps_data = self
            .gps
            .as_ref()
            .ok_or_else(|| TimeSeriesError::InvalidInput("GPS data required".to_string()))?;

        if gps_data.nrows() < 2 {
            return Ok(0.0);
        }

        let mut total_distance = 0.0;

        for i in 1..gps_data.nrows() {
            let lat1 = gps_data[[i - 1, 0]].to_radians();
            let lon1 = gps_data[[i - 1, 1]].to_radians();
            let lat2 = gps_data[[i, 0]].to_radians();
            let lon2 = gps_data[[i, 1]].to_radians();

            // Haversine formula for distance calculation
            let dlat = lat2 - lat1;
            let dlon = lon2 - lon1;

            let a =
                (dlat / 2.0).sin().powi(2) + lat1.cos() * lat2.cos() * (dlon / 2.0).sin().powi(2);
            let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
            let distance = 6371000.0 * c; // Earth radius in meters

            total_distance += distance;
        }

        Ok(total_distance)
    }

    /// Detect falls using accelerometer data
    pub fn fall_detection(&self, threshold: f64) -> Result<Vec<usize>> {
        let accel_data = self.acceleration.as_ref().ok_or_else(|| {
            TimeSeriesError::InvalidInput("Accelerometer data required".to_string())
        })?;

        let mut fall_events = Vec::new();

        // Calculate magnitude of acceleration vector
        for (i, row) in accel_data.outer_iter().enumerate() {
            let magnitude = (row[0] * row[0] + row[1] * row[1] + row[2] * row[2]).sqrt();

            // Fall detection: sudden increase followed by near-zero acceleration
            if magnitude > threshold {
                // Check for low acceleration in the next few samples (impact followed by stillness)
                let check_samples = ((0.5 * self.fs) as usize).min(accel_data.nrows() - i - 1);
                if check_samples > 0 {
                    let future_window = accel_data
                        .slice(scirs2_core::ndarray::s![i + 1..i + 1 + check_samples, ..]);
                    let future_magnitudes: Array1<f64> = future_window
                        .outer_iter()
                        .map(|row| (row[0] * row[0] + row[1] * row[1] + row[2] * row[2]).sqrt())
                        .collect();

                    let mean_future = future_magnitudes.mean();
                    if mean_future < 2.0 {
                        // Low acceleration following high impact
                        fall_events.push(i);
                    }
                }
            }
        }

        Ok(fall_events)
    }

    /// Calculate step count from accelerometer data
    pub fn step_count(&self) -> Result<usize> {
        let accel_data = self.acceleration.as_ref().ok_or_else(|| {
            TimeSeriesError::InvalidInput("Accelerometer data required".to_string())
        })?;

        // Calculate magnitude of acceleration
        let magnitude: Array1<f64> = accel_data
            .outer_iter()
            .map(|row| (row[0] * row[0] + row[1] * row[1] + row[2] * row[2]).sqrt())
            .collect();

        // Apply high-pass filter to remove gravity component
        let mean_magnitude = magnitude.clone().mean();
        let filtered: Array1<f64> = magnitude.iter().map(|&x| x - mean_magnitude).collect();

        // Simple peak detection for step counting
        let mut steps = 0;
        let min_peak_height = 1.0; // Threshold for step detection
        let min_peak_distance = (0.5 * self.fs) as usize; // Minimum 0.5 seconds between steps
        let mut last_peak = 0;

        for i in 1..filtered.len() - 1 {
            if filtered[i] > filtered[i - 1]
                && filtered[i] > filtered[i + 1]
                && filtered[i] > min_peak_height
                && (i - last_peak) > min_peak_distance
            {
                steps += 1;
                last_peak = i;
            }
        }

        Ok(steps)
    }
}

/// General IoT sensor data quality assessment
pub struct IoTDataQualityAnalysis {
    /// Sensor data array
    pub data: Array1<f64>,
    /// Sensor type
    pub sensor_type: SensorType,
    /// Time stamps
    pub timestamps: Array1<i64>,
}

impl IoTDataQualityAnalysis {
    /// Create new data quality analysis
    pub fn new(
        data: Array1<f64>,
        sensor_type: SensorType,
        timestamps: Array1<i64>,
    ) -> Result<Self> {
        if data.len() != timestamps.len() {
            return Err(TimeSeriesError::InvalidInput(
                "Data and timestamp arrays must have same length".to_string(),
            ));
        }

        Ok(Self {
            data,
            sensor_type,
            timestamps,
        })
    }

    /// Assess overall data quality
    pub fn assess_quality(&self) -> Result<DataQuality> {
        let missing_percentage = self.calculate_missing_percentage()?;
        let outlier_percentage = self.calculate_outlier_percentage()?;
        let snr = self.calculate_snr()?;
        let consistency_score = self.calculate_consistency_score()?;

        Ok(DataQuality {
            missing_percentage,
            outlier_percentage,
            snr,
            consistency_score,
        })
    }

    /// Calculate percentage of missing data points
    fn calculate_missing_percentage(&self) -> Result<f64> {
        let missing_count = self.data.iter().filter(|&&x| x.is_nan()).count();
        Ok((missing_count as f64 / self.data.len() as f64) * 100.0)
    }

    /// Calculate percentage of outliers using IQR method
    fn calculate_outlier_percentage(&self) -> Result<f64> {
        let mut sorted_data: Vec<f64> = self
            .data
            .iter()
            .filter(|&&x| !x.is_nan())
            .cloned()
            .collect();
        sorted_data.sort_by(|a, b| a.partial_cmp(b).expect("Operation failed"));

        if sorted_data.len() < 4 {
            return Ok(0.0);
        }

        let q1_idx = sorted_data.len() / 4;
        let q3_idx = 3 * sorted_data.len() / 4;
        let q1 = sorted_data[q1_idx];
        let q3 = sorted_data[q3_idx];
        let iqr = q3 - q1;

        let lower_bound = q1 - 1.5 * iqr;
        let upper_bound = q3 + 1.5 * iqr;

        let outlier_count = self
            .data
            .iter()
            .filter(|&&x| !x.is_nan() && (x < lower_bound || x > upper_bound))
            .count();

        Ok((outlier_count as f64 / sorted_data.len() as f64) * 100.0)
    }

    /// Calculate signal-to-noise ratio
    fn calculate_snr(&self) -> Result<f64> {
        let valid_data: Vec<f64> = self
            .data
            .iter()
            .filter(|&&x| !x.is_nan())
            .cloned()
            .collect();

        if valid_data.is_empty() {
            return Ok(0.0);
        }

        let mean = valid_data.iter().sum::<f64>() / valid_data.len() as f64;
        let variance =
            valid_data.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / valid_data.len() as f64;

        let signal_power = mean.abs();
        let noise_power = variance.sqrt();

        if noise_power == 0.0 {
            Ok(f64::INFINITY)
        } else {
            Ok(20.0 * (signal_power / noise_power).log10())
        }
    }

    /// Calculate data consistency score based on expected patterns
    fn calculate_consistency_score(&self) -> Result<f64> {
        // Check for reasonable sampling rate consistency
        if self.timestamps.len() < 2 {
            return Ok(100.0);
        }

        let mut intervals = Vec::new();
        for i in 1..self.timestamps.len() {
            intervals.push(self.timestamps[i] - self.timestamps[i - 1]);
        }

        if intervals.is_empty() {
            return Ok(100.0);
        }

        let mean_interval = intervals.iter().sum::<i64>() as f64 / intervals.len() as f64;
        let std_interval = {
            let variance = intervals
                .iter()
                .map(|&x| (x as f64 - mean_interval).powi(2))
                .sum::<f64>()
                / intervals.len() as f64;
            variance.sqrt()
        };

        // Consistency score based on interval regularity (0-100)
        let consistency = if mean_interval == 0.0 {
            0.0
        } else {
            100.0 - (std_interval / mean_interval * 100.0).min(100.0)
        };

        Ok(consistency.max(0.0))
    }
}

/// Comprehensive IoT sensor analysis
pub struct IoTAnalysis {
    /// Environmental sensor analysis
    pub environmental: Option<EnvironmentalSensorAnalysis>,
    /// Motion sensor analysis
    pub motion: Option<MotionSensorAnalysis>,
    /// Data quality assessments
    pub quality_assessments: HashMap<String, DataQuality>,
}

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

impl IoTAnalysis {
    /// Create new IoT analysis
    pub fn new() -> Self {
        Self {
            environmental: None,
            motion: None,
            quality_assessments: HashMap::new(),
        }
    }

    /// Add environmental sensor analysis
    pub fn with_environmental(mut self, analysis: EnvironmentalSensorAnalysis) -> Self {
        self.environmental = Some(analysis);
        self
    }

    /// Add motion sensor analysis
    pub fn with_motion(mut self, analysis: MotionSensorAnalysis) -> Self {
        self.motion = Some(analysis);
        self
    }

    /// Add data quality assessment for a sensor
    pub fn add_quality_assessment(&mut self, sensorname: String, quality: DataQuality) {
        self.quality_assessments.insert(sensorname, quality);
    }

    /// Generate comprehensive IoT system health report
    pub fn system_health_report(&self) -> Result<HashMap<String, String>> {
        let mut report = HashMap::new();

        // Environmental system health
        if let Some(ref env) = self.environmental {
            let malfunctions = env.detect_sensor_malfunctions()?;
            let total_issues: usize = malfunctions.values().map(|v| v.len()).sum();

            let env_status = if total_issues == 0 {
                "All environmental sensors functioning normally".to_string()
            } else {
                format!("{total_issues} environmental sensor issues detected")
            };
            report.insert("Environmental_Status".to_string(), env_status);

            // Energy recommendations
            let recommendations = env.energy_optimization_recommendations()?;
            if !recommendations.is_empty() {
                report.insert(
                    "Energy_Recommendations".to_string(),
                    recommendations.join("; "),
                );
            }
        }

        // Motion system health
        if let Some(ref motion) = self.motion {
            if motion.acceleration.is_some() {
                let activities = motion.activity_recognition()?;
                let activity_summary = format!("{} activity periods detected", activities.len());
                report.insert("Activity_Status".to_string(), activity_summary);
            }

            if motion.gps.is_some() {
                let distance = motion.distance_traveled()?;
                report.insert(
                    "Distance_Traveled".to_string(),
                    format!("{distance:.2} meters"),
                );
            }
        }

        // Overall data quality
        let mut quality_issues = 0;
        for quality in self.quality_assessments.values() {
            if quality.missing_percentage > 10.0
                || quality.outlier_percentage > 5.0
                || quality.snr < 10.0
            {
                quality_issues += 1;
            }
        }

        let quality_status = if quality_issues == 0 {
            "All sensors showing good data quality".to_string()
        } else {
            format!("{quality_issues} sensors showing data quality issues")
        };
        report.insert("Data_Quality_Status".to_string(), quality_status);

        Ok(report)
    }

    /// Predict maintenance needs based on sensor data patterns
    pub fn predictive_maintenance(&self) -> Result<Vec<String>> {
        let mut maintenance_alerts = Vec::new();

        // Check data quality degradation
        for (sensor_name, quality) in &self.quality_assessments {
            if quality.missing_percentage > 20.0 {
                maintenance_alerts.push(format!(
                    "Sensor '{sensor_name}' may need replacement - high missing data rate"
                ));
            }

            if quality.outlier_percentage > 15.0 {
                maintenance_alerts.push(format!(
                    "Sensor '{sensor_name}' may need calibration - high outlier rate"
                ));
            }

            if quality.snr < 5.0 {
                maintenance_alerts.push(format!(
                    "Sensor '{sensor_name}' may have connectivity issues - low signal quality"
                ));
            }
        }

        // Environmental sensor specific maintenance
        if let Some(ref env) = self.environmental {
            let malfunctions = env.detect_sensor_malfunctions()?;

            for (sensor_type, issues) in malfunctions {
                if issues.len() > 100 {
                    maintenance_alerts.push(format!(
                        "{sensor_type} sensor requires immediate attention - multiple malfunctions detected"
                    ));
                }
            }
        }

        Ok(maintenance_alerts)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{arr1, arr2};

    #[test]
    fn test_environmental_sensor_analysis() {
        let timestamps = arr1(&[1, 2, 3, 4, 5]);
        let temperatures = arr1(&[20.0, 21.0, 22.0, 23.0, 24.0]);
        let humidity = arr1(&[45.0, 46.0, 47.0, 48.0, 49.0]);

        let analysis = EnvironmentalSensorAnalysis::new(timestamps, 1.0)
            .expect("Operation failed")
            .with_temperature(temperatures)
            .expect("Operation failed")
            .with_humidity(humidity)
            .expect("Operation failed");

        let comfort = analysis.comfort_index().expect("Operation failed");
        assert_eq!(comfort.len(), 5);
        assert!(comfort.iter().all(|&x| (0.0..=100.0).contains(&x)));

        let malfunctions = analysis
            .detect_sensor_malfunctions()
            .expect("Operation failed");
        assert!(malfunctions.contains_key("Temperature"));
        assert!(malfunctions.contains_key("Humidity"));
    }

    #[test]
    fn test_motion_sensor_analysis() {
        let timestamps = arr1(&[1, 2, 3, 4, 5]);
        let accel_data = arr2(&[
            [1.0, 2.0, 9.8],
            [1.1, 2.1, 9.9],
            [1.2, 2.2, 10.0],
            [1.3, 2.3, 10.1],
            [1.4, 2.4, 10.2],
        ]);

        // Use a lower sampling frequency so the window size fits our data
        let analysis = MotionSensorAnalysis::new(timestamps, 2.0)
            .expect("Operation failed")
            .with_accelerometer(accel_data)
            .expect("Operation failed");

        let activities = analysis.activity_recognition().expect("Operation failed");
        assert!(!activities.is_empty());

        let _steps = analysis.step_count().expect("Operation failed");
    }

    #[test]
    fn test_iot_data_quality() {
        let data = arr1(&[1.0, 2.0, 3.0, 100.0, 5.0]); // Contains outlier
        let timestamps = arr1(&[1, 2, 3, 4, 5]);

        let quality_analysis =
            IoTDataQualityAnalysis::new(data, SensorType::Temperature, timestamps)
                .expect("Operation failed");
        let quality = quality_analysis.assess_quality().expect("Operation failed");

        assert!(quality.outlier_percentage > 0.0);
        assert!(quality.consistency_score >= 0.0 && quality.consistency_score <= 100.0);
    }

    #[test]
    fn test_fall_detection() {
        let timestamps = arr1(&[1, 2, 3, 4, 5]);
        // Simulate fall: normal acceleration followed by high impact then stillness
        let accel_data = arr2(&[
            [1.0, 2.0, 9.8],    // Normal
            [1.0, 2.0, 9.8],    // Normal
            [10.0, 15.0, 20.0], // High impact (fall)
            [0.5, 0.5, 0.5],    // Near stillness after fall
            [0.5, 0.5, 0.5],    // Continued stillness
        ]);

        let analysis = MotionSensorAnalysis::new(timestamps, 100.0)
            .expect("Operation failed")
            .with_accelerometer(accel_data)
            .expect("Operation failed");

        let falls = analysis.fall_detection(25.0).expect("Operation failed");
        assert!(!falls.is_empty());
        assert_eq!(falls[0], 2); // Fall detected at index 2
    }
}