forked from Intan-Technologies/IntanToNWB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteNWB.py
356 lines (305 loc) · 18.2 KB
/
WriteNWB.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
from hdmf.backends.hdf5.h5_utils import H5DataIO
import numpy as np
class WrappedData:
# Class for storing wrapped data arrays
def __init__(self):
self.t = None
self.t_lowpass = None
self.t_supply_voltage = None
self.data_amplifier = None
self.data_dc_amplifier = None
self.data_lowpass = None
self.data_highpass = None
self.data_stim = None
self.data_board_adc = None
self.data_board_dac = None
self.data_board_dig_in = None
self.data_board_dig_out = None
self.data_amp_settle = None
self.data_charge_recovery = None
self.data_compliance_limit = None
self.data_aux_in = None
self.data_supply_voltage = None
self.data_temp = None
def create_intan_device(nwbfile, header):
""" Create 'device' object for the Intan system that the data was acquired with.
Parameters
----------
nwbfile : pynwb.file.NWBFile
Previously created NWB file that should contain this device
header : dict
Dict containing previously read header information
Returns
-------
pynwb.device.Device
Created NWB device representing Intan system
"""
intan_device_name = 'Unknown Intan System'
board_mode = str(header['board_mode']) if header['filetype'] == 'rhd' else str(header['eval_board_mode'])
intan_device_description = 'Unrecognized system that generated an .' + header['filetype'] + ' file with a board mode of ' + board_mode
if header['filetype'] == 'rhd':
if header['board_mode'] == 0:
intan_device_name = 'Intan USB Interface Board'
intan_device_description = '256-channel RHD2000 USB Interface Board, part number C3100'
elif header['board_mode'] == 13:
intan_device_name = 'Intan Recording Controller'
intan_device_description = '512-channel or 1024-channel RHD2000 Recording Controller, part number C3004 or C3008'
else:
intan_device_name = 'Intan StimulationRecording Controller'
intan_device_description = '128-channel RHS2000 StimulationRecording Controller, part number M4200'
intan_device_description += '. File version ' + str(header['version']['major']) + '.' + str(header['version']['minor'])
return nwbfile.create_device(name=intan_device_name,
description=intan_device_description,
manufacturer='Intan Technologies'
)
def create_electrode_table_region(nwbfile, header, intan_device):
""" Create 'electrode table region' object for the electrodes that the data was acquired with.
Parameters
----------
nwbfile : pynwb.file.NWBFile
Previously created NWB file that should contain this electrode table region
header : dict
Dict containing previously read header information
intan_device : pynwb.device.Device
Previously created NWB device representing Intan system
Returns
-------
electrode_table_region : hdmf.common.table.DynamicTableRegion
Electrode table region for the electrodes that the data was acquired with.
"""
# Create an electrode group for each port
if header['num_amplifier_channels'] > 0:
nwbfile.add_electrode_column(name='imp_phase',
description='phase (in degrees) of complex impedance of this channel')
nwbfile.add_electrode_column(name='native_channel_name',
description='native, uneditable name (for example, A-000) of this channel')
nwbfile.add_electrode_column(name='custom_channel_name',
description='custom, user-editable name of this channel')
created_electrode_groups = {}
for channel in range(header['num_amplifier_channels']):
group_name = 'Intan ' + header['amplifier_channels'][channel]['port_name'] + ' electrode group';
if not (group_name in created_electrode_groups):
electrode_group = nwbfile.create_electrode_group(name=group_name,
description='description',
location='location',
device=intan_device)
created_electrode_groups[group_name] = electrode_group
# Create an electrode for each channel, and associate it with it's port's group
for channel in range(header['num_amplifier_channels']):
this_channel_struct = header['amplifier_channels'][channel]
custom_channel_name = this_channel_struct['custom_channel_name']
group_name = 'Intan ' + this_channel_struct['port_name'] + ' electrode group'
description = 'electrode for channel ' + custom_channel_name # apparently this description is unused - how to give channel name?
location = 'none'
nwbfile.add_electrode(id=channel,
x=0.0,
y=0.0,
z=0.0,
imp=this_channel_struct['electrode_impedance_magnitude'],
imp_phase=this_channel_struct['electrode_impedance_phase'],
native_channel_name=this_channel_struct['native_channel_name'],
custom_channel_name=this_channel_struct['custom_channel_name'],
location=location,
filtering='none',
group=created_electrode_groups[group_name])
# If there are any amplifier channels, create this region
if header['num_amplifier_channels'] > 0:
electrode_table_region = nwbfile.create_electrode_table_region(list(range(0,header['num_amplifier_channels'])),
'Intan electrode table region')
else:
electrode_table_region = None
return electrode_table_region
def append_to_dataset(dataset, data_to_add):
""" Append data_to_add to dataset, along the first axis.
Parameters
----------
dataset : h5py._hl.dataset.Dataset
h5py dataset to be appended to
data_to_add : hdmf.backends.hdf5.h5_utils.H5DataIO
H5DataIO object containing data to be added
Returns
-------
None
"""
# Increase the dataset's size to handle this new chunk of data
dataset.resize(dataset.shape[0] + data_to_add.shape[0], axis=0)
# Write this chunk to the dataset
dataset[-data_to_add.shape[0]:] = data_to_add
def get_compression_settings(use_compression, compression_level):
""" Get compression settings to pass to H5DataIO functions
Parameters
----------
use_compression : bool
Whether compression is to be used for written NWB data
compression_level : int
What level of compression is to be applied to written NWB data
Returns
-------
compression : str
What type of compression is to be used for written NWB data, for example, 'gzip'
compression_opts : int
Options for compression. For gzip, what level of compression is to be applied to written NWB data
"""
if use_compression is False:
compression = False
compression_opts = None
else:
compression = 'gzip'
compression_opts = compression_level
return (compression, compression_opts)
def wrap_data_1D(data_array, samples_this_chunk, total_num_samples, compression_settings):
""" Wrap generic 1D data in a H5DataIO object
Parameters
----------
data_array : numpy.ndarray
Array containing data that needs wrapping
samples_this_chunk : int
Number of samples in this chunk
total_num_samples : int
Total number of samples to write in this conversion
compression_settings : tuple
Tuple containing 'compression' and 'compression_opts'
Returns
-------
d : hdmf.backends.hdf5.h5_utils.H5DataIO
Wrapped H5DataIO object for this data
"""
d = H5DataIO(data=data_array,
chunks=(samples_this_chunk,),
maxshape=(total_num_samples,),
compression=compression_settings[0],
compression_opts=compression_settings[1])
return d
def wrap_data_2D(data_array, samples_this_chunk, total_num_samples, num_channels, compression_settings):
""" Wrap generic 2D data in a H5DataIO object
Parameters
----------
data_array :
Array containing data that needs wrapping
samples_this_chunk : int
Number of samples in this chunk
total_num_samples : int
Total number of samples to write in this conversion
compression_settings : tuple
Tuple containing 'compression' and 'compression_opts'
Returns
-------
d = hdmf.backends.hdf5.h5_utils.H5DataIO
Wrapped H5DataIO object for this data
"""
d = H5DataIO(data=np.array(data_array).T,
chunks=(samples_this_chunk, num_channels),
maxshape=(total_num_samples, num_channels),
compression=compression_settings[0],
compression_opts=compression_settings[1])
return d
def wrap_data_arrays(header, data, t_key, amp_samples_this_chunk, total_num_amp_samples, use_compression, compression_level):
wrapped_data = WrappedData()
# Determine compression settings to pass to H5DataIO functions
compression_settings = get_compression_settings(use_compression, compression_level)
wrapped_data.t = wrap_data_1D(data_array=data[t_key],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
compression_settings=compression_settings)
if header['lowpass_present']:
wrapped_data.t_lowpass = wrap_data_1D(data_array=data[t_key][0::header['lowpass_downsample_factor']],
samples_this_chunk=int(amp_samples_this_chunk / header['lowpass_downsample_factor']),
total_num_samples=int(total_num_amp_samples / header['lowpass_downsample_factor']),
compression_settings=compression_settings)
if header['num_amplifier_channels'] > 0:
wrapped_data.data_amplifier = wrap_data_2D(data_array=data['amplifier_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
if header['lowpass_present']:
wrapped_data.data_lowpass = wrap_data_2D(data_array=data['lowpass_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
if header['highpass_present']:
wrapped_data.data_highpass = wrap_data_2D(data_array=data['highpass_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
if header['num_board_adc_channels'] > 0:
wrapped_data.data_board_adc = wrap_data_2D(data_array=data['board_adc_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_board_adc_channels'],
compression_settings=compression_settings)
if header['num_board_dig_in_channels'] > 0:
wrapped_data.data_board_dig_in = wrap_data_2D(data_array=data['board_dig_in_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_board_dig_in_channels'],
compression_settings=compression_settings)
if header['num_board_dig_out_channels'] > 0:
wrapped_data.data_board_dig_out = wrap_data_2D(data_array=data['board_dig_out_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_board_dig_out_channels'],
compression_settings=compression_settings)
if header['filetype'] == 'rhd':
wrapped_data.t_supply_voltage = wrap_data_1D(data_array=data[t_key][0::header['num_samples_per_data_block']],
samples_this_chunk=int(amp_samples_this_chunk / header['num_samples_per_data_block']),
total_num_samples=int(total_num_amp_samples / header['num_samples_per_data_block']),
compression_settings=compression_settings)
if header['num_aux_input_channels'] > 0:
wrapped_data.data_aux_in = wrap_data_2D(data_array=data['aux_input_data'],
samples_this_chunk=int(amp_samples_this_chunk / 4),
total_num_samples=int(total_num_amp_samples / 4),
num_channels=header['num_aux_input_channels'],
compression_settings=compression_settings)
wrapped_data.t_aux_input = wrap_data_1D(data_array=data['t_aux_input'],
samples_this_chunk=int(amp_samples_this_chunk / 4),
total_num_samples=int(total_num_amp_samples / 4),
compression_settings=compression_settings)
if header['num_supply_voltage_channels'] > 0:
wrapped_data.data_supply_voltage = wrap_data_2D(data_array=data['supply_voltage_data'],
samples_this_chunk=int(amp_samples_this_chunk / header['num_samples_per_data_block']),
total_num_samples=int(total_num_amp_samples / header['num_samples_per_data_block']),
num_channels=header['num_supply_voltage_channels'],
compression_settings=compression_settings)
if header['num_temp_sensor_channels'] > 0:
wrapped_data.data_temp = wrap_data_2D(data_array=data['temp_sensor_data'],
samples_this_chunk=int(amp_samples_this_chunk / header['num_samples_per_data_block']),
total_num_samples=int(total_num_amp_samples / header['num_samples_per_data_block']),
num_channels=header['num_temp_sensor_channels'],
compression_settings=compression_settings)
else:
if header['dc_amplifier_data_saved']:
wrapped_data.data_dc_amplifier = wrap_data_2D(data_array=data['dc_amplifier_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
wrapped_data.data_amp_settle = wrap_data_2D(data_array=data['amp_settle_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
wrapped_data.data_charge_recovery = wrap_data_2D(data_array=data['charge_recovery_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
wrapped_data.data_compliance_limit = wrap_data_2D(data_array=data['compliance_limit_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
wrapped_data.data_stim = wrap_data_2D(data_array=data['stim_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_amplifier_channels'],
compression_settings=compression_settings)
if header['num_board_dac_channels'] > 0:
wrapped_data.data_board_dac = wrap_data_2D(data_array=data['board_dac_data'],
samples_this_chunk=amp_samples_this_chunk,
total_num_samples=total_num_amp_samples,
num_channels=header['num_board_dac_channels'],
compression_settings=compression_settings)
return wrapped_data