-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviewer.py
executable file
·782 lines (676 loc) · 30.4 KB
/
viewer.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# -*- coding: utf-8 -*-
"""
Simple example of loading UI template created with Qt Designer.
This example uses uic.loadUiType to parse and load the ui at runtime. It is also
possible to pre-compile the .ui file using pyuic (see VideoSpeedTest and
ScatterPlotSpeedTest examples; these .ui files have been compiled with the
tools/rebuildUi.py script).
"""
#import initExample ## Add path to library (just for examples; you do not need this)
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui#QStringList,QString
import numpy as np
import os
pg.mkQApp()
## Define main window class from template
path = os.path.dirname(os.path.abspath(__file__))
uiFile = os.path.join(path, 'viewer.ui')
WindowTemplate, TemplateBaseClass = pg.Qt.loadUiType(uiFile)
import tifffile
import numpy as np
#import db_access as dba
import muscle_model as mm
#fly_db = dba.get_db()
default_rframe_data = {'a1': np.array([ 51.5848967 , -5.93928407]),
'a2': np.array([ -0.09151179, 88.42505672]),
'p': np.array([ 26.66908747, 34.43488385])}
#stacked_muscles = tifffile.TiffFile('stacked_muscles.tiff')
#overlay = np.transpose(stacked_muscles.asarray(),(1,0,2))[:,::-1].astype(np.float32)
def fit_to_model(imchunk,model, mode = 'pinv',fit_pix_mask = None,baseline = None):
import numpy as np
#im_array = (imchunk-baseline)#/baseline
if not(baseline is None):
im_array = imchunk-baseline#/baseline
else:
im_array = imchunk
imshape = np.shape(im_array[0])
im_array = im_array.reshape((-1,imshape[0]*imshape[1]))
if mode == 'nnls':
fits = np.empty((np.shape(model)[0],np.shape(im_array)[0]))
for i,im2 in enumerate(im_array):
im = im2.copy()
im[~np.isfinite(im)] = 0
from scipy.optimize import nnls
if not(fit_pix_mask is None):
fits[:,i] = nnls(model[:,fit_pix_mask].T,im[fit_pix_mask])[0]
else:
fits[:,i] = nnls(model.T,im)[0]
else:
im = im_array
print np.shape(im_array)
from numpy.linalg import pinv
if not(fit_pix_mask is None):
fits = np.dot(pinv(model[:,fit_pix_mask]).T,im[:,fit_pix_mask].T)
else:
fits = np.dot(pinv(model).T,im)
return fits
#extract the data give the fly_path and 'line_name'
def extract_signals(fly):
print self.thorax_view.model
return
import muscle_model as mm
import numpy as np
import h5py
import cv2
model_type = 'volumetric'
#model_type = 'masks'
#load the reference frame of the cofocal data and that of the imaged fly
confocal_model = mm.GeometricModel(filepath = 'model_data.cpkl')
#confocal_view = mm.ModelViewMPL(confocal_model)
pkname = 'basis_fits.cpkl'
fly_frame = mm.Frame();fly_frame.load(pkname)
#get the transformation matrix A and compose with a scaling using a scaling of s
#to construct a transformation for homogenious vectors
s = 1 #scale
A = fly_frame.get_transform(confocal_model.frame)
Ap = np.dot([[s,0.0,0],[0,s,0],[0,0,1]],A)
#parse the GMR genotype to get the line name
line_name = parse_GMR_genotype(fly.get_genotype())['gal4']
#get the list of muscles for a given line
muscles = get_muscle_list(line_name)
muscles = [m for m in muscles if not('DVM' in m) and not('DLM' in m) and not('ps' in m)]
#get a reference to the image data
#fly_record = h5py.File(fly.fly_path + 'fly_record.hdf5','r')
#exp_record = fly_record['experiments'].values()[0]
imgs = exp_record['tiff_data']['images']
#the output shape of the warped model
output_shape = np.shape(imgs[0])
if model_type == 'masks':
#get the mask of all the muscles for fit
masks = confocal_model.get_masks(fly_frame,np.shape(imgs[0]))
#create the model using only the muscles that express in a given line
model = np.vstack([masks[mask_key].T.ravel().astype(float) for mask_key in muscles])
#construct a mask do reduce the projection to just the data within the model
fit_pix_mask = np.sum(model,axis=0) > 0
if model_type == 'volumetric':
model_data = h5py.File(gd.muscle_anatomy_dir + 'flatened_model.hdf5','r')
model_muscles = [np.array(model_data[muscle]) for muscle in muscles]
output_shapes = [output_shape for muscle in muscles]
transforms = [Ap[:-1,:] for muscle in muscles]
model = v.map(cv2.warpAffine,model_muscles,transforms,output_shapes)
model = np.vstack([muscle.T.ravel() for muscle in model])
#model = np.vstack([cv2.warpAffine(np.array(model_data[muscle]), \
# Ap[:-1,:],output_shape).T.ravel() \
# for muscle in muscles])
fit_pix_mask = np.ones_like(model[0]) > 0
f = open(fly.fly_path + 'epoch_data.cpkl')
import cPickle
baseline_range = cPickle.load(f)['baseline_F']
f.close()
baseln = np.mean(imgs[baseline_range],axis = 0)
chnk_sz = 2000
num_samps = np.shape(imgs)[0]
chunks = [slice(x,x+chnk_sz if x+chnk_sz < num_samps else num_samps) for x in range(0,num_samps,chnk_sz)]
img_chunks = [np.array(imgs[chunk]) for chunk in chunks]
models = [model for chunk in chunks]
modes = ['nnls' for chunk in chunks]
fit_pix_masks = [fit_pix_mask for chunk in chunks]
baselines = [baseln for chunk in chunks]
fits = v.map(fit_to_model,img_chunks,models,modes,fit_pix_masks,baselines)
#fit = fit_to_model(imchunk,model,mode = 'nnls',fit_pix_mask = fit_pix_mask)
return np.hstack(fits),muscles
class ModelView(object):
def __init__(self,model):
import copy
self.model = model
self.plot_frame = copy.copy(model.frame)
self.curves = None
self.element_list = []
#self.element_list = ['b2', 'b1', 'ttm', 'b3', 'pr', 'nm',
# 'i1', 'iii24', 'A', 'C', 'B', 'E', 'D',
# 'G', 'F', 'I', 'H', 'K', 'i2', 'J', 'tpd',
# 'iii1', 'iii3', 'hg2', 'hg3', 'hg1', 'tpv',
# 'DVM1', 'hg4', 'DVM3', 'DVM2']
#self.element_list = ['b2', 'b1', 'ttm', 'b3', 'pr', 'nm',
# 'i1', 'i2','iii24', 'tpd', 'iii1', 'iii3', 'hg2',
# 'hg3', 'hg1', 'tpv',]
def plot(self,basis,plotobject):
if self.curves:
for pitem in self.curves:
plotobject.removeItem(pitem)
lines = self.model.coords_from_frame(basis)
self.curves = list()
for element_name, line in lines.items():
if element_name in self.element_list:
self.curves.append(plotobject.plot(line[0,:],line[1,:]))
def update_basis(self,basis):
lines = self.model.coords_from_frame(basis)
lines = [l for k,l in lines.items() if k in self.element_list]
if self.curves:
for curve,line in zip(self.curves,lines):#lines.values()):
curve.setData(line[0,:],line[1,:])
def basis_changed(self,roi):
pnts = roi.saveState()['points']
p = np.array(pnts[1])
a1 = np.array(pnts[0])-p
a2 = np.array(pnts[2])-p
self.plot_frame['p'] = p
self.plot_frame['a1'] = a1
self.plot_frame['a2'] = a2
self.update_basis(self.plot_frame)
class RefrenceFrameROI(pg.ROI):
def __init__(self, basis, closed=False, pos=None, **args):
pos = [0,0]
self.closed = closed
self.segments = []
pg.ROI.__init__(self, pos, **args)
self.addFreeHandle((basis['p'][0]+basis['a1'][0],basis['p'][1]+basis['a1'][1]))
self.addFreeHandle((basis['p'][0],basis['p'][1]))
self.addFreeHandle((basis['p'][0]+basis['a2'][0],basis['p'][1]+basis['a2'][1]))
for i in range(0, len(self.handles)-1):
self.addSegment(self.handles[i]['item'], self.handles[i+1]['item'])
def addSegment(self, h1, h2, index=None):
seg = pg.LineSegmentROI(handles=(h1, h2), pen=self.pen, parent=self, movable=False)
if index is None:
self.segments.append(seg)
else:
self.segments.insert(index, seg)
#seg.sigClicked.connect(self.segmentClicked)
#seg.setAcceptedMouseButtons(QtCore.Qt.LeftButton)
seg.setZValue(self.zValue()+1)
for h in seg.handles:
h['item'].setDeletable(False)
def saveState(self):
state = pg.ROI.saveState(self)
state['closed'] = self.closed
state['points'] = [tuple(h.pos()) for h in self.getHandles()]
return state
def setState(self,state):
pg.ROI.setState(self,state,update = False)
#state = pg.ROI.saveState(self)
for h,p in zip(self.getHandles(),state['points']):
self.movePoint(h,p)
self.stateChanged(finish=True)
return state
class MainWindow(TemplateBaseClass):
def __init__(self):
TemplateBaseClass.__init__(self)
self.setWindowTitle('muscle imaging browser')
# Create the main window
self.ui = WindowTemplate()
#initialize the items created in designer
self.ui.setupUi(self)
#frame view
self.plt = pg.PlotItem()
self.ui.frameView.setCentralItem(self.plt)
self.frameView = pg.ImageItem()
self.plt.addItem(self.frameView)
#transform image
#self.transformPlt = pg.PlotItem()
#self.ui.transformImage.setCentralItem(self.transformPlt)
#self.transformImage = pg.ImageItem()
#self.transformPlt.addItem(self.transformImage)
#gama plot
self.gammaPlt = pg.PlotItem()
self.ui.gammaPlot.setCentralItem(self.gammaPlt)
self.ui.gammaSlider.valueChanged.connect(self.gammaChange)
#default gama
self.gammaf = lambda x: x**1
self.gammax = np.linspace(0,2,100)
self.gammaCurve = self.gammaPlt.plot(self.gammax,self.gammaf(self.gammax))
#timeSeries
self.timeSeriesPlt = pg.PlotItem()
self.ui.timeSeriesPlt.setCentralItem(self.timeSeriesPlt)
self.tserTrace = self.timeSeriesPlt.plot(np.ones(1000))
self.tpointLine = pg.InfiniteLine(pos = 0,movable = True)
self.tpointLine.sigPositionChanged.connect(self.tpointLineMoved)
self.timeSeriesPlt.addItem(self.tpointLine)
#load frames button
self.ui.loadFrames.clicked.connect(self.loadFrames)
#save data button
self.ui.saveFit.clicked.connect(self.saveFit)
self.ui.loadFit.clicked.connect(self.loadFit)
##scroll bar
self.ui.frameScrollBar.valueChanged.connect(self.frameScrollBar_valueChanged)
# Contrast/color control
self.hist = pg.HistogramLUTItem()
self.hist.setImageItem(self.frameView)
self.ui.frameHist.setCentralItem(self.hist)
self.componentsModel = QtGui.QStandardItemModel(self.ui.componentsView)
self.ui.componentsView.setModel(self.componentsModel)
self.componentsModel.itemChanged.connect(self.componentsChanged)
#modelSelector
self.loadedComponents = list()
self.updateModelList()
self.ui.modelselectBox.currentIndexChanged.connect(self.modelSelected)
self.modelSelected(0)
#profileSelector
self.updateProfileList()
self.ui.profileselectBox.currentIndexChanged.connect(self.profileSelected)
self.profileSelected(0)
self.ui.saveProfile.clicked.connect(self.saveProfile)
#load outlines
self.loadLines()
self.current_frame = 0
self.show()
#self.ui.commentBox
self.ui.frameNumber.setText(str(self.current_frame))
self.ui.frameNumber.textEdited.connect(self.frameInput)
#addEpoch
self.epochPlots = dict()
self.epoch_dict = dict()
self.ui.newEpoch.clicked.connect(self.newEpoch)
self.ui.saveEpoch.clicked.connect(self.saveEpoch)
self.ui.epochStart.textEdited.connect(self.updateEpochFromText)
self.ui.epochEnd.textEdited.connect(self.updateEpochFromText)
#muscle demixing
self.ui.applyDemixing.clicked.connect(self.extract_signals)
self.ui.subtractBackground.stateChanged.connect(self.subtractBackgroundChecked)
self.subtract_background = self.ui.subtractBackground.isChecked()
def subtractBackgroundChecked(self,i):
self.subtract_background = self.ui.subtractBackground.isChecked()
print self.subtract_background
def profileSelected(self,i):
import cPickle
profile = self.ui.profileselectBox.currentText()
with open('models/%s/profiles/%s'%(self.cur_model,profile),'rb') as f:
profile_data = cPickle.load(f)
for component in self.loadedComponents:
if component['name'] in profile_data['selected_components']:
component['checkbox'].setCheckState(True)
else:
component['checkbox'].setCheckState(False)
self.ui.profileName.setText(profile)
print profile_data
def updateProfileList(self):
import os
profile_list = os.listdir('models/%s/profiles'%(self.cur_model))
if len(profile_list) == 0:
print 'creating default profile'
import cPickle
with open('models/%s/profiles/default.cpkl'%(self.cur_model),'wb') as f:
cPickle.dump({'selected_components':[]},f)
self.updateProfileList()
else:
for profile in profile_list:
self.ui.profileselectBox.addItem(profile)
index = self.ui.profileselectBox.findText('default.cpkl', QtCore.Qt.MatchFixedString)
if index >= 0:
self.ui.profileselectBox.setCurrentIndex(index)
def saveProfile(self):
profile_dir = 'models/%s/profiles/'%(self.cur_model)
name = str(self.ui.profileName.text())
print profile_dir + name
f = open(profile_dir + name,'wb')
import cPickle
cPickle.dump({'selected_components':self.thorax_view.element_list},f)
#self.updateProfileList()
def componentsChanged(self):
# If the changed item is not checked, don't bother checking others
#if not item.checkState():
# return
# Loop through the items until you get None, which
# means you've passed the end of the list
i = 0
item_list = list()
while self.componentsModel.item(i):
if self.componentsModel.item(i).checkState():
item_list.append(i)
#return
i += 1
#skeys = self.signalshelf.keys()
self.checked_signals = [self.loadedComponents[i]['name'] for i in item_list]
print self.checked_signals
self.thorax_view.element_list = self.checked_signals
self.thorax_view.plot(self.thorax_view.plot_frame,self.plt)
self.roi.stateChanged()
#self.thorax_view.plot()
#self.update_tser_plot()
def modelSelected(self,i):
import cPickle
self.cur_model = os.listdir('models')[i]
#print self.cur_modelcomponentsModel
with open('models/%s/outlines.cpkl'%(self.cur_model),'rb') as f:
self.outlines = cPickle.load(f)
for key in self.outlines.keys():
print key
item = QtGui.QStandardItem(key)
#check = 1 if np.random.randint(0, 1) == 1 else 0
item.setCheckable(True)
item.setCheckState(False)
self.loadedComponents.append({'checkbox':item,'name':key})
self.componentsModel.appendRow(item)
#self.color_dict[key] = 'r'
def roiClicked(self,item):
#print item.mname
print 'here'
color = pg.QtGui.QColorDialog.getColor()
#self.color_dict[item.mname] = color
#self.update_tser_plot()
def updateModelList(self):
import os
for mstr in os.listdir('models'):
self.ui.modelselectBox.addItem(mstr)
def newEpoch(self):
name = str(self.ui.epochName.text())
print name
if (not(name in self.epoch_dict.keys()) and not(name == '')):
epoch_range = [self.current_frame,self.current_frame + 100]
self.epoch_dict[name] = epoch_range
self.plotEpoch(name)
ep_plot = self.epochPlots[name]
sta,stp = ep_plot.getRegion()
self.ui.epochStart.setText(str(int(sta)))
self.ui.epochEnd.setText(str(int(stp)))
def clearEpochs(self):
for k in self.epoch_dict.keys():
self.timeSeriesPlt.removeItem(self.epochPlots[k])
self.epochPlots.pop(k)
self.epoch_dict.pop(k)
def plotEpoch(self,k):
ep = pg.LinearRegionItem(values= self.epoch_dict[k])
ep.epoch_name = k
ep.sigRegionChanged.connect(self.updateEpochPlot)
self.epochPlots[k] = ep
self.timeSeriesPlt.addItem(ep)
self.tpointLine.setZValue(ep.zValue()+1)
def updateEpochPlot(self,ep):
self.ui.epochName.setText(ep.epoch_name)
self.updateCurrentEpochState()
def updateEpochFromText(self):
k = str(self.ui.epochName.text())
ep_plot = self.epochPlots[k]
sta = int(self.ui.epochStart.text())
stp = int(self.ui.epochEnd.text())
ep_plot.setRegion((sta,stp))
self.epoch_dict[k] = [sta,stp]
def updateCurrentEpochState(self):
k = str(self.ui.epochName.text())
ep = self.epoch_dict[k]
ep_plot = self.epochPlots[k]
sta,stp = ep_plot.getRegion()
self.ui.epochStart.setText(str(int(sta)))
self.ui.epochEnd.setText(str(int(stp)))
self.epoch_dict[k] = [int(sta),int(stp)]
def saveEpoch(self):
flydir = '%s%s/'%(dba.root_dir,self.current_fly)
f = open(flydir + 'epoch_data.cpkl','wb')
import cPickle
cPickle.dump(self.epoch_dict,f)
print self.epoch_dict
def frameInput(self,value):
self.current_frame = int(value)
self.showFrame()
def tpointLineMoved(self):
self.current_frame = int(self.tpointLine.value())
self.showFrame()
def gammaChange(self,value):
gamma = value/50.0
self.gammaf = lambda x: x**gamma
#print gamma
self.gammaCurve.setData(self.gammax,self.gammaf(self.gammax))
self.showFrame()
def loadfileTree(self):
self.ui.fileTree.setColumnCount(1)
items = []
#for key,fly in zip(fly_db.keys(),fly_db.values()):
for key,fly in sorted(fly_db.items()):#zip(fly_db.keys(),fly_db.values()):
#print key
try:
exp1 = fly['experiments'].values()[0]
exptype = fly['experiments'].keys()[0]
if 'tiff_data' in exp1.keys():
#item_list.append('fly%s'%key)
item = QtGui.QTreeWidgetItem(None,['Fly%04d'%int(key)])
for img_key in ['images','refstack']:
if img_key in exp1['tiff_data'].keys():
#data_ref = exp1['tiff_data'][img_key]
child = QtGui.QTreeWidgetItem(None,[img_key])
child.setData(0,QtCore.Qt.UserRole,key)
item.insertChild(0,child)
items.append(item)
#print (img_key,np.shape(exp1['tiff_data'][img_key]))
else:
pass
else:
print exp1.keys()
except KeyError:
pass
self.ui.fileTree.insertTopLevelItems(0,items)
def loadLines(self):
import cPickle
#f = open('model_data.cpkl','rb')
###f = open('/media/flyranch/ICRA_2015/model_data.cpkl','rb')
model_data = self.outlines
#f.close()
########################
#model_keys = []
e1 = model_data['e1']
e2 = model_data['e2']
muscle_dict = dict()
for key in model_data.keys():
if not(key in ['e1','e2']):
muscle_dict[key] = model_data[key]
frame = mm.Frame()
frame['a2'] = e1[1]-e2[0]
frame['a1'] = e2[1]-e2[0]
frame['p'] = e2[0]
thorax = mm.GeometricModel(muscle_dict,frame)
self.thorax_view = ModelView(thorax)
self.roi = RefrenceFrameROI(thorax.frame)
self.roi.sigRegionChanged.connect(self.thorax_view.basis_changed)
#self.roi.sigRegionChanged.connect(self.affineWarp)
self.plt.disableAutoRange('xy')
state = self.roi.getState()
rf = default_rframe_data
pnts = [(rf['p'][0]+rf['a1'][0],rf['p'][1]+rf['a1'][1]),
(rf['p'][0],rf['p'][1]),
(rf['p'][0]+rf['a2'][0],rf['p'][1]+rf['a2'][1])]
state['points'] = pnts
self.roi.setState(state)
self.roi.stateChanged()
self.plt.addItem(self.roi)
self.thorax_view.plot(self.thorax_view.plot_frame,self.plt)
def loadFrames(self):
#selection = self.ui.fileTree.selectedItems()[0]
#self.current_fly = selection.parent().text(0)
#fnum = int(self.current_fly.split('Fly')[1])
#print fnum
#fnum = selection.data(0,QtCore.Qt.UserRole)
#print 'here'
#print int(fnum)
#self.images = np.array(fly_db[fnum]['experiments'].values()[0]['tiff_data']['images'])
#tfile = tifffile.TiffFile('image_stack.tif')
#self.images = tfile.asarray()
self.CurrentTiffFileName = str(QtGui.QFileDialog.getOpenFileName(self,
'Dialog Title',
'',
selectedFilter='*.tif'))
tfile = tifffile.TiffFile(self.CurrentTiffFileName)
#self.images = np.array(fly_db[fnum]['experiments'].values()[0]['tiff_data']['images'])
import os
self.CurrentDirPath = os.path.split(self.CurrentTiffFileName)[0]
print self.CurrentDirPath
tfile = tifffile.TiffFile(self.CurrentTiffFileName)
self.images = tfile.asarray()
#self.maximg = np.max(self.images,axis = 0)
#self.transform_img = self.affineWarp(self.maximg)
#self.current_fly = selection.parent().text(0)
#print self.current_fly
#flydir = '%s%s/'%(dba.root_dir,self.current_fly)
import cPickle
#with open('tseries_data.cpkl','rb') as f:
# tser_data = cPickle.load(f)
#tser_data = np.array(fly_db[fnum]['experiments'].values()[0]['tiff_data']['axon_framebase']['wb_frequency'])
#self.tserTrace.setData(tser_data)
try:
f = open('basis_fits.cpkl','rb')
import cPickle
basis = cPickle.load(f)
state = self.roi.getState()
pnts = [(basis['p'][0]+basis['a1'][0],basis['p'][1]+basis['a1'][1]),
(basis['p'][0],basis['p'][1]),
(basis['p'][0]+basis['a2'][0],basis['p'][1]+basis['a2'][1])]
state['points'] = pnts
self.roi.setState(state)
self.roi.stateChanged()
self.ui.commentBox.setPlainText(basis['commentBox'])
except IOError:
print 'no file'
self.ui.commentBox.setPlainText('')
self.clearEpochs()
try:
f = open('epoch_data.cpkl','rb')
import cPickle
self.epoch_dict = cPickle.load(f)
for k in self.epoch_dict.keys():
self.plotEpoch(k)
self.ui.epochName.setText(self.epoch_dict.keys()[0])
self.updateCurrentEpochState()
except IOError:
print 'no epoch file'
self.ui.epochName.setText('')
self.ui.epochStart.setText('')
self.ui.epochEnd.setText('')
#self.frameView.setImage(self.images[0,:,:])
self.current_frame = 0
self.showFrame()
#self.transformImage.setImage(self.transform_img.astype(np.float32))
self.ui.frameScrollBar.setMaximum(np.shape(self.images)[0])
self.plt.autoRange()
#set transformImage
def showFrame(self):
img = self.gammaf(self.images[self.current_frame,:,:].astype(np.float32))
self.frameView.setImage(img.astype(np.float32))
self.ui.frameNumber.setText(str(self.current_frame))
self.ui.frameScrollBar.setValue(self.current_frame)
self.tpointLine.setValue(self.current_frame)
def affineWarp(self,roi):
src_f = self.thorax_view.plot_basis
dst_f = self.thorax_view.model.basis
dst_p0 = dst_f['a1'] + dst_f['p']
dst_p1 = dst_f['p']
dst_p2 = dst_f['a2'] + dst_f['p']
src_p0 = src_f['a1'] + src_f['p']
src_p1 = src_f['p']
src_p2 = src_f['a2'] + src_f['p']
#import cv2
#A = cv2.getAffineTransform(np.float32([src_p0,src_p1,src_p2]),np.float32([dst_p0,dst_p1,dst_p2]))
#output_shape = (1024, 1024)
#self.transform_img = cv2.warpAffine(self.maximg.T,A,output_shape).T[:,::-1].astype(np.float32)
#display_img = np.dstack((self.transform_img ,self.transform_img ,self.transform_img ))
#display_img += overlay*0.2
#self.transformImage.setImage(display_img)
def frameScrollBar_valueChanged(self,value):
#self.frameView.setImage(self.images[value,:,:])
self.current_frame = value
self.showFrame()
def saveFit(self):
import cPickle
savedata = dict(self.thorax_view.plot_basis)
comment_text = self.ui.commentBox.toPlainText()
savedata['commentBox'] = comment_text
flydir = '%s%s/'%(dba.root_dir,self.current_fly)
f = open(flydir+'basis_fits.cpkl','wb')
cPickle.dump(savedata,f)
f.close()
def loadFit(self):
pass
#print self.ui.fileTree.selectedItems()[0].data(0,QtCore.Qt.UserRole).toPyObject()
def extract_signals(self):
#import muscle_model as mm
print self.thorax_view.model.frame
#return
import numpy as np
import h5py
import cv2
model_type = 'volumetric'
#model_type = 'masks'
#load the reference frame of the cofocal data and that of the imaged fly
#confocal_model = mm.GeometricModel(filepath = 'anatomy_outlines.cpkl')
confocal_frame = self.thorax_view.model.frame
fly_frame = self.thorax_view.plot_frame
#confocal_view = mm.ModelViewMPL(confocal_model)
#pkname = os.path.join(self.CurrentDirPath,'frame_fits.cpkl')
#fly_frame = mm.Frame();fly_frame.load(pkname)
#get the transformation matrix A and compose with a scaling of s
#to construct a transformation for homogenious vectors
s = 1 #scale
A = fly_frame.get_transform(confocal_frame)
Ap = np.dot([[s,0.0,0],[0,s,0],[0,0,1]],A)
#parse the GMR genotype to get the line name
#line_name = parse_GMR_genotype(fly.get_genotype())['gal4']
#get the list of muscles for a given line
#muscles = get_muscle_list()
#muscles = [m for m in muscles if not('DVM' in m) and not('DLM' in m) and not('ps' in m)]
muscles = self.thorax_view.element_list
#get a reference to the image data
#fly_record = h5py.File(fly.fly_path + 'fly_record.hdf5','r')
#exp_record = fly_record['experiments'].values()[0]
#tfile = tifffile.TiffFile('image_stack.tif')
#imgs = tfile.asarray()
imgs = self.images
#the output shape of the warped model
output_shape = np.shape(imgs[0])
if model_type == 'masks':
#get the mask of all the muscles for fit
masks = confocal_model.get_masks(fly_frame,np.shape(imgs[0]))
#create the model using only the muscles that express in a given line
model = np.vstack([masks[mask_key].T.ravel().astype(float) for mask_key in muscles])
#construct a mask do reduce the projection to just the data within the model
fit_pix_mask = np.sum(model,axis=0) > 0
if model_type == 'volumetric':
model_data = h5py.File('models/%s/components/flatened_components_10x_nikon.hdf5'%(self.cur_model),'r')
#muscles = model_data.keys()
model_muscles = [np.array(model_data[muscle]) for muscle in muscles]
output_shapes = [output_shape for muscle in muscles]
transforms = [Ap[:-1,:] for muscle in muscles]
model = map(cv2.warpAffine,model_muscles,transforms,output_shapes)
model.append(np.ones_like(model[0]))
muscles.append('bkg')
model = np.vstack([muscle.T.ravel() for muscle in model])
fit_pix_mask = np.ones_like(model[0]) > 0
print muscles
print np.shape(model)
#return
#fname = os.path.join(self.CurrentDirPath,'epoch_data.cpkl')
#with open(fname,'rb') as f:
# import cPickle
# baseline_range = cPickle.load(f)['baseline_F']
if self.subtract_background:
baseline_range = self.epoch_dict['background']
baseln = np.mean(imgs[baseline_range],axis = 0)
else:
baseln = None
print 'here'
chnk_sz = 100
num_samps = np.shape(imgs)[0]
print num_samps
chunks = [slice(x,x+chnk_sz if x+chnk_sz < num_samps else num_samps) for x in range(0,num_samps,chnk_sz)]
img_chunks = [np.array(imgs[chunk]) for chunk in chunks]
models = [model for chunk in chunks]
modes = ['nnls' for chunk in chunks]
fit_pix_masks = [fit_pix_mask for chunk in chunks]
baselines = [baseln for chunk in chunks]
fits = map(fit_to_model,img_chunks,models,modes,fit_pix_masks,baselines)
#fit = fit_to_model(imchunk,model,mode = 'nnls',fit_pix_mask = fit_pix_mask)
fname = os.path.join(self.CurrentDirPath,'model_fits.cpkl')
savedict = dict()
import cPickle
with open(fname,'wb') as f:
[savedict.update({str(mname):sig}) for sig,mname in zip(np.hstack(fits),muscles)]
cPickle.dump(savedict,f)
#import shelve
#fname = os.path.join(self.CurrentDirPath,'model_fits.shelve')
#self.signalshelf = shelve.open(fname)
print np.shape(np.hstack(fits))
print muscles
#[self.signalshelf.update({str(mname):sig}) for sig,mname in zip(np.hstack(fits),muscles)]
# self.add_model_signals()
win = MainWindow()
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
#fly_db.close()