forked from flatironinstitute/CaImAn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_script_EFTY.py
169 lines (152 loc) · 4.18 KB
/
demo_script_EFTY.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 9 18:47:13 2015
@author: epnevmatikakis
"""
#%%
%load_ext autoreload
%autoreload 2
import sys
import numpy as np
import scipy.io as sio
sys.path.append("../SPGL1_python_port/")
#%%
import ca_source_extraction
from ca_source_extraction.initialization import greedyROI2d, hals_2D,arpfit
from ca_source_extraction.spatial import update_spatial_components
from ca_source_extraction.temporal import update_temporal_components
from ca_source_extraction.merging import mergeROIS
from ca_source_extraction.utilities import *
#%%
from matplotlib import pyplot as plt
from time import time
import pylab as pl
from scipy.sparse import coo_matrix
import scipy
from sklearn.decomposition import NMF
#%%
import calblitz as cb
#%%
import tifffile
#%%
#try:
# pl.ion()
# %load_ext autoreload
# %autoreload 2
#except:
# print "Probably not a Ipython interactive environment"
#%%
t = tifffile.TiffFile('movies/demoMovie.tif')
#t = libtiff.TiffFile('/Users/eftychios/Documents/_code/calcium_paper_code/datasets/clay/2014-04-05-003.tif')
Y = t.asarray()
Y = np.transpose(Y,(1,2,0))*1.
d1,d2,T=Y.shape
#%%
#m=cb.load('demoMovie.tif',fr=8);
#T,h,w=np.shape(m)
#Y=np.asarray(m)
#Y = np.transpose(Y,(1,2,0))
##Ymat = sio.loadmat('Y.mat')
##Y = Ymat['Y']*1.
#d1,d2,T = np.shape(Y)
#%%
m=cb.load('movies/demoMovie.tif',fr=8);
T,h,w=np.shape(m)
Y=np.asarray(m)
Y = np.transpose(Y,(1,2,0))
#Ymat = sio.loadmat('Y.mat')
#Y = Ymat['Y']*1.
d1,d2,T = np.shape(Y)
#%%
t = tifffile.TiffFile('movies/demoMovie.tif')
Y = t.asarray()
Y = np.transpose(Y,(1,2,0))*1.
d1,d2,T=Y.shape
a = sio.loadmat('movies/demo_movie_test.mat')
Y_test=a['Y']
assert scipy.linalg.norm(Y-Y_test)==0
#%%
#a = sio.loadmat('tmp.mat')
Ain_test=a['Ain']
Cin_test=a['Cin']
bin_test=a['bin']
fin_test=np.squeeze(a['fin'])
center_test=a['center']
#%%
#
#Ain=Ain_test
#Cin=Cin_test
#b_in=bin_test
#f_in=fin_test
#center=center_test-1
#%%
nr = 30
t1 = time()
Ain,Cin,_, b_in, f_in = greedyROI2d(Y, nr = nr, gSig = [4,4], gSiz = [9,9], use_median = False)
t_elGREEDY = time()-t1
Ain, Cin, b_in, f_in = hals_2D(Y, Ain, Cin, b_in, f_in,maxIter=10);
center = com(Ain,d1,d2);
Cn = local_correlations(Y)
plt.subplot(2,1,1)
plt1 = plt.imshow(Cn,interpolation='none')
plt.colorbar()
plt.scatter(x=center[:,1], y=center[:,0], c='m', s=40)
plt.axis((-0.5,d2-0.5,-0.5,d1-0.5))
plt.gca().invert_yaxis()
plt.subplot(2,1,2)
plt2 = plt.imshow(Cn,interpolation='none')
plt.colorbar()
plt.scatter(x=center_test[:,1]-1, y=center_test[:,0]-1, c='m', s=40)
plt.axis((-0.5,d2-0.5,-0.5,d1-0.5))
plt.gca().invert_yaxis()
#%%
plt.subplot(1,2,1)
crd = plot_contours(coo_matrix(Ain[:,::-1]),Cn,thr=0.9)
plt.subplot(1,2,2)
crd = plot_contours(coo_matrix(Ain_test[:,::-1]),Cn,thr=0.9)
#%% matlab input
#Ain=Ain_test
#Cin=Cin_test
#b_in=bin_test
#f_in=fin_test
#center=center_test-1
#%%
active_pixels = np.squeeze(np.nonzero(np.sum(Ain,axis=1)))
Yr = np.reshape(Y,(d1*d2,T),order='F')
p = 2;
P = arpfit(Yr,p=p,pixels = active_pixels)
Y_res = Yr - np.dot(Ain,Cin)
model = NMF(n_components=1, init='random', random_state=0)
model.fit(np.maximum(Y_res,0))
fin = model.components_.squeeze()
#%%
t1 = time()
A,b,Cin = update_spatial_components(Yr, Cin, fin, Ain, d1=d1, d2=d2, sn = P['sn'],dist=2,max_size=8,min_size=3)
t_elSPATIAL = time() - t1
#%%
crd = plot_contours(A,Cn,thr=0.9)
#%%
t1 = time()
C,f,Y_res,Pnew = update_temporal_components(Yr,A,b,Cin,fin,ITER=2,deconv_method = 'spgl1')
t_elTEMPORAL2 = time() - t1
#%%
t1 = time()
A_sp=A.tocsc();
A_m,C_m,nr_m,merged_ROIs,P_m=mergeROIS(Y_res,A_sp,b,np.array(C),f,d1,d2,Pnew,sn=P['sn'],thr=.85,deconv_method='spgl1',min_size=3,max_size=8,dist=2)
t_elMERGE = time() - t1
#%%
crd = plot_contours(A_m,Cn,thr=0.9)
#%%
A2,b2,C_m_ = update_spatial_components(Yr, C_m, f, A_m, d1=d1, d2=d2, sn = P['sn'],dist=2,max_size=8,min_size=3)
C2,f2,Y_res2,Pnew2 = update_temporal_components(Yr,A2,b2,C_m_,f,ITER=2,deconv_method = 'spgl1')
#%%
crd = plot_contours(A2,Cn,thr=0.9,cmap=pl.cm.gray)
#%%
A_or, C_or, srt = order_components(A2,C2)
C_df = extract_DF_F(Yr,A2,C2)
crd = plot_contours(coo_matrix(A_or[:,::-1]),Cn,thr=0.9)
#%%
Mn=np.std(m,axis=0)
crd = plot_contours(coo_matrix(A_or[:,::-1]),Mn,thr=0.9)
#%%
view_patches(Yr,coo_matrix(A_or),C_or,b,f,d1,d2)