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
635 stars 369 forks source link

Motion correction works on individual files but not on single concatenated file #819

Closed jchutrue closed 3 years ago

jchutrue commented 4 years ago

*You can get the CaImAn version by creating a params object and then typing params.data['caiman_version']. If the field doesn't exist, type N/A and consider upgrading)

I have 40+ video files that when concatenated together would form a continuous dataset (total of 16.6 GB). When I cm.save_memmap(), then cm.save_memmap_join() and run motion correction on the single concatenated file, the motion correction fails, with some very large shifts/jumps between consecutive frames. When I motion correct each of the files individually, using exactly the same parameters, I do not observe this problem. This effect was observed with both rigid and piecewise rigid motion correction.

The reason I do motion correction on the large concatenated file is because I want to run CNMF-E afterwards. If I run CNMF-E for each file, some neurons are merged when they shouldn't be. This is because those neurons are spatially close and sometimes fire at similar times during synchronous events. However with more data, it becomes clear that those neurons are indeed separate.

Finally, even for one video file the motion correction does not seem to correct slight rotations in the FOV well, so the subsequent CNMF-E thinks there are two overlapping neurons rather than one.

agiovann commented 3 years ago

@jchutrue are you sure there is not a dramatic change in the appearance in the field of view between the first and last movie? This might cause an issue similar to what you describe.

jchutrue commented 3 years ago

@agiovann There is a noticeable rotation between the FOVs of the first and last movie.

j-friedrich commented 3 years ago

You can try to motion correct each file separately, calculate the shifts between the templates of each file, and align the files with each other. The latter can be done by either calculating total_shifts=in_session_shifts+between_session_shifts and applying the total_shifts to the raw data, or by applying between_session_shifts to the separately motion corrected data. See issue #871 and this notebook. While it didn't seem to help in the notebook, it might help with your data.

Because you observe a noticeable rotation you'd need to use non-rigid (i.e. piece-wise rigid) instead of the simple rigid registration i used in the notebook.