Skip to content
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

Include background component indices when removing empty/nan components in spatial.py #1422

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

ethanbb
Copy link
Contributor

@ethanbb ethanbb commented Nov 14, 2024

Description

This fixes an indexing error in the "Eliminating empty and nan components" step that occurs when using seeded CNMF. The error I was getting is as follows:

Traceback (most recent call last):
  File "/u/ethan/mesmerize-core/mesmerize_core/algorithms/cnmf.py", line 94, in run_algo
    cnm = cnm.fit(images)
          ^^^^^^^^^^^^^^^
  File "/u/ethan/CaImAn/caiman/source_extraction/cnmf/cnmf.py", line 523, in fit
    self.update_spatial(Yr, use_init=True)
  File "/u/ethan/CaImAn/caiman/source_extraction/cnmf/cnmf.py", line 915, in update_spatial
    update_spatial_components(Y, C=self.estimates.C, f=self.estimates.f, A_in=self.estimates.A,
  File "/u/ethan/CaImAn/caiman/source_extraction/cnmf/spatial.py", line 189, in update_spatial_components
    ind2_ = [ind_list[np.setdiff1d(a,ff)] if len(a) else a for a in ind2_]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/u/ethan/CaImAn/caiman/source_extraction/cnmf/spatial.py", line 189, in <listcomp>
    ind2_ = [ind_list[np.setdiff1d(a,ff)] if len(a) else a for a in ind2_]
             ~~~~~~~~^^^^^^^^^^^^^^^^^^^^
IndexError: index 1637 is out of bounds for axis 0 with size 1637

It looks like this is happening because when A_in is provided and has type bool, the following lines add indices corresponding to the spatial components (>= nr) to non-empty entries of ind2_:

ind2_ = [np.hstack((np.where(iid_)[0], nr + np.arange(f.shape[0])))
if np.size(np.where(iid_)[0]) > 0 else [] for iid_ in dist_indicator]

However, the code to remove empty components assumes that the highest original component number is nr-1:

ind_list = list(range(nr-np.size(ff)))
for i in ff:
ind_list.insert(i, 0)
ind_list = np.array(ind_list, dtype=int)
ind2_ = [ind_list[np.setdiff1d(a,ff)] if len(a) else a for a in ind2_]

To fix this, I changed this to use nr + nb. However, I'm not 100% sure that the behavior of adding indices for background components is correct in the first place, since it seems inconsistent with what happens with no masks passed in.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Has your PR been tested?

The CNMF run I that was failing for me before now succeeds. Since there wasn't a failing test before, I'm guessing this part of the code (under the condition of seeded CNMF) wasn't being tested, which could be changed.

@pgunn
Copy link
Member

pgunn commented Dec 18, 2024

We're likely to merge this after the next release; if you can test this the best you can with a variety of settings in the meantime that'd be great. Sorry for the delay.

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

Successfully merging this pull request may close these issues.

2 participants