-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspikeinterface_batch.py
More file actions
executable file
·216 lines (184 loc) · 6.08 KB
/
spikeinterface_batch.py
File metadata and controls
executable file
·216 lines (184 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
"""
SpikeInterface batch pipeline for Open Ephys recordings using Kilosort 4.
This version is SLURM-compatible and accepts CLI arguments for flexible deployment.
"""
import argparse
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import xml.etree.ElementTree as ET
from spikeinterface import extract_waveforms
from spikeinterface.extractors import read_openephys
from spikeinterface.preprocessing import (
phase_shift,
bandpass_filter,
common_reference,
scale_to_physical_units,
)
from spikeinterface.sorters import run_sorter
from spikeinterface.qualitymetrics import compute_quality_metrics
from spikeinterface import curation
from spikeinterface.widgets import plot_traces
from probeinterface.plotting import plot_probe
def get_scaled_recording(recording):
return scale_to_physical_units(recording)
# ---------------------------
# Argument Parsing
# ---------------------------
parser = argparse.ArgumentParser(
description="SpikeInterface SLURM-compatible pipeline with Kilosort 4"
)
parser.add_argument("data_path", help="Path to the Open Ephys recording folder")
parser.add_argument(
"--output_path",
default="./spike_sorting_output",
help="Output directory for results",
)
parser.add_argument(
"--show_probe", action="store_true", help="Plot probe layout"
)
parser.add_argument(
"--show_preprocessing",
action="store_true",
help="Plot preprocessing traces",
)
args = parser.parse_args()
# ---------------------------
# Paths
# ---------------------------
data_path = Path(args.data_path).resolve()
output_path = Path(args.output_path).resolve()
plot_path = output_path / "plots"
plot_path.mkdir(parents=True, exist_ok=True)
# ---------------------------
# Load and Trim Recording (full duration)
# ---------------------------
print(f"Loading recording from: {data_path}")
raw_recording = read_openephys(data_path, stream_id="0")
fs = raw_recording.get_sampling_frequency()
print(f"Recording dtype: {raw_recording.get_dtype()}")
print(f"Duration: {raw_recording.get_num_frames() / fs:.1f} seconds")
# ---------------------------
# Attach Probe Geometry
# ---------------------------
if "location" not in raw_recording.get_property_keys():
print("No channel locations — extracting from settings.xml")
settings_path = data_path.parent.parent / "settings.xml"
if not settings_path.exists():
raise FileNotFoundError(f"Missing: {settings_path}")
tree = ET.parse(settings_path)
root = tree.getroot()
xpos, ypos = [], []
for ch in range(384):
x = float(root.find(".//ELECTRODE_XPOS").get(f"CH{ch}"))
y = float(root.find(".//ELECTRODE_YPOS").get(f"CH{ch}"))
xpos.append(x)
ypos.append(y)
coords = np.column_stack((xpos, ypos))
raw_recording.set_property("location", coords)
else:
print("Probe locations already present.")
if args.show_probe:
print("Saving probe layout plot...")
plot_probe(raw_recording.get_probe())
plt.savefig(plot_path / "probe_layout.png", dpi=300, bbox_inches="tight")
plt.close()
# ---------------------------
# Preprocessing
# ---------------------------
if "inter_sample_shift" in raw_recording.get_property_keys():
shifted_recording = phase_shift(raw_recording)
recording_for_filter = shifted_recording
print("Applied phase shift.")
else:
recording_for_filter = raw_recording
print("Skipped phase shift.")
filtered_recording = bandpass_filter(
recording_for_filter, freq_min=300, freq_max=6000
)
channel_group = filtered_recording.get_property("group")
channel_ids = filtered_recording.get_channel_ids()
if channel_group is None:
split_channel_ids = [channel_ids.tolist()]
print(f"No 'group' property — using all {len(channel_ids)} channels.")
else:
split_channel_ids = [
channel_ids[channel_group == idx].tolist()
for idx in np.unique(channel_group)
]
print(f"Found {len(split_channel_ids)} channel groups.")
preprocessed_recording = common_reference(
filtered_recording,
reference="global",
operator="median",
groups=split_channel_ids,
)
# ---------------------------
# Plot Preprocessed Signal (Optional)
# ---------------------------
if args.show_preprocessing:
print("Plotting preprocessed signal...")
plt.figure(figsize=(20, 24))
plot_traces(
get_scaled_recording(preprocessed_recording),
time_range=(10, 11),
order_channel_by_depth=True,
show_channel_ids=False,
mode="map",
return_scaled=True,
clim=(-150, 150),
)
plt.xlabel("Time (s)")
plt.ylabel("Depth (µm)")
plt.title("Preprocessed Signal Map")
plt.grid(axis="y", linestyle="--", linewidth=0.3, alpha=0.4)
plt.tight_layout()
plt.savefig(
plot_path / "preprocessing_full.png", dpi=200, bbox_inches="tight"
)
plt.close()
# ---------------------------
# Run Kilosort 4
# ---------------------------
print("Running Kilosort 4...")
preprocessed_recording.set_property(
"group", np.zeros(len(channel_ids), dtype=int)
)
sorting = run_sorter(
"kilosort4",
preprocessed_recording,
(output_path / "sorting").as_posix(),
singularity_image=True,
remove_existing_folder=True,
)
# ---------------------------
# Curate and Extract Waveforms
# ---------------------------
sorting = sorting.remove_empty_units()
sorting = curation.remove_excess_spikes(sorting, preprocessed_recording)
print("Extracting waveforms...")
waveforms = extract_waveforms(
preprocessed_recording,
sorting,
folder=(output_path / "postprocessing").as_posix(),
ms_before=2,
ms_after=2,
max_spikes_per_unit=500,
return_scaled=True,
sparse=True,
peak_sign="neg",
method="radius",
radius_um=75,
)
# ---------------------------
# Compute Quality Metrics
# ---------------------------
print("Computing quality metrics...")
quality_metrics = compute_quality_metrics(waveforms)
quality_metrics.to_csv(output_path / "postprocessing" / "quality_metrics.csv")
print(
"Done. Quality metrics saved to:",
output_path / "postprocessing" / "quality_metrics.csv",
)
print("All plots saved to:", plot_path)