catalystneuro / IBL-to-nwb

Conversion of IBL data to NWB format.
BSD 3-Clause "New" or "Revised" License
2 stars 3 forks source link

Code for getting a SortingExtractor with timestamps in synch with current recording in dandi. #60

Closed h-mayorquin closed 1 week ago

h-mayorquin commented 5 months ago

from dandi.dandiapi import DandiAPIClient
from spikeinterface.extractors import NwbRecordingExtractor, IblSortingExtractor

client = DandiAPIClient.for_dandi_instance("dandi")

# We specifiy a dataset by is dandiset_id and its asset path
dandiset_id = "000409"
dandiset = client.get_dandiset(dandiset_id)

asset_path = "sub-KS042/sub-KS042_ses-8c552ddc-813e-4035-81cc-3971b57efe65_behavior+ecephys+image.nwb"
recording_asset = dandiset.get_asset_by_path(path=asset_path)
url = recording_asset.get_content_url(follow_redirects=True, strip_query=True)
file_path = url

# Note that this ElectricalSeries corresponds to the data from probe 00
electrical_series_path = "acquisition/ElectricalSeriesAp00"
recording = NwbRecordingExtractor(file_path=file_path, stream_mode="remfile", electrical_series_path=electrical_series_path)  
session_id = recording._file["general"]["session_id"][()].decode()
eid = session_id.split("-chunking")[0] # eid : experiment id

# We use the sorting extractor from the IBL spike sorting pipeline that matches with eid
from one.api import ONE
ONE.setup(base_url='https://openalyx.internationalbrainlab.org', silent=True)
one_instance = ONE(password='international')

# Then we match the available probes with the probe number in the electrical series path
pids, probes = one_instance.eid2pid(eid)
probe_number = electrical_series_path.split("Ap")[-1]

sorting_pid = None 
for pid, probe in zip(pids, probes):
    probe_number_in_pid = probe[-2:]
    if probe_number_in_pid == probe_number:
        sorting_pid = pid
        break

sorting = IblSortingExtractor(pid=sorting_pid, one=one_instance, good_clusters_only=True)

Note that Ibl might have been modified https://github.com/SpikeInterface/spikeinterface/pull/2617

CodyCBakerPhD commented 5 months ago

Alessios sorting extractor appears different from mine: https://github.com/SpikeInterface/spikeinterface/blob/main/src/spikeinterface/extractors/iblextractors.py#L285