@@ -145,20 +145,21 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
145145 # read functions are different based on the version of biocam
146146 if self ._read_function is readHDF5t_brw4_sparse :
147147 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'." )
151149 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+ )
154154 use_synthetic_noise = True
155155 elif self ._fill_gaps_strategy == "zeros" :
156156 use_synthetic_noise = False
157157 else :
158158 raise ValueError ("`fill_gaps_strategy` must be 'zeros' or 'synthetic_noise'" )
159159
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+ )
162163 else :
163164 data = self ._read_function (self ._filehandle , i_start , i_stop , self ._num_channels )
164165
@@ -273,7 +274,7 @@ def open_biocam_file_header(filename) -> dict:
273274 min_digital = experiment_settings ["ValueConverter" ]["MinDigitalValue" ]
274275 scale_factor = experiment_settings ["ValueConverter" ]["ScaleFactor" ]
275276 sampling_rate = experiment_settings ["TimeConverter" ]["FrameRate" ]
276- num_frames = rf [' TOC' ][- 1 ,- 1 ]
277+ num_frames = rf [" TOC" ][- 1 , - 1 ]
277278
278279 num_channels = None
279280 well_ID = None
@@ -282,7 +283,9 @@ def open_biocam_file_header(filename) -> dict:
282283 num_channels = len (rf [well_ID ]["StoredChIdxs" ])
283284 if "Raw" in rf [well_ID ]:
284285 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+ )
286289 num_frames = len (rf [well_ID ]["Raw" ]) // num_channels
287290 break
288291 elif "EventsBasedSparseRaw" in rf [well_ID ]:
@@ -360,7 +363,7 @@ def readHDF5t_brw4_sparse(rf, t0, t1, nch, use_synthetic_noise=False):
360363 data .fill (2048 )
361364 else :
362365 # 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)
364367 # fill the data collection with the decoded event based sparse raw data
365368 data = decode_event_based_raw_data (rf , data , well_ID , start_frame , num_frames )
366369
@@ -376,38 +379,38 @@ def decode_event_based_raw_data(rf, data, well_ID, start_frame, num_frames):
376379 # from the given start position and duration in frames, localize the corresponding event positions
377380 # using the TOC
378381 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 )
382383 events_start_pos = events_toc [toc_start_idx ]
383384 events_end_pos = events_toc [toc_end_idx ]
384385 # decode all data for the given well ID and time interval
385386 binary_data = rf [well_ID ]["EventsBasedSparseRaw" ][events_start_pos :events_end_pos ]
386387 binary_data_length = len (binary_data )
387388 pos = 0
388389 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" )
390391 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" )
392393 pos += 4
393394 ch_data_pos = pos
394395 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" )
396397 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" )
398399 pos += 8
399400 range_data_pos = pos
400401 for j in range (from_inclusive , to_exclusive ):
401402 if j >= start_frame + num_frames :
402403 break
403404 if j >= start_frame :
404405 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+ )
406408 range_data_pos += 2
407409 pos += (to_exclusive - from_inclusive ) * 2
408410
409411 return data
410412
413+
411414def generate_synthetic_noise (rf , data , well_ID , start_frame , num_frames ):
412415 # Source: Documentation by 3Brain
413416 # 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):
451454 # fill with Gaussian noise
452455 for ch_idx in range (len (data )):
453456 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+ )
456460 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 )
459462
460463 return data
0 commit comments