catalystneuro / neuroconv

Create NWB files by converting and combining neural data in proprietary formats and adding essential metadata.
https://neuroconv.readthedocs.io
BSD 3-Clause "New" or "Revised" License
51 stars 21 forks source link

[Bug]: ROI extractor returns error "tuple index out of range" #1109

Open laurelrr opened 2 days ago

laurelrr commented 2 days ago

What happened?

I am working with this code base to convert 6 subjects: https://github.com/catalystneuro/tye-lab-to-nwb/tree/main/src/tye_lab_to_nwb/ast_ophys I created individual convert_session.py files for each subject. Three of them are working correctly and convert my data as expected. However, one subject returned an error, and I am not sure what to look for in my data in order to correct it.

Steps to Reproduce

git clone https://github.com/catalystneuro/tye-lab-to-nwb
cd tye-lab-to-nwb
conda env create --file make_env.yml
conda activate tye-lab-to-nwb-env
python  ~/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/convert_session_jackalope.py

Traceback

Source data is valid!
/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/pymatreader/utils.py:238: UserWarning: Complex objects (like classes) are not supported. They are importedeffort base but your mileage will vary.
  warn('Complex objects (like classes) are not supported. '
Traceback (most recent call last):
  File "/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/convert_session_jackalope.py", line 207, in <module>
    session_to_nwb(
  File "/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/convert_session_jackalope.py", line 107, in session_to_nwb
    converter = AStOphysNWBConverter(source_data=source_data)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/neuroconv/nwbconverter.py", line 65, in __init__
    self.data_interface_objects = {
                                  ^
  File "/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/neuroconv/nwbconverter.py", line 66, in <dictcomp>
    name: data_interface(**source_data[name])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nadata/snlkt/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/interfaces/cnmfe_matlab_segmentationinterface.py", line 12, in __init__
    super().__init__(file_path=file_path)
  File "/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/neuroconv/datainterfaces/ophys/basesegmentationextractorinterface.py", line 20, in __init__
    self.segmentation_extractor = self.get_extractor()(**source_data)
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nadata/snlkt/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/extractors/cnmfe_matlab_segmentationextractor.py", line 42, in __init__
    self._image_masks = self._transform_image_masks()
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nadata/snlkt/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/extractors/cnmfe_matlab_segmentationextractor.py", line 71, in _transform_image_masks
    num_rois = self.get_num_rois()
               ^^^^^^^^^^^^^^^^^^^
  File "/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/roiextractors/segmentationextractor.py", line 278, in get_num_rois
    return trace.shape[1]
           ~~~~~~~~~~~^^^
IndexError: tuple index out of range

Operating System

Linux

Python Executable

Conda

Python Version

3.8

Package Versions

environment_for_issue.txt

Code of Conduct

h-mayorquin commented 1 day ago

It seems that the trace for that example does not have two dimensions, maybe only one roi? I think that @weiglszonja is the one that worked with that code base, could you take a look?

weiglszonja commented 1 day ago

It seems that the trace for that example does not have two dimensions, maybe only one roi? I think that @weiglszonja is the one that worked with that code base, could you take a look?

That would be my first guess too. @laurelrr can you check the shape of the traces in the extractor?

from tye_lab_to_nwb.ast_ophys.extractors.cnmfe_matlab_segmentationextractor import CnmfeMatlabSegmentationExtractor

extractor = CnmfeMatlabSegmentationExtractor(file_path="path/to/problematic_file.mat")
# Check the original trace shape, should be [num_rois x num_frames]
print(extractor._dataset_file["C"].shape)
print(len(extractor._dataset_file["C"].shape))

If this returns a one dimensional array then we have to fix the code in the custom extractor .

Let me know if this is the case and I'll help to fix it.

laurelrr commented 19 hours ago

I ran into the same error at the extractor line:

Python 3.11.2 (main, Mar 27 2023, 23:42:44) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tye_lab_to_nwb.ast_ophys.extractors.cnmfe_matlab_segmentationextractor import CnmfeMatlabSegmentationExtractor
>>> extractor = CnmfeMatlabSegmentationExtractor("/snlkt/ast/Miniscope/expAnalysis/20211206_CACO_ALL_DISC/CACO3_DISC6/analysis/msCam_MC/G2B1_C4-6886_L_jackalope_DISC6UNP+TONES_neuron.mat")
/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/pymatreader/utils.py:238: UserWarning: Complex objects (like classes) are not supported. They are imported on a best effort base but your mileage will vary.
  warn('Complex objects (like classes) are not supported. '
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nadata/snlkt/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/extractors/cnmfe_matlab_segmentationextractor.py", line 42, in __init__
    self._image_masks = self._transform_image_masks()
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nadata/snlkt/home/lkeyes/Projects/GIT/tye-lab-to-nwb/src/tye_lab_to_nwb/ast_ophys/extractors/cnmfe_matlab_segmentationextractor.py", line 71, in _transform_image_masks
    num_rois = self.get_num_rois()
               ^^^^^^^^^^^^^^^^^^^
  File "/home/lkeyes/anaconda3/envs/tye_lab_to_nwb_env/lib/python3.11/site-packages/roiextractors/segmentationextractor.py", line 278, in get_num_rois
    return trace.shape[1]
           ~~~~~~~~~~~^^^
IndexError: tuple index out of range

Not sure if this is helpful or not but I loaded the file into Matlab and here is what is in it:


  struct with fields:

                    A: [90240×1 double]
               A_prev: [90240×6 double]
                    C: [0.3787 0.3574 0.4006 0.3782 0.3933 0.4259 0.3824 0.3880 0.4590 0.3978 0.3650 0.4191 0.4809 0.3841 0.3219 0.3921 0.3981 0.3856 0.3413 0.3595 0.4127 0.3992 … ]
               C_prev: [6×15218 double]
                C_raw: [0.3787 0.3574 0.4006 0.3782 0.3933 0.4259 0.3824 0.3880 0.4590 0.3978 0.3650 0.4191 0.4809 0.3841 0.3219 0.3921 0.3981 0.3856 0.3413 0.3595 0.4127 0.3992 … ]
                    S: [1×15218 double]
               kernel: [1×1 struct]
                    b: {3×4 cell}
                    f: {3×4 cell}
                    W: {3×4 cell}
                   b0: {3×4 cell}
               b0_new: [240×376 double]
              options: [1×1 struct]
                    P: [1×1 struct]
                   Fs: 15
                 file: '/nadata/snlkt/ast/Miniscope/expAnalysis/20211206_CACO_ALL_DISC/CACO3_DISC6/analysis/msCam_MC/G2B1_C4-6886_L_jackalope_DISC6UNP+TONES_msCamComb_MC.mat'
          frame_range: [1 15218]
                  ids: 2
                 tags: 0
                   Cn: [240×376 double]
                  PNR: [240×376 double]
                 Coor: {4×1 cell}
    neurons_per_patch: []
                   Df: []
                 C_df: []
                 S_df: []
              batches: {}
              file_id: []