From ac98c654623ae21f0f143e56468c1299906cfcec Mon Sep 17 00:00:00 2001 From: zyyk78 Date: Mon, 12 Oct 2026 20:20:25 +0800 Subject: [PATCH] Fix AttributeError in CNMF.fit when low_rank_background is None --- caiman/source_extraction/cnmf/cnmf.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/caiman/source_extraction/cnmf/cnmf.py b/caiman/source_extraction/cnmf/cnmf.py index 70954ed68..29af2d668 100644 --- a/caiman/source_extraction/cnmf/cnmf.py +++ b/caiman/source_extraction/cnmf/cnmf.py @@ -466,8 +466,12 @@ def fit(self, images, indices=(slice(None), slice(None))) -> None: #print("D: Finished with run_CNMF_patches(), self.estimates.* are populated. Next step would be update_temporal() but first: Entering a shell.") #code.interact(local=dict(globals(), **locals()) ) - if (self.estimates.b is not None) and (len(list(np.where(~self.estimates.b.any(axis=0))[0])) > 0): # If any of the background ended up completely empty - raise Exception("After run_CNMF_patches(), one or more of the background components is empty. Please restart analysis with init/nb set to a lower value") + if self.estimates.b is not None: # If any of the background ended up completely empty + # run_CNMF_patches returns a sparse b when low_rank_background is + # None, and scipy sparse matrices have no .any(axis=...). + _b_nonempty = np.ravel(np.asarray((self.estimates.b != 0).sum(axis=0))) + if len(list(np.where(_b_nonempty == 0)[0])) > 0: + raise Exception("After run_CNMF_patches(), one or more of the background components is empty. Please restart analysis with init/nb set to a lower value") self.estimates.bl, self.estimates.c1, self.estimates.g, self.estimates.neurons_sn = None, None, None, None logger.info("Merging")