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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404 | class SGLXKilosortPipeline:
"""
An object of SGLXKilosortPipeline manages the state of the Kilosort data processing pipeline
for one Neuropixels probe in one recording session using the Spike GLX acquisition software.
Primarily calling routines specified from:
https://github.com/jenniferColonell/ecephys_spike_sorting
"""
_modules = [
"kilosort_helper",
"kilosort_postprocessing",
"noise_templates",
"mean_waveforms",
"quality_metrics",
]
_default_catgt_params = {
"catGT_car_mode": "gblcar",
"catGT_loccar_min_um": 40,
"catGT_loccar_max_um": 160,
"catGT_cmd_string": "-prb_fld -out_prb_fld -gfix=0.4,0.10,0.02",
"ni_present": False,
"ni_extract_string": "-XA=0,1,3,500 -iXA=1,3,3,0 -XD=-1,1,50 -XD=-1,2,1.7 -XD=-1,3,5 -iXD=-1,3,5",
}
_input_json_args = list(inspect.signature(createInputJson).parameters)
def __init__(
self,
npx_input_dir: str,
ks_output_dir: str,
params: dict,
KS2ver: str,
run_CatGT=False,
ni_present=False,
ni_extract_string=None,
):
self._npx_input_dir = pathlib.Path(npx_input_dir)
self._ks_output_dir = pathlib.Path(ks_output_dir)
self._ks_output_dir.mkdir(parents=True, exist_ok=True)
self._params = params
self._KS2ver = KS2ver
self._run_CatGT = run_CatGT
self._run_CatGT = run_CatGT
self._default_catgt_params["ni_present"] = ni_present
self._default_catgt_params["ni_extract_string"] = (
ni_extract_string or self._default_catgt_params["ni_extract_string"]
)
self._json_directory = self._ks_output_dir / "json_configs"
self._json_directory.mkdir(parents=True, exist_ok=True)
self._module_input_json = (
self._json_directory / f"{self._npx_input_dir.name}-input.json"
)
self._module_logfile = (
self._json_directory / f"{self._npx_input_dir.name}-run_modules-log.txt"
)
self._CatGT_finished = False
self.ks_input_params = None
self._modules_input_hash = None
self._modules_input_hash_fp = None
def parse_input_filename(self):
meta_filename = next(self._npx_input_dir.glob("*.ap.meta")).name
match = re.search("(.*)_g(\d)_t(\d+|cat)\.imec(\d?)\.ap\.meta", meta_filename)
session_str, gate_str, trigger_str, probe_str = match.groups()
return session_str, gate_str, trigger_str, probe_str or "0"
def generate_CatGT_input_json(self):
if not self._run_CatGT:
print("run_CatGT is set to False, skipping...")
return
session_str, gate_str, trig_str, probe_str = self.parse_input_filename()
first_trig, last_trig = SpikeGLX_utils.ParseTrigStr(
"start,end", probe_str, gate_str, self._npx_input_dir.as_posix()
)
trigger_str = repr(first_trig) + "," + repr(last_trig)
self._catGT_input_json = (
self._json_directory / f"{session_str}{probe_str}_CatGT-input.json"
)
catgt_params = {
k: self._params.get(k, v) for k, v in self._default_catgt_params.items()
}
ni_present = catgt_params.pop("ni_present")
ni_extract_string = catgt_params.pop("ni_extract_string")
catgt_params["catGT_stream_string"] = "-ap -ni" if ni_present else "-ap"
sync_extract = "-SY=" + probe_str + ",-1,6,500"
extract_string = sync_extract + (f" {ni_extract_string}" if ni_present else "")
catgt_params["catGT_cmd_string"] += f" {extract_string}"
input_meta_fullpath, continuous_file = self._get_raw_data_filepaths()
# create symbolic link to the actual data files - as CatGT expects files to follow a certain naming convention
continuous_file_symlink = (
continuous_file.parent
/ f"{session_str}_g{gate_str}"
/ f"{session_str}_g{gate_str}_imec{probe_str}"
/ f"{session_str}_g{gate_str}_t{trig_str}.imec{probe_str}.ap.bin"
)
continuous_file_symlink.parent.mkdir(parents=True, exist_ok=True)
if continuous_file_symlink.exists():
continuous_file_symlink.unlink()
continuous_file_symlink.symlink_to(continuous_file)
input_meta_fullpath_symlink = (
input_meta_fullpath.parent
/ f"{session_str}_g{gate_str}"
/ f"{session_str}_g{gate_str}_imec{probe_str}"
/ f"{session_str}_g{gate_str}_t{trig_str}.imec{probe_str}.ap.meta"
)
input_meta_fullpath_symlink.parent.mkdir(parents=True, exist_ok=True)
if input_meta_fullpath_symlink.exists():
input_meta_fullpath_symlink.unlink()
input_meta_fullpath_symlink.symlink_to(input_meta_fullpath)
createInputJson(
self._catGT_input_json.as_posix(),
KS2ver=self._KS2ver,
npx_directory=self._npx_input_dir.as_posix(),
spikeGLX_data=True,
catGT_run_name=session_str,
gate_string=gate_str,
trigger_string=trigger_str,
probe_string=probe_str,
continuous_file=continuous_file.as_posix(),
input_meta_path=input_meta_fullpath.as_posix(),
extracted_data_directory=self._ks_output_dir.as_posix(),
kilosort_output_directory=self._ks_output_dir.as_posix(),
kilosort_output_tmp=self._ks_output_dir.as_posix(),
kilosort_repository=_get_kilosort_repository(self._KS2ver),
**{k: v for k, v in catgt_params.items() if k in self._input_json_args},
)
def run_CatGT(self, force_rerun=False):
if self._run_CatGT and (not self._CatGT_finished or force_rerun):
self.generate_CatGT_input_json()
print("---- Running CatGT ----")
catGT_input_json = self._catGT_input_json.as_posix()
catGT_output_json = catGT_input_json.replace(
"CatGT-input.json", "CatGT-output.json"
)
command = (
sys.executable
+ " -W ignore -m ecephys_spike_sorting.modules."
+ "catGT_helper"
+ " --input_json "
+ catGT_input_json
+ " --output_json "
+ catGT_output_json
)
subprocess.check_call(command.split(" "))
self._CatGT_finished = True
def generate_modules_input_json(self):
session_str, _, _, probe_str = self.parse_input_filename()
self._module_input_json = (
self._json_directory / f"{session_str}_imec{probe_str}-input.json"
)
input_meta_fullpath, continuous_file = self._get_raw_data_filepaths()
params = {}
for k, v in self._params.items():
value = str(v) if isinstance(v, list) else v
if f"ks_{k}" in self._input_json_args:
params[f"ks_{k}"] = value
if k in self._input_json_args:
params[k] = value
self.ks_input_params = createInputJson(
self._module_input_json.as_posix(),
KS2ver=self._KS2ver,
npx_directory=self._npx_input_dir.as_posix(),
spikeGLX_data=True,
continuous_file=continuous_file.as_posix(),
input_meta_path=input_meta_fullpath.as_posix(),
extracted_data_directory=self._ks_output_dir.as_posix(),
kilosort_output_directory=self._ks_output_dir.as_posix(),
kilosort_output_tmp=self._ks_output_dir.as_posix(),
ks_make_copy=True,
noise_template_use_rf=self._params.get("noise_template_use_rf", False),
c_Waves_snr_um=self._params.get("c_Waves_snr_um", 160),
qm_isi_thresh=self._params.get("refPerMS", 2.0) / 1000,
kilosort_repository=_get_kilosort_repository(self._KS2ver),
**params,
)
self._modules_input_hash = dict_to_uuid(dict(self._params, KS2ver=self._KS2ver))
def run_modules(self, modules_to_run=None):
if self._run_CatGT and not self._CatGT_finished:
self.run_CatGT()
print("---- Running Modules ----")
self.generate_modules_input_json()
module_input_json = self._module_input_json.as_posix()
module_logfile = self._module_logfile.as_posix()
modules = modules_to_run or self._modules
for module in modules:
module_status = self._get_module_status(module)
if module_status["completion_time"] is not None:
continue
module_output_json = self._get_module_output_json_filename(module)
command = (
sys.executable
+ " -W ignore -m ecephys_spike_sorting.modules."
+ module
+ " --input_json "
+ module_input_json
+ " --output_json "
+ module_output_json
)
start_time = datetime.utcnow()
self._update_module_status(
{
module: {
"start_time": start_time,
"completion_time": None,
"duration": None,
}
}
)
with open(module_logfile, "a") as f:
subprocess.check_call(command.split(" "), stdout=f)
completion_time = datetime.utcnow()
self._update_module_status(
{
module: {
"start_time": start_time,
"completion_time": completion_time,
"duration": (completion_time - start_time).total_seconds(),
}
}
)
self._update_total_duration()
def _get_raw_data_filepaths(self):
session_str, gate_str, _, probe_str = self.parse_input_filename()
if self._CatGT_finished:
catGT_dest = self._ks_output_dir
run_str = session_str + "_g" + gate_str
run_folder = "catgt_" + run_str
prb_folder = run_str + "_imec" + probe_str
data_directory = catGT_dest / run_folder / prb_folder
else:
data_directory = self._npx_input_dir
try:
meta_fp = next(data_directory.glob(f"{session_str}*.ap.meta"))
bin_fp = next(data_directory.glob(f"{session_str}*.ap.bin"))
except StopIteration:
raise RuntimeError(
f"No ap meta/bin files found in {data_directory} - CatGT error?"
)
return meta_fp, bin_fp
def _update_module_status(self, updated_module_status={}):
if self._modules_input_hash is None:
raise RuntimeError('"generate_modules_input_json()" not yet performed!')
self._modules_input_hash_fp = (
self._json_directory / f".{self._modules_input_hash}.json"
)
if self._modules_input_hash_fp.exists():
with open(self._modules_input_hash_fp) as f:
modules_status = json.load(f)
modules_status = {**modules_status, **updated_module_status}
else:
# handle cases of processing rerun on different parameters (the hash changes)
# delete outdated files
[
f.unlink()
for f in self._json_directory.glob("*")
if f.is_file() and f.name != self._module_input_json.name
]
modules_status = {
module: {"start_time": None, "completion_time": None, "duration": None}
for module in self._modules
}
with open(self._modules_input_hash_fp, "w") as f:
json.dump(modules_status, f, default=str)
def _get_module_status(self, module):
if self._modules_input_hash_fp is None:
self._update_module_status()
if self._modules_input_hash_fp.exists():
with open(self._modules_input_hash_fp) as f:
modules_status = json.load(f)
if modules_status[module]["completion_time"] is None:
# additional logic to read from the "-output.json" file for this module as well
# handle cases where the module has finished successfully,
# but the "_modules_input_hash_fp" is not updated (for whatever reason),
# resulting in this module not registered as completed in the "_modules_input_hash_fp"
module_output_json_fp = pathlib.Path(
self._get_module_output_json_filename(module)
)
if module_output_json_fp.exists():
with open(module_output_json_fp) as f:
module_run_output = json.load(f)
modules_status[module]["duration"] = module_run_output[
"execution_time"
]
modules_status[module]["completion_time"] = datetime.strptime(
modules_status[module]["start_time"], "%Y-%m-%d %H:%M:%S.%f"
) + timedelta(seconds=module_run_output["execution_time"])
return modules_status[module]
return {"start_time": None, "completion_time": None, "duration": None}
def _get_module_output_json_filename(self, module):
module_input_json = self._module_input_json.as_posix()
module_output_json = module_input_json.replace(
"-input.json",
"-" + module + "-" + str(self._modules_input_hash) + "-output.json",
)
return module_output_json
def _update_total_duration(self):
with open(self._modules_input_hash_fp) as f:
modules_status = json.load(f)
cumulative_execution_duration = sum(
v["duration"] or 0
for k, v in modules_status.items()
if k not in ("cumulative_execution_duration", "total_duration")
)
for m in self._modules:
first_start_time = modules_status[m]["start_time"]
if first_start_time is not None:
break
for m in self._modules[::-1]:
last_completion_time = modules_status[m]["completion_time"]
if last_completion_time is not None:
break
if first_start_time is None or last_completion_time is None:
return
total_duration = (
datetime.strptime(
last_completion_time,
"%Y-%m-%d %H:%M:%S.%f",
)
- datetime.strptime(first_start_time, "%Y-%m-%d %H:%M:%S.%f")
).total_seconds()
self._update_module_status(
{
"cumulative_execution_duration": cumulative_execution_duration,
"total_duration": total_duration,
}
)
|