nex5file 1.2.1

Read and write NeuroExplorer .nex and .nex5 files
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
//! Zero-copy views over memory-mapped variable payloads (`mmap` feature).

use crate::error::{NexError, Result};
use crate::file_data::FileData;
use crate::format::VariableHeader;
use crate::variables::{NexFileVarType, Variable};

/// Raw timestamp bytes for a spike/event/marker/waveform variable (no decode allocation).
#[derive(Debug, Clone, Copy)]
pub struct MmapTimestampsView<'a> {
    pub raw: &'a [u8],
    pub ts_data_type: i32,
    pub count: usize,
    pub freq_hz: f64,
}

impl<'a> MmapTimestampsView<'a> {
    pub fn iter_seconds(self) -> MmapTimestampIter<'a> {
        MmapTimestampIter {
            raw: self.raw,
            ts_data_type: self.ts_data_type,
            freq_hz: self.freq_hz,
            index: 0,
            count: self.count,
        }
    }

    pub fn get_seconds(&self, index: usize) -> Option<f64> {
        if index >= self.count {
            return None;
        }
        let item_size = if self.ts_data_type == 0 { 4 } else { 8 };
        let start = index * item_size;
        let end = start + item_size;
        let ticks = if self.ts_data_type == 0 {
            i32::from_le_bytes(self.raw.get(start..end)?.try_into().ok()?) as f64
        } else {
            i64::from_le_bytes(self.raw.get(start..end)?.try_into().ok()?) as f64
        };
        Some(ticks / self.freq_hz)
    }

    pub fn len(&self) -> usize {
        self.count
    }

    pub fn is_empty(&self) -> bool {
        self.count == 0
    }
}

/// Lazily decode timestamps from a mmap slice (one `f64` at a time).
#[derive(Debug, Clone)]
pub struct MmapTimestampIter<'a> {
    raw: &'a [u8],
    ts_data_type: i32,
    freq_hz: f64,
    index: usize,
    count: usize,
}

impl Iterator for MmapTimestampIter<'_> {
    type Item = f64;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.count {
            return None;
        }
        let item_size = if self.ts_data_type == 0 { 4 } else { 8 };
        let start = self.index * item_size;
        let end = start + item_size;
        let ticks = if self.ts_data_type == 0 {
            i32::from_le_bytes(self.raw.get(start..end)?.try_into().ok()?) as f64
        } else {
            i64::from_le_bytes(self.raw.get(start..end)?.try_into().ok()?) as f64
        };
        self.index += 1;
        Some(ticks / self.freq_hz)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let rem = self.count.saturating_sub(self.index);
        (rem, Some(rem))
    }
}

impl ExactSizeIterator for MmapTimestampIter<'_> {}

/// Zero-copy view of flat waveform sample bytes (`f32` or scaled `i16` in file).
#[derive(Debug, Clone, Copy)]
pub struct MmapWaveformSamplesView<'a> {
    pub raw: &'a [u8],
    pub cont_data_type: i32,
    pub n_points: u64,
    pub count: u64,
    pub ad_to_mv: f64,
    pub mv_offset: f64,
}

impl<'a> MmapWaveformSamplesView<'a> {
    pub fn wave_bytes(&self, index: usize) -> Option<&'a [u8]> {
        let n = self.n_points as usize;
        let item = self.bytes_per_sample();
        let start = index.checked_mul(n)?.checked_mul(item)?;
        self.raw.get(start..start + n * item)
    }

    /// Decode one waveform into millivolt `f32` values (allocates `n_points` floats).
    pub fn wave_f32(&self, index: usize) -> Option<Vec<f32>> {
        let bytes = self.wave_bytes(index)?;
        Some(decode_cont_bytes(
            bytes,
            self.cont_data_type,
            self.ad_to_mv,
            self.mv_offset,
        ))
    }

    pub fn iter_waves(self) -> MmapWaveIter<'a> {
        MmapWaveIter {
            view: self,
            index: 0,
        }
    }

    pub fn bytes_per_sample(&self) -> usize {
        if self.cont_data_type == 0 {
            2
        } else {
            4
        }
    }

    pub fn num_waves(&self) -> usize {
        self.count as usize
    }
}

/// Lazily decode individual waveforms from a mmap slice.
#[derive(Debug, Clone, Copy)]
pub struct MmapWaveIter<'a> {
    view: MmapWaveformSamplesView<'a>,
    index: usize,
}

impl Iterator for MmapWaveIter<'_> {
    type Item = Vec<f32>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.view.num_waves() {
            return None;
        }
        let wave = self.view.wave_f32(self.index)?;
        self.index += 1;
        Some(wave)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let rem = self.view.num_waves().saturating_sub(self.index);
        (rem, Some(rem))
    }
}

impl ExactSizeIterator for MmapWaveIter<'_> {}

/// Zero-copy view of continuous A/D sample bytes (after fragment metadata).
#[derive(Debug, Clone, Copy)]
pub struct MmapContinuousSamplesView<'a> {
    pub raw: &'a [u8],
    pub cont_data_type: i32,
    pub num_samples: usize,
    pub ad_to_mv: f64,
    pub mv_offset: f64,
}

impl<'a> MmapContinuousSamplesView<'a> {
    pub fn bytes_per_sample(&self) -> usize {
        if self.cont_data_type == 0 {
            2
        } else {
            4
        }
    }

    pub fn sample_f32(&self, index: usize) -> Option<f32> {
        let item = self.bytes_per_sample();
        let start = index.checked_mul(item)?;
        let end = start + item;
        decode_cont_sample(self.raw.get(start..end)?, self.cont_data_type, self.ad_to_mv, self.mv_offset)
    }

    pub fn iter_samples(self) -> MmapContinuousSampleIter<'a> {
        MmapContinuousSampleIter {
            view: self,
            index: 0,
        }
    }
}

/// Lazily decode continuous samples from a mmap slice.
#[derive(Debug, Clone, Copy)]
pub struct MmapContinuousSampleIter<'a> {
    view: MmapContinuousSamplesView<'a>,
    index: usize,
}

impl Iterator for MmapContinuousSampleIter<'_> {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.view.num_samples {
            return None;
        }
        let v = self.view.sample_f32(self.index)?;
        self.index += 1;
        Some(v)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let rem = self.view.num_samples.saturating_sub(self.index);
        (rem, Some(rem))
    }
}

impl ExactSizeIterator for MmapContinuousSampleIter<'_> {}

/// Resolved layout for a variable payload inside a mmap'd file.
#[derive(Debug, Clone)]
pub enum MmapVariableView<'a> {
    Timestamps(MmapTimestampsView<'a>),
    Waveform {
        timestamps: MmapTimestampsView<'a>,
        samples: MmapWaveformSamplesView<'a>,
    },
    Continuous {
        fragment_timestamps: MmapTimestampsView<'a>,
        payload: &'a [u8],
        samples: MmapContinuousSamplesView<'a>,
    },
    Opaque(&'a [u8]),
}

impl MmapVariableView<'_> {
    pub fn variable_name_hint(&self, fallback: &str) -> String {
        let _ = self;
        fallback.to_string()
    }
}

/// Resolve a zero-copy payload view for `name` inside `file_bytes`.
pub fn mmap_view_for_variable<'a>(
    file_bytes: &'a [u8],
    data: &FileData,
    name: &str,
) -> Result<MmapVariableView<'a>> {
    let idx = data.index_of(name)?;
    let var = &data.variables[idx];
    let hw = var.header();
    if hw.data_offset as usize >= file_bytes.len() {
        return Err(NexError::InvalidDataOffset {
            name: name.to_string(),
            offset: hw.data_offset,
            file_size: file_bytes.len() as u64,
        });
    }
    let base = hw.data_offset as usize;

    match var {
        Variable::Neuron(_) | Variable::Event(_) | Variable::Marker(_) => {
            Ok(MmapVariableView::Timestamps(ts_view(
                file_bytes, base, hw, data.timestamp_frequency_hz, name,
            )?))
        }
        Variable::Waveform(_) => {
            let ts = ts_view(file_bytes, base, hw, data.timestamp_frequency_hz, name)?;
            let ts_bytes = hw.bytes_in_timestamp().saturating_mul(hw.count as usize);
            let sample_start = base + ts_bytes;
            let n_samples = hw.count.saturating_mul(hw.n_points_wave) as usize;
            let sample_bytes = n_samples.saturating_mul(hw.bytes_in_cont_value());
            let samples_raw = file_bytes
                .get(sample_start..sample_start + sample_bytes)
                .ok_or_else(|| NexError::InvalidDataOffset {
                    name: name.to_string(),
                    offset: sample_start as u64,
                    file_size: file_bytes.len() as u64,
                })?;
            Ok(MmapVariableView::Waveform {
                timestamps: ts,
                samples: MmapWaveformSamplesView {
                    raw: samples_raw,
                    cont_data_type: hw.cont_data_type,
                    n_points: hw.n_points_wave,
                    count: hw.count,
                    ad_to_mv: hw.ad_to_mv,
                    mv_offset: hw.mv_offset,
                },
            })
        }
        Variable::Continuous(_) => {
            let frag_count = hw.count as usize;
            let ts_bytes = hw.bytes_in_timestamp().saturating_mul(frag_count);
            let idx_bytes = hw.bytes_in_fragment_index().saturating_mul(frag_count);
            let meta_end = base + ts_bytes + idx_bytes;
            let num_samples = hw.n_points_wave as usize;
            let sample_bytes = num_samples.saturating_mul(hw.bytes_in_cont_value());
            let payload_end = meta_end + sample_bytes;
            let payload = file_bytes
                .get(base..payload_end)
                .ok_or_else(|| NexError::InvalidDataOffset {
                    name: name.to_string(),
                    offset: hw.data_offset,
                    file_size: file_bytes.len() as u64,
                })?;
            let samples_raw = payload
                .get(ts_bytes + idx_bytes..)
                .ok_or_else(|| NexError::InvalidDataOffset {
                    name: name.to_string(),
                    offset: meta_end as u64,
                    file_size: file_bytes.len() as u64,
                })?;
            Ok(MmapVariableView::Continuous {
                fragment_timestamps: ts_view(
                    file_bytes,
                    base,
                    hw,
                    data.timestamp_frequency_hz,
                    name,
                )?,
                payload,
                samples: MmapContinuousSamplesView {
                    raw: samples_raw,
                    cont_data_type: hw.cont_data_type,
                    num_samples,
                    ad_to_mv: hw.ad_to_mv,
                    mv_offset: hw.mv_offset,
                },
            })
        }
        _ => {
            let end = file_bytes.len().min(base + payload_size_estimate(hw));
            Ok(MmapVariableView::Opaque(
                file_bytes
                    .get(base..end)
                    .ok_or_else(|| NexError::InvalidDataOffset {
                        name: name.to_string(),
                        offset: hw.data_offset,
                        file_size: file_bytes.len() as u64,
                    })?,
            ))
        }
    }
}

fn ts_view<'a>(
    file_bytes: &'a [u8],
    base: usize,
    hw: &VariableHeader,
    freq_hz: f64,
    name: &str,
) -> Result<MmapTimestampsView<'a>> {
    let ts_bytes = hw.bytes_in_timestamp().saturating_mul(hw.count as usize);
    let raw = file_bytes
        .get(base..base + ts_bytes)
        .ok_or_else(|| NexError::InvalidDataOffset {
            name: name.to_string(),
            offset: hw.data_offset,
            file_size: file_bytes.len() as u64,
        })?;
    Ok(MmapTimestampsView {
        raw,
        ts_data_type: hw.ts_data_type,
        count: hw.count as usize,
        freq_hz,
    })
}

fn decode_cont_bytes(raw: &[u8], cont_data_type: i32, ad_to_mv: f64, mv_offset: f64) -> Vec<f32> {
    if cont_data_type == 0 {
        raw.chunks_exact(2)
            .map(|chunk| {
                decode_cont_sample(chunk, cont_data_type, ad_to_mv, mv_offset).unwrap_or(0.0)
            })
            .collect()
    } else {
        raw.chunks_exact(4)
            .map(|chunk| {
                decode_cont_sample(chunk, cont_data_type, ad_to_mv, mv_offset).unwrap_or(0.0)
            })
            .collect()
    }
}

fn decode_cont_sample(
    bytes: &[u8],
    cont_data_type: i32,
    ad_to_mv: f64,
    mv_offset: f64,
) -> Option<f32> {
    if cont_data_type == 0 {
        let raw = i16::from_le_bytes(bytes.try_into().ok()?);
        let scale = if ad_to_mv > 0.0 { ad_to_mv } else { 1.0 };
        Some((f64::from(raw) * scale + mv_offset) as f32)
    } else {
        Some(f32::from_le_bytes(bytes.try_into().ok()?))
    }
}

fn payload_size_estimate(hw: &VariableHeader) -> usize {
    match NexFileVarType::from_i32(hw.var_type) {
        Some(NexFileVarType::Interval) => hw.bytes_in_timestamp() * hw.count as usize * 2,
        Some(NexFileVarType::PopulationVector) => 8 * hw.count as usize,
        _ => hw.bytes_in_timestamp() * hw.count as usize,
    }
}