# Get displacement vector fields within a segment from vector volume

**URL:** <https://discourse.slicer.org/t/get-displacement-vector-fields-within-a-segment-from-vector-volume/17511>\
**Category:** Support\
**Created:** [May 7, 2021, 3:55pm UTC](https://discourse.slicer.org/t/get-displacement-vector-fields-within-a-segment-from-vector-volume/17511 "2021-05-07T15:55:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marianaslicer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/marianaslicer/32/8555_2.png) [@marianaslicer](https://discourse.slicer.org/u/marianaslicer)\
**Post date:** [May 7, 2021, 3:55pm UTC](https://discourse.slicer.org/t/get-displacement-vector-fields-within-a-segment-from-vector-volume/17511/1 "2021-05-07T15:55:13Z")

</div>

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](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277) 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 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 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]
  [1 1 1 1 1 1 1 0 0 0 0]
  [1 1 1 1 1 1 1 1 0 0 0]
  [1 1 1 1 1 1 1 1 0 0 0]
  [1 1 1 1 1 1 1 1 1 0 0]
  [0 1 1 1 1 1 1 1 1 0 0]
  [0 0 1 1 1 1 1 1 1 0 0]
  [0 0 1 1 1 1 1 1 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 1 1 1 1 1 1 1 0 0]
  [0 1 1 1 1 1 1 1 1 0 0]
  [1 1 1 1 1 1 1 1 1 1 0]
  [1 1 1 1 1 1 1 1 1 1 0]
  [1 1 1 1 1 1 1 1 1 1 1]
  [0 1 1 1 1 1 1 1 1 1 1]
  [0 1 1 1 1 1 1 1 1 1 0]
  [0 0 1 1 1 1 1 1 1 1 0]
  [0 0 0 1 1 1 1 1 1 0 0]]

 [[0 0 0 0 0 1 1 1 0 0 0]
  [0 0 0 0 1 1 1 1 1 0 0]
  [0 0 0 0 1 1 1 1 1 0 0]
  [0 0 1 1 1 1 1 1 1 1 0]
  [0 1 1 1 1 1 1 1 1 1 0]
  [0 1 1 1 1 1 1 1 1 1 0]
  [0 1 1 1 1 1 1 1 1 1 0]
  [0 1 1 1 1 1 1 1 1 1 0]
  [0 0 1 1 1 1 1 1 1 0 0]
  [0 0 1 1 1 1 1 1 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 1 1 1 0 0 0]
  [0 0 0 0 0 1 1 1 0 0 0]
  [0 0 0 0 1 1 1 1 1 0 0]
  [0 0 1 1 1 1 1 1 1 0 0]
  [0 1 1 1 1 1 1 1 1 0 0]
  [0 0 1 1 1 1 1 1 1 0 0]
  [0 0 0 1 1 1 1 1 0 0 0]
  [0 0 0 0 1 1 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0]]]

```

I would appreciate some help. Thank you.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 13, 2021, 4:52am UTC](https://discourse.slicer.org/t/get-displacement-vector-fields-within-a-segment-from-vector-volume/17511/2 "2021-05-13T04:52:03Z")

</div>

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

```python
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, :]

```
