forked from mne-tools/mne-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nihon.py
377 lines (316 loc) · 13.3 KB
/
nihon.py
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
# Authors: Federico Raimondo <[email protected]>
#
# License: BSD (3-clause)
from collections import OrderedDict
from datetime import datetime, timezone
from pathlib import Path
import numpy as np
from ...utils import fill_doc, logger, verbose, warn
from ..base import BaseRaw
from ..meas_info import create_info
from ...annotations import Annotations
from ..utils import _mult_cal_one
def _ensure_path(fname):
out = fname
if not isinstance(out, Path):
out = Path(out)
return out
@fill_doc
def read_raw_nihon(fname, preload=False, verbose=None):
"""Reader for an Nihon Kohden EEG file.
Parameters
----------
fname : str
Path to the Nihon Kohden data file (.eeg).
preload : bool
If True, all data are loaded at initialization.
%(verbose)s
Returns
-------
raw : instance of RawNihon
A Raw object containing Nihon Kohden data.
See Also
--------
mne.io.Raw : Documentation of attribute and methods.
"""
return RawNihon(fname, preload, verbose)
_valid_headers = [
'EEG-1100A V01.00',
'EEG-1100B V01.00',
'EEG-1100C V01.00',
'QI-403A V01.00',
'QI-403A V02.00',
'EEG-2100 V01.00',
'EEG-2100 V02.00',
'DAE-2100D V01.30',
'DAE-2100D V02.00',
]
def _read_nihon_metadata(fname):
metadata = {}
fname = _ensure_path(fname)
pnt_fname = fname.with_suffix('.PNT')
if not pnt_fname.exists():
warn('No PNT file exists. Metadata will be blank')
return metadata
logger.info('Found PNT file, reading metadata.')
with open(pnt_fname, 'r') as fid:
version = np.fromfile(fid, '|S16', 1).astype('U16')[0]
if version not in _valid_headers:
raise ValueError(f'Not a valid Nihon Kohden PNT file ({version})')
metadata['version'] = version
# Read timestamp
fid.seek(0x40)
meas_str = np.fromfile(fid, '|S14', 1).astype('U14')[0]
meas_date = datetime.strptime(meas_str, '%Y%m%d%H%M%S')
meas_date = meas_date.replace(tzinfo=timezone.utc)
metadata['meas_date'] = meas_date
return metadata
_default_chan_labels = [
'FP1', 'FP2', 'F3', 'F4', 'C3', 'C4', 'P3', 'P4', 'O1', 'O2', 'F7', 'F8',
'T3', 'T4', 'T5', 'T6', 'FZ', 'CZ', 'PZ', 'E', 'PG1', 'PG2', 'A1', 'A2',
'T1', 'T2'
]
_default_chan_labels += [f'X{i}' for i in range(1, 12)]
_default_chan_labels += [f'NA{i}' for i in range(1, 6)]
_default_chan_labels += [f'DC{i:02}' for i in range(1, 33)]
_default_chan_labels += ['BN1', 'BN2', 'Mark1', 'Mark2']
_default_chan_labels += [f'NA{i}' for i in range(6, 28)]
_default_chan_labels += ['X12/BP1', 'X13/BP2', 'X14/BP3', 'X15/BP4']
_default_chan_labels += [f'X{i}' for i in range(16, 166)]
_default_chan_labels += ['NA28', 'Z']
def _read_21e_file(fname):
fname = _ensure_path(fname)
e_fname = fname.with_suffix('.21E')
_chan_labels = [x for x in _default_chan_labels]
if e_fname.exists():
# Read the 21E file and update the labels accordingly.
logger.info('Found 21E file, reading channel names.')
with open(e_fname, 'r') as fid:
keep_parsing = False
for line in fid:
if line.startswith('['):
if 'ELECTRODE' in line or 'REFERENCE' in line:
keep_parsing = True
else:
keep_parsing = False
elif keep_parsing is True:
idx, name = line.split('=')
idx = int(idx)
_chan_labels[idx] = name.strip()
return _chan_labels
def _read_nihon_header(fname):
# Read the Nihon Kohden EEG file header
fname = _ensure_path(fname)
_chan_labels = _read_21e_file(fname)
header = {}
logger.info(f'Reading header from {fname}')
with open(fname, 'r') as fid:
version = np.fromfile(fid, '|S16', 1).astype('U16')[0]
if version not in _valid_headers:
raise ValueError(
'Not a valid Nihon Kohden EEG file ({})'.format(version))
fid.seek(0x0081)
control_block = np.fromfile(fid, '|S16', 1).astype('U16')[0]
if control_block not in _valid_headers:
raise ValueError('Not a valid Nihon Kohden EEG file '
'(control block {})'.format(version))
fid.seek(0x17fe)
waveform_sign = np.fromfile(fid, np.uint8, 1)[0]
if waveform_sign != 1:
raise ValueError('Not a valid Nihon Kohden EEG file '
'(waveform block)')
header['version'] = version
fid.seek(0x0091)
n_ctlblocks = np.fromfile(fid, np.uint8, 1)[0]
header['n_ctlblocks'] = n_ctlblocks
controlblocks = []
for i_ctl_block in range(n_ctlblocks):
t_controlblock = {}
fid.seek(0x0092 + i_ctl_block * 20)
t_ctl_address = np.fromfile(fid, np.uint32, 1)[0]
t_controlblock['address'] = t_ctl_address
fid.seek(t_ctl_address + 17)
n_datablocks = np.fromfile(fid, np.uint8, 1)[0]
t_controlblock['n_datablocks'] = n_datablocks
t_controlblock['datablocks'] = []
for i_data_block in range(n_datablocks):
t_datablock = {}
fid.seek(t_ctl_address + i_data_block * 20 + 18)
t_data_address = np.fromfile(fid, np.uint32, 1)[0]
t_datablock['address'] = t_data_address
fid.seek(t_data_address + 0x26)
t_n_channels = np.fromfile(fid, np.uint8, 1)[0]
t_datablock['n_channels'] = t_n_channels
t_channels = []
for i_ch in range(t_n_channels):
fid.seek(t_data_address + 0x27 + (i_ch * 10))
t_idx = np.fromfile(fid, np.uint8, 1)[0]
t_channels.append(_chan_labels[t_idx])
t_datablock['channels'] = t_channels
fid.seek(t_data_address + 0x1C)
t_record_duration = np.fromfile(fid, np.uint32, 1)[0]
t_datablock['duration'] = t_record_duration
fid.seek(t_data_address + 0x1a)
sfreq = np.fromfile(fid, np.uint16, 1)[0] & 0x3FFF
t_datablock['sfreq'] = sfreq
t_datablock['n_samples'] = int(t_record_duration * sfreq / 10)
t_controlblock['datablocks'].append(t_datablock)
controlblocks.append(t_controlblock)
header['controlblocks'] = controlblocks
# Now check that every data block has the same channels and sfreq
chans = []
sfreqs = []
nsamples = []
for t_ctl in header['controlblocks']:
for t_dtb in t_ctl['datablocks']:
chans.append(t_dtb['channels'])
sfreqs.append(t_dtb['sfreq'])
nsamples.append(t_dtb['n_samples'])
for i_elem in range(1, len(chans)):
if chans[0] != chans[i_elem]:
raise ValueError('Channel names in datablocks do not match')
if sfreqs[0] != sfreqs[i_elem]:
raise ValueError('Sample frequency in datablocks do not match')
header['ch_names'] = chans[0]
header['sfreq'] = sfreqs[0]
header['n_samples'] = np.sum(nsamples)
# TODO: Support more than one controlblock and more than one datablock
if header['n_ctlblocks'] != 1:
raise NotImplementedError('I dont know how to read more than one '
'control block for this type of file :(')
if header['controlblocks'][0]['n_datablocks'] != 1:
raise NotImplementedError('I dont know how to read more than one '
'data block for this type of file :(')
return header
def _read_nihon_annotations(fname, orig_time):
fname = _ensure_path(fname)
annotations = None
log_fname = fname.with_suffix('.LOG')
if not log_fname.exists():
warn('No LOG file exists. Annotations will not be read')
return annotations
logger.info('Found LOG file, reading events.')
with open(log_fname, 'r') as fid:
version = np.fromfile(fid, '|S16', 1).astype('U16')[0]
if version not in _valid_headers:
raise ValueError(
'Not a valid Nihon Kohden LOG file ({})'.format(version))
fid.seek(0x91)
n_logblocks = np.fromfile(fid, np.uint8, 1)[0]
all_onsets = []
all_descriptions = []
for t_block in range(n_logblocks):
fid.seek(0x92 + t_block * 20)
t_blk_address = np.fromfile(fid, np.uint32, 1)[0]
fid.seek(t_blk_address + 0x12)
n_logs = np.fromfile(fid, np.uint8, 1)[0]
fid.seek(t_blk_address + 0x14)
t_logs = np.fromfile(fid, '|S45', n_logs).astype('U45')
for t_log in t_logs:
t_desc = t_log[:20].strip('\x00')
t_onset = datetime.strptime(t_log[20:26], '%H%M%S')
t_onset = (t_onset.hour * 3600 + t_onset.minute * 60 +
t_onset.second)
all_onsets.append(t_onset)
all_descriptions.append(t_desc)
annotations = Annotations(all_onsets, 0.0, all_descriptions, orig_time)
return annotations
def _map_ch_to_type(ch_name):
ch_type_pattern = OrderedDict([
('stim', ('Mark',)), ('misc', ('DC', 'NA', 'Z')), ('bio', ('X',))])
for key, kinds in ch_type_pattern.items():
if any(kind in ch_name for kind in kinds):
return key
return 'eeg'
def _map_ch_to_specs(ch_name):
unit_mult = 1e-3
phys_min = -12002.9
phys_max = 12002.56
dig_min = -32768
if ch_name.upper() in _default_chan_labels:
idx = _default_chan_labels.index(ch_name.upper())
if (idx < 42 or idx > 73) and idx not in [76, 77]:
unit_mult = 1e-6
phys_min = -3200
phys_max = 3199.902
t_range = phys_max - phys_min
cal = t_range / 65535
offset = phys_min - (dig_min * cal)
out = dict(unit=unit_mult, phys_min=phys_min, phys_max=phys_max,
dig_min=dig_min, cal=cal, offset=offset)
return out
@fill_doc
class RawNihon(BaseRaw):
"""Raw object from a Nihon Kohden EEG file.
Parameters
----------
fname : str
Path to the Nihon Kohden data file (.eeg).
preload : bool
If True, all data are loaded at initialization.
%(verbose)s
See Also
--------
mne.io.Raw : Documentation of attribute and methods.
"""
@verbose
def __init__(self, fname, preload=False, verbose=None):
fname = _ensure_path(fname)
data_name = fname.name
logger.info('Loading %s' % data_name)
header = _read_nihon_header(fname)
metadata = _read_nihon_metadata(fname)
# n_chan = len(header['ch_names']) + 1
sfreq = header['sfreq']
# data are multiplexed int16
ch_names = header['ch_names']
ch_types = [_map_ch_to_type(x) for x in ch_names]
info = create_info(ch_names, sfreq, ch_types)
n_samples = header['n_samples']
if 'meas_date' in metadata:
info['meas_date'] = metadata['meas_date']
chs = {x: _map_ch_to_specs(x) for x in ch_names}
orig_ch_names = header['ch_names']
cal = np.array(
[chs[x]['cal'] for x in orig_ch_names], float)[:, np.newaxis]
offsets = np.array(
[chs[x]['offset'] for x in orig_ch_names], float)[:, np.newaxis]
gains = np.array(
[chs[x]['unit'] for x in orig_ch_names], float)[:, np.newaxis]
raw_extras = dict(
cal=cal, offsets=offsets, gains=gains, header=header)
self._header = header
for i_ch, ch_name in enumerate(info['ch_names']):
t_range = (chs[ch_name]['phys_max'] - chs[ch_name]['phys_min'])
info['chs'][i_ch]['range'] = t_range
info['chs'][i_ch]['cal'] = 1 / t_range
super(RawNihon, self).__init__(
info, preload=preload, last_samps=(n_samples - 1,),
filenames=[fname.as_posix()], orig_format='short',
raw_extras=[raw_extras])
# Get annotations from LOG file
annots = _read_nihon_annotations(fname, orig_time=info['meas_date'])
self.set_annotations(annots)
def _read_segment_file(self, data, idx, fi, start, stop, cals, mult):
"""Read a chunk of raw data."""
# For now we assume one control block and one data block.
header = self._raw_extras[fi]['header']
# Get the original cal, offsets and gains
cal = self._raw_extras[fi]['cal']
offsets = self._raw_extras[fi]['offsets']
gains = self._raw_extras[fi]['gains']
datablock = header['controlblocks'][0]['datablocks'][0]
n_channels = datablock['n_channels'] + 1
datastart = (datablock['address'] + 0x27 +
(datablock['n_channels'] * 10))
with open(self._filenames[fi], 'rb') as fid:
start_offset = datastart + start * n_channels * 2
to_read = (stop - start) * n_channels
fid.seek(start_offset)
block_data = np.fromfile(fid, '<u2', to_read) + 0x8000
block_data = block_data.astype(np.int16)
block_data = block_data.reshape(n_channels, -1, order='F')
block_data = block_data[:-1] * cal # cast to float64
block_data += offsets
block_data *= gains
_mult_cal_one(data, block_data, idx, cals, mult)