Mercurial > hg > segmentation
diff cnmf.py @ 11:915c849b17ea
added arg in cnmf script to choose to run standard nmf
author | mitian |
---|---|
date | Mon, 18 May 2015 17:43:48 +0100 |
parents | 294f66d285af |
children | c01fcb752221 |
line wrap: on
line diff
--- a/cnmf.py Thu May 14 18:01:00 2015 +0100 +++ b/cnmf.py Mon May 18 17:43:48 2015 +0100 @@ -51,7 +51,14 @@ G = np.asarray(nmf_mdl.H) return F, G - +def nmf(S, rank, nither=500): + nmf_mdl = pymf.NMF(S, num_bases=rank, niter=nither) + nmf_mdl.factorize() + F = np.asarray(nmf_mdl.W) + G = np.asarray(nmf_mdl.H) + return F, G + + def most_frequent(x): """Returns the most frequent value in x.""" return np.argmax(np.bincount(x)) @@ -94,7 +101,7 @@ return G.flatten() -def segmentation(X, rank=4, R=15, h=8, niter=300): +def segmentation(X, rank=4, R=15, h=8, niter=300, CNMF=True): """ Gets the segmentation (boundaries and labels) from the factorization matrices. @@ -111,7 +118,9 @@ Number of iterations for k-means bound_idxs : list Use previously found boundaries (None to detect them) - + CNMF : bool + If True, use CNMF; otherwise use NMF + Returns ------- bounds_idx: np.array @@ -129,7 +138,8 @@ while True: if bound_idxs is None: try: - F, G = cnmf(X, rank, niter=niter) + if CNMF: F, G = cnmf(X, rank, niter=niter) + else: F, G = nmf(X, rank, niter=niter) except: return np.empty(0), [1]