kushalkolar / MESmerize

Platform for Calcium Imaging analysis. DEPRECATED.
GNU General Public License v3.0
58 stars 14 forks source link

[BUG] correlation_pnr() output has edge artifacts not present when I run same in caiman #62

Closed EricThomson closed 1 year ago

EricThomson commented 3 years ago

Describe the bug When I run the "inspect correlation and pnr" step of cnmfe and look at the image, there are very large edge artifacts in both the correlation image and pnr image that are not present when I run the same method in caiman.

To Reproduce Steps to reproduce the behavior, for example:

  1. Go to Caiman toolbox and select CNMF-E and enter gSig value (say, 7) and add to batch.
  2. Run the batch and then inspect the result.
  3. Anomalous image pops up.

Expected behavior Should look the same in both caiman and mesmerize.

Screenshots From caiman: pnr_gsig7_caiman

From mesmerize (note the bright artifact band around the outer edge of the image for both corr and pnr): pnr_gsig7

Operating System & specs (CPU, RAM etc.). Please complete the following information: Windows 10 128GB RAM Xeon Processor (2.4GHz)

Details about your Mesmerize install Mesmerize 0.7.1 installed using pip.

Additional comments I poked around in the source code: https://github.com/kushalkolar/MESmerize/blob/0b884e758a9fca2dac3e9106721ece5c0daf9f28/mesmerize/viewer/modules/batch_run_modules/CNMFE.py#L117

I can't see anything strange going on it is running the exact same method as the caiman code. This is truly a head-scratcher. I have seen since I started using mesmerize though so I know it's not just a one-off thing. I haven't dug very deeply into it would be nice to know if others see the same thing.

kushalkolar commented 3 years ago

Thanks for the details @EricThomson !

Are you setting your border pixel parameters when you run it yourself in a notebook?

kushalkolar commented 3 years ago

This looks like it's either due to border pixel not being set in your notebook, or the nan border not being set in mesmerize for some reason.

EricThomson commented 3 years ago

Good point I did not mess with that, and that's almost certainly the culprit. I was a little confused about how to set it as a kwarg because with the "stop here" option it seemed there is only the gSig parameter to tweak I assumed the kwarg window was only for when you go full CNMFE.

When I try to change params in the kwarg window (check Use CNMF kwargs box, and set bord_px=0 or border_nan='copy' -- or even border_pix=0) the border_pix parameter stays 12 so I am getting the same result.

But you are almost certainly right about the root of the problem: I haven't tried this yet but if I just go into the code and change it, it will be a reasonable workaround.

kushalkolar commented 3 years ago

Are you setting the bord pix when running it in a notebook though? Mesmerize should set it automatically.

On Wed, Aug 4, 2021, 08:13 Eric Thomson @.***> wrote:

Good point I did not mess with that, and that's almost certainly the culprit. I was a little confused about how to set it as a kwarg because with the "stop here" option it seemed there is only the gSig parameter to tweak I assumed the kwarg window was only for when you go full CNMFE.

When I try to change params in the kwarg window (e.g., with bord_px=0 or border_nan='copy' or border_pix=0) the border_pix parameter stays 12 so I am getting the same result.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/kushalkolar/MESmerize/issues/62#issuecomment-892607639, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACHXXRCZVASPCGRVX3RCDNLT3EVINANCNFSM5BQADOEA .

EricThomson commented 3 years ago

Yes in my notebook I'm setting it to 0 (I use border_nan='copy') and this is what I was trying to recreate with kwargs after you pointed out this was likely the issue.

But that is it for sure: when I manually set bord_px to 12 in my notebook, I get this same artifact. For now I can just tweak the relevant code in mesmerize (where it saves the memmapped file) to recreate my usual workflow until I get time.

kushalkolar commented 3 years ago

Ah ok, what if you set the border manually in the notebook though?

On Wed, Aug 4, 2021, 12:46 Eric Thomson @.***> wrote:

Yes I'm setting it to 0, and using border_nan='copy' this is what I was trying to recreate with kwargs.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/kushalkolar/MESmerize/issues/62#issuecomment-892811408, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACHXXRB25I7HFUPW4GQR2ZLT3FVHLANCNFSM5BQADOEA .

EricThomson commented 3 years ago

Yes as I mentioned when I manually set bord_px to 12 in my notebook (the same value it is being set to in mesmerize), I get the artifact so that is the issue.

kushalkolar commented 3 years ago

Ah ok! In your notebook if you don't set border pix does CNMF pick up junk in the border area?

EricThomson commented 3 years ago

Yes it looks like it, but frankly I haven't played with this at all I have always just used border_nan='copy' and sort of lived in ignorance of what that parameter did.

Or to be more precise, this (so I basically don't use the border pixel calculation spit out from motion correction):

bord_px = 0 if border_nan is 'copy' else bord_px

EricThomson commented 3 years ago

I think maybe just adding a border_pix option for this method would help. When I set bord_px in the kwarg window for cnmfe it seems to work for the full cnmfe calculation. It's just for the correlation_pnr() that there are problems. It wouldn't be a big deal but the artifact sometimes dominates the signal, making it hard to set pnr/corr values in the plot.

For now I'm just zeroing out bord_px out in cnmfe.py around line 49 where it pulls it from the motion correction outputs, and it seems to be working fine.

kushalkolar commented 3 years ago

@EricThomson So if I understand correctly, if there's a checkbox for border_nan='copy' which sets that kwarg and simultaneously doesn't pass the bord_px at all that would solve the issue? If so I could show you how to add this to GUI, takes 5 mins :D.

kushalkolar commented 3 years ago

You'll just need to install qtcreator, not sure how to do that on Windows though.

EricThomson commented 3 years ago

I think something like that would be good. Yes it would be great for me to start to learn to make these adjustments. I have a Linux machine with Qt Creator set up it might be better to do dev work there. Let me work on setting up a caiman/mesmerize environment on that machine this weekend, and getting Qt Creator resuscitated. I think maybe fewer headaches if I just do it on Ubuntu probably.

kushalkolar commented 2 years ago

@EricThomson Do you want to make a PR for this sometime?

EricThomson commented 2 years ago

Yes that would probably be helpful. I can do a quick one soon (probably next weekend I'm about to get slammed this week in lab stuff).