wifi-densepose-sensing-server 0.3.2

Lightweight Axum server for WiFi sensing UI with RuVector signal processing
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
//! Dataset loaders for WiFi-to-DensePose training pipeline (ADR-023 Phase 1).
//!
//! Provides unified data loading for MM-Fi (NeurIPS 2023) and Wi-Pose datasets,
//! with from-scratch .npy/.mat v5 parsers, subcarrier resampling, and a unified
//! `DataPipeline` for normalized, windowed training samples.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::io;
use std::path::{Path, PathBuf};

// ── Error type ───────────────────────────────────────────────────────────────

#[derive(Debug)]
pub enum DatasetError {
    Io(io::Error),
    Format(String),
    Missing(String),
    Shape(String),
}

impl fmt::Display for DatasetError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(e) => write!(f, "I/O error: {e}"),
            Self::Format(s) => write!(f, "format error: {s}"),
            Self::Missing(s) => write!(f, "missing: {s}"),
            Self::Shape(s) => write!(f, "shape error: {s}"),
        }
    }
}

impl std::error::Error for DatasetError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        if let Self::Io(e) = self {
            Some(e)
        } else {
            None
        }
    }
}

impl From<io::Error> for DatasetError {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

pub type Result<T> = std::result::Result<T, DatasetError>;

// ── NpyArray ─────────────────────────────────────────────────────────────────

/// Dense array from .npy: flat f32 data with shape metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NpyArray {
    pub shape: Vec<usize>,
    pub data: Vec<f32>,
}

impl NpyArray {
    pub fn len(&self) -> usize {
        self.data.len()
    }
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
    pub fn ndim(&self) -> usize {
        self.shape.len()
    }
}

// ── NpyReader ────────────────────────────────────────────────────────────────

/// Minimal NumPy .npy format reader (f32/f64, v1/v2).
pub struct NpyReader;

impl NpyReader {
    pub fn read_file(path: &Path) -> Result<NpyArray> {
        Self::parse(&std::fs::read(path)?)
    }

    pub fn parse(buf: &[u8]) -> Result<NpyArray> {
        if buf.len() < 10 {
            return Err(DatasetError::Format("file too small for .npy".into()));
        }
        if &buf[0..6] != b"\x93NUMPY" {
            return Err(DatasetError::Format("missing .npy magic".into()));
        }
        let major = buf[6];
        let (header_len, header_start) = match major {
            1 => (u16::from_le_bytes([buf[8], buf[9]]) as usize, 10usize),
            2 | 3 => {
                if buf.len() < 12 {
                    return Err(DatasetError::Format("truncated v2 header".into()));
                }
                (
                    u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]) as usize,
                    12,
                )
            }
            _ => {
                return Err(DatasetError::Format(format!(
                    "unsupported .npy version {major}"
                )))
            }
        };
        let header_end = header_start + header_len;
        if header_end > buf.len() {
            return Err(DatasetError::Format("header past EOF".into()));
        }
        let hdr = std::str::from_utf8(&buf[header_start..header_end])
            .map_err(|_| DatasetError::Format("non-UTF8 header".into()))?;

        let dtype = Self::extract_field(hdr, "descr")?;
        let is_f64 = dtype.contains("f8") || dtype.contains("float64");
        let is_f32 = dtype.contains("f4") || dtype.contains("float32");
        let is_big = dtype.starts_with('>');
        if !is_f32 && !is_f64 {
            return Err(DatasetError::Format(format!("unsupported dtype '{dtype}'")));
        }
        let fortran = Self::extract_field(hdr, "fortran_order")
            .unwrap_or_else(|_| "False".into())
            .contains("True");
        let shape = Self::parse_shape(hdr)?;
        let elem_sz: usize = if is_f64 { 8 } else { 4 };
        let total: usize = shape.iter().product::<usize>().max(1);
        if header_end + total * elem_sz > buf.len() {
            return Err(DatasetError::Format("data truncated".into()));
        }
        let raw = &buf[header_end..header_end + total * elem_sz];
        let mut data: Vec<f32> = if is_f64 {
            raw.chunks_exact(8)
                .map(|c| {
                    let v = if is_big {
                        f64::from_be_bytes(c.try_into().unwrap())
                    } else {
                        f64::from_le_bytes(c.try_into().unwrap())
                    };
                    v as f32
                })
                .collect()
        } else {
            raw.chunks_exact(4)
                .map(|c| {
                    if is_big {
                        f32::from_be_bytes(c.try_into().unwrap())
                    } else {
                        f32::from_le_bytes(c.try_into().unwrap())
                    }
                })
                .collect()
        };
        if fortran && shape.len() == 2 {
            let (r, c) = (shape[0], shape[1]);
            let mut cd = vec![0.0f32; data.len()];
            for ri in 0..r {
                for ci in 0..c {
                    cd[ri * c + ci] = data[ci * r + ri];
                }
            }
            data = cd;
        }
        let shape = if shape.is_empty() { vec![1] } else { shape };
        Ok(NpyArray { shape, data })
    }

    fn extract_field(hdr: &str, field: &str) -> Result<String> {
        for pat in &[
            format!("'{field}': "),
            format!("'{field}':"),
            format!("\"{field}\": "),
        ] {
            if let Some(s) = hdr.find(pat.as_str()) {
                let rest = &hdr[s + pat.len()..];
                let end = rest
                    .find(',')
                    .or_else(|| rest.find('}'))
                    .unwrap_or(rest.len());
                return Ok(rest[..end]
                    .trim()
                    .trim_matches('\'')
                    .trim_matches('"')
                    .into());
            }
        }
        Err(DatasetError::Format(format!("field '{field}' not found")))
    }

    fn parse_shape(hdr: &str) -> Result<Vec<usize>> {
        let si = hdr
            .find("'shape'")
            .or_else(|| hdr.find("\"shape\""))
            .ok_or_else(|| DatasetError::Format("no 'shape'".into()))?;
        let rest = &hdr[si..];
        let ps = rest
            .find('(')
            .ok_or_else(|| DatasetError::Format("no '('".into()))?;
        let pe = rest[ps..]
            .find(')')
            .ok_or_else(|| DatasetError::Format("no ')'".into()))?;
        let inner = rest[ps + 1..ps + pe].trim();
        if inner.is_empty() {
            return Ok(vec![]);
        }
        inner
            .split(',')
            .map(|s| s.trim())
            .filter(|s| !s.is_empty())
            .map(|s| {
                s.parse::<usize>()
                    .map_err(|_| DatasetError::Format(format!("bad dim: '{s}'")))
            })
            .collect()
    }
}

// ── MatReader ────────────────────────────────────────────────────────────────

/// Minimal MATLAB .mat v5 reader for numeric arrays.
pub struct MatReader;

const MI_INT8: u32 = 1;
#[allow(dead_code)]
const MI_UINT8: u32 = 2;
#[allow(dead_code)]
const MI_INT16: u32 = 3;
#[allow(dead_code)]
const MI_UINT16: u32 = 4;
const MI_INT32: u32 = 5;
const MI_UINT32: u32 = 6;
const MI_SINGLE: u32 = 7;
const MI_DOUBLE: u32 = 9;
const MI_MATRIX: u32 = 14;

impl MatReader {
    pub fn read_file(path: &Path) -> Result<HashMap<String, NpyArray>> {
        Self::parse(&std::fs::read(path)?)
    }

    pub fn parse(buf: &[u8]) -> Result<HashMap<String, NpyArray>> {
        if buf.len() < 128 {
            return Err(DatasetError::Format("too small for .mat v5".into()));
        }
        let swap = u16::from_le_bytes([buf[126], buf[127]]) == 0x4D49;
        let mut result = HashMap::new();
        let mut off = 128;
        while off + 8 <= buf.len() {
            let (dt, ds, ts) = Self::read_tag(buf, off, swap)?;
            let el_start = off + ts;
            let el_end = el_start + ds;
            if el_end > buf.len() {
                break;
            }
            if dt == MI_MATRIX {
                if let Ok((n, a)) = Self::parse_matrix(&buf[el_start..el_end], swap) {
                    result.insert(n, a);
                }
            }
            off = (el_end + 7) & !7;
        }
        Ok(result)
    }

    fn read_tag(buf: &[u8], off: usize, swap: bool) -> Result<(u32, usize, usize)> {
        if off + 4 > buf.len() {
            return Err(DatasetError::Format("truncated tag".into()));
        }
        let raw = Self::u32(buf, off, swap);
        let upper = (raw >> 16) & 0xFFFF;
        if upper != 0 && upper <= 4 {
            return Ok((raw & 0xFFFF, upper as usize, 4));
        }
        if off + 8 > buf.len() {
            return Err(DatasetError::Format("truncated tag".into()));
        }
        Ok((raw, Self::u32(buf, off + 4, swap) as usize, 8))
    }

    fn parse_matrix(buf: &[u8], swap: bool) -> Result<(String, NpyArray)> {
        let (mut name, mut shape, mut data) = (String::new(), Vec::new(), Vec::new());
        let mut off = 0;
        while off + 4 <= buf.len() {
            let (st, ss, ts) = Self::read_tag(buf, off, swap)?;
            let ss_start = off + ts;
            let ss_end = (ss_start + ss).min(buf.len());
            match st {
                MI_UINT32 if shape.is_empty() && ss == 8 => {}
                MI_INT32 if shape.is_empty() => {
                    for i in 0..ss / 4 {
                        shape.push(Self::i32(buf, ss_start + i * 4, swap) as usize);
                    }
                }
                MI_INT8 if name.is_empty() && ss_end <= buf.len() => {
                    name = String::from_utf8_lossy(&buf[ss_start..ss_end])
                        .trim_end_matches('\0')
                        .to_string();
                }
                MI_DOUBLE => {
                    for i in 0..ss / 8 {
                        let p = ss_start + i * 8;
                        if p + 8 <= buf.len() {
                            data.push(Self::f64(buf, p, swap) as f32);
                        }
                    }
                }
                MI_SINGLE => {
                    for i in 0..ss / 4 {
                        let p = ss_start + i * 4;
                        if p + 4 <= buf.len() {
                            data.push(Self::f32(buf, p, swap));
                        }
                    }
                }
                _ => {}
            }
            off = (ss_end + 7) & !7;
        }
        if name.is_empty() {
            name = "unnamed".into();
        }
        if shape.is_empty() && !data.is_empty() {
            shape = vec![data.len()];
        }
        // Transpose column-major to row-major for 2D
        if shape.len() == 2 {
            let (r, c) = (shape[0], shape[1]);
            if r * c == data.len() {
                let mut cd = vec![0.0f32; data.len()];
                for ri in 0..r {
                    for ci in 0..c {
                        cd[ri * c + ci] = data[ci * r + ri];
                    }
                }
                data = cd;
            }
        }
        Ok((name, NpyArray { shape, data }))
    }

    fn u32(b: &[u8], o: usize, s: bool) -> u32 {
        let v = [b[o], b[o + 1], b[o + 2], b[o + 3]];
        if s {
            u32::from_be_bytes(v)
        } else {
            u32::from_le_bytes(v)
        }
    }
    fn i32(b: &[u8], o: usize, s: bool) -> i32 {
        let v = [b[o], b[o + 1], b[o + 2], b[o + 3]];
        if s {
            i32::from_be_bytes(v)
        } else {
            i32::from_le_bytes(v)
        }
    }
    fn f64(b: &[u8], o: usize, s: bool) -> f64 {
        let v: [u8; 8] = b[o..o + 8].try_into().unwrap();
        if s {
            f64::from_be_bytes(v)
        } else {
            f64::from_le_bytes(v)
        }
    }
    fn f32(b: &[u8], o: usize, s: bool) -> f32 {
        let v = [b[o], b[o + 1], b[o + 2], b[o + 3]];
        if s {
            f32::from_be_bytes(v)
        } else {
            f32::from_le_bytes(v)
        }
    }
}

// ── Core data types ──────────────────────────────────────────────────────────

/// A single CSI (Channel State Information) sample.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CsiSample {
    pub amplitude: Vec<f32>,
    pub phase: Vec<f32>,
    pub timestamp_ms: u64,
}

/// UV coordinate map for a body part in DensePose representation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BodyPartUV {
    pub part_id: u8,
    pub u_coords: Vec<f32>,
    pub v_coords: Vec<f32>,
}

/// Pose label: 17 COCO keypoints + optional DensePose body-part UVs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoseLabel {
    pub keypoints: [(f32, f32, f32); 17],
    pub body_parts: Vec<BodyPartUV>,
    pub confidence: f32,
}

impl Default for PoseLabel {
    fn default() -> Self {
        Self {
            keypoints: [(0.0, 0.0, 0.0); 17],
            body_parts: Vec::new(),
            confidence: 0.0,
        }
    }
}

// ── SubcarrierResampler ──────────────────────────────────────────────────────

/// Resamples subcarrier data via linear interpolation or zero-padding.
pub struct SubcarrierResampler;

impl SubcarrierResampler {
    /// Resample: passthrough if equal, zero-pad if upsampling, interpolate if downsampling.
    pub fn resample(input: &[f32], from: usize, to: usize) -> Vec<f32> {
        if from == to || from == 0 || to == 0 {
            return input.to_vec();
        }
        if from < to {
            Self::zero_pad(input, from, to)
        } else {
            Self::interpolate(input, from, to)
        }
    }

    /// Resample phase data with unwrapping before interpolation.
    pub fn resample_phase(input: &[f32], from: usize, to: usize) -> Vec<f32> {
        if from == to || from == 0 || to == 0 {
            return input.to_vec();
        }
        let unwrapped = Self::phase_unwrap(input);
        let resampled = if from < to {
            Self::zero_pad(&unwrapped, from, to)
        } else {
            Self::interpolate(&unwrapped, from, to)
        };
        let pi = std::f32::consts::PI;
        resampled
            .iter()
            .map(|&p| {
                let mut w = p % (2.0 * pi);
                if w > pi {
                    w -= 2.0 * pi;
                }
                if w < -pi {
                    w += 2.0 * pi;
                }
                w
            })
            .collect()
    }

    fn zero_pad(input: &[f32], from: usize, to: usize) -> Vec<f32> {
        let pad_left = (to - from) / 2;
        let mut out = vec![0.0f32; to];
        for i in 0..from.min(input.len()) {
            if pad_left + i < to {
                out[pad_left + i] = input[i];
            }
        }
        out
    }

    fn interpolate(input: &[f32], from: usize, to: usize) -> Vec<f32> {
        let n = input.len().min(from);
        if n <= 1 {
            return vec![input.first().copied().unwrap_or(0.0); to];
        }
        (0..to)
            .map(|i| {
                let pos = i as f64 * (n - 1) as f64 / (to - 1).max(1) as f64;
                let lo = pos.floor() as usize;
                let hi = (lo + 1).min(n - 1);
                let f = (pos - lo as f64) as f32;
                input[lo] * (1.0 - f) + input[hi] * f
            })
            .collect()
    }

    fn phase_unwrap(phase: &[f32]) -> Vec<f32> {
        let pi = std::f32::consts::PI;
        let mut out = vec![0.0f32; phase.len()];
        if phase.is_empty() {
            return out;
        }
        out[0] = phase[0];
        for i in 1..phase.len() {
            let mut d = phase[i] - phase[i - 1];
            while d > pi {
                d -= 2.0 * pi;
            }
            while d < -pi {
                d += 2.0 * pi;
            }
            out[i] = out[i - 1] + d;
        }
        out
    }
}

// ── MmFiDataset ──────────────────────────────────────────────────────────────

/// MM-Fi (NeurIPS 2023) dataset loader with 56 subcarriers and 17 COCO keypoints.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MmFiDataset {
    pub csi_frames: Vec<CsiSample>,
    pub labels: Vec<PoseLabel>,
    pub sample_rate_hz: f32,
    pub n_subcarriers: usize,
}

impl MmFiDataset {
    pub const SUBCARRIERS: usize = 56;

    /// Load from directory with csi_amplitude.npy/csi.npy and labels.npy/keypoints.npy.
    pub fn load_from_directory(path: &Path) -> Result<Self> {
        if !path.is_dir() {
            return Err(DatasetError::Missing(format!(
                "directory not found: {}",
                path.display()
            )));
        }
        let amp = NpyReader::read_file(&Self::find(path, &["csi_amplitude.npy", "csi.npy"])?)?;
        let n = amp.shape.first().copied().unwrap_or(0);
        let raw_sc = if amp.shape.len() >= 2 {
            amp.shape[1]
        } else {
            amp.data.len() / n.max(1)
        };
        let phase_arr = Self::find(path, &["csi_phase.npy"])
            .ok()
            .and_then(|p| NpyReader::read_file(&p).ok());
        let lab = NpyReader::read_file(&Self::find(path, &["labels.npy", "keypoints.npy"])?)?;

        let mut csi_frames = Vec::with_capacity(n);
        let mut labels = Vec::with_capacity(n);
        for i in 0..n {
            let s = i * raw_sc;
            if s + raw_sc > amp.data.len() {
                break;
            }
            let amplitude =
                SubcarrierResampler::resample(&amp.data[s..s + raw_sc], raw_sc, Self::SUBCARRIERS);
            let phase = phase_arr
                .as_ref()
                .map(|pa| {
                    let ps = i * raw_sc;
                    if ps + raw_sc <= pa.data.len() {
                        SubcarrierResampler::resample_phase(
                            &pa.data[ps..ps + raw_sc],
                            raw_sc,
                            Self::SUBCARRIERS,
                        )
                    } else {
                        vec![0.0; Self::SUBCARRIERS]
                    }
                })
                .unwrap_or_else(|| vec![0.0; Self::SUBCARRIERS]);

            csi_frames.push(CsiSample {
                amplitude,
                phase,
                timestamp_ms: i as u64 * 50,
            });

            let ks = i * 17 * 3;
            let label = if ks + 51 <= lab.data.len() {
                let d = &lab.data[ks..ks + 51];
                let mut kp = [(0.0f32, 0.0, 0.0); 17];
                for k in 0..17 {
                    kp[k] = (d[k * 3], d[k * 3 + 1], d[k * 3 + 2]);
                }
                PoseLabel {
                    keypoints: kp,
                    body_parts: Vec::new(),
                    confidence: 1.0,
                }
            } else {
                PoseLabel::default()
            };
            labels.push(label);
        }
        Ok(Self {
            csi_frames,
            labels,
            sample_rate_hz: 20.0,
            n_subcarriers: Self::SUBCARRIERS,
        })
    }

    pub fn resample_subcarriers(&mut self, from: usize, to: usize) {
        for f in &mut self.csi_frames {
            f.amplitude = SubcarrierResampler::resample(&f.amplitude, from, to);
            f.phase = SubcarrierResampler::resample_phase(&f.phase, from, to);
        }
        self.n_subcarriers = to;
    }

    pub fn iter_windows(
        &self,
        ws: usize,
        stride: usize,
    ) -> impl Iterator<Item = (&[CsiSample], &[PoseLabel])> {
        let stride = stride.max(1);
        let n = self.csi_frames.len();
        (0..n)
            .step_by(stride)
            .filter(move |&s| s + ws <= n)
            .map(move |s| (&self.csi_frames[s..s + ws], &self.labels[s..s + ws]))
    }

    pub fn split_train_val(self, ratio: f32) -> (Self, Self) {
        let split = (self.csi_frames.len() as f32 * ratio.clamp(0.0, 1.0)) as usize;
        let (tc, vc) = self.csi_frames.split_at(split);
        let (tl, vl) = self.labels.split_at(split);
        let mk = |c: &[CsiSample], l: &[PoseLabel]| Self {
            csi_frames: c.to_vec(),
            labels: l.to_vec(),
            sample_rate_hz: self.sample_rate_hz,
            n_subcarriers: self.n_subcarriers,
        };
        (mk(tc, tl), mk(vc, vl))
    }

    pub fn len(&self) -> usize {
        self.csi_frames.len()
    }
    pub fn is_empty(&self) -> bool {
        self.csi_frames.is_empty()
    }
    pub fn get(&self, idx: usize) -> Option<(&CsiSample, &PoseLabel)> {
        self.csi_frames.get(idx).zip(self.labels.get(idx))
    }

    fn find(dir: &Path, names: &[&str]) -> Result<PathBuf> {
        for n in names {
            let p = dir.join(n);
            if p.exists() {
                return Ok(p);
            }
        }
        Err(DatasetError::Missing(format!(
            "none of {names:?} in {}",
            dir.display()
        )))
    }
}

// ── WiPoseDataset ────────────────────────────────────────────────────────────

/// Wi-Pose dataset loader: .mat v5, 30 subcarriers (-> 56), 18 keypoints (-> 17 COCO).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WiPoseDataset {
    pub csi_frames: Vec<CsiSample>,
    pub labels: Vec<PoseLabel>,
    pub sample_rate_hz: f32,
    pub n_subcarriers: usize,
}

impl WiPoseDataset {
    pub const RAW_SUBCARRIERS: usize = 30;
    pub const TARGET_SUBCARRIERS: usize = 56;
    pub const RAW_KEYPOINTS: usize = 18;
    pub const COCO_KEYPOINTS: usize = 17;

    pub fn load_from_mat(path: &Path) -> Result<Self> {
        let arrays = MatReader::read_file(path)?;
        let csi = arrays
            .get("csi")
            .or_else(|| arrays.get("csi_data"))
            .or_else(|| arrays.get("CSI"))
            .ok_or_else(|| DatasetError::Missing("no CSI variable in .mat".into()))?;
        let n = csi.shape.first().copied().unwrap_or(0);
        let raw = if csi.shape.len() >= 2 {
            csi.shape[1]
        } else {
            Self::RAW_SUBCARRIERS
        };
        let lab = arrays
            .get("keypoints")
            .or_else(|| arrays.get("labels"))
            .or_else(|| arrays.get("pose"));

        let mut csi_frames = Vec::with_capacity(n);
        let mut labels = Vec::with_capacity(n);
        for i in 0..n {
            let s = i * raw;
            if s + raw > csi.data.len() {
                break;
            }
            let amp =
                SubcarrierResampler::resample(&csi.data[s..s + raw], raw, Self::TARGET_SUBCARRIERS);
            csi_frames.push(CsiSample {
                amplitude: amp,
                phase: vec![0.0; Self::TARGET_SUBCARRIERS],
                timestamp_ms: i as u64 * 100,
            });
            let label = lab
                .and_then(|la| {
                    let ks = i * Self::RAW_KEYPOINTS * 3;
                    if ks + Self::RAW_KEYPOINTS * 3 <= la.data.len() {
                        Some(Self::map_18_to_17(
                            &la.data[ks..ks + Self::RAW_KEYPOINTS * 3],
                        ))
                    } else {
                        None
                    }
                })
                .unwrap_or_default();
            labels.push(label);
        }
        Ok(Self {
            csi_frames,
            labels,
            sample_rate_hz: 10.0,
            n_subcarriers: Self::TARGET_SUBCARRIERS,
        })
    }

    /// Map 18 keypoints to 17 COCO: keep index 0 (nose), drop index 1, map 2..18 -> 1..16.
    fn map_18_to_17(data: &[f32]) -> PoseLabel {
        let mut kp = [(0.0f32, 0.0, 0.0); 17];
        if data.len() >= 18 * 3 {
            kp[0] = (data[0], data[1], data[2]);
            #[allow(clippy::needless_range_loop)]
            for i in 1..17 {
                let s = (i + 1) * 3;
                kp[i] = (data[s], data[s + 1], data[s + 2]);
            }
        }
        PoseLabel {
            keypoints: kp,
            body_parts: Vec::new(),
            confidence: 1.0,
        }
    }

    pub fn len(&self) -> usize {
        self.csi_frames.len()
    }
    pub fn is_empty(&self) -> bool {
        self.csi_frames.is_empty()
    }
}

// ── DataPipeline ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DataSource {
    MmFi(PathBuf),
    WiPose(PathBuf),
    Combined(Vec<DataSource>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataConfig {
    pub source: DataSource,
    pub window_size: usize,
    pub stride: usize,
    pub target_subcarriers: usize,
    pub normalize: bool,
}

impl Default for DataConfig {
    fn default() -> Self {
        Self {
            source: DataSource::Combined(Vec::new()),
            window_size: 10,
            stride: 5,
            target_subcarriers: 56,
            normalize: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingSample {
    pub csi_window: Vec<Vec<f32>>,
    pub pose_label: PoseLabel,
    pub source: &'static str,
}

/// Unified pipeline: loads, resamples, windows, and normalizes training data.
pub struct DataPipeline {
    config: DataConfig,
}

impl DataPipeline {
    pub fn new(config: DataConfig) -> Self {
        Self { config }
    }

    pub fn load(&self) -> Result<Vec<TrainingSample>> {
        let mut out = Vec::new();
        self.load_source(&self.config.source, &mut out)?;
        if self.config.normalize && !out.is_empty() {
            Self::normalize_samples(&mut out);
        }
        Ok(out)
    }

    fn load_source(&self, src: &DataSource, out: &mut Vec<TrainingSample>) -> Result<()> {
        match src {
            DataSource::MmFi(p) => {
                let mut ds = MmFiDataset::load_from_directory(p)?;
                if ds.n_subcarriers != self.config.target_subcarriers {
                    let f = ds.n_subcarriers;
                    ds.resample_subcarriers(f, self.config.target_subcarriers);
                }
                self.extract_windows(&ds.csi_frames, &ds.labels, "mmfi", out);
            }
            DataSource::WiPose(p) => {
                let ds = WiPoseDataset::load_from_mat(p)?;
                self.extract_windows(&ds.csi_frames, &ds.labels, "wipose", out);
            }
            DataSource::Combined(srcs) => {
                for s in srcs {
                    self.load_source(s, out)?;
                }
            }
        }
        Ok(())
    }

    fn extract_windows(
        &self,
        frames: &[CsiSample],
        labels: &[PoseLabel],
        source: &'static str,
        out: &mut Vec<TrainingSample>,
    ) {
        let (ws, stride) = (self.config.window_size, self.config.stride.max(1));
        let mut s = 0;
        while s + ws <= frames.len() {
            let window: Vec<Vec<f32>> = frames[s..s + ws]
                .iter()
                .map(|f| f.amplitude.clone())
                .collect();
            let label = labels.get(s + ws / 2).cloned().unwrap_or_default();
            out.push(TrainingSample {
                csi_window: window,
                pose_label: label,
                source,
            });
            s += stride;
        }
    }

    fn normalize_samples(samples: &mut [TrainingSample]) {
        let ns = samples
            .first()
            .and_then(|s| s.csi_window.first())
            .map(|f| f.len())
            .unwrap_or(0);
        if ns == 0 {
            return;
        }
        let (mut sum, mut sq) = (vec![0.0f64; ns], vec![0.0f64; ns]);
        let mut cnt = 0u64;
        for s in samples.iter() {
            for f in &s.csi_window {
                for (j, &v) in f.iter().enumerate().take(ns) {
                    let v = v as f64;
                    sum[j] += v;
                    sq[j] += v * v;
                }
                cnt += 1;
            }
        }
        if cnt == 0 {
            return;
        }
        let mean: Vec<f64> = sum.iter().map(|s| s / cnt as f64).collect();
        let std: Vec<f64> = sq
            .iter()
            .zip(mean.iter())
            .map(|(&s, &m)| (s / cnt as f64 - m * m).max(0.0).sqrt().max(1e-8))
            .collect();
        for s in samples.iter_mut() {
            for f in &mut s.csi_window {
                for (j, v) in f.iter_mut().enumerate().take(ns) {
                    *v = ((*v as f64 - mean[j]) / std[j]) as f32;
                }
            }
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

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

    fn make_npy_f32(shape: &[usize], data: &[f32]) -> Vec<u8> {
        let ss = if shape.len() == 1 {
            format!("({},)", shape[0])
        } else {
            format!(
                "({})",
                shape
                    .iter()
                    .map(|d| d.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        };
        let hdr = format!("{{'descr': '<f4', 'fortran_order': False, 'shape': {ss}, }}");
        let total = 10 + hdr.len();
        let padded = total.div_ceil(64) * 64;
        let hl = padded - 10;
        let mut buf = Vec::new();
        buf.extend_from_slice(b"\x93NUMPY\x01\x00");
        buf.extend_from_slice(&(hl as u16).to_le_bytes());
        buf.extend_from_slice(hdr.as_bytes());
        buf.resize(10 + hl, b' ');
        for &v in data {
            buf.extend_from_slice(&v.to_le_bytes());
        }
        buf
    }

    fn make_npy_f64(shape: &[usize], data: &[f64]) -> Vec<u8> {
        let ss = if shape.len() == 1 {
            format!("({},)", shape[0])
        } else {
            format!(
                "({})",
                shape
                    .iter()
                    .map(|d| d.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        };
        let hdr = format!("{{'descr': '<f8', 'fortran_order': False, 'shape': {ss}, }}");
        let total = 10 + hdr.len();
        let padded = total.div_ceil(64) * 64;
        let hl = padded - 10;
        let mut buf = Vec::new();
        buf.extend_from_slice(b"\x93NUMPY\x01\x00");
        buf.extend_from_slice(&(hl as u16).to_le_bytes());
        buf.extend_from_slice(hdr.as_bytes());
        buf.resize(10 + hl, b' ');
        for &v in data {
            buf.extend_from_slice(&v.to_le_bytes());
        }
        buf
    }

    #[test]
    fn npy_header_parse_1d() {
        let buf = make_npy_f32(&[5], &[1.0, 2.0, 3.0, 4.0, 5.0]);
        let arr = NpyReader::parse(&buf).unwrap();
        assert_eq!(arr.shape, vec![5]);
        assert_eq!(arr.ndim(), 1);
        assert_eq!(arr.len(), 5);
        assert!((arr.data[0] - 1.0).abs() < f32::EPSILON);
        assert!((arr.data[4] - 5.0).abs() < f32::EPSILON);
    }

    #[test]
    fn npy_header_parse_2d() {
        let data: Vec<f32> = (0..12).map(|i| i as f32).collect();
        let buf = make_npy_f32(&[3, 4], &data);
        let arr = NpyReader::parse(&buf).unwrap();
        assert_eq!(arr.shape, vec![3, 4]);
        assert_eq!(arr.ndim(), 2);
        assert_eq!(arr.len(), 12);
    }

    #[test]
    fn npy_header_parse_3d() {
        let data: Vec<f64> = (0..24).map(|i| i as f64 * 0.5).collect();
        let buf = make_npy_f64(&[2, 3, 4], &data);
        let arr = NpyReader::parse(&buf).unwrap();
        assert_eq!(arr.shape, vec![2, 3, 4]);
        assert_eq!(arr.ndim(), 3);
        assert_eq!(arr.len(), 24);
        assert!((arr.data[23] - 11.5).abs() < 1e-5);
    }

    #[test]
    fn subcarrier_resample_passthrough() {
        let input: Vec<f32> = (0..56).map(|i| i as f32).collect();
        let output = SubcarrierResampler::resample(&input, 56, 56);
        assert_eq!(output, input);
    }

    #[test]
    fn subcarrier_resample_upsample() {
        let input: Vec<f32> = (0..30).map(|i| (i + 1) as f32).collect();
        let out = SubcarrierResampler::resample(&input, 30, 56);
        assert_eq!(out.len(), 56);
        // pad_left = 13, leading zeros
        for (i, &v) in out[..13].iter().enumerate() {
            assert!(v.abs() < f32::EPSILON, "expected zero at {i}");
        }
        // original data in middle
        for (&ov, &iv) in out[13..43].iter().zip(input.iter()) {
            assert!((ov - iv).abs() < f32::EPSILON);
        }
        // trailing zeros
        #[allow(clippy::needless_range_loop)]
        for i in 43..56 {
            assert!(out[i].abs() < f32::EPSILON, "expected zero at {i}");
        }
    }

    #[test]
    fn subcarrier_resample_downsample() {
        let input: Vec<f32> = (0..114).map(|i| i as f32).collect();
        let out = SubcarrierResampler::resample(&input, 114, 56);
        assert_eq!(out.len(), 56);
        assert!((out[0]).abs() < f32::EPSILON);
        assert!((out[55] - 113.0).abs() < 0.1);
        for i in 1..56 {
            assert!(out[i] >= out[i - 1], "not monotonic at {i}");
        }
    }

    #[test]
    fn subcarrier_resample_preserves_dc() {
        let out = SubcarrierResampler::resample(&vec![42.0f32; 114], 114, 56);
        assert_eq!(out.len(), 56);
        for (i, &v) in out.iter().enumerate() {
            assert!((v - 42.0).abs() < 1e-5, "DC not preserved at {i}: {v}");
        }
    }

    #[test]
    fn mmfi_sample_structure() {
        let s = CsiSample {
            amplitude: vec![0.0; 56],
            phase: vec![0.0; 56],
            timestamp_ms: 100,
        };
        assert_eq!(s.amplitude.len(), 56);
        assert_eq!(s.phase.len(), 56);
    }

    #[test]
    fn wipose_zero_pad() {
        let raw: Vec<f32> = (1..=30).map(|i| i as f32).collect();
        let p = SubcarrierResampler::resample(&raw, 30, 56);
        assert_eq!(p.len(), 56);
        assert!(p[0].abs() < f32::EPSILON);
        assert!((p[13] - 1.0).abs() < f32::EPSILON);
        assert!((p[42] - 30.0).abs() < f32::EPSILON);
        assert!(p[55].abs() < f32::EPSILON);
    }

    #[test]
    fn wipose_keypoint_mapping() {
        let mut kp = vec![0.0f32; 18 * 3];
        kp[0] = 1.0;
        kp[1] = 2.0;
        kp[2] = 1.0; // nose
        kp[3] = 99.0;
        kp[4] = 99.0;
        kp[5] = 99.0; // extra (dropped)
        kp[6] = 3.0;
        kp[7] = 4.0;
        kp[8] = 1.0; // left eye -> COCO 1
        let label = WiPoseDataset::map_18_to_17(&kp);
        assert_eq!(label.keypoints.len(), 17);
        assert!((label.keypoints[0].0 - 1.0).abs() < f32::EPSILON);
        assert!((label.keypoints[1].0 - 3.0).abs() < f32::EPSILON); // not 99
    }

    #[test]
    fn train_val_split_ratio() {
        let mk = |n: usize| MmFiDataset {
            csi_frames: (0..n)
                .map(|i| CsiSample {
                    amplitude: vec![i as f32; 56],
                    phase: vec![0.0; 56],
                    timestamp_ms: i as u64,
                })
                .collect(),
            labels: (0..n).map(|_| PoseLabel::default()).collect(),
            sample_rate_hz: 20.0,
            n_subcarriers: 56,
        };
        let (train, val) = mk(100).split_train_val(0.8);
        assert_eq!(train.len(), 80);
        assert_eq!(val.len(), 20);
        assert_eq!(train.len() + val.len(), 100);
    }

    #[test]
    fn sliding_window_count() {
        let ds = MmFiDataset {
            csi_frames: (0..20)
                .map(|i| CsiSample {
                    amplitude: vec![i as f32; 56],
                    phase: vec![0.0; 56],
                    timestamp_ms: i as u64,
                })
                .collect(),
            labels: (0..20).map(|_| PoseLabel::default()).collect(),
            sample_rate_hz: 20.0,
            n_subcarriers: 56,
        };
        assert_eq!(ds.iter_windows(5, 5).count(), 4);
        assert_eq!(ds.iter_windows(5, 1).count(), 16);
    }

    #[test]
    fn sliding_window_overlap() {
        let ds = MmFiDataset {
            csi_frames: (0..10)
                .map(|i| CsiSample {
                    amplitude: vec![i as f32; 56],
                    phase: vec![0.0; 56],
                    timestamp_ms: i as u64,
                })
                .collect(),
            labels: (0..10).map(|_| PoseLabel::default()).collect(),
            sample_rate_hz: 20.0,
            n_subcarriers: 56,
        };
        let w: Vec<_> = ds.iter_windows(4, 2).collect();
        assert_eq!(w.len(), 4);
        assert!((w[0].0[0].amplitude[0]).abs() < f32::EPSILON);
        assert!((w[1].0[0].amplitude[0] - 2.0).abs() < f32::EPSILON);
        assert_eq!(w[0].0[2].amplitude[0], w[1].0[0].amplitude[0]); // overlap
    }

    #[test]
    fn data_pipeline_normalize() {
        let mut samples = vec![
            TrainingSample {
                csi_window: vec![vec![10.0, 20.0, 30.0]; 2],
                pose_label: PoseLabel::default(),
                source: "test",
            },
            TrainingSample {
                csi_window: vec![vec![30.0, 40.0, 50.0]; 2],
                pose_label: PoseLabel::default(),
                source: "test",
            },
        ];
        DataPipeline::normalize_samples(&mut samples);
        for j in 0..3 {
            let (mut s, mut c) = (0.0f64, 0u64);
            for sam in &samples {
                for f in &sam.csi_window {
                    s += f[j] as f64;
                    c += 1;
                }
            }
            assert!((s / c as f64).abs() < 1e-5, "mean not ~0 for sub {j}");
            let mut vs = 0.0f64;
            let m = s / c as f64;
            for sam in &samples {
                for f in &sam.csi_window {
                    vs += (f[j] as f64 - m).powi(2);
                }
            }
            assert!(
                ((vs / c as f64).sqrt() - 1.0).abs() < 0.1,
                "std not ~1 for sub {j}"
            );
        }
    }

    #[test]
    fn pose_label_default() {
        let l = PoseLabel::default();
        assert_eq!(l.keypoints.len(), 17);
        assert!(l.body_parts.is_empty());
        assert!(l.confidence.abs() < f32::EPSILON);
        for (i, kp) in l.keypoints.iter().enumerate() {
            assert!(
                kp.0.abs() < f32::EPSILON && kp.1.abs() < f32::EPSILON,
                "kp {i} not zero"
            );
        }
    }

    #[test]
    fn body_part_uv_round_trip() {
        let bpu = BodyPartUV {
            part_id: 5,
            u_coords: vec![0.1, 0.2, 0.3],
            v_coords: vec![0.4, 0.5, 0.6],
        };
        let json = serde_json::to_string(&bpu).unwrap();
        let r: BodyPartUV = serde_json::from_str(&json).unwrap();
        assert_eq!(r.part_id, 5);
        assert_eq!(r.u_coords.len(), 3);
        assert!((r.u_coords[0] - 0.1).abs() < f32::EPSILON);
        assert!((r.v_coords[2] - 0.6).abs() < f32::EPSILON);
    }

    #[test]
    fn combined_source_merges_datasets() {
        let mk = |n: usize, base: f32| -> (Vec<CsiSample>, Vec<PoseLabel>) {
            let f: Vec<CsiSample> = (0..n)
                .map(|i| CsiSample {
                    amplitude: vec![base + i as f32; 56],
                    phase: vec![0.0; 56],
                    timestamp_ms: i as u64 * 50,
                })
                .collect();
            let l: Vec<PoseLabel> = (0..n).map(|_| PoseLabel::default()).collect();
            (f, l)
        };
        let pipe = DataPipeline::new(DataConfig {
            source: DataSource::Combined(Vec::new()),
            window_size: 3,
            stride: 1,
            target_subcarriers: 56,
            normalize: false,
        });
        let mut all = Vec::new();
        let (fa, la) = mk(5, 0.0);
        pipe.extract_windows(&fa, &la, "mmfi", &mut all);
        assert_eq!(all.len(), 3);
        let (fb, lb) = mk(4, 100.0);
        pipe.extract_windows(&fb, &lb, "wipose", &mut all);
        assert_eq!(all.len(), 5);
        assert_eq!(all[0].source, "mmfi");
        assert_eq!(all[3].source, "wipose");
        assert!(all[0].csi_window[0][0] < 10.0);
        assert!(all[4].csi_window[0][0] > 90.0);
    }
}