frenz-export 0.1.0

Turn openfrenz recordings of the FRENZ brainband into time-aligned Parquet session datasets
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
//! Sessions from SQLite recordings written by the `frenz` crate.

use std::path::Path;

use frenz::{Decoder, Health};
use frenz_proto::{Characteristic, Packet};
use rusqlite::{Connection, OpenFlags};
use serde_json::{Map, Value, json};

use crate::table::{Column, Table};
use crate::timing::{ClockFit, fit_lower_envelope};
use crate::{Error, Session, eeg_channel_json, motion_json};

/// The EEG sequence counter restarts at 0 within this time of a stream-start command (observed).
const RESTART_WINDOW_NS: i64 = 1_000_000_000;

/// One received packet's samples, before timing.
struct PacketRows<T> {
    arrival: f64,
    mono: i64,
    device_time: u32,
    sequence: Option<u8>,
    samples: Vec<T>,
}

/// Assigns segment and sample indices, fits each segment's clock, and returns per-sample
/// `(t, segment, packet, sample_in_packet, duplicate)` plus the fits.
struct Indexed {
    t: Vec<f64>,
    segment: Vec<i64>,
    packet: Vec<i64>,
    sample: Vec<i8>,
    duplicate: Vec<bool>,
    held: Vec<bool>,
    fits: Vec<ClockFit>,
}

fn index_and_time<T: PartialEq>(
    packets: &[PacketRows<T>],
    break_before: &[bool],
    starts: &[i64],
    rate_hz: f64,
    lost_frames: &mut u64,
    restarts: &mut u64,
) -> Indexed {
    // Per-packet: segment and index of its first sample within the segment.
    let mut seg_of = Vec::with_capacity(packets.len());
    let mut first_sample = Vec::with_capacity(packets.len());
    let (mut seg, mut n) = (0i64, 0i64);
    let packet_period = packets.first().map_or(0.0, |p| p.samples.len() as f64 / rate_hz);
    // Sequence-less streams send a fixed number of packets per device second (2 observed); more
    // means a burst (seen during PPG freezes), after which the sample count no longer tracks time.
    let per_second = (1.0 / packet_period.max(1e-9)).round() as usize;
    let mut same_second = 0usize;
    for (i, p) in packets.iter().enumerate() {
        if let Some(prev) = i.checked_sub(1).and_then(|j| packets.get(j)) {
            let forced = break_before.get(i).copied().unwrap_or(false);
            let step = match (prev.sequence, p.sequence) {
                (Some(a), Some(b)) => {
                    let d = i64::from(b.wrapping_sub(a));
                    let after_start = starts.iter().any(|s| (0..RESTART_WINDOW_NS).contains(&(p.mono - s)));
                    if d == 1 {
                        Some(1)
                    } else if b == 0 && after_start {
                        *restarts += 1;
                        Some(1)
                    } else if d > 1 && p.arrival - prev.arrival < (d as f64 + 4.0) * packet_period + 1.0 {
                        *lost_frames += (d - 1) as u64;
                        Some(d)
                    } else {
                        None
                    }
                }
                // Streams without a sequence counter: a long silence or a device clock jump starts
                // a new segment, because lost packets cannot be counted.
                _ => {
                    let gap = p.arrival - prev.arrival;
                    let clock_step = i64::from(p.device_time) - i64::from(prev.device_time);
                    same_second = if clock_step == 0 { same_second + 1 } else { 1 };
                    let burst = same_second > per_second;
                    (gap < 2.0 * packet_period.max(0.5) && (0..=1).contains(&clock_step) && !burst).then_some(1)
                }
            };
            match step {
                Some(d) if !forced => n += d * prev.samples.len() as i64,
                _ => {
                    seg += 1;
                    n = 0;
                    same_second = 1;
                }
            }
        } else {
            same_second = 1;
        }
        seg_of.push(seg);
        first_sample.push(n);
    }
    // Fit each segment on non-duplicate packets.
    let segments = seg_of.last().map_or(0, |s| s + 1);
    let mut fits = Vec::new();
    for s in 0..segments {
        let points: Vec<(f64, f64)> = packets
            .iter()
            .enumerate()
            .filter(|(i, p)| seg_of.get(*i) == Some(&s) && !is_duplicate(packets, *i) && !is_held(p))
            .map(|(i, p)| {
                (
                    (first_sample.get(i).copied().unwrap_or(0) + p.samples.len() as i64 - 1) as f64,
                    p.arrival,
                )
            })
            .collect();
        fits.push(fit_lower_envelope(&points, rate_hz));
    }
    let mut out = Indexed {
        t: vec![],
        segment: vec![],
        packet: vec![],
        sample: vec![],
        duplicate: vec![],
        held: vec![],
        fits,
    };
    for (i, p) in packets.iter().enumerate() {
        let s = seg_of.get(i).copied().unwrap_or(0);
        let fit = out.fits.get(s as usize).copied();
        let dup = is_duplicate(packets, i);
        let held = is_held(p);
        for k in 0..p.samples.len() {
            let n = first_sample.get(i).copied().unwrap_or(0) + k as i64;
            out.t.push(fit.map_or(p.arrival, |f| f.time(n as f64)));
            out.segment.push(s);
            out.packet.push(i as i64);
            out.sample.push(k as i8);
            out.duplicate.push(dup);
            out.held.push(held);
        }
    }
    out
}

/// A packet whose samples are all identical: the sensor output is frozen (seen on PPG for seconds
/// at a time while electrode contact fluctuated).
fn is_held<T: PartialEq>(p: &PacketRows<T>) -> bool {
    p.samples.len() > 1 && p.samples.windows(2).all(|w| w.first() == w.get(1))
}

/// A packet whose samples equal the previous packet's (the band has been seen to resend a block).
fn is_duplicate<T: PartialEq>(packets: &[PacketRows<T>], i: usize) -> bool {
    match (i.checked_sub(1).and_then(|j| packets.get(j)), packets.get(i)) {
        (Some(a), Some(b)) => a.samples == b.samples,
        _ => false,
    }
}

fn median(mut v: Vec<i64>) -> i64 {
    v.sort_unstable();
    v.get(v.len() / 2).copied().unwrap_or_default()
}

fn health_json(h: &Health) -> Value {
    json!({
        "notifications": h.notifications, "decode_errors": h.decode_errors,
        "irregular_eeg_frames": h.irregular_eeg_frames, "sequence_gaps": h.sequence_gaps,
        "frames_lost": h.frames_lost, "counter_restarts": h.counter_restarts,
    })
}

/// Reads a recording.
pub fn from_recording(path: &Path) -> Result<Session, Error> {
    let db = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)?;
    let mut recording_meta = Map::new();
    for row in db
        .prepare("SELECT key, value FROM meta")?
        .query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?
    {
        let (k, v) = row?;
        recording_meta.insert(k, serde_json::from_str(&v).unwrap_or(Value::String(v)));
    }
    let notifications: Vec<(i64, i64, String, Vec<u8>)> = db
        .prepare("SELECT mono_ns, wall_ns, char, payload FROM notifications ORDER BY id")?
        .query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)))?
        .collect::<Result<_, _>>()?;
    let starts: Vec<i64> = db
        .prepare("SELECT mono_ns FROM writes WHERE hex(payload) = '03FF01' ORDER BY id")?
        .query_map([], |r| r.get(0))?
        .collect::<Result<_, _>>()?;
    let markers: Vec<(i64, String)> = db
        .prepare("SELECT mono_ns, label FROM markers ORDER BY id")?
        .query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?
        .collect::<Result<_, _>>()?;
    let writes: Vec<(i64, String, Vec<u8>)> = db
        .prepare("SELECT mono_ns, label, payload FROM writes ORDER BY id")?
        .query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))?
        .collect::<Result<_, _>>()?;

    // One fixed monotonic→Unix mapping per recording (wall clock may jump; monotonic does not).
    let offset_ns = median(notifications.iter().map(|(m, w, _, _)| w - m).collect());
    let unix = |mono: i64| (mono + offset_ns) as f64 / 1e9;

    let (mut decoder, mut health, mut events) = (Decoder::default(), Health::default(), Vec::new());
    let mut eeg: Vec<PacketRows<[i32; 6]>> = Vec::new();
    let mut eeg_flags: Vec<[u8; 9]> = Vec::new();
    let mut eeg_break = Vec::new();
    let mut pending_break = false;
    let mut accel: Vec<PacketRows<[i16; 3]>> = Vec::new();
    let mut gyro: Vec<PacketRows<[i16; 3]>> = Vec::new();
    let mut ppg: Vec<PacketRows<[u32; 3]>> = Vec::new();
    let mut vitals = (Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new());
    let mut battery = (Vec::new(), Vec::new(), Vec::new());
    let mut contact = (Vec::new(), Vec::new(), Vec::new());
    let mut status = (Vec::new(), Vec::new(), Vec::new());
    for (mono, _, uuid, bytes) in &notifications {
        let Some(c) = uuid::Uuid::parse_str(uuid)
            .ok()
            .and_then(|u| Characteristic::from_uuid(u.as_u128()))
        else {
            continue;
        };
        let last_start = starts
            .iter()
            .rev()
            .find(|s| **s <= *mono)
            .copied()
            .unwrap_or(i64::MIN / 2);
        decoder.handle(*mono, c, bytes, last_start, &mut health, &mut events);
        events.clear();
        let arrival = unix(*mono);
        match Packet::decode(c, bytes) {
            Ok(Packet::Eeg(e)) => {
                eeg_break.push(std::mem::take(&mut pending_break));
                eeg_flags.push(e.flags);
                eeg.push(PacketRows {
                    arrival,
                    mono: *mono,
                    device_time: e.device_time,
                    sequence: Some(e.sequence),
                    samples: e.samples.to_vec(),
                });
            }
            Err(frenz_proto::ParseError::EegFrameType(_) | frenz_proto::ParseError::EegUnsequenced) => {
                pending_break = true
            }
            Ok(Packet::Accelerometer(m)) => accel.push(PacketRows {
                arrival,
                mono: *mono,
                device_time: m.device_time,
                sequence: None,
                samples: m.samples.to_vec(),
            }),
            Ok(Packet::Gyroscope(m)) => gyro.push(PacketRows {
                arrival,
                mono: *mono,
                device_time: m.device_time,
                sequence: None,
                samples: m.samples.to_vec(),
            }),
            Ok(Packet::Ppg(p)) => ppg.push(PacketRows {
                arrival,
                mono: *mono,
                device_time: p.device_time,
                sequence: None,
                samples: p.samples().to_vec(),
            }),
            Ok(Packet::HeartRate(v) | Packet::Spo2(v)) => {
                vitals.0.push(arrival);
                vitals.1.push(i64::from(v.device_time));
                vitals.2.push(if c == Characteristic::HeartRate {
                    "heart_rate".to_owned()
                } else {
                    "spo2".to_owned()
                });
                vitals.3.push(v.value);
                vitals.4.push(v.check);
            }
            Ok(Packet::Battery(b)) => {
                battery.0.push(arrival);
                battery.1.push(i64::from(b.device_time));
                battery.2.push(b.percent);
            }
            Ok(Packet::Contact(k)) => {
                contact.0.push(arrival);
                contact.1.push(i64::from(k.device_time));
                contact.2.push(k.state);
            }
            Ok(Packet::Status(s)) => {
                status.0.push(arrival);
                status.1.push(format!("{:?}", s.kind()));
                status
                    .2
                    .push(s.bytes().iter().map(|b| format!("{b:02x}")).collect::<String>());
            }
            Ok(Packet::Response(r)) => {
                status.0.push(arrival);
                status.1.push(format!("response_0x{:02x}", r.opcode));
                status
                    .2
                    .push(r.payload().iter().map(|b| format!("{b:02x}")).collect::<String>());
            }
            Err(_) => {}
        }
    }

    let mut session = Session::new(
        json!({"kind": "openfrenz-recording", "file": path.file_name().map(|f| f.to_string_lossy().to_string())}),
    );
    session
        .meta
        .insert("recording_meta".into(), Value::Object(recording_meta));
    session.meta.insert("clock".into(), json!({"mono_to_unix_offset_ns": offset_ns, "time_column": "estimated acquisition time, Unix seconds (host clock)"}));
    session.meta.insert("health".into(), health_json(&health));
    let mut streams = Map::new();

    // EEG
    if !eeg.is_empty() {
        let (mut lost, mut restarts) = (0, 0);
        let ix = index_and_time(&eeg, &eeg_break, &starts, 125.0, &mut lost, &mut restarts);
        let slot = |s: usize| -> Vec<i32> {
            eeg.iter()
                .flat_map(|p| p.samples.iter().map(move |r| r.get(s).copied().unwrap_or(0)))
                .collect()
        };
        let inactive_nonzero = [2usize, 5].iter().any(|s| slot(*s).iter().any(|v| *v != 0));
        let mut table = Table::default()
            .with("t", Column::F64(ix.t))
            .with("segment", Column::I64(ix.segment))
            .with("packet", Column::I64(ix.packet))
            .with("sample", Column::I8(ix.sample))
            .with(
                "sequence",
                Column::I16(
                    eeg.iter()
                        .flat_map(|p| std::iter::repeat_n(i16::from(p.sequence.unwrap_or(0)), p.samples.len()))
                        .collect(),
                ),
            )
            .with(
                "device_time",
                Column::I64(
                    eeg.iter()
                        .flat_map(|p| std::iter::repeat_n(i64::from(p.device_time), p.samples.len()))
                        .collect(),
                ),
            )
            .with(
                "arrival",
                Column::F64(
                    eeg.iter()
                        .flat_map(|p| std::iter::repeat_n(p.arrival, p.samples.len()))
                        .collect(),
                ),
            );
        for (slot_index, name) in frenz_proto::EegPacket::ACTIVE_CHANNELS {
            table = table.with(name, Column::I32(slot(slot_index)));
        }
        if inactive_nonzero {
            table = table
                .with("slot2", Column::I32(slot(2)))
                .with("slot5", Column::I32(slot(5)));
        }
        table = table
            .with(
                "flag",
                Column::U8(eeg_flags.iter().flat_map(|f| f.iter().copied()).collect()),
            )
            .with("duplicate", Column::Bool(ix.duplicate.clone()))
            .with("held", Column::Bool(ix.held.clone()));
        let duplicates = ix.duplicate.iter().filter(|d| **d).count() / 9;
        streams.insert("eeg".into(), json!({
            "nominal_rate_hz": 125.0, "units": "raw ADC counts (µV scale unknown)", "channels": eeg_channel_json(),
            "inactive_slots_nonzero": inactive_nonzero, "segments": ix.fits.iter().map(ClockFit::to_json).collect::<Vec<_>>(),
            "frames_lost_within_segments": lost, "counter_restarts": restarts, "duplicate_packets": duplicates,
            "held_packets": ix.held.iter().filter(|h| **h).count() / 9,
        }));
        session.tables.insert("eeg", table);
    }
    // Accelerometer and gyroscope
    for (name, packets) in [("accel", &accel), ("gyro", &gyro)] {
        if packets.is_empty() {
            continue;
        }
        let (mut lost, mut restarts) = (0, 0);
        let breaks = vec![false; packets.len()];
        let ix = index_and_time(packets, &breaks, &starts, 50.0, &mut lost, &mut restarts);
        let axis = |a: usize| -> Vec<i16> {
            packets
                .iter()
                .flat_map(|p| p.samples.iter().map(move |s| s.get(a).copied().unwrap_or(0)))
                .collect()
        };
        let duplicates = ix.duplicate.iter().filter(|d| **d).count() / 25;
        let table = Table::default()
            .with("t", Column::F64(ix.t))
            .with("segment", Column::I64(ix.segment))
            .with("packet", Column::I64(ix.packet))
            .with("sample", Column::I8(ix.sample))
            .with(
                "device_time",
                Column::I64(
                    packets
                        .iter()
                        .flat_map(|p| std::iter::repeat_n(i64::from(p.device_time), 25))
                        .collect(),
                ),
            )
            .with(
                "arrival",
                Column::F64(
                    packets
                        .iter()
                        .flat_map(|p| std::iter::repeat_n(p.arrival, 25))
                        .collect(),
                ),
            )
            .with("x", Column::I16(axis(0)))
            .with("y", Column::I16(axis(1)))
            .with("z", Column::I16(axis(2)))
            .with("duplicate", Column::Bool(ix.duplicate))
            .with("held", Column::Bool(ix.held.clone()));
        let mut info = motion_json(name == "accel");
        if let Some(obj) = info.as_object_mut() {
            obj.insert(
                "segments".into(),
                json!(ix.fits.iter().map(ClockFit::to_json).collect::<Vec<_>>()),
            );
            obj.insert("duplicate_packets".into(), json!(duplicates));
            obj.insert(
                "held_packets".into(),
                json!(ix.held.iter().filter(|h| **h).count() / 25),
            );
        }
        streams.insert(name.into(), info);
        session.tables.insert(name, table);
    }
    // PPG
    if !ppg.is_empty() {
        let (mut lost, mut restarts) = (0, 0);
        let breaks = vec![false; ppg.len()];
        let ix = index_and_time(&ppg, &breaks, &starts, 25.0, &mut lost, &mut restarts);
        let ch = |c: usize| -> Vec<u32> {
            ppg.iter()
                .flat_map(|p| p.samples.iter().map(move |s| s.get(c).copied().unwrap_or(0)))
                .collect()
        };
        let table = Table::default()
            .with("t", Column::F64(ix.t))
            .with("segment", Column::I64(ix.segment))
            .with("packet", Column::I64(ix.packet))
            .with("sample", Column::I8(ix.sample))
            .with(
                "device_time",
                Column::I64(
                    ppg.iter()
                        .flat_map(|p| std::iter::repeat_n(i64::from(p.device_time), p.samples.len()))
                        .collect(),
                ),
            )
            .with(
                "arrival",
                Column::F64(
                    ppg.iter()
                        .flat_map(|p| std::iter::repeat_n(p.arrival, p.samples.len()))
                        .collect(),
                ),
            )
            .with("tag1", Column::U32(ch(0)))
            .with("tag2", Column::U32(ch(1)))
            .with("tag3", Column::U32(ch(2)))
            .with("duplicate", Column::Bool(ix.duplicate))
            .with("held", Column::Bool(ix.held.clone()));
        let held_samples = ix.held.iter().filter(|h| **h).count();
        streams.insert("ppg".into(), json!({
            "held_samples": held_samples,
            "nominal_rate_hz": 25.0, "units": "raw 19-bit optical counts",
            "channels": {"tag1": "always 0 on verified hardware", "tag2": "optical channel (LED unverified)", "tag3": "optical channel (LED unverified)"},
            "segments": ix.fits.iter().map(ClockFit::to_json).collect::<Vec<_>>(),
        }));
        session.tables.insert("ppg", table);
    }
    if !vitals.0.is_empty() {
        session.tables.insert(
            "vitals",
            Table::default()
                .with("t", Column::F64(vitals.0))
                .with("device_time", Column::I64(vitals.1))
                .with("kind", Column::Str(vitals.2))
                .with("value", Column::U8(vitals.3))
                .with("check", Column::U8(vitals.4)),
        );
    }
    if !battery.0.is_empty() {
        session.tables.insert(
            "battery",
            Table::default()
                .with("t", Column::F64(battery.0))
                .with("device_time", Column::I64(battery.1))
                .with("percent", Column::U8(battery.2)),
        );
    }
    if !contact.0.is_empty() {
        session.tables.insert(
            "contact",
            Table::default()
                .with("t", Column::F64(contact.0))
                .with("device_time", Column::I64(contact.1))
                .with("state", Column::U8(contact.2)),
        );
    }
    if !status.0.is_empty() {
        session.tables.insert(
            "status",
            Table::default()
                .with("t", Column::F64(status.0))
                .with("kind", Column::Str(status.1))
                .with("hex", Column::Str(status.2)),
        );
    }
    let mut mt = (Vec::new(), Vec::new());
    for (m, label) in &markers {
        mt.0.push(unix(*m));
        mt.1.push(label.clone());
    }
    for (m, label, bytes) in &writes {
        mt.0.push(unix(*m));
        mt.1.push(format!(
            "write:{label}:{}",
            bytes.iter().map(|b| format!("{b:02x}")).collect::<String>()
        ));
    }
    if !mt.0.is_empty() {
        let mut order: Vec<usize> = (0..mt.0.len()).collect();
        order.sort_by(|a, b| {
            mt.0.get(*a)
                .copied()
                .unwrap_or(0.0)
                .total_cmp(&mt.0.get(*b).copied().unwrap_or(0.0))
        });
        session.tables.insert(
            "markers",
            Table::default()
                .with(
                    "t",
                    Column::F64(order.iter().filter_map(|i| mt.0.get(*i).copied()).collect()),
                )
                .with(
                    "label",
                    Column::Str(order.iter().filter_map(|i| mt.1.get(*i).cloned()).collect()),
                ),
        );
    }
    session.meta.insert("streams".into(), Value::Object(streams));
    Ok(session)
}