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
640 stars 370 forks source link

openCV issue when playing the movie #1227

Open andrestrocyte opened 1 year ago

andrestrocyte commented 1 year ago

Hello! I ran the demo pipeline on a test imaging session that I took (single tiff, 3.8GB).

I can successfully go through all the steps, except when trying to display the movie, having an Error message from OpenCV. I have tried to troubleshoot it with no success.

Any input would be appreciated. Thanks a lot!

Screenshot 2023-11-15 at 13 48 49

Please fill in the following for any issues

Your setup:

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

Before we dig into this, could you please upgrade to our most recent version of Caiman (1.9.16)? This probably won't make a difference, as you are on a Mac, but just in case.

andrestrocyte commented 1 year ago

Thanks a lot for your help Eric. Indeed, I upgraded to caiman 1.9.16 and was able to run all the cells of the script again but not visualise the movie of my imaging session, 1 bigtiff of 3.8GB. With Fiji I am able to play it. I am not sure why openCV does not work with it. I am able to play the demo video though. Thanks so much.

andrestrocyte commented 1 year ago

Just discovered that caiman is detecting my tif file as a movie of just 1 frame as print(m_orig.shape) gives (1, 512, 512) as output.

However, using PIL library the same script detects the .tif as having 7000 frames.

Screenshot 2023-11-17 at 11 00 32
EricThomson commented 1 year ago

Could you use tiffit to extract info about your movie and print it here? https://github.com/EricThomson/tiffit

Try converting to a "good" tiff format using tiffit convert in.tiff out.tiff. Note tiffit is a command line interface so you use it from the shell.

Caiman doesn't like things that have been saved by ImageJ. We probably should just automate this it comes up too often and ImageJ is a common but idiosyncratic package when it comes to tiff.

andrestrocyte commented 1 year ago

Here it is:

Info about bigtiff_1.tif: Number of images: 7202 Dimensions of first image: (512, 512) Byte representation: uint16 ImageJ format? False BigTIFF format? True

andrestrocyte commented 1 year ago

PS: I ran into the exact same trouble after converting my tiff into a well behaving tiff using fittiff.

Thanks for the indications

EricThomson commented 1 year ago

Dang it's not going to be as simple as I hoped. your movie is imagej=False and BigTiff=True, which is good.

How much RAM do you have? What happens when you try with the Sue demo movie?

andrestrocyte commented 1 year ago

The problem is that cm.load_movie_chain is importing my multi frame (7202 frames) big tiff into a single frame movie.

Screenshot 2023-11-21 at 16 08 56

andrestrocyte commented 1 year ago

The Sue movie runs well and is detected correctly as a 3000 frames movie. I have 16GB =. Thanks so much!

EricThomson commented 1 year ago

Could you trim your movie down to 2000 frames resave and see if it is still a problem? It probably isn’t it but just checking the obvious stuff. If still a problem do you mind sharing the smaller movie?

andrestrocyte commented 1 year ago

The problem persists with less than 2000 frames.

You can download the small tiff here:

https://drive.google.com/file/d/1Sy-hBVuZoZWOLaacDih5qsL4sIQP_udc/view?usp=sharing

Thanks a lot!

EricThomson commented 1 year ago

I reproduced this behavior. I will have to look more deeply at the metadata for your movie my guess is it is something we've never seen before. It's just loading a single image from a 2000-frame stack. At least it isn't mac specific. 😬

EricThomson commented 1 year ago

Even just tifffile.imload() is seeing it as a single image, so this problem is bubbling down even into the back end tiff loader that we use for pretty much everything. I probably won't be able to get to this until after Thanksgiving vacation which is this week. But it is truly strange. the more information you can get me about this movie the better. Where is this data from how did you collect it, i.e., is it some proprietary image acquisition system or something custom?

If you can get me an even shorter version that would be helpful (like 400 frames), and with your permission I'll probably follow up with the developer of tifffile if I can't crack it. I've looked at the metadata in some detail and am not seeing anything particularly alarming. It opens fine in ImageJ, which is just bizarre.

cgohlke commented 1 year ago

The shared bigtiff_1.tif file contains 1994 separate "series". Tifffile returns the first series by default. Looks like the software used to produce the file did not use tifffile correctly to write a single-series multi-page TIFF.

andrestrocyte commented 1 year ago

Thanks a lot @cgohlke and @EricThomson.

The images are taken using a 2p microscope Bruker system and Prairie View. However, as the output were single image tiffs, I combined them into a big tiff using custom written code. As it passed the sanity check of ImageJ, I considered it was good.

I will check my code to generate single-series TIFFs and retry. I will come back here to confirm it is solved.

Thanks a lot for the help and Happy Thanksgiving!

EricThomson commented 1 year ago

Thanks a lot @cgohlke! That sounds like it will be the issue. I will have to figure out a way to check for this and hopefully correct for it in tiffit.

One thing at the workshop at SFN we realized we should provide is a method to convert a pile of tiff to a single multi page file. This comes up a lot.

@andrestrocyte feel free to share your code here, or I will post some that works when I get a chance.

andrestrocyte commented 11 months ago

Hello, @EricThomson . Thanks again for the help. I wrote some simple code that now appends all the individual single TIFFs into a single plane, single series, multipage big tiff.

It works well with the scripts for 20000 frames videos (9GB).

Should I still keep the videos below 4GB even if my bigger Bigtiffs are multipage? Would you be interested in me sharing the custom code?

EricThomson commented 11 months ago

Good to hear! You don't need to keep files under 4GB. Bigtiff can handle very large file sizes (many petabytes), so it's more a matter of how you want to organize things.

I'd definitely be curious to see what you did. Feel free to email it to me if you'd rather do that. I was planning to work on it this week so it would be good to see something that worked :smile:

andrestrocyte commented 11 months ago

Sure, I copy-paste, like this anyone with the same issue can benefit from it. Feel free to indicate any potential flaws.

Thanks a lot!

https://github.com/andrestrocyte/big-tiff_generator

EricThomson commented 11 months ago

Sure, I copy-paste, like this anyone with the same issue can benefit from it. Feel free to indicate any potential flaws.

Thanks a lot!

https://github.com/andrestrocyte/big-tiff_generator

Thanks that's really helpful: I was thinking of using PIL but I will copy you and use tifffile! BTW I love the logo! :smile:

I'll post more later. If we end up doing something like this in Caiman I'll let you know we should make you a contributor!

andrestrocyte commented 11 months ago

🙂Thanks for the help along the way!