Skip to content

Commit

Permalink
remove sortingview
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoe91 committed Nov 26, 2024
1 parent d031d06 commit eae068e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 119 deletions.
32 changes: 0 additions & 32 deletions src/expipe_plugin_cinpla/scripts/curation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import json
import shutil
import warnings

Expand Down Expand Up @@ -200,37 +199,6 @@ def get_phy_run_command(self, sorter):
phy_run_command = f"phy template-gui {phy_path}"
return phy_run_command

def get_sortingview_link(self, sorter):
visualization_json = self.si_path / sorter / "sortingview_links.json"
if not visualization_json.is_file():
return "Sorting view link not found."
with open(visualization_json) as f:
sortingview_links = json.load(f)
return sortingview_links["raw"]

def apply_sortingview_curation(self, sorter, curated_link):
import spikeinterface.curation as sc

sorting_raw = self.load_raw_sorting(sorter)
assert sorting_raw is not None, f"Could not load raw sorting for {sorter}."
sorting_raw = sorting_raw.save(format="memory")

# delete NWB-specific properties: id, waveform_mean, and waveform_sd properties
sorting_raw.delete_property("waveform_mean")
sorting_raw.delete_property("waveform_sd")
sorting_raw.delete_property("electrodes")

curated_link_ = curated_link.replace("%22", '"')
curation_str = curated_link_[curated_link_.find("sortingCuration") :]
uri = curation_str[curation_str.find("sha1://") : -2]
sorting_curated = sc.apply_sortingview_curation(sorting_raw, uri_or_json=uri)
# exclude noise
good_units = sorting_curated.unit_ids[sorting_curated.get_property("noise") == False] # noqa E712
# create single property for SUA and MUA
sorting_curated = sorting_curated.select_units(good_units)
self.apply_curation(sorter, sorting_curated)
self.curation_description = f"Curation manually performed in Sortingview\nLink: {curated_link}"

def apply_qc_curator(self, sorter, query):
raw_we = self.load_raw_waveforms(sorter)
qm_table = raw_we.load_extension("quality_metrics").get_data()
Expand Down
41 changes: 1 addition & 40 deletions src/expipe_plugin_cinpla/scripts/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def process_ecephys(
overwrite=False,
singularity_image=None,
n_components=3,
plot_sortingview=True,
verbose=True,
):
import warnings
Expand All @@ -41,7 +40,6 @@ def process_ecephys(
import spikeinterface.preprocessing as spre
import spikeinterface.qualitymetrics as sqm
import spikeinterface.sorters as ss
import spikeinterface.widgets as sw
from neuroconv.tools.spikeinterface import add_recording
from pynwb import NWBHDF5IO

Expand Down Expand Up @@ -279,7 +277,7 @@ def process_ecephys(
_ = spost.compute_template_metrics(we)
if verbose:
print("\tComputing QC metrics")
qm = sqm.compute_quality_metrics(we, metric_names=metric_names)
_ = sqm.compute_quality_metrics(we, metric_names=metric_names)

if verbose:
print("\tExporting to phy")
Expand All @@ -296,43 +294,6 @@ def process_ecephys(
# generate files to be used with restore
utils.generate_phy_restore_files(phy_folder)

if plot_sortingview:
if verbose:
print("\tGenerating sortingview link")
unit_table_properties = ["group"]
if "firing_rate" in qm:
we.sorting.set_property("firing_rate", np.round(qm["firing_rate"], 2))
unit_table_properties.append("firing_rate")
if "amplitude_median" in qm:
we.sorting.set_property("amplitude", np.round(qm["amplitude_median"]))
unit_table_properties.append("firing_rate")
try:
w = sw.plot_sorting_summary(
we,
backend="sortingview",
curation=True,
unit_table_properties=["group", "firing_rate", "amplitude"],
label_choices=["SUA", "MUA", "noise"],
figlabel=f"{action_id}-{sorter}",
)
sorting_summary_url = w.url
visualization_dict = dict(raw=sorting_summary_url)
# save params in output
with open(si_folder / sorter / "sortingview_links.json", "w") as f:
json.dump(visualization_dict, f, indent=4)
except Exception as e:
print(
f"Could not generate sortingview link, probably due to a wrong configuration. "
f"You can setup sortingview using the following instructions:\n"
"https://spikeinterface.readthedocs.io/en/latest/modules/widgets.html#sorting-view"
f"\nError:\n{e}"
)
w = None
if "firing_rate" in qm:
we.sorting.delete_property("firing_rate")
if "amplitude_median" in qm:
we.sorting.delete_property("amplitude")

if verbose:
print("\nWriting to NWB")
nwb_path.unlink()
Expand Down
50 changes: 3 additions & 47 deletions src/expipe_plugin_cinpla/widgets/curation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ def __init__(self, project):

# 1. Load from Phy
# 2. Use Quality metrics
# 3. Use sortingview
strategy = ipywidgets.RadioButtons(
options=["Phy", "Sortingview", "Quality Metrics"],
options=["Phy", "Quality Metrics"],
description="Curation strategy:",
disabled=False,
value="Phy",
Expand All @@ -114,24 +113,6 @@ def __init__(self, project):
layout={"width": "initial"},
)

# Sortingview
sv_visualization_link = ipywidgets.Text(
value="",
placeholder="The Sortingview link will appear here",
description="Curation link:",
disabled=True,
layout={"width": "500px"},
)
sv_curated_link = ipywidgets.Text(
value="",
placeholder="Enter the link to the curated sorting here",
description="Curated link:",
disabled=False,
layout={"width": "500px"},
)
apply_sv_curation = ipywidgets.Button(description="Apply", layout={"width": "200px"})
apply_sv_curation.style.button_color = "pink"

# Quality metrics
qm_thresholds = []
standard_qms = project.config.get("custom_qms") if project.config.get("custom_qms") is not None else default_qms
Expand Down Expand Up @@ -159,8 +140,6 @@ def __init__(self, project):
]
)

sortingview_panel = ipywidgets.VBox([sv_visualization_link, sv_curated_link, apply_sv_curation])

qm_panel = ipywidgets.VBox(
[
qc_controls,
Expand Down Expand Up @@ -214,9 +193,7 @@ def on_action(change):
if units_main is not None:
w = nwb2widget(units_main, custom_main_unit_vis)
units_viewers["main"] = w
if strategy.value == "Sortingview":
sv_visualization_link.value = self.sorting_curator.get_sortingview_link(sorter_list.value[0])
elif strategy.value == "Phy":
if strategy.value == "Phy":
run_phy_command.value = self.sorting_curator.get_phy_run_command(sorter_list.value[0])
units_dropdown.value = "Raw"
on_choose_units(None)
Expand All @@ -235,10 +212,7 @@ def on_sorter(change):
if units_main is not None:
w = nwb2widget(units_main, custom_main_unit_vis)
units_viewers["main"] = w
if strategy.value == "Sortingview":
# load visualization link
sv_visualization_link.value = self.sorting_curator.get_sortingview_link(sorter_list.value[0])
elif strategy.value == "Phy":
if strategy.value == "Phy":
run_phy_command.value = self.sorting_curator.get_phy_run_command(sorter_list.value[0])
units_dropdown.value = "Raw"
on_choose_units(None)
Expand All @@ -253,23 +227,6 @@ def on_change_strategy(change):
# check if metrics apply
if len(sorter_list.value) == 1:
check_metrics(project, actions_list, sorter_list.value[0], qc_metrics)
elif change["new"] == "Sortingview":
curation_panel.children = [strategy, sortingview_panel]
if len(sorter_list.value) == 1:
sv_visualization_link.value = self.sorting_curator.get_sortingview_link(sorter_list.value[0])

@self.output.capture()
def on_curated_link(change):
required_values_filled(actions_list, sorter_list, sv_curated_link)
if len(sorter_list.value) > 1:
print("Select one spike sorting output at a time")
else:
print(f"Applying curation from {sv_curated_link.value}")
self.sorting_curator.apply_sortingview_curation(sorter_list.value[0], sv_curated_link.value)
units = self.sorting_curator.construct_curated_units()
if units:
w = nwb2widget(units, custom_curated_unit_vis)
units_viewers["curated"] = w

def on_add_metric(change):
action = project.actions[actions_list.value]
Expand Down Expand Up @@ -358,7 +315,6 @@ def on_save_to_nwb(change):
sorter_list.observe(on_sorter)
set_default_qms.on_click(on_set_default_qms)
apply_qm_curation.on_click(on_apply_qm_curation)
apply_sv_curation.on_click(on_curated_link)
units_dropdown.observe(on_choose_units)

add_metric_button.on_click(on_add_metric)
Expand Down

0 comments on commit eae068e

Please sign in to comment.