python-intan 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- intan/__init__.py +42 -0
- intan/control/__init__.py +1 -0
- intan/control/_messaging.py +171 -0
- intan/io/__init__.py +64 -0
- intan/io/_block_parser.py +253 -0
- intan/io/_channel_utils.py +86 -0
- intan/io/_data_processing.py +193 -0
- intan/io/_exceptions.py +37 -0
- intan/io/_file_utils.py +110 -0
- intan/io/_header_parsing.py +383 -0
- intan/io/_metadata_utils.py +150 -0
- intan/io/_rhd_loader.py +199 -0
- intan/plotting/__init__.py +2 -0
- intan/plotting/_emg_plots.py +245 -0
- intan/plotting/_waterfall.py +132 -0
- intan/processing/__init__.py +20 -0
- intan/processing/_filters.py +569 -0
- intan/processing/_metrics_utils.py +84 -0
- python_intan-0.0.2.dist-info/METADATA +185 -0
- python_intan-0.0.2.dist-info/RECORD +23 -0
- python_intan-0.0.2.dist-info/WHEEL +5 -0
- python_intan-0.0.2.dist-info/licenses/LICENSE +21 -0
- python_intan-0.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import struct
|
|
3
|
+
from intan.io._exceptions import UnknownChannelTypeError, QStringError, UnrecognizedFileError
|
|
4
|
+
from intan.io._block_parser import plural
|
|
5
|
+
|
|
6
|
+
def check_magic_number(fid):
|
|
7
|
+
"""Checks magic number at beginning of file to verify this is an Intan
|
|
8
|
+
Technologies RHD data file.
|
|
9
|
+
"""
|
|
10
|
+
magic_number, = struct.unpack('<I', fid.read(4))
|
|
11
|
+
if magic_number != int('c6912702', 16):
|
|
12
|
+
raise UnrecognizedFileError('Unrecognized file type.')
|
|
13
|
+
|
|
14
|
+
def read_notch_filter_frequency(header, freq, fid):
|
|
15
|
+
"""Reads notch filter mode from fid, and stores frequency (in Hz) in
|
|
16
|
+
'header' and 'freq' dicts.
|
|
17
|
+
"""
|
|
18
|
+
notch_filter_mode, = struct.unpack('<h', fid.read(2))
|
|
19
|
+
header['notch_filter_frequency'] = 0
|
|
20
|
+
if notch_filter_mode == 1:
|
|
21
|
+
header['notch_filter_frequency'] = 50
|
|
22
|
+
elif notch_filter_mode == 2:
|
|
23
|
+
header['notch_filter_frequency'] = 60
|
|
24
|
+
freq['notch_filter_frequency'] = header['notch_filter_frequency']
|
|
25
|
+
|
|
26
|
+
def read_header(fid):
|
|
27
|
+
"""Reads the Intan File Format header from the given file.
|
|
28
|
+
"""
|
|
29
|
+
check_magic_number(fid)
|
|
30
|
+
|
|
31
|
+
header = {}
|
|
32
|
+
|
|
33
|
+
read_version_number(header, fid)
|
|
34
|
+
set_num_samples_per_data_block(header)
|
|
35
|
+
|
|
36
|
+
freq = {}
|
|
37
|
+
|
|
38
|
+
read_sample_rate(header, fid)
|
|
39
|
+
read_freq_settings(freq, fid)
|
|
40
|
+
read_notch_filter_frequency(header, freq, fid)
|
|
41
|
+
read_impedance_test_frequencies(freq, fid)
|
|
42
|
+
read_notes(header, fid)
|
|
43
|
+
read_num_temp_sensor_channels(header, fid)
|
|
44
|
+
read_eval_board_mode(header, fid)
|
|
45
|
+
read_reference_channel(header, fid)
|
|
46
|
+
|
|
47
|
+
set_sample_rates(header, freq)
|
|
48
|
+
set_frequency_parameters(header, freq)
|
|
49
|
+
|
|
50
|
+
initialize_channels(header)
|
|
51
|
+
|
|
52
|
+
read_signal_summary(header, fid)
|
|
53
|
+
|
|
54
|
+
return header
|
|
55
|
+
|
|
56
|
+
def read_num_temp_sensor_channels(header, fid):
|
|
57
|
+
"""Stores number of temp sensor channels in
|
|
58
|
+
header['num_temp_sensor_channels']. Temp sensor data may be saved from
|
|
59
|
+
versions 1.1 and later.
|
|
60
|
+
"""
|
|
61
|
+
header['num_temp_sensor_channels'] = 0
|
|
62
|
+
if ((header['version']['major'] == 1 and header['version']['minor'] >= 1)
|
|
63
|
+
or (header['version']['major'] > 1)):
|
|
64
|
+
header['num_temp_sensor_channels'], = struct.unpack('<h', fid.read(2))
|
|
65
|
+
|
|
66
|
+
def read_version_number(header, fid, verbose=True):
|
|
67
|
+
"""Reads version number (major and minor) from fid. Stores them into
|
|
68
|
+
header['version']['major'] and header['version']['minor'].
|
|
69
|
+
"""
|
|
70
|
+
version = {}
|
|
71
|
+
(version['major'], version['minor']) = struct.unpack('<hh', fid.read(4))
|
|
72
|
+
header['version'] = version
|
|
73
|
+
|
|
74
|
+
if verbose:
|
|
75
|
+
print('\nReading Intan Technologies RHD Data File, Version {}.{}\n'
|
|
76
|
+
.format(version['major'], version['minor']))
|
|
77
|
+
|
|
78
|
+
def set_num_samples_per_data_block(header):
|
|
79
|
+
"""Determines how many samples are present per data block (60 or 128),
|
|
80
|
+
depending on version. Data files v2.0 or later have 128 samples per block,
|
|
81
|
+
otherwise 60.
|
|
82
|
+
"""
|
|
83
|
+
header['num_samples_per_data_block'] = 60
|
|
84
|
+
if header['version']['major'] > 1:
|
|
85
|
+
header['num_samples_per_data_block'] = 128
|
|
86
|
+
|
|
87
|
+
def read_sample_rate(header, fid):
|
|
88
|
+
"""Reads sample rate from fid. Stores it into header['sample_rate'].
|
|
89
|
+
"""
|
|
90
|
+
header['sample_rate'], = struct.unpack('<f', fid.read(4))
|
|
91
|
+
|
|
92
|
+
def read_freq_settings(freq, fid):
|
|
93
|
+
"""Reads amplifier frequency settings from fid. Stores them in 'freq' dict.
|
|
94
|
+
"""
|
|
95
|
+
(freq['dsp_enabled'],
|
|
96
|
+
freq['actual_dsp_cutoff_frequency'],
|
|
97
|
+
freq['actual_lower_bandwidth'],
|
|
98
|
+
freq['actual_upper_bandwidth'],
|
|
99
|
+
freq['desired_dsp_cutoff_frequency'],
|
|
100
|
+
freq['desired_lower_bandwidth'],
|
|
101
|
+
freq['desired_upper_bandwidth']) = struct.unpack('<hffffff', fid.read(26))
|
|
102
|
+
|
|
103
|
+
def read_impedance_test_frequencies(freq, fid):
|
|
104
|
+
"""Reads desired and actual impedance test frequencies from fid, and stores
|
|
105
|
+
them (in Hz) in 'freq' dicts.
|
|
106
|
+
"""
|
|
107
|
+
(freq['desired_impedance_test_frequency'],
|
|
108
|
+
freq['actual_impedance_test_frequency']) = (
|
|
109
|
+
struct.unpack('<ff', fid.read(8)))
|
|
110
|
+
|
|
111
|
+
def read_notes(header, fid):
|
|
112
|
+
"""Reads notes as QStrings from fid, and stores them as strings in
|
|
113
|
+
header['notes'] dict.
|
|
114
|
+
"""
|
|
115
|
+
header['notes'] = {'note1': read_qstring(fid),
|
|
116
|
+
'note2': read_qstring(fid),
|
|
117
|
+
'note3': read_qstring(fid)}
|
|
118
|
+
|
|
119
|
+
def read_eval_board_mode(header, fid):
|
|
120
|
+
"""Stores eval board mode in header['eval_board_mode']. Board mode is saved
|
|
121
|
+
from versions 1.3 and later.
|
|
122
|
+
"""
|
|
123
|
+
header['eval_board_mode'] = 0
|
|
124
|
+
if ((header['version']['major'] == 1 and header['version']['minor'] >= 3)
|
|
125
|
+
or (header['version']['major'] > 1)):
|
|
126
|
+
header['eval_board_mode'], = struct.unpack('<h', fid.read(2))
|
|
127
|
+
|
|
128
|
+
def read_reference_channel(header, fid):
|
|
129
|
+
"""Reads name of reference channel as QString from fid, and stores it as
|
|
130
|
+
a string in header['reference_channel']. Data files v2.0 or later include
|
|
131
|
+
reference channel.
|
|
132
|
+
"""
|
|
133
|
+
if header['version']['major'] > 1:
|
|
134
|
+
header['reference_channel'] = read_qstring(fid)
|
|
135
|
+
|
|
136
|
+
def set_sample_rates(header, freq):
|
|
137
|
+
"""Determines what the sample rates are for various signal types, and
|
|
138
|
+
stores them in 'freq' dict.
|
|
139
|
+
"""
|
|
140
|
+
freq['amplifier_sample_rate'] = header['sample_rate']
|
|
141
|
+
freq['aux_input_sample_rate'] = header['sample_rate'] / 4
|
|
142
|
+
freq['supply_voltage_sample_rate'] = (header['sample_rate'] /
|
|
143
|
+
header['num_samples_per_data_block'])
|
|
144
|
+
freq['board_adc_sample_rate'] = header['sample_rate']
|
|
145
|
+
freq['board_dig_in_sample_rate'] = header['sample_rate']
|
|
146
|
+
|
|
147
|
+
def set_frequency_parameters(header, freq):
|
|
148
|
+
"""Stores frequency parameters (set in other functions) in
|
|
149
|
+
header['frequency_parameters']
|
|
150
|
+
"""
|
|
151
|
+
header['frequency_parameters'] = freq
|
|
152
|
+
|
|
153
|
+
def initialize_channels(header):
|
|
154
|
+
"""Creates empty lists for each type of data channel and stores them in
|
|
155
|
+
'header' dict.
|
|
156
|
+
"""
|
|
157
|
+
header['spike_triggers'] = []
|
|
158
|
+
header['amplifier_channels'] = []
|
|
159
|
+
header['aux_input_channels'] = []
|
|
160
|
+
header['supply_voltage_channels'] = []
|
|
161
|
+
header['board_adc_channels'] = []
|
|
162
|
+
header['board_dig_in_channels'] = []
|
|
163
|
+
header['board_dig_out_channels'] = []
|
|
164
|
+
|
|
165
|
+
def read_signal_summary(header, fid, verbose=False):
|
|
166
|
+
"""Reads signal summary from data file header and stores information for
|
|
167
|
+
all signal groups and their channels in 'header' dict.
|
|
168
|
+
"""
|
|
169
|
+
number_of_signal_groups, = struct.unpack('<h', fid.read(2))
|
|
170
|
+
for signal_group in range(1, number_of_signal_groups + 1):
|
|
171
|
+
add_signal_group_information(header, fid, signal_group)
|
|
172
|
+
add_num_channels(header)
|
|
173
|
+
if verbose:
|
|
174
|
+
print_header_summary(header)
|
|
175
|
+
|
|
176
|
+
def add_signal_group_information(header, fid, signal_group):
|
|
177
|
+
"""Adds information for a signal group and all its channels to 'header'
|
|
178
|
+
dict.
|
|
179
|
+
"""
|
|
180
|
+
signal_group_name = read_qstring(fid)
|
|
181
|
+
signal_group_prefix = read_qstring(fid)
|
|
182
|
+
(signal_group_enabled, signal_group_num_channels, _) = struct.unpack(
|
|
183
|
+
'<hhh', fid.read(6))
|
|
184
|
+
|
|
185
|
+
if signal_group_num_channels > 0 and signal_group_enabled > 0:
|
|
186
|
+
for _ in range(0, signal_group_num_channels):
|
|
187
|
+
add_channel_information(header, fid, signal_group_name,
|
|
188
|
+
signal_group_prefix, signal_group)
|
|
189
|
+
|
|
190
|
+
def add_channel_information(header, fid, signal_group_name,
|
|
191
|
+
signal_group_prefix, signal_group):
|
|
192
|
+
"""Reads a new channel's information from fid and appends it to 'header'
|
|
193
|
+
dict.
|
|
194
|
+
"""
|
|
195
|
+
(new_channel, new_trigger_channel, channel_enabled,
|
|
196
|
+
signal_type) = read_new_channel(
|
|
197
|
+
fid, signal_group_name, signal_group_prefix, signal_group)
|
|
198
|
+
append_new_channel(header, new_channel, new_trigger_channel,
|
|
199
|
+
channel_enabled, signal_type)
|
|
200
|
+
|
|
201
|
+
def read_new_channel(fid, signal_group_name, signal_group_prefix,
|
|
202
|
+
signal_group):
|
|
203
|
+
"""Reads a new channel's information from fid.
|
|
204
|
+
"""
|
|
205
|
+
new_channel = {'port_name': signal_group_name,
|
|
206
|
+
'port_prefix': signal_group_prefix,
|
|
207
|
+
'port_number': signal_group}
|
|
208
|
+
new_channel['native_channel_name'] = read_qstring(fid)
|
|
209
|
+
new_channel['custom_channel_name'] = read_qstring(fid)
|
|
210
|
+
(new_channel['native_order'],
|
|
211
|
+
new_channel['custom_order'],
|
|
212
|
+
signal_type, channel_enabled,
|
|
213
|
+
new_channel['chip_channel'],
|
|
214
|
+
new_channel['board_stream']) = (
|
|
215
|
+
struct.unpack('<hhhhhh', fid.read(12)))
|
|
216
|
+
new_trigger_channel = {}
|
|
217
|
+
(new_trigger_channel['voltage_trigger_mode'],
|
|
218
|
+
new_trigger_channel['voltage_threshold'],
|
|
219
|
+
new_trigger_channel['digital_trigger_channel'],
|
|
220
|
+
new_trigger_channel['digital_edge_polarity']) = (
|
|
221
|
+
struct.unpack('<hhhh', fid.read(8)))
|
|
222
|
+
(new_channel['electrode_impedance_magnitude'],
|
|
223
|
+
new_channel['electrode_impedance_phase']) = (
|
|
224
|
+
struct.unpack('<ff', fid.read(8)))
|
|
225
|
+
|
|
226
|
+
return new_channel, new_trigger_channel, channel_enabled, signal_type
|
|
227
|
+
|
|
228
|
+
def append_new_channel(header, new_channel, new_trigger_channel,
|
|
229
|
+
channel_enabled, signal_type):
|
|
230
|
+
""""Appends 'new_channel' to 'header' dict depending on if channel is
|
|
231
|
+
enabled and the signal type.
|
|
232
|
+
"""
|
|
233
|
+
if not channel_enabled:
|
|
234
|
+
return
|
|
235
|
+
|
|
236
|
+
if signal_type == 0:
|
|
237
|
+
header['amplifier_channels'].append(new_channel)
|
|
238
|
+
header['spike_triggers'].append(new_trigger_channel)
|
|
239
|
+
elif signal_type == 1:
|
|
240
|
+
header['aux_input_channels'].append(new_channel)
|
|
241
|
+
elif signal_type == 2:
|
|
242
|
+
header['supply_voltage_channels'].append(new_channel)
|
|
243
|
+
elif signal_type == 3:
|
|
244
|
+
header['board_adc_channels'].append(new_channel)
|
|
245
|
+
elif signal_type == 4:
|
|
246
|
+
header['board_dig_in_channels'].append(new_channel)
|
|
247
|
+
elif signal_type == 5:
|
|
248
|
+
header['board_dig_out_channels'].append(new_channel)
|
|
249
|
+
else:
|
|
250
|
+
raise UnknownChannelTypeError('Unknown channel type.')
|
|
251
|
+
|
|
252
|
+
def add_num_channels(header):
|
|
253
|
+
"""Adds channel numbers for all signal types to 'header' dict.
|
|
254
|
+
"""
|
|
255
|
+
header['num_amplifier_channels'] = len(header['amplifier_channels'])
|
|
256
|
+
header['num_aux_input_channels'] = len(header['aux_input_channels'])
|
|
257
|
+
header['num_supply_voltage_channels'] = len(
|
|
258
|
+
header['supply_voltage_channels'])
|
|
259
|
+
header['num_board_adc_channels'] = len(header['board_adc_channels'])
|
|
260
|
+
header['num_board_dig_in_channels'] = len(header['board_dig_in_channels'])
|
|
261
|
+
header['num_board_dig_out_channels'] = len(
|
|
262
|
+
header['board_dig_out_channels'])
|
|
263
|
+
|
|
264
|
+
def header_to_result(header, result):
|
|
265
|
+
"""Merges header information from .rhd file into a common 'result' dict.
|
|
266
|
+
If any fields have been allocated but aren't relevant (for example, no
|
|
267
|
+
channels of this type exist), does not copy those entries into 'result'.
|
|
268
|
+
"""
|
|
269
|
+
if header['num_amplifier_channels'] > 0:
|
|
270
|
+
result['spike_triggers'] = header['spike_triggers']
|
|
271
|
+
result['amplifier_channels'] = header['amplifier_channels']
|
|
272
|
+
|
|
273
|
+
result['notes'] = header['notes']
|
|
274
|
+
result['frequency_parameters'] = header['frequency_parameters']
|
|
275
|
+
|
|
276
|
+
if header['version']['major'] > 1:
|
|
277
|
+
result['reference_channel'] = header['reference_channel']
|
|
278
|
+
|
|
279
|
+
if header['num_aux_input_channels'] > 0:
|
|
280
|
+
result['aux_input_channels'] = header['aux_input_channels']
|
|
281
|
+
|
|
282
|
+
if header['num_supply_voltage_channels'] > 0:
|
|
283
|
+
result['supply_voltage_channels'] = header['supply_voltage_channels']
|
|
284
|
+
|
|
285
|
+
if header['num_board_adc_channels'] > 0:
|
|
286
|
+
result['board_adc_channels'] = header['board_adc_channels']
|
|
287
|
+
|
|
288
|
+
if header['num_board_dig_in_channels'] > 0:
|
|
289
|
+
result['board_dig_in_channels'] = header['board_dig_in_channels']
|
|
290
|
+
|
|
291
|
+
if header['num_board_dig_out_channels'] > 0:
|
|
292
|
+
result['board_dig_out_channels'] = header['board_dig_out_channels']
|
|
293
|
+
|
|
294
|
+
return result
|
|
295
|
+
|
|
296
|
+
def print_header_summary(header):
|
|
297
|
+
"""Prints summary of contents of RHD header to console.
|
|
298
|
+
"""
|
|
299
|
+
print('Found {} amplifier channel{}.'.format(
|
|
300
|
+
header['num_amplifier_channels'],
|
|
301
|
+
plural(header['num_amplifier_channels'])))
|
|
302
|
+
print('Found {} auxiliary input channel{}.'.format(
|
|
303
|
+
header['num_aux_input_channels'],
|
|
304
|
+
plural(header['num_aux_input_channels'])))
|
|
305
|
+
print('Found {} supply voltage channel{}.'.format(
|
|
306
|
+
header['num_supply_voltage_channels'],
|
|
307
|
+
plural(header['num_supply_voltage_channels'])))
|
|
308
|
+
print('Found {} board ADC channel{}.'.format(
|
|
309
|
+
header['num_board_adc_channels'],
|
|
310
|
+
plural(header['num_board_adc_channels'])))
|
|
311
|
+
print('Found {} board digital input channel{}.'.format(
|
|
312
|
+
header['num_board_dig_in_channels'],
|
|
313
|
+
plural(header['num_board_dig_in_channels'])))
|
|
314
|
+
print('Found {} board digital output channel{}.'.format(
|
|
315
|
+
header['num_board_dig_out_channels'],
|
|
316
|
+
plural(header['num_board_dig_out_channels'])))
|
|
317
|
+
print('Found {} temperature sensors channel{}.'.format(
|
|
318
|
+
header['num_temp_sensor_channels'],
|
|
319
|
+
plural(header['num_temp_sensor_channels'])))
|
|
320
|
+
print('')
|
|
321
|
+
|
|
322
|
+
def read_qstring(fid):
|
|
323
|
+
"""Reads Qt style QString.
|
|
324
|
+
|
|
325
|
+
The first 32-bit unsigned number indicates the length of the string
|
|
326
|
+
(in bytes). If this number equals 0xFFFFFFFF, the string is null.
|
|
327
|
+
|
|
328
|
+
Strings are stored as unicode.
|
|
329
|
+
"""
|
|
330
|
+
|
|
331
|
+
length, = struct.unpack('<I', fid.read(4))
|
|
332
|
+
if length == int('ffffffff', 16):
|
|
333
|
+
return ""
|
|
334
|
+
|
|
335
|
+
if length > (os.fstat(fid.fileno()).st_size - fid.tell() + 1):
|
|
336
|
+
print(length)
|
|
337
|
+
raise QStringError('Length too long.')
|
|
338
|
+
|
|
339
|
+
# convert length from bytes to 16-bit Unicode words
|
|
340
|
+
length = int(length / 2)
|
|
341
|
+
|
|
342
|
+
data = []
|
|
343
|
+
for _ in range(0, length):
|
|
344
|
+
c, = struct.unpack('<H', fid.read(2))
|
|
345
|
+
data.append(c)
|
|
346
|
+
|
|
347
|
+
return ''.join([chr(c) for c in data])
|
|
348
|
+
|
|
349
|
+
def data_to_result(header, data, result):
|
|
350
|
+
"""Merges data from all present signals into a common 'result' dict. If
|
|
351
|
+
any signal types have been allocated but aren't relevant (for example,
|
|
352
|
+
no channels of this type exist), does not copy those entries into 'result'.
|
|
353
|
+
"""
|
|
354
|
+
if len(header['amplifier_channels']) > 0:
|
|
355
|
+
result['t_amplifier'] = data['t_amplifier']
|
|
356
|
+
result['amplifier_data'] = data['amplifier_data']
|
|
357
|
+
|
|
358
|
+
if header['num_aux_input_channels'] > 0:
|
|
359
|
+
result['t_aux_input'] = data['t_aux_input']
|
|
360
|
+
result['aux_input_data'] = data['aux_input_data']
|
|
361
|
+
|
|
362
|
+
if header['num_supply_voltage_channels'] > 0:
|
|
363
|
+
result['t_supply_voltage'] = data['t_supply_voltage']
|
|
364
|
+
result['supply_voltage_data'] = data['supply_voltage_data']
|
|
365
|
+
|
|
366
|
+
if header['num_temp_sensor_channels'] > 0:
|
|
367
|
+
result['t_temp_sensor'] = data['t_temp_sensor']
|
|
368
|
+
|
|
369
|
+
if header['num_board_adc_channels'] > 0:
|
|
370
|
+
result['t_board_adc'] = data['t_board_adc']
|
|
371
|
+
result['board_adc_data'] = data['board_adc_data']
|
|
372
|
+
|
|
373
|
+
if (header['num_board_dig_in_channels'] > 0
|
|
374
|
+
or header['num_board_dig_out_channels'] > 0):
|
|
375
|
+
result['t_dig'] = data['t_dig']
|
|
376
|
+
|
|
377
|
+
if header['num_board_dig_in_channels'] > 0:
|
|
378
|
+
result['board_dig_in_data'] = data['board_dig_in_data']
|
|
379
|
+
|
|
380
|
+
if header['num_board_dig_out_channels'] > 0:
|
|
381
|
+
result['board_dig_out_data'] = data['board_dig_out_data']
|
|
382
|
+
|
|
383
|
+
return result
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
|
|
2
|
+
def plural(number_of_items):
|
|
3
|
+
"""Utility function to pluralize words based on the number of items.
|
|
4
|
+
"""
|
|
5
|
+
if number_of_items == 1:
|
|
6
|
+
return ''
|
|
7
|
+
return 's'
|
|
8
|
+
|
|
9
|
+
def get_bytes_per_data_block(header):
|
|
10
|
+
"""Calculates the number of bytes in each 60 or 128 sample datablock."""
|
|
11
|
+
# Depending on the system used to acquire the data,
|
|
12
|
+
# 'num_samples_per_data_block' will be either 60 (USB Interface Board)
|
|
13
|
+
# or 128 (Recording Controller).
|
|
14
|
+
# Use this number along with numbers of channels to accrue a sum of how
|
|
15
|
+
# many bytes each data block should contain.
|
|
16
|
+
|
|
17
|
+
# Timestamps (one channel always present): Start with 4 bytes per sample.
|
|
18
|
+
bytes_per_block = bytes_per_signal_type(
|
|
19
|
+
header['num_samples_per_data_block'],
|
|
20
|
+
1,
|
|
21
|
+
4)
|
|
22
|
+
|
|
23
|
+
# Amplifier data: Add 2 bytes per sample per enabled amplifier channel.
|
|
24
|
+
bytes_per_block += bytes_per_signal_type(
|
|
25
|
+
header['num_samples_per_data_block'],
|
|
26
|
+
header['num_amplifier_channels'],
|
|
27
|
+
2)
|
|
28
|
+
|
|
29
|
+
# Auxiliary data: Add 2 bytes per sample per enabled aux input channel.
|
|
30
|
+
# Note that aux inputs are sample 4x slower than amplifiers, so there
|
|
31
|
+
# are 1/4 as many samples.
|
|
32
|
+
bytes_per_block += bytes_per_signal_type(
|
|
33
|
+
header['num_samples_per_data_block'] / 4,
|
|
34
|
+
header['num_aux_input_channels'],
|
|
35
|
+
2)
|
|
36
|
+
|
|
37
|
+
# Supply voltage: Add 2 bytes per sample per enabled vdd channel.
|
|
38
|
+
# Note that aux inputs are sampled once per data block
|
|
39
|
+
# (60x or 128x slower than amplifiers), so there are
|
|
40
|
+
# 1/60 or 1/128 as many samples.
|
|
41
|
+
bytes_per_block += bytes_per_signal_type(
|
|
42
|
+
1,
|
|
43
|
+
header['num_supply_voltage_channels'],
|
|
44
|
+
2)
|
|
45
|
+
|
|
46
|
+
# Analog inputs: Add 2 bytes per sample per enabled analog input channel.
|
|
47
|
+
bytes_per_block += bytes_per_signal_type(
|
|
48
|
+
header['num_samples_per_data_block'],
|
|
49
|
+
header['num_board_adc_channels'],
|
|
50
|
+
2)
|
|
51
|
+
|
|
52
|
+
# Digital inputs: Add 2 bytes per sample.
|
|
53
|
+
# Note that if at least 1 channel is enabled, a single 16-bit sample
|
|
54
|
+
# is saved, with each bit corresponding to an individual channel.
|
|
55
|
+
if header['num_board_dig_in_channels'] > 0:
|
|
56
|
+
bytes_per_block += bytes_per_signal_type(
|
|
57
|
+
header['num_samples_per_data_block'],
|
|
58
|
+
1,
|
|
59
|
+
2)
|
|
60
|
+
|
|
61
|
+
# Digital outputs: Add 2 bytes per sample.
|
|
62
|
+
# Note that if at least 1 channel is enabled, a single 16-bit sample
|
|
63
|
+
# is saved, with each bit corresponding to an individual channel.
|
|
64
|
+
if header['num_board_dig_out_channels'] > 0:
|
|
65
|
+
bytes_per_block += bytes_per_signal_type(
|
|
66
|
+
header['num_samples_per_data_block'],
|
|
67
|
+
1,
|
|
68
|
+
2)
|
|
69
|
+
|
|
70
|
+
# Temp sensor: Add 2 bytes per sample per enabled temp sensor channel.
|
|
71
|
+
# Note that temp sensor inputs are sampled once per data block
|
|
72
|
+
# (60x or 128x slower than amplifiers), so there are
|
|
73
|
+
# 1/60 or 1/128 as many samples.
|
|
74
|
+
if header['num_temp_sensor_channels'] > 0:
|
|
75
|
+
bytes_per_block += bytes_per_signal_type(
|
|
76
|
+
1,
|
|
77
|
+
header['num_temp_sensor_channels'],
|
|
78
|
+
2)
|
|
79
|
+
|
|
80
|
+
return bytes_per_block
|
|
81
|
+
|
|
82
|
+
def bytes_per_signal_type(num_samples, num_channels, bytes_per_sample):
|
|
83
|
+
"""Calculates the number of bytes, per data block, for a signal type
|
|
84
|
+
provided the number of samples (per data block), the number of enabled
|
|
85
|
+
channels, and the size of each sample in bytes.
|
|
86
|
+
"""
|
|
87
|
+
return num_samples * num_channels * bytes_per_sample
|
|
88
|
+
|
|
89
|
+
def calculate_data_size(header, filename, fid, verbose=True):
|
|
90
|
+
"""Calculates how much data is present in this file. Returns:
|
|
91
|
+
data_present: Bool, whether any data is present in file
|
|
92
|
+
filesize: Int, size (in bytes) of file
|
|
93
|
+
num_blocks: Int, number of 60 or 128-sample data blocks present
|
|
94
|
+
num_samples: Int, number of samples present in file
|
|
95
|
+
"""
|
|
96
|
+
bytes_per_block = get_bytes_per_data_block(header)
|
|
97
|
+
|
|
98
|
+
# Determine filesize and if any data is present.
|
|
99
|
+
filesize = os.path.getsize(filename)
|
|
100
|
+
data_present = False
|
|
101
|
+
bytes_remaining = filesize - fid.tell()
|
|
102
|
+
if bytes_remaining > 0:
|
|
103
|
+
data_present = True
|
|
104
|
+
|
|
105
|
+
# If the file size is somehow different than expected, raise an error.
|
|
106
|
+
if bytes_remaining % bytes_per_block != 0:
|
|
107
|
+
raise FileSizeError(
|
|
108
|
+
'Something is wrong with file size : '
|
|
109
|
+
'should have a whole number of data blocks')
|
|
110
|
+
|
|
111
|
+
# Calculate how many data blocks are present.
|
|
112
|
+
num_blocks = int(bytes_remaining / bytes_per_block)
|
|
113
|
+
|
|
114
|
+
num_samples = calculate_num_samples(header, num_blocks)
|
|
115
|
+
|
|
116
|
+
if verbose:
|
|
117
|
+
print_record_time_summary(num_samples['amplifier'],
|
|
118
|
+
header['sample_rate'],
|
|
119
|
+
data_present)
|
|
120
|
+
|
|
121
|
+
return data_present, filesize, num_blocks, num_samples
|
|
122
|
+
|
|
123
|
+
def calculate_num_samples(header, num_data_blocks):
|
|
124
|
+
"""Calculates number of samples for each signal type, storing the results
|
|
125
|
+
in num_samples dict for later use.
|
|
126
|
+
"""
|
|
127
|
+
samples_per_block = header['num_samples_per_data_block']
|
|
128
|
+
num_samples = {}
|
|
129
|
+
num_samples['amplifier'] = int(samples_per_block * num_data_blocks)
|
|
130
|
+
num_samples['aux_input'] = int((samples_per_block / 4) * num_data_blocks)
|
|
131
|
+
num_samples['supply_voltage'] = int(num_data_blocks)
|
|
132
|
+
num_samples['board_adc'] = int(samples_per_block * num_data_blocks)
|
|
133
|
+
num_samples['board_dig_in'] = int(samples_per_block * num_data_blocks)
|
|
134
|
+
num_samples['board_dig_out'] = int(samples_per_block * num_data_blocks)
|
|
135
|
+
return num_samples
|
|
136
|
+
|
|
137
|
+
def print_record_time_summary(num_amp_samples, sample_rate, data_present):
|
|
138
|
+
"""Prints summary of how much recorded data is present in RHD file
|
|
139
|
+
to console.
|
|
140
|
+
"""
|
|
141
|
+
record_time = num_amp_samples / sample_rate
|
|
142
|
+
|
|
143
|
+
if data_present:
|
|
144
|
+
print('File contains {:0.3f} seconds of data. '
|
|
145
|
+
'Amplifiers were sampled at {:0.2f} kS/s.'
|
|
146
|
+
.format(record_time, sample_rate / 1000))
|
|
147
|
+
else:
|
|
148
|
+
print('Header file contains no data. '
|
|
149
|
+
'Amplifiers were sampled at {:0.2f} kS/s.'
|
|
150
|
+
.format(sample_rate / 1000))
|