Skip to content

Analyzing TIFF files with time frames < 500 #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ViciousPlants opened this issue Nov 28, 2024 · 12 comments
Open

Analyzing TIFF files with time frames < 500 #323

ViciousPlants opened this issue Nov 28, 2024 · 12 comments

Comments

@ViciousPlants
Copy link

ViciousPlants commented Nov 28, 2024

After installing mesmerize-core and mesmerize-vis I was able to run "mcorr_cnmf.ipynb" with the "Sue_2x_3000_40_-46.tif" example. However, when loading my own TIFF files, running caiman gives error at the step of "Computing correlation image". After playing around with the number of frames (t) in the "Sue_2x_3000_40_-46.tif" example, I discovered that the same type of error is encountered whenever there is frame < 500. (Note, changing "num_frames_split" in the mcorr_params1 based on what I can find online does not help with this issue)

Below is the error from caiman.run()
Mes_err_2
ring caiman.run()

Below is the traceback error for "Sue_2x_3000_40_-46.tif" with 499 frames:
Mes_err

This is my first time trying to analyze calcium imaging data and although the user guides and API have much technical information, I am unsure how to link them to biological data/interpretation. Is there any reason why the input images/movies must be >= 500 frames?

  • OS: WIndows 10
  • mesmerize-core version: 0.4.0. install via pip with python 3.10
  • caiman version: 1.11.3
  • RAM: 128 GB
  • CPU: Intel i9-10940X
  • GPU: NVIDIA RTX A4000
@ethanbb
Copy link
Collaborator

ethanbb commented Nov 29, 2024

I could be wrong but I don't think this is intentional...local_correlations_movie_offline (in base CaImAn, not mesmerize-core) does not behave correctly if the window size is less than the number of frames; it should raise a more informative error, but it hasn't been implemented (e.g. see flatironinstitute/CaImAn#919). Right now the window size in the call in mesmerize is hard-coded to 1000. (Incidentally, if the number of frames is between 500 and 1000, it silently does the wrong thing.)

I think the best way to work around is to use min(1000, T) for the window size @kushalkolar? This footgun should really also be fixed upstream though.

@kushalkolar
Copy link
Collaborator

I don't know if this will be fixed, easiest way for you to move forward is to use your own fork and change the hardcoded values here: https://github.com/nel-lab/mesmerize-core/blob/master/mesmerize_core%2Falgorithms%2Fmcorr.py#L95-L96

I am unsure how to link them to biological data/interpretation.

What specifically are you unsure about?

@ViciousPlants
Copy link
Author

ViciousPlants commented Nov 30, 2024

Hey @ethanbb and @kushalkolar, thank you for the quick response. Some follow up questions:

I think the best way to work around is to use min(1000, T) for the window size?

I tried to do this by changing my image. Currently, it is 1000 px by 1000 px [I assume the X and Y are the window size??] with 103-time frames. However, similar error persists (see attached screenshot).
image

What specifically are you unsure about?

I am unsure why the window size has to be fixed to be 1000. What does the "window size" represent? The X and Y dimension of pixels during image acquisition of microscope?

easiest way for you to move forward is to use your own fork and change the hardcoded values here: https://github.com/nel-lab/mesmerize-core/blob/master/mesmerize_core%2Falgorithms%2Fmcorr.py#L95-L96

I changed the values to fit my images (see image below). However, the exact same error is still raised. The only change was that when "Computing correlation image" was running, it ran for longer before giving the error.
image

@kushalkolar
Copy link
Collaborator

It's the time dimension. Where did you make the change, if you have a local fork you have to pip install it in place. From the traceback it shows it's using the mescore in the environment.

@ethanbb
Copy link
Collaborator

ethanbb commented Nov 30, 2024

Yeah to add to this, sorry it was unclear, the "window" has nothing to do with X and Y so you don't have to truncate your movie or do anything else to it. It's just the length of the window in time that the local correlation algorithm uses, which can't be longer than the number of frames in your data.

@ViciousPlants
Copy link
Author

It's the time dimension. Where did you make the change, if you have a local fork you have to pip install it in place. From the traceback it shows it's using the mescore in the environment.

I installed mescore with mamba by creating a mescore env. I changed the code within the python script where it was installed in the mescore environment "C:\Users\name\miniconda3\envs\mescore\Lib\site-packages\mesmerize_core\algorithms"

Wouldn't pip install create a new script with the original values?

@kushalkolar
Copy link
Collaborator

You want to do an inplace install with a local fork where you maek your changes

git clone https://github.com/nel-lab/mesmerize-core.git
# make your changes
cd mesmerize-core
pip install -e .

@ViciousPlants
Copy link
Author

Update: I have successfully changed the mcorr.py and processed my data.

Follow-up: My actual data is 3D +time and the demo only have 2D data. Do you have any recommendations for the analysis of 3D data and options to tinker with?

@kushalkolar
Copy link
Collaborator

kushalkolar commented Dec 3, 2024

mesmerize-viz only supports 2D visualizations, you'll have to use the fastplotlib ImageWidget directly: https://github.com/fastplotlib/fastplotlib/blob/v0.1.0.a16/examples/notebooks/image_widget.ipynb

I've never done 3D motion correction but I think @ethanbb has experience with this

@ethanbb
Copy link
Collaborator

ethanbb commented Dec 3, 2024

Yes mesmerize-core (but not -viz) should support 3D motion correction and CNMF, as long as you are using CaImAn version 1.11.2 or later. Note that there are a few parameters that must be changed from their defaults in order for 3D motion correction to work. These can either be specified directly under params['main'] or in a dict at params['main']['motion']:

'is3D': True
'indices': (slice(None), slice(None), slice(None))  # or other slices if you want, in Y, X, Z order
'max_shifts': (max_y, max_x, max_z)  # (tuple of 3 ints, the default is (6, 6) for Y and X but you have to provide a value for Z)
# if you want to do piecewise motion correction...
'pw_rigid': True
'strides': (stride_y, stride_x, stride_z)  # again because default is a 2-element tuple
'overlaps': (overlap_y, overlap_x, overlap_z)  # ditto

You should check the main CaImAn demo for more information about what the piecewise stride/overlap parameters do.

Give it a try and let me know if you run into any problems.

@ViciousPlants
Copy link
Author

Give it a try and let me know if you run into any problems.

Hi, I am currently trying to with my data that has 50 z slices and 103 time slices. I used the following parameters:
image

But I encountered the following errors while running mc
image

Traceback is here:
image

Is this due to something missing in my image data resulting in NaN slices?

@ethanbb
Copy link
Collaborator

ethanbb commented Dec 4, 2024

I've never run into that error, unfortunately. I looked through the code a bit and it seems like with border_nan = 'copy', it would be unlikely for the template to end up containing NaNs unless the input image had NaNs, but I could be missing something. You could either try to step through carefully with a debugger and see where you start getting NaNs, or ask on the main CaImAn discussion board (https://github.com/flatironinstitute/CaImAn/discussions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants