Skip to content

Commit 228c343

Browse files
committed
tidy up after Heberto feedback
add more comments convert errors to neo errors
1 parent bd78348 commit 228c343

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

neo/rawio/biocamrawio.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
_spike_channel_dtype,
1818
_event_channel_dtype,
1919
)
20+
from neo.core import NeoReadWriteError
2021

2122

2223
class BiocamRawIO(BaseRawIO):
@@ -123,15 +124,18 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
123124
if i_stop is None:
124125
i_stop = self._num_frames
125126

126-
127+
# read functions are different based on the version of biocam
127128
data = self._read_function(self._filehandle, i_start, i_stop, self._num_channels)
128129

129-
# older style data return everything
130-
if len(data.shape) > 1:
130+
# older style data returns array of (n_samples, n_channels), should be a view
131+
# but if memory issues come up we should doublecheck out how the file is being stored
132+
if data.ndim > 1:
131133
if channel_indexes is None:
132134
channel_indexes = slice(None)
133135
sig_chunk = data[:, channel_indexes]
134-
# newer style data returns an initial flat array
136+
137+
# newer style data returns an initial flat array (n_samples * n_channels)
138+
# we iterate through channels rather than slicing
135139
else:
136140
if channel_indexes is None:
137141
channel_indexes = [ch for ch in range(self._num_channels)]
@@ -145,9 +149,19 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
145149
return sig_chunk
146150

147151

148-
def open_biocam_file_header(filename):
152+
def open_biocam_file_header(filename)-> dict:
149153
"""Open a Biocam hdf5 file, read and return the recording info, pick the correct method to access raw data,
150-
and return this to the caller."""
154+
and return this to the caller
155+
156+
Parameters
157+
----------
158+
filename: str
159+
The file to be parsed
160+
161+
Returns
162+
-------
163+
dict
164+
The information necessary to read a biocam file (gain, n_samples, n_channels, etc)."""
151165
import h5py
152166

153167
rf = h5py.File(filename, "r")
@@ -171,9 +185,9 @@ def open_biocam_file_header(filename):
171185
elif file_format in (101, 102) or file_format is None:
172186
num_channels = int(rf["3BData/Raw"].shape[0] / num_frames)
173187
else:
174-
raise Exception("Unknown data file format.")
188+
raise NeoReadWriteError("Unknown data file format.")
175189

176-
# # get channels
190+
# get channels
177191
channels = rf["3BRecInfo/3BMeaStreams/Raw/Chs"][:]
178192

179193
# determine correct function to read data
@@ -183,14 +197,14 @@ def open_biocam_file_header(filename):
183197
elif signal_inv == -1:
184198
read_function = readHDF5t_100_i
185199
else:
186-
raise Exception("Unknown signal inversion")
200+
raise NeoReadWriteError("Unknown signal inversion")
187201
else:
188202
if signal_inv == 1:
189203
read_function = readHDF5t_101
190204
elif signal_inv == -1:
191205
read_function = readHDF5t_101_i
192206
else:
193-
raise Exception("Unknown signal inversion")
207+
raise NeoReadWriteError("Unknown signal inversion")
194208

195209
gain = (max_uv - min_uv) / (2**bit_depth)
196210
offset = min_uv
@@ -217,19 +231,22 @@ def open_biocam_file_header(filename):
217231
scale_factor = experiment_settings["ValueConverter"]["ScaleFactor"]
218232
sampling_rate = experiment_settings["TimeConverter"]["FrameRate"]
219233

234+
num_channels = None
220235
for key in rf:
221236
if key[:5] == "Well_":
222237
num_channels = len(rf[key]["StoredChIdxs"])
223238
if len(rf[key]["Raw"]) % num_channels:
224-
raise RuntimeError(f"Length of raw data array is not multiple of channel number in {key}")
239+
raise NeoReadWriteError(f"Length of raw data array is not multiple of channel number in {key}")
225240
num_frames = len(rf[key]["Raw"]) // num_channels
226241
break
227-
try:
242+
243+
if num_channels is not None:
228244
num_channels_x = num_channels_y = int(np.sqrt(num_channels))
229-
except NameError:
230-
raise RuntimeError("No Well found in the file")
245+
else:
246+
raise NeoReadWriteError("No Well found in the file")
247+
231248
if num_channels_x * num_channels_y != num_channels:
232-
raise RuntimeError(f"Cannot determine structure of the MEA plate with {num_channels} channels")
249+
raise NeoReadWriteError(f"Cannot determine structure of the MEA plate with {num_channels} channels")
233250
channels = 1 + np.concatenate(np.transpose(np.meshgrid(range(num_channels_x), range(num_channels_y))))
234251

235252
gain = scale_factor * (max_uv - min_uv) / (max_digital - min_digital)
@@ -247,6 +264,10 @@ def open_biocam_file_header(filename):
247264
offset=offset,
248265
)
249266

267+
268+
######################################################################
269+
# Helper functions to obtain the raw data split by Biocam version.
270+
250271
# return the full array for the old datasets
251272
def readHDF5t_100(rf, t0, t1, nch):
252273
return rf["3BData/Raw"][t0:t1]

0 commit comments

Comments
 (0)