def showVolumeFromHierarchyNode(self, hierarchyNode):

        volume = self.loadVolumeFromHierarchyNode(hierarchyNode)

        if not volume:
            print "No volume"
            return

        if volume.IsA('vtkMRMLVolumeNode'):
            if not volume.GetDisplayNodeID():
                displayNode = None
                if volume.IsA('vtkMRMLScalarVolumeNode'):
                    displayNode = slicer.vtkMRMLScalarVolumeDisplayNode()
                    displayNode.SetAndObserveColorNodeID("vtkMRMLColorTableNodeGrey")
                if volume.IsA('vtkMRMLVectorVolumeNode'):
                    displayNode = slicer.vtkMRMLVectorVolumeDisplayNode()
                    displayNode.SetAndObserveColorNodeID("vtkMRMLColorTableNodeGrey")
                if displayNode:
                    slicer.mrmlScene.AddNode(displayNode)
                    volume.SetAndObserveDisplayNodeID(displayNode.GetID())
            else:
                volume.GetDisplayNode().SetAndObserveColorNodeID("vtkMRMLColorTableNodeGrey")
            selectionNode = slicer.app.applicationLogic().GetSelectionNode()
            selectionNode.SetReferenceActiveVolumeID(volume.GetID())
            slicer.app.applicationLogic().PropagateVolumeSelection(0)
    def computeDifference(self, fieldA, fieldB, roi, differenceVolume):
        referenceVolume = self.createVectorVolumeFromRoi(
            roi, self.ReferenceVolumeSpacingMm)
        referenceVolume.SetName('ReferenceVolume')
        slicer.mrmlScene.AddNode(referenceVolume)
        resampledFieldA = self.resampleVolume(fieldA, referenceVolume)
        resampledFieldB = self.resampleVolume(fieldB, referenceVolume)
        subtractor = vtk.vtkImageMathematics()
        subtractor.SetOperationToSubtract()
        subtractor.SetInput1Data(resampledFieldA.GetImageData())
        subtractor.SetInput2Data(resampledFieldB.GetImageData())
        differenceVolume.SetImageDataConnection(subtractor.GetOutputPort())
        ijkToRasMatrix = vtk.vtkMatrix4x4()
        referenceVolume.GetIJKToRASMatrix(ijkToRasMatrix)
        differenceVolume.SetIJKToRASMatrix(ijkToRasMatrix)

        differenceVolumeDisplayNode = slicer.vtkMRMLVectorVolumeDisplayNode()
        slicer.mrmlScene.AddNode(differenceVolumeDisplayNode)
        differenceVolumeDisplayNode.SetAndObserveColorNodeID(
            "vtkMRMLColorTableNodeRainbow")
        differenceVolume.SetAndObserveNthDisplayNodeID(
            0, differenceVolumeDisplayNode.GetID())

        slicer.mrmlScene.RemoveNode(resampledFieldA)
        slicer.mrmlScene.RemoveNode(resampledFieldB)
        slicer.mrmlScene.RemoveNode(referenceVolume)
  def computeDifference(self, fieldA, fieldB, roi, differenceVolume):
    referenceVolume = self.createVectorVolumeFromRoi(roi, self.ReferenceVolumeSpacingMm)
    referenceVolume.SetName('ReferenceVolume')
    slicer.mrmlScene.AddNode( referenceVolume )
    resampledFieldA = self.resampleVolume(fieldA, referenceVolume)
    resampledFieldB = self.resampleVolume(fieldB, referenceVolume)
    subtractor = vtk.vtkImageMathematics()
    subtractor.SetOperationToSubtract()
    subtractor.SetInput1Data(resampledFieldA.GetImageData())
    subtractor.SetInput2Data(resampledFieldB.GetImageData())
    differenceVolume.SetImageDataConnection(subtractor.GetOutputPort())
    ijkToRasMatrix = vtk.vtkMatrix4x4()
    referenceVolume.GetIJKToRASMatrix(ijkToRasMatrix)
    differenceVolume.SetIJKToRASMatrix(ijkToRasMatrix)

    differenceVolumeDisplayNode = slicer.vtkMRMLVectorVolumeDisplayNode()
    slicer.mrmlScene.AddNode( differenceVolumeDisplayNode )
    differenceVolumeDisplayNode.SetAndObserveColorNodeID("vtkMRMLColorTableNodeRainbow");
    differenceVolume.SetAndObserveNthDisplayNodeID(0, differenceVolumeDisplayNode.GetID());

    slicer.mrmlScene.RemoveNode( resampledFieldA )
    slicer.mrmlScene.RemoveNode( resampledFieldB )
    slicer.mrmlScene.RemoveNode( referenceVolume )
  def computeAverage(self, inputFieldNodes, roi, outputAverageVolume, outputVarianceVolume):

    referenceVolume = self.createVectorVolumeFromRoi(roi, self.ReferenceVolumeSpacingMm)
    referenceVolume.SetName('ReferenceVolume')
    slicer.mrmlScene.AddNode( referenceVolume )

    fieldNodes=[]
    fieldImageData=[]
    for fieldNode in inputFieldNodes:
      resampledFieldNode = self.resampleVolume(fieldNode, referenceVolume)
      fieldNodes.append(resampledFieldNode)
      fieldImageData.append(resampledFieldNode.GetImageData())

    ijkToRasMatrix = vtk.vtkMatrix4x4()

    # Average volume
    averageImageData = vtk.vtkImageData()
    averageImageData.DeepCopy(referenceVolume.GetImageData())
    outputAverageVolume.SetAndObserveImageData(averageImageData)
    referenceVolume.GetIJKToRASMatrix(ijkToRasMatrix)
    outputAverageVolume.SetIJKToRASMatrix(ijkToRasMatrix)

    # Variance volume
    varianceImageData = vtk.vtkImageData()
    varianceImageData.SetExtent(averageImageData.GetExtent())
    if vtk.VTK_MAJOR_VERSION <= 5:
      varianceImageData.SetScalarType(vtk.VTK_DOUBLE)
      varianceImageData.SetNumberOfScalarComponents(1)
      varianceImageData.AllocateScalars()
    else:
      varianceImageData.AllocateScalars(vtk.VTK_DOUBLE, 1)
    outputVarianceVolume.SetIJKToRASMatrix(ijkToRasMatrix)
    outputVarianceVolume.SetAndObserveImageData(varianceImageData)

    # Compute

    dims = averageImageData.GetDimensions()
    # [field, component]
    voxelValues = numpy.zeros([len(fieldImageData), 3])
    for z in xrange(dims[2]):
      for y in xrange(dims[1]):
        for x in xrange(dims[0]):
          fieldIndex = 0
          for imageData in fieldImageData:
            voxelValues[fieldIndex,0] = imageData.GetScalarComponentAsDouble(x, y, z, 0)
            voxelValues[fieldIndex,1] = imageData.GetScalarComponentAsDouble(x, y, z, 1)
            voxelValues[fieldIndex,2] = imageData.GetScalarComponentAsDouble(x, y, z, 2)
            fieldIndex = fieldIndex+1
          meanVoxelValues = numpy.mean(voxelValues, axis = 0)
          averageImageData.SetScalarComponentFromDouble(x, y, z, 0, meanVoxelValues[0])
          averageImageData.SetScalarComponentFromDouble(x, y, z, 1, meanVoxelValues[1])
          averageImageData.SetScalarComponentFromDouble(x, y, z, 2, meanVoxelValues[2])
          # Compute the mean of the magnitude of the error vectors
          errorValues = voxelValues-meanVoxelValues
          errorVectorMagnitudes = numpy.sqrt(numpy.sum(errorValues*errorValues, axis=1))
          varianceImageData.SetScalarComponentFromDouble(x, y, z, 0, numpy.mean(errorVectorMagnitudes))

    averageImageData.Modified()
    varianceImageData.Modified()

    # Create display node if they have not created yet

    if not outputAverageVolume.GetNthDisplayNode(0):
      outputAverageVolumeDisplayNode = slicer.vtkMRMLVectorVolumeDisplayNode()
      slicer.mrmlScene.AddNode( outputAverageVolumeDisplayNode )
      outputAverageVolumeDisplayNode.SetAndObserveColorNodeID("vtkMRMLColorTableNodeRainbow");
      outputAverageVolume.SetAndObserveNthDisplayNodeID(0, outputAverageVolumeDisplayNode.GetID());

    if not outputVarianceVolume.GetNthDisplayNode(0):
      outputVarianceVolumeDisplayNode = slicer.vtkMRMLScalarVolumeDisplayNode()
      slicer.mrmlScene.AddNode( outputVarianceVolumeDisplayNode )
      outputVarianceVolumeDisplayNode.SetAndObserveColorNodeID("vtkMRMLColorTableNodeRainbow");
      outputVarianceVolume.SetAndObserveNthDisplayNodeID(0, outputVarianceVolumeDisplayNode.GetID());

    # Clean up temporary nodes

    for fieldNode in fieldNodes:
      slicer.mrmlScene.RemoveNode( fieldNode )

    slicer.mrmlScene.RemoveNode( referenceVolume )
    def computeAverage(self, inputFieldNodes, roi, outputAverageVolume,
                       outputVarianceVolume):

        referenceVolume = self.createVectorVolumeFromRoi(
            roi, self.ReferenceVolumeSpacingMm)
        referenceVolume.SetName('ReferenceVolume')
        slicer.mrmlScene.AddNode(referenceVolume)

        fieldNodes = []
        fieldImageData = []
        for fieldNode in inputFieldNodes:
            resampledFieldNode = self.resampleVolume(fieldNode,
                                                     referenceVolume)
            fieldNodes.append(resampledFieldNode)
            fieldImageData.append(resampledFieldNode.GetImageData())

        ijkToRasMatrix = vtk.vtkMatrix4x4()

        # Average volume
        averageImageData = vtk.vtkImageData()
        averageImageData.DeepCopy(referenceVolume.GetImageData())
        outputAverageVolume.SetAndObserveImageData(averageImageData)
        referenceVolume.GetIJKToRASMatrix(ijkToRasMatrix)
        outputAverageVolume.SetIJKToRASMatrix(ijkToRasMatrix)

        # Variance volume
        varianceImageData = vtk.vtkImageData()
        varianceImageData.SetExtent(averageImageData.GetExtent())
        if vtk.VTK_MAJOR_VERSION <= 5:
            varianceImageData.SetScalarType(vtk.VTK_DOUBLE)
            varianceImageData.SetNumberOfScalarComponents(1)
            varianceImageData.AllocateScalars()
        else:
            varianceImageData.AllocateScalars(vtk.VTK_DOUBLE, 1)
        outputVarianceVolume.SetIJKToRASMatrix(ijkToRasMatrix)
        outputVarianceVolume.SetAndObserveImageData(varianceImageData)

        # Compute

        dims = averageImageData.GetDimensions()
        # [field, component]
        voxelValues = numpy.zeros([len(fieldImageData), 3])
        for z in xrange(dims[2]):
            for y in xrange(dims[1]):
                for x in xrange(dims[0]):
                    fieldIndex = 0
                    for imageData in fieldImageData:
                        voxelValues[fieldIndex,
                                    0] = imageData.GetScalarComponentAsDouble(
                                        x, y, z, 0)
                        voxelValues[fieldIndex,
                                    1] = imageData.GetScalarComponentAsDouble(
                                        x, y, z, 1)
                        voxelValues[fieldIndex,
                                    2] = imageData.GetScalarComponentAsDouble(
                                        x, y, z, 2)
                        fieldIndex = fieldIndex + 1
                    meanVoxelValues = numpy.mean(voxelValues, axis=0)
                    averageImageData.SetScalarComponentFromDouble(
                        x, y, z, 0, meanVoxelValues[0])
                    averageImageData.SetScalarComponentFromDouble(
                        x, y, z, 1, meanVoxelValues[1])
                    averageImageData.SetScalarComponentFromDouble(
                        x, y, z, 2, meanVoxelValues[2])
                    # Compute the mean of the magnitude of the error vectors
                    errorValues = voxelValues - meanVoxelValues
                    errorVectorMagnitudes = numpy.sqrt(
                        numpy.sum(errorValues * errorValues, axis=1))
                    varianceImageData.SetScalarComponentFromDouble(
                        x, y, z, 0, numpy.mean(errorVectorMagnitudes))

        averageImageData.Modified()
        varianceImageData.Modified()

        # Create display node if they have not created yet

        if not outputAverageVolume.GetNthDisplayNode(0):
            outputAverageVolumeDisplayNode = slicer.vtkMRMLVectorVolumeDisplayNode(
            )
            slicer.mrmlScene.AddNode(outputAverageVolumeDisplayNode)
            outputAverageVolumeDisplayNode.SetAndObserveColorNodeID(
                "vtkMRMLColorTableNodeRainbow")
            outputAverageVolume.SetAndObserveNthDisplayNodeID(
                0, outputAverageVolumeDisplayNode.GetID())

        if not outputVarianceVolume.GetNthDisplayNode(0):
            outputVarianceVolumeDisplayNode = slicer.vtkMRMLScalarVolumeDisplayNode(
            )
            slicer.mrmlScene.AddNode(outputVarianceVolumeDisplayNode)
            outputVarianceVolumeDisplayNode.SetAndObserveColorNodeID(
                "vtkMRMLColorTableNodeRainbow")
            outputVarianceVolume.SetAndObserveNthDisplayNodeID(
                0, outputVarianceVolumeDisplayNode.GetID())

        # Clean up temporary nodes

        for fieldNode in fieldNodes:
            slicer.mrmlScene.RemoveNode(fieldNode)

        slicer.mrmlScene.RemoveNode(referenceVolume)