-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.py
252 lines (185 loc) · 9.71 KB
/
playground.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#%% Imports
import seaborn
import matplotlib.pyplot as plt
import numpy as np
#different matplotlib backend for better plots
import matplotlib
matplotlib.rcParams['backend'] = 'TkAgg'
##
import caiman as cm
#from caiman.source_extraction import cnmf
from caiman.utils.utils import download_demo
from caiman.utils.visualization import inspect_correlation_pnr
#from caiman.motion_correction import MotionCorrect
#from caiman.source_extraction.cnmf import params as params
# Other imports
import glob
import time
import natsort
import pickle as pkl
#%% Test for the automatic PNR CORR calculation
results = {}
times = []
path_list = []
cn_filters = []
pnr_list = []
frame_increments = []
results_file = r'O:\archive\projects\2023_students\Result_files\Test_summary_image_result.pkl'
paths = [r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230705_m1310_som_1410\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230704_m1310_som_1939\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230627_m1310_som_1325\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230620_m1310_som_1711\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230619_m1310_som_0947\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230616_m1310_som_1554\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230707_m1309_som_1152\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230612_m1309_som_1302\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230707_m1308_som_1520\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230615_m1308_som_0936\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230616_m1310_som_1150\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230705_m1309_som_1453\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230705_m1308_som_1534\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230608_m1308_som_1636\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230704_m1308_som_1813\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230608_m1310_som_1357\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230621_m1310_som_0933\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230802_m1309_som_1746\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230809_m1310_som_1243\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230801_m1310_som_1056\miniscope_video\*',
r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230728_m1310_som_1747\miniscope_video\*'
]
for path in paths:
files_names = glob.glob(path + r'*F_frames*1000.mmap')
files_names = natsort.natsorted(files_names)
for r in range(0,5):
file_increment = 5
images = []
st = time.time()
memmap_list = [] # a list of the individual videos as memmaps
for index, name in enumerate(files_names):
Yr, dims, T = cm.load_memmap(name, mode='r')
memmap_list.append(Yr.T)
gSig = (5, 5)
try:
images = np.concatenate(([item for item in memmap_list[r::file_increment]]), axis=0)
images = images.reshape(len(images), dims[0], dims[1], order='F')
cn_filter, pnr = cm.summary_images.correlation_pnr(images[::1], gSig=gSig[0], swap_dim=False)
except:
pass
et = time.time()
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
file_increment = str(file_increment)+ '_' + str(r)
frame_increments.append(file_increment)
times.append(elapsed_time)
cn_filters.append(cn_filter)
pnr_list.append(pnr)
path_list.append(path)
# set for the next round
del images
del memmap_list
# Save results in file
results['frame_increments'] = frame_increments
results['times'] = times
results['paths'] = path_list
results['cn_filters'] = cn_filters
results['pnr_list'] = pnr_list
pkl.dump(results, open(results_file, "wb"))
#
# results = pkl.load(open(results_file, "rb"))
# times = results['times']
# path_list = results['paths']
# cn_filters = results['cn_filters']
# pnr_list = results['pnr_list']
# frame_increments = results['frame_increments']
#%% test code to make the DB calulate Summary image on a seet of F_ordered memmaps instead of the big C_ordered one
st = time.time()
# path = r'D:\CaImAn_Data\data\1_preprocessed\20230405_m1018_wt_1110\*' # path to the result files
path = r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230405_m1018_wt_1110\miniscope_video\*' # sever path
frame_increment = 5 # downsamppling parameter
files_names = glob.glob(path + r'*F_frames*1000.mmap')
files_names = natsort.natsorted(files_names)
memmap_list = [] # a list of the individual videos as memmaps
for index,name in enumerate(files_names):
Yr, dims, T = cm.load_memmap(name, mode='r')
memmap_list.append(Yr.T)
gSig = (5, 5)
images = np.concatenate(([item for item in memmap_list[::frame_increment]]),axis = 0)
images = images.reshape(len(images),dims[0],dims[1],order='F')
cn_filter,pnr = cm.summary_images.correlation_pnr(images[::2], gSig=gSig[0], swap_dim=False)
et = time.time()
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
# cm.utils.visualization.inspect_correlation_pnr(cn_filter, pnr)
#%% TEST the test code for Summary image
results = {}
times = []
frame_increments = []
cn_filters = []
pnr_list = []
# path = r'D:\CaImAn_Data\data\1_preprocessed\20230405_m1018_wt_1110\*' # path to the result files
path = r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230405_m1018_wt_1110\miniscope_video\*' # sever path
files_names = glob.glob(path + r'*F_frames*1000.mmap')
files_names = natsort.natsorted(files_names)
for frame_increment in range(10,0,-1):
images = []
st = time.time()
memmap_list = [] # a list of the individual videos as memmaps
for index, name in enumerate(files_names):
Yr, dims, T = cm.load_memmap(name, mode='r')
memmap_list.append(Yr.T)
gSig = (5, 5)
try:
images = np.concatenate(([item for item in memmap_list[::frame_increment]]), axis=0)
images = images.reshape(len(images), dims[0], dims[1], order='F')
cn_filter, pnr = cm.summary_images.correlation_pnr(images[::1], gSig=gSig[0], swap_dim=False)
except:
pass
et = time.time()
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
# #collect results
# results = pkl.load(open("ResultsDB_F_Frame_Files.p", "rb"))
# times = results['times']
# frame_increments = results['frame_increments']
# cn_filters = results['cn_filters']
# pnr_list = results['pnr_list']
times.append(elapsed_time)
cn_filters.append(cn_filter)
pnr_list.append(pnr)
frame_increments.append(frame_increment)
# set for the next round
del images
del memmap_list
# do a safety save before testing the big file
results['times'] = times
results['frame_increments'] = frame_increments
results['cn_filters'] = cn_filters
results['pnr_list'] = pnr_list
pkl.dump( results, open( "ResultsDB_F_Frame_Files_1.p", "wb" ) )
# Test the big original file
video_for_calc = cm.load(r'O:\archive\projects\2023_intercontext\PICAST\data\1_preprocessed\20230405_m1018_wt_1110\miniscope_video\memmap__d1_341_d2_398_d3_1_order_C_frames_37564.mmap')
gsig = 5
for frame_increment in range(1,0,-1):
st = time.time()
try:
cn_filter, pnr = cm.summary_images.correlation_pnr(video_for_calc[::frame_increment], gSig=gsig, swap_dim=False)
except:
cn_filter = [0]
pnr = [0]
et = time.time()
elapsed_time = et - st
times.append(elapsed_time)
cn_filters.append(cn_filter)
pnr_list.append(pnr)
frame_increments.append(100000+frame_increment)
results['times'] = times
results['frame_increments'] = frame_increments
results['cn_filters'] = cn_filters
results['pnr_list'] = pnr_list
pkl.dump( results, open( "ResultsDB_F_Frame_Files_2.p", "wb" ) )
# os.system("shutdown /s /t 1")
# pkl.load( open ("ResultsDB_F_Frame_Files.p", "rb")
# cm.utils.visualization.inspect_correlation_pnr(cn_filter, pnr)
# cm.utils.visualization.inspect_correlation_pnr(cn_filters[i], pnr_list[i])
# seaborn.kdeplot(cn_filter.flatten())
#% Test powerpoint plot generation code