Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/spikeinterface/preprocessing/common_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from spikeinterface.core.baserecording import BaseRecording

from .filter import fix_dtype
from functools import cache


class CommonReferenceRecording(BasePreprocessor):
Expand Down Expand Up @@ -83,13 +84,14 @@ def __init__(
groups: list | None = None,
ref_channel_ids: list | str | int | None = None,
local_radius: tuple[float, float] = (30.0, 55.0),
min_local_neighbors: int = 5,
dtype: str | np.dtype | None = None,
):
num_chans = recording.get_num_channels()
neighbors = None
# some checks
if reference not in ("global", "single", "local"):
raise ValueError("'reference' must be either 'global', 'single' or 'local'")
raise ValueError("'reference' must be either 'global', 'single', 'local'")
if operator not in ("median", "average"):
raise ValueError("'operator' must be either 'median', 'average'")

Expand Down Expand Up @@ -117,10 +119,10 @@ def __init__(
closest_inds, dist = get_closest_channels(recording)
neighbors = {}
for i in range(num_chans):
mask = (dist[i, :] > local_radius[0]) & (dist[i, :] <= local_radius[1])
mask = dist[i, :] > local_radius[0]
nn = np.cumsum(mask)
mask &= (dist[i, :] <= local_radius[1]) | ((0 < nn) & (nn <= min_local_neighbors))
neighbors[i] = closest_inds[i, mask]
assert len(neighbors[i]) > 0, "No reference channels available in the local annulus for selection."

dtype_ = fix_dtype(recording, dtype)
BasePreprocessor.__init__(self, recording, dtype=dtype_)

Expand All @@ -136,7 +138,14 @@ def __init__(

for parent_segment in recording._recording_segments:
rec_segment = CommonReferenceRecordingSegment(
parent_segment, reference, operator, group_indices, ref_channel_indices, local_radius, neighbors, dtype_
parent_segment,
reference,
operator,
group_indices,
ref_channel_indices,
local_radius,
neighbors,
dtype_,
)
self.add_recording_segment(rec_segment)

Expand All @@ -147,6 +156,7 @@ def __init__(
operator=operator,
ref_channel_ids=ref_channel_ids,
local_radius=local_radius,
min_local_neighbors=min_local_neighbors,
dtype=dtype_.str,
)

Expand Down
Loading