|
17 | 17 |
|
18 | 18 | import numpy as np |
19 | 19 | import json |
| 20 | +import warnings |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class BiocamRawIO(BaseRawIO): |
@@ -47,9 +48,15 @@ class BiocamRawIO(BaseRawIO): |
47 | 48 | extensions = ["h5", "brw"] |
48 | 49 | rawmode = "one-file" |
49 | 50 |
|
50 | | - def __init__(self, filename=""): |
| 51 | + def __init__(self, filename="", true_zeroes=False, use_synthetic_noise=False): |
51 | 52 | BaseRawIO.__init__(self) |
52 | 53 | self.filename = filename |
| 54 | + self.true_zeroes = true_zeroes |
| 55 | + self.use_synthetic_noise = use_synthetic_noise |
| 56 | + |
| 57 | + if self.use_synthetic_noise: |
| 58 | + warnings.warn("Event-based compression : gaps will be filled with synthetic noise." |
| 59 | + "Traces won't be raw.") |
53 | 60 |
|
54 | 61 | def _source_name(self): |
55 | 62 | return self.filename |
@@ -122,7 +129,10 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea |
122 | 129 | i_stop = self._num_frames |
123 | 130 | if channel_indexes is None: |
124 | 131 | channel_indexes = slice(None) |
125 | | - data = self._read_function(self._filehandle, i_start, i_stop, self._num_channels) |
| 132 | + 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) |
| 134 | + else: |
| 135 | + data = self._read_function(self._filehandle, i_start, i_stop, self._num_channels) |
126 | 136 | return data[:, channel_indexes] |
127 | 137 |
|
128 | 138 |
|
@@ -201,7 +211,7 @@ def open_biocam_file_header(filename): |
201 | 211 |
|
202 | 212 | wellID = None |
203 | 213 | for key in rf: |
204 | | - if key[:5] == "Well_": |
| 214 | + if key.startswith("Well_"): |
205 | 215 | wellID = key |
206 | 216 | num_channels = len(rf[key]["StoredChIdxs"]) |
207 | 217 | if "Raw" in rf[key]: |
@@ -258,26 +268,29 @@ def readHDF5t_brw4(rf, t0, t1, nch): |
258 | 268 | return rf[key]["Raw"][nch * t0 : nch * t1].reshape((t1 - t0, nch), order="C") |
259 | 269 |
|
260 | 270 |
|
261 | | -def readHDF5t_brw4_sparse(rf, t0, t1, nch): |
262 | | - useSyntheticNoise = True |
| 271 | +def readHDF5t_brw4_sparse(rf, t0, t1, nch, true_zeroes=False, use_synthetic_noise=False): |
| 272 | + |
263 | 273 | noiseStdDev = None |
264 | 274 | startFrame = t0 |
265 | 275 | numFrames = t1 - t0 |
266 | 276 | for key in rf: |
267 | | - if key[:5] == "Well_": |
| 277 | + if key.startswith("Well_"): |
268 | 278 | wellID = key |
269 | 279 | break |
270 | 280 | # initialize an empty (fill with zeros) data collection |
271 | 281 | data = np.zeros((nch, numFrames), dtype=np.int16) |
| 282 | + if not true_zeroes: |
| 283 | + # Will read as 0s after 12 bits signed conversion |
| 284 | + data.fill(2048) |
272 | 285 | # fill the data collection with Gaussian noise if requested |
273 | | - if useSyntheticNoise: |
274 | | - generateSyntheticNoise(rf, data, wellID, startFrame, numFrames, stdDev=noiseStdDev) |
| 286 | + if use_synthetic_noise: |
| 287 | + data = generate_synthetic_noise(rf, data, wellID, startFrame, numFrames, stdDev=noiseStdDev) |
275 | 288 | # fill the data collection with the decoded event based sparse raw data |
276 | | - decodeEventBasedRawData(rf, data, wellID, startFrame, numFrames) |
| 289 | + data = decode_event_based_raw_data(rf, data, wellID, startFrame, numFrames) |
277 | 290 | return data.T |
278 | 291 |
|
279 | 292 |
|
280 | | -def decodeEventBasedRawData(rf, data, wellID, startFrame, numFrames): |
| 293 | +def decode_event_based_raw_data(rf, data, wellID, startFrame, numFrames): |
281 | 294 | # Source: Documentation by 3Brain |
282 | 295 | # 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 |
283 | 296 | # collect the TOCs |
@@ -316,8 +329,9 @@ def decodeEventBasedRawData(rf, data, wellID, startFrame, numFrames): |
316 | 329 | rangeDataPos += 2 |
317 | 330 | pos += (toExclusive - fromInclusive) * 2 |
318 | 331 |
|
| 332 | + return data |
319 | 333 |
|
320 | | -def generateSyntheticNoise(rf, data, wellID, startFrame, numFrames, stdDev=None): |
| 334 | +def generate_synthetic_noise(rf, data, wellID, startFrame, numFrames, stdDev=None): |
321 | 335 | # Source: Documentation by 3Brain |
322 | 336 | # 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 |
323 | 337 | # collect the TOCs |
@@ -368,3 +382,4 @@ def generateSyntheticNoise(rf, data, wellID, startFrame, numFrames, stdDev=None) |
368 | 382 | data[chIdx] = np.array(np.random.normal(dataMean, dataStdDev, numFrames), |
369 | 383 | dtype=np.int16) |
370 | 384 |
|
| 385 | + return data |
0 commit comments