def TestSection_1_AddRemoveSegment(self):
    # Add/remove segment from segmentation (check display properties, color table, etc.)
    logging.info('Test section 1: Add/remove segment')

    # Get baseline values
    displayNode = self.inputSegmentationNode.GetDisplayNode()
    self.assertIsNotNone(displayNode)
    colorTableNode = displayNode.GetColorNode()
    self.assertIsNotNone(colorTableNode)
    self.assertEqual(colorTableNode.GetNumberOfColors(), 4)
    # If segments are not found then the returned color is the pre-defined invalid color
    bodyColor = displayNode.GetSegmentColor('Body_Contour')
    self.assertTrue(int(bodyColor[0]*100) == 33 and int(bodyColor[1]*100) == 66 and bodyColor[2] == 0.0)
    tumorColor = displayNode.GetSegmentColor('Tumor_Contour')
    self.assertTrue(tumorColor[0] == 1.0 and tumorColor[1] == 0.0 and tumorColor[2] == 0.0)

    # Create new segment
    sphere = vtk.vtkSphereSource()
    sphere.SetCenter(0,50,0)
    sphere.SetRadius(50)
    sphere.Update()
    spherePolyData = vtk.vtkPolyData()
    spherePolyData.DeepCopy(sphere.GetOutput())
    
    self.sphereSegment = vtkSegmentationCore.vtkSegment()
    self.sphereSegment.SetName(self.sphereSegmentName)
    self.sphereSegment.SetDefaultColor(0.0,0.0,1.0)
    self.sphereSegment.AddRepresentation(self.closedSurfaceReprName, spherePolyData)

    # Add segment to segmentation
    self.inputSegmentationNode.GetSegmentation().AddSegment(self.sphereSegment)
    self.assertEqual(self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(), 3)
    self.assertEqual(colorTableNode.GetNumberOfColors(), 5)
    sphereColor = displayNode.GetSegmentColor(self.sphereSegmentName)
    self.assertTrue(sphereColor[0] == 0.0 and sphereColor[1] == 0.0 and sphereColor[2] == 1.0)
    
    # Check merged labelmap
    imageStat = vtk.vtkImageAccumulate()
    imageStat.SetInputData(self.inputSegmentationNode.GetImageData())
    imageStat.SetComponentExtent(0,4,0,0,0,0)
    imageStat.SetComponentOrigin(0,0,0)
    imageStat.SetComponentSpacing(1,1,1)
    imageStat.Update()
    self.assertEqual(imageStat.GetVoxelCount(), 1000)
    imageStatResult = imageStat.GetOutput()
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(0,0,0,0), 814)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(1,0,0,0), 0)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(2,0,0,0), 175)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(3,0,0,0), 4)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(4,0,0,0), 7)

    # Remove segment from segmentation
    self.inputSegmentationNode.GetSegmentation().RemoveSegment(self.sphereSegmentName)
    self.assertEqual(self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(), 2)
    self.assertEqual(colorTableNode.GetNumberOfColors(), 5)
    sphereColorArray = [0]*4
    colorTableNode.GetColor(4,sphereColorArray)
    self.assertTrue(int(sphereColorArray[0]*100) == 50 and int(sphereColorArray[1]*100) == 50 and int(sphereColorArray[2]*100) == 50)
    sphereColor = displayNode.GetSegmentColor(self.sphereSegmentName)
    self.assertTrue(sphereColor[0] == 0.5 and sphereColor[1] == 0.5 and sphereColor[2] == 0.5)
Ejemplo n.º 2
0
 def add_layer(self):
     # add layer to slicer
     self.inputSegmentationNode = slicer.mrmlScene.AddNewNodeByClass(
         'vtkMRMLSegmentationNode')
     for layer in self.layer:
         Segment = vtkSegmentationCore.vtkSegment()
         Segment.SetName(layer[0])
         Segment.SetColor(layer[1])
         self.inputSegmentationNode.GetSegmentation().AddSegment(Segment)
    def TestSection_1_AddRemoveSegment(self):
        # Add/remove segment from segmentation (check display properties, color table, etc.)
        logging.info('Test section 1: Add/remove segment')

        # Get baseline values
        displayNode = self.inputSegmentationNode.GetDisplayNode()
        self.assertIsNotNone(displayNode)
        colorTableNode = displayNode.GetColorNode()
        self.assertIsNotNone(colorTableNode)
        self.assertEqual(colorTableNode.GetNumberOfColors(), 4)
        # If segments are not found then the returned color is the pre-defined invalid color
        bodyColor = displayNode.GetSegmentColor('Body_Contour')
        self.assertTrue(
            int(bodyColor[0] * 100) == 33 and int(bodyColor[1] * 100) == 66
            and bodyColor[2] == 0.0)
        tumorColor = displayNode.GetSegmentColor('Tumor_Contour')
        self.assertTrue(tumorColor[0] == 1.0 and tumorColor[1] == 0.0
                        and tumorColor[2] == 0.0)

        # Create new segment
        sphere = vtk.vtkSphereSource()
        sphere.SetCenter(0, 50, 0)
        sphere.SetRadius(50)
        sphere.Update()
        spherePolyData = vtk.vtkPolyData()
        spherePolyData.DeepCopy(sphere.GetOutput())

        self.sphereSegment = vtkSegmentationCore.vtkSegment()
        self.sphereSegment.SetName(self.sphereSegmentName)
        self.sphereSegment.SetDefaultColor(0.0, 0.0, 1.0)
        self.sphereSegment.AddRepresentation(self.closedSurfaceReprName,
                                             spherePolyData)

        # Add segment to segmentation
        self.inputSegmentationNode.GetSegmentation().AddSegment(
            self.sphereSegment)
        self.assertEqual(
            self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(),
            3)
        self.assertEqual(colorTableNode.GetNumberOfColors(), 5)
        sphereColor = displayNode.GetSegmentColor(self.sphereSegmentName)
        self.assertTrue(sphereColor[0] == 0.0 and sphereColor[1] == 0.0
                        and sphereColor[2] == 1.0)

        # Check merged labelmap
        imageStat = vtk.vtkImageAccumulate()
        imageStat.SetInputData(self.inputSegmentationNode.GetImageData())
        imageStat.SetComponentExtent(0, 4, 0, 0, 0, 0)
        imageStat.SetComponentOrigin(0, 0, 0)
        imageStat.SetComponentSpacing(1, 1, 1)
        imageStat.Update()
        self.assertEqual(imageStat.GetVoxelCount(), 1000)
        imageStatResult = imageStat.GetOutput()
        self.assertEqual(
            imageStatResult.GetScalarComponentAsDouble(0, 0, 0, 0), 814)
        self.assertEqual(
            imageStatResult.GetScalarComponentAsDouble(1, 0, 0, 0), 0)
        self.assertEqual(
            imageStatResult.GetScalarComponentAsDouble(2, 0, 0, 0), 175)
        self.assertEqual(
            imageStatResult.GetScalarComponentAsDouble(3, 0, 0, 0), 4)
        self.assertEqual(
            imageStatResult.GetScalarComponentAsDouble(4, 0, 0, 0), 7)

        # Remove segment from segmentation
        self.inputSegmentationNode.GetSegmentation().RemoveSegment(
            self.sphereSegmentName)
        self.assertEqual(
            self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(),
            2)
        self.assertEqual(colorTableNode.GetNumberOfColors(), 5)
        sphereColorArray = [0] * 4
        colorTableNode.GetColor(4, sphereColorArray)
        self.assertTrue(
            int(sphereColorArray[0] * 100) == 50
            and int(sphereColorArray[1] * 100) == 50
            and int(sphereColorArray[2] * 100) == 50)
        sphereColor = displayNode.GetSegmentColor(self.sphereSegmentName)
        self.assertTrue(sphereColor[0] == 0.5 and sphereColor[1] == 0.5
                        and sphereColor[2] == 0.5)
Ejemplo n.º 4
0
    def TestSection_AddRemoveSegment(self):
        # Add/remove segment from segmentation (check display properties, color table, etc.)
        logging.info('Test section: Add/remove segment')

        # Get baseline values
        displayNode = self.inputSegmentationNode.GetDisplayNode()
        self.assertIsNotNone(displayNode)
        # If segments are not found then the returned color is the pre-defined invalid color
        bodyColor = self.inputSegmentationNode.GetSegmentation().GetSegment(self.bodySegmentName).GetColor()
        logging.info(f"bodyColor: {bodyColor}")
        self.assertEqual(int(bodyColor[0] * 100), 33)
        self.assertEqual(int(bodyColor[1] * 100), 66)
        self.assertEqual(int(bodyColor[2] * 100), 0)
        tumorColor = self.inputSegmentationNode.GetSegmentation().GetSegment(self.tumorSegmentName).GetColor()
        logging.info(f"tumorColor: {tumorColor}")
        self.assertEqual(int(tumorColor[0] * 100), 100)
        self.assertEqual(int(tumorColor[1] * 100), 0)
        self.assertEqual(int(tumorColor[2] * 100), 0)

        # Create new segment
        sphere = vtk.vtkSphereSource()
        sphere.SetCenter(0, 50, 0)
        sphere.SetRadius(80)
        sphere.Update()
        spherePolyData = vtk.vtkPolyData()
        spherePolyData.DeepCopy(sphere.GetOutput())

        self.sphereSegment = vtkSegmentationCore.vtkSegment()
        self.sphereSegment.SetName(self.sphereSegmentName)
        self.sphereSegment.SetColor(0.0, 0.0, 1.0)
        self.sphereSegment.AddRepresentation(self.closedSurfaceReprName, spherePolyData)

        # Add segment to segmentation
        self.inputSegmentationNode.GetSegmentation().AddSegment(self.sphereSegment)
        self.assertEqual(self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(), 3)

        # Check merged labelmap
        mergedLabelmap = vtkSegmentationCore.vtkOrientedImageData()
        self.inputSegmentationNode.GetSegmentation().CreateRepresentation(self.binaryLabelmapReprName)
        self.inputSegmentationNode.GenerateMergedLabelmapForAllSegments(mergedLabelmap, 0)
        imageStat = vtk.vtkImageAccumulate()
        imageStat.SetInputData(mergedLabelmap)
        imageStat.SetComponentExtent(0, 4, 0, 0, 0, 0)
        imageStat.SetComponentOrigin(0, 0, 0)
        imageStat.SetComponentSpacing(1, 1, 1)
        imageStat.Update()
        imageStatResult = imageStat.GetOutput()
        for i in range(4):
            logging.info(f"Volume {i}: {imageStatResult.GetScalarComponentAsDouble(i,0,0,0)}")
        self.assertEqual(imageStat.GetVoxelCount(), 1000)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(0, 0, 0, 0), 786)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(1, 0, 0, 0), 170)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(2, 0, 0, 0), 4)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(3, 0, 0, 0), 40)

        # Check if segment reorder is taken into account in merged labelmap generation
        # Change segment order
        sphereSegmentId = self.inputSegmentationNode.GetSegmentation().GetSegmentIdBySegment(self.sphereSegment)
        self.inputSegmentationNode.GetSegmentation().SetSegmentIndex(sphereSegmentId, 1)
        # Re-generate merged labelmap
        self.inputSegmentationNode.GenerateMergedLabelmapForAllSegments(mergedLabelmap, 0)
        imageStat.SetInputData(mergedLabelmap)
        imageStat.Update()
        imageStatResult = imageStat.GetOutput()
        for i in range(4):
            logging.info(f"Volume {i}: {imageStatResult.GetScalarComponentAsDouble(i,0,0,0)}")
        self.assertEqual(imageStat.GetVoxelCount(), 1000)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(0, 0, 0, 0), 786)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(1, 0, 0, 0), 170)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(2, 0, 0, 0), 39)
        self.assertEqual(imageStatResult.GetScalarComponentAsDouble(3, 0, 0, 0), 5)

        # Remove segment from segmentation
        self.inputSegmentationNode.GetSegmentation().RemoveSegment(self.sphereSegmentName)
        self.assertEqual(self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(), 2)
Ejemplo n.º 5
0
  def TestSection_AddRemoveSegment(self):
    # Add/remove segment from segmentation (check display properties, color table, etc.)
    logging.info('Test section: Add/remove segment')

    # Get baseline values
    displayNode = self.inputSegmentationNode.GetDisplayNode()
    self.assertIsNotNone(displayNode)
    # If segments are not found then the returned color is the pre-defined invalid color
    bodyColor = self.inputSegmentationNode.GetSegmentation().GetSegment(self.bodySegmentName).GetColor()
    logging.info("bodyColor: {0}".format(bodyColor))
    self.assertEqual(int(bodyColor[0]*100), 33)
    self.assertEqual(int(bodyColor[1]*100), 66)
    self.assertEqual(int(bodyColor[2]*100), 0)
    tumorColor = self.inputSegmentationNode.GetSegmentation().GetSegment(self.tumorSegmentName).GetColor()
    logging.info("tumorColor: {0}".format(tumorColor))
    self.assertEqual(int(tumorColor[0]*100), 100)
    self.assertEqual(int(tumorColor[1]*100), 0)
    self.assertEqual(int(tumorColor[2]*100), 0)

    # Create new segment
    sphere = vtk.vtkSphereSource()
    sphere.SetCenter(0,50,0)
    sphere.SetRadius(80)
    sphere.Update()
    spherePolyData = vtk.vtkPolyData()
    spherePolyData.DeepCopy(sphere.GetOutput())

    self.sphereSegment = vtkSegmentationCore.vtkSegment()
    self.sphereSegment.SetName(self.sphereSegmentName)
    self.sphereSegment.SetColor(0.0,0.0,1.0)
    self.sphereSegment.AddRepresentation(self.closedSurfaceReprName, spherePolyData)

    # Add segment to segmentation
    self.inputSegmentationNode.GetSegmentation().AddSegment(self.sphereSegment)
    self.assertEqual(self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(), 3)

    # Check merged labelmap
    mergedLabelmap = vtkSegmentationCore.vtkOrientedImageData()
    self.inputSegmentationNode.GetSegmentation().CreateRepresentation(self.binaryLabelmapReprName)
    self.inputSegmentationNode.GenerateMergedLabelmapForAllSegments(mergedLabelmap, 0)
    imageStat = vtk.vtkImageAccumulate()
    imageStat.SetInputData(mergedLabelmap)
    imageStat.SetComponentExtent(0,4,0,0,0,0)
    imageStat.SetComponentOrigin(0,0,0)
    imageStat.SetComponentSpacing(1,1,1)
    imageStat.Update()
    imageStatResult = imageStat.GetOutput()
    for i in range(4):
      logging.info("Volume {0}: {1}".format(i, imageStatResult.GetScalarComponentAsDouble(i,0,0,0)))
    self.assertEqual(imageStat.GetVoxelCount(), 1000)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(0,0,0,0), 786)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(1,0,0,0), 170)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(2,0,0,0), 4)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(3,0,0,0), 40)

    # Check if segment reorder is taken into account in merged labelmap generation
    # Change segment order
    sphereSegmentId = self.inputSegmentationNode.GetSegmentation().GetSegmentIdBySegment(self.sphereSegment)
    self.inputSegmentationNode.GetSegmentation().SetSegmentIndex(sphereSegmentId, 1)
    # Re-generate merged labelmap
    self.inputSegmentationNode.GenerateMergedLabelmapForAllSegments(mergedLabelmap, 0)
    imageStat.SetInputData(mergedLabelmap)
    imageStat.Update()
    imageStatResult = imageStat.GetOutput()
    for i in range(4):
      logging.info("Volume {0}: {1}".format(i, imageStatResult.GetScalarComponentAsDouble(i,0,0,0)))
    self.assertEqual(imageStat.GetVoxelCount(), 1000)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(0,0,0,0), 786)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(1,0,0,0), 170)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(2,0,0,0), 39)
    self.assertEqual(imageStatResult.GetScalarComponentAsDouble(3,0,0,0), 5)

    # Remove segment from segmentation
    self.inputSegmentationNode.GetSegmentation().RemoveSegment(self.sphereSegmentName)
    self.assertEqual(self.inputSegmentationNode.GetSegmentation().GetNumberOfSegments(), 2)
layers = []
for label in labels:
    print(label)
    layers.append([
        label,
        [
            random.uniform(0.0, 1.0),
            random.uniform(0.0, 1.0),
            random.uniform(0.0, 1.0)
        ]
    ])
print layers[1]

# In[11]:

inputSegmentationNode = slicer.mrmlScene.AddNewNodeByClass(
    'vtkMRMLSegmentationNode')

# In[12]:

Segment = vtkSegmentationCore.vtkSegment()
for layer in layers:
    print layer[0]
    print layer[1]
    Segment = vtkSegmentationCore.vtkSegment()
    Segment.SetName(layer[0])
    Segment.SetColor(layer[1])
    inputSegmentationNode.GetSegmentation().AddSegment(Segment)

# In[ ]:
  def load(self,loadable):
    """ Load the DICOM SEG object
    """
    print('DICOM SEG load()')
    labelNodes = vtk.vtkCollection()

    uid = None

    try:
      uid = loadable.uid
      print ('in load(): uid = ', uid)
    except AttributeError:
      return False

    res = False
    # make the output directory
    outputDir = os.path.join(slicer.app.temporaryPath,"QIICR","SEG",loadable.uid)
    try:
      os.makedirs(outputDir)
    except:
      pass

    # produces output label map files, one per segment, and information files with
    # the terminology information for each segment
    segFileName = slicer.dicomDatabase.fileForInstance(uid)
    if segFileName is None:
      print 'Failed to get the filename from the DICOM database for ', uid
      return False

    parameters = {
      "inputSEGFileName": segFileName,
      "outputDirName": outputDir,
      }
    seg2nrrd = None
    try:
      seg2nrrd = slicer.modules.seg2nrrd
    except AttributeError:
      print 'Unable to find CLI module SEG2NRRD, unable to load DICOM Segmentation object'
      return False
    
    cliNode = None
    cliNode = slicer.cli.run(seg2nrrd, cliNode, parameters, wait_for_completion=True)
    if cliNode.GetStatusString() != 'Completed':
      print 'SEG2NRRD did not complete successfully, unable to load DICOM Segmentation'
      return False

    # create a new color node to be set up with the colors in these segments
    colorLogic = slicer.modules.colors.logic()
    segmentationColorNode = slicer.vtkMRMLColorTableNode()
    segmentationColorNode.SetName(loadable.name)
    segmentationColorNode.SetTypeToUser();
    segmentationColorNode.SetHideFromEditors(0)
    segmentationColorNode.SetAttribute("Category", "File")
    segmentationColorNode.NamesInitialisedOff()
    slicer.mrmlScene.AddNode(segmentationColorNode)

    # also create a new terminology and associate it with this color node
    colorLogic.CreateNewTerminology(segmentationColorNode.GetName())

    import glob
    numberOfSegments = len(glob.glob(os.path.join(outputDir,'*.nrrd')))

    # resize the color table to include the segments plus 0 for the background
    print ('number of segments = ',numberOfSegments)
    segmentationColorNode.SetNumberOfColors(numberOfSegments + 1)
    segmentationColorNode.SetColor(0, 'background', 0.0, 0.0, 0.0, 0.0)

    seriesName = self.referencedSeriesName(loadable)
    segmentNodes = []
    for segmentId in range(numberOfSegments):
      # load each of the segments' segmentations
      # Initialize color and terminology from .info file
      # See SEG2NRRD.cxx and EncodeSEG.cxx for how it's written.
      # Format of the .info file (no leading spaces, labelNum, RGBColor, SegmentedPropertyCategory and
      # SegmentedPropertyCategory are required):
      # labelNum;RGB:R,G,B;SegmentedPropertyCategory:code,scheme,meaning;SegmentedPropertyType:code,scheme,meaning;SegmentedPropertyTypeModifier:code,scheme,meaning;AnatomicRegion:code,scheme,meaning;AnatomicRegionModifier:code,scheme,meaning
      # R, G, B are 0-255 in file, but mapped to 0-1 for use in color node
      # set defaults in case of missing fields, modifiers are optional
      rgb = (0., 0., 0.)
      colorIndex = segmentId + 1
      categoryCode = 'T-D0050'
      categoryCodingScheme = 'SRT'
      categoryCodeMeaning = 'Tissue'
      typeCode = 'T-D0050'
      typeCodeMeaning = 'Tissue'
      typeCodingScheme = 'SRT'
      typeModCode = ''
      typeModCodingScheme = ''
      typeModCodeMeaning = ''
      regionCode = 'T-D0010'
      regionCodingScheme = 'SRT'
      regionCodeMeaning = 'Entire Body'
      regionModCode = ''
      regionModCodingScheme = ''
      regionModCodeMeaning = ''
      infoFileName = os.path.join(outputDir,str(segmentId+1)+".info")
      print ('Parsing info file', infoFileName)
      with open(infoFileName, 'r') as infoFile:
        for line in infoFile:
          line = line.rstrip()
          if len(line) == 0:
            # empty line
            continue
          terms = line.split(';')
          for term in terms:
            # label number is the first thing, no key
            if len(term.split(':')) == 1:
              colorIndex = int(term)
            else:
              key = term.split(':')[0]
              if key == "RGB":
                rgb255 = term.split(':')[1].split(',')
                rgb = map(lambda c: float(c) / 255., rgb255)
              elif key == "AnatomicRegion":
                # Get the Region information
                region = term.split(':')[1]
                # use partition to deal with any commas in the meaning
                regionCode, sep, regionCodingSchemeAndCodeMeaning = region.partition(',')
                regionCodingScheme, sep, regionCodeMeaning = regionCodingSchemeAndCodeMeaning.partition(',')
              elif key == "AnatomicRegionModifier":
                regionMod = term.split(':')[1]
                regionModCode, sep, regionModCodingSchemeAndCodeMeaning = regionMod.partition(',')
                regionModCodingScheme, sep, regionModCodeMeaning = regionModCodingSchemeAndCodeMeaning.partition(',')
              elif key == "SegmentedPropertyCategory":
                # Get the Category information
                category = term.split(':')[1]
                categoryCode, sep, categoryCodingSchemeAndCodeMeaning = category.partition(',')
                categoryCodingScheme, sep, categoryCodeMeaning = categoryCodingSchemeAndCodeMeaning.partition(',')
              elif key == "SegmentedPropertyType":
                # Get the Type information
                types = term.split(':')[1]
                typeCode, sep, typeCodingSchemeAndCodeMeaning = types.partition(',')
                typeCodingScheme, sep, typeCodeMeaning = typeCodingSchemeAndCodeMeaning.partition(',')
              elif key == "SegmentedPropertyTypeModifier":
                typeMod = term.split(':')[1]
                typeModCode, sep, typeModCodingSchemeAndCodeMeaning = typeMod.partition(',')
                typeModCodingScheme, sep, typeModCodeMeaning = typeModCodingSchemeAndCodeMeaning.partition(',')

        # set the color name from the terminology
        colorName = typeCodeMeaning
        segmentationColorNode.SetColor(colorIndex, colorName, *rgb)

        colorLogic.AddTermToTerminology(segmentationColorNode.GetName(), colorIndex,
                                        categoryCode, categoryCodingScheme, categoryCodeMeaning,
                                        typeCode, typeCodingScheme, typeCodeMeaning,
                                        typeModCode, typeModCodingScheme, typeModCodeMeaning,
                                        regionCode, regionCodingScheme, regionCodeMeaning,
                                        regionModCode, regionModCodingScheme, regionModCodeMeaning)
        # end of processing a line of terminology
      infoFile.close()

      #TODO: Create logic class that both CLI and this plugin uses so that we don't need to have temporary NRRD files and labelmap nodes
      #if not hasattr(slicer.modules, 'segmentations'):

      # load the segmentation volume file and name it for the reference series and segment color
      labelFileName = os.path.join(outputDir,str(segmentId+1)+".nrrd")
      segmentName = seriesName + "-" + colorName + "-label"
      (success,labelNode) = slicer.util.loadLabelVolume(labelFileName,
                                                        properties = {'name' : segmentName},
                                                        returnNode=True)
      segmentNodes.append(labelNode)

      # point the label node to the color node we're creating
      labelDisplayNode = labelNode.GetDisplayNode()
      if labelDisplayNode == None:
        print ('Warning: no label map display node for segment ',segmentId,', creating!')
        labelNode.CreateDefaultDisplayNodes()
        labelDisplayNode = labelNode.GetDisplayNode()
      labelDisplayNode.SetAndObserveColorNodeID(segmentationColorNode.GetID())

      # TODO: initialize referenced UID (and segment number?) attribute(s)

      # create Subject hierarchy nodes for the loaded series
      self.addSeriesInSubjectHierarchy(loadable, labelNode)

    # create a combined (merge) label volume node (only if a segment was created)
    if labelNode:
      volumeLogic = slicer.modules.volumes.logic()
      mergeNode = volumeLogic.CloneVolume(labelNode, seriesName + "-label")
      combiner = slicer.vtkImageLabelCombine()
      for segmentNode in segmentNodes:
        combiner.SetInputConnection(0, mergeNode.GetImageDataConnection() )
        combiner.SetInputConnection(1, segmentNode.GetImageDataConnection() )
        combiner.Update()
        mergeNode.GetImageData().DeepCopy( combiner.GetOutput() )
      for segmentNode in segmentNodes:
        segmentNode.Modified() # sets the MTime so the editor won't think they are older than mergeNode
      # display the mergeNode
      selectionNode = slicer.app.applicationLogic().GetSelectionNode()
      selectionNode.SetReferenceActiveLabelVolumeID( mergeNode.GetID() )
      slicer.app.applicationLogic().PropagateVolumeSelection(0)

    # finalize the color node
    segmentationColorNode.NamesInitialisedOn()

    # TODO: the outputDir should be cleaned up

    if hasattr(slicer.modules, 'segmentations'):

      import vtkSlicerSegmentationsModuleLogic
      import vtkSlicerSegmentationsModuleMRML
      import vtkSegmentationCore

      segmentationNode = vtkSlicerSegmentationsModuleMRML.vtkMRMLSegmentationNode()
      segmentationNode.SetName(seriesName)
      segmentationNode.AddNodeReferenceID('colorNodeID', segmentationColorNode.GetID())
      slicer.mrmlScene.AddNode(segmentationNode)

      segmentationDisplayNode = vtkSlicerSegmentationsModuleMRML.vtkMRMLSegmentationDisplayNode()
      segmentationNode.SetAndObserveDisplayNodeID(segmentationDisplayNode.GetID())
      slicer.mrmlScene.AddNode(segmentationDisplayNode)

      segmentation = vtkSegmentationCore.vtkSegmentation()
      segmentation.SetMasterRepresentationName(vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName())

      segmentationNode.SetAndObserveSegmentation(segmentation)
      self.addSeriesInSubjectHierarchy(loadable, segmentationNode)

      colorID = 1
      for segmentNode in segmentNodes:
        segment = vtkSegmentationCore.vtkSegment()
        segment.SetName(segmentNode.GetName())

        segmentColor = [0,0,0,0]
        segmentationColorNode.GetColor(colorID, segmentColor)
        segment.SetDefaultColor(segmentColor[0:3])

        colorID += 1

        #TODO: when the logic class is created, this will need to be changed
        logic = vtkSlicerSegmentationsModuleLogic.vtkSlicerSegmentationsModuleLogic()
        orientedImage = logic.CreateOrientedImageDataFromVolumeNode(segmentNode)
        segment.AddRepresentation(vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName(), orientedImage)
        segmentation.AddSegment(segment)

        segmentDisplayNode = segmentNode.GetDisplayNode()
        slicer.mrmlScene.RemoveNode(segmentDisplayNode)
        slicer.mrmlScene.RemoveNode(segmentNode)

      segmentation.CreateRepresentation(vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationClosedSurfaceRepresentationName(), True)
      slicer.mrmlScene.RemoveNode(mergeNode)

    return True