def updateChart(self, outputArray, name, lineResolution): # Change the layout to one that has a chart. This created the ChartView ln = slicer.util.getNode(pattern='vtkMRMLLayoutNode*') ln.SetViewArrangement(24) # Get the first ChartView node cvn = slicer.util.getNode(pattern='vtkMRMLChartViewNode*') # If we already created a chart node and it is still exists then reuse that cn = None if self.chartNodeID: cn = slicer.mrmlScene.GetNodeByID(cvn.GetChartNodeID()) if not cn: cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) self.chartNodeID = cn.GetID() # Configure properties of the Chart cn.SetProperty('default', 'title', 'Line profile') cn.SetProperty('default', 'xAxisLabel', 'Distance (mm)') cn.SetProperty('default', 'yAxisLabel', 'Intensity') cn.AddArray(name, outputArray.GetID()) # Set the chart to display cvn.SetChartNodeID(cn.GetID()) cvn.Modified()
def showChart(self, samples, names): lm = slicer.app.layoutManager() lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) doubleArrays = [] for sample in samples: arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() nDataPoints = sample.GetNumberOfTuples() array.SetNumberOfTuples(nDataPoints) array.SetNumberOfComponents(3) for i in range(nDataPoints): array.SetComponent(i, 0, i) array.SetComponent(i, 1, sample.GetTuple1(i)) array.SetComponent(i, 2, 0) doubleArrays.append(arrayNode) chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) cvNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') cvNodes.SetReferenceCount(cvNodes.GetReferenceCount()-1) cvNodes.InitTraversal() cvNode = cvNodes.GetNextItemAsObject() for pairs in zip(names,doubleArrays): chartNode.AddArray(pairs[0],pairs[1].GetID()) cvNode.SetChartNodeID(chartNode.GetID()) return
def __init__( self, parent=None ): if not parent: self.parent = slicer.qMRMLWidget() self.parent.setLayout( qt.QVBoxLayout() ) else: self.parent = parent self.layout = self.parent.layout() # this flag is 1 if there is an update in progress self.__updating = 1 self.__mvNode = None self.extractFrame = False # chart view node self.__cvn = None # data node #slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) # chart node self.__cn = slicer.vtkMRMLChartNode() self.__cn.SetScene(slicer.mrmlScene) slicer.mrmlScene.AddNode(self.__cn) # image play setup self.timer = qt.QTimer() self.timer.setInterval(50) self.timer.connect('timeout()', self.goToNext)
def __init__(self, parent=None): if not parent: self.parent = slicer.qMRMLWidget() self.parent.setLayout(qt.QVBoxLayout()) else: self.parent = parent self.layout = self.parent.layout() # this flag is 1 if there is an update in progress self.__updating = 1 self.__mvNode = None self.extractFrame = False # chart view node self.__cvn = None # data node #slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) # chart node self.__cn = slicer.vtkMRMLChartNode() self.__cn.SetScene(slicer.mrmlScene) slicer.mrmlScene.AddNode(self.__cn) # image play setup self.timer = qt.QTimer() self.timer.setInterval(50) self.timer.connect('timeout()', self.goToNext)
def updateChart(self,outputArray,name,lineResolution): # Change the layout to one that has a chart. This created the ChartView ln = slicer.util.getNode(pattern='vtkMRMLLayoutNode*') ln.SetViewArrangement(24) # Get the first ChartView node cvn = slicer.util.getNode(pattern='vtkMRMLChartViewNode*') # If we already created a chart node and it is still exists then reuse that cn = None if self.chartNodeID: cn = slicer.mrmlScene.GetNodeByID(cvn.GetChartNodeID()) if not cn: cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) self.chartNodeID = cn.GetID() # Configure properties of the Chart cn.SetProperty('default', 'title', 'Line profile') cn.SetProperty('default', 'xAxisLabel', 'Distance (mm)') cn.SetProperty('default', 'yAxisLabel', 'Intensity') cn.AddArray(name, outputArray.GetID()) # Set the chart to display cvn.SetChartNodeID(cn.GetID()) cvn.Modified()
def createStatsChart(self, labelNode, valueToPlot, ignoreZero=False): """Make a MRML chart of the current stats """ layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount() - 1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement( slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass( 'vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount() - 1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() samples = len(self.labelStats["Labels"]) tuples = samples if ignoreZero and self.labelStats["Labels"].__contains__(0): tuples -= 1 array.SetNumberOfTuples(tuples) tuple = 0 for i in xrange(samples): index = self.labelStats["Labels"][i] if not (ignoreZero and index == 0): array.SetComponent(tuple, 0, index + 1) array.SetComponent(tuple, 1, self.labelStats[index, valueToPlot]) array.SetComponent(tuple, 2, 0) tuple += 1 chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray(valueToPlot, arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Label Statistics') chartNode.SetProperty('default', 'xAxisLabel', 'Label') chartNode.SetProperty('default', 'yAxisLabel', valueToPlot) chartNode.SetProperty('default', 'type', 'Line') chartNode.SetProperty('default', 'xAxisType', 'categorical') chartNode.SetProperty('default', 'showLegend', 'off') # series level properties if labelNode.IsA("vtkMRMLSequenceNode"): if labelNode.GetNthDataNode( 0).GetDisplayNode() != None and labelNode.GetNthDataNode( 0).GetDisplayNode().GetColorNode() != None: chartNode.SetProperty( valueToPlot, 'lookupTable', labelNode.GetNthDataNode( 0).GetDisplayNode().GetColorNodeID()) else: chartNode.SetProperty( valueToPlot, 'lookupTable', slicer.mrmlScene.GetNodeByID('vtkMRMLColorTableNodeRed'))
def chartContact(self, screwCount): # Get the Chart View Node cvns = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') cvns.InitTraversal() cvn = cvns.GetNextItemAsObject() cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) arrayNodes = [] for i in range(0,screwCount): # Create an Array Node and add some data dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) arrayNodes.insert(i,dn) a = dn.GetArray() a.SetNumberOfTuples(10) x = range(0, 10) screwValues = self.screwContact[i] for j in range(len(x)): a.SetComponent(j, 0, (j * 10) + 5) a.SetComponent(j, 1, screwValues[j]) a.SetComponent(j, 2, 0) print j print screwValues[j] cn.AddArray('Screw %s' % i, dn.GetID()) dnCort = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) dnCanc = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a1 = dnCort.GetArray() a2 = dnCanc.GetArray() a1.SetNumberOfTuples(2) a2.SetNumberOfTuples(2) a1.SetComponent(0, 0, 0) a1.SetComponent(0, 1, 250) a1.SetComponent(0, 2, 0) a1.SetComponent(1, 0, 100) a1.SetComponent(1, 1, 250) a1.SetComponent(1, 2, 0) a2.SetComponent(0, 0, 0) a2.SetComponent(0, 1, 130) a2.SetComponent(0, 2, 0) a2.SetComponent(1, 0, 100) a2.SetComponent(1, 1, 130) a2.SetComponent(1, 2, 0) cn.AddArray('Cortical Bone', dnCort.GetID()) cn.AddArray('Cancellous Bone', dnCanc.GetID()) #cn.SetProperty('default', 'title', 'Information for Screw at point %s' % fidName) cn.SetProperty('default', 'title', 'Screw - Bone Contact') cn.SetProperty('default', 'xAxisLabel', 'Screw Percentile (Head - Tip)') #cn.SetProperty('default', 'xAxisType', 'categorical') cn.SetProperty('default', 'yAxisLabel', 'Average HU Contact') cn.SetProperty('default', 'showLegend', 'on') cn.SetProperty('default', 'type', 'Line') cn.SetProperty('default', 'xAxisPad', '0') #cn.SetProperty('default', 'xAxisPad', '0') cvn.SetChartNodeID(cn.GetID())
def prepareCharts(self): layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() chartNode = slicer.mrmlScene.GetNodesByClassByName("vtkMRMLChartNode","OTF").GetItemAsObject(0) if not chartNode: chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.SetName("OTF") print("set properties") chartNode.SetProperty('default', 'title', 'OTF') chartNode.SetProperty('default', 'xAxisLabel', 'lp/mm') chartNode.SetProperty('default', 'yAxisLabel', 'Signal') chartNode.SetProperty('default', 'type', 'Line') chartNode.SetProperty('default', 'showLegend', 'on') chartNode.SetProperty('default', 'Markers', 'on') chartViewNode.SetChartNodeID(chartNode.GetID()) SFchartNode = None if self.intermSave: SFchartNode = slicer.mrmlScene.GetNodesByClassByName("vtkMRMLChartNode","SF").GetItemAsObject(0) if not SFchartNode: SFchartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) SFchartNode.SetName("SF") print("set properties") SFchartNode.SetProperty('default', 'title', 'SF') SFchartNode.SetProperty('default', 'xAxisLabel', 'mm') SFchartNode.SetProperty('default', 'yAxisLabel', 'Signal') SFchartNode.SetProperty('default', 'type', 'Line') SFchartNode.SetProperty('default', 'showLegend', 'on') SFchartNode.SetProperty('default', 'Markers', 'on') return [chartNode,SFchartNode]
def createStatsChart(self, labelNode, valueToPlot, ignoreZero=False): """Make a MRML chart of the current stats """ layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() samples = len(self.labelStats["Labels"]) tuples = samples if ignoreZero and self.labelStats["Labels"].__contains__(0): tuples -= 1 array.SetNumberOfTuples(tuples) tuple = 0 for i in xrange(samples): index = self.labelStats["Labels"][i] if not (ignoreZero and index == 0): array.SetComponent(tuple, 0, index) try: v = float(self.labelStats[index,valueToPlot]) except (KeyError, TypeError): v = float(0) array.SetComponent(tuple, 1, v) array.SetComponent(tuple, 2, 0) tuple += 1 chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) state = chartNode.StartModify() chartNode.AddArray(valueToPlot, arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Label Statistics') chartNode.SetProperty('default', 'xAxisLabel', 'Label') chartNode.SetProperty('default', 'yAxisLabel', valueToPlot) chartNode.SetProperty('default', 'type', 'Bar'); chartNode.SetProperty('default', 'xAxisType', 'categorical') chartNode.SetProperty('default', 'showLegend', 'off') # series level properties if labelNode.GetDisplayNode() != None and labelNode.GetDisplayNode().GetColorNode() != None: chartNode.SetProperty(valueToPlot, 'lookupTable', labelNode.GetDisplayNode().GetColorNodeID()); chartNode.EndModify(state)
def Chart(self,dists): """Make a MRML chart of the current stats """ if dists: layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() #dists=self.sides + self.diagonals # samples=len(dists) minimum=min(dists) maximum=max(dists) nbins=10.0 binsize=(maximum-minimum)/nbins hist=self.createHst(binsize,dists) samples=len(hist['bin']) print("samples: " + str(samples)) array.SetNumberOfTuples(samples) # for n in range(samples): # array.SetComponent(n, 0, n) # array.SetComponent(n, 1, dists[n]) for n in range(samples): array.SetComponent(n, 0, hist['bin'][n]) array.SetComponent(n, 1, hist['freq'][n]) chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.SetName("Freq") chartNode.AddArray("Distances", arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) print("set properties") chartNode.SetProperty('default', 'title', 'Measured Distances') chartNode.SetProperty('default', 'xAxisLabel', 'Distance') chartNode.SetProperty('default', 'yAxisLabel', 'N') #chartNode.SetProperty('default', 'type', 'Bar'); chartNode.SetProperty('default', 'type', 'Scatter'); #chartNode.SetProperty('default', 'xAxisType', 'categorical') chartNode.SetProperty('default', 'showLegend', 'off')
def section_PluginAutoSearch(self): self.delayDisplay("Plugin auto search",self.delayMs) # Test whether the owner plugin is automatically searched when the associated data node changes chartNode2 = slicer.vtkMRMLChartNode() slicer.mrmlScene.AddNode(chartNode2) chartNode2.SetName(self.sampleChartName + '2') clonedMarkupShNode = slicer.util.getNode(self.sampleMarkupName + ' Copy_SubjectHierarchy') clonedMarkupShNode.SetAssociatedNodeID(chartNode2.GetID()) self.assertTrue( clonedMarkupShNode.GetOwnerPluginName() == 'Charts' )
def showChart(self, samples, names): print("Logic showing chart") print 'samples: ', samples lm = slicer.app.layoutManager() lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) # initialize double array MRML node for each sample list, # since this is what chart view MRML node needs doubleArrays = [] for sample in samples: print sample arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() nDataPoints = sample.GetNumberOfTuples() array.SetNumberOfTuples(nDataPoints) array.SetNumberOfComponents(3) #print 'sample.GetTuple(0): ', sample.GetTuple(0) for i in range(nDataPoints): array.SetComponent(i, 0, i) array.SetComponent(i, 1, sample.GetTuple1(i)) array.SetComponent(i, 2, 0) doubleArrays.append(arrayNode) chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # get chart view MRML node cvNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') cvNodes.SetReferenceCount(cvNodes.GetReferenceCount()-1) cvNodes.InitTraversal() cvNode = cvNodes.GetNextItemAsObject() # create a new chart node chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) for pairs in zip(names, doubleArrays): chartNode.AddArray(pairs[0], pairs[1].GetID()) cvNode.SetChartNodeID( chartNode.GetID() ) return
def chartRatios(self): ratiosAr = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) ratiosArinv = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) firstName=self.firstSelector.currentNode().GetName() secondName=self.secondSelector.currentNode().GetName() ratiosAr.SetName(firstName+"-"+secondName+"-ratios") ratiosArinv.SetName(firstName+"-"+secondName+"-inverse-ratios") arrayD = ratiosAr.GetArray() arrayDinv = ratiosArinv.GetArray() arrayD.SetNumberOfTuples(self.table.rowCount) arrayDinv.SetNumberOfTuples(self.table.rowCount) for n in range(self.table.rowCount): if self.table.item(n,3): arrayD.SetComponent(n, 0, n) arrayDinv.SetComponent(n, 0, n) arrayD.SetComponent(n, 1, float(self.table.item(n,3).text())) arrayDinv.SetComponent(n, 1, float(self.table.item(self.table.rowCount-1-n,3).text())) layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() chartNode = slicer.mrmlScene.GetNodesByClassByName("vtkMRMLChartNode","Ratios").GetItemAsObject(0) if not chartNode: chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.SetName("Ratios") print("set properties") chartNode.SetProperty('default', 'title', 'Ratios') chartNode.SetProperty('default', 'xAxisLabel', 'slice') chartNode.SetProperty('default', 'yAxisLabel', 'Ratio') chartNode.SetProperty('default', 'type', 'Line') chartNode.SetProperty('default', 'showLegend', 'on') chartNode.SetProperty('default', 'Markers', 'on') chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.AddArray("Ratio", ratiosAr.GetID()) chartNode.AddArray("Ratioinv", ratiosArinv.GetID())
def createStatsChart(self, labelNode, valueToPlot, ignoreZero=False): """Make a MRML chart of the current stats """ layoutNodes = slicer.mrmlScene.GetNodesByClass("vtkMRMLLayoutNode") layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount() - 1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass("vtkMRMLChartViewNode") chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount() - 1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() samples = len(self.labelStats["Labels"]) tuples = samples if ignoreZero and self.labelStats["Labels"].__contains__(0): tuples -= 1 array.SetNumberOfTuples(tuples) tuple = 0 for i in xrange(samples): index = self.labelStats["Labels"][i] if not (ignoreZero and index == 0): array.SetComponent(tuple, 0, index) array.SetComponent(tuple, 1, self.labelStats[index, valueToPlot]) array.SetComponent(tuple, 2, 0) tuple += 1 chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) state = chartNode.StartModify() chartNode.AddArray(valueToPlot, arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty("default", "title", "Label Statistics") chartNode.SetProperty("default", "xAxisLabel", "Label") chartNode.SetProperty("default", "yAxisLabel", valueToPlot) chartNode.SetProperty("default", "type", "Bar") chartNode.SetProperty("default", "xAxisType", "categorical") chartNode.SetProperty("default", "showLegend", "off") # series level properties if labelNode.GetDisplayNode() != None and labelNode.GetDisplayNode().GetColorNode() != None: chartNode.SetProperty(valueToPlot, "lookupTable", labelNode.GetDisplayNode().GetColorNodeID()) chartNode.EndModify(state)
def createStatsChart(self, labelNode, valueToPlot, ignoreZero=False): """Make a MRML chart of the current stats """ layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount() - 1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement( slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass( 'vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount() - 1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() samples = len(self.regionTags) tuples = samples array.SetNumberOfTuples(tuples) tuple = 0 cycle = 'insp' for i in xrange(samples): index = self.regionTags[i] array.SetComponent(tuple, 0, i) array.SetComponent(tuple, 1, self.labelStats[cycle, valueToPlot, index]) array.SetComponent(tuple, 2, 0) tuple += 1 chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray(valueToPlot, arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Parenchyma Statistics') chartNode.SetProperty('default', 'xAxisLabel', 'Label') chartNode.SetProperty('default', 'yAxisLabel', valueToPlot) chartNode.SetProperty('default', 'type', 'Bar') chartNode.SetProperty('default', 'xAxisType', 'categorical') chartNode.SetProperty('default', 'showLegend', 'off') # series level properties if labelNode.GetDisplayNode() != None and labelNode.GetDisplayNode( ).GetColorNode() != None: chartNode.SetProperty(valueToPlot, 'lookupTable', labelNode.GetDisplayNode().GetColorNodeID())
def createChartNodeAndInsertData(self): # setup color node colorNode = self.labelNode.GetDisplayNode().GetColorNode() lut = colorNode.GetLookupTable() # add initialized data nodes to the chart chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.ClearArrays() for k in self.labeledVoxels.keys(): k = int(k) name = colorNode.GetColorName(k) chartNode.AddArray(name, self.dataNodes[k].GetID()) rgb = lut.GetTableValue(int(k)) colorStr = Helper.RGBtoHex(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255) chartNode.SetProperty(name, "color", colorStr) return chartNode
def initializeCharting(self): # Enable iCharting Settings self.iCharting.setChecked(True) self.iCharting.enabled = True self.iChartingCollapsibleButton.collapsed = False # Initialize image data (pre-contrast node at index '0') self.nComponents = len(self.volumeNodes) self.nodeImageData = [] for node in xrange(self.nComponents): self.nodeImageData.append(self.volumeNodes[node].GetImageData()) self.extent = self.nodeImageData[0].GetExtent() # Change active volume nodes in scene appLogic = slicer.app.applicationLogic() selectionNode = appLogic.GetSelectionNode() selectionNode.SetReferenceActiveVolumeID(self.nodePre.GetID()) selectionNode.SetReferenceActiveLabelVolumeID( self.nodeSegmentCAD.GetID()) selectionNode.SetReferenceSecondaryVolumeID(self.node1.GetID()) appLogic.PropagateVolumeSelection() # Change scene layout to conventional quantitative view layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement( slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) # Set up chart within the scene self.chartViewNodes = slicer.mrmlScene.GetNodesByClass( 'vtkMRMLChartViewNode') self.chartViewNodes.InitTraversal() self.chartViewNode = self.chartViewNodes.GetNextItemAsObject() self.arrayNode = slicer.mrmlScene.AddNode( slicer.vtkMRMLDoubleArrayNode()) self.chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) self.chartNode.AddArray('voxelijk', self.arrayNode.GetID()) self.chartNode.SetProperty('default', 'title', 'Interactive Charting') self.chartNode.SetProperty('default', 'xAxisLabel', 'DCE-MRI Volumes') self.chartNode.SetProperty('default', 'yAxisLabel', 'Percentage Increase from Baseline') self.chartNode.SetProperty('default', 'type', 'Line') self.chartNode.SetProperty('default', 'xAxisType', 'quantitative') self.chartNode.SetProperty('default', 'showGrid', 'on') self.chartNode.SetProperty('default', 'showLegend', 'on') self.chartNode.SetProperty('default', 'showMarkers', 'on') self.chartNode.SetProperty('default', 'xAxisPad', '1') self.chartViewNode.SetChartNodeID(self.chartNode.GetID())
def section_ChartRole(self): self.delayDisplay("Chart role",self.delayMs) # Create sample chart node chartNode = slicer.vtkMRMLChartNode() slicer.mrmlScene.AddNode(chartNode) chartNode.SetName(self.sampleChartName) # Add markups to subject hierarchy from vtkSlicerSubjectHierarchyModuleMRML import vtkMRMLSubjectHierarchyNode studyNode = slicer.util.getNode(self.studyName + slicer.vtkMRMLSubjectHierarchyConstants.GetSubjectHierarchyNodeNamePostfix()) self.assertTrue( studyNode != None ) chartShNode = vtkMRMLSubjectHierarchyNode.CreateSubjectHierarchyNode(slicer.mrmlScene, studyNode, slicer.vtkMRMLSubjectHierarchyConstants.GetDICOMLevelSeries(), self.sampleChartName, chartNode) self.assertTrue( chartShNode != None ) self.assertTrue( chartShNode.GetParentNode() == studyNode ) self.assertTrue( chartShNode.GetOwnerPluginName() == 'Charts' )
def createStatsChart(self, labelNode, valueToPlot, ignoreZero=False): """Make a MRML chart of the current stats """ layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() samples = len(self.regionTags) tuples = samples array.SetNumberOfTuples(tuples) tuple = 0 cycle = 'insp' for i in xrange(samples): index = self.regionTags[i] array.SetComponent(tuple, 0, i) array.SetComponent(tuple, 1, self.labelStats[cycle,valueToPlot,index]) array.SetComponent(tuple, 2, 0) tuple += 1 chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray(valueToPlot, arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Parenchyma Statistics') chartNode.SetProperty('default', 'xAxisLabel', 'Label') chartNode.SetProperty('default', 'yAxisLabel', valueToPlot) chartNode.SetProperty('default', 'type', 'Bar'); chartNode.SetProperty('default', 'xAxisType', 'categorical') chartNode.SetProperty('default', 'showLegend', 'off') # series level properties if labelNode.GetDisplayNode() != None and labelNode.GetDisplayNode().GetColorNode() != None: chartNode.SetProperty(valueToPlot, 'lookupTable', labelNode.GetDisplayNode().GetColorNodeID());
def section_PluginAutoSearch(self): self.delayDisplay("Plugin auto search",self.delayMs) # Disable subject hierarchy auto-creation to be able to test plugin auto search subjectHierarchyWidget = slicer.modules.subjecthierarchy.widgetRepresentation() subjectHierarchyPluginLogic = subjectHierarchyWidget.pluginLogic() self.assertTrue( subjectHierarchyWidget != None ) self.assertTrue( subjectHierarchyPluginLogic != None ) subjectHierarchyPluginLogic.autoCreateSubjectHierarchy = False # Test whether the owner plugin is automatically searched when the associated data node changes chartNode2 = slicer.vtkMRMLChartNode() chartNode2.SetName(self.sampleChartName + '2') slicer.mrmlScene.AddNode(chartNode2) clonedMarkupShNode = slicer.util.getNode(self.sampleMarkupName + ' Copy' + slicer.vtkMRMLSubjectHierarchyConstants.GetSubjectHierarchyNodeNamePostfix()) clonedMarkupShNode.SetAssociatedNodeID(chartNode2.GetID()) self.assertTrue( clonedMarkupShNode.GetOwnerPluginName() == 'Charts' )
def addChart(self,patient): ln = slicer.util.getNode(pattern='vtkMRMLLayoutNode*') ln.SetViewArrangement(26) cvn = slicer.util.getNode(pattern='vtkMRMLChartViewNode*') cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) linePattern = ["solid","dashed","dotted","dashed-dotted"] planNumber = 0 chartNames = [] if patient.plans == []: print "No plans." return for plan in patient.plans: for voi in plan.vois: if voi.voiTableCheckBox.checkState() == 0: continue voi.setSlicerDoubleArray() #set double array for chart newName = voi.name n = 1 nameBool = True while nameBool: nameBool = False for name in chartNames: if newName == name: nameBool = True newName = voi.name + "_" + str(n) n += 1 break cn.AddArray(newName, voi.dn.GetID()) cn.SetProperty(voi.name,'linePattern',linePattern[planNumber]) chartNames.append(newName) planNumber += 1 if plan.optDose == 100: cn.SetProperty('default', 'xAxisLabel', 'Dose [%]') else: cn.SetProperty('default', 'xAxisLabel', 'Dose [Gy]') cn.SetProperty('default', 'yAxisLabel', 'Volume [%]') cvn.SetChartNodeID(cn.GetID()) return
def initializeCharting(self): # Enable iCharting Settings self.iCharting.setChecked(True) self.iCharting.enabled = True self.iChartingCollapsibleButton.collapsed = False # Initialize image data (pre-contrast node at index '0') self.nComponents = len(self.volumeNodes) self.nodeImageData = [] for node in xrange (self.nComponents): self.nodeImageData.append(self.volumeNodes[node].GetImageData()) self.extent = self.nodeImageData[0].GetExtent() # Change active volume nodes in scene appLogic = slicer.app.applicationLogic() selectionNode = appLogic.GetSelectionNode() selectionNode.SetReferenceActiveVolumeID(self.nodePre.GetID()) selectionNode.SetReferenceActiveLabelVolumeID(self.nodeSegmentCAD.GetID()) selectionNode.SetReferenceSecondaryVolumeID(self.node1.GetID()) appLogic.PropagateVolumeSelection() # Change scene layout to conventional quantitative view layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) # Set up chart within the scene self.chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') self.chartViewNodes.InitTraversal() self.chartViewNode = self.chartViewNodes.GetNextItemAsObject() self.arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) self.chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) self.chartNode.AddArray('voxelijk', self.arrayNode.GetID()) self.chartNode.SetProperty('default', 'title', 'Interactive Charting') self.chartNode.SetProperty('default', 'xAxisLabel', 'DCE-MRI Volumes') self.chartNode.SetProperty('default', 'yAxisLabel', 'Percentage Increase from Baseline') self.chartNode.SetProperty('default', 'type', 'Line') self.chartNode.SetProperty('default', 'xAxisType', 'quantitative') self.chartNode.SetProperty('default', 'showGrid', 'on') self.chartNode.SetProperty('default', 'showLegend', 'on') self.chartNode.SetProperty('default', 'showMarkers', 'on') self.chartNode.SetProperty('default', 'xAxisPad', '1') self.chartViewNode.SetChartNodeID(self.chartNode.GetID())
def initializeChart(self, data, dataName): # set the layout that includes a chart viewer lm = slicer.app.layoutManager() lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) # chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() # populate DoubleArrayNode with the result of the probing arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() samples = data.GetNumberOfTuples() array.SetNumberOfTuples(samples) tuple = 0 for i in xrange(samples): array.SetComponent(tuple, 0, i) array.SetComponent(tuple, 1, data.GetTuple1(i)) array.SetComponent(tuple, 2, 0) tuple += 1 # create a new ChartNode chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) state = chartNode.StartModify() chartNode.AddArray(dataName, arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Image line probe') chartNode.SetProperty('default', 'xAxisLabel', 'Sample point') chartNode.SetProperty('default', 'yAxisLabel', 'Intensity') chartNode.SetProperty('default', 'type', 'Line'); chartNode.SetProperty('default', 'showLegend', 'on') chartNode.EndModify(state)
def onApplyThreshold(self): min, max = self.getDistanceBound() newPoints = vtk.vtkPoints() newLines = vtk.vtkCellArray() newTensors = vtk.vtkFloatArray() newTensors.SetNumberOfComponents(9) newScalars = vtk.vtkFloatArray() points = self.inputPolyData.GetPoints() lines = self.inputPolyData.GetLines() tensors = self.inputPolyData.GetPointData().GetTensors() lines.InitTraversal() newId = 0 for length in self.distanceTable: if length <= self.thresholdMax.value and length >= self.thresholdMin.value: ids = vtk.vtkIdList() lines.GetNextCell(ids) newLine = vtk.vtkPolyLine() #print(ids.GetNumberOfIds()) newLine.GetPointIds().SetNumberOfIds(ids.GetNumberOfIds()) #print(((length-min)/(max-min))*100) for i in range(ids.GetNumberOfIds()): newPoints.InsertNextPoint(points.GetPoint(ids.GetId(i))) newLine.GetPointIds().SetId(i, newId) newScalars.InsertNextValue(((length - min) / (max - min))) newId += 1 tensorValue = [0] * 9 if (tensors != None): for j in range(9): tensorValue[j] = tensors.GetComponent( ids.GetId(i), j) newTensors.InsertNextTuple(tensorValue) newLines.InsertNextCell(newLine) self.outputPolyData = vtk.vtkPolyData() self.outputPolyData.SetPoints(newPoints) self.outputPolyData.SetLines(newLines) self.outputPolyData.GetPointData().SetTensors(newTensors) newScalars.SetName("Length") self.outputPolyData.GetPointData().AddArray(newScalars) self.outputNode.SetAndObservePolyData(self.outputPolyData) chartViewNodes = slicer.mrmlScene.GetNodesByClass( 'vtkMRMLChartViewNode') chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() array.SetNumberOfTuples(10) step = (max - min) / 10 interMin = min interMax = min + step for i in range(10): numberOfFibers = 0 for length in self.distanceTable: if length <= interMax and length >= interMin and length <= self.thresholdMax.value and length >= self.thresholdMin.value: numberOfFibers += 1 array.SetComponent(i, 0, (interMin + interMax) / 2) array.SetComponent(i, 1, numberOfFibers) array.SetComponent(i, 2, 0) interMin += step interMax += step chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray("Fiber Length", arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Length Distribution') chartNode.SetProperty('default', 'xAxisLabel', 'Length') chartNode.SetProperty('default', 'yAxisLabel', 'Distribution') chartNode.SetProperty('default', 'type', 'Bar')
def onCalculateLength(self): self.inputPolyData = self.vtkNode.GetPolyData() points = self.inputPolyData.GetPoints() lines = self.inputPolyData.GetLines() lines.InitTraversal() self.distanceTable = list() for i in range(self.inputPolyData.GetNumberOfCells()): fiberLength = 0 ids = vtk.vtkIdList() lines.GetNextCell(ids) #print(ids.GetNumberOfIds()) for j in range(ids.GetNumberOfIds() - 1): point1 = [0, 0, 0] point2 = [0, 0, 0] points.GetPoint(ids.GetId(j), point1) points.GetPoint(ids.GetId(j + 1), point2) x = point2[0] - point1[0] y = point2[1] - point1[1] z = point2[2] - point1[2] step = (x * x + y * y + z * z)**.5 fiberLength += step self.distanceTable.append(fiberLength) min, max = self.getDistanceBound() self.thresholdMin.setValue(min) self.thresholdMin.enabled = True self.thresholdMax.setValue(max + 1) self.thresholdMax.enabled = True self.applyThresholdButton.enabled = True layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement( slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass( 'vtkMRMLChartViewNode') chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() array.SetNumberOfTuples(10) step = (max - min) / 10 interMin = min interMax = min + step for i in range(10): numberOfFibers = 0 for length in self.distanceTable: if length <= interMax and length >= interMin and length <= self.thresholdMax.value and length >= self.thresholdMin.value: numberOfFibers += 1 array.SetComponent(i, 0, (interMin + interMax) / 2) array.SetComponent(i, 1, numberOfFibers) array.SetComponent(i, 2, 0) interMin += step interMax += step chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray("Fiber Length", arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Length Distribution') chartNode.SetProperty('default', 'xAxisLabel', 'Length') chartNode.SetProperty('default', 'yAxisLabel', 'Distribution') chartNode.SetProperty('default', 'type', 'Bar')
def onChartRequested(self): # iterate over the label image and collect the IJK for each label element labelNode = self.__fSelector.currentNode() mvNode = self.__mvSelector.currentNode() if labelNode == None or mvNode == None: return labelID = labelNode.GetID() img = labelNode.GetImageData() extent = img.GetWholeExtent() labeledVoxels = {} for i in range(extent[1]): for j in range(extent[3]): for k in range(extent[5]): labelValue = img.GetScalarComponentAsFloat(i,j,k,0) if labelValue: if labelValue in labeledVoxels.keys(): labeledVoxels[labelValue].append([i,j,k]) else: labeledVoxels[labelValue] = [] labeledVoxels[labelValue].append([i,j,k]) # go over all elements, calculate the mean in each frame for each label # and add to the chart array nComponents = self.__mvNode.GetNumberOfFrames() dataNodes = {} for k in labeledVoxels.keys(): dataNodes[k] = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) dataNodes[k].GetArray().SetNumberOfTuples(nComponents) mvImage = self.__mvNode.GetImageData() for c in range(nComponents): for k in labeledVoxels.keys(): arr = dataNodes[k].GetArray() mean = 0. cnt = 0. for v in labeledVoxels[k]: mean = mean+mvImage.GetScalarComponentAsFloat(v[0],v[1],v[2],c) cnt = cnt+1 arr.SetComponent(c, 0, self.__mvLabels[c]) arr.SetComponent(c, 1, mean/cnt) arr.SetComponent(c, 2, 0) if self.iChartingPercent.checked: nBaselines = min(self.baselineFrames.value,nComponents) for k in labeledVoxels.keys(): arr = dataNodes[k].GetArray() baseline = 0 for bc in range(nBaselines): baseline += arr.GetComponent(bc,1) baseline /= nBaselines if baseline != 0: for ic in range(nComponents): intensity = arr.GetComponent(ic,1) percentChange = (intensity/baseline-1)*100. arr.SetComponent(ic,1,percentChange) layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # setup color node colorNodeID = labelNode.GetDisplayNode().GetColorNodeID() colorNode = labelNode.GetDisplayNode().GetColorNode() lut = colorNode.GetLookupTable() # add initialized data nodes to the chart chartNode.ClearArrays() for k in labeledVoxels.keys(): name = colorNode.GetColorName(k) chartNode.AddArray(name, dataNodes[k].GetID()) rgb = lut.GetTableValue(int(k)) colorStr = self.RGBtoHex(rgb[0]*255,rgb[1]*255,rgb[2]*255) chartNode.SetProperty(name, "color", colorStr) tag = str(self.__mvNode.GetAttribute('MultiVolume.FrameIdentifyingDICOMTagName')) units = str(self.__mvNode.GetAttribute('MultiVolume.FrameIdentifyingDICOMTagUnits')) xTitle = tag+', '+units chartNode.SetProperty('default','xAxisLabel',xTitle) if self.iChartingPercent.checked: chartNode.SetProperty('default','yAxisLabel','change relative to baseline, %') else: chartNode.SetProperty('default','yAxisLabel','mean signal intensity') chartViewNode.SetChartNodeID(chartNode.GetID())
def onChartRequested(self): # iterate over the label image and collect the IJK for each label element labelNode = self.__fSelector.currentNode() mvNode = self.__mvSelector.currentNode() if labelNode == None or mvNode == None: return labelID = labelNode.GetID() img = labelNode.GetImageData() extent = img.GetWholeExtent() labeledVoxels = {} for i in range(extent[1]): for j in range(extent[3]): for k in range(extent[5]): labelValue = img.GetScalarComponentAsFloat(i, j, k, 0) if labelValue: if labelValue in labeledVoxels.keys(): labeledVoxels[labelValue].append([i, j, k]) else: labeledVoxels[labelValue] = [] labeledVoxels[labelValue].append([i, j, k]) # go over all elements, calculate the mean in each frame for each label # and add to the chart array nComponents = self.__mvNode.GetNumberOfFrames() dataNodes = {} for k in labeledVoxels.keys(): dataNodes[k] = slicer.mrmlScene.AddNode( slicer.vtkMRMLDoubleArrayNode()) dataNodes[k].GetArray().SetNumberOfTuples(nComponents) mvImage = self.__mvNode.GetImageData() for c in range(nComponents): for k in labeledVoxels.keys(): arr = dataNodes[k].GetArray() mean = 0. cnt = 0. for v in labeledVoxels[k]: mean = mean + mvImage.GetScalarComponentAsFloat( v[0], v[1], v[2], c) cnt = cnt + 1 arr.SetComponent(c, 0, self.__mvLabels[c]) arr.SetComponent(c, 1, mean / cnt) arr.SetComponent(c, 2, 0) if self.iChartingPercent.checked: nBaselines = min(self.baselineFrames.value, nComponents) for k in labeledVoxels.keys(): arr = dataNodes[k].GetArray() baseline = 0 for bc in range(nBaselines): baseline += arr.GetComponent(bc, 1) baseline /= nBaselines if baseline != 0: for ic in range(nComponents): intensity = arr.GetComponent(ic, 1) percentChange = (intensity / baseline - 1) * 100. arr.SetComponent(ic, 1, percentChange) layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount() - 1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement( slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass( 'vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount() - 1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # setup color node colorNodeID = labelNode.GetDisplayNode().GetColorNodeID() colorNode = labelNode.GetDisplayNode().GetColorNode() lut = colorNode.GetLookupTable() # add initialized data nodes to the chart chartNode.ClearArrays() for k in labeledVoxels.keys(): name = colorNode.GetColorName(k) chartNode.AddArray(name, dataNodes[k].GetID()) rgb = lut.GetTableValue(int(k)) colorStr = self.RGBtoHex(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255) chartNode.SetProperty(name, "color", colorStr) tag = str( self.__mvNode.GetAttribute( 'MultiVolume.FrameIdentifyingDICOMTagName')) units = str( self.__mvNode.GetAttribute( 'MultiVolume.FrameIdentifyingDICOMTagUnits')) xTitle = tag + ', ' + units chartNode.SetProperty('default', 'xAxisLabel', xTitle) if self.iChartingPercent.checked: chartNode.SetProperty('default', 'yAxisLabel', 'change relative to baseline, %') else: chartNode.SetProperty('default', 'yAxisLabel', 'mean signal intensity') chartViewNode.SetChartNodeID(chartNode.GetID())
def chartTest(self): import math, random lns = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') lns.InitTraversal() ln = lns.GetNextItemAsObject() ln.SetViewArrangement(24) chartView = findChildren(className='qMRMLChartView')[0] print( chartView.connect("dataMouseOver(const char *,int,double,double)", self.chartMouseOverCallback)) print( chartView.connect( "dataPointClicked(const char *,int,double,double)", self.chartCallback)) cvns = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') cvns.InitTraversal() cvn = cvns.GetNextItemAsObject() dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) phase = random.random() for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.sin(phase + x[i] / 50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.cos(phase + x[i] / 50.0)) a.SetComponent(i, 2, 0) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) cn.AddArray('A double array', dn.GetID()) cn.AddArray('Another double array', dn2.GetID()) cvn.SetChartNodeID(cn.GetID()) cn.SetProperty('default', 'title', 'A simple chart with 2 curves') cn.SetProperty('default', 'xAxisLabel', 'Something in x') cn.SetProperty('default', 'yAxisLabel', 'Something in y') cvn.SetChartNodeID(cn.GetID()) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray('Just one array', dn.GetID()) cn.SetProperty('default', 'title', 'A simple chart with 1 curve') cn.SetProperty('default', 'xAxisLabel', 'Just x') cn.SetProperty('default', 'yAxisLabel', 'Just y') cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray('The other array', dn2.GetID()) cn.SetProperty('default', 'title', 'A simple chart with another curve') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('The other array', 'showLines', 'on') cn.SetProperty('The other array', 'showMarkers', 'off') cn.SetProperty('The other array', 'color', '#fe7d20') dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) print dn3.GetID() a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 4.0) a.SetComponent(i, 1, math.sin(x[i] / 4.0)) a.SetComponent(i, 2, 0) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray('Periodic', dn3.GetID()) cn.SetProperty('default', 'title', 'A bar chart') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('default', 'type', 'Bar')
def getSliceThk(self,meanProfile=False): profiles=self.getProfile(self.direction) self.spacing=self.input.GetSpacing()[self.direction] #todo: adjust if not meanProfile: profile=profiles[int(len(profiles)/2)] X=range(len(profile)) else: [X,profile]=self.meanTprofiles(profiles) if self.type==0: ramp=list(numpy.diff(profile)/numpy.diff(X)) else: ramp=profile FWHMres=self.getFWHM(ramp,X) self.FWHM=FWHMres[0] FWHMcurve=[[FWHMres[1],FWHMres[1],FWHMres[2],FWHMres[2]],[0,FWHMres[3],FWHMres[3],0]] layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.SetReferenceCount(layoutNodes.GetReferenceCount()-1) layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.SetReferenceCount(chartViewNodes.GetReferenceCount()-1) chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() ProfileNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) ProfileNode.SetName("Profile-"+self.input.GetName()+"-"+self.ROI.GetName()+"-array") self.arrayName=ProfileNode.GetName() Profile = ProfileNode.GetArray() Profile.SetNumberOfTuples(len(ramp)) for n in range(len(ramp)): Profile.SetComponent(n, 0, self.spacing*X[n]) Profile.SetComponent(n, 1, ramp[n]) FWHMNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) FWHMNode.SetName("FWHM-"+self.input.GetName()+"-"+self.ROI.GetName()+"-array") FWHMarray = FWHMNode.GetArray() FWHMarray.SetNumberOfTuples(len(FWHMcurve[0])) for n in range(len(FWHMcurve[0])): FWHMarray.SetComponent(n, 0, FWHMcurve[0][n]) FWHMarray.SetComponent(n, 1, FWHMcurve[1][n]) chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.SetName("Profile") chartNode.AddArray("Profile", ProfileNode.GetID()) chartNode.AddArray("FWHM", FWHMNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) print("set properties") chartNode.SetProperty('default', 'title', 'Profile') chartNode.SetProperty('default', 'xAxisLabel', 'Distance(mm)') chartNode.SetProperty('default', 'yAxisLabel', 'Profile') chartNode.SetProperty('default', 'type', 'Line') chartNode.SetProperty('default', 'showLegend', 'on') chartNode.SetProperty('default', 'Markers', 'on')
def test_Charting1(self): """ Testing charting """ self.delayDisplay("Starting the test") # # first, get some data # #import urllib #downloads = ( # ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume), # ) # #for url,name,loader in downloads: # filePath = slicer.app.temporaryPath + '/' + name # if not os.path.exists(filePath) or os.stat(filePath).st_size == 0: # print('Requesting download %s from %s...\n' % (name, url)) # urllib.urlretrieve(url, filePath) # if loader: # print('Loading %s...\n' % (name,)) # loader(filePath) #self.delayDisplay('Finished with download and loading\n') #volumeNode = slicer.util.getNode(pattern="FA") #logic = ChartingLogic() #self.assertTrue( logic.hasImageData(volumeNode) ) # Change the layout to one that has a chart. This created the ChartView ln = slicer.util.getNode(pattern='vtkMRMLLayoutNode*') ln.SetViewArrangement(24) # Get the first ChartView node cvn = slicer.util.getNode(pattern='vtkMRMLChartViewNode*') # Create arrays of data dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.sin(x[i] / 50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.cos(x[i] / 50.0)) a.SetComponent(i, 2, 0) # Create the ChartNode, cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('A double array', dn.GetID()) cn.AddArray('Another double array', dn2.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A simple chart with 2 curves') cn.SetProperty('default', 'xAxisLabel', 'Something in x') cn.SetProperty('default', 'yAxisLabel', 'Something in y') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with 2 curves') # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('Just one array', dn.GetID()) # Configure properties of the chart cn.SetProperty('default', 'title', 'A simple chart with 1 curve') cn.SetProperty('default', 'xAxisLabel', 'Just x') cn.SetProperty('default', 'yAxisLabel', 'Just y') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with 1 curve') # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('The other array', dn2.GetID()) # Set properties on the chart cn.SetProperty('default', 'title', 'A simple chart with another curve') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('The other array', 'showLines', 'on') cn.SetProperty('The other array', 'showMarkers', 'off') cn.SetProperty('The other array', 'color', '#fe7d20') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with another curve') # Create another data array dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 4.0) a.SetComponent(i, 1, math.sin(x[i] / 4.0)) a.SetComponent(i, 2, 0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray('Periodic', dn3.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A bar chart') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('default', 'type', 'Bar') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A bar chart') # self.delayDisplay('Test passed!')
def setup(self): ScriptedLoadableModuleWidget.setup(self) # Instantiate and connect widgets ... # # Parameters Area # parametersCollapsibleButton = ctk.ctkCollapsibleButton() parametersCollapsibleButton.text = "Parameters" self.layout.addWidget(parametersCollapsibleButton) # Layout within the dummy collapsible button parametersFormLayout = qt.QFormLayout(parametersCollapsibleButton) # # First volume selector # self.inputSelector1 = slicer.qMRMLNodeComboBox() self.inputSelector1.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" ) self.inputSelector1.addAttribute( "vtkMRMLScalarVolumeNode", "LabelMap", 0 ) self.inputSelector1.selectNodeUponCreation = True self.inputSelector1.addEnabled = False self.inputSelector1.removeEnabled = False self.inputSelector1.noneEnabled = False self.inputSelector1.showHidden = False self.inputSelector1.showChildNodeTypes = False self.inputSelector1.setMRMLScene( slicer.mrmlScene ) self.inputSelector1.setToolTip( "Pick the first input to the algorithm." ) parametersFormLayout.addRow("First Volume: ", self.inputSelector1) # # Second volume selector # self.inputSelector2 = slicer.qMRMLNodeComboBox() self.inputSelector2.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" ) self.inputSelector2.addAttribute( "vtkMRMLScalarVolumeNode", "LabelMap", 0 ) self.inputSelector2.selectNodeUponCreation = True self.inputSelector2.addEnabled = False self.inputSelector2.removeEnabled = False self.inputSelector2.noneEnabled = False self.inputSelector2.showHidden = False self.inputSelector2.showChildNodeTypes = False self.inputSelector2.setMRMLScene( slicer.mrmlScene ) self.inputSelector2.setToolTip( "Pick the second input to the algorithm." ) parametersFormLayout.addRow("Second Volume: ", self.inputSelector2) # # output volume selector # self.outputSelector = slicer.qMRMLNodeComboBox() self.outputSelector.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" ) self.outputSelector.addAttribute( "vtkMRMLScalarVolumeNode", "LabelMap", 0 ) self.outputSelector.selectNodeUponCreation = False self.outputSelector.addEnabled = True self.outputSelector.removeEnabled = True self.outputSelector.noneEnabled = False self.outputSelector.showHidden = False self.outputSelector.showChildNodeTypes = False self.outputSelector.setMRMLScene( slicer.mrmlScene ) self.outputSelector.setToolTip( "Pick the output to the algorithm." ) parametersFormLayout.addRow("Output Volume: ", self.outputSelector) # # Ruler selector # self.rulerSelector = slicer.qMRMLNodeComboBox () self.rulerSelector.nodeTypes = ( ("vtkMRMLAnnotationRulerNode"), "" ) self.rulerSelector.selectNodeUponCreation = True self.rulerSelector.addEnabled = False self.rulerSelector.removeEnabled = False self.rulerSelector.noneEnabled = False self.rulerSelector.showHidden = False self.rulerSelector.showChildNodeTypes = False self.rulerSelector.setMRMLScene( slicer.mrmlScene ) self.rulerSelector.setToolTip( "Pick the ruler to sample along." ) parametersFormLayout.addRow("Ruler: ", self.rulerSelector) # #Number of sample points parameter # self.numOfPointsField = qt.QSpinBox() self.numOfPointsField.setRange(1, 2000) self.numOfPointsField.value = 100 self.numOfPointsField.setSingleStep(1) parametersFormLayout.addRow("Num Of Points: ", self.numOfPointsField) # # Apply Button # self.applyButton = qt.QPushButton("Apply") self.applyButton.toolTip = "Run the algorithm." self.applyButton.enabled = True parametersFormLayout.addRow(self.applyButton) self.chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # connections self.applyButton.connect('clicked(bool)', self.onApplyButton) # Add vertical spacer self.layout.addStretch(1)
def chartTest(self): import math,random lns = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') lns.InitTraversal() ln = lns.GetNextItemAsObject() ln.SetViewArrangement(24) chartView = findChildren(className='qMRMLChartView')[0] print(chartView.connect("dataMouseOver(const char *,int,double,double)", self.chartMouseOverCallback)) print(chartView.connect("dataPointClicked(const char *,int,double,double)", self.chartCallback)) cvns = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') cvns.InitTraversal() cvn = cvns.GetNextItemAsObject() dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) phase = random.random() for i in range(len(x)): a.SetComponent(i, 0, x[i]/50.0) a.SetComponent(i, 1, math.sin(phase+x[i]/50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i]/50.0) a.SetComponent(i, 1, math.cos(phase+x[i]/50.0)) a.SetComponent(i, 2, 0) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) cn.AddArray('A double array', dn.GetID()) cn.AddArray('Another double array', dn2.GetID()) cvn.SetChartNodeID(cn.GetID()) cn.SetProperty('default', 'title', 'A simple chart with 2 curves') cn.SetProperty('default', 'xAxisLabel', 'Something in x') cn.SetProperty('default', 'yAxisLabel', 'Something in y') cvn.SetChartNodeID(cn.GetID()) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray('Just one array', dn.GetID()) cn.SetProperty('default', 'title', 'A simple chart with 1 curve') cn.SetProperty('default', 'xAxisLabel', 'Just x') cn.SetProperty('default', 'yAxisLabel', 'Just y') cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray('The other array', dn2.GetID()) cn.SetProperty('default', 'title', 'A simple chart with another curve') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('The other array', 'showLines', 'on') cn.SetProperty('The other array', 'showMarkers', 'off') cn.SetProperty('The other array', 'color', '#fe7d20') dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) print dn3.GetID() a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i]/4.0) a.SetComponent(i, 1, math.sin(x[i]/4.0)) a.SetComponent(i, 2, 0) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray('Periodic', dn3.GetID()) cn.SetProperty('default', 'title', 'A bar chart') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('default', 'type', 'Bar');
def test_Charting1(self): """ Testing charting """ self.delayDisplay("Starting the test") # # first, get some data # # import urllib # downloads = ( # ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume), # ) # # for url,name,loader in downloads: # filePath = slicer.app.temporaryPath + '/' + name # if not os.path.exists(filePath) or os.stat(filePath).st_size == 0: # print('Requesting download %s from %s...\n' % (name, url)) # urllib.urlretrieve(url, filePath) # if loader: # print('Loading %s...\n' % (name,)) # loader(filePath) # self.delayDisplay('Finished with download and loading\n') # volumeNode = slicer.util.getNode(pattern="FA") # logic = ChartingLogic() # self.assertTrue( logic.hasImageData(volumeNode) ) # Change the layout to one that has a chart. This created the ChartView ln = slicer.util.getNode(pattern="vtkMRMLLayoutNode*") ln.SetViewArrangement(24) # Get the first ChartView node cvn = slicer.util.getNode(pattern="vtkMRMLChartViewNode*") # Create arrays of data dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.sin(x[i] / 50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.cos(x[i] / 50.0)) a.SetComponent(i, 2, 0) # Create the ChartNode, cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray("A double array", dn.GetID()) cn.AddArray("Another double array", dn2.GetID()) # Configure properties of the Chart cn.SetProperty("default", "title", "A simple chart with 2 curves") cn.SetProperty("default", "xAxisLabel", "Something in x") cn.SetProperty("default", "yAxisLabel", "Something in y") # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A simple chart with 2 curves") # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray("Just one array", dn.GetID()) # Configure properties of the chart cn.SetProperty("default", "title", "A simple chart with 1 curve") cn.SetProperty("default", "xAxisLabel", "Just x") cn.SetProperty("default", "yAxisLabel", "Just y") # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A simple chart with 1 curve") # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray("The other array", dn2.GetID()) # Set properties on the chart cn.SetProperty("default", "title", "A simple chart with another curve") cn.SetProperty("default", "xAxisLabel", "time") cn.SetProperty("default", "yAxisLabel", "velocity") cn.SetProperty("The other array", "showLines", "on") cn.SetProperty("The other array", "showMarkers", "off") cn.SetProperty("The other array", "color", "#fe7d20") # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A simple chart with another curve") # Create another data array dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 4.0) a.SetComponent(i, 1, math.sin(x[i] / 4.0)) a.SetComponent(i, 2, 0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray("Periodic", dn3.GetID()) # Configure properties of the Chart cn.SetProperty("default", "title", "A bar chart") cn.SetProperty("default", "xAxisLabel", "time") cn.SetProperty("default", "yAxisLabel", "velocity") cn.SetProperty("default", "type", "Bar") # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A bar chart") # Test using a date axis # # Create another data array dn4 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn4.GetArray() # ugh, python uses localtime dates = [ "3/27/2010", "6/15/2010", "12/14/2010", "3/8/2011", "9/5/2011", "12/20/2011", "3/17/2012", "6/12/2012", "9/22/2012", "12/14/2012", "3/23/2012", ] # dates = ["3/27/2010","6/15/2010","9/21/2010","12/14/2010","3/8/2011","5/31/2011","9/5/2011","12/20/2011","3/17/2012","6/12/2012","9/22/2012","12/14/2012","3/23/2012"] times = [] for i in range(len(dates)): times.append(time.mktime(datetime.datetime.strptime(dates[i], "%m/%d/%Y").timetuple())) a.SetNumberOfTuples(len(times)) for i in range(len(times)): a.SetComponent(i, 0, times[i]) a.SetComponent(i, 1, math.sin(x[i] / 4.0) + 4) a.SetComponent(i, 2, 0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray("Lesion #1", dn4.GetID()) # Configure properties of the Chart cn.SetProperty("default", "title", "A chart with dates") cn.SetProperty("default", "xAxisLabel", "date") cn.SetProperty("default", "xAxisType", "date") cn.SetProperty("default", "yAxisLabel", "size (cm)") cn.SetProperty("default", "type", "Bar") # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A chart with dates") # Test using a color table to look up label names # # # Create another data array dn5 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn5.GetArray() a.SetNumberOfTuples(4) a.SetComponent(0, 0, 6) a.SetComponent(0, 1, 32) a.SetComponent(1, 0, 3) a.SetComponent(1, 1, 12) a.SetComponent(2, 0, 4) a.SetComponent(2, 1, 20) a.SetComponent(3, 0, 5) a.SetComponent(3, 1, 6) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray("Volumes", dn5.GetID()) # Configure properties of the Chart cn.SetProperty("default", "title", "A chart with labels") cn.SetProperty("default", "xAxisLabel", "structure") cn.SetProperty("default", "xAxisType", "categorical") cn.SetProperty("default", "yAxisLabel", "size (cm)") cn.SetProperty("default", "type", "Bar") cn.SetProperty("Volumes", "lookupTable", slicer.util.getNode("GenericAnatomyColors").GetID()) # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A chart with labels") # Test box plots # # # Create another data array dn6 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn6.GetArray() a.SetNumberOfTuples(40) for i in range(a.GetNumberOfTuples()): a.SetComponent(i, 0, 1) a.SetComponent(i, 1, (2.0 * random.random() - 0.5) + 20.0) # Create another data array dn7 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn7.GetArray() a.SetNumberOfTuples(25) for i in range(20): a.SetComponent(i, 0, 2) a.SetComponent(i, 1, 2.0 * (2.0 * random.random() - 1.0) + 27.0) for i in range(5): a.SetComponent(20 + i, 0, 2) a.SetComponent(20 + i, 1, 10.0 * (2.0 * random.random() - 1.0) + 27.0) # Create another data array dn8 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn8.GetArray() a.SetNumberOfTuples(25) for i in range(20): a.SetComponent(i, 0, 3) a.SetComponent(i, 1, 3.0 * (2.0 * random.random() - 1.0) + 24.0) for i in range(5): a.SetComponent(20 + i, 0, 2) a.SetComponent(20 + i, 1, 10.0 * (2.0 * random.random() - 1.0) + 24.0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray("Controls", dn6.GetID()) cn.AddArray("Group A", dn7.GetID()) cn.AddArray("Group B", dn8.GetID()) # Configure properties of the Chart cn.SetProperty("default", "title", "A box chart") cn.SetProperty("default", "xAxisLabel", "population") cn.SetProperty("default", "xAxisType", "categorical") cn.SetProperty("default", "yAxisLabel", "size (ml)") cn.SetProperty("default", "type", "Box") # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay("A box chart") # self.delayDisplay("Test passed!")
def test_LineIntensityProfile1(self): """ Ideally you should have several levels of tests. At the lowest level tests sould exercise the functionality of the logic with different inputs (both valid and invalid). At higher levels your tests should emulate the way the user would interact with your code and confirm that it still works the way you intended. One of the most important features of the tests is that it should alert other developers when their changes will have an impact on the behavior of your module. For example, if a developer removes a feature that you depend on, your test should break so they know that the feature is needed. """ self.delayDisplay("Starting the test") # # first, get some data # import urllib downloads = ( ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume), ) for url,name,loader in downloads: filePath = slicer.app.temporaryPath + '/' + name if not os.path.exists(filePath) or os.stat(filePath).st_size == 0: print('Requesting download %s from %s...\n' % (name, url)) urllib.urlretrieve(url, filePath) if loader: print('Loading %s...\n' % (name,)) loader(filePath) self.delayDisplay('Finished with download and loading\n') volumeNode = slicer.util.getNode(pattern="FA") logic = LineIntensityProfileLogic() self.assertTrue( logic.hasImageData(volumeNode) ) # initialize ruler node in a known location rulerNode = slicer.vtkMRMLAnnotationRulerNode() slicer.mrmlScene.AddNode(rulerNode) rulerNode.SetPosition1(-65,110,60) rulerNode.SetPosition2(-15,60,60) rulerNode.SetName('Test') numOfPointsField = qt.QSpinBox() numOfPointsField.value = 100 chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # initialize input selectors moduleWidget = slicer.modules.LineIntensityProfileWidget moduleWidget.rulerSelector.setCurrentNode(rulerNode) moduleWidget.inputSelector1.setCurrentNode(volumeNode) moduleWidget.inputSelector2.setCurrentNode(volumeNode) self.delayDisplay('Inputs initialized!') # run the logic with the initialized inputs moduleWidget.onApplyButton() self.delayDisplay('If you see a ruler and a plot - test passed!') # here we check the sample is correct or not GroundTruth = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 \ ,0.0499952547252 ,0.132161304355,0.157250195742,0.182541355491 ,0.224097684026,0.307691186666,0.421751081944,0.539702475071,0.566307783127,0.502181112766,0.466381758451 \ ,0.454870164394 ,0.451874107122 ,0.45154094696,0.445982009172,0.434947699308,0.422306239605,0.413488268852,0.409934103489,0.414202958345,0.409307956696 \ ,0.397122174501,0.397800117731,0.411369532347,0.429417848587,0.439514577389,0.423818171024,0.40110757947,0.386767745018,0.379366248846,0.370953738689,0.36441424489 \ ,0.36140397191,0.367857933044,0.38929861784,0.418379157782,0.408268928528,0.379027366638,0.332919150591,0.297374159098,0.296240001917,0.311986237764,0.350458532572 \ ,0.370930314064,0.357974469662,0.326200008392,0.316009640694,0.312490910292,0.32052642107,0.316521972418,0.279196560383,0.244257837534,0.238702788949,0.255041092634 \ ,0.280225723982,0.320494830608,0.28876376152,0.277780592442,0.253679126501,0.247220918536,0.244660764933] logic.run(volumeNode, volumeNode, rulerNode, numOfPointsField, chartNode) sample = logic.getImageSamples()[0] isTestValid = True for i in range(numOfPointsField.value): if abs(sample.GetTuple1(i)-float(GroundTruth[i])) > 0.000001: isTestValid = False if isTestValid: self.delayDisplay('Sample point test passed!') else: self.delayDisplay('Sample point test failed!')
def grafTiempo(self): # Switch to a layout (24) that contains a Chart View to initiate the construction of the widget and Chart View Node lns = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') lns.InitTraversal() ln = lns.GetNextItemAsObject() ln.SetViewArrangement(24) vectorintensidad = [] # Get the Chart View Node cvns = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') cvns.InitTraversal() cvn = cvns.GetNextItemAsObject() # Create an Array Node and add some data dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(27) #Volumen escena = slicer.mrmlScene #Se recupera la escena cargada volumen4D = self.inputSelector5.currentNode() imagenvtk4D = volumen4D.GetImageData() numero_imagenes = volumen4D.GetNumberOfFrames() for i in range(0, numero_imagenes, 1): extract2 = vtk.vtkImageExtractComponents() extract2.SetInputData(imagenvtk4D) imagen_movil = extract2.SetComponents(i) extract2.Update() #Matriz de transformacion ras2ijk = vtk.vtkMatrix4x4() ijk2ras = vtk.vtkMatrix4x4() #Se solicita al volumen original que devuelva las matrices volumen4D.GetRASToIJKMatrix(ras2ijk) volumen4D.GetIJKToRASMatrix(ijk2ras) #Se crea un volumen movil, y se realiza el mismo procedimiento que con el fijo volumenMovil = slicer.vtkMRMLScalarVolumeNode() volumenMovil.SetRASToIJKMatrix(ras2ijk) volumenMovil.SetIJKToRASMatrix(ijk2ras) volumenMovil.SetAndObserveImageData(extract2.GetOutput()) escena.AddNode(volumenMovil) z = slicer.util.arrayFromVolume(volumenMovil) m = np.mean(z[:]) vectorintensidad.append((m / 15000) - 1) for i in range(0, numero_imagenes): a.SetComponent(i, 0, i * 11.11) a.SetComponent(i, 1, vectorintensidad[i]) a.SetComponent(i, 2, 0) # Create a Chart Node. cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add the Array Nodes to the Chart. The first argument is a string used for the legend and to refer to the Array when setting properties. # Set a few properties on the Chart. The first argument is a string identifying which Array to assign the property. # 'default' is used to assign a property to the Chart itself (as opposed to an Array Node). cn.SetProperty('default', 'title', 'intesidad vs tiempo') cn.SetProperty('default', 'xAxisLabel', 'Tiempo(s)') cn.SetProperty('default', 'yAxisLabel', 'Intensidad') # Tell the Chart View which Chart to display cvn.SetChartNodeID(cn.GetID())
def chartTest(self): import math, random lns = slicer.mrmlScene.GetNodesByClass("vtkMRMLLayoutNode") lns.InitTraversal() ln = lns.GetNextItemAsObject() ln.SetViewArrangement(24) chartView = findChildren(className="qMRMLChartView")[0] print (chartView.connect("dataMouseOver(const char *,int,double,double)", self.chartMouseOverCallback)) print (chartView.connect("dataPointClicked(const char *,int,double,double)", self.chartCallback)) cvns = slicer.mrmlScene.GetNodesByClass("vtkMRMLChartViewNode") cvns.InitTraversal() cvn = cvns.GetNextItemAsObject() dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) phase = random.random() for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.sin(phase + x[i] / 50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 50.0) a.SetComponent(i, 1, math.cos(phase + x[i] / 50.0)) a.SetComponent(i, 2, 0) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) cn.AddArray("A double array", dn.GetID()) cn.AddArray("Another double array", dn2.GetID()) cvn.SetChartNodeID(cn.GetID()) cn.SetProperty("default", "title", "A simple chart with 2 curves") cn.SetProperty("default", "xAxisLabel", "Something in x") cn.SetProperty("default", "yAxisLabel", "Something in y") cvn.SetChartNodeID(cn.GetID()) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray("Just one array", dn.GetID()) cn.SetProperty("default", "title", "A simple chart with 1 curve") cn.SetProperty("default", "xAxisLabel", "Just x") cn.SetProperty("default", "yAxisLabel", "Just y") cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray("The other array", dn2.GetID()) cn.SetProperty("default", "title", "A simple chart with another curve") cn.SetProperty("default", "xAxisLabel", "time") cn.SetProperty("default", "yAxisLabel", "velocity") cn.SetProperty("The other array", "showLines", "on") cn.SetProperty("The other array", "showMarkers", "off") cn.SetProperty("The other array", "color", "#fe7d20") dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) print dn3.GetID() a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i] / 4.0) a.SetComponent(i, 1, math.sin(x[i] / 4.0)) a.SetComponent(i, 2, 0) cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) print cn.GetID() cn.AddArray("Periodic", dn3.GetID()) cn.SetProperty("default", "title", "A bar chart") cn.SetProperty("default", "xAxisLabel", "time") cn.SetProperty("default", "yAxisLabel", "velocity") cn.SetProperty("default", "type", "Bar")
def test_Charting1(self): """ Testing charting """ self.delayDisplay("Starting the test") # # first, get some data # #import urllib #downloads = ( # ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume), # ) # #for url,name,loader in downloads: # filePath = slicer.app.temporaryPath + '/' + name # if not os.path.exists(filePath) or os.stat(filePath).st_size == 0: # print('Requesting download %s from %s...\n' % (name, url)) # urllib.urlretrieve(url, filePath) # if loader: # print('Loading %s...\n' % (name,)) # loader(filePath) #self.delayDisplay('Finished with download and loading\n') #volumeNode = slicer.util.getNode(pattern="FA") #logic = ChartingLogic() #self.assertTrue( logic.hasImageData(volumeNode) ) # Change the layout to one that has a chart. This created the ChartView ln = slicer.util.getNode(pattern='vtkMRMLLayoutNode*') ln.SetViewArrangement(24) # Get the first ChartView node cvn = slicer.util.getNode(pattern='vtkMRMLChartViewNode*') # Create arrays of data dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i]/50.0) a.SetComponent(i, 1, math.sin(x[i]/50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i]/50.0) a.SetComponent(i, 1, math.cos(x[i]/50.0)) a.SetComponent(i, 2, 0) # Create the ChartNode, cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('A double array', dn.GetID()) cn.AddArray('Another double array', dn2.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A simple chart with 2 curves') cn.SetProperty('default', 'xAxisLabel', 'Something in x') cn.SetProperty('default', 'yAxisLabel', 'Something in y') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with 2 curves') # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('Just one array', dn.GetID()) # Configure properties of the chart cn.SetProperty('default', 'title', 'A simple chart with 1 curve') cn.SetProperty('default', 'xAxisLabel', 'Just x') cn.SetProperty('default', 'yAxisLabel', 'Just y') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with 1 curve') # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('The other array', dn2.GetID()) # Set properties on the chart cn.SetProperty('default', 'title', 'A simple chart with another curve') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('The other array', 'showLines', 'on') cn.SetProperty('The other array', 'showMarkers', 'off') cn.SetProperty('The other array', 'color', '#fe7d20') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with another curve') # Create another data array dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i]/4.0) a.SetComponent(i, 1, math.sin(x[i]/4.0)) a.SetComponent(i, 2, 0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray('Periodic', dn3.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A bar chart') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('default', 'type', 'Bar'); # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A bar chart') # Test using a date axis # # Create another data array dn4 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn4.GetArray() # ugh, python uses localtime dates = ["3/27/2010","6/15/2010","12/14/2010","3/8/2011","9/5/2011","12/20/2011","3/17/2012","6/12/2012","9/22/2012","12/14/2012","3/23/2012"] #dates = ["3/27/2010","6/15/2010","9/21/2010","12/14/2010","3/8/2011","5/31/2011","9/5/2011","12/20/2011","3/17/2012","6/12/2012","9/22/2012","12/14/2012","3/23/2012"] times = [] for i in range(len(dates)): times.append(time.mktime(datetime.datetime.strptime(dates[i], "%m/%d/%Y").timetuple())) a.SetNumberOfTuples(len(times)) for i in range(len(times)): a.SetComponent(i, 0, times[i]) a.SetComponent(i, 1, math.sin(x[i]/4.0) + 4) a.SetComponent(i, 2, 0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray('Lesion #1', dn4.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A chart with dates') cn.SetProperty('default', 'xAxisLabel', 'date') cn.SetProperty('default', 'xAxisType', 'date') cn.SetProperty('default', 'yAxisLabel', 'size (cm)') cn.SetProperty('default', 'type', 'Bar'); # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A chart with dates') # Test using a color table to look up label names # # # Create another data array dn5 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn5.GetArray() a.SetNumberOfTuples(4) a.SetComponent(0, 0, 6) a.SetComponent(0, 1, 32) a.SetComponent(1, 0, 3) a.SetComponent(1, 1, 12) a.SetComponent(2, 0, 4) a.SetComponent(2, 1, 20) a.SetComponent(3, 0, 5) a.SetComponent(3, 1, 6) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray('Volumes', dn5.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A chart with labels') cn.SetProperty('default', 'xAxisLabel', 'structure') cn.SetProperty('default', 'xAxisType', 'categorical') cn.SetProperty('default', 'yAxisLabel', 'size (cm)') cn.SetProperty('default', 'type', 'Bar'); cn.SetProperty('Volumes', 'lookupTable', slicer.util.getNode('GenericAnatomyColors').GetID() ) # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A chart with labels') # Test box plots # # # Create another data array dn6 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn6.GetArray() a.SetNumberOfTuples(40) for i in range(a.GetNumberOfTuples()): a.SetComponent(i, 0, 1) a.SetComponent(i, 1, (2.0*random.random() - 0.5) + 20.0) # Create another data array dn7 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn7.GetArray() a.SetNumberOfTuples(25) for i in range(20): a.SetComponent(i, 0, 2) a.SetComponent(i, 1, 2.0*(2.0*random.random()-1.0) + 27.0) for i in range(5): a.SetComponent(20+i, 0, 2) a.SetComponent(20+i, 1, 10.0*(2.0*random.random()-1.0) + 27.0) # Create another data array dn8 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn8.GetArray() a.SetNumberOfTuples(25) for i in range(20): a.SetComponent(i, 0, 3) a.SetComponent(i, 1, 3.0*(2.0*random.random()-1.0) + 24.0) for i in range(5): a.SetComponent(20+i, 0, 2) a.SetComponent(20+i, 1, 10.0*(2.0*random.random()-1.0) + 24.0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray('Controls', dn6.GetID()) cn.AddArray('Group A', dn7.GetID()) cn.AddArray('Group B', dn8.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A box chart') cn.SetProperty('default', 'xAxisLabel', 'population') cn.SetProperty('default', 'xAxisType', 'categorical') cn.SetProperty('default', 'yAxisLabel', 'size (ml)') cn.SetProperty('default', 'type', 'Box'); # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A box chart') # self.delayDisplay('Test passed!')
def test_Charting1(self): """ Testing charting """ self.delayDisplay("Starting the test") # # first, get some data # #import urllib #downloads = ( # ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume), # ) # #for url,name,loader in downloads: # filePath = slicer.app.temporaryPath + '/' + name # if not os.path.exists(filePath) or os.stat(filePath).st_size == 0: # print('Requesting download %s from %s...\n' % (name, url)) # urllib.urlretrieve(url, filePath) # if loader: # print('Loading %s...\n' % (name,)) # loader(filePath) #self.delayDisplay('Finished with download and loading\n') #volumeNode = slicer.util.getNode(pattern="FA") #logic = ChartingLogic() #self.assertTrue( logic.hasImageData(volumeNode) ) # Change the layout to one that has a chart. This created the ChartView ln = slicer.util.getNode(pattern='vtkMRMLLayoutNode*') ln.SetViewArrangement(24) # Get the first ChartView node cvn = slicer.util.getNode(pattern='vtkMRMLChartViewNode*') # Create arrays of data dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i]/50.0) a.SetComponent(i, 1, math.sin(x[i]/50.0)) a.SetComponent(i, 2, 0) dn2 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn2.GetArray() a.SetNumberOfTuples(600) x = range(0, 600) for i in range(len(x)): a.SetComponent(i, 0, x[i]/50.0) a.SetComponent(i, 1, math.cos(x[i]/50.0)) a.SetComponent(i, 2, 0) # Create the ChartNode, cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('A double array', dn.GetID()) cn.AddArray('Another double array', dn2.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A simple chart with 2 curves') cn.SetProperty('default', 'xAxisLabel', 'Something in x') cn.SetProperty('default', 'yAxisLabel', 'Something in y') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with 2 curves') # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('Just one array', dn.GetID()) # Configure properties of the chart cn.SetProperty('default', 'title', 'A simple chart with 1 curve') cn.SetProperty('default', 'xAxisLabel', 'Just x') cn.SetProperty('default', 'yAxisLabel', 'Just y') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with 1 curve') # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the Chart cn.AddArray('The other array', dn2.GetID()) # Set properties on the chart cn.SetProperty('default', 'title', 'A simple chart with another curve') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('The other array', 'showLines', 'on') cn.SetProperty('The other array', 'showMarkers', 'off') cn.SetProperty('The other array', 'color', '#fe7d20') # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A simple chart with another curve') # Create another data array dn3 = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) a = dn3.GetArray() a.SetNumberOfTuples(12) x = range(0, 12) for i in range(len(x)): a.SetComponent(i, 0, x[i]/4.0) a.SetComponent(i, 1, math.sin(x[i]/4.0)) a.SetComponent(i, 2, 0) # Create another ChartNode cn = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) # Add data to the chart cn.AddArray('Periodic', dn3.GetID()) # Configure properties of the Chart cn.SetProperty('default', 'title', 'A bar chart') cn.SetProperty('default', 'xAxisLabel', 'time') cn.SetProperty('default', 'yAxisLabel', 'velocity') cn.SetProperty('default', 'type', 'Bar'); # Set the chart to display cvn.SetChartNodeID(cn.GetID()) self.delayDisplay('A bar chart') # self.delayDisplay('Test passed!')
def onCalculateLength(self): self.inputPolyData = self.vtkNode.GetPolyData() points = self.inputPolyData.GetPoints() lines = self.inputPolyData.GetLines() lines.InitTraversal() self.distanceTable = list() for i in range(self.inputPolyData.GetNumberOfCells()): fiberLength = 0 ids = vtk.vtkIdList() lines.GetNextCell(ids) #print(ids.GetNumberOfIds()) for j in range(ids.GetNumberOfIds() - 1): point1 = [0,0,0] point2 = [0,0,0] points.GetPoint(ids.GetId(j), point1) points.GetPoint(ids.GetId(j+1), point2) x = point2[0] - point1[0] y = point2[1] - point1[1] z = point2[2] - point1[2] step = (x*x + y*y + z*z)**.5 fiberLength += step self.distanceTable.append(fiberLength) min,max=self.getDistanceBound() self.thresholdMin.setValue(min) self.thresholdMin.enabled = True self.thresholdMax.setValue(max+1) self.thresholdMax.enabled = True self.applyThresholdButton.enabled = True layoutNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLayoutNode') layoutNodes.InitTraversal() layoutNode = layoutNodes.GetNextItemAsObject() layoutNode.SetViewArrangement(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalQuantitativeView) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() array.SetNumberOfTuples(10) step = (max-min)/10 interMin = min interMax = min+step for i in range(10): numberOfFibers = 0 for length in self.distanceTable: if length<=interMax and length>=interMin and length<=self.thresholdMax.value and length>=self.thresholdMin.value: numberOfFibers += 1 array.SetComponent(i,0,(interMin+interMax)/2) array.SetComponent(i,1,numberOfFibers) array.SetComponent(i,2,0) interMin += step interMax += step chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray("Fiber Length",arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Length Distribution') chartNode.SetProperty('default', 'xAxisLabel', 'Length') chartNode.SetProperty('default', 'yAxisLabel', 'Distribution') chartNode.SetProperty('default', 'type', 'Bar')
def onApplyThreshold(self): min,max = self.getDistanceBound() newPoints = vtk.vtkPoints() newLines = vtk.vtkCellArray() newTensors = vtk.vtkFloatArray() newTensors.SetNumberOfComponents(9) newScalars = vtk.vtkFloatArray() points = self.inputPolyData.GetPoints() lines = self.inputPolyData.GetLines() tensors = self.inputPolyData.GetPointData().GetTensors() lines.InitTraversal() newId = 0 for length in self.distanceTable: if length<=self.thresholdMax.value and length>=self.thresholdMin.value: ids = vtk.vtkIdList() lines.GetNextCell(ids) newLine = vtk.vtkPolyLine() #print(ids.GetNumberOfIds()) newLine.GetPointIds().SetNumberOfIds(ids.GetNumberOfIds()) #print(((length-min)/(max-min))*100) for i in range(ids.GetNumberOfIds()): newPoints.InsertNextPoint(points.GetPoint(ids.GetId(i))) newLine.GetPointIds().SetId(i,newId) newScalars.InsertNextValue(((length-min)/(max-min))) newId += 1 tensorValue = [0]*9 if(tensors != None): for j in range(9): tensorValue[j] = tensors.GetComponent(ids.GetId(i),j) newTensors.InsertNextTuple(tensorValue) newLines.InsertNextCell(newLine) self.outputPolyData = vtk.vtkPolyData() self.outputPolyData.SetPoints(newPoints) self.outputPolyData.SetLines(newLines) self.outputPolyData.GetPointData().SetTensors(newTensors) newScalars.SetName("Length") self.outputPolyData.GetPointData().AddArray(newScalars) self.outputNode.SetAndObservePolyData(self.outputPolyData) chartViewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode') chartViewNodes.InitTraversal() chartViewNode = chartViewNodes.GetNextItemAsObject() arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode()) array = arrayNode.GetArray() array.SetNumberOfTuples(10) step = (max-min)/10 interMin = min interMax = min+step for i in range(10): numberOfFibers = 0 for length in self.distanceTable: if length<=interMax and length>=interMin and length<=self.thresholdMax.value and length>=self.thresholdMin.value: numberOfFibers += 1 array.SetComponent(i,0,(interMin+interMax)/2) array.SetComponent(i,1,numberOfFibers) array.SetComponent(i,2,0) interMin += step interMax += step chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode()) chartNode.AddArray("Fiber Length",arrayNode.GetID()) chartViewNode.SetChartNodeID(chartNode.GetID()) chartNode.SetProperty('default', 'title', 'Length Distribution') chartNode.SetProperty('default', 'xAxisLabel', 'Length') chartNode.SetProperty('default', 'yAxisLabel', 'Distribution') chartNode.SetProperty('default', 'type', 'Bar')