parameters: (self)
docstring: This tests some aspects of the segment statistics plugins
docstring_summary: This tests some aspects of the segment statistics plugins
docstring_tokens: This tests some aspects of the segment statistics plugins
function: 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)
      self.assertFalse('Volume [cm3] (3)' in columnHeaders)
  
      self.delayDisplay("Test re-enabling of individual measurements")
      segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.voxel_count.enabled",str(True))
      segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.volume_cm3.enabled",str(True))
      segStatLogic.computeStatistics()
      segStatLogic.exportToTable(resultsTableNode)
      segStatLogic.showTable(resultsTableNode)
      columnHeaders = [resultsTableNode.GetColumnName(i) for i in range(resultsTableNode.GetNumberOfColumns())]
      self.assertTrue('Number of voxels [voxels] (1)' in columnHeaders)
      self.assertTrue('Volume [mm3] (1)' in columnHeaders)
      self.assertTrue('Volume [cm3] (1)' in columnHeaders)
  
      # test enabling/disabling of individual plugins
      self.delayDisplay("Test disabling of plugin")
      segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.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] (3)' in columnHeaders)
      self.assertFalse('Volume [mm3] (3)' in columnHeaders)
      self.assertTrue('Volume [mm3] (2)' in columnHeaders)
  
      self.delayDisplay("Test re-enabling of plugin")
      segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled",str(True))
      segStatLogic.computeStatistics()
      segStatLogic.exportToTable(resultsTableNode)
      segStatLogic.showTable(resultsTableNode)
      columnHeaders = [resultsTableNode.GetColumnName(i) for i in range(resultsTableNode.GetNumberOfColumns())]
      self.assertTrue('Number of voxels [voxels] (2)' in columnHeaders)
      self.assertTrue('Volume [mm3] (3)' in columnHeaders)
  
      # test unregistering/registering of plugins
      self.delayDisplay("Test of removing all registered plugins")
      SegmentStatisticsLogic.registeredPlugins = [] # remove all registered plugins
      segStatLogic = SegmentStatisticsLogic()
      segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
      segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
      segStatLogic.computeStatistics()
      segStatLogic.exportToTable(resultsTableNode)
      segStatLogic.showTable(resultsTableNode)
      columnHeaders = [resultsTableNode.GetColumnName(i) for i in range(resultsTableNode.GetNumberOfColumns())]
      self.assertEqual(len(columnHeaders),1) # only header element should be "Segment"
      self.assertEqual(columnHeaders[0],"Segment") # only header element should be "Segment"
  
      self.delayDisplay("Test registering plugins")
      SegmentStatisticsLogic.registerPlugin(LabelmapSegmentStatisticsPlugin())
      SegmentStatisticsLogic.registerPlugin(ScalarVolumeSegmentStatisticsPlugin())
      SegmentStatisticsLogic.registerPlugin(ClosedSurfaceSegmentStatisticsPlugin())
      segStatLogic = SegmentStatisticsLogic()
      segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
      segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
      segStatLogic.computeStatistics()
      segStatLogic.exportToTable(resultsTableNode)
      segStatLogic.showTable(resultsTableNode)
      columnHeaders = [resultsTableNode.GetColumnName(i) for i in range(resultsTableNode.GetNumberOfColumns())]
      self.assertTrue('Number of voxels [voxels] (1)' in columnHeaders)
      self.assertTrue('Number of voxels [voxels] (2)' in columnHeaders)
      self.assertTrue('Surface area [mm2]' in columnHeaders)
  
      self.delayDisplay('test_SegmentStatisticsPlugins passed!')
function_tokens: def test_SegmentStatisticsPlugins ( self ) : 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 ) self . assertFalse ( 'Volume [cm3] (3)' in columnHeaders ) self . delayDisplay ( "Test re-enabling of individual measurements" ) segStatLogic . getParameterNode ( ) . SetParameter ( "LabelmapSegmentStatisticsPlugin.voxel_count.enabled" , str ( True ) ) segStatLogic . getParameterNode ( ) . SetParameter ( "LabelmapSegmentStatisticsPlugin.volume_cm3.enabled" , str ( True ) ) segStatLogic . computeStatistics ( ) segStatLogic . exportToTable ( resultsTableNode ) segStatLogic . showTable ( resultsTableNode ) columnHeaders = [ resultsTableNode . GetColumnName ( i ) for i in range ( resultsTableNode . GetNumberOfColumns ( ) ) ] self . assertTrue ( 'Number of voxels [voxels] (1)' in columnHeaders ) self . assertTrue ( 'Volume [mm3] (1)' in columnHeaders ) self . assertTrue ( 'Volume [cm3] (1)' in columnHeaders ) # test enabling/disabling of individual plugins self . delayDisplay ( "Test disabling of plugin" ) segStatLogic . getParameterNode ( ) . SetParameter ( "LabelmapSegmentStatisticsPlugin.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] (3)' in columnHeaders ) self . assertFalse ( 'Volume [mm3] (3)' in columnHeaders ) self . assertTrue ( 'Volume [mm3] (2)' in columnHeaders ) self . delayDisplay ( "Test re-enabling of plugin" ) segStatLogic . getParameterNode ( ) . SetParameter ( "LabelmapSegmentStatisticsPlugin.enabled" , str ( True ) ) segStatLogic . computeStatistics ( ) segStatLogic . exportToTable ( resultsTableNode ) segStatLogic . showTable ( resultsTableNode ) columnHeaders = [ resultsTableNode . GetColumnName ( i ) for i in range ( resultsTableNode . GetNumberOfColumns ( ) ) ] self . assertTrue ( 'Number of voxels [voxels] (2)' in columnHeaders ) self . assertTrue ( 'Volume [mm3] (3)' in columnHeaders ) # test unregistering/registering of plugins self . delayDisplay ( "Test of removing all registered plugins" ) SegmentStatisticsLogic . registeredPlugins = [ ] # remove all registered plugins segStatLogic = SegmentStatisticsLogic ( ) segStatLogic . getParameterNode ( ) . SetParameter ( "Segmentation" , segmentationNode . GetID ( ) ) segStatLogic . getParameterNode ( ) . SetParameter ( "ScalarVolume" , masterVolumeNode . GetID ( ) ) segStatLogic . computeStatistics ( ) segStatLogic . exportToTable ( resultsTableNode ) segStatLogic . showTable ( resultsTableNode ) columnHeaders = [ resultsTableNode . GetColumnName ( i ) for i in range ( resultsTableNode . GetNumberOfColumns ( ) ) ] self . assertEqual ( len ( columnHeaders ) , 1 ) # only header element should be "Segment" self . assertEqual ( columnHeaders [ 0 ] , "Segment" ) # only header element should be "Segment" self . delayDisplay ( "Test registering plugins" ) SegmentStatisticsLogic . registerPlugin ( LabelmapSegmentStatisticsPlugin ( ) ) SegmentStatisticsLogic . registerPlugin ( ScalarVolumeSegmentStatisticsPlugin ( ) ) SegmentStatisticsLogic . registerPlugin ( ClosedSurfaceSegmentStatisticsPlugin ( ) ) segStatLogic = SegmentStatisticsLogic ( ) segStatLogic . getParameterNode ( ) . SetParameter ( "Segmentation" , segmentationNode . GetID ( ) ) segStatLogic . getParameterNode ( ) . SetParameter ( "ScalarVolume" , masterVolumeNode . GetID ( ) ) segStatLogic . computeStatistics ( ) segStatLogic . exportToTable ( resultsTableNode ) segStatLogic . showTable ( resultsTableNode ) columnHeaders = [ resultsTableNode . GetColumnName ( i ) for i in range ( resultsTableNode . GetNumberOfColumns ( ) ) ] self . assertTrue ( 'Number of voxels [voxels] (1)' in columnHeaders ) self . assertTrue ( 'Number of voxels [voxels] (2)' in columnHeaders ) self . assertTrue ( 'Surface area [mm2]' in columnHeaders ) self . delayDisplay ( 'test_SegmentStatisticsPlugins passed!' )