wifi-densepose-mat 0.3.2

Mass Casualty Assessment Tool - WiFi-based disaster survivor detection
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
//! Detection pipeline combining all vital signs detectors.
//!
//! This module provides both traditional signal-processing-based detection
//! and optional ML-enhanced detection for improved accuracy.

use super::{
    BreathingDetector, BreathingDetectorConfig, HeartbeatDetector, HeartbeatDetectorConfig,
    MovementClassifier, MovementClassifierConfig,
};
use crate::domain::{ScanZone, VitalSignsReading};
#[cfg(feature = "ml")]
use crate::ml::{MlDetectionConfig, MlDetectionPipeline, MlDetectionResult};
use crate::{DisasterConfig, MatError};

/// Configuration for the detection pipeline
#[derive(Debug, Clone)]
pub struct DetectionConfig {
    /// Breathing detector configuration
    pub breathing: BreathingDetectorConfig,
    /// Heartbeat detector configuration
    pub heartbeat: HeartbeatDetectorConfig,
    /// Movement classifier configuration
    pub movement: MovementClassifierConfig,
    /// Sample rate of CSI data (Hz)
    pub sample_rate: f64,
    /// Whether to enable heartbeat detection (slower, more processing)
    pub enable_heartbeat: bool,
    /// Minimum overall confidence to report detection
    pub min_confidence: f64,
    /// Enable ML-enhanced detection (requires the `ml` feature to have any effect)
    pub enable_ml: bool,
    /// ML detection configuration (if enabled)
    #[cfg(feature = "ml")]
    pub ml_config: Option<MlDetectionConfig>,
}

impl Default for DetectionConfig {
    fn default() -> Self {
        Self {
            breathing: BreathingDetectorConfig::default(),
            heartbeat: HeartbeatDetectorConfig::default(),
            movement: MovementClassifierConfig::default(),
            sample_rate: 1000.0,
            enable_heartbeat: false,
            min_confidence: 0.3,
            enable_ml: false,
            #[cfg(feature = "ml")]
            ml_config: None,
        }
    }
}

impl DetectionConfig {
    /// Create configuration from disaster config
    pub fn from_disaster_config(config: &DisasterConfig) -> Self {
        let mut detection_config = Self::default();

        // Adjust sensitivity
        detection_config.breathing.confidence_threshold = (1.0 - config.sensitivity) as f32 * 0.5;
        detection_config.heartbeat.confidence_threshold = (1.0 - config.sensitivity) as f32 * 0.5;
        detection_config.min_confidence = 1.0 - config.sensitivity * 0.7;

        // Enable heartbeat for high sensitivity
        detection_config.enable_heartbeat = config.sensitivity > 0.7;

        detection_config
    }

    /// Enable ML-enhanced detection with the given configuration
    #[cfg(feature = "ml")]
    pub fn with_ml(mut self, ml_config: MlDetectionConfig) -> Self {
        self.enable_ml = true;
        self.ml_config = Some(ml_config);
        self
    }

    /// Enable ML-enhanced detection with default configuration
    #[cfg(feature = "ml")]
    pub fn with_default_ml(mut self) -> Self {
        self.enable_ml = true;
        self.ml_config = Some(MlDetectionConfig::default());
        self
    }
}

/// Trait for vital signs detection
pub trait VitalSignsDetector: Send + Sync {
    /// Process CSI data and detect vital signs
    fn detect(&self, csi_data: &CsiDataBuffer) -> Option<VitalSignsReading>;
}

/// Buffer for CSI data samples
#[derive(Debug, Default, Clone)]
pub struct CsiDataBuffer {
    /// Amplitude samples
    pub amplitudes: Vec<f64>,
    /// Phase samples (unwrapped)
    pub phases: Vec<f64>,
    /// Sample timestamps
    pub timestamps: Vec<f64>,
    /// Sample rate
    pub sample_rate: f64,
}

impl CsiDataBuffer {
    /// Create a new buffer
    pub fn new(sample_rate: f64) -> Self {
        Self {
            amplitudes: Vec::new(),
            phases: Vec::new(),
            timestamps: Vec::new(),
            sample_rate,
        }
    }

    /// Add samples to the buffer
    pub fn add_samples(&mut self, amplitudes: &[f64], phases: &[f64]) {
        self.amplitudes.extend(amplitudes);
        self.phases.extend(phases);

        // Generate timestamps
        let start = self.timestamps.last().copied().unwrap_or(0.0);
        let dt = 1.0 / self.sample_rate;
        for i in 0..amplitudes.len() {
            self.timestamps.push(start + (i + 1) as f64 * dt);
        }
    }

    /// Clear the buffer
    pub fn clear(&mut self) {
        self.amplitudes.clear();
        self.phases.clear();
        self.timestamps.clear();
    }

    /// Get the duration of data in the buffer (seconds)
    pub fn duration(&self) -> f64 {
        self.amplitudes.len() as f64 / self.sample_rate
    }

    /// Check if buffer has enough data for analysis
    pub fn has_sufficient_data(&self, min_duration: f64) -> bool {
        self.duration() >= min_duration
    }
}

/// Detection pipeline that combines all detectors
pub struct DetectionPipeline {
    config: DetectionConfig,
    breathing_detector: BreathingDetector,
    heartbeat_detector: HeartbeatDetector,
    movement_classifier: MovementClassifier,
    data_buffer: parking_lot::RwLock<CsiDataBuffer>,
    /// Optional ML detection pipeline
    #[cfg(feature = "ml")]
    ml_pipeline: Option<MlDetectionPipeline>,
}

impl DetectionPipeline {
    /// Create a new detection pipeline
    pub fn new(config: DetectionConfig) -> Self {
        #[cfg(feature = "ml")]
        let ml_pipeline = if config.enable_ml {
            config.ml_config.clone().map(MlDetectionPipeline::new)
        } else {
            None
        };

        Self {
            breathing_detector: BreathingDetector::new(config.breathing.clone()),
            heartbeat_detector: HeartbeatDetector::new(config.heartbeat.clone()),
            movement_classifier: MovementClassifier::new(config.movement.clone()),
            data_buffer: parking_lot::RwLock::new(CsiDataBuffer::new(config.sample_rate)),
            #[cfg(feature = "ml")]
            ml_pipeline,
            config,
        }
    }

    /// Initialize ML models asynchronously (if enabled)
    #[cfg(feature = "ml")]
    pub async fn initialize_ml(&mut self) -> Result<(), MatError> {
        if let Some(ref mut ml) = self.ml_pipeline {
            ml.initialize().await.map_err(MatError::from)?;
        }
        Ok(())
    }

    /// Check if ML pipeline is ready
    #[cfg(feature = "ml")]
    pub fn ml_ready(&self) -> bool {
        self.ml_pipeline.as_ref().is_none_or(|ml| ml.is_ready())
    }

    /// Process a scan zone and return detected vital signs.
    ///
    /// CSI data must be pushed into the pipeline via [`add_data`] before calling
    /// this method. The pipeline processes buffered amplitude/phase samples through
    /// breathing, heartbeat, and movement detectors. If ML is enabled and ready,
    /// results are enhanced with ML predictions.
    ///
    /// Returns `None` if insufficient data is buffered (< 5 seconds) or if
    /// detection confidence is below the configured threshold.
    pub async fn process_zone(
        &self,
        zone: &ScanZone,
    ) -> Result<Option<VitalSignsReading>, MatError> {
        // Process buffered CSI data through the signal processing pipeline.
        // Data arrives via add_data() from hardware adapters (ESP32, Intel 5300, etc.)
        // or from the CSI push API endpoint.
        // Drop the MutexGuard before hitting any await point.
        let reading = {
            let buffer = self.data_buffer.read();
            if !buffer.has_sufficient_data(5.0) {
                // Need at least 5 seconds of data
                return Ok(None);
            }
            // Detect vital signs using traditional pipeline
            self.detect_from_buffer(&buffer, zone)?
            // `buffer` guard dropped here
        };

        // If ML is enabled and ready, enhance with ML predictions (only
        // compiled under the `ml` feature; the base build is signal-only).
        let enhanced_reading = {
            #[cfg(feature = "ml")]
            {
                if self.config.enable_ml && self.ml_ready() {
                    // Snapshot the buffer under the lock, then drop the guard before await.
                    let buffer_snapshot = { self.data_buffer.read().clone() };
                    self.enhance_with_ml(reading, &buffer_snapshot).await?
                } else {
                    reading
                }
            }
            #[cfg(not(feature = "ml"))]
            {
                reading
            }
        };

        // Check minimum confidence
        if let Some(ref r) = enhanced_reading {
            if r.confidence.value() < self.config.min_confidence {
                return Ok(None);
            }
        }

        Ok(enhanced_reading)
    }

    /// Enhance detection results with ML predictions
    #[cfg(feature = "ml")]
    async fn enhance_with_ml(
        &self,
        traditional_reading: Option<VitalSignsReading>,
        buffer: &CsiDataBuffer,
    ) -> Result<Option<VitalSignsReading>, MatError> {
        let ml_pipeline = match &self.ml_pipeline {
            Some(ml) => ml,
            None => return Ok(traditional_reading),
        };

        // Get ML predictions
        let ml_result = ml_pipeline.process(buffer).await.map_err(MatError::from)?;

        // If we have ML vital classification, use it to enhance or replace traditional
        if let Some(ref ml_vital) = ml_result.vital_classification {
            if let Some(vital_reading) = ml_vital.to_vital_signs_reading() {
                // If ML result has higher confidence, prefer it
                if let Some(ref traditional) = traditional_reading {
                    if ml_result.overall_confidence() > traditional.confidence.value() as f32 {
                        return Ok(Some(vital_reading));
                    }
                } else {
                    // No traditional reading, use ML result
                    return Ok(Some(vital_reading));
                }
            }
        }

        Ok(traditional_reading)
    }

    /// Get the latest ML detection results (if ML is enabled)
    #[cfg(feature = "ml")]
    pub async fn get_ml_results(&self) -> Option<MlDetectionResult> {
        let ml = match &self.ml_pipeline {
            Some(ml) => ml,
            None => return None,
        };
        // Acquire lock, clone the relevant buffer data, then drop the guard before awaiting.
        let buffer = {
            let guard = self.data_buffer.read();
            guard.clone()
        };
        ml.process(&buffer).await.ok()
    }

    /// Add CSI data to the processing buffer
    pub fn add_data(&self, amplitudes: &[f64], phases: &[f64]) {
        let mut buffer = self.data_buffer.write();
        buffer.add_samples(amplitudes, phases);

        // Keep only recent data (last 30 seconds)
        let max_samples = (30.0 * self.config.sample_rate) as usize;
        if buffer.amplitudes.len() > max_samples {
            let drain_count = buffer.amplitudes.len() - max_samples;
            buffer.amplitudes.drain(0..drain_count);
            buffer.phases.drain(0..drain_count);
            buffer.timestamps.drain(0..drain_count);
        }
    }

    /// Clear the data buffer
    pub fn clear_buffer(&self) {
        self.data_buffer.write().clear();
    }

    /// Detect vital signs from buffered data
    fn detect_from_buffer(
        &self,
        buffer: &CsiDataBuffer,
        _zone: &ScanZone,
    ) -> Result<Option<VitalSignsReading>, MatError> {
        // Detect breathing
        let breathing = self
            .breathing_detector
            .detect(&buffer.amplitudes, buffer.sample_rate);

        // Detect heartbeat (if enabled)
        let heartbeat = if self.config.enable_heartbeat {
            let breathing_rate = breathing.as_ref().map(|b| b.rate_bpm as f64);
            self.heartbeat_detector
                .detect(&buffer.phases, buffer.sample_rate, breathing_rate)
        } else {
            None
        };

        // Classify movement
        let movement = self
            .movement_classifier
            .classify(&buffer.amplitudes, buffer.sample_rate);

        // Check if we detected anything
        if breathing.is_none()
            && heartbeat.is_none()
            && movement.movement_type == crate::domain::MovementType::None
        {
            return Ok(None);
        }

        // Create vital signs reading
        let reading = VitalSignsReading::new(breathing, heartbeat, movement);

        Ok(Some(reading))
    }

    /// Get configuration
    pub fn config(&self) -> &DetectionConfig {
        &self.config
    }

    /// Update configuration
    pub fn update_config(&mut self, config: DetectionConfig) {
        self.breathing_detector = BreathingDetector::new(config.breathing.clone());
        self.heartbeat_detector = HeartbeatDetector::new(config.heartbeat.clone());
        self.movement_classifier = MovementClassifier::new(config.movement.clone());

        // Update ML pipeline if configuration changed
        #[cfg(feature = "ml")]
        if config.enable_ml != self.config.enable_ml || config.ml_config != self.config.ml_config {
            self.ml_pipeline = if config.enable_ml {
                config.ml_config.clone().map(MlDetectionPipeline::new)
            } else {
                None
            };
        }

        self.config = config;
    }

    /// Get the ML pipeline (if enabled)
    #[cfg(feature = "ml")]
    pub fn ml_pipeline(&self) -> Option<&MlDetectionPipeline> {
        self.ml_pipeline.as_ref()
    }
}

impl VitalSignsDetector for DetectionPipeline {
    fn detect(&self, csi_data: &CsiDataBuffer) -> Option<VitalSignsReading> {
        // Detect breathing from amplitude variations
        let breathing = self
            .breathing_detector
            .detect(&csi_data.amplitudes, csi_data.sample_rate);

        // Detect heartbeat from phase variations
        let heartbeat = if self.config.enable_heartbeat {
            let breathing_rate = breathing.as_ref().map(|b| b.rate_bpm as f64);
            self.heartbeat_detector
                .detect(&csi_data.phases, csi_data.sample_rate, breathing_rate)
        } else {
            None
        };

        // Classify movement
        let movement = self
            .movement_classifier
            .classify(&csi_data.amplitudes, csi_data.sample_rate);

        // Create reading if we detected anything
        if breathing.is_some()
            || heartbeat.is_some()
            || movement.movement_type != crate::domain::MovementType::None
        {
            Some(VitalSignsReading::new(breathing, heartbeat, movement))
        } else {
            None
        }
    }
}

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

    fn create_test_buffer() -> CsiDataBuffer {
        let mut buffer = CsiDataBuffer::new(100.0);

        // Add 10 seconds of simulated breathing signal
        let num_samples = 1000;
        let amplitudes: Vec<f64> = (0..num_samples)
            .map(|i| {
                let t = i as f64 / 100.0;
                // 16 BPM breathing (0.267 Hz)
                (2.0 * std::f64::consts::PI * 0.267 * t).sin()
            })
            .collect();

        let phases: Vec<f64> = (0..num_samples)
            .map(|i| {
                let t = i as f64 / 100.0;
                // Phase variation from movement
                (2.0 * std::f64::consts::PI * 0.267 * t).sin() * 0.5
            })
            .collect();

        buffer.add_samples(&amplitudes, &phases);
        buffer
    }

    #[test]
    fn test_pipeline_creation() {
        let config = DetectionConfig::default();
        let pipeline = DetectionPipeline::new(config);
        assert_eq!(pipeline.config().sample_rate, 1000.0);
    }

    #[test]
    fn test_csi_buffer() {
        let mut buffer = CsiDataBuffer::new(100.0);

        assert!(!buffer.has_sufficient_data(5.0));

        let amplitudes: Vec<f64> = vec![1.0; 600];
        let phases: Vec<f64> = vec![0.0; 600];
        buffer.add_samples(&amplitudes, &phases);

        assert!(buffer.has_sufficient_data(5.0));
        assert_eq!(buffer.duration(), 6.0);
    }

    #[test]
    fn test_vital_signs_detection() {
        let config = DetectionConfig::default();
        let pipeline = DetectionPipeline::new(config);
        let buffer = create_test_buffer();

        let result = pipeline.detect(&buffer);
        assert!(result.is_some());

        let reading = result.unwrap();
        assert!(reading.has_vitals());
    }

    #[test]
    fn test_config_from_disaster_config() {
        let disaster_config = DisasterConfig::builder().sensitivity(0.9).build();

        let detection_config = DetectionConfig::from_disaster_config(&disaster_config);

        // High sensitivity should enable heartbeat detection
        assert!(detection_config.enable_heartbeat);
        // Low minimum confidence due to high sensitivity
        assert!(detection_config.min_confidence < 0.4);
    }
}