-
Notifications
You must be signed in to change notification settings - Fork 280
Add read_kilosort4_motion function
#4518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3dc5729
Test IBL extractors tests failing for PI update
alejoe91 d1a0532
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 33c6769
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 2c94bac
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 a40d073
Merge branch 'main' of github.com:alejoe91/spikeinterface
alejoe91 ef40b73
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 11c5812
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 ada53f8
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 22ff8fd
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 cbc36de
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 6b3e373
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 7eb2251
Merge branch 'main' of github.com:SpikeInterface/spikeinterface
alejoe91 e1006bc
Add read_kilosort4_motion function
alejoe91 6d904db
Fix imports
alejoe91 bb0456a
fix imports 2
alejoe91 b26ab8f
fix logger closing
alejoe91 0ea163f
fix: read function displacement, add test for read_kilosort4_motion f…
alejoe91 6e6c21f
Merge branch 'main' into read_ks_motion
alejoe91 77019af
Merge branch 'main' into read_ks_motion
alejoe91 c1c902c
Merge branch 'main' into read_ks_motion
alejoe91 ba5f23a
Merge branch 'main' into read_ks_motion
alejoe91 4da9433
build: update minimal supported KS version to 4.1.0
alejoe91 e6ab01f
build: update minimal supported KS version to 4.1.1
alejoe91 0cfcf88
fix: read_kilosort_as_analyzer
alejoe91 41ea5f0
fix: extract main_channel_indices in read_kilosort_as_analyzer
alejoe91 09b07dc
Update src/spikeinterface/extractors/phykilosortextractors.py
chrishalcrow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import warnings | ||
| from pathlib import Path | ||
| from packaging import version | ||
|
|
||
| from spikeinterface.core import write_binary_recording | ||
| import numpy as np | ||
|
|
||
| from spikeinterface.core import write_binary_recording, Motion, BaseRecording | ||
| from spikeinterface.sorters.basesorter import BaseSorter, get_job_kwargs | ||
| from .kilosortbase import KilosortBase | ||
| from spikeinterface.sorters.basesorter import get_job_kwargs | ||
|
|
@@ -77,7 +80,7 @@ def is_installed(cls): | |
|
|
||
| @classmethod | ||
| def get_sorter_version(cls): | ||
| """kilosort.__version__ <4.0.10 is always '4'""" | ||
| """kilosort.__version__ < 4.0.10 is always '4'""" | ||
| return importlib_version("kilosort") | ||
|
|
||
| @classmethod | ||
|
|
@@ -122,12 +125,11 @@ def initialize_folder(cls, recording, output_folder, verbose, remove_existing_fo | |
| @classmethod | ||
| def check_sorter_version(cls): | ||
| kilosort_version = version.parse(cls.get_sorter_version()) | ||
| if kilosort_version < version.parse("4.0.16"): | ||
| raise Exception( | ||
| f"""SpikeInterface only supports kilosort versions 4.0.16 and above. You are running version {kilosort_version}. To install the latest version, run: | ||
| >>> pip install kilosort --upgrade | ||
| """ | ||
| ) | ||
| if kilosort_version < version.parse("4.1.1"): | ||
| raise Exception(f"""SpikeInterface only supports kilosort versions 4.1.1 and above (which support numpy>=2). | ||
| You are running version {kilosort_version}. To install the latest version, run: | ||
| >>> pip install kilosort --upgrade | ||
| """) | ||
|
|
||
| @classmethod | ||
| def _setup_recording(cls, recording, sorter_output_folder, params, verbose): | ||
|
|
@@ -177,7 +179,6 @@ def _run_from_folder(cls, sorter_output_folder, params, verbose): | |
|
|
||
| import time | ||
| import torch | ||
| import numpy as np | ||
| import logging | ||
|
|
||
| if version.parse(cls.get_sorter_version()) < version.parse("4.0.16"): | ||
|
|
@@ -461,6 +462,11 @@ def _run_from_folder(cls, sorter_output_folder, params, verbose): | |
| if (sorter_output_folder / "recording.dat").is_file(): | ||
| (sorter_output_folder / "recording.dat").unlink() | ||
|
|
||
| # close logger | ||
| for handler in logger.handlers.copy(): | ||
| logger.removeHandler(handler) | ||
| handler.close() | ||
|
|
||
| @classmethod | ||
| def _get_result_from_folder(cls, sorter_output_folder): | ||
| return KilosortBase._get_result_from_folder(sorter_output_folder) | ||
|
|
@@ -469,7 +475,6 @@ def _get_result_from_folder(cls, sorter_output_folder): | |
| def _setup_json_probe_map(cls, recording, sorter_output_folder): | ||
| """Create a JSON probe map file for Kilosort4.""" | ||
| from kilosort.io import save_probe | ||
| import numpy as np | ||
|
|
||
| groups = recording.get_channel_groups() | ||
| positions = np.array(recording.get_channel_locations()) | ||
|
|
@@ -492,3 +497,47 @@ def _setup_json_probe_map(cls, recording, sorter_output_folder): | |
| "n_chan": n_chan, | ||
| } | ||
| save_probe(probe, str(sorter_output_folder / "chanMap.json")) | ||
|
|
||
|
|
||
| def read_kilosort4_motion(sorter_output_folder: str | Path, recording: BaseRecording | None = None) -> Motion: | ||
| """Reads the motion information from a Kilosort4 output folder and returns a Motion object. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| sorter_output_folder: str or Path | ||
| The path to the Kilosort4 output folder. | ||
| recording: BaseRecording, optional | ||
| The recording object. If provided, the temporal bins will be estimated based on the recording's | ||
| start and end times. If not provided, the temporal bins will be estimated based on the number | ||
| of batches in the ops file. | ||
|
|
||
| Returns | ||
| ------- | ||
| Motion | ||
| A Motion object containing the displacement, temporal bins, and spatial bins. | ||
|
|
||
| """ | ||
| sorter_output_folder = Path(sorter_output_folder) | ||
| ops_file = sorter_output_folder / "ops.npy" | ||
| if not ops_file.is_file(): | ||
| raise FileNotFoundError("'ops.npy' file not found!") | ||
| ops = np.load(ops_file, allow_pickle=True).item() | ||
| yblk = ops.get("yblk") | ||
| dshift = ops.get("dshift") | ||
| if yblk is None or dshift is None: | ||
| raise Exception("'yblk' and 'dshift' fields not found in ops file!") | ||
| displacement = dshift | ||
| spatial_bins_um = yblk | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this ordered always ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regularly spaced, no duplicates |
||
| # estimate temporal bins | ||
| batch_size = ops["batch_size"] | ||
| fs = ops["fs"] | ||
| t_bin = batch_size / fs | ||
| if recording is not None: | ||
| t_start = recording.get_start_time() | ||
| t_end = recording.get_end_time() | ||
| temporal_bins_s = np.linspace(t_start + t_bin / 2, t_end - t_bin / 2, displacement.shape[0]) | ||
| else: | ||
| temporal_bins_s = np.arange(displacement.shape[0]) * t_bin + t_bin / 2 | ||
|
|
||
| motion = Motion(displacement=displacement, temporal_bins_s=temporal_bins_s, spatial_bins_um=spatial_bins_um) | ||
| return motion | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that us using
stable = True(which is already all over the place) has forced us to pinnumpy >= 2. And that that's ok!