nel-lab / mesmerize-core

High level pandas-based API for batch analysis of Calcium Imaging data using CaImAn
Other
60 stars 15 forks source link

cnmf_eval and cnmf_viz notebooks unable to import completed CNMF batch items (+ missing 'corr-img-path') #214

Closed wmsheer closed 1 year ago

wmsheer commented 1 year ago

Hi Kushal,

I'm having an issue with both the cnmf_eval and cnmf_viz notebooks where I have never yet been able to successfully load in any completed CNMF batch item for visualization. Specifically, the following line:

contours, coms = df.iloc[index].cnmf.get_contours("all", swap_dim=False)

always yields the following error message for me:

C:\Users\sheer\anaconda3\envs\mescore\lib\site-packages\mesmerize_core\caiman_extensions\cnmf.py:354: RuntimeWarning: Mean of empty slice.
  com = coors.mean(axis=0)
C:\Users\sheer\anaconda3\envs\mescore\lib\site-packages\numpy\core\_methods.py:184: RuntimeWarning: invalid value encountered in divide
  ret = um.true_divide(

My guess is that it seems that there is some issue with a NaN or 0 value being used in a numpy operation. Looking through the coms variable, I do see a few NaN values, which tracks with similar warnings I see when I originally run the CNMF algorithm (e.g., WARNING:root:NaN values detected for space correlation in [2605 3408 4731 5269 6331 6919 6947 7270 8161]. Changing their value to -1.). Do you know what may be happening here and what the best way is to get around it to visualize/evaluate my CNMF components?

Also, l if I continue beyond this original issue, I consistently get the following error message in a subsequent cell. Not sure if this is related to some variable not being able to be defined because of the previous issue:

rcm = df.iloc[index].cnmf.get_rcm()
corr_img = df.iloc[index].caiman.get_corr_image()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[22], line 2
      1 rcm = df.iloc[index].cnmf.get_rcm()
----> 2 corr_img = df.iloc[index].caiman.get_corr_image()

File ~\anaconda3\envs\mescore\lib\site-packages\mesmerize_core\caiman_extensions\_utils.py:25, in validate.<locals>.dec.<locals>.wrapper(self, *args, **kwargs)
     23     tb = self._series["outputs"]["traceback"]
     24     raise BatchItemUnsuccessfulError(f"Batch item was unsuccessful, traceback from subprocess:\n{tb}")
---> 25 return func(self, *args, **kwargs)

File ~\anaconda3\envs\mescore\lib\site-packages\mesmerize_core\caiman_extensions\common.py:559, in CaimanSeriesExtensions.get_corr_image(self)
    551 @validate()
    552 def get_corr_image(self) -> np.ndarray:
    553     """
    554     Returns
    555     -------
    556     np.ndarray
    557         correlation image
    558     """
--> 559     path = self._series.paths.resolve(self._series["outputs"]["corr-img-path"])
    560     return np.load(str(path))

KeyError: 'corr-img-path'
kushalkolar commented 1 year ago

Hi Kushal,

I'm having an issue with both the cnmf_eval and cnmf_viz notebooks where I have never yet been able to successfully load in any completed CNMF batch item for visualization. Specifically, the following line:

contours, coms = df.iloc[index].cnmf.get_contours("all", swap_dim=False)

always yields the following error message for me:

C:\Users\sheer\anaconda3\envs\mescore\lib\site-packages\mesmerize_core\caiman_extensions\cnmf.py:354: RuntimeWarning: Mean of empty slice.
  com = coors.mean(axis=0)
C:\Users\sheer\anaconda3\envs\mescore\lib\site-packages\numpy\core\_methods.py:184: RuntimeWarning: invalid value encountered in divide
  ret = um.true_divide(

My guess is that it seems that there is some issue with a NaN or 0 value being used in a numpy operation. Looking through the coms variable, I do see a few NaN values, which tracks with similar warnings I see when I originally run the CNMF algorithm (e.g., WARNING:root:NaN values detected for space correlation in [2605 3408 4731 5269 6331 6919 6947 7270 8161]. Changing their value to -1.). Do you know what may be happening here and what the best way is to get around it to visualize/evaluate my CNMF components?

This is just a runtime warning, are you able to visualize contours? What does this give you:

import fastplotlib as fpl

plot = fpl.Plot()

contours, coms = df.iloc[index].cnmf.get_contours("good", swap_dim=False)

plot.add_line_collection(contours)

plot.show()

Also, l if I continue beyond this original issue, I consistently get the following error message in a subsequent cell. Not sure if this is related to some variable not being able to be defined because of the previous issue:

rcm = df.iloc[index].cnmf.get_rcm()
corr_img = df.iloc[index].caiman.get_corr_image()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[22], line 2
      1 rcm = df.iloc[index].cnmf.get_rcm()
----> 2 corr_img = df.iloc[index].caiman.get_corr_image()

File ~\anaconda3\envs\mescore\lib\site-packages\mesmerize_core\caiman_extensions\_utils.py:25, in validate.<locals>.dec.<locals>.wrapper(self, *args, **kwargs)
     23     tb = self._series["outputs"]["traceback"]
     24     raise BatchItemUnsuccessfulError(f"Batch item was unsuccessful, traceback from subprocess:\n{tb}")
---> 25 return func(self, *args, **kwargs)

File ~\anaconda3\envs\mescore\lib\site-packages\mesmerize_core\caiman_extensions\common.py:559, in CaimanSeriesExtensions.get_corr_image(self)
    551 @validate()
    552 def get_corr_image(self) -> np.ndarray:
    553     """
    554     Returns
    555     -------
    556     np.ndarray
    557         correlation image
    558     """
--> 559     path = self._series.paths.resolve(self._series["outputs"]["corr-img-path"])
    560     return np.load(str(path))

KeyError: 'corr-img-path'

It looks like we didn't add the correlation image to the cnmfe outputs (I'm assuming you're using cnmfe) because you usually look at those before CNMFE, the "corr-pnr seeding" section in the CNMFE notebook: https://github.com/nel-lab/mesmerize-core/blob/master/notebooks/cnmfe.ipynb

kushalkolar commented 1 year ago

On point (1), I also wonder if you ran into a case where the contours for a component are empty. I don't exactly know how this happens but I've seen it once before in my data, a component that is comprised of zero pixels! @EricThomson have you run into this, or any ideas? I wonder if such zero-pixel components have a meaningful temporal component.

If this is the case then the code I gave above should produce a blank plot, but matplotlib should be able to plot it:

from matplotlib import pyplot as plt

contours, coms = df.iloc[index].cnmf.get_contours("good", swap_dim=False)

for contour in contours:
  plt.plot(contour[:, 0], contour[:, 1])

plt.show()

If that is indeed the case, then I can get on with fixing https://github.com/pygfx/pygfx/issues/549 :smile:

wmsheer commented 1 year ago

Sorry for the delayed response - got caught up with some other lab work and then the long weekend.

I suspect the contours of one of my components are empty given the nature of the runtime warning, but both of those suggestions of yours worked in producing an image: image

image

So I expect that I should be able to move forward with visualization/evaluation via the final cell in the cnmf_eval notebook so long as I have something to point it to for corr_img. What do you think the best thing to define as that would be? Some modification of iw_corr_pnr in the cnmfe notebook?

kushalkolar commented 1 year ago

I suspect the contours of one of my components are empty given the nature of the runtime warning, but both of those suggestions of yours worked in producing an image: image

This is just a warning that it's converting float64 data to flaot32 (because regular commerical GPUs don't handle float32).

So I expect that I should be able to move forward with visualization/evaluation via the final cell in the cnmf_eval notebook so long as I have something to point it to for corr_img. What do you think the best thing to define as that would be? Some modification of iw_corr_pnr in the cnmfe notebook?

The eval notebook isn't ready yet, been bogged down with a lot of things. I will post in the gitter when it's ready.

kushalkolar commented 1 year ago

closing due to inactivity, feel free to reopen.