-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathintan_loader.py
More file actions
298 lines (247 loc) · 11.1 KB
/
intan_loader.py
File metadata and controls
298 lines (247 loc) · 11.1 KB
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
import numpy as np
import os, sys, struct
from pathlib import Path
def _read_qstring(fid):
"""Read Qt style QString.
The first 32-bit unsigned number indicates the length of the string (in bytes).
If this number equals 0xFFFFFFFF, the string is null.
Strings are stored as unicode.
"""
(length,) = struct.unpack("<I", fid.read(4))
if length == int("ffffffff", 16):
return ""
if length > (os.fstat(fid.fileno()).st_size - fid.tell() + 1):
print(length)
raise Exception("Length too long.")
# convert length from bytes to 16-bit Unicode words
length = int(length / 2)
data = []
for i in range(0, length):
(c,) = struct.unpack("<H", fid.read(2))
data.append(c)
if sys.version_info >= (3, 0):
a = "".join([chr(c) for c in data])
else:
a = "".join([unichr(c) for c in data])
return a
def _read_header(fid):
"""Reads the Intan File Format header from the given file."""
# Check 'magic number' at beginning of file to make sure this is an Intan
# Technologies RHD2000 data file.
(magic_number,) = struct.unpack("<I", fid.read(4))
if magic_number != int("0xD69127AC", 16):
raise Exception("Unrecognized file type.")
header = {}
# Read version number.
version = {}
(version["major"], version["minor"]) = struct.unpack("<hh", fid.read(4))
header["version"] = version
# Read information of sampling rate and amplifier frequency settings.
(header["sample_rate"],) = struct.unpack("<f", fid.read(4))
(
header["dsp_enabled"],
header["actual_dsp_cutoff_frequency"],
header["actual_lower_bandwidth"],
header["actual_lower_settle_bandwidth"],
header["actual_upper_bandwidth"],
header["desired_dsp_cutoff_frequency"],
header["desired_lower_bandwidth"],
header["desired_lower_settle_bandwidth"],
header["desired_upper_bandwidth"],
) = struct.unpack("<hffffffff", fid.read(34))
# This tells us if a software 50/60 Hz notch filter was enabled during
# the data acquisition.
(notch_filter_mode,) = struct.unpack("<h", fid.read(2))
header["notch_filter_frequency"] = 0
if notch_filter_mode == 1:
header["notch_filter_frequency"] = 50
elif notch_filter_mode == 2:
header["notch_filter_frequency"] = 60
(
header["desired_impedance_test_frequency"],
header["actual_impedance_test_frequency"],
) = struct.unpack("<ff", fid.read(8))
(header["amp_settle_mode"], header["charge_recovery_mode"]) = struct.unpack(
"<hh", fid.read(4)
)
frequency_parameters = {}
frequency_parameters["amplifier_sample_rate"] = header["sample_rate"]
frequency_parameters["board_adc_sample_rate"] = header["sample_rate"]
frequency_parameters["board_dig_in_sample_rate"] = header["sample_rate"]
frequency_parameters["desired_dsp_cutoff_frequency"] = header[
"desired_dsp_cutoff_frequency"
]
frequency_parameters["actual_dsp_cutoff_frequency"] = header[
"actual_dsp_cutoff_frequency"
]
frequency_parameters["dsp_enabled"] = header["dsp_enabled"]
frequency_parameters["desired_lower_bandwidth"] = header["desired_lower_bandwidth"]
frequency_parameters["desired_lower_settle_bandwidth"] = header[
"desired_lower_settle_bandwidth"
]
frequency_parameters["actual_lower_bandwidth"] = header["actual_lower_bandwidth"]
frequency_parameters["actual_lower_settle_bandwidth"] = header[
"actual_lower_settle_bandwidth"
]
frequency_parameters["desired_upper_bandwidth"] = header["desired_upper_bandwidth"]
frequency_parameters["actual_upper_bandwidth"] = header["actual_upper_bandwidth"]
frequency_parameters["notch_filter_frequency"] = header["notch_filter_frequency"]
frequency_parameters["desired_impedance_test_frequency"] = header[
"desired_impedance_test_frequency"
]
frequency_parameters["actual_impedance_test_frequency"] = header[
"actual_impedance_test_frequency"
]
header["frequency_parameters"] = frequency_parameters
(
header["stim_step_size"],
header["recovery_current_limit"],
header["recovery_target_voltage"],
) = struct.unpack("fff", fid.read(12))
note1 = _read_qstring(fid)
note2 = _read_qstring(fid)
note3 = _read_qstring(fid)
header["notes"] = {"note1": note1, "note2": note2, "note3": note3}
(header["dc_amplifier_data_saved"], header["eval_board_mode"]) = struct.unpack(
"<hh", fid.read(4)
)
header["ref_channel_name"] = _read_qstring(fid)
# Create structure arrays for each type of data channel.
header["spike_triggers"] = []
header["amplifier_channels"] = []
header["board_adc_channels"] = []
header["board_dac_channels"] = []
header["board_dig_in_channels"] = []
header["board_dig_out_channels"] = []
# Read signal summary from data file header.
(number_of_signal_groups,) = struct.unpack("<h", fid.read(2))
# print("n signal groups {}".format(number_of_signal_groups))
for signal_group in range(1, number_of_signal_groups + 1):
signal_group_name = _read_qstring(fid)
signal_group_prefix = _read_qstring(fid)
(
signal_group_enabled,
signal_group_num_channels,
signal_group_num_amp_channels,
) = struct.unpack("<hhh", fid.read(6))
if (signal_group_num_channels > 0) and (signal_group_enabled > 0):
for signal_channel in range(0, signal_group_num_channels):
new_channel = {
"port_name": signal_group_name,
"port_prefix": signal_group_prefix,
"port_number": signal_group,
}
new_channel["native_channel_name"] = _read_qstring(fid)
new_channel["custom_channel_name"] = _read_qstring(fid)
(
new_channel["native_order"],
new_channel["custom_order"],
signal_type,
channel_enabled,
new_channel["chip_channel"],
command_stream,
new_channel["board_stream"],
) = struct.unpack(
"<hhhhhhh", fid.read(14)
) # ignore command_stream
new_trigger_channel = {}
(
new_trigger_channel["voltage_trigger_mode"],
new_trigger_channel["voltage_threshold"],
new_trigger_channel["digital_trigger_channel"],
new_trigger_channel["digital_edge_polarity"],
) = struct.unpack("<hhhh", fid.read(8))
(
new_channel["electrode_impedance_magnitude"],
new_channel["electrode_impedance_phase"],
) = struct.unpack("<ff", fid.read(8))
if channel_enabled:
if signal_type == 0:
header["amplifier_channels"].append(new_channel)
header["spike_triggers"].append(new_trigger_channel)
elif signal_type == 1:
raise Exception("Wrong signal type for the rhs format")
# header['aux_input_channels'].append(new_channel)
elif signal_type == 2:
raise Exception("Wrong signal type for the rhs format")
# header['supply_voltage_channels'].append(new_channel)
elif signal_type == 3:
header["board_adc_channels"].append(new_channel)
elif signal_type == 4:
header["board_dac_channels"].append(new_channel)
elif signal_type == 5:
header["board_dig_in_channels"].append(new_channel)
elif signal_type == 6:
header["board_dig_out_channels"].append(new_channel)
else:
raise Exception("Unknown channel type.")
# Summarize contents of data file.
header["num_amplifier_channels"] = len(header["amplifier_channels"])
header["num_board_adc_channels"] = len(header["board_adc_channels"])
header["num_board_dac_channels"] = len(header["board_dac_channels"])
header["num_board_dig_in_channels"] = len(header["board_dig_in_channels"])
header["num_board_dig_out_channels"] = len(header["board_dig_out_channels"])
return header
def load_rhs(folder: str, file_expr: str = "*"):
"""Load rhs data
Example:
# Read data
>>> rhs_data = load_rhs("/home/inbox/organoids21/032520_US_885kHz_sham", file_expr="amp*dat")
# Plot data
>>> import matplotlib.pyplot as plt
>>> plt.plot(rhs_data["time"], rhs_data["recordings"]["amp-B-000.dat"])
>>> plt.xlabel("Time (s)")
>>> plt.ylabel("Reading")
>>> plt.show()
Args:
folder (str): Folder that contains info.rhs, time.dat, and *.dat files
file_expr (str): pattern matching of file names to be read. Defaults to "*" (read all files).
Returns:
rhs_data (dict): RHS data.
rhs_data["header"] (dict): Header.
rhs_data["recordings"] (dict): Readings from various files
rhs_data["timestamps"] (np.array_like): Relative timestamps in seconds.
"""
rhs_data = {}
# Get header
header_filepath = next(Path(folder).glob("info.rhs"))
with open(header_filepath, "rb") as fid:
rhs_data["header"] = _read_header(fid)
# Get timestamps
time_file = next(Path(folder).glob("time.dat"))
rhs_data["timestamps"] = (
np.memmap(time_file, dtype=np.int32)
/ rhs_data["header"]["frequency_parameters"]["amplifier_sample_rate"]
)
# Get data files
file_paths = Path(folder).glob(file_expr)
exclude_list = ["time", "info", "Zone.Identifier"]
file_paths = [
file
for file in file_paths
if not any(string in file.as_posix() for string in exclude_list)
]
# Get recording data
rhs_data["recordings"] = {}
if len(file_paths):
for file_path in sorted(file_paths):
signal_type = file_path.stem.split("-")[0]
if signal_type == "amp":
signal = np.memmap(file_path, dtype=np.int16)
signal = signal * 0.195 # Convert to microvolts
elif signal_type == "board":
signal = np.memmap(file_path, dtype=np.uint16)
signal = (signal - 32768) * 0.0003125 # Convert to volts
elif signal_type == "dc":
signal = np.memmap(file_path, dtype=np.uint16)
signal = (signal - 512) * 19.23 # Convert to milivolts
elif signal_type == "stim":
signal = np.memmap(file_path, dtype=np.uint16)
# convert the signal from 9-bit one's complement to standard encoding
current = (
np.bitwise_and(signal, 255) * rhs_data["header"]["stim_step_size"]
)
sign = 1 - np.bitwise_and(signal, 256) // 128
signal = current * sign
rhs_data["recordings"][file_path.stem] = signal
return rhs_data