Mercurial > hg > segmentation
comparison 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 |
comparison
equal
deleted
inserted
replaced
10:6d1c6639f5db | 11:915c849b17ea |
---|---|
49 nmf_mdl.factorize(niter=niter) | 49 nmf_mdl.factorize(niter=niter) |
50 F = np.asarray(nmf_mdl.W) | 50 F = np.asarray(nmf_mdl.W) |
51 G = np.asarray(nmf_mdl.H) | 51 G = np.asarray(nmf_mdl.H) |
52 return F, G | 52 return F, G |
53 | 53 |
54 | 54 def nmf(S, rank, nither=500): |
55 nmf_mdl = pymf.NMF(S, num_bases=rank, niter=nither) | |
56 nmf_mdl.factorize() | |
57 F = np.asarray(nmf_mdl.W) | |
58 G = np.asarray(nmf_mdl.H) | |
59 return F, G | |
60 | |
61 | |
55 def most_frequent(x): | 62 def most_frequent(x): |
56 """Returns the most frequent value in x.""" | 63 """Returns the most frequent value in x.""" |
57 return np.argmax(np.bincount(x)) | 64 return np.argmax(np.bincount(x)) |
58 | 65 |
59 | 66 |
92 G = np.sum(G, axis=1) | 99 G = np.sum(G, axis=1) |
93 G = SegUtil.median_filter(G[:, np.newaxis], R) | 100 G = SegUtil.median_filter(G[:, np.newaxis], R) |
94 return G.flatten() | 101 return G.flatten() |
95 | 102 |
96 | 103 |
97 def segmentation(X, rank=4, R=15, h=8, niter=300): | 104 def segmentation(X, rank=4, R=15, h=8, niter=300, CNMF=True): |
98 """ | 105 """ |
99 Gets the segmentation (boundaries and labels) from the factorization | 106 Gets the segmentation (boundaries and labels) from the factorization |
100 matrices. | 107 matrices. |
101 | 108 |
102 Parameters | 109 Parameters |
109 Size of the median filter for activation matrix | 116 Size of the median filter for activation matrix |
110 niter: int | 117 niter: int |
111 Number of iterations for k-means | 118 Number of iterations for k-means |
112 bound_idxs : list | 119 bound_idxs : list |
113 Use previously found boundaries (None to detect them) | 120 Use previously found boundaries (None to detect them) |
114 | 121 CNMF : bool |
122 If True, use CNMF; otherwise use NMF | |
123 | |
115 Returns | 124 Returns |
116 ------- | 125 ------- |
117 bounds_idx: np.array | 126 bounds_idx: np.array |
118 Bound indeces found | 127 Bound indeces found |
119 labels: np.array | 128 labels: np.array |
127 # Find non filtered boundaries | 136 # Find non filtered boundaries |
128 bound_idxs = None | 137 bound_idxs = None |
129 while True: | 138 while True: |
130 if bound_idxs is None: | 139 if bound_idxs is None: |
131 try: | 140 try: |
132 F, G = cnmf(X, rank, niter=niter) | 141 if CNMF: F, G = cnmf(X, rank, niter=niter) |
142 else: F, G = nmf(X, rank, niter=niter) | |
133 except: | 143 except: |
134 return np.empty(0), [1] | 144 return np.empty(0), [1] |
135 | 145 |
136 # Filter G | 146 # Filter G |
137 G = filter_activation_matrix(G.T, R) | 147 G = filter_activation_matrix(G.T, R) |