Skip to content

Commit 1d84c9f

Browse files
committed
Expose use_synthetic_noise as an argument
1 parent cc6ae8d commit 1d84c9f

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

neo/io/biocamio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class BiocamIO(BiocamRawIO, BaseFromRaw):
66
__doc__ = BiocamRawIO.__doc__
77
mode = "file"
88

9-
def __init__(self, filename):
10-
BiocamRawIO.__init__(self, filename=filename)
9+
def __init__(self, filename, true_zeroes=False, use_synthetic_noise=False):
10+
BiocamRawIO.__init__(self, filename=filename, true_zeroes=true_zeroes,
11+
use_synthetic_noise=use_synthetic_noise)
1112
BaseFromRaw.__init__(self, filename)

neo/rawio/biocamrawio.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import numpy as np
1919
import json
20+
import warnings
2021

2122

2223
class BiocamRawIO(BaseRawIO):
@@ -47,9 +48,15 @@ class BiocamRawIO(BaseRawIO):
4748
extensions = ["h5", "brw"]
4849
rawmode = "one-file"
4950

50-
def __init__(self, filename=""):
51+
def __init__(self, filename="", true_zeroes=False, use_synthetic_noise=False):
5152
BaseRawIO.__init__(self)
5253
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.")
5360

5461
def _source_name(self):
5562
return self.filename
@@ -122,7 +129,10 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
122129
i_stop = self._num_frames
123130
if channel_indexes is None:
124131
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)
126136
return data[:, channel_indexes]
127137

128138

@@ -201,7 +211,7 @@ def open_biocam_file_header(filename):
201211

202212
wellID = None
203213
for key in rf:
204-
if key[:5] == "Well_":
214+
if key.startswith("Well_"):
205215
wellID = key
206216
num_channels = len(rf[key]["StoredChIdxs"])
207217
if "Raw" in rf[key]:
@@ -258,26 +268,29 @@ def readHDF5t_brw4(rf, t0, t1, nch):
258268
return rf[key]["Raw"][nch * t0 : nch * t1].reshape((t1 - t0, nch), order="C")
259269

260270

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+
263273
noiseStdDev = None
264274
startFrame = t0
265275
numFrames = t1 - t0
266276
for key in rf:
267-
if key[:5] == "Well_":
277+
if key.startswith("Well_"):
268278
wellID = key
269279
break
270280
# initialize an empty (fill with zeros) data collection
271281
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)
272285
# 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)
275288
# 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)
277290
return data.T
278291

279292

280-
def decodeEventBasedRawData(rf, data, wellID, startFrame, numFrames):
293+
def decode_event_based_raw_data(rf, data, wellID, startFrame, numFrames):
281294
# Source: Documentation by 3Brain
282295
# 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
283296
# collect the TOCs
@@ -316,8 +329,9 @@ def decodeEventBasedRawData(rf, data, wellID, startFrame, numFrames):
316329
rangeDataPos += 2
317330
pos += (toExclusive - fromInclusive) * 2
318331

332+
return data
319333

320-
def generateSyntheticNoise(rf, data, wellID, startFrame, numFrames, stdDev=None):
334+
def generate_synthetic_noise(rf, data, wellID, startFrame, numFrames, stdDev=None):
321335
# Source: Documentation by 3Brain
322336
# 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
323337
# collect the TOCs
@@ -368,3 +382,4 @@ def generateSyntheticNoise(rf, data, wellID, startFrame, numFrames, stdDev=None)
368382
data[chIdx] = np.array(np.random.normal(dataMean, dataStdDev, numFrames),
369383
dtype=np.int16)
370384

385+
return data

0 commit comments

Comments
 (0)