-
Notifications
You must be signed in to change notification settings - Fork 2
/
viewerUtilities.py
764 lines (623 loc) · 25.5 KB
/
viewerUtilities.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
import argparse
import os
import vtk.util.numpy_support
import nibabel as nib
import liteViewer
import math
from __main__ import qt, ctk, vtk, slicer
#
# Functions
#
def InitialSlicerSetup():
# Hides the module panel so that viewer is of maximum size
# if sliceNodeList is not defined then it is the single viewer which does not have the module panel
modulePanel = slicer.util.findChildren(name='PanelDockWidget')
if modulePanel:
modulePanel[0].hide()
# remove other viewers
layoutManager = slicer.app.layoutManager()
for node in slicer.util.getNodes('vtkMRMLSliceNode*').values():
slicer.mrmlScene.RemoveNode(node)
def ReadImageSlicer(FileName):
mrml = slicer.vtkMRMLScene()
vl = slicer.vtkSlicerVolumesLogic()
vl.SetAndObserveMRMLScene(mrml)
n = vl.AddArchetypeVolume(FileName,'CTC')
i = n.GetImageData()
return i
# Implementation based on https://github.com/pieper/CaseHub/blob/5aaeb51d4b7281b39f116b180123227331ce791a/BenchtopNeuro/BenchtopNeuro.py#L306-L348
# interesting to look at
# https://github.com/loli/medpy/blob/master/medpy/io/load.py
# no flipping needed - checked it
# reads in image data from second frame (as we assume 1st frame is already read)
def nibVtkConversionDirect(loadedFile,scalarType):
# startT=time.time()
py4DImg=loadedFile.get_data()
tDim=py4DImg.shape[3]
shape3D=py4DImg.shape[0:3]
# same as dim=vNode.GetImageData().GetDimensions()
sDim=shape3D[2]
yDim=shape3D[1]
xDim=shape3D[0]
imgList=[]
for index in xrange(1,tDim):
vtkImg=vtk.vtkImageData()
vtkImg.SetDimensions(shape3D)
vtkImg.AllocateScalars(scalarType,1)
# Maybe this is a better way
# import SimpleITK as sitk
# img=sitk.GetImageFromArray(py4DImg[:,:,:,index])
# problem with orientation so currently
vtkImgArray = vtk.util.numpy_support.vtk_to_numpy(vtkImg.GetPointData().GetScalars()).reshape(shape3D[2],shape3D[1],shape3D[0])
#http://nipy.org/nibabel/coordinate_systems.html
# Problem - different coordinate system between nibabel and vkt
for slice in xrange(sDim):
vtkImgArray[slice,:,:] = py4DImg[:,:,slice,index].T
# py3DImg = numpy.fliplr(py4DImg[:,:,slice,index])
# This does not work as
# Still Slow
# http://www.vtk.org/Wiki/VTK/Examples/Cxx/ImageData/IterateImageData
#for slice in xrange(0,sDim):
# for y in xrange(0,yDim):
# for x in xrange(0,xDim):
# ptr= vtk.util.numpy_support.vtk_to_numpy(vtkImg.GetScalarPointer(x,y,slice))
# print ptr
# ptr[0]=py4DImg[x,y,slice,index]
imgList.append(vtkImg)
# endT=time.time()
# print "Took: %f" % (endT - startT)
return imgList
def load4DVolume(listFileNames,labelFlag):
# Needs to be done that way to work for single file name and multi one
firstFileName=listFileNames
if isinstance(firstFileName, basestring) :
multiFileFlag=False
else :
firstFileName=listFileNames[0]
if len(listFileNames) > 1:
multiFileFlag=True
else:
multiFileFlag=False
# Load first volume
vNode = liteViewer.loadVolume(firstFileName,labelFlag)
if not vNode:
liteViewer.errorPrint(0,"Could not load %s!" % firstFileName)
return (None,None)
iVtkList=[vNode.GetImageData()]
#
# Load Multi File Volume
#
if multiFileFlag:
for FILE in listFileNames[1:] :
img=ReadImageSlicer(FILE)
if img:
iVtkList.append(img)
else :
return (None,None)
return (vNode,iVtkList)
#
# Load Single File Volume
#
fileExt=os.path.splitext(firstFileName)[1]
if fileExt == ".gz" :
fileExt=os.path.splitext(firstFileName)[1]
# nibabel 1.3 cannot read nrrd file - so assume it is 3D right now
if fileExt == ".nrrd":
print "Warning: NRRD files are assumed to be 3D"
return (vNode,iVtkList)
loadedFile=nib.load(firstFileName)
# Nothing to do as it is a 3D Volume - so already read by slicer function
if len(loadedFile.shape) == 3 :
return (vNode,iVtkList)
# Sadly enough this was the best solution I found so far - write to file and read in again
# imgList=nibVtkConversionWrite(loadedFile)
imgList=nibVtkConversionDirect(loadedFile,vNode.GetImageData().GetScalarType())
iVtkList.extend(imgList)
return (vNode,iVtkList)
def loadVolumes(fileList,labelFlag,fourDFlag):
imgList=[]
volNodeList=[]
missingList=[]
if not fileList:
return (volNodeList,imgList,missingList)
if isinstance(fileList, basestring) :
fileList = [ fileList ]
elif not fourDFlag:
tmpList=[]
for file in fileList :
if isinstance(file, basestring) :
tmpList.append(file)
else:
tmpList.extend(file)
fileList=tmpList
# [item for sublist in fileList for item in sublist]
numVolumes=len(fileList)
# For multiple input
for index in xrange(numVolumes):
(volNode,iList) = load4DVolume(fileList[index],labelFlag)
if volNode:
volNodeList.append(volNode)
imgList.append(iList)
else :
missingList.append(fileList[index])
return (volNodeList,imgList,missingList)
class CtrlPanelWidget:
def __init__(self,sliceNodes,sliceWidget,fgNodeList,fgNodeImgList,bgNodeList,bgNodeImgList,lmNodeList,lmNodeImgList,orientation,all_orientations,fg_Color_Table,fg_Lower_Threshold):
self.sliceNodeList = sliceNodes
self.singleViewerWidget = sliceWidget
self.nodeType = ('FG', 'BG', 'LM')
self.nodeList=[fgNodeList,bgNodeList,lmNodeList]
self.nodeImgList=[fgNodeImgList,bgNodeImgList,lmNodeImgList]
self.ctrlWidget = None
self.selectedOrientation = orientation
self.allOrientationsFlag=all_orientations
self.fgColorTable = fg_Color_Table
self.fgLowerThreshold = fg_Lower_Threshold
self.layoutManager = slicer.app.layoutManager()
self.orientations = ('Axial', 'Sagittal', 'Coronal')
self.ctrlFrameSlider = None
self.ctrlLevelSlider = None
self.ctrlWindowSlider = None
self.styleObserverTags = []
def setup(self,wName,frameActiveFlag,parent):
if self.ctrlWidget :
return
self.SetLinkViewers(1)
self.numFrames=len(self.nodeImgList[0][0])
if parent:
self.ctrlWidget = parent
else :
# Create seperate window
self.ctrlWidget=slicer.qMRMLWidget()
self.ctrlWidget.setMRMLScene(slicer.mrmlScene)
self.ctrlWidget.setLayout(qt.QFormLayout())
self.ctrlWidget.setWindowTitle(wName)
ctrlLayout = self.ctrlWidget.layout()
# Create Slider Panel
self.sliderPanel=qt.QWidget()
self.sliderPanel.setLayout(qt.QGridLayout())
ctrlLayout.addWidget(self.sliderPanel)
sliderLayout = self.sliderPanel.layout()
if self.numFrames > 1 or frameActiveFlag:
self.ctrlFrameLabel = qt.QLabel('Frame')
self.ctrlFrameSlider = ctk.ctkSliderWidget()
self.ctrlFrameSlider.connect('valueChanged(double)', self.onSliderFrameChanged)
sliderLayout.addWidget(self.ctrlFrameLabel, 0, 0)
sliderLayout.addWidget(self.ctrlFrameSlider, 0, 1)
self.ctrlLevelLabel = qt.QLabel('Level')
self.ctrlLevelSlider = ctk.ctkSliderWidget()
self.ctrlLevelSlider.connect('valueChanged(double)', self.onSliderLevelChanged)
sliderLayout.addWidget(self.ctrlLevelLabel, 1, 0)
sliderLayout.addWidget(self.ctrlLevelSlider, 1, 1)
self.ctrlWindowLabel = qt.QLabel('Window')
self.ctrlWindowSlider = ctk.ctkSliderWidget()
self.ctrlWindowSlider.connect('valueChanged(double)', self.onSliderWindowChanged)
sliderLayout.addWidget(self.ctrlWindowLabel, 2, 0)
sliderLayout.addWidget(self.ctrlWindowSlider, 2, 1)
# self.setSliderRangesAndValues()
if self.sliceNodeList :
self.orientPanel=qt.QWidget()
self.orientPanel.setLayout(qt.QGridLayout())
ctrlLayout.addWidget(self.orientPanel)
self.orientationButtons = {}
index=0
for orientation in self.orientations:
self.orientationButtons[orientation] = qt.QRadioButton()
self.orientationButtons[orientation].text = orientation
# self.orientationBox.layout().addWidget(self.orientationButtons[orientation])
if not self.allOrientationsFlag:
self.orientPanel.layout().addWidget(self.orientationButtons[orientation],0,index)
self.orientationButtons[orientation].connect("clicked()",lambda o=orientation: self.setOrientation(o))
index+=1
self.setOrientation(self.selectedOrientation)
self.setDisplay()
if len(self.nodeList[1]) :
self.SetFGOpacity(0.6)
self.valuePanel=qt.QWidget()
self.valuePanel.setLayout(qt.QGridLayout())
ctrlLayout.addWidget(self.valuePanel)
valueLayout = self.valuePanel.layout()
self.valueFrameLabel = qt.QLabel('FG:')
self.valueFrameValues = qt.QLabel('')
valueLayout.addWidget(self.valueFrameLabel, 0, 0)
valueLayout.addWidget(self.valueFrameValues, 0, 1)
if True:
# self.plotFrame = ctk.ctkCollapsibleButton()
# self.plotFrame.text = "Plotting"
# self.plotFrame.collapsed = 0
# f=ctk.ctkVTKChartView()
# f.show()
self.plotFrame=qt.QWidget()
# self.plotFrame.resize(self.plotFrame.width,400)
plotFrameLayout = qt.QGridLayout(self.plotFrame)
ctrlLayout.addWidget(self.plotFrame)
self.plotSettingsFrame = ctk.ctkCollapsibleButton()
self.plotSettingsFrame.text = "Settings"
self.plotSettingsFrame.collapsed = 1
plotSettingsFrameLayout = qt.QGridLayout(self.plotSettingsFrame)
# plotFrameLayout.addWidget(self.plotSettingsFrame,0,1)
self.xLogScaleCheckBox = qt.QCheckBox()
self.xLogScaleCheckBox.setChecked(0)
self.yLogScaleCheckBox = qt.QCheckBox()
self.yLogScaleCheckBox.setChecked(0)
# taken from https://github.com/fedorov/MultiVolumeExplorer
self.__chartView = ctk.ctkVTKChartView(self.ctrlWidget)
plotFrameLayout.addWidget(self.__chartView,0,0)
self.__chart = self.__chartView.chart()
self.__chartTable = vtk.vtkTable()
self.__xArray = vtk.vtkFloatArray()
self.__yArray = vtk.vtkFloatArray()
# will crash if there is no name
self.__xArray.SetName('Time1')
self.__yArray.SetName('Intensity1')
self.__chartTable.AddColumn(self.__xArray)
self.__chartTable.AddColumn(self.__yArray)
self.onInputChanged()
self.refreshObservers()
self.buttonPanel=qt.QWidget()
self.buttonPanel.setLayout(qt.QGridLayout())
ctrlLayout.addWidget(self.buttonPanel)
self.exitButton = qt.QPushButton("Exit")
self.exitButton.toolTip = "Close down slicer."
self.exitButton.name = "sviewer exit"
self.buttonPanel.layout().addWidget(self.exitButton,0,0)
self.exitButton.connect('clicked()', exit)
# do not do ctrlWin.show() here - for some reason window does not pop up then
return self.ctrlWidget
def removeObservers(self):
# remove observers and reset
for observee,tag in self.styleObserverTags:
observee.RemoveObserver(tag)
self.styleObserverTags = []
self.sliceWidgetsPerStyle = {}
def refreshObservers(self):
""" When the layout changes, drop the observers from
all the old widgets and create new observers for the
newly created widgets"""
self.removeObservers()
# get new slice nodes
sWidgetList = []
if self.sliceNodeList :
for snode in self.sliceNodeList.values() :
sliceWidget = self.layoutManager.sliceWidget(snode.GetLayoutName())
if sliceWidget:
sWidgetList.append(sliceWidget)
else:
sWidgetList.append(self.singleViewerWidget)
for sliceWidget in sWidgetList :
# add obserservers and keep track of tags
style = sliceWidget.sliceView().interactorStyle()
self.sliceWidgetsPerStyle[style] = sliceWidget
events = ("MouseMoveEvent", "EnterEvent", "LeaveEvent")
for event in events:
tag = style.AddObserver(event, self.processEvent)
self.styleObserverTags.append([style,tag])
def processEvent(self,observee,event):
#if not self.iCharting.checked:
# return
fgImages = self.nodeImgList[0]
mvImage = fgImages[0]
fgNodes = self.nodeList[0]
nComponents = self.numFrames
# TODO: use a timer to delay calculation and compress events
if event == 'LeaveEvent':
# reset all the readouts
# TODO: reset the label text
return
if not self.sliceWidgetsPerStyle.has_key(observee):
return
sliceWidget = self.sliceWidgetsPerStyle[observee]
sliceLogic = sliceWidget.sliceLogic()
interactor = observee.GetInteractor()
xy = interactor.GetEventPosition()
coordinates = []
# we need a 4 element point to be able to multiply further down
coordinates.append( xy[0] )
coordinates.append( xy[1] )
coordinates.append( 0 )
coordinates.append( 1 )
xyToRas = sliceLogic.GetSliceNode().GetXYToRAS()
rasPos = xyToRas.MultiplyPoint( coordinates )
# VTK_CREATE(vtkMatrix4x4, rasToijk);
rasToijk=vtk.vtkMatrix4x4()
fgNodes[0].GetRASToIJKMatrix(rasToijk)
ijkFloat=rasToijk.MultiplyPoint(rasPos)
ijk = []
for element in ijkFloat:
try:
index = int(round(element))
except ValueError:
index = 0
ijk.append(index)
extent = mvImage[0].GetExtent()
if not (ijk[0]>=extent[0] and ijk[0]<=extent[1] and \
ijk[1]>=extent[2] and ijk[1]<=extent[3] and \
ijk[2]>=extent[4] and ijk[2]<=extent[5]):
return
nComponents=self.numFrames
# for testing
# nComponents=1
if True :
fgxArray = vtk.vtkFloatArray()
fgxArray.SetNumberOfTuples(nComponents)
fgxArray.SetNumberOfComponents(1)
fgxArray.Allocate(nComponents)
fgxArray.SetName('frame')
fgyArray = vtk.vtkFloatArray()
fgyArray.SetNumberOfTuples(nComponents)
fgyArray.SetNumberOfComponents(1)
fgyArray.Allocate(nComponents)
fgyArray.SetName('signal intensity')
# will crash if there is no name
fgChartTable = vtk.vtkTable()
fgChartTable.AddColumn(fgxArray)
fgChartTable.AddColumn(fgyArray)
fgChartTable.SetNumberOfRows(nComponents)
# get the vector of values at IJK
if (self.ctrlLevelSlider.maximum - self.ctrlLevelSlider.minimum) > 20 :
intFlag=True
else :
intFlag=False
disVal = []
if self.ctrlFrameSlider :
cInd = int(self.ctrlFrameSlider.value)
else :
cInd = 0
for c in range(nComponents):
for img in fgImages :
# for c in range(1):
iLen = len(img)
if c < iLen :
val = img[c].GetScalarComponentAsDouble(ijk[0],ijk[1],ijk[2],0)
else:
val = img[iLen-1].GetScalarComponentAsDouble(ijk[0],ijk[1],ijk[2],0)
if intFlag :
val = int(val)
else :
val = round(val*100) / 100.0
# Kilian: will only show last image - change later
self.__chartTable.SetValue(c, 0, self.__mvLabels[c])
self.__chartTable.SetValue(c, 1, val)
if cInd == c :
disVal.append(val)
#if useFg:
# fgValue = fgImage.GetScalarComponentAsDouble(ijk[0],ijk[1],ijk[2],0)
# fgChartTable.SetValue(c,0,self.__mvLabels[c])
# fgChartTable.SetValue(c,1,fgValue)
self.valueFrameValues.setText(disVal)
# To disable plot uncomment
# return
baselineAverageSignal = 0
# if self.iChartingPercent.checked:
# # check if percent plotting was requested and recalculate
# nBaselines = min(self.baselineFrames.value,nComponents)
# for c in range(nBaselines):
# baselineAverageSignal += mvImage[0].GetScalarComponentAsDouble(ijk[0],ijk[1],ijk[2],c)
# baselineAverageSignal /= nBaselines
# if baselineAverageSignal != 0:
# for c in range(nComponents):
# intensity = mvImage[0].GetScalarComponentAsDouble(ijk[0],ijk[1],ijk[2],c)
# self.__chartTable.SetValue(c,1,(intensity/baselineAverageSignal-1)*100.)
self.__chart.RemovePlot(0)
self.__chart.RemovePlot(0)
#if self.iChartingPercent.checked and baselineAverageSignal != 0:
# self.__chart.GetAxis(0).SetTitle('change relative to baseline, %')
#else:
self.__chart.GetAxis(0).SetTitle('signal intensity')
#tag = str(self.__mvNode.GetAttribute('MultiVolume.FrameIdentifyingDICOMTagName'))
#units = str(self.__mvNode.GetAttribute('MultiVolume.FrameIdentifyingDICOMTagUnits'))
#xTitle = tag+', '+units
self.__chart.GetAxis(1).SetTitle("Time")
#if self.iChartingIntensityFixedAxes.checked == True:
# self.__chart.GetAxis(0).SetBehavior(vtk.vtkAxis.FIXED)
# self.__chart.GetAxis(0).SetRange(self.__mvRange[0],self.__mvRange[1])
# self.__chart.GetAxis(0).SetRange(2000,6000)
# self.__chart.GetAxis(1).SetBehavior(vtk.vtkAxis.FIXED)
# self.__chart.GetAxis(1).SetRange(2000,6000)
#else:
# self.__chart.GetAxis(0).SetBehavior(vtk.vtkAxis.AUTO)
# if useFg:
useFg=False
if useFg:
plot = self.__chart.AddPlot(vtk.vtkChart.POINTS)
if vtk.VTK_MAJOR_VERSION <= 5:
plot.SetInput(self.__chartTable, 0, 1)
else:
plot.SetInputData(self.__chartTable, 0, 1)
fgplot = self.__chart.AddPlot(vtk.vtkChart.LINE)
if vtk.VTK_MAJOR_VERSION <= 5:
fgplot.SetInput(fgChartTable, 0, 1)
else:
fgplot.SetInputData(fgChartTable, 0, 1)
else:
plot = self.__chart.AddPlot(vtk.vtkChart.LINE)
if vtk.VTK_MAJOR_VERSION <= 5:
plot.SetInput(self.__chartTable, 0, 1)
else:
plot.SetInputData(self.__chartTable, 0, 1)
if self.xLogScaleCheckBox.checkState() == 2:
title = self.__chart.GetAxis(1).GetTitle()
self.__chart.GetAxis(1).SetTitle('log of '+title)
if self.yLogScaleCheckBox.checkState() == 2:
title = self.__chart.GetAxis(0).GetTitle()
self.__chart.GetAxis(0).SetTitle('log of '+title)
# seems to update only after another plot?..
def onInputChanged(self):
self.__chartTable.SetNumberOfRows(self.numFrames)
self.plotFrame.enabled = True
# self.plotFrame.collapsed = 0
self.__xArray.SetNumberOfTuples(self.numFrames)
self.__xArray.SetNumberOfComponents(1)
self.__xArray.Allocate(self.numFrames)
self.__xArray.SetName('Time')
self.__yArray.SetNumberOfTuples(self.numFrames)
self.__yArray.SetNumberOfComponents(1)
self.__yArray.Allocate(self.numFrames)
self.__yArray.SetName('Intensity')
self.__chartTable = vtk.vtkTable()
self.__chartTable.AddColumn(self.__xArray)
self.__chartTable.AddColumn(self.__yArray)
self.__chartTable.SetNumberOfRows(self.numFrames)
# get the range of intensities for the
self.__mvRange = [0,0]
for v in self.nodeImgList[0][0]:
frame = v
frameRange = frame.GetScalarRange()
self.__mvRange[0] = min(self.__mvRange[0], frameRange[0])
self.__mvRange[1] = max(self.__mvRange[1], frameRange[1])
# self.__mvLabels = string.split(self.__mvNode.GetAttribute('MultiVolume.FrameLabels'),',')
#if len(self.__mvLabels) != nFrames:
# return
self.__mvLabels = []
for l in range(self.numFrames):
self.__mvLabels.append(float(l))
# self.baselineFrames.maximum = self.numFrames
def SetLinkViewers(self,flag):
if self.sliceNodeList:
sliceNode = self.sliceNodeList.values()[0]
sliceWidget = self.layoutManager.sliceWidget(sliceNode.GetLayoutName())
sliceWidget.sliceController().setSliceLink(flag)
def SetOutlineLabelMapView(self):
if self.sliceNodeList:
sliceNode = self.sliceNodeList.values()[0]
sliceWidget = self.layoutManager.sliceWidget(sliceNode.GetLayoutName())
else :
sliceWidget = self.singleViewerWidget
if sliceWidget:
sliceWidget.sliceController().showLabelOutline(True)
def SetFGOpacity(self,val):
if self.sliceNodeList:
sliceNode = self.sliceNodeList.values()[0]
sliceWidget = self.layoutManager.sliceWidget(sliceNode.GetLayoutName())
else :
sliceWidget = self.singleViewerWidget
if sliceWidget:
sliceWidget.sliceController().setForegroundOpacity(0.6)
def SetInterpolationOff(self):
if self.sliceNodeList:
sliceNode = self.sliceNodeList.values()[0]
sliceWidget = self.layoutManager.sliceWidget(sliceNode.GetLayoutName())
else :
sliceWidget = self.singleViewerWidget
if sliceWidget:
sliceWidget.sliceController().setForegroundInterpolation(False)
sliceWidget.sliceController().setBackgroundInterpolation(False)
def onSliderFrameChanged(self,newValue):
index=int(newValue)
for vType in xrange(3):
for node,imgList in zip(self.nodeList[vType], self.nodeImgList[vType]) :
if index < len(imgList):
node.SetAndObserveImageData(imgList[index])
def onSliderLevelChanged(self,newValue):
for node in self.nodeList[0] :
dNode=node.GetVolumeDisplayNode()
dNode.AutoWindowLevelOff()
dNode.SetLevel(newValue)
def onSliderWindowChanged(self,newValue):
for node in self.nodeList[0] :
dNode=node.GetVolumeDisplayNode()
dNode.AutoWindowLevelOff()
dNode.SetWindow(newValue)
def setOrientation(self,orientation):
if self.sliceNodeList :
if orientation in self.orientations:
self.selectedOrientation = orientation
self.orientationButtons[orientation].checked = True
for sliceNode in self.sliceNodeList.values():
sliceNode.SetOrientation(orientation)
def setSliderRangesAndValues(self):
if self.ctrlFrameSlider:
self.ctrlFrameSlider.minimum=0
self.ctrlFrameSlider.maximum=self.numFrames-1
self.ctrlFrameSlider.value=0
vImg = self.nodeImgList[0][0][0]
vNode = self.nodeList[0][0]
if vImg and vNode:
iRange=vImg.GetScalarRange()
if self.ctrlLevelSlider :
self.ctrlLevelSlider.minimum = iRange[0]
self.ctrlLevelSlider.maximum = iRange[1]
self.ctrlLevelSlider.value=vNode.GetVolumeDisplayNode().GetLevel()
if self.ctrlWindowSlider:
self.ctrlWindowSlider.minimum = iRange[0]
self.ctrlWindowSlider.maximum = iRange[1]*1.5
self.ctrlWindowSlider.value=vNode.GetVolumeDisplayNode().GetWindow()
def setDisplay(self):
# Set display according to node list
# i.e. CompareViewer specific setting
if self.sliceNodeList :
self.SetLinkViewers(0)
singleBGFlag=(len(self.nodeList[1]) == 1)
singleLMFlag=(len(self.nodeList[2]) == 1)
numColumns=len(self.nodeList[0])
index = 0
orientIndex=0
if (self.allOrientationsFlag) :
orientation = self.orientations[0]
else :
orientation = self.selectedOrientation
for vName in sorted(self.sliceNodeList.keys()):
if (index == numColumns) :
index = 0
orientIndex += 1
orientation = self.orientations[orientIndex]
sNode = self.sliceNodeList[vName]
sWidget = self.layoutManager.sliceWidget(sNode.GetLayoutName())
sComposite=sWidget.sliceLogic().GetSliceCompositeNode()
sNode.SetOrientation(orientation)
if self.nodeList[0] and (len(self.nodeList[0]) > index) and self.nodeList[0][index]:
sComposite.SetForegroundVolumeID(self.nodeList[0][index].GetID())
else :
sComposite.SetForegroundVolumeID("")
if self.nodeList[1]:
if singleBGFlag:
sComposite.SetBackgroundVolumeID(self.nodeList[1][0].GetID())
elif (len(self.nodeList[1]) > index) and self.nodeList[1][index]:
sComposite.SetBackgroundVolumeID(self.nodeList[1][index].GetID())
else:
sComposite.SetBackgroundVolumeID("")
else :
sComposite.SetBackgroundVolumeID("")
if self.nodeList[2]:
if singleBGFlag:
sComposite.SetLabelVolumeID(self.nodeList[2][0].GetID())
elif (len(self.nodeList[2]) > index) and self.nodeList[2][index]:
sComposite.SetLabelVolumeID(self.nodeList[2][index].GetID())
else:
sComposite.SetLabelVolumeID("")
else :
sComposite.SetLabelVolumeID("")
sWidget.fitSliceToBackground()
index += 1
self.SetLinkViewers(1)
# End of CompareViewer specific setting
self.SetOutlineLabelMapView()
self.SetInterpolationOff()
# only execute if variabels are set
for IND in range(len(self.nodeList[0])):
dn = self.nodeList[0][IND].GetDisplayNode()
if not math.isnan(self.fgLowerThreshold) :
dn.SetLowerThreshold(self.fgLowerThreshold)
dn.ApplyThresholdOn()
if self.fgColorTable :
dn.SetAndObserveColorNodeID(self.fgColorTable)
# update Slider ranges
self.setSliderRangesAndValues()
def setNodeListsAndDisplay(self,newNodeList,newNodeImgList,removeFlag):
if removeFlag:
for type in xrange(3):
if self.nodeList[type] :
for node in self.nodeList[type] :
dNode=node.GetVolumeDisplayNode()
if dNode :
slicer.mrmlScene.RemoveNode(dNode)
slicer.mrmlScene.RemoveNode(node)
self.nodeList = newNodeList
self.nodeImgList=newNodeImgList
self.numFrames = len(self.nodeImgList[0][0])
self.setDisplay()
# Debug stuff
# slicer.util.saveScene('/tmp/scene.mrml')
# dir(foregroundNode) returns all functions
# [s for s in dir(blub) if "Get" in s]
# class MarkupsInViewsSelfTestWidget: