Skip to content

Commit 5824440

Browse files
committed
Snake case rewrite
1 parent 97cb693 commit 5824440

File tree

1 file changed

+84
-83
lines changed

1 file changed

+84
-83
lines changed

neo/rawio/biocamrawio.py

Lines changed: 84 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
130130
if channel_indexes is None:
131131
channel_indexes = slice(None)
132132
if self._read_function is readHDF5t_brw4_sparse:
133-
data = self._read_function(self._filehandle, i_start, i_stop, self._num_channels, self.true_zeroes, self.use_synthetic_noise)
133+
data = self._read_function(self._filehandle, i_start, i_stop, self._num_channels,
134+
self.true_zeroes, self.use_synthetic_noise)
134135
else:
135136
data = self._read_function(self._filehandle, i_start, i_stop, self._num_channels)
136137
return data[:, channel_indexes]
@@ -209,18 +210,18 @@ def open_biocam_file_header(filename):
209210
sampling_rate = experiment_settings["TimeConverter"]["FrameRate"]
210211
num_frames = rf['TOC'][-1,-1]
211212

212-
wellID = None
213-
for key in rf:
214-
if key.startswith("Well_"):
215-
wellID = key
216-
num_channels = len(rf[key]["StoredChIdxs"])
217-
if "Raw" in rf[key]:
218-
if len(rf[key]["Raw"]) % num_channels:
219-
raise RuntimeError(f"Length of raw data array is not multiple of channel number in {key}")
220-
if num_frames != len(rf[key]["Raw"]) // num_channels:
221-
raise RuntimeError(f"Estimated number of frames from TOC does not match length of raw data array in {key}")
213+
well_ID = None
214+
for well_ID in rf:
215+
if well_ID.startswith("Well_"):
216+
num_channels = len(rf[well_ID]["StoredChIdxs"])
217+
if "Raw" in rf[well_ID]:
218+
if len(rf[well_ID]["Raw"]) % num_channels:
219+
raise RuntimeError(f"Length of raw data array is not multiple of channel number in {well_ID}")
220+
if num_frames != len(rf[well_ID]["Raw"]) // num_channels:
221+
raise RuntimeError(f"Estimated number of frames from TOC does not match"
222+
f"length of raw data array in {well_ID}")
222223
break
223-
if not wellID:
224+
if not well_ID:
224225
raise RuntimeError("No Well found in the file")
225226
num_channels_x = num_channels_y = int(np.sqrt(num_channels))
226227
if num_channels_x * num_channels_y != num_channels:
@@ -229,9 +230,9 @@ def open_biocam_file_header(filename):
229230

230231
gain = scale_factor * (max_uv - min_uv) / (max_digital - min_digital)
231232
offset = min_uv
232-
if "Raw" in rf[wellID]:
233+
if "Raw" in rf[well_ID]:
233234
read_function = readHDF5t_brw4
234-
elif "EventsBasedSparseRaw" in rf[wellID]:
235+
elif "EventsBasedSparseRaw" in rf[well_ID]:
235236
read_function = readHDF5t_brw4_sparse
236237

237238
return dict(
@@ -270,116 +271,116 @@ def readHDF5t_brw4(rf, t0, t1, nch):
270271

271272
def readHDF5t_brw4_sparse(rf, t0, t1, nch, true_zeroes=False, use_synthetic_noise=False):
272273

273-
noiseStdDev = None
274-
startFrame = t0
275-
numFrames = t1 - t0
276-
for key in rf:
277-
if key.startswith("Well_"):
278-
wellID = key
274+
# noise_std = None
275+
start_frame = t0
276+
num_frames = t1 - t0
277+
for well_ID in rf:
278+
if well_ID.startswith("Well_"):
279279
break
280280
# initialize an empty (fill with zeros) data collection
281-
data = np.zeros((nch, numFrames), dtype=np.int16)
281+
data = np.zeros((nch, num_frames), dtype=np.int16)
282282
if not true_zeroes:
283283
# Will read as 0s after 12 bits signed conversion
284284
data.fill(2048)
285285
# fill the data collection with Gaussian noise if requested
286286
if use_synthetic_noise:
287-
data = generate_synthetic_noise(rf, data, wellID, startFrame, numFrames, stdDev=noiseStdDev)
287+
data = generate_synthetic_noise(rf, data, well_ID, start_frame, num_frames) #, std=noise_std)
288288
# fill the data collection with the decoded event based sparse raw data
289-
data = decode_event_based_raw_data(rf, data, wellID, startFrame, numFrames)
289+
data = decode_event_based_raw_data(rf, data, well_ID, start_frame, num_frames)
290+
290291
return data.T
291292

292293

293-
def decode_event_based_raw_data(rf, data, wellID, startFrame, numFrames):
294+
def decode_event_based_raw_data(rf, data, well_ID, start_frame, num_frames):
294295
# Source: Documentation by 3Brain
295296
# https://gin.g-node.org/NeuralEnsemble/ephy_testing_data/src/master/biocam/documentation_brw_4.x_bxr_3.x_bcmp_1.x_in_brainwave_5.x_v1.1.3.pdf
296297
# collect the TOCs
297298
toc = np.array(rf["TOC"])
298-
eventsToc = np.array(rf[wellID]["EventsBasedSparseRawTOC"])
299+
events_toc = np.array(rf[well_ID]["EventsBasedSparseRawTOC"])
299300
# from the given start position and duration in frames, localize the corresponding event positions
300301
# using the TOC
301-
tocStartIdx = np.searchsorted(toc[:, 1], startFrame)
302-
tocEndIdx = min(
303-
np.searchsorted(toc[:, 1], startFrame + numFrames, side="right") + 1,
302+
toc_start_idx = np.searchsorted(toc[:, 1], start_frame)
303+
toc_end_idx = min(
304+
np.searchsorted(toc[:, 1], start_frame + num_frames, side="right") + 1,
304305
len(toc) - 1)
305-
eventsStartPosition = eventsToc[tocStartIdx]
306-
eventsEndPosition = eventsToc[tocEndIdx]
306+
events_start_pos = events_toc[toc_start_idx]
307+
events_end_pos = events_toc[toc_end_idx]
307308
# decode all data for the given well ID and time interval
308-
binaryData = rf[wellID]["EventsBasedSparseRaw"][eventsStartPosition:eventsEndPosition]
309-
binaryDataLength = len(binaryData)
309+
binary_data = rf[well_ID]["EventsBasedSparseRaw"][events_start_pos:events_end_pos]
310+
binary_data_length = len(binary_data)
310311
pos = 0
311-
while pos < binaryDataLength:
312-
chIdx = int.from_bytes(binaryData[pos:pos + 4], byteorder="little", signed=True)
312+
while pos < binary_data_length:
313+
ch_idx = int.from_bytes(binary_data[pos:pos + 4], byteorder="little", signed=True)
313314
pos += 4
314-
chDataLength = int.from_bytes(binaryData[pos:pos + 4], byteorder="little", signed=True)
315+
ch_data_length = int.from_bytes(binary_data[pos:pos + 4], byteorder="little", signed=True)
315316
pos += 4
316-
chDataPos = pos
317-
while pos < chDataPos + chDataLength:
318-
fromInclusive = int.from_bytes(binaryData[pos:pos + 8], byteorder="little", signed=True)
317+
ch_data_pos = pos
318+
while pos < ch_data_pos + ch_data_length:
319+
from_inclusive = int.from_bytes(binary_data[pos:pos + 8], byteorder="little", signed=True)
319320
pos += 8
320-
toExclusive = int.from_bytes(binaryData[pos:pos + 8], byteorder="little", signed=True)
321+
to_exclusive = int.from_bytes(binary_data[pos:pos + 8], byteorder="little", signed=True)
321322
pos += 8
322-
rangeDataPos = pos
323-
for j in range(fromInclusive, toExclusive):
324-
if j >= startFrame + numFrames:
323+
range_data_pos = pos
324+
for j in range(from_inclusive, to_exclusive):
325+
if j >= start_frame + num_frames:
325326
break
326-
if j >= startFrame:
327-
data[chIdx][j - startFrame] = int.from_bytes(
328-
binaryData[rangeDataPos:rangeDataPos + 2], byteorder="little", signed=True)
329-
rangeDataPos += 2
330-
pos += (toExclusive - fromInclusive) * 2
327+
if j >= start_frame:
328+
data[ch_idx][j - start_frame] = int.from_bytes(
329+
binary_data[range_data_pos:range_data_pos + 2], byteorder="little", signed=True)
330+
range_data_pos += 2
331+
pos += (to_exclusive - from_inclusive) * 2
331332

332333
return data
333334

334-
def generate_synthetic_noise(rf, data, wellID, startFrame, numFrames, stdDev=None):
335+
def generate_synthetic_noise(rf, data, well_ID, start_frame, num_frames): #, std=None):
335336
# Source: Documentation by 3Brain
336337
# https://gin.g-node.org/NeuralEnsemble/ephy_testing_data/src/master/biocam/documentation_brw_4.x_bxr_3.x_bcmp_1.x_in_brainwave_5.x_v1.1.3.pdf
337338
# collect the TOCs
338339
toc = np.array(rf["TOC"])
339-
noiseToc = np.array(rf[wellID]["NoiseTOC"])
340+
noise_toc = np.array(rf[well_ID]["NoiseTOC"])
340341
# from the given start position in frames, localize the corresponding noise positions
341342
# using the TOC
342-
tocStartIdx = np.searchsorted(toc[:, 1], startFrame)
343-
noiseStartPosition = noiseToc[tocStartIdx]
344-
noiseEndPosition = noiseStartPosition
345-
for i in range(tocStartIdx + 1, len(noiseToc)):
346-
nextPosition = noiseToc[i]
347-
if nextPosition > noiseStartPosition:
348-
noiseEndPosition = nextPosition
343+
toc_start_idx = np.searchsorted(toc[:, 1], start_frame)
344+
noise_start_pos = noise_toc[toc_start_idx]
345+
noise_end_pos = noise_start_pos
346+
for i in range(toc_start_idx + 1, len(noise_toc)):
347+
next_pos = noise_toc[i]
348+
if next_pos > noise_start_pos:
349+
noise_end_pos = next_pos
349350
break
350-
if noiseEndPosition == noiseStartPosition:
351-
for i in range(tocStartIdx - 1, 0, -1):
352-
previousPosition = noiseToc[i]
353-
if previousPosition < noiseStartPosition:
354-
noiseEndPosition = noiseStartPosition
355-
noiseStartPosition = previousPosition
351+
if noise_end_pos == noise_start_pos:
352+
for i in range(toc_start_idx - 1, 0, -1):
353+
previous_pos = noise_toc[i]
354+
if previous_pos < noise_start_pos:
355+
noise_end_pos = noise_start_pos
356+
noise_start_pos = previous_pos
356357
break
357358
# obtain the noise info at the start position
358-
noiseChIdxs = rf[wellID]["NoiseChIdxs"][noiseStartPosition:noiseEndPosition]
359-
noiseMean = rf[wellID]["NoiseMean"][noiseStartPosition:noiseEndPosition]
360-
if stdDev is None:
361-
noiseStdDev = rf[wellID]["NoiseStdDev"][noiseStartPosition:noiseEndPosition]
362-
else:
363-
noiseStdDev = np.repeat(stdDev, noiseEndPosition - noiseStartPosition)
364-
noiseLength = noiseEndPosition - noiseStartPosition
365-
noiseInfo = {}
366-
meanCollection = []
367-
stdDevCollection = []
368-
for i in range(1, noiseLength):
369-
noiseInfo[noiseChIdxs[i]] = [noiseMean[i], noiseStdDev[i]]
370-
meanCollection.append(noiseMean[i])
371-
stdDevCollection.append(noiseStdDev[i])
359+
noise_ch_idx = rf[well_ID]["NoiseChIdxs"][noise_start_pos:noise_end_pos]
360+
noise_mean = rf[well_ID]["NoiseMean"][noise_start_pos:noise_end_pos]
361+
# if std is None:
362+
noise_std = rf[well_ID]["NoiseStdDev"][noise_start_pos:noise_end_pos]
363+
# else:
364+
# noise_std = np.repeat(std, noise_end_pos - noise_start_pos)
365+
noise_length = noise_end_pos - noise_start_pos
366+
noise_info = {}
367+
mean_collection = []
368+
std_collection = []
369+
for i in range(1, noise_length):
370+
noise_info[noise_ch_idx[i]] = [noise_mean[i], noise_std[i]]
371+
mean_collection.append(noise_mean[i])
372+
std_collection.append(noise_std[i])
372373
# calculate the median mean and standard deviation of all channels to be used for
373374
# invalid channels
374-
dataMean = np.median(meanCollection)
375-
dataStdDev = np.median(stdDevCollection)
375+
median_mean = np.median(mean_collection)
376+
median_std = np.median(std_collection)
376377
# fill with Gaussian noise
377-
for chIdx in range(len(data)):
378-
if chIdx in noiseInfo:
379-
data[chIdx] = np.array(np.random.normal(noiseInfo[chIdx][0], noiseInfo[chIdx][1],
380-
numFrames), dtype=np.int16)
378+
for ch_idx in range(len(data)):
379+
if ch_idx in noise_info:
380+
data[ch_idx] = np.array(np.random.normal(noise_info[ch_idx][0], noise_info[ch_idx][1],
381+
num_frames), dtype=np.int16)
381382
else:
382-
data[chIdx] = np.array(np.random.normal(dataMean, dataStdDev, numFrames),
383+
data[ch_idx] = np.array(np.random.normal(median_mean, median_std, num_frames),
383384
dtype=np.int16)
384385

385386
return data

0 commit comments

Comments
 (0)