本文整理汇总了Python中SegmentStatistics.SegmentStatisticsLogic.exportToTable方法的典型用法代码示例。如果您正苦于以下问题:Python SegmentStatisticsLogic.exportToTable方法的具体用法?Python SegmentStatisticsLogic.exportToTable怎么用?Python SegmentStatisticsLogic.exportToTable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SegmentStatistics.SegmentStatisticsLogic
的用法示例。
在下文中一共展示了SegmentStatisticsLogic.exportToTable方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_SegmentStatisticsBasic
# 需要导入模块: from SegmentStatistics import SegmentStatisticsLogic [as 别名]
# 或者: from SegmentStatistics.SegmentStatisticsLogic import exportToTable [as 别名]
def test_SegmentStatisticsBasic(self):
"""
This tests some aspects of the label statistics
"""
self.delayDisplay("Starting test_SegmentStatisticsBasic")
import vtkSegmentationCorePython as vtkSegmentationCore
import SampleData
from SegmentStatistics import SegmentStatisticsLogic
self.delayDisplay("Load master volume")
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
self.delayDisplay("Create segmentation containing a few spheres")
segmentationNode = slicer.vtkMRMLSegmentationNode()
slicer.mrmlScene.AddNode(segmentationNode)
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# Geometry for each segment is defined by: radius, posX, posY, posZ
segmentGeometries = [[10, -6,30,28], [20, 0,65,32], [15, 1, -14, 30], [12, 0, 28, -7], [5, 0,30,64],
[12, 31, 33, 27], [17, -42, 30, 27]]
for segmentGeometry in segmentGeometries:
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(segmentGeometry[0])
sphereSource.SetCenter(segmentGeometry[1], segmentGeometry[2], segmentGeometry[3])
sphereSource.Update()
uniqueSegmentID = segmentationNode.GetSegmentation().GenerateUniqueSegmentID("Test")
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(sphereSource.GetOutput(), uniqueSegmentID)
self.delayDisplay("Compute statistics")
segStatLogic = SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
segStatLogic.computeStatistics()
self.delayDisplay("Check a few numerical results")
self.assertEqual( segStatLogic.getStatistics()["Test_2","LabelmapSegmentStatisticsPlugin.voxel_count"], 9807)
self.assertEqual( segStatLogic.getStatistics()["Test_4","ScalarVolumeSegmentStatisticsPlugin.voxel_count"], 380)
self.delayDisplay("Export results to table")
resultsTableNode = slicer.vtkMRMLTableNode()
slicer.mrmlScene.AddNode(resultsTableNode)
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
self.delayDisplay("Export results to string")
logging.info(segStatLogic.exportToString())
outputFilename = slicer.app.temporaryPath + '/SegmentStatisticsTestOutput.csv'
self.delayDisplay("Export results to CSV file: "+outputFilename)
segStatLogic.exportToCSVFile(outputFilename)
self.delayDisplay('test_SegmentStatisticsBasic passed!')
示例2: exportToTable
# 需要导入模块: from SegmentStatistics import SegmentStatisticsLogic [as 别名]
# 或者: from SegmentStatistics.SegmentStatisticsLogic import exportToTable [as 别名]
def exportToTable(self, table=None, nonEmptyKeysOnly=True):
if not table:
table = slicer.vtkMRMLTableNode()
table.SetName(slicer.mrmlScene.GenerateUniqueName(self.grayscaleNode.GetName() + ' statistics'))
slicer.mrmlScene.AddNode(table)
table.SetUseColumnNameAsColumnHeader(True)
SegmentStatisticsLogic.exportToTable(self, table, nonEmptyKeysOnly)
return table
示例3: test_ScriptedSegmentEditorEffectModuleTemplate1
# 需要导入模块: from SegmentStatistics import SegmentStatisticsLogic [as 别名]
# 或者: from SegmentStatistics.SegmentStatisticsLogic import exportToTable [as 别名]
def test_ScriptedSegmentEditorEffectModuleTemplate1(self):
"""
Basic automated test of the segmentation method:
- Create segmentation by placing sphere-shaped seeds
- Run segmentation
- Verify results using segment statistics
The test can be executed from SelfTests module (test name: SegmentEditorScriptedSegmentEditorEffectModuleTemplate)
"""
self.delayDisplay("Starting test_ScriptedSegmentEditorEffectModuleTemplate1")
import vtkSegmentationCorePython as vtkSegmentationCore
import vtkSlicerSegmentationsModuleLogicPython as vtkSlicerSegmentationsModuleLogic
import SampleData
from SegmentStatistics import SegmentStatisticsLogic
##################################
self.delayDisplay("Load master volume")
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
##################################
self.delayDisplay("Create segmentation containing a few spheres")
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# Segments are defined by a list of: name and a list of sphere [radius, posX, posY, posZ]
segmentGeometries = [
['Tumor', [[10, -6,30,28]]],
['Background', [[10, 0,65,22], [15, 1, -14, 30], [12, 0, 28, -7], [5, 0,30,54], [12, 31, 33, 27], [17, -42, 30, 27], [6, -2,-17,71]]],
['Air', [[10, 76,73,0], [15, -70,74,0]]] ]
for segmentGeometry in segmentGeometries:
segmentName = segmentGeometry[0]
appender = vtk.vtkAppendPolyData()
for sphere in segmentGeometry[1]:
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(sphere[0])
sphereSource.SetCenter(sphere[1], sphere[2], sphere[3])
appender.AddInputConnection(sphereSource.GetOutputPort())
segment = vtkSegmentationCore.vtkSegment()
segment.SetName(segmentationNode.GetSegmentation().GenerateUniqueSegmentID(segmentName))
appender.Update()
segment.AddRepresentation(vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationClosedSurfaceRepresentationName(), appender.GetOutput())
segmentationNode.GetSegmentation().AddSegment(segment)
##################################
self.delayDisplay("Create segment editor")
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.show()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
##################################
self.delayDisplay("Run segmentation")
segmentEditorWidget.setActiveEffectByName("ScriptedSegmentEditorEffectModuleTemplate")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("ObjectScaleMm", 3.0)
effect.self().onApply()
##################################
self.delayDisplay("Make segmentation results nicely visible in 3D")
segmentationDisplayNode = segmentationNode.GetDisplayNode()
segmentationDisplayNode.SetSegmentVisibility("Air", False)
segmentationDisplayNode.SetSegmentOpacity3D("Background",0.5)
##################################
self.delayDisplay("Compute statistics")
segStatLogic = SegmentStatisticsLogic()
segStatLogic.computeStatistics(segmentationNode, masterVolumeNode)
# Export results to table (just to see all results)
resultsTableNode = slicer.vtkMRMLTableNode()
slicer.mrmlScene.AddNode(resultsTableNode)
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
self.delayDisplay("Check a few numerical results")
self.assertEqual( round(segStatLogic.statistics["Tumor","LM volume cc"]), 16)
self.assertEqual( round(segStatLogic.statistics["Background","LM volume cc"]), 3010)
self.delayDisplay('test_ScriptedSegmentEditorEffectModuleTemplate1 passed')
示例4: test_SegmentStatisticsPlugins
# 需要导入模块: from SegmentStatistics import SegmentStatisticsLogic [as 别名]
# 或者: from SegmentStatistics.SegmentStatisticsLogic import exportToTable [as 别名]
def test_SegmentStatisticsPlugins(self):
"""
This tests some aspects of the segment statistics plugins
"""
self.delayDisplay("Starting test_SegmentStatisticsPlugins")
import vtkSegmentationCorePython as vtkSegmentationCore
import SampleData
from SegmentStatistics import SegmentStatisticsLogic
self.delayDisplay("Load master volume")
masterVolumeNode = SampleData.downloadSample('MRBrainTumor1')
self.delayDisplay("Create segmentation containing a few spheres")
segmentationNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# Geometry for each segment is defined by: radius, posX, posY, posZ
segmentGeometries = [[10, -6,30,28], [20, 0,65,32], [15, 1, -14, 30], [12, 0, 28, -7], [5, 0,30,64],
[12, 31, 33, 27], [17, -42, 30, 27]]
for segmentGeometry in segmentGeometries:
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(segmentGeometry[0])
sphereSource.SetCenter(segmentGeometry[1], segmentGeometry[2], segmentGeometry[3])
sphereSource.Update()
segment = vtkSegmentationCore.vtkSegment()
uniqueSegmentID = segmentationNode.GetSegmentation().GenerateUniqueSegmentID("Test")
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(sphereSource.GetOutput(), uniqueSegmentID)
# test calculating only measurements for selected segments
self.delayDisplay("Test calculating only measurements for individual segments")
segStatLogic = SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
segStatLogic.updateStatisticsForSegment('Test_2')
resultsTableNode = slicer.vtkMRMLTableNode()
slicer.mrmlScene.AddNode(resultsTableNode)
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
self.assertEqual( segStatLogic.getStatistics()["Test_2","LabelmapSegmentStatisticsPlugin.voxel_count"], 9807)
with self.assertRaises(KeyError): segStatLogic.getStatistics()["Test_4","ScalarVolumeSegmentStatisticsPlugin.voxel count"]
# assert there are no result for this segment
segStatLogic.updateStatisticsForSegment('Test_4')
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
self.assertEqual( segStatLogic.getStatistics()["Test_2","LabelmapSegmentStatisticsPlugin.voxel_count"], 9807)
self.assertEqual( segStatLogic.getStatistics()["Test_4","LabelmapSegmentStatisticsPlugin.voxel_count"], 380)
with self.assertRaises(KeyError): segStatLogic.getStatistics()["Test_5","ScalarVolumeSegmentStatisticsPlugin.voxel count"]
# assert there are no result for this segment
# calculate measurements for all segments
segStatLogic.computeStatistics()
self.assertEqual( segStatLogic.getStatistics()["Test","LabelmapSegmentStatisticsPlugin.voxel_count"], 2948)
self.assertEqual( segStatLogic.getStatistics()["Test_1","LabelmapSegmentStatisticsPlugin.voxel_count"], 23281)
# test updating measurements for segments one by one
self.delayDisplay("Update some segments in the segmentation")
segmentGeometriesNew = [[5, -6,30,28], [21, 0,65,32]]
# We add/remove representations, so we temporarily block segment modifications
# to make sure display managers don't try to access data while it is in an
# inconsistent state.
wasModified = segmentationNode.StartModify()
for i in range(len(segmentGeometriesNew)):
segmentGeometry = segmentGeometriesNew[i]
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(segmentGeometry[0])
sphereSource.SetCenter(segmentGeometry[1], segmentGeometry[2], segmentGeometry[3])
sphereSource.Update()
segment = segmentationNode.GetSegmentation().GetNthSegment(i)
segment.RemoveAllRepresentations()
closedSurfaceName = vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationClosedSurfaceRepresentationName()
segment.AddRepresentation(closedSurfaceName,
sphereSource.GetOutput())
segmentationNode.EndModify(wasModified)
self.assertEqual( segStatLogic.getStatistics()["Test","LabelmapSegmentStatisticsPlugin.voxel_count"], 2948)
self.assertEqual( segStatLogic.getStatistics()["Test_1","LabelmapSegmentStatisticsPlugin.voxel_count"], 23281)
segStatLogic.updateStatisticsForSegment('Test_1')
self.assertEqual( segStatLogic.getStatistics()["Test","LabelmapSegmentStatisticsPlugin.voxel_count"], 2948)
self.assertTrue( segStatLogic.getStatistics()["Test_1","LabelmapSegmentStatisticsPlugin.voxel_count"]!=23281)
segStatLogic.updateStatisticsForSegment('Test')
self.assertTrue( segStatLogic.getStatistics()["Test","LabelmapSegmentStatisticsPlugin.voxel_count"]!=2948)
self.assertTrue( segStatLogic.getStatistics()["Test_1","LabelmapSegmentStatisticsPlugin.voxel_count"]!=23281)
# test enabling/disabling of individual measurements
self.delayDisplay("Test disabling of individual measurements")
segStatLogic = SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.voxel_count.enabled",str(False))
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.volume_cm3.enabled",str(False))
segStatLogic.computeStatistics()
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
columnHeaders = [resultsTableNode.GetColumnName(i) for i in range(resultsTableNode.GetNumberOfColumns())]
self.assertFalse('Number of voxels [voxels] (1)' in columnHeaders)
self.assertTrue('Volume [mm3] (1)' in columnHeaders)
#.........这里部分代码省略.........
示例5: SegmentStatisticsWidget
# 需要导入模块: from SegmentStatistics import SegmentStatisticsLogic [as 别名]
# 或者: from SegmentStatistics.SegmentStatisticsLogic import exportToTable [as 别名]
#.........这里部分代码省略.........
if self.segmentationSelector.currentNode() is None:
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")
self.segmentationSelector.setCurrentNode(segmentationNode)
def cleanup(self):
if self.parameterNode and self.parameterNodeObserver:
self.parameterNode.RemoveObserver(self.parameterNodeObserver)
def onNodeSelectionChanged(self):
self.applyButton.enabled = (self.segmentationSelector.currentNode() is not None and
self.parameterNodeSelector.currentNode() is not None)
if self.segmentationSelector.currentNode():
self.outputTableSelector.baseName = self.segmentationSelector.currentNode().GetName() + ' statistics'
def onApply(self):
"""Calculate the label statistics
"""
if not self.outputTableSelector.currentNode():
newTable = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode")
self.outputTableSelector.setCurrentNode(newTable)
# Lock GUI
self.applyButton.text = "Working..."
self.applyButton.setEnabled(False)
slicer.app.processEvents()
# set up parameters for computation
self.logic.getParameterNode().SetParameter("Segmentation", self.segmentationSelector.currentNode().GetID())
if self.scalarSelector.currentNode():
self.logic.getParameterNode().SetParameter("ScalarVolume", self.scalarSelector.currentNode().GetID())
else:
self.logic.getParameterNode().UnsetParameter("ScalarVolume")
self.logic.getParameterNode().SetParameter("MeasurementsTable", self.outputTableSelector.currentNode().GetID())
# Compute statistics
self.logic.computeStatistics()
self.logic.exportToTable(self.outputTableSelector.currentNode())
# Unlock GUI
self.applyButton.setEnabled(True)
self.applyButton.text = "Apply"
self.logic.showTable(self.outputTableSelector.currentNode())
def onEditParameters(self, pluginName=None):
"""Open dialog box to edit plugin's parameters"""
if self.parameterNodeSelector.currentNode():
SegmentStatisticsParameterEditorDialog.editParameters(self.parameterNodeSelector.currentNode(),pluginName)
def addPluginOptionWidgets(self):
self.pluginEnabledCheckboxes = {}
self.parametersLayout.addRow(qt.QLabel("Enabled segment statistics plugins:"))
for plugin in self.logic.plugins:
checkbox = qt.QCheckBox(plugin.name+" Statistics")
checkbox.checked = True
checkbox.connect('stateChanged(int)', self.updateParameterNodeFromGui)
optionButton = qt.QPushButton("Options")
from functools import partial
optionButton.connect('clicked()',partial(self.onEditParameters, plugin.name))
editWidget = qt.QWidget()
editWidget.setLayout(qt.QHBoxLayout())
editWidget.layout().margin = 0
editWidget.layout().addWidget(checkbox, 0)
editWidget.layout().addStretch(1)
editWidget.layout().addWidget(optionButton, 0)
self.pluginEnabledCheckboxes[plugin.name] = checkbox
self.parametersLayout.addRow(editWidget)
# embed widgets for editing plugin' parameters
#for plugin in self.logic.plugins:
# pluginOptionsCollapsibleButton = ctk.ctkCollapsibleGroupBox()
示例6: SegmentStatisticsWidget
# 需要导入模块: from SegmentStatistics import SegmentStatisticsLogic [as 别名]
# 或者: from SegmentStatistics.SegmentStatisticsLogic import exportToTable [as 别名]
class SegmentStatisticsWidget(ScriptedLoadableModuleWidget):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def setup(self):
ScriptedLoadableModuleWidget.setup(self)
self.logic = SegmentStatisticsLogic()
self.grayscaleNode = None
self.labelNode = None
self.fileName = None
self.fileDialog = None
# Instantiate and connect widgets ...
#
# Inputs
inputsCollapsibleButton = ctk.ctkCollapsibleButton()
inputsCollapsibleButton.text = "Inputs"
self.layout.addWidget(inputsCollapsibleButton)
inputsFormLayout = qt.QFormLayout(inputsCollapsibleButton)
# Segmentation selector
self.segmentationSelector = slicer.qMRMLNodeComboBox()
self.segmentationSelector.nodeTypes = ["vtkMRMLSegmentationNode"]
self.segmentationSelector.addEnabled = False
self.segmentationSelector.removeEnabled = True
self.segmentationSelector.renameEnabled = True
self.segmentationSelector.setMRMLScene( slicer.mrmlScene )
self.segmentationSelector.setToolTip( "Pick the segmentation to compute statistics for" )
inputsFormLayout.addRow("Segmentation:", self.segmentationSelector)
# Grayscale volume selector
self.grayscaleSelector = slicer.qMRMLNodeComboBox()
self.grayscaleSelector.nodeTypes = ["vtkMRMLScalarVolumeNode"]
self.grayscaleSelector.addEnabled = False
self.grayscaleSelector.removeEnabled = True
self.grayscaleSelector.renameEnabled = True
self.grayscaleSelector.noneEnabled = True
self.grayscaleSelector.showChildNodeTypes = False
self.grayscaleSelector.setMRMLScene( slicer.mrmlScene )
self.grayscaleSelector.setToolTip( "Select the grayscale volume for intensity statistics calculations")
inputsFormLayout.addRow("Grayscale volume:", self.grayscaleSelector)
# Output table selector
outputCollapsibleButton = ctk.ctkCollapsibleButton()
outputCollapsibleButton.text = "Output"
self.layout.addWidget(outputCollapsibleButton)
outputFormLayout = qt.QFormLayout(outputCollapsibleButton)
self.outputTableSelector = slicer.qMRMLNodeComboBox()
self.outputTableSelector.nodeTypes = ["vtkMRMLTableNode"]
self.outputTableSelector.addEnabled = True
self.outputTableSelector.selectNodeUponCreation = True
self.outputTableSelector.renameEnabled = True
self.outputTableSelector.removeEnabled = True
self.outputTableSelector.noneEnabled = False
self.outputTableSelector.setMRMLScene( slicer.mrmlScene )
self.outputTableSelector.setToolTip( "Select the table where statistics will be saved into")
outputFormLayout.addRow("Output table:", self.outputTableSelector)
# Apply Button
self.applyButton = qt.QPushButton("Apply")
self.applyButton.toolTip = "Calculate Statistics."
self.applyButton.enabled = False
self.parent.layout().addWidget(self.applyButton)
# Add vertical spacer
self.parent.layout().addStretch(1)
# connections
self.applyButton.connect('clicked()', self.onApply)
self.grayscaleSelector.connect('currentNodeChanged(vtkMRMLNode*)', self.onNodeSelectionChanged)
self.segmentationSelector.connect('currentNodeChanged(vtkMRMLNode*)', self.onNodeSelectionChanged)
self.outputTableSelector.connect('currentNodeChanged(vtkMRMLNode*)', self.onNodeSelectionChanged)
self.onNodeSelectionChanged()
def onNodeSelectionChanged(self):
self.applyButton.enabled = (self.segmentationSelector.currentNode() is not None) and (self.outputTableSelector.currentNode() is not None)
if self.segmentationSelector.currentNode():
self.outputTableSelector.baseName = self.segmentationSelector.currentNode().GetName() + ' statistics'
def onApply(self):
"""Calculate the label statistics
"""
# Lock GUI
self.applyButton.text = "Working..."
self.applyButton.setEnabled(False)
slicer.app.processEvents()
# Compute statistics
self.logic.computeStatistics(self.segmentationSelector.currentNode(), self.grayscaleSelector.currentNode())
self.logic.exportToTable(self.outputTableSelector.currentNode())
# Unlock GUI
self.applyButton.setEnabled(True)
self.applyButton.text = "Apply"
self.logic.showTable(self.outputTableSelector.currentNode())