def test_nmf(): img_file = 'boat.png' try: img = Image.open(img_file) except: print("Cannot load image %s : skipping test" %img_file) return None I = np.array(img) / 255. if I.ndim == 3: A = np.asfortranarray(I.reshape((I.shape[0],I.shape[1] * I.shape[2])),dtype = myfloat) rgb = True else: A = np.asfortranarray(I,dtype = myfloat) rgb = False m = 16;n = 16; X = spams.im2col_sliding(A,m,n,rgb) X = X[:,::10] X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1)),dtype = myfloat) ########## FIRST EXPERIMENT ########### tic = time.time() (U,V) = spams.nmf(X,return_lasso= True,K = 49,numThreads=4,iter = -5) tac = time.time() t = tac - tic print('time of computation for Dictionary Learning: %f' %t) print('Evaluating cost function...') Y = X - U * V R = np.mean(0.5 * (Y * Y).sum(axis=0)) print('objective function: %f' %R) return None
def test_nmf(): img_file = 'boat.png' try: img = Image.open(img_file) except: print "Cannot load image %s : skipping test" %img_file return None I = np.array(img) / 255. if I.ndim == 3: A = np.asfortranarray(I.reshape((I.shape[0],I.shape[1] * I.shape[2])),dtype = myfloat) rgb = True else: A = np.asfortranarray(I,dtype = myfloat) rgb = False m = 16;n = 16; X = spams.im2col_sliding(A,m,n,rgb) X = X[:,::10] X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1)),dtype = myfloat) ########## FIRST EXPERIMENT ########### tic = time.time() (U,V) = spams.nmf(X,return_lasso= True,K = 49,numThreads=4,iter = -5) tac = time.time() t = tac - tic print 'time of computation for Dictionary Learning: %f' %t print 'Evaluating cost function...' Y = X - U * V R = np.mean(0.5 * (Y * Y).sum(axis=0)) print 'objective function: %f' %R return None
def smaf(X, d, lda1, lda2, maxItr=10, UW=None, posW=False, posU=True, use_chol=False, module_lower=500, activity_lower=5, donorm=False, mode=1, mink=5, U0=[], U0_delta=0.1, doprint=False): # use Cholesky when we expect a very sparse result # this tends to happen more on the full vs subsampled matrices if UW == None: U, W = spams.nmf(np.asfortranarray(X), return_lasso=True, K=d, numThreads=THREADS) W = np.asarray(W.todense()) else: U, W = UW Xhat = U.dot(W) Xnorm = np.linalg.norm(X) ** 2 / X.shape[1] for itr in range(maxItr): if mode == 1: # In this mode the ldas correspond to an approximate desired fit # Higher lda will be a worse fit, but will result in a sparser sol'n U = spams.lasso(np.asfortranarray(X.T), D=np.asfortranarray(W.T), lambda1=lda2 * Xnorm, mode=1, numThreads=THREADS, cholesky=use_chol, pos=posU) U = np.asarray(U.todense()).T elif mode == 2: if len(U0) > 0: U = projected_grad_desc(W.T, X.T, U.T, U0.T, lda2, U0_delta, maxItr=400) U = U.T else: U = spams.lasso(np.asfortranarray(X.T), D=np.asfortranarray(W.T), lambda1=lda2, lambda2=0.0, mode=2, numThreads=THREADS, cholesky=use_chol, pos=posU) U = np.asarray(U.todense()).T if donorm: U = U / np.linalg.norm(U, axis=0) U[np.isnan(U)] = 0 if mode == 1: wf = (1 - lda2) W = sparse_decode(X, U, lda1, worstFit=wf, mink=mink) elif mode == 2: if len(U0) > 0: W = projected_grad_desc(U, X, W, [], lda1, 0., nonneg=posW, maxItr=400) else: W = spams.lasso(np.asfortranarray(X), D=np.asfortranarray(U), lambda1=lda1, lambda2=1.0, mode=2, numThreads=THREADS, cholesky=use_chol, pos=posW) W = np.asarray(W.todense()) Xhat = U.dot(W) module_size = np.average([np.exp(entropy(u)) for u in U.T if u.sum() > 0]) activity_size = np.average([np.exp(entropy(abs(w))) for w in W.T]) if doprint: print distance.correlation(X.flatten(), Xhat.flatten()), module_size, activity_size, lda1, lda2 if module_size < module_lower: lda2 /= 2. if activity_size < activity_lower: lda2 /= 2. return U, W
def online_NMF(self,n_components=30,method='nnsc',lambda1=100,iterations=-5,batchsize=512,model=None,**kwargs): """ Method performing online matrix factorization and using the spams (http://spams-devel.gforge.inria.fr/doc-python/html/index.html) package from Inria. Implements bith the nmf and nnsc methods Parameters ---------- n_components: int method: 'nnsc' or 'nmf' (see http://spams-devel.gforge.inria.fr/doc-python/html/index.html) lambda1: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html iterations: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html batchsize: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html model: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html **kwargs: more arguments to be passed to nmf or nnsc Return: ------- time_comps space_comps """ try: import spams except: print("You need to install the SPAMS package") raise T,d1,d2=np.shape(self) d=d1*d2 X=np.asfortranarray(np.reshape(self,[T,d],order='F')) if method == 'nmf': (time_comps,V) = spams.nmf(X,return_lasso= True ,K = n_components,numThreads=4,iter = iterations,**kwargs) elif method == 'nnsc': (time_comps,V) = spams.nnsc(X,return_lasso=True,K=n_components, lambda1 = lambda1,iter = iterations, model = model, **kwargs) else: raise Exception('Method unknown') space_comps=[] for idx,mm in enumerate(V): space_comps.append(np.reshape(mm.todense(),(d1,d2),order='F')) return time_comps,np.array(space_comps)
def snmf(self, img): """Sparse Non-negative Matrix Factorization with spams.""" img = np.asfortranarray(img) (W, H) = spams.nmf(img, K=2, return_lasso=True) H = np.array(H.todense()) return W, H
'map input file (libsvm) to <argv[3]> topics, output csv' from sklearn.datasets import load_svmlight_file import numpy as np import spams import sys input_file = sys.argv[1] output_file = sys.argv[2] try: num_topics = int( sys.argv[3] ) except IndexError: num_topics = 50 x_train, y_train = load_svmlight_file( input_file ) x_train_t = np.transpose( x_train ) u = spams.nmf( x_train_t, return_lasso = False, K = num_topics ) mapped_x = x_train * u y_train.shape = y_train.shape[0], 1 np.savetxt( output_file, np.hstack(( y_train, mapped_x )), delimiter = ",", fmt = '%.6f' )
#%% mcr = cm.load('/home/andrea/NEL-LAB Dropbox/Andrea Giovannucci/Kaspar-Andrea/exampledata/Other/403106_3min_rois_mc_lp_crop_2.hdf5') #%% fname = '/home/nel/data/voltage_data/Marton/454597/Cell_0/40x_patch1/movie/40x_patch1_000_mc_small.hdf5' #%% mcr = cm.load(fname) ycr = mcr.to_2D() ycr = ycr - ycr.min() mcr_lc = local_correlations_movie_offline(fname) ycr_lc = mcr_lc.to_2D() #%% D_lc,tr_lc = spams.nmf(np.asfortranarray(ycr_lc.T), K=2, return_lasso=True) plt.figure();plt.plot(tr_lc.T.toarray()) plt.figure();plt.imshow(D_lc[:,0].reshape(mcr.shape[1:], order='F'), cmap='gray') #%% D,tr = spams.trainDL(np.asfortranarray(ycr.T), K=2, D=D_lc, lambda1=0) #%% D,tr = spams.nnsc(np.asfortranarray(ycr.T), K=2, return_lasso=True, lambda1=0) #%% D,tr = spams.nmf(np.asfortranarray(np.abs(ycr.T)), K=2, return_lasso=True) #%% D,tr = spams.nnsc(np.asfortranarray(ycr.T), K=2, return_lasso=True, lambda1=1) #%% plt.figure();plt.plot(tr.T.toarray()) plt.figure();plt.imshow(D[:,1].reshape(mcr.shape[1:], order='F'), cmap='gray');plt.title('comp1')
Psi=ua, snr=SNR, use_ridge=False) Results['SVD (training)'] = compare_results(xa, x1a) x1b, phi, y, w, d, psi = recover_system_knownBasis(xb, measurements, sparsity, Psi=ua, snr=SNR, use_ridge=False) Results['SVD (testing)'] = compare_results(xb, x1b) np.save("./Data/linedata/GSE78779_svd.npy", x1b) ua, va = spams.nmf(np.asfortranarray(xa), return_lasso=True, K=dictionary_size, clean=True, numThreads=THREADS) x2a, phi, y, w, d, psi = recover_system_knownBasis(xa, measurements, sparsity, Psi=ua, snr=SNR, use_ridge=False) Results['sparse NMF (training)'] = compare_results(xa, x2a) x2b, phi, y, w, d, psi = recover_system_knownBasis(xb, measurements, sparsity, Psi=ua, snr=SNR, use_ridge=False)
u, s, vt = np.linalg.svd(xa, full_matrices=False) variance_fraction = np.cumsum(s**2 / (s**2).sum()) basis_size = np.where(variance_fraction > 1 - ERROR_THRESH)[0][0] basis_size = min(MAX_BASIS, basis_size) ua = u[:, :basis_size] va = np.diag(s[:basis_size]).dot(vt[:basis_size]) xh_svd = ua.dot(va) fit_svd = 1 - np.linalg.norm(xa - xh_svd)**2 / np.linalg.norm(xa)**2 module_size_svd = np.array([np.exp(entropy(abs(x))) for x in ua.T]) usage_svd = np.array([np.exp(entropy(abs(x))) for x in va.T]) ua_svd = ua w_svd = va ds = basis_size while True: ua, va = spams.nmf(np.asfortranarray(xa), return_lasso=True, K=ds, numThreads=THREADS) va = np.asarray(va.todense()) x0 = ua.dot(va) d = 1 - np.linalg.norm(xa - x0)**2 / np.linalg.norm(xa)**2 if (d > 1 - ERROR_THRESH) or (ds > xa.shape[1] * .4) or (ds > MAX_BASIS): break ds += max(1, ds / 10) xh_nmf = ua.dot(va) fit_nmf = 1 - np.linalg.norm(xa - xh_nmf)**2 / np.linalg.norm(xa)**2 module_size_nmf = np.array([np.exp(entropy(abs(x))) for x in ua.T]) usage_nmf = np.array([np.exp(entropy(abs(x))) for x in va.T]) ua_nmf = ua w_nmf = va k = min(int(xa.shape[1] * 1.5), ds * 4)
# NMf accepts onlt non-negative data, So it should not be centered. if m != 'nmf': X_m = X_m - X_m.mean(axis=0) # normalizing.. X_m = np.asfortranarray(X_m / np.sqrt((X_m * X_m).sum(axis=0)), dtype=np.float64) if m == 'pca': svd = TruncatedSVD(n_components=D, n_iter=7) W = svd.fit_transform(X_m) else: param = {'K': D, 'numThreads': -1, 'batchsize': 256, 'iter': 1000} if m == 'nmf': W = nmf(X_m, **param) else: param['verbose'] = False if m == 'spca': param['lambda1'] = 0.1 param['gamma1'] = 0.1 param['modeD'] = 1 W = trainDL(X_m, **param) elif m == 'dl': param['lambda1'] = 0.1 param['mode'] = 2 W = trainDL(X_m, **param) # resulting basisvectors will be visualized as image patches. img = displayPatches(W) img = np.uint8(img[:, :, 0] * 255.)
def online_NMF(self, n_components=30, method='nnsc', lambda1=100, iterations=-5, batchsize=512, model=None, **kwargs): """ Method performing online matrix factorization and using the spams (http://spams-devel.gforge.inria.fr/doc-python/html/index.html) package from Inria. Implements bith the nmf and nnsc methods Parameters ---------- n_components: int method: 'nnsc' or 'nmf' (see http://spams-devel.gforge.inria.fr/doc-python/html/index.html) lambda1: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html iterations: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html batchsize: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html model: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html **kwargs: more arguments to be passed to nmf or nnsc Return: ------- time_comps space_comps """ try: import spams except: print "You need to install the SPAMS package" raise T, d1, d2 = np.shape(self) d = d1 * d2 X = np.asfortranarray(np.reshape(self, [T, d], order='F')) if method == 'nmf': (time_comps, V) = spams.nmf(X, return_lasso=True, K=n_components, numThreads=4, iter=iterations, **kwargs) elif method == 'nnsc': (time_comps, V) = spams.nnsc(X, return_lasso=True, K=n_components, lambda1=lambda1, iter=iterations, model=model, **kwargs) else: raise Exception('Method unknown') space_comps = [] for idx, mm in enumerate(V): space_comps.append(np.reshape(mm.todense(), (d1, d2), order='F')) return time_comps, np.array(space_comps)