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

Tracking the same ROIs for multiday recording of a single FOV #1249

Closed annahayoon closed 10 months ago

annahayoon commented 10 months ago

Hi, I have data of the same FOV over multidays, and I've registered the tiff stacks based on the first day's image.

I'm trying to use the same ROIs detected by CNMFe on the first day's data and propagate that for the rest of the days data. I've tried using Ain from the first day's data and running CNMFe for the other sets, but I end up getting a different spatial masks (I think due to the spatial update iteration). If I want to extract the background and time traces based on the SAME EXACT ROIs, how can I do that?

Thank you!

EricThomson commented 10 months ago

Thanks for the question. This is related to #1162 .

To my knowledge, Caiman doesn't let you just feed in a spatial mask and clamp it to remain fixed. It currently must go through the spatial update iterations and temporal update iterations. If you wanted to try something a bit risky, you could comment out the self.update_spatial() line in the code and see what happens :smile: and create a special "fixed spatial" version of CNMF (or do something like embed that line in a conditional that you activate with your own parameter). Note caveat emptor this would be untested behavior so you'd need to really play with it.

It's called at this line: https://github.com/flatironinstitute/CaImAn/blob/9b0b79ca61f20ce93259b9833e1fe18e26d4e086/caiman/source_extraction/cnmf/cnmf.py#L537

I'd be wary of doing this, but if you truly have fixed spatial footprints A, then maybe you could get away with it. Note I'm not saying that's the only thing you'd need to do in the code, you'd really need to work through it to get it to work. I could help you if you wanted to do this.

Not sure if you have any thoughts about this @j-friedrich

annahayoon commented 10 months ago

Hi Eric,

Yes, I would like to try that--could you help me to do this? Thank you!

EricThomson commented 10 months ago

@annahayoon the first thing I'd do is make a new CNMF class (name it CNMFSpaceFixed or something), and comment out every line of code that has update_spatial(). Create a cnmf object using that class, and see if it even works. :smile: That is, does it run without error? Second, do the results look even close to reasonable (i.e., are the A footprints indeed unchanged)? My guess is it will have problems, but you should try that and let me know what happens 😬

Edit added: @annahayoon If you are not sure how to do those steps, I could help with that too -- it would probably be best if you set up a development environment (as described in our Installation page).

annahayoon commented 10 months ago

After some tweaks, I was able to get it to work--I will share this code once I'm done cleaning through them. Meanwhile, I think I can close this case.

EricThomson commented 10 months ago

@annahayoon that's great please do share it once you are ready!