flatironinstitute / CaImAn

Computational toolbox for large scale Calcium Imaging Analysis, including movie handling, motion correction, source extraction, spike deconvolution and result visualization.
https://caiman.readthedocs.io
GNU General Public License v2.0
639 stars 370 forks source link

motion correction breaks when tif file carries channel data #1393

Open ashwin-miriyala opened 3 months ago

ashwin-miriyala commented 3 months ago

Please fill in the following for any issues

Your setup:

  1. Operating System (Linux, MacOS, Windows): Windows
  2. Hardware type (x86, ARM..) and RAM: x86
  3. Python Version (e.g. 3.9): 3.10.8
  4. Caiman version (e.g. 1.9.12): 1.9.14
  5. Which demo exhibits the problem (if applicable):
  6. How you installed Caiman (pure conda, conda + compile, colab, ..): conda
  7. Details:

Using scanimage, I have recorded a tif file that carries frames from 2 channels. These frames are interleaved, i.e. channel 1 is on odd frames and channel 2 is on even frames in the tif file. I thought I would run motion correction on the entire tif file and then worry about de-interleaving afterwards. However, while running motion correction, there is a step where the shape of the tiff file is read, and it doesn't account for the extra dimention that mentions the number of channels. I've found an example of where this occurs (in utilities.py):

dims, T = cm.source_extraction.cnmf.utilities.get_file_size(fname, var_name_hdf5=var_name_hdf5)

within this definition 'get_file_size'... ... if extension in ['.tif', '.tiff', '.btf']: tffl = tifffile.TiffFile(file_name) siz = tffl.series[0].shape

tiff files written in append mode

            if len(siz) < 3:
                dims = siz
                T = len(tffl.pages)
            else:
                T, dims = siz[0], siz[1:]

Here, my tif file returns dimensions (2056,2,256,256). If there was only one channel during acquisition, the size would be (2056,256,256). I'm not sure if this problem stems from the tifffile class or the scanimage software that saves the tif file.

While a workaround would be simple for this particular definition, I'm not sure how many other definitions I'd need to update. Do you think this is a valid problem? Or should I deinterleave and make sure of only 1 channel per tif file and appropriate dimensions before running motion correction?

Thanks!

pgunn commented 2 months ago

Hello, Right now I think the easiest route would be for you to preprocess the data to remove the extra dimension; I don't think we've ever run into someone doing things they way you describe before, and adding code to support too many data layout variations would make things hard to maintain.

kushalkolar commented 2 months ago

Yea scanimage does bizarre things, IIRC it does that if the acquisition was stopped midway, if all frames that were set to be recorded were not recorded it just leave the tiff file like that there. Anyways, as Pat said you have to save them to two separate files.