Skip to content

Commit d7dcc6b

Browse files
authored
Fix non-referencing option in lfp artifact detection (#863)
* fix non-referencing option in lfp artifact detection * update changelog
1 parent a689162 commit d7dcc6b

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
- Increase the required `spikeinterface` version to >=0.99.1 for `get_sorting` method associated with `SpikeSorting` and `CurationV1` tables in spike sorting V1 pipeline. Limit version to <0.100 in case there are other issues with it. #852
2222
- Bug fix in single artifact interval edge case #859
2323

24+
- LFP
25+
- In LFPArtifactDetection, only apply referencing if explicitly selected #863
26+
2427
## [0.5.0] (February 9, 2024)
2528

2629
### Infrastructure

src/spyglass/lfp/v1/lfp_artifact.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,41 @@ def make(self, key):
132132

133133
algorithm = artifact_params["artifact_detection_algorithm"]
134134
params = artifact_params["artifact_detection_algorithm_params"]
135-
lfp_band_ref_id = artifact_params["referencing"]["reference_list"]
136135

137136
# get LFP data
138137
lfp_eseries = (LFPV1 & key).fetch_nwb()[0]["lfp"]
139138
sampling_frequency = (LFPV1 & key).fetch("lfp_sampling_rate")[0]
140-
141-
# do referencing at this step
142139
lfp_data = np.asarray(
143140
lfp_eseries.data[:, :],
144141
dtype=type(lfp_eseries.data[0][0]),
145142
)
146143

147-
lfp_band_ref_index = get_electrode_indices(lfp_eseries, lfp_band_ref_id)
148-
lfp_band_elect_index = get_electrode_indices(
149-
lfp_eseries, artifact_params["referencing"]["electrode_list"]
150-
)
151-
152-
# maybe this lfp_elec_list is supposed to be a list on indices
153-
154-
for index, elect_index in enumerate(lfp_band_elect_index):
155-
if lfp_band_ref_id[index] == -1:
156-
continue
157-
lfp_data[:, elect_index] = (
158-
lfp_data[:, elect_index]
159-
- lfp_data[:, lfp_band_ref_index[index]]
160-
)
161-
162144
is_diff = algorithm == "difference"
145+
# do referencing at this step
146+
if "referencing" in artifact_params:
147+
ref = artifact_params["referencing"]["ref_on"] if is_diff else None
148+
lfp_band_ref_id = artifact_params["referencing"]["reference_list"]
149+
if artifact_params["referencing"]["ref_on"]:
150+
lfp_band_ref_index = get_electrode_indices(
151+
lfp_eseries, lfp_band_ref_id
152+
)
153+
lfp_band_elect_index = get_electrode_indices(
154+
lfp_eseries,
155+
artifact_params["referencing"]["electrode_list"],
156+
)
157+
158+
# maybe this lfp_elec_list is supposed to be a list on indices
159+
for index, elect_index in enumerate(lfp_band_elect_index):
160+
if lfp_band_ref_id[index] == -1:
161+
continue
162+
lfp_data[:, elect_index] = (
163+
lfp_data[:, elect_index]
164+
- lfp_data[:, lfp_band_ref_index[index]]
165+
)
166+
else:
167+
ref = False if is_diff else None
168+
163169
data = lfp_data if is_diff else lfp_eseries
164-
ref = artifact_params["referencing"]["ref_on"] if is_diff else None
165170

166171
(
167172
artifact_removed_valid_times,

src/spyglass/lfp/v1/lfp_artifact_difference_detection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def difference_artifact_detector(
2424
removal_window_ms: float = 1.0,
2525
local_window_ms: float = 1.0,
2626
sampling_frequency: float = 1000.0,
27-
referencing: int = 0,
27+
referencing: bool = False,
2828
):
2929
"""Detects times during which artifacts do and do not occur.
3030
@@ -52,6 +52,8 @@ def difference_artifact_detector(
5252
removal_window_ms : float, optional
5353
Width of the window in milliseconds to mask out per artifact (window/2
5454
removed on each side of threshold crossing), defaults to 1 ms
55+
referencing : bool, optional
56+
Whether or not the data passed to this function is referenced, defaults to False
5557
5658
Returns
5759
-------
@@ -66,7 +68,7 @@ def difference_artifact_detector(
6668
# NOTE: 7-17-23 updated to remove recording.data, since it will converted to
6769
# numpy array before referencing check for referencing flag
6870

69-
if referencing == 1:
71+
if referencing:
7072
logger.info("referencing activated. may be set to -1")
7173

7274
# valid_timestamps = recording.timestamps

0 commit comments

Comments
 (0)