forked from yves-weissenberger/twoptb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_Viewer
executable file
·388 lines (308 loc) · 16.1 KB
/
Data_Viewer
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#! /home/yves/anaconda2/bin/python
import h5py
import sys
def MASK_DRAWER_GUI(areaFile,restart=False):
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph import Qt
import pyqtgraph as pg
import time
import sys
import os
import copy as cp
from skimage.filters import gaussian as gaussian_filter
from skimage.morphology import disk
from skimage.filters.rank import median as median_filter
class Visualizator(QtGui.QMainWindow):
def __init__(self, ROI_patches,ROI_masks,areaFile):
QtGui.QMainWindow.__init__(self)
self.Folder = os.path.dirname(areaFile.file.filename) + '/'
self.idx = 0
self.ROI_patches = ROI_patches
if 'mean_image' in (areaFile.attrs.iterkeys()):
self.meanIm = areaFile.attrs['mean_image']
else:
self.meanIm = np.mean(areaFile[:100],axis=0)
self.masks = ROI_masks
self.play_video = False
self.rolling_average = 10
if ROI_centres!=None:
self.ROI_centres = areaFile.attrs['ROI_centres']
self.video = areaFile
self.nFrames = self.video.shape[0]
self.frame_idx = self.rolling_average
self.smoothing = 1
self.maskalpha = 0.1
self.show_mean_image = True
#initialise the main window
w = QtGui.QWidget()
layout = QtGui.QGridLayout()
w.setLayout(layout)
self.prevT = time.time()
self.IFI=5
self.vidTimer = QtCore.QTimer()
self.vidTimer.timeout.connect(self.update_video)
self.vidTimer.start(self.IFI)
self.img = pg.ImageItem()
self.img.setImage(self.video[self.frame_idx])
self.frameTxt = pg.TextItem('Frame Nr: ' + str(self.frame_idx+1) + '/' + str(self.nFrames))
""" This section contains the code to
create and upgrade the histogram
used to control the image """
self.histLI = pg.HistogramLUTWidget(image=self.img,fillHistogram=False)
self.histLI.autoHistogramRange=False
self.vb = pg.ViewBox()
self.vb.setAspectLocked(1)
self.vb.addItem(self.img)
#self.vb.addItem(self.tx)
self.vb.addItem(self.frameTxt)
grV1 = pg.GraphicsView()
grV1.setCentralItem(self.vb)
self.vb.scene().sigMouseMoved.connect(self.mouseMoved)
self.Gplt = pg.PlotWidget(background='w')
self.Gplt.setFixedHeight(200)
self.Gplt.setXRange(0,self.nFrames)
self.timeLine = pg.InfiniteLine(pos=self.frame_idx,angle=90,movable=True)
self.Gplt.addItem(self.timeLine)
self.timeLine.sigDragged.connect(self.update_timeline)
btn1 = QtGui.QPushButton("Previous ROI", self)
btn2 = QtGui.QPushButton("Previous ROI", self)
btn3 = QtGui.QPushButton("Save Progress", self)
btn4 = QtGui.QPushButton("Clear ROI", self)
btn5 = QtGui.QPushButton("Play Video", self)
btn6 = QtGui.QPushButton("Update ROI Trace",self)
btn7 = QtGui.QPushButton("Increase Rolling Average", self)
btn8 = QtGui.QPushButton("Decrease Rolling Average", self)
btn9 = QtGui.QPushButton("Increase Spatial Smoothing", self)
btn10 = QtGui.QPushButton("Decrease Spatial Smoothing", self)
btn11 = QtGui.QPushButton("Show Mean Image", self)
btn12 = QtGui.QPushButton("Hide Mask", self)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
btn3.clicked.connect(self.save_ROIS)
btn4.clicked.connect(self.buttonClicked)
btn5.clicked.connect(self.buttonClicked)
btn6.clicked.connect(self.buttonClicked)
btn7.clicked.connect(self.buttonClicked)
btn8.clicked.connect(self.buttonClicked)
btn9.clicked.connect(self.buttonClicked)
btn10.clicked.connect(self.buttonClicked)
btn11.clicked.connect(self.buttonClicked)
btn12.clicked.connect(self.buttonClicked)
layout.addWidget(grV1,0,0,7,8)
layout.addWidget(btn1,9,1,1,1)
layout.addWidget(btn2,9,0,1,1)
layout.addWidget(btn3,10,2,1,1)
layout.addWidget(btn4,10,3,1,1)
layout.addWidget(btn5,9,2,1,1)
layout.addWidget(btn6,9,3,1,1)
layout.addWidget(btn7,9,5,1,1)
layout.addWidget(btn8,9,6,1,1)
layout.addWidget(btn9,10,5,1,1)
layout.addWidget(btn10,10,6,1,1)
layout.addWidget(btn11,10,1,1,1)
layout.addWidget(btn12,10,0,1,1)
layout.addWidget(self.Gplt,11,0,3,8)
layout.addWidget(self.histLI,0,8,7,2)
self.setCentralWidget(w)
self.show()
#self.connect(self, Qt.SIGNAL('triggered()'), self.closeEvent
def save_ROIS(self):
#arr = cp.deepcopy(np.array((self.masks)))
#areaFile.attrs['ROI_masks'] = arr
##-------- Save ROIs to hdf5
#f_handle = h5py.File(self.Folder +'ROI_data.h5','w',libver='latest')
#f_handle.create_dataset('ROI_masks',data=cp.deepcopy(np.array((self.masks))),dtype='int16')
#f_handle.create_dataset('ROI_patches',data=areaFile.attrs['ROI_patches'],dtype='int16')
#f_handle.create_dataset('ROI_locs',data=np.array(areaFile.attrs['ROI_centres']),dtype='int16')
#f_handle.attrs['parent_file'] = areaFile.name
#f_handle.close()
print 'EMPTY'
def closeEvent(self, event):
print 'leaving now \n you have drawn %s ROIs' %self.masks.shape[2]
event.accept() # let the window close
#areaFile
def buttonClicked(self):
sender = self.sender()
if sender.text()=='Next ROI':
if self.idx<nROIs-1:
self.idx += 1
self.frame_idx = self.rolling_average
self.Gplt.clear()
self.Gplt.addItem(self.timeLine)
#elif sender.text()=='Previous ROI':
# if self.idx>=1:
# self.idx -= 1
# self.frame_idx = self.rolling_average
# self.Gplt.clear()
# self.Gplt.addItem(self.timeLine)
#elif sender.text()=='Clear ROI':
# self.masks[:,:,self.idx] = 0
#elif sender.text()=='Hide Mask':
# if self.maskalpha==0:
# self.maskalpha=0.2
# self.img_ROI.setOpacity(self.maskalpha)
#
# else:
# self.maskalpha=0
# self.img_ROI.setOpacity(self.maskalpha)
#
elif sender.text()=='Increase Rolling Average':
self.rolling_average += 1
print self.rolling_average
elif sender.text()=='Show Mean Image':
self.show_mean_image = not self.show_mean_image
elif sender.text()=='Decrease Rolling Average':
if self.rolling_average>=1:
self.rolling_average -= 1
print self.rolling_average
elif sender.text()=='Increase Spatial Smoothing':
self.smoothing += 0.1
elif sender.text()=='Decrease Spatial Smoothing':
if self.smoothing>0:
self.smoothing -= .1
elif (sender.text()=='Play Video' or sender.text()=='Pause Video'):
self.play_video = not self.play_video
self.show_mean_image = False
if not self.play_video:
sender.setText("Play Video")
else:
sender.setText('Pause Video')
#elif sender.text()=="Update ROI Trace":
# print 'button regged'
# if ((self.masks.shape[1]<self.ROI_centres[self.idx][0]<512-self.masks.shape[1]) and
# (self.masks.shape[1]<self.ROI_centres[self.idx][1]<512-self.masks.shape[1])):
# print 'check completed %s' %self.ROI_centres[self.idx]
# trace = np.mean(self.masks[:,:,self.idx]*self.video[:,int(self.ROI_centres[self.idx][1]-self.masks.shape[1]/2):int(self.ROI_centres[self.idx][1]+self.masks.shape[1]/2),
# int(self.ROI_centres[self.idx][0] - self.masks.shape[1]/2):int(self.ROI_centres[self.idx][0]+self.masks.shape[1]/2)],
# axis=(1,2))
# print 'trace extracted'
# self.Gplt.plot(trace)
# else:
# print 'unable to plot outside of range'
#self.tx.setText('ROI Nr: ' + str(self.idx+1) + '/' + str(nROIs))
#mask = np.zeros([self.masks.shape[0],self.masks.shape[1],3])
#print self.masks.shape
#mask[np.where(self.masks[:,:,self.idx])] = (1,0,0)
#self.img_ROI.setImage(mask)
self.img.setImage(self.video[self.frame_idx])
#Play Video Play Function
def update_video(self):
if self.play_video:
video_image = np.mean(self.video[self.frame_idx-self.rolling_average:self.frame_idx+self.rolling_average+1],axis=0)
#video_image = median_filter(self.video,disk(2))
#video_image = gaussian_filter(video_image,self.smoothing)
self.img.setImage(video_image,autoLevels=False)
if self.frame_idx>=self.nFrames-1:
self.frame_idx=self.rolling_average
self.frame_idx += 1
self.frameTxt.setText('Frame Nr: ' + str(self.frame_idx+1) + '/' + str(self.nFrames))
self.timeLine.setPos(self.frame_idx)
if (self.show_mean_image and not self.play_video):
self.img.setImage(self.meanIm)
#if (not self.show_mean_image and not self.play_video):
#
# video_image = np.mean(self.video[self.frame_idx-self.rolling_average:self.frame_idx+self.rolling_average+1,
# int(self.ROI_centres[self.idx][1]-self.masks.shape[1]/2):int(self.ROI_centres[self.idx][1]+self.masks.shape[1]/2),
# int(self.ROI_centres[self.idx][0] - self.masks.shape[1]/2):int(self.ROI_centres[self.idx][0]+self.masks.shape[1]/2)],
# axis=0)
#
# #video_image = median_filter(self.video,disk(2))
# video_image = gaussian_filter(video_image,self.smoothing)
# self.img.setImage(self.video[self.frame_idx],autoLevels=False)
#else:
# img.setImage(np.fliplr(np.mean(b[frame_idx-rolling_average:frame_idx+rolling_average+1,:,:],axis=0).T),
# autoLevels=False)
def update_timeline(self):
while self.timeLine.isUnderMouse():
self.play_video = False
self.frame_idx = int(self.timeLine.getXPos())
self.timeLine.setPos(self.frame_idx)
#img.setImage(np.fliplr(np.mean(b[frame_idx-rolling_average:frame_idx+rolling_average+1,:,:],axis=0).T),autoLevels=False)
self.play_video = True
def mouseMoved(self,e):
if (time.time() - self.prevT)>0.01:
a = self.img.mapFromScene(e)
x_pos = a.x()
y_pos = a.y()
if (y_pos<(self.masks.shape[1]-1) and x_pos<(self.masks.shape[0]-1) and
y_pos>0 and x_pos>0):
if y_pos>self.masks.shape[1]:
y_pos=self.masks.shape[1]
if x_pos>self.masks.shape[0]:
x_pos=self.masks.shape[0]
modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == (QtCore.Qt.ControlModifier | QtCore.Qt.ShiftModifier):
self.masks[:,:,self.idx][int(np.floor(x_pos)),int(np.floor(y_pos))] = 1
self.masks[:,:,self.idx][int(np.ceil(x_pos)),int(np.ceil(y_pos))] = 1
mask = np.zeros([self.masks.shape[0],self.masks.shape[1],3])
mask[np.where(self.masks[:,:,self.idx])] = (1,0,0)
self.img_ROI.setImage(mask)
elif (modifiers == QtCore.Qt.ControlModifier and
not modifiers == (QtCore.Qt.ControlModifier | QtCore.Qt.ShiftModifier)):
self.masks[:,:,self.idx][int(x_pos),int(y_pos)] = 1
mask = np.zeros([self.masks.shape[0],self.masks.shape[1],3])
mask[np.where(self.masks[:,:,self.idx])] = (1,0,0)
self.img_ROI.setImage(mask)
elif (modifiers == QtCore.Qt.AltModifier and
not modifiers == (QtCore.Qt.ShiftModifier | QtCore.Qt.AltModifier)):
self.masks[:,:,self.idx][int(x_pos),int(y_pos)] = 0
mask = np.zeros([self.masks.shape[0],self.masks.shape[1],3])
mask[np.where(self.masks[:,:,self.idx])] = (1,0,0)
self.img_ROI.setImage(mask)
elif modifiers == (QtCore.Qt.ShiftModifier | QtCore.Qt.AltModifier):
self.masks[:,:,self.idx][int(np.floor(x_pos)),int(np.floor(y_pos))] = 0
self.masks[:,:,self.idx][int(np.ceil(x_pos)),int(np.ceil(y_pos))] = 0
mask = np.zeros([self.masks.shape[0],self.masks.shape[1],3])
mask[np.where(self.masks[:,:,self.idx])] = (1,0,0)
self.img_ROI.setImage(mask)
self.prevT = time.time()
nROIs = areaFile.attrs['ROI_patches'].shape[2]
#if restart==True:
#areaFile.attrs['ROI_masks'] = np.zeros(areaFile.attrs['ROI_patches'].shape)
if 'ROI_masks' not in (areaFile.attrs.iterkeys()):
print 'no masks exist, creating empty ones'
areaFile.attrs['ROI_masks'] = np.zeros(areaFile.attrs['ROI_patches'].shape)
#roi_masks = cp.deepcopy(np.array(areaFile.attrs['ROI_masks'].astype('int')))
app = QtGui.QApplication([])
win = Visualizator(areaFile.attrs['ROI_patches'],areaFile.attrs['ROI_masks'],areaFile)
#app.aboutToQuit.connect(app.deleteLater)
#app.exec_()
print sys.exit(app.exec_())
return app
if __name__=="__main__":
hdfPath = sys.argv[1]
with h5py.File(hdfPath, 'a', libver='latest') as HDF_File:
try:
print HDF_File.filename
print 'Sessions:'
sessions = list((i for i in HDF_File.iterkeys()))
for idx,f in enumerate(sessions):
print idx, f
session = int(raw_input('Select Session Nr:'))
sessions = list((i for i in HDF_File.iterkeys()))
#print sessions
dataType = 0
if 'registered_data' in HDF_File[sessions[session]].iterkeys():
if len(HDF_File[sessions[session]]['registered_data'])>0:
print 'Using registered Data'
dataType = 'registered_data'
else:
print '\n!!!!!!!!!!WARNING!!!!!!!!!!!!\nUsing Raw Data\n!!!!!!!!!!WARNING!!!!!!!!!!!!'
dataType = 'raw_data'
else:
print '\n!!!!!!!!!!WARNING!!!!!!!!!!!!\nUsing Raw Data\n!!!!!!!!!!WARNING!!!!!!!!!!!!'
dataType = 'raw_data'
areas = list((i for i in HDF_File[sessions[int(session)]][dataType].iterkeys()))
for idx,f in enumerate(areas):
print idx, f
areaID = int(raw_input('Select Area Nr:'))
areaFile = HDF_File[sessions[session]][dataType][areas[areaID]]
#print '...into function'
#areaFile.attrs['ROI_patches']
app = MASK_DRAWER_GUI(areaFile,restart=False)
#print sys.exit(app.exec_())
except:
#raise
raise
print 'HDF_File Closed, PyQt Closed'