TheChymera / neuralynx_nwb

Neuralynx to NWB converstion scripts (ideally to be upstreamed)
0 stars 0 forks source link

`ValueError: Multi-segment object. Provide 'segment_index'` #9

Closed TheChymera closed 1 year ago

TheChymera commented 1 year ago
[deco]~/src/neuralynx_nwb ❱ ./convert.sh ~/.local/share/datalad/vStr_phase_stim/M322/M322-2022-07-22/
/usr/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
/home/chymera/.local/share/datalad/vStr_phase_stim/M322/M322-2022-07-22/M322_2022_07_22_Expkeys.m
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/chymera/src/neuralynx_nwb/neuralynx_nwb/newconvert.py", line 43, in reposit_data
    interface.run_conversion(nwbfile_path="/tmp/path.nwb", metadata=metadata, stub_test=True)  # stub_test for fast testing
  File "/usr/lib/python3.10/site-packages/neuroconv/datainterfaces/ecephys/baserecordingextractorinterface.py", line 174, in run_conversion
    recording = self.subset_recording(stub_test=stub_test)
  File "/usr/lib/python3.10/site-packages/neuroconv/datainterfaces/ecephys/baserecordingextractorinterface.py", line 97, in subset_recording
    end_frame = min([num_frames, self.recording_extractor.get_num_frames()])
  File "/usr/lib/python3.10/site-packages/spikeinterface/core/baserecording.py", line 88, in get_num_samples
    segment_index = self._check_segment_index(segment_index)
  File "/usr/lib/python3.10/site-packages/spikeinterface/core/base.py", line 78, in _check_segment_index
    raise ValueError("Multi-segment object. Provide 'segment_index'")
ValueError: Multi-segment object. Provide 'segment_index'
[deco]~/src/neuralynx_nwb ❱ cat neuralynx_nwb/newconvert.py
import os

from datetime import datetime
from dateutil import tz

from neuroconv.datainterfaces import NeuralynxRecordingInterface
from neuroconv.tools.spikeinterface import write_recording
from spikeinterface.extractors import NeuralynxRecordingExtractor

def lab_metadata(in_dir):
    session_name = [i for i in in_dir.split("/") if i][-1]
    session_name = session_name.replace('-', "_")
    exp_metadata = os.path.join(in_dir, session_name+"_Expkeys.m")
    print(exp_metadata)

def reposit_data(
    session_dir='~/.local/share/datalad/vStr_phase_stim/M235/M235-2021-07-16',
    lab_name='MVDMLab',
    institution='Dartmouth College',
    keywords=[
        'DANDI Pilot',
        ],
    experimenter='Manish Mohapatra',
    experiment_description='...',
    debug=True,
    session_description='Extracellular ephys recording in the ventral Striatum',
    keep_original_times=True,
    output_filename='neuralynx_nwb_testfile',
    ):

    session_dir = os.path.abspath(os.path.expanduser(session_dir))
    now = datetime.today().strftime('%Y%m%d%H%M%S')
    out_file = session_dir.rstrip("/") + f"-{now}.nwb"
    lab_metadata(session_dir)

    # Interface approach
    interface = NeuralynxRecordingInterface(folder_path=session_dir, verbose=False)
    metadata = interface.get_metadata()
    session_start_time = datetime(2020, 1, 1, 12, 30, 0, tzinfo=tz.gettz("US/Pacific"))
    metadata["NWBFile"].update(session_start_time=session_start_time)
    print(metadata)
    interface.run_conversion(nwbfile_path="/tmp/path.nwb", metadata=metadata, stub_test=True)  # stub_test for fast testing
    #interface.run_conversion(nwbfile_path="/tmp/path.nwb", metadata=metadata)

I get the aforementioned value if I use .run_conversion() with stub_test=True. It “works” otherwise, though I assume this is because it's just not testing whether the data is ok, and it is in fact not ok.

@CodyCBakerPhD @manimoh any ideas what this is about and what I should do to sort it out? Couldn't find much documentation on the issue.

CodyCBakerPhD commented 1 year ago

As mentioned in https://github.com/TheChymera/neuralynx_nwb/issues/2#issuecomment-1446474075 I don't think the interface approach works here due to the multiple streams, I did add this in https://github.com/catalystneuro/neuroconv/pull/369 but haven't heard confirmation that it works (and doesn't seem to be in the init of the interface here either)

Unless you are running this on a single-stream session, in which case the thing to confirm would be if you are able to call recording_extractor.get_num_frames() (the error stack seems go to core SI stuff) on the Neuralynx extractor initialized on the same data. Also make sure you have spikeinterface updated to latest release

h-mayorquin commented 1 year ago

This was a bug, now it it should be sorted on main.

TheChymera commented 1 year ago

@h-mayorquin yep, this is fixed now. Thank you.