@@ -145,20 +145,21 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
145
145
# read functions are different based on the version of biocam
146
146
if self ._read_function is readHDF5t_brw4_sparse :
147
147
if self ._fill_gaps_strategy is None :
148
- raise ValueError (
149
- "Please set `fill_gaps_strategy` to 'zeros' or 'synthetic_noise'."
150
- )
148
+ raise ValueError ("Please set `fill_gaps_strategy` to 'zeros' or 'synthetic_noise'." )
151
149
if self ._fill_gaps_strategy == "synthetic_noise" :
152
- warnings .warn ("Event-based compression : gaps will be filled with synthetic noise. "
153
- "Set `fill_gaps_strategy` to 'zeros' to fill gaps with 0s." )
150
+ warnings .warn (
151
+ "Event-based compression : gaps will be filled with synthetic noise. "
152
+ "Set `fill_gaps_strategy` to 'zeros' to fill gaps with 0s."
153
+ )
154
154
use_synthetic_noise = True
155
155
elif self ._fill_gaps_strategy == "zeros" :
156
156
use_synthetic_noise = False
157
157
else :
158
158
raise ValueError ("`fill_gaps_strategy` must be 'zeros' or 'synthetic_noise'" )
159
159
160
- data = self ._read_function (self ._filehandle , i_start , i_stop , self ._num_channels ,
161
- use_synthetic_noise = use_synthetic_noise )
160
+ data = self ._read_function (
161
+ self ._filehandle , i_start , i_stop , self ._num_channels , use_synthetic_noise = use_synthetic_noise
162
+ )
162
163
else :
163
164
data = self ._read_function (self ._filehandle , i_start , i_stop , self ._num_channels )
164
165
@@ -273,7 +274,7 @@ def open_biocam_file_header(filename) -> dict:
273
274
min_digital = experiment_settings ["ValueConverter" ]["MinDigitalValue" ]
274
275
scale_factor = experiment_settings ["ValueConverter" ]["ScaleFactor" ]
275
276
sampling_rate = experiment_settings ["TimeConverter" ]["FrameRate" ]
276
- num_frames = rf [' TOC' ][- 1 ,- 1 ]
277
+ num_frames = rf [" TOC" ][- 1 , - 1 ]
277
278
278
279
num_channels = None
279
280
well_ID = None
@@ -282,7 +283,9 @@ def open_biocam_file_header(filename) -> dict:
282
283
num_channels = len (rf [well_ID ]["StoredChIdxs" ])
283
284
if "Raw" in rf [well_ID ]:
284
285
if len (rf [well_ID ]["Raw" ]) % num_channels :
285
- raise NeoReadWriteError (f"Length of raw data array is not multiple of channel number in { well_ID } " )
286
+ raise NeoReadWriteError (
287
+ f"Length of raw data array is not multiple of channel number in { well_ID } "
288
+ )
286
289
num_frames = len (rf [well_ID ]["Raw" ]) // num_channels
287
290
break
288
291
elif "EventsBasedSparseRaw" in rf [well_ID ]:
@@ -360,7 +363,7 @@ def readHDF5t_brw4_sparse(rf, t0, t1, nch, use_synthetic_noise=False):
360
363
data .fill (2048 )
361
364
else :
362
365
# fill the data collection with Gaussian noise if requested
363
- data = generate_synthetic_noise (rf , data , well_ID , start_frame , num_frames ) # , std=noise_std)
366
+ data = generate_synthetic_noise (rf , data , well_ID , start_frame , num_frames ) # , std=noise_std)
364
367
# fill the data collection with the decoded event based sparse raw data
365
368
data = decode_event_based_raw_data (rf , data , well_ID , start_frame , num_frames )
366
369
@@ -376,38 +379,38 @@ def decode_event_based_raw_data(rf, data, well_ID, start_frame, num_frames):
376
379
# from the given start position and duration in frames, localize the corresponding event positions
377
380
# using the TOC
378
381
toc_start_idx = np .searchsorted (toc [:, 1 ], start_frame )
379
- toc_end_idx = min (
380
- np .searchsorted (toc [:, 1 ], start_frame + num_frames , side = "right" ) + 1 ,
381
- len (toc ) - 1 )
382
+ toc_end_idx = min (np .searchsorted (toc [:, 1 ], start_frame + num_frames , side = "right" ) + 1 , len (toc ) - 1 )
382
383
events_start_pos = events_toc [toc_start_idx ]
383
384
events_end_pos = events_toc [toc_end_idx ]
384
385
# decode all data for the given well ID and time interval
385
386
binary_data = rf [well_ID ]["EventsBasedSparseRaw" ][events_start_pos :events_end_pos ]
386
387
binary_data_length = len (binary_data )
387
388
pos = 0
388
389
while pos < binary_data_length :
389
- ch_idx = int .from_bytes (binary_data [pos : pos + 4 ], byteorder = "little" )
390
+ ch_idx = int .from_bytes (binary_data [pos : pos + 4 ], byteorder = "little" )
390
391
pos += 4
391
- ch_data_length = int .from_bytes (binary_data [pos : pos + 4 ], byteorder = "little" )
392
+ ch_data_length = int .from_bytes (binary_data [pos : pos + 4 ], byteorder = "little" )
392
393
pos += 4
393
394
ch_data_pos = pos
394
395
while pos < ch_data_pos + ch_data_length :
395
- from_inclusive = int .from_bytes (binary_data [pos : pos + 8 ], byteorder = "little" )
396
+ from_inclusive = int .from_bytes (binary_data [pos : pos + 8 ], byteorder = "little" )
396
397
pos += 8
397
- to_exclusive = int .from_bytes (binary_data [pos : pos + 8 ], byteorder = "little" )
398
+ to_exclusive = int .from_bytes (binary_data [pos : pos + 8 ], byteorder = "little" )
398
399
pos += 8
399
400
range_data_pos = pos
400
401
for j in range (from_inclusive , to_exclusive ):
401
402
if j >= start_frame + num_frames :
402
403
break
403
404
if j >= start_frame :
404
405
data [ch_idx ][j - start_frame ] = int .from_bytes (
405
- binary_data [range_data_pos :range_data_pos + 2 ], byteorder = "little" )
406
+ binary_data [range_data_pos : range_data_pos + 2 ], byteorder = "little"
407
+ )
406
408
range_data_pos += 2
407
409
pos += (to_exclusive - from_inclusive ) * 2
408
410
409
411
return data
410
412
413
+
411
414
def generate_synthetic_noise (rf , data , well_ID , start_frame , num_frames ):
412
415
# Source: Documentation by 3Brain
413
416
# 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
@@ -451,10 +454,10 @@ def generate_synthetic_noise(rf, data, well_ID, start_frame, num_frames):
451
454
# fill with Gaussian noise
452
455
for ch_idx in range (len (data )):
453
456
if ch_idx in noise_info :
454
- data [ch_idx ] = np .array (np .random .normal (noise_info [ch_idx ][0 ], noise_info [ch_idx ][1 ],
455
- num_frames ), dtype = np .uint16 )
457
+ data [ch_idx ] = np .array (
458
+ np .random .normal (noise_info [ch_idx ][0 ], noise_info [ch_idx ][1 ], num_frames ), dtype = np .uint16
459
+ )
456
460
else :
457
- data [ch_idx ] = np .array (np .random .normal (median_mean , median_std , num_frames ),
458
- dtype = np .uint16 )
461
+ data [ch_idx ] = np .array (np .random .normal (median_mean , median_std , num_frames ), dtype = np .uint16 )
459
462
460
463
return data
0 commit comments