openreadout-plexon 0.1.0

Reader for Plexon PLX and PL2 electrophysiology files, part of OpenReadout
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
//! PLX ("Plexon 1") layout: file header, channel headers and the data-block walk. Layout and
//! names: `docs/formats/plexon.md`.

use openreadout_core::source::SourceFile as File;
use std::collections::BTreeMap;
use std::path::Path;

use openreadout_core::bytes::{Block, read_block};
use openreadout_core::model::Finding;
use openreadout_core::{Error, Result};

use crate::FORMAT_ID;

/// Bytes 0–3 of a PLX file.
pub const PLX_MAGIC: [u8; 4] = *b"PLEX";
/// Length of the file header.
pub const FILE_HEADER_LEN: u64 = 7504;
/// Length of one spike-channel header.
pub const SPIKE_HEADER_LEN: u64 = 1020;
/// Length of one event-channel header.
pub const EVENT_HEADER_LEN: u64 = 296;
/// Length of one continuous-channel header.
pub const CONTINUOUS_HEADER_LEN: u64 = 296;
/// Length of a data-block header.
pub const BLOCK_HEADER_LEN: u64 = 16;
/// Block type of a spike waveform.
pub const BLOCK_SPIKE: u16 = 1;
/// Block type of an event.
pub const BLOCK_EVENT: u16 = 4;
/// Block type of a run of continuous samples.
pub const BLOCK_CONTINUOUS: u16 = 5;
/// Most channel headers of one kind accepted.
pub const MAX_CHANNEL_HEADERS: i32 = 1 << 16;

/// The file header.
#[derive(Debug, Clone, Default)]
pub struct PlxHeader {
    /// Format version (100–106 are known).
    pub version: i32,
    /// Free-text comment.
    pub comment: String,
    /// Timestamp clock, ticks per second.
    pub clock_hz: i32,
    /// Number of spike-channel headers.
    pub spike_channel_count: i32,
    /// Number of event-channel headers.
    pub event_channel_count: i32,
    /// Number of continuous-channel headers.
    pub continuous_channel_count: i32,
    /// Samples per spike waveform.
    pub waveform_points: i32,
    /// Waveform samples before the threshold crossing.
    pub pre_threshold_points: i32,
    /// Recording start, `YYYY-MM-DDThh:mm:ss` (no zone), when the date fields are valid.
    pub recorded_at: Option<String>,
    /// Waveform sampling rate (Hz; 0 in old files).
    pub waveform_rate_hz: i32,
    /// Timestamp of the last data block, in ticks, as the header records it.
    pub last_timestamp: f64,
    /// Electrodes per spike channel (1 single, 2 stereotrode, 4 tetrode; version ≥ 103).
    pub electrodes_per_channel: u8,
    /// Electrodes per spike channel in the data blocks (version ≥ 103).
    pub data_electrodes_per_channel: u8,
    /// Bits per spike sample (version ≥ 103).
    pub spike_bits: u8,
    /// Bits per continuous sample (version ≥ 103).
    pub continuous_bits: u8,
    /// Spike input range, ± mV (version ≥ 103).
    pub spike_max_mv: u16,
    /// Continuous input range, ± mV (version ≥ 103).
    pub continuous_max_mv: u16,
    /// Spike preamplifier gain (version ≥ 105).
    pub spike_preamp_gain: u16,
    /// Acquisition application (version ≥ 106).
    pub acquired_with: String,
    /// Application that last processed the file (version ≥ 106).
    pub processed_with: String,
}

/// One spike-channel header.
#[derive(Debug, Clone, Default)]
pub struct SpikeChannel {
    /// Channel name.
    pub name: String,
    /// Name of the signal the channel records.
    pub signal_name: String,
    /// Channel number used in data blocks.
    pub channel: i32,
    /// Amplifier gain.
    pub gain: i32,
    /// Filter setting.
    pub filter: i32,
    /// Detection threshold (counts).
    pub threshold: i32,
    /// Sorted units.
    pub unit_count: i32,
    /// Comment (version ≥ 105).
    pub comment: String,
}

/// One event-channel header.
#[derive(Debug, Clone, Default)]
pub struct EventChannel {
    /// Channel name.
    pub name: String,
    /// Channel number used in data blocks.
    pub channel: i32,
    /// Comment (version ≥ 105).
    pub comment: String,
}

/// One continuous-channel header.
#[derive(Debug, Clone, Default)]
pub struct ContinuousChannel {
    /// Channel name.
    pub name: String,
    /// Channel number used in data blocks.
    pub channel: i32,
    /// Sampling rate, Hz.
    pub rate_hz: i32,
    /// Amplifier gain.
    pub gain: i32,
    /// Recording enabled.
    pub enabled: bool,
    /// Preamplifier gain.
    pub preamp_gain: i32,
    /// Comment (version ≥ 105).
    pub comment: String,
}

/// A run of continuous samples: where its samples start and when.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SampleBlock {
    /// File offset of the first sample.
    pub offset: u64,
    /// Timestamp of the first sample, ticks.
    pub timestamp: u64,
    /// Samples in the block.
    pub samples: u32,
}

/// One event block.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EventRecord {
    /// Timestamp, ticks.
    pub timestamp: u64,
    /// Event channel number.
    pub channel: u16,
    /// Event value (a strobed word, or 0).
    pub value: u16,
}

/// A gap-free run of one continuous channel's blocks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Run {
    /// Index of the first block (into the channel's block list).
    pub first_block: usize,
    /// Blocks in the run.
    pub block_count: usize,
    /// Samples in the run.
    pub samples: u64,
    /// Timestamp of the first sample, ticks.
    pub timestamp: u64,
}

/// Everything found by walking the data blocks.
#[derive(Debug, Clone, Default)]
pub struct PlxIndex {
    /// Continuous blocks per channel number, in file order.
    pub continuous: BTreeMap<u16, Vec<SampleBlock>>,
    /// File offsets of the spike blocks (block header), in file order.
    pub spikes: Vec<u64>,
    /// Longest spike waveform (waveforms × words), samples.
    pub max_waveform: u32,
    /// Event blocks, in file order.
    pub events: Vec<EventRecord>,
    /// Blocks per type (1, 4, 5, and any other).
    pub block_counts: BTreeMap<u16, u64>,
    /// End of the last whole block.
    pub data_end: u64,
    /// Problems found.
    pub findings: Vec<Finding>,
}

/// A parsed PLX file: header, channel headers and the block index.
#[derive(Debug, Clone, Default)]
pub struct PlxFile {
    /// File header.
    pub header: PlxHeader,
    /// Spike-channel headers.
    pub spike_channels: Vec<SpikeChannel>,
    /// Event-channel headers.
    pub event_channels: Vec<EventChannel>,
    /// Continuous-channel headers.
    pub continuous_channels: Vec<ContinuousChannel>,
    /// Offset of the first data block.
    pub data_start: u64,
    /// File length.
    pub file_len: u64,
    /// The block walk.
    pub index: PlxIndex,
}

fn need<T>(b: &Block, at: u64, read: fn(&Block, u64) -> Option<T>) -> Result<T> {
    read(b, at).ok_or_else(|| Error::corrupt_at(FORMAT_ID, at, "header cut off"))
}

/// Parse the file header (the first [`FILE_HEADER_LEN`] bytes).
pub fn parse_file_header(b: &Block) -> Result<PlxHeader> {
    if b.slice(0, 4) != Some(&PLX_MAGIC[..]) {
        return Err(Error::corrupt_at(FORMAT_ID, 0, "no PLEX signature"));
    }
    if (b.len() as u64) < FILE_HEADER_LEN {
        return Err(Error::corrupt_at(
            FORMAT_ID,
            b.len() as u64,
            format!(
                "the file is {} bytes, shorter than the {FILE_HEADER_LEN}-byte file header",
                b.len()
            ),
        ));
    }
    let version = need(b, 4, Block::i32_at)?;
    let date: Vec<i32> = (0..6)
        .map(|k| need(b, 160 + 4 * k, Block::i32_at))
        .collect::<Result<_>>()?;
    let recorded_at = ((1900..=2200).contains(&date[0])
        && (1..=12).contains(&date[1])
        && (1..=31).contains(&date[2])
        && (0..24).contains(&date[3])
        && (0..60).contains(&date[4])
        && (0..62).contains(&date[5]))
    .then(|| {
        format!(
            "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}",
            date[0], date[1], date[2], date[3], date[4], date[5]
        )
    });
    let v103 = version >= 103;
    let u8f = |at| if v103 { b.u8_at(at).unwrap_or(0) } else { 0 };
    let u16f = |at, min| {
        if version >= min {
            b.u16_at(at).unwrap_or(0)
        } else {
            0
        }
    };
    Ok(PlxHeader {
        version,
        comment: b.text_at(8, 128).unwrap_or_default(),
        clock_hz: need(b, 136, Block::i32_at)?,
        spike_channel_count: need(b, 140, Block::i32_at)?,
        event_channel_count: need(b, 144, Block::i32_at)?,
        continuous_channel_count: need(b, 148, Block::i32_at)?,
        waveform_points: need(b, 152, Block::i32_at)?,
        pre_threshold_points: need(b, 156, Block::i32_at)?,
        recorded_at,
        waveform_rate_hz: need(b, 188, Block::i32_at)?,
        last_timestamp: b.f64_at(192).unwrap_or(0.0),
        electrodes_per_channel: u8f(200),
        data_electrodes_per_channel: u8f(201),
        spike_bits: u8f(202),
        continuous_bits: u8f(203),
        spike_max_mv: u16f(204, 103),
        continuous_max_mv: u16f(206, 103),
        spike_preamp_gain: u16f(208, 105),
        acquired_with: if version >= 106 {
            b.text_at(210, 18).unwrap_or_default()
        } else {
            String::new()
        },
        processed_with: if version >= 106 {
            b.text_at(228, 18).unwrap_or_default()
        } else {
            String::new()
        },
    })
}

impl PlxHeader {
    /// mV per count of a continuous channel (Neo's formulas per version; see the format notes).
    pub fn continuous_scale(&self, c: &ContinuousChannel) -> f64 {
        let gain = f64::from(c.gain);
        match self.version {
            ..=101 => 5000.0 / (2048.0 * gain * 1000.0),
            102 => 5000.0 / (2048.0 * gain * f64::from(c.preamp_gain)),
            _ => {
                f64::from(self.continuous_max_mv)
                    / (0.5
                        * f64::from(2u32.pow(u32::from(self.spike_bits.min(31))))
                        * gain
                        * f64::from(c.preamp_gain))
            }
        }
    }

    /// mV per count of a spike channel's waveform samples.
    pub fn spike_scale(&self, c: &SpikeChannel) -> f64 {
        let gain = f64::from(c.gain);
        let full = f64::from(2u32.pow(u32::from(self.spike_bits.min(31))));
        match self.version {
            ..=102 => 3000.0 / (2048.0 * gain * 1000.0),
            103 | 104 => f64::from(self.spike_max_mv) / (0.5 * full * gain * 1000.0),
            _ => {
                f64::from(self.spike_max_mv)
                    / (0.5 * full * gain * f64::from(self.spike_preamp_gain))
            }
        }
    }
}

/// Parse the headers and walk every data block.
pub fn parse_plx(f: &mut File, path: &Path, file_len: u64) -> Result<PlxFile> {
    let hb = read_block(f, path, 0, FILE_HEADER_LEN, file_len)?;
    let header = parse_file_header(&hb)?;
    for (n, what) in [
        (header.spike_channel_count, "spike"),
        (header.event_channel_count, "event"),
        (header.continuous_channel_count, "continuous"),
    ] {
        if !(0..=MAX_CHANNEL_HEADERS).contains(&n) {
            return Err(Error::corrupt_at(
                FORMAT_ID,
                140,
                format!("{n} {what} channel headers"),
            ));
        }
    }
    let ns = u64::try_from(header.spike_channel_count).unwrap_or(0);
    let ne = u64::try_from(header.event_channel_count).unwrap_or(0);
    let nc = u64::try_from(header.continuous_channel_count).unwrap_or(0);
    let data_start = FILE_HEADER_LEN
        + ns * SPIKE_HEADER_LEN
        + ne * EVENT_HEADER_LEN
        + nc * CONTINUOUS_HEADER_LEN;
    if data_start > file_len {
        return Err(Error::corrupt_at(
            FORMAT_ID,
            file_len,
            format!(
                "the channel headers end at byte {data_start}, past the end of the file ({file_len} bytes)"
            ),
        ));
    }
    let b = read_block(
        f,
        path,
        FILE_HEADER_LEN,
        data_start - FILE_HEADER_LEN,
        file_len,
    )?;
    let v105 = header.version >= 105;
    let mut at = FILE_HEADER_LEN;
    let mut spike_channels = Vec::new();
    for _ in 0..ns {
        spike_channels.push(SpikeChannel {
            name: b.text_at(at, 32).unwrap_or_default(),
            signal_name: b.text_at(at + 32, 32).unwrap_or_default(),
            channel: need(&b, at + 64, Block::i32_at)?,
            gain: need(&b, at + 80, Block::i32_at)?,
            filter: need(&b, at + 84, Block::i32_at)?,
            threshold: need(&b, at + 88, Block::i32_at)?,
            unit_count: need(&b, at + 96, Block::i32_at)?,
            comment: if v105 {
                b.text_at(at + 848, 128).unwrap_or_default()
            } else {
                String::new()
            },
        });
        at += SPIKE_HEADER_LEN;
    }
    let mut event_channels = Vec::new();
    for _ in 0..ne {
        event_channels.push(EventChannel {
            name: b.text_at(at, 32).unwrap_or_default(),
            channel: need(&b, at + 32, Block::i32_at)?,
            comment: if v105 {
                b.text_at(at + 36, 128).unwrap_or_default()
            } else {
                String::new()
            },
        });
        at += EVENT_HEADER_LEN;
    }
    let mut continuous_channels = Vec::new();
    for _ in 0..nc {
        continuous_channels.push(ContinuousChannel {
            name: b.text_at(at, 32).unwrap_or_default(),
            channel: need(&b, at + 32, Block::i32_at)?,
            rate_hz: need(&b, at + 36, Block::i32_at)?,
            gain: need(&b, at + 40, Block::i32_at)?,
            enabled: need(&b, at + 44, Block::i32_at)? != 0,
            preamp_gain: need(&b, at + 48, Block::i32_at)?,
            comment: if v105 {
                b.text_at(at + 56, 128).unwrap_or_default()
            } else {
                String::new()
            },
        });
        at += CONTINUOUS_HEADER_LEN;
    }
    let index = walk_blocks(f, path, data_start, file_len)?;
    Ok(PlxFile {
        header,
        spike_channels,
        event_channels,
        continuous_channels,
        data_start,
        file_len,
        index,
    })
}

/// Read the 16-byte block header at `at`: (type, timestamp, channel, unit, waveforms, words).
pub fn block_header(b: &Block, at: u64) -> Option<(u16, u64, u16, u16, u16, u16)> {
    let kind = b.u16_at(at)?;
    let high = u64::from(b.u16_at(at + 2)?);
    let low = u64::from(b.u32_at(at + 4)?);
    Some((
        kind,
        (high << 32) | low,
        b.u16_at(at + 8)?,
        b.u16_at(at + 10)?,
        b.u16_at(at + 12)?,
        b.u16_at(at + 14)?,
    ))
}

/// Walk every data block from `start` to the end of the file.
pub fn walk_blocks(f: &mut File, path: &Path, start: u64, file_len: u64) -> Result<PlxIndex> {
    const CHUNK: u64 = 8 << 20;
    let mut ix = PlxIndex::default();
    let mut pos = start;
    let mut buf = Block::default();
    while pos < file_len {
        if pos + BLOCK_HEADER_LEN > file_len {
            ix.findings.push(
                Finding::error(
                    "truncated",
                    format!(
                        "{} bytes after the last whole data block (a block header is cut off)",
                        file_len - pos
                    ),
                )
                .at(pos),
            );
            break;
        }
        if pos < buf.origin || pos + BLOCK_HEADER_LEN > buf.end() {
            buf = read_block(f, path, pos, CHUNK, file_len)?;
        }
        let Some((kind, ts, ch, unit, waves, words)) = block_header(&buf, pos) else {
            break;
        };
        let samples = u64::from(waves) * u64::from(words);
        let len = BLOCK_HEADER_LEN + 2 * samples;
        if pos + len > file_len {
            ix.findings.push(
                Finding::error(
                    "truncated",
                    format!(
                        "the data block at byte {pos} declares {samples} samples but the file ends {} bytes later (cut off)",
                        file_len - pos
                    ),
                )
                .at(pos),
            );
            break;
        }
        *ix.block_counts.entry(kind).or_default() += 1;
        match kind {
            BLOCK_SPIKE => {
                ix.spikes.push(pos);
                ix.max_waveform = ix
                    .max_waveform
                    .max(u32::try_from(samples).unwrap_or(u32::MAX));
            }
            BLOCK_EVENT => ix.events.push(EventRecord {
                timestamp: ts,
                channel: ch,
                value: unit,
            }),
            BLOCK_CONTINUOUS => ix.continuous.entry(ch).or_default().push(SampleBlock {
                offset: pos + BLOCK_HEADER_LEN,
                timestamp: ts,
                samples: u32::try_from(samples).unwrap_or(u32::MAX),
            }),
            other => {
                ix.findings.push(
                    Finding::error(
                        "bad_block",
                        format!("data block of unknown type {other} at byte {pos}; the rest of the file is not read"),
                    )
                    .at(pos),
                );
                break;
            }
        }
        pos += len;
        ix.data_end = pos;
    }
    if ix.data_end == 0 {
        ix.data_end = start;
    }
    Ok(ix)
}

/// Split one channel's blocks into gap-free runs: a block that starts more than half a sample
/// period away from where the previous one ended starts a new run.
pub fn runs(blocks: &[SampleBlock], clock_hz: f64, rate_hz: f64) -> Vec<Run> {
    let mut out: Vec<Run> = Vec::new();
    let ticks = if rate_hz > 0.0 {
        clock_hz / rate_hz
    } else {
        0.0
    };
    let mut expect: Option<f64> = None;
    for (i, b) in blocks.iter().enumerate() {
        let start = b.timestamp as f64;
        let joins = expect.is_some_and(|e| (start - e).abs() <= ticks / 2.0);
        match out.last_mut() {
            Some(r) if joins => {
                r.block_count += 1;
                r.samples += u64::from(b.samples);
            }
            _ => out.push(Run {
                first_block: i,
                block_count: 1,
                samples: u64::from(b.samples),
                timestamp: b.timestamp,
            }),
        }
        expect = Some(start + f64::from(b.samples) * ticks);
    }
    out
}