Skip to content

Commit

Permalink
l1_ratio in cnmf.paras.init for call to NMF()
Browse files Browse the repository at this point in the history
  • Loading branch information
pgunn committed Jan 14, 2025
1 parent 3ed5256 commit 6cf6c27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 13 additions & 4 deletions caiman/source_extraction/cnmf/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def initialize_components(Y, K=30, gSig=[5, 5], gSiz=None, ssub=1, tsub=1, nIter
elif method == 'sparse_nmf':
Ain, Cin, _, b_in, f_in = sparseNMF(
Y_ds, nr=K, nb=nb, max_iter_snmf=max_iter_snmf, alpha=alpha_snmf,
sigma_smooth=sigma_smooth_snmf, remove_baseline=remove_baseline, perc_baseline=perc_baseline_snmf)
sigma_smooth=sigma_smooth_snmf, remove_baseline=remove_baseline, perc_baseline=perc_baseline_snmf, options=options_total)

elif method == 'compressed_nmf':
Ain, Cin, _, b_in, f_in = compressedNMF(
Expand Down Expand Up @@ -485,7 +485,7 @@ def ICA_PCA(Y_ds, nr, sigma_smooth=(.5, .5, .5), truncate=2, fun='logcosh',
return A_in, C_in, center, b_in, f_in

def sparseNMF(Y_ds, nr, max_iter_snmf=200, alpha=0.5, sigma_smooth=(.5, .5, .5),
remove_baseline=True, perc_baseline=20, nb=1, truncate=2):
remove_baseline=True, perc_baseline=20, nb=1, truncate=2, options=None):
"""
Initialization using sparse NMF
Expand All @@ -511,6 +511,9 @@ def sparseNMF(Y_ds, nr, max_iter_snmf=200, alpha=0.5, sigma_smooth=(.5, .5, .5),
nb: int
Number of background components
options: dict
Extra options for sparseNMF
Returns:
A: np.array
2d array of size (# of pixels) x nr with the spatial components.
Expand All @@ -523,6 +526,12 @@ def sparseNMF(Y_ds, nr, max_iter_snmf=200, alpha=0.5, sigma_smooth=(.5, .5, .5),
2d array of size nr x 2 [ or 3] with the components centroids
"""
logger = logging.getLogger("caiman")

if options is not None and 'l1_ratio' in options:
l1_ratio_thresh = options['l1_ratio']
else:
l1_ratio_thresh = 0.0

m = scipy.ndimage.gaussian_filter(np.transpose(
Y_ds, np.roll(np.arange(Y_ds.ndim), 1)), sigma=sigma_smooth,
mode='nearest', truncate=truncate)
Expand All @@ -539,15 +548,15 @@ def sparseNMF(Y_ds, nr, max_iter_snmf=200, alpha=0.5, sigma_smooth=(.5, .5, .5),
d = np.prod(dims)
yr = np.reshape(m1, [T, d], order='F')

logger.debug(f"Running SparseNMF with alpha_W={alpha}")
logger.debug(f"Running SparseNMF with alpha_W={alpha} and l1_ratio={l1_ratio_thresh}")
mdl = NMF(n_components=nr,
verbose=False,
init='nndsvd',
tol=1e-10,
max_iter=max_iter_snmf,
shuffle=False,
alpha_W=alpha,
l1_ratio=0.0)
l1_ratio=l1_ratio_thresh)
C = mdl.fit_transform(yr).T
A = mdl.components_.T
A_in = A
Expand Down
3 changes: 2 additions & 1 deletion caiman/source_extraction/cnmf/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ def __init__(self, fnames=None, dims=None, dxy=(1, 1),
'gSiz': gSiz,
'init_iter': init_iter,
'kernel': None, # user specified template for greedyROI
'lambda_gnmf' :1, # regularization weight for graph NMF
'lambda_gnmf': 1, # regularization weight for graph NMF
'l1_ratio': 0.0,
'maxIter': 5, # number of HALS iterations
'max_iter_snmf': 500,
'method_init': method_init, # can be greedy_roi, corr_pnr sparse_nmf, local_NMF
Expand Down

0 comments on commit 6cf6c27

Please sign in to comment.