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

Save memory by returning a sparse array from extract_binary_masks_from_structural_channel #1395

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions caiman/base/rois.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,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:
Expand All @@ -92,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
"""

Expand All @@ -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)
Expand All @@ -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):
Expand Down
11 changes: 9 additions & 2 deletions caiman/source_extraction/cnmf/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down