From 1b86d1af701167348dbdffb3498d18ab7865f402 Mon Sep 17 00:00:00 2001 From: Ethan Blackwood Date: Sun, 25 Aug 2024 17:08:52 -0400 Subject: [PATCH 1/3] Return a sparse array from extract_binary_masks_from_structural_channel --- caiman/base/rois.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/caiman/base/rois.py b/caiman/base/rois.py index fddd1471f..6b0195a26 100644 --- a/caiman/base/rois.py +++ b/caiman/base/rois.py @@ -8,6 +8,7 @@ import numpy as np import os import scipy +import scipy.sparse from scipy.ndimage import label, gaussian_filter from scipy.optimize import linear_sum_assignment import shutil @@ -106,7 +107,7 @@ def extract_binary_masks_from_structural_channel(Y, th = remove_small_objects(th, min_size=min_area_size) areas = label(th) - A = np.zeros((np.prod(th.shape), areas[1]), dtype=bool) + A = scipy.sparse.lil_array((areas[1], np.prod(th.shape)), dtype=bool) for i in range(areas[1]): temp = (areas[0] == i + 1) @@ -115,9 +116,9 @@ def extract_binary_masks_from_structural_channel(Y, elif expand_method == 'closing': temp = closing(temp, footprint=selem) - A[:, i] = temp.flatten('F') + A.getrowview(i)[:] = temp.flatten('F') - return A, mR + return A.tocsr().T, mR def mask_to_2d(mask): From d6e8310d8ccea20a643f945293125d6353b6187c Mon Sep 17 00:00:00 2001 From: Ethan Blackwood Date: Sun, 25 Aug 2024 21:55:54 -0400 Subject: [PATCH 2/3] Make computing_indicator work with sparse A_in --- caiman/source_extraction/cnmf/spatial.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/caiman/source_extraction/cnmf/spatial.py b/caiman/source_extraction/cnmf/spatial.py index 0ba5f2b4e..a72c28385 100644 --- a/caiman/source_extraction/cnmf/spatial.py +++ b/caiman/source_extraction/cnmf/spatial.py @@ -1063,8 +1063,15 @@ def computing_indicator(Y, A_in, b, C, f, nb, method, dims, min_size, max_size, Y) - dist_indicator_av.T.dot(b).dot(f), 0) A_in = scipy.sparse.coo_matrix(A_in.astype(np.float32)) nr, _ = np.shape(C) # number of neurons - 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] + ind2_ = [] + for iid_ in dist_indicator: + if scipy.sparse.issparse(iid_): + iid_ = iid_.toarray().squeeze() + comps = np.where(iid_)[0] + if np.size(comps) > 0: + ind2_.append(np.hstack((comps, nr + np.arange(f.shape[0])))) + else: + ind2_.append([]) else: if C is None: From 4a2fc991f254e1982b40edc7988e26ea85454062 Mon Sep 17 00:00:00 2001 From: Ethan Blackwood Date: Sun, 25 Aug 2024 23:00:26 -0400 Subject: [PATCH 3/3] Fix types for extract_binary_masks_from_structural_channel --- caiman/base/rois.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/caiman/base/rois.py b/caiman/base/rois.py index 6b0195a26..66b2cd287 100644 --- a/caiman/base/rois.py +++ b/caiman/base/rois.py @@ -67,7 +67,7 @@ def extract_binary_masks_from_structural_channel(Y, min_hole_size: int = 15, gSig: int = 5, expand_method: str = 'closing', - selem: np.array = np.ones((3, 3))) -> tuple[np.ndarray, np.array]: + selem: np.ndarray = np.ones((3, 3))) -> tuple[scipy.sparse.csc_array, np.ndarray]: """Extract binary masks by using adaptive thresholding on a structural channel Args: @@ -93,7 +93,7 @@ def extract_binary_masks_from_structural_channel(Y, A: sparse column format matrix matrix of binary masks to be used for CNMF seeding - mR: np.array + mR: np.ndarray mean image used to detect cell boundaries """