Skip to content
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

Wrap medicine motion estimation #3552

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/spikeinterface/sortingcomponents/motion/medecine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import numpy as np

from .motion_utils import Motion
import tempfile
import shutil
from pathlib import Path

from .motion_utils import get_spatial_windows


class MedecineRegistration:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class MedecineRegistration:
class MedicineRegistration:

""" """

name = "medecine"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = "medecine"
name = "medicine"

need_peak_location = True
params_doc = """

"""

@classmethod
def run(
cls,
recording,
peaks,
peak_locations,
direction,
# unsed need to be adapted
rigid,
win_shape,
win_step_um,
win_scale_um,
win_margin_um,
verbose,
progress_bar,
extra,
# bin_um=5.0,
# hist_margin_um=20.0,
bin_s=1.0,
time_kernel_width=30.0,
amplitude_threshold_quantile=0.0,
####
training_steps=10_000,
):

from medicine import run_medicine

# folder = Path(tempfile.gettempdir())

if rigid:
# force one bin
num_depth_bins = 1
else:

# we use the spatial window mechanism only to estimate the number one spatial bins
dim = ["x", "y", "z"].index(direction)
contact_depths = recording.get_channel_locations()[:, dim]

deph_range = max(contact_depths) - min(contact_depths)
if win_margin_um is not None:
deph_range = deph_range - 2 * win_margin_um
num_depth_bins = max(int(np.round(deph_range / win_scale_um)), 1)
print("num_depth_bins", num_depth_bins)
Comment on lines +49 to +62
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nwatters01 this is the main zone where we need to discuss.
I think that windows should come with the probe boundaries +/- margin not by the the spike depth limits.
Otherwise the number of spatial windows will depend on the recording itself and could vary from one recording to another.


trainer, motion = run_medicine(
peak_amplitudes=peaks["amplitude"],
peak_depths=peak_locations[direction],
peak_times=peaks["sample_index"] / recording.get_sampling_frequency(),
time_bin_size=bin_s,
num_depth_bins=num_depth_bins,
training_steps=training_steps,
time_kernel_width=time_kernel_width,
amplitude_threshold_quantile=amplitude_threshold_quantile,
output_dir=None,
plot_figures=False,
return_motion=True,
)

# Load motion estimated by MEDiCINe
# motion_array = np.load(folder / 'motion.npy')
# time_bins = np.load(folder / 'time_bins.npy')
# depth_bins = np.load(folder / 'depth_bins.npy')

# # Use interpolation to correct for motion estimated by MEDiCINe
# motion = Motion(
# displacement=[motion_array],
# temporal_bins_s=[time_bins],
# spatial_bins_um=depth_bins,
# )

# TODO check why not working
# shutil.rmtree(folder)

return motion
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .decentralized import DecentralizedRegistration
from .iterative_template import IterativeTemplateRegistration
from .dredge import DredgeLfpRegistration, DredgeApRegistration
from .medecine import MedecineRegistration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from .medecine import MedecineRegistration
from .medicine import MedicineRegistration



# estimate_motion > infer_motion
Expand Down Expand Up @@ -130,7 +131,13 @@ def estimate_motion(
return motion


_methods_list = [DecentralizedRegistration, IterativeTemplateRegistration, DredgeLfpRegistration, DredgeApRegistration]
_methods_list = [
DecentralizedRegistration,
IterativeTemplateRegistration,
DredgeLfpRegistration,
DredgeApRegistration,
MedecineRegistration,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MedecineRegistration,
MedicineRegistration,

]
estimate_motion_methods = {m.name: m for m in _methods_list}
method_doc = make_multi_method_doc(_methods_list)
estimate_motion.__doc__ = estimate_motion.__doc__.format(method_doc=method_doc)