forked from agiovann/CalBlitz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoPreprocessLabeling.py
234 lines (207 loc) · 6.3 KB
/
DemoPreprocessLabeling.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 16 17:56:14 2016
@author: agiovann
"""
#%%
try:
%load_ext autoreload
%autoreload 2
print 1
except:
print 'NOT IPYTHON'
import matplotlib as mpl
mpl.use('TKAgg')
from matplotlib import pyplot as plt
#plt.ion()
import sys
import numpy as np
import ca_source_extraction as cse
#sys.path.append('../SPGL1_python_port')
#%
from time import time
from scipy.sparse import coo_matrix
import tifffile
import subprocess
import time as tm
from time import time
import pylab as pl
import psutil
import calblitz as cb
import shutil
import glob
from ipyparallel import Client
import os
import glob
import h5py
import re
#%% Sue Ann create data for labeling
diameter_bilateral_blur=4
median_filter_size=(2,1,1)
from scipy.ndimage import filters as ft
res=[]
with open('file_list.txt') as f:
for ln in f:
ln1=ln[:-1]
print(ln1)
with h5py.File(ln1) as hh:
print hh.keys()
mov=np.array(hh['binnedF'],dtype=np.float32)
mov=cb.movie(mov,fr=3)
mov=mov.bilateral_blur_2D(diameter=diameter_bilateral_blur)
mov1=cb.movie(ft.median_filter(mov,median_filter_size),fr=3)
#mov1=mov1-np.median(mov1,0)
mov1=mov1.resize(1,1,.3)
mov1=mov1-cb.utils.mode_robust(mov1,0)
mov=mov.resize(1,1,.3)
# mov=mov-np.percentile(mov,1)
mov.save(ln1[:-3] + '_compress_.tif')
mov1.save(ln1[:-3] + '_BL_compress_.tif')
res.append(ln1[:-3] + '_compress_.tif')
res.append(ln1[:-3] + '_BL_compress_.tif')
#%% extract correlation image
with open('file_list.txt') as f:
for ln in f:
ln1=ln[:ln.find('summary')]
print(ln1)
with h5py.File(ln1 +'summary.mat') as hh:
print hh.keys()
mov=np.array(hh['binnedF'],dtype=np.float32)
mov=cb.movie(mov,fr=3)
img=cb.movie(mov.local_correlations(eight_neighbours=True),fr=1)
img.save(ln1[:-3] + 'summary._correlation_image_full.tif')
#%%
with open('file_list.txt') as f:
for ln in f:
ln1=ln[:ln.find('summary')]
print(ln1)
ffinal=ln1[:-3] + 'summary._correlation_image_full.tif'
shutil.copyfile(ffinal,os.path.join('./correlation_images/',os.path.split(ffinal)[-1]))
#%% Create images for Labeling
from glob import glob
import scipy.stats as st
import calblitz as cb
import numpy as np
for fl in res:
print(fl)
m=cb.load(fl,fr=3)
img=m.local_correlations(eight_neighbours=True)
im=cb.movie(img,fr=1)
im.save(fl[:-4]+'correlation_image.tif')
m=np.array(m)
img=st.skew(m,0)
im=cb.movie(img,fr=1)
im.save(fl[:-4]+'skew.tif')
img=st.kurtosis(m,0)
im=cb.movie(img,fr=1)
im.save(fl[:-4]+'kurtosis.tif')
img=np.std(m,0)
im=cb.movie(img,fr=1)
im.save(fl[:-4]+'std.tif')
img=np.median(m,0)
im=cb.movie(img,fr=1)
im.save(fl[:-4]+'median.tif')
img=np.max(m,0)
im=cb.movie(img,fr=1)
im.save(fl[:-4]+'max.tif')
#%% create and save tail probability image
from scipy.io import loadmat
idxChunk=0
with open('file_list.txt') as f:
for ln in f:
nf=ln[:-19]+'.proto-roi.mat'
print(nf)
c=loadmat(nf)
tailImg=c['prototypes']['metric'][0,idxChunk]['tailProb'][0][0]
cb.movie(tailImg,fr=1).save(ln[:-13]+'_compress_tail_img.tif')
#%% LOGIN TO MASTER NODE
# TYPE salloc -n n_nodes --exclusive
# source activate environment_name
#%%#%%
slurm_script='/mnt/xfs1/home/agiovann/SOFTWARE/Constrained_NMF/SLURM/slurmStart.sh'
cse.utilities.start_server(slurm_script=slurm_script)
#n_processes = 27#np.maximum(psutil.cpu_count() - 2,1) # roughly number of cores on your machine minus 1
pdir, profile = os.environ['IPPPDIR'], os.environ['IPPPROFILE']
client_ = Client(ipython_dir=pdir, profile=profile)
dview=client_[::2]
print 'Using '+ str(len(client_)) + ' processes'
#%% no server
if 0:
dview=None
#%%
import os
fnames=[]
base_folder='/mnt/ceph/users/agiovann/ImagingData/LABELLING/Farzaneh/'
for file_ in glob.glob(base_folder+'151102_001_00*[0-9].tif'):
# if not os.path.exists(file_[:-3]+'hdf5'):
fnames.append(file_)
fnames.sort()
print fnames
#%% motion correct
max_shift_w=45
max_shift_h=45
t1 = time()
file_res=cb.motion_correct_parallel(fnames,fr=30,template=None,margins_out=0,max_shift_w=max_shift_w, max_shift_h=max_shift_h,dview=dview,apply_smooth=True)
t2=time()-t1
print t2
#%%
all_movs=[]
for f in fnames:
print f
with np.load(f[:-3]+'npz') as fl:
# pl.subplot(1,2,1)
# pl.imshow(fl['template'],cmap=pl.cm.gray)
# pl.subplot(1,2,2)
# pl.plot(fl['shifts'])
all_movs.append(fl['template'][np.newaxis,:,:])
# pl.pause(.1)
# pl.cla()
#%%
all_movs=cb.movie(np.concatenate(all_movs,axis=0),fr=10)
all_movs,shifts,corss,_=all_movs.motion_correct(template=all_movs[1],max_shift_w=max_shift_w, max_shift_h=max_shift_h)
#%%
template=np.median(all_movs[:],axis=0)
np.save(base_folder+'template_total',template)
#%%
pl.imshow(template,cmap=pl.cm.gray,vmax=120)
#%%
all_movs.play(backend='opencv',gain=1,fr=100)
#%%
t1 = time()
file_res=cb.motion_correct_parallel(fnames,30,template=template,margins_out=0,max_shift_w=max_shift_w, max_shift_h=max_shift_h,dview=dview,remove_blanks=False)
t2=time()-t1
print t2
#%%
fnames=[]
for file in glob.glob(base_folder+'15*[0-9].hdf5'):
fnames.append(file)
fnames.sort()
print fnames
#%%
file_res=cb.utils.pre_preprocess_movie_labeling(dview, fnames, median_filter_size=(2,1,1),
resize_factors=[.2,.1666666666*3],diameter_bilateral_blur=4)
#%%
client_.close()
cse.utilities.stop_server(is_slurm=True,pdir=pdir,profile=profile)
#%%
#%%
fold=os.path.split(os.path.split(fnames[0])[-2])[-1]
os.mkdir(fold)
#%%
files=glob.glob(fnames[0][:-19]+'*BL_compress_.tif')
files.sort()
print files
#%%
m=cb.load_movie_chain(files,fr=3)
m.play(backend='opencv',gain=10,fr=10)
#%%
m.save(files[0][:-20]+'_All_BL.tif')
#%%
files=glob.glob(fnames[0][:-19]+'*[0-9]._compress_.tif')
files.sort()
print files
#%%
m=cb.load_movie_chain(files,fr=3)
m.play(backend='opencv',gain=3,fr=40)
#%%
m.save(files[0][:-20]+'_All.tif')