nex5file 1.2.0

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
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
use crate::compat::{vec, HashMap, String, ToString, Vec};
use crate::error::{NexError, Result};
use crate::format::VariableHeader;
use crate::variables::{
    ContinuousVariable, EventVariable, IntervalVariable, MarkerFieldValue, MarkerVariable,
    NeuronVariable, NexFileVarType, PopulationVector, Timestamps, Variable, WaveformVariable,
};
use core::ops::{Index, IndexMut};
use serde_json::Value as JsonValue;

/// In-memory representation of a .nex or .nex5 file, mirroring the Python `FileData` class.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct FileData {
    pub timestamp_frequency_hz: f64,
    pub comment: String,
    pub beg_seconds: f64,
    pub end_seconds: f64,
    pub metadata: JsonValue,
    pub variables: Vec<Variable>,
    #[serde(skip)]
    name_index: HashMap<String, usize>,
}

impl<'de> serde::Deserialize<'de> for FileData {
    fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        struct Helper {
            timestamp_frequency_hz: f64,
            comment: String,
            beg_seconds: f64,
            end_seconds: f64,
            metadata: JsonValue,
            variables: Vec<Variable>,
        }
        let h = Helper::deserialize(deserializer)?;
        let mut data = Self {
            timestamp_frequency_hz: h.timestamp_frequency_hz,
            comment: h.comment,
            beg_seconds: h.beg_seconds,
            end_seconds: h.end_seconds,
            metadata: h.metadata,
            variables: h.variables,
            name_index: HashMap::new(),
        };
        data.rebuild_index();
        Ok(data)
    }
}

impl FileData {
    pub fn new(ts_frequency: f64, comment: impl Into<String>) -> Result<Self> {
        if ts_frequency <= 0.0 {
            return Err(NexError::InvalidTimestampFrequency);
        }
        Ok(Self::from_parts(
            ts_frequency,
            comment.into(),
            0.0,
            0.0,
            JsonValue::Object(Default::default()),
            Vec::new(),
        ))
    }

    pub(crate) fn from_header(file_header: &crate::format::FileHeader) -> Self {
        Self::from_parts(
            file_header.frequency,
            file_header.comment.clone(),
            file_header.beg_seconds,
            file_header.end_seconds,
            JsonValue::Object(Default::default()),
            Vec::new(),
        )
    }

    fn from_parts(
        timestamp_frequency_hz: f64,
        comment: String,
        beg_seconds: f64,
        end_seconds: f64,
        metadata: JsonValue,
        variables: Vec<Variable>,
    ) -> Self {
        let mut data = Self {
            timestamp_frequency_hz,
            comment,
            beg_seconds,
            end_seconds,
            metadata,
            variables,
            name_index: HashMap::new(),
        };
        data.rebuild_index();
        data
    }

    pub(crate) fn rebuild_index(&mut self) {
        self.name_index = self
            .variables
            .iter()
            .enumerate()
            .map(|(i, v)| (v.name().to_string(), i))
            .collect();
    }

    pub(crate) fn index_of(&self, name: &str) -> Result<usize> {
        self.name_index
            .get(name)
            .copied()
            .ok_or_else(|| NexError::VariableNotFound(name.to_string()))
    }

    pub fn get_variable(&self, name: &str) -> Result<&Variable> {
        Ok(&self.variables[self.index_of(name)?])
    }

    pub fn get_variable_mut(&mut self, name: &str) -> Result<&mut Variable> {
        let idx = self.index_of(name)?;
        Ok(&mut self.variables[idx])
    }

    pub fn event(&self, name: &str) -> Result<&EventVariable> {
        match self.get_variable(name)? {
            Variable::Event(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "event",
            )),
        }
    }

    pub fn neuron(&self, name: &str) -> Result<&NeuronVariable> {
        match self.get_variable(name)? {
            Variable::Neuron(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "neuron",
            )),
        }
    }

    pub fn interval(&self, name: &str) -> Result<&IntervalVariable> {
        match self.get_variable(name)? {
            Variable::Interval(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "interval",
            )),
        }
    }

    pub fn marker(&self, name: &str) -> Result<&MarkerVariable> {
        match self.get_variable(name)? {
            Variable::Marker(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "marker",
            )),
        }
    }

    pub fn waveform(&self, name: &str) -> Result<&WaveformVariable> {
        match self.get_variable(name)? {
            Variable::Waveform(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "waveform",
            )),
        }
    }

    pub fn continuous(&self, name: &str) -> Result<&ContinuousVariable> {
        match self.get_variable(name)? {
            Variable::Continuous(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "continuous",
            )),
        }
    }

    pub fn population_vector(&self, name: &str) -> Result<&PopulationVector> {
        match self.get_variable(name)? {
            Variable::PopulationVector(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "population_vector",
            )),
        }
    }

    pub fn event_mut(&mut self, name: &str) -> Result<&mut EventVariable> {
        match self.get_variable_mut(name)? {
            Variable::Event(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "event",
            )),
        }
    }

    pub fn neuron_mut(&mut self, name: &str) -> Result<&mut NeuronVariable> {
        match self.get_variable_mut(name)? {
            Variable::Neuron(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "neuron",
            )),
        }
    }

    pub fn continuous_mut(&mut self, name: &str) -> Result<&mut ContinuousVariable> {
        match self.get_variable_mut(name)? {
            Variable::Continuous(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "continuous",
            )),
        }
    }

    pub fn waveform_mut(&mut self, name: &str) -> Result<&mut WaveformVariable> {
        match self.get_variable_mut(name)? {
            Variable::Waveform(v) => Ok(v),
            other => Err(NexError::WrongVariableType(
                other.name().to_string(),
                "waveform",
            )),
        }
    }

    pub fn is_variable_loaded(&self, name: &str) -> Result<bool> {
        Ok(self.get_variable(name)?.is_loaded())
    }

    pub fn delete_variable(&mut self, name: &str) -> Result<()> {
        let pos = self.index_of(name)?;
        self.variables.remove(pos);
        self.rebuild_index();
        Ok(())
    }

    pub fn doc_comment(&self) -> &str {
        &self.comment
    }

    pub fn timestamp_frequency(&self) -> f64 {
        self.timestamp_frequency_hz
    }

    pub fn doc_start_time(&self) -> f64 {
        self.beg_seconds
    }

    pub fn doc_end_time(&self) -> f64 {
        self.end_seconds
    }

    fn var_names(&self, var_type: NexFileVarType) -> Vec<String> {
        self.variables
            .iter()
            .filter(|v| v.var_type() == var_type as i32)
            .map(|v| v.name().to_string())
            .collect()
    }

    pub fn neuron_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::Neuron)
    }

    pub fn event_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::Event)
    }

    pub fn interval_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::Interval)
    }

    pub fn wave_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::Waveform)
    }

    pub fn marker_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::Marker)
    }

    pub fn continuous_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::Continuous)
    }

    pub fn pop_vector_names(&self) -> Vec<String> {
        self.var_names(NexFileVarType::PopulationVector)
    }

    /// All variable names in file order.
    pub fn variable_names(&self) -> Vec<String> {
        self.variables.iter().map(|v| v.name().to_string()).collect()
    }

    pub(crate) fn maximum_timestamp(&self) -> f64 {
        self.variables
            .iter()
            .map(|v| v.maximum_timestamp())
            .fold(0.0, f64::max)
    }

    fn add_variable(&mut self, var: Variable) -> Result<()> {
        if var.var_type() < 0 || var.name().is_empty() {
            return Err(NexError::InvalidVariable);
        }
        let name = var.name().to_string();
        if self.name_index.contains_key(&name) {
            return Err(NexError::DuplicateVariable(name));
        }
        let idx = self.variables.len();
        self.variables.push(var);
        self.name_index.insert(name, idx);
        self.end_seconds = self.maximum_timestamp();
        Ok(())
    }

    /// Rename a variable (updates the name index).
    pub fn rename_variable(&mut self, old_name: &str, new_name: impl Into<String>) -> Result<()> {
        let new_name = new_name.into();
        if new_name.is_empty() {
            return Err(NexError::InvalidVariable);
        }
        if self.name_index.contains_key(&new_name) {
            return Err(NexError::DuplicateVariable(new_name));
        }
        let idx = self.index_of(old_name)?;
        self.variables[idx].header_mut().name = new_name.clone();
        self.name_index.remove(old_name);
        self.name_index.insert(new_name, idx);
        Ok(())
    }

    /// Shallow copy with only the listed variables (headers + loaded payloads).
    pub fn subset(&self, names: &[impl AsRef<str>]) -> Result<Self> {
        let mut out = Self::new(self.timestamp_frequency_hz, &self.comment)?;
        out.beg_seconds = self.beg_seconds;
        out.end_seconds = self.end_seconds;
        out.metadata = self.metadata.clone();
        for name in names {
            let name = name.as_ref();
            let var = self.get_variable(name)?.clone();
            out.add_variable(var)?;
        }
        Ok(out)
    }

    /// Append variables from `other` (frequencies must match).
    pub fn merge(&mut self, other: &FileData) -> Result<()> {
        if (self.timestamp_frequency_hz - other.timestamp_frequency_hz).abs() > f64::EPSILON {
            return Err(NexError::InvalidTimestampFrequency);
        }
        if self.comment.is_empty() && !other.comment.is_empty() {
            self.comment = other.comment.clone();
        }
        self.beg_seconds = self.beg_seconds.min(other.beg_seconds);
        for var in &other.variables {
            self.add_variable(var.clone())?;
        }
        self.end_seconds = self.maximum_timestamp();
        Ok(())
    }

    pub fn add_event(&mut self, ev_name: impl Into<String>, ev_timestamps: Vec<f64>) -> Result<()> {
        let name = ev_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Event as i32,
            name,
            ..Default::default()
        };
        let mut ev = EventVariable::new(header);
        ev.timestamps = Timestamps::from(ev_timestamps);
        self.add_variable(Variable::Event(ev))
    }

    pub fn add_neuron(
        &mut self,
        nr_name: impl Into<String>,
        nr_timestamps: Vec<f64>,
        wire: i32,
        unit: i32,
        x_position: f64,
        y_position: f64,
    ) -> Result<()> {
        let name = nr_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Neuron as i32,
            name,
            wire,
            unit,
            x_pos: x_position,
            y_pos: y_position,
            ..Default::default()
        };
        let mut nr = NeuronVariable::new(header);
        nr.timestamps = Timestamps::from(nr_timestamps);
        self.add_variable(Variable::Neuron(nr))
    }

    pub fn add_interval_as_pairs_start_end(
        &mut self,
        int_name: impl Into<String>,
        intervals_as_pairs: &[(f64, f64)],
    ) -> Result<()> {
        let name = int_name.into();
        let (starts, ends): (Vec<f64>, Vec<f64>) = intervals_as_pairs.iter().copied().unzip();
        crate::validation::validate_intervals(&starts, &ends)?;
        let header = VariableHeader {
            var_type: NexFileVarType::Interval as i32,
            name,
            ..Default::default()
        };
        let mut int_var = IntervalVariable::new(header);
        int_var.interval_starts = starts;
        int_var.interval_ends = ends;
        self.add_variable(Variable::Interval(int_var))
    }

    pub fn add_marker(
        &mut self,
        marker_name: impl Into<String>,
        timestamps: Vec<f64>,
        field_names: Vec<String>,
        fields: Vec<Vec<MarkerFieldValue>>,
    ) -> Result<()> {
        if field_names.len() != fields.len() {
            return Err(NexError::InvalidMarkerParameters);
        }
        for f in &fields {
            if f.len() != timestamps.len() {
                return Err(NexError::InvalidMarkerParameters);
            }
        }

        let name = marker_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Marker as i32,
            name,
            n_markers: field_names.len() as i32,
            ..Default::default()
        };
        let mut marker_var = MarkerVariable::new(header);
        marker_var.timestamps = Timestamps::from(timestamps);
        marker_var.marker_field_names = field_names;
        marker_var.marker_fields = fields;
        self.add_variable(Variable::Marker(marker_var))
    }

    pub fn add_population_vector(
        &mut self,
        name: impl Into<String>,
        weights: Vec<f64>,
    ) -> Result<()> {
        let name = name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::PopulationVector as i32,
            name,
            ..Default::default()
        };
        let mut pv = PopulationVector::new(header);
        pv.weights = weights;
        self.add_variable(Variable::PopulationVector(pv))
    }

    pub fn add_cont_var_with_floats_single_fragment(
        &mut self,
        cont_name: impl Into<String>,
        sampling_rate: f64,
        start_timestamp: f64,
        cont_values: Vec<f64>,
    ) -> Result<()> {
        if sampling_rate < 0.0 || sampling_rate > self.timestamp_frequency_hz {
            return Err(NexError::InvalidSamplingRate);
        }

        let name = cont_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Continuous as i32,
            name,
            sampling_rate,
            n_points_wave: cont_values.len() as u64,
            cont_data_type: 1,
            ad_to_mv: 1.0,
            ..Default::default()
        };
        let mut cont = ContinuousVariable::new(header);
        cont.fragment_timestamps = vec![start_timestamp];
        cont.fragment_indexes = vec![0];
        cont.continuous_values = cont_values;
        cont.calculate_fragment_counts_from_indexes();
        self.add_variable(Variable::Continuous(cont))
    }

    pub fn add_cont_single_fragment_values_int16(
        &mut self,
        cont_name: impl Into<String>,
        sampling_rate: f64,
        start_timestamp: f64,
        cont_values_as_int16: &[i16],
        raw_to_mv: f64,
        raw_offset: f64,
    ) -> Result<()> {
        if sampling_rate < 0.0 || sampling_rate > self.timestamp_frequency_hz {
            return Err(NexError::InvalidSamplingRate);
        }

        let values: Vec<f64> = cont_values_as_int16
            .iter()
            .map(|&v| v as f64 * raw_to_mv + raw_offset)
            .collect();

        let name = cont_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Continuous as i32,
            name,
            sampling_rate,
            n_points_wave: values.len() as u64,
            ad_to_mv: raw_to_mv,
            mv_offset: raw_offset,
            cont_data_type: 0,
            ..Default::default()
        };
        let mut cont = ContinuousVariable::new(header);
        cont.fragment_timestamps = vec![start_timestamp];
        cont.fragment_indexes = vec![0];
        cont.continuous_values = values;
        cont.calculate_fragment_counts_from_indexes();
        cont.hash_cont_values();
        self.add_variable(Variable::Continuous(cont))
    }

    pub fn add_cont_var_with_floats_all_timestamps(
        &mut self,
        cont_name: impl Into<String>,
        sampling_rate: f64,
        all_timestamps: Vec<f64>,
        cont_values: Vec<f64>,
    ) -> Result<()> {
        if sampling_rate < 0.0 || sampling_rate > self.timestamp_frequency_hz {
            return Err(NexError::InvalidSamplingRate);
        }
        if all_timestamps.len() != cont_values.len() {
            return Err(NexError::InvalidTimestampsAndValues);
        }

        let name = cont_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Continuous as i32,
            name,
            sampling_rate,
            n_points_wave: cont_values.len() as u64,
            cont_data_type: 1,
            ..Default::default()
        };
        let mut cont = ContinuousVariable::new(header);
        cont.fragment_timestamps = all_timestamps;
        cont.continuous_values = cont_values;
        cont.calculate_fragments_from_all_timestamps(self.timestamp_frequency_hz);
        cont.calculate_fragment_counts_from_indexes();
        self.add_variable(Variable::Continuous(cont))
    }

    pub fn add_wave_var_with_floats(
        &mut self,
        wave_name: impl Into<String>,
        sampling_rate: f64,
        timestamps: Vec<f64>,
        wave_values: Vec<Vec<f32>>,
    ) -> Result<()> {
        if sampling_rate < 0.0 || sampling_rate > self.timestamp_frequency_hz {
            return Err(NexError::InvalidSamplingRate);
        }

        let name = wave_name.into();
        let header = VariableHeader {
            var_type: NexFileVarType::Waveform as i32,
            name,
            sampling_rate,
            cont_data_type: 1,
            ..Default::default()
        };
        let mut wave = WaveformVariable::new(header);
        wave.timestamps = Timestamps::from(timestamps);
        wave.set_from_nested(wave_values)?;
        wave.assign_num_points_wave()?;
        self.add_variable(Variable::Waveform(wave))
    }
}

impl Index<&str> for FileData {
    type Output = Variable;

    fn index(&self, name: &str) -> &Self::Output {
        self.get_variable(name)
            .unwrap_or_else(|_| panic!("variable \"{name}\" not found in file data"))
    }
}

impl IndexMut<&str> for FileData {
    fn index_mut(&mut self, name: &str) -> &mut Self::Output {
        self.get_variable_mut(name)
            .unwrap_or_else(|_| panic!("variable \"{name}\" not found in file data"))
    }
}