Skip to content

Commit 8fbf100

Browse files
committed
Expose clear_Cache argument in KS4
1 parent 0ed4876 commit 8fbf100

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

.github/scripts/test_kilosort4_ci.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,30 @@ def test_kilosort4_main(self, recording_and_paths, default_kilosort_sorting, tmp
368368
with pytest.raises(AssertionError):
369369
check_sortings_equal(default_kilosort_sorting, sorting_si)
370370

371+
def test_clear_cache(self,recording_and_paths, tmp_path):
372+
"""
373+
Test clear_cache parameter in kilosort4.run_kilosort
374+
"""
375+
recording, paths = recording_and_paths
376+
377+
spikeinterface_output_dir = tmp_path / "spikeinterface_output_clear"
378+
sorting_si_clear = si.run_sorter(
379+
"kilosort4",
380+
recording,
381+
remove_existing_folder=True,
382+
folder=spikeinterface_output_dir,
383+
clear_cache=True
384+
)
385+
spikeinterface_output_dir = tmp_path / "spikeinterface_output_no_clear"
386+
sorting_si_no_clear = si.run_sorter(
387+
"kilosort4",
388+
recording,
389+
remove_existing_folder=True,
390+
folder=spikeinterface_output_dir,
391+
clear_cache=False
392+
)
393+
check_sortings_equal(sorting_si_clear, sorting_si_no_clear)
394+
371395
def test_kilosort4_no_correction(self, recording_and_paths, tmp_path):
372396
"""
373397
Test the SpikeInterface wrappers `do_correction` argument. We set

src/spikeinterface/sorters/external/kilosort4.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Kilosort4Sorter(BaseSorter):
6565
"save_preprocessed_copy": False,
6666
"torch_device": "auto",
6767
"bad_channels": None,
68+
"clear_cache": False,
6869
"use_binary_file": None,
6970
"delete_recording_dat": True,
7071
}
@@ -111,6 +112,7 @@ class Kilosort4Sorter(BaseSorter):
111112
"save_preprocessed_copy": "save a pre-processed copy of the data (including drift correction) to temp_wh.dat in the results directory and format Phy output to use that copy of the data",
112113
"torch_device": "Select the torch device auto/cuda/cpu",
113114
"bad_channels": "A list of channel indices (rows in the binary file) that should not be included in sorting. Listing channels here is equivalent to excluding them from the probe dictionary.",
115+
"clear_cache": "If True, force pytorch to free up memory reserved for its cache in between memory-intensive operations. Note that setting `clear_cache=True` is NOT recommended unless you encounter GPU out-of-memory errors, since this can result in slower sorting.",
114116
"use_binary_file": "If True then Kilosort is run using a binary file. In this case, if the input recording is not binary compatible, it is written to a binary file in the output folder. "
115117
"If False then Kilosort is run on the recording object directly using the RecordingExtractorAsArray object. If None, then if the recording is binary compatible, the sorter will use the binary file, otherwise the RecordingExtractorAsArray. "
116118
"Default is None.",
@@ -284,6 +286,7 @@ def _run_from_folder(cls, sorter_output_folder, params, verbose):
284286
data_dir = ""
285287
results_dir = sorter_output_folder
286288
bad_channels = params["bad_channels"]
289+
clear_cache = params["clear_cache"]
287290

288291
filename, data_dir, results_dir, probe = set_files(
289292
settings=settings,
@@ -347,17 +350,31 @@ def _run_from_folder(cls, sorter_output_folder, params, verbose):
347350

348351
# this function applies both preprocessing and drift correction
349352
ops, bfile, st0 = compute_drift_correction(
350-
ops=ops, device=device, tic0=tic0, progress_bar=progress_bar, file_object=file_object
353+
ops=ops,
354+
device=device,
355+
tic0=tic0,
356+
progress_bar=progress_bar,
357+
file_object=file_object,
358+
clear_cache=clear_cache,
351359
)
352360

353361
if save_preprocessed_copy:
354362
save_preprocessing(results_dir / "temp_wh.dat", ops, bfile)
355363

356364
# Sort spikes and save results
357-
st, tF, _, _ = detect_spikes(ops=ops, device=device, bfile=bfile, tic0=tic0, progress_bar=progress_bar)
365+
st, tF, _, _ = detect_spikes(
366+
ops=ops, device=device, bfile=bfile, tic0=tic0, progress_bar=progress_bar, clear_cache=clear_cache
367+
)
358368

359369
clu, Wall = cluster_spikes(
360-
st=st, tF=tF, ops=ops, device=device, bfile=bfile, tic0=tic0, progress_bar=progress_bar
370+
st=st,
371+
tF=tF,
372+
ops=ops,
373+
device=device,
374+
bfile=bfile,
375+
tic0=tic0,
376+
progress_bar=progress_bar,
377+
clear_cache=clear_cache,
361378
)
362379

363380
if params["skip_kilosort_preprocessing"]:

0 commit comments

Comments
 (0)