Get displacement vector fields within a segment from vector volume

Hi everyone,

I used the Transforms module to convert a transform in a vector volume node because I’m interested in getting the motion of a ROI in the three directions.

I think I was able to get the magnitude of the motion in the three directions using the commands:

volumeNode = getNode('Displacement Field')
volumeArray = slicer.util.arrayFromVolume(volumeNode)

Result:
print(volumeArray)
[[[[ 1.8819143e-01 2.5158906e-01 2.4303983e-01]
[ 1.9190283e-01 2.5705618e-01 2.4819151e-01]
[ 1.9539744e-01 2.6224315e-01 2.5308487e-01]

[-2.4863344e-01 2.2675639e-01 2.8394413e-01]
[-2.4424334e-01 2.2257148e-01 2.7850366e-01]
[-2.3958866e-01 2.1815073e-01 2.7276406e-01]]

  [[ 1.9246668e-01  2.5698945e-01  2.4814722e-01]
   [ 1.9626349e-01  2.6257366e-01  2.5340959e-01]
   [ 1.9983855e-01  2.6787159e-01  2.5840810e-01]
   ...
   [-2.5424054e-01  2.3186195e-01  2.9020959e-01]
   [-2.4974997e-01  2.2758357e-01  2.8464693e-01]
   [-2.4498877e-01  2.2306396e-01  2.7877831e-01]]

  [[ 1.9650456e-01  2.6209354e-01  2.5296500e-01]
   [ 2.0038210e-01  2.6778823e-01  2.5833178e-01]
   [ 2.0403317e-01  2.7319089e-01  2.6342940e-01]
   ...

Now, I would like to get the vector fields within a specific segment (e.g. tumor).
I tried to use the example code here but when I try to run

segmentVoxels = volumeArray[labelArray == labelValue]

I’m having the following error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
IndexError: boolean index did not match indexed array along dimension 0; dimension is 137 but corresponding boolean dimension is 5

More Info:
labelValue = 1

print(labelArray)

[[[0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 1 1 0 0 0 0 0 0]
  [0 0 1 1 1 1 0 0 0 0 0]
  [0 0 1 1 1 1 0 0 0 0 0]
  [0 1 1 1 1 1 1 0 0 0 0]
  [0 0 1 1 1 1 1 0 0 0 0]
  [0 0 0 1 1 1 1 0 0 0 0]
  [0 0 0 0 1 1 1 0 0 0 0]
  [0 0 0 0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  [0 0 0 0 0 0 0 0 0 0 0]]]

I would appreciate some help. Thank you.

You almost got it right, just missed a dimension when specified the displacement array indices. Here is a complete working example:

displacementVolumeNode = getNode('Displacement Field')
segmentationNode = getNode('Segmentation')
labelValue = 1  # label value of first segment

# Get displacement field as numpy array
displacementVolumeArray = slicer.util.arrayFromVolume(volumeNode)

# Get segment as numpy array
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
labelmapVolumeArray = slicer.util.arrayFromVolume(labelmapVolumeNode)

# Get displacement vectors in the segment as numpy array
displacementsInSegment = displacementVolumeArray[labelmapVolumeArray == labelValue, :]