Skip to content

Commit cbacc4c

Browse files
committed
fix: apodization_factor -> apodization_samples and move scipy import
1 parent 1586fa5 commit cbacc4c

3 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/spikeinterface/preprocessing/detect_artifacts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ class DetectAndRemoveArtifactsRecording(SilencedPeriodsRecording):
718718
- "noise": The periods are filled with a gaussion noise that has the
719719
same variance that the one in the recordings, on a per channel
720720
basis
721-
- "apodization": The periods zeroed, but are apodized with a cosine taper (using `apodization_factor`)
722-
apodization_factor : int, default: 7
721+
- "apodization": The periods zeroed, but are apodized with a cosine taper (using `apodization_samples`)
722+
apodization_samples : int, default: 7
723723
The factor used for the cosine taper when mode is "apodization". Higher values create a wider taper.
724724
seed : int | None, default: None
725725
Random seed for `get_noise_levels` and `NoiseGeneratorRecording`.

src/spikeinterface/preprocessing/silence_periods.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import scipy.signal
32

43
from spikeinterface.core.core_tools import define_function_handling_dict_from_class
54
from .basepreprocessor import BasePreprocessor, BasePreprocessorSegment
@@ -34,8 +33,8 @@ class SilencedPeriodsRecording(BasePreprocessor):
3433
- "noise": The periods are filled with a gaussion noise that has the
3534
same variance that the one in the recordings, on a per channel
3635
basis
37-
- "apodization": The periods zeroed, but are apodized with a cosine taper (using `apodization_factor`)
38-
apodization_factor : int, default: 7
36+
- "apodization": The periods zeroed, but are apodized with a cosine taper (using `apodization_samples`)
37+
apodization_samples : int, default: 7
3938
The factor used for the cosine taper when mode is "apodization". Higher values create a wider taper.
4039
noise_levels : array
4140
Noise levels if already computed
@@ -57,7 +56,7 @@ def __init__(
5756
# this is kept for backward compatibility
5857
list_periods=None,
5958
mode="zeros",
60-
apodization_factor=7,
59+
apodization_samples=7,
6160
noise_levels=None,
6261
seed=None,
6362
**noise_levels_kwargs,
@@ -120,7 +119,7 @@ def __init__(
120119
mode,
121120
noise_generator,
122121
seg_index,
123-
apodization_factor=apodization_factor,
122+
apodization_samples=apodization_samples,
124123
)
125124
self.add_recording_segment(rec_segment)
126125

@@ -130,7 +129,7 @@ def __init__(
130129
mode=mode,
131130
seed=seed,
132131
noise_levels=noise_levels,
133-
apodization_factor=apodization_factor,
132+
apodization_samples=apodization_samples,
134133
)
135134

136135

@@ -173,13 +172,13 @@ def _check_periods(periods, num_seg):
173172

174173

175174
class SilencedPeriodsRecordingSegment(BasePreprocessorSegment):
176-
def __init__(self, parent_recording_segment, periods, mode, noise_generator, seg_index, apodization_factor=7):
175+
def __init__(self, parent_recording_segment, periods, mode, noise_generator, seg_index, apodization_samples=7):
177176
BasePreprocessorSegment.__init__(self, parent_recording_segment)
178177
self.periods = periods
179178
self.mode = mode
180179
self.seg_index = seg_index
181180
self.noise_generator = noise_generator
182-
self.apodization_factor = apodization_factor
181+
self.apodization_samples = apodization_samples
183182

184183
def get_traces(self, start_frame, end_frame, channel_indices):
185184
traces = self.parent_recording_segment.get_traces(start_frame, end_frame, channel_indices)
@@ -206,10 +205,12 @@ def get_traces(self, start_frame, end_frame, channel_indices):
206205
]
207206
traces[onset:offset, :] = noise[onset:offset]
208207
elif self.mode == "apodization":
208+
import scipy.signal
209+
209210
# apply a cosine taper to the saturation to create a mute function
210211
mute = np.zeros(traces.shape[0], dtype=np.float32)
211212
mute[onset:offset] = 1
212-
win = scipy.signal.windows.cosine(self.apodization_factor)
213+
win = scipy.signal.windows.cosine(self.apodization_samples)
213214
mute = np.maximum(0, 1 - scipy.signal.convolve(mute, win, mode="same"))
214215
traces = (traces.astype(np.float32) * mute[:, np.newaxis]).astype(traces.dtype)
215216
return traces

src/spikeinterface/preprocessing/tests/test_silence_periods.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def test_silence(create_cache_folder):
4848
assert not np.all(traces_mix[:-200] == 0)
4949

5050
# test that the apodization creates a taper
51-
apodization_factor = 10
52-
rec2 = silence_periods(rec, periods=periods, mode="apodization", apodization_factor=apodization_factor)
51+
apodization_samples = 10
52+
rec2 = silence_periods(rec, periods=periods, mode="apodization", apodization_samples=apodization_samples)
5353
rec2 = rec2.save(format="memory", verbose=False, overwrite=True)
5454
traces_in0 = rec2.get_traces(segment_index=0, start_frame=0, end_frame=1000)
5555
traces_in1 = rec2.get_traces(segment_index=0, start_frame=5000, end_frame=6000)
@@ -58,10 +58,10 @@ def test_silence(create_cache_folder):
5858
assert np.all(traces_in1 == 0)
5959

6060
# at margins, traces should not be all zero, but should be apodized
61-
apodized_traces_in0 = rec2.get_traces(segment_index=0, start_frame=1000, end_frame=1000 + apodization_factor)
62-
apodized_traces_in1 = rec2.get_traces(segment_index=0, start_frame=5000 - apodization_factor, end_frame=5000)
63-
traces_raw_in0 = rec.get_traces(segment_index=0, start_frame=1000, end_frame=1000 + apodization_factor)
64-
traces_raw_in1 = rec.get_traces(segment_index=0, start_frame=5000 - apodization_factor, end_frame=5000)
61+
apodized_traces_in0 = rec2.get_traces(segment_index=0, start_frame=1000, end_frame=1000 + apodization_samples)
62+
apodized_traces_in1 = rec2.get_traces(segment_index=0, start_frame=5000 - apodization_samples, end_frame=5000)
63+
traces_raw_in0 = rec.get_traces(segment_index=0, start_frame=1000, end_frame=1000 + apodization_samples)
64+
traces_raw_in1 = rec.get_traces(segment_index=0, start_frame=5000 - apodization_samples, end_frame=5000)
6565
# the apodized traces should be less than the raw traces in absolute value,
6666
# since they are multiplied by a cosine taper between 0 and 1
6767
assert np.all(np.abs(apodized_traces_in0) <= np.abs(traces_raw_in0))

0 commit comments

Comments
 (0)