# Negative Values in DVF Histogram

**URL:** <https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008>\
**Category:** Support\
**Created:** [August 1, 2021, 3:44am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008 "2021-08-01T03:44:19Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [August 26, 2021, 6:48am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/21 "2021-08-26T06:48:00Z")

</div>

I tried to get the histogram for each segment with this code.  
However, I got the same result for every segment.  
I apologize for the inconvenience. Please check.

```python
>>> labelValue = 1
Loading with imageIOName: GDCM
>>> labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
>>> segmentationNode = getNode('segmentation')
>>> volumeNode = getNode('Displacement Field')
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> segmentVoxels = volumeArray[labelArray==labelValue]
>>> import numpy as np
>>> histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1)
(MRMLCorePython.vtkMRMLPlotChartNode)000002174751D408
>>> range = [-5.0, 5.0]
>>> bins = 100
>>> histogramR, histogramBins = np.histogram(arrayFromVolume(volumeNode)[:,:,:,0], bins, range)
>>> histogramA = np.histogram(arrayFromVolume(volumeNode)[:,:,:,1], bins, range)[0]
>>> histogramS = np.histogram(arrayFromVolume(volumeNode)[:,:,:,2], bins, range)[0]
>>> slicer.util.plot(np.vstack([histogramBins[:-1], histogramR, histogramA, histogramS]).T, xColumnIndex = 0, columnNames=['N', 'R', 'A', 'S'], title='Histogram')
(MRMLCorePython.vtkMRMLPlotChartNode)000002174751A3A8
>>> magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
>>> histogram = np.histogram(magnitudes, bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")
(MRMLCorePython.vtkMRMLPlotChartNode)000002174751AE88
>>>

```

---

<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:** [August 27, 2021, 2:17am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/22 "2021-08-27T02:17:19Z")

</div>

You nicely computed `segmentVoxels` but did not use it when you computed the histogram.

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [August 27, 2021, 11:28am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/23 "2021-08-27T11:28:41Z")

</div>

> > > segmentVoxels = volumeArray[labelArray==labelValue]

Is the above not correct?  
If you know, I would like to know more about the code to add and where to fix it.

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [August 30, 2021, 7:03am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/24 "2021-08-30T07:03:28Z")

</div>

> > > labelValue = 1  
> > > Loading with imageIOName: GDCM  
> > > labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)  
> > > segmentationNode = getNode(‘segmentation’)  
> > > volumeNode = getNode(‘Displacement Field’)  
> > > labelmapVolumeNode = getNode(‘LabelMapVolume’)  
> > > slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)  
> > > True  
> > > volumeArray = slicer.util.arrayFromVolume(volumeNode)  
> > > labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)  
> > > segmentVoxels = volumeArray[labelArray==labelValue]  
> > > import numpy as np  
> > > histogram = np.histogram(arrayFromVolume(volumeNode),segmentVoxels, bins=50)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > File “\< **array\_function** internals\>”, line 4, in histogram  
> > > TypeError: \_histogram\_dispatcher() got multiple values for argument ‘bins’

I changed the code a bit.

> > > histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)

Added a segment voxel to the above.

> > > histogram = np.histogram(arrayFromVolume(volumeNode),segmentVoxels, bins=100)

But an error occurred.  
I’m sorry for the inconvenience, but please confirm.

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [August 31, 2021, 8:58am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/25 "2021-08-31T08:58:46Z")

</div>

Sorry for asking so many questions.  
Next, I tried to write such a code.  
I changed the arrayFromVolume(volumeNode) in the code you gave me to segmentVoxels.  
However, I got an error again.  
How can I get the histogram for each segment?

```python
>>> labelValue = 1
Loading with imageIOName: GDCM
>>> labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
>>> segmentationNode = getNode('segmentation')
>>> volumeNode = getNode('Displacement Field')
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> import numpy as np
>>> histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1)
(MRMLCorePython.vtkMRMLPlotChartNode)000001F95ACA9E88
>>> segmentationNode = getNode('segmentation')
>>> volumeNode = getNode('Displacement Field')
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> segmentVoxels = volumeArray[labelArray==labelValue]
>>> import numpy as np
>>> histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1)
(MRMLCorePython.vtkMRMLPlotChartNode)000001F95ACA9408
>>> segmentationNode = getNode('segmentation')
>>> volumeNode = getNode('Displacement Field')
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> segmentVoxels = volumeArray[labelArray==labelValue]
>>> import numpy as np
>>> histogram = np.histogram(segmentVoxels, bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1)
(MRMLCorePython.vtkMRMLPlotChartNode)000001F95ACA9E28
>>> range = [-5.0, 5.0]
>>> bins = 100
>>> histogramR, histogramBins = np.histogram(segmentVoxles[:,:,:,0], bins, range)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'segmentVoxles' is not defined

```

---

<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:** [August 31, 2021, 1:21pm UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/26 "2021-08-31T13:21:21Z")

</div>

There is a typo in the variable name.

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [September 1, 2021, 6:12am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/27 "2021-09-01T06:12:38Z")

</div>

Where is the mistake?  
I changed arrayFromVolume(volumeNode) to segmentVoxels, but this did not give me a histogram for each segment.  
What should I do?

> > > labelValue = 1  
> > > Loading with imageIOName: GDCM  
> > > labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)  
> > > segmentationNode = getNode(‘segmentation’)  
> > > volumeNode = getNode(‘Displacement Field’)  
> > > labelmapVolumeNode = getNode(‘LabelMapVolume’)  
> > > slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)  
> > > True  
> > > volumeArray = slicer.util.arrayFromVolume(volumeNode)  
> > > labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)  
> > > segmentVoxels = volumeArray[labelArray==labelValue]  
> > > import numpy as np  
> > > histogram = np.histogram(segmentVoxels, bins=100)  
> > > slicer.util.plot(histogram, xColumnIndex = 1)  
> > > (MRMLCorePython.vtkMRMLPlotChartNode)0000024502908828  
> > > histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)  
> > > slicer.util.plot(histogram, xColumnIndex = 1)  
> > > (MRMLCorePython.vtkMRMLPlotChartNode)0000024502908408  
> > > range = [-5.0, 5.0]  
> > > bins = 100  
> > > histogramR, histogramBins = np.histogram(segmentationVoxels[:,:,:,0], bins, range)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘segmentationVoxels’ is not defined

If you know how to get each contour, please let me know.

---

<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:** [September 1, 2021, 4:52pm UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/28 "2021-09-01T16:52:28Z")

</div>

> [@toyama](#):
>
> ```auto
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> NameError: name 'segmentVoxles' is not defined
> 
> ```

> [@lassoan](#):
>
> There is a typo in the variable name.

> [@toyama](#):
>
> Where is the mistake?

`segmentVoxles` is mistyped. The name is not the same as the `segmentVoxels` variable that you created above.

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [September 2, 2021, 8:24am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/29 "2021-09-02T08:24:46Z")

</div>

Thank you for letting me know.  
I’ve modified the code.  
Is it now possible to get the DVF histogram for each segment?

> > > labelValue = 1  
> > > Loading with imageIOName: GDCM  
> > > labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)  
> > > segmentationNode = getNode(‘segmentation’)  
> > > volumeNode = getNode(‘Displacement Field’)  
> > > labelmapVolumeNode = getNode(‘LabelMapVolume’)  
> > > slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)  
> > > True  
> > > volumeArray = slicer.util.arrayFromVolume(volumeNode)  
> > > labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)  
> > > segmentVoxels = volumeArray[labelArray==labelValue]  
> > > import numpy as np  
> > > histogram = np.histogram(segmentVoxels, bins=100)  
> > > slicer.util.plot(histogram, xColumnIndex = 1)  
> > > (MRMLCorePython.vtkMRMLPlotChartNode)0000015B0A709EE8  
> > > range = [-5.0, 5.0]  
> > > bins = 100  
> > > histogramR, histogramBins = np.histogram(segmentVoxels[:,0], bins, range)  
> > > histogramA = np.histogram(segmentVoxels[:,1], bins, range)[0]  
> > > histogramS = np.histogram(segmentVoxels[:,2], bins, range)[0]  
> > > slicer.util.plot(np.vstack([histogramBins[:-1], histogramR, histogramA, histogramS]).T, xColumnIndex = 0, columnNames=[‘N’, ‘R’, ‘A’, ‘S’], title=‘Histogram’)  
> > > (MRMLCorePython.vtkMRMLPlotChartNode)0000015B0A709468  
> > > magnitudes = np.linalg.norm(segmentVoxels, axis=1)  
> > > histogram = np.histogram(magnitudes, bins=100)  
> > > slicer.util.plot(histogram, xColumnIndex = 1, columnNames=[‘magnitude’, ‘N’], title=“displacement”)  
> > > (MRMLCorePython.vtkMRMLPlotChartNode)0000015B0DBC3F48

The code you gave me before gave me this error.

> > > histogramR, histogramBins = np.histogram(segmentVoxels[:,:,:,0], bins, range)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > IndexError: too many indices for array: array is 2-dimensional, but 4 were indexed

> > > magnitudes = np.linalg.norm(segmentVoxels, axis=3)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > File “\< **array\_function** internals\>”, line 6, in norm  
> > > return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))  
> > > numpy.AxisError: axis 3 is out of bounds for array of dimension 2

I modified this part, but is the code at the top correct?

---

<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:** [September 2, 2021, 6:06pm UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/30 "2021-09-02T18:06:59Z")

</div>

The code snippets that I provided [above](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/18) all work correctly and you only need a little Python and numpy knowledge to apply them to your data. You may try to complete Python and numpy tutorials or find someone locally who can help you out with this.

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [September 3, 2021, 10:44am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/31 "2021-09-03T10:44:29Z")

</div>

Is it wrong to change [:,:,:,0] and axis=3 to fit the segment?  
I was able to get the DVF for each axis and the total DVF with this code, but was the code wrong?  
Is it impossible to get the DVF for each segment?  
Is it possible to calculate only by volume?  
I would like your answer.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/a/3abddb725a6407e72238ada519a1be5451cf9369.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/e/eeffd87274baab629ee557cc0f91fb534fde547e.png)

---

<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:** [September 5, 2021, 2:26am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/32 "2021-09-05T02:26:17Z")

</div>

You can use standard Python array indexing to filter voxels that are in a selected segment. For example:

```python
volumeNode = getNode('Displacement Field')
segmentationNode = getNode('Segmentation')
segmentId = 'Segment_3'

magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
magnitudesInSegment = magnitudes[arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId) != 0]
histogram = np.histogram(magnitudesInSegment, bins=100)
slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")

```

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [September 6, 2021, 8:38am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/33 "2021-09-06T08:38:04Z")

</div>

Thank you for your answer.  
I tried, but I got an error.  
I apologize for the inconvenience, but I would appreciate your advice.

```auto
>>> labelValue = 1
Loading with imageIOName: GDCM
>>> labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
>>> volumeNode = getNode('Displacement Field')
>>> segmentationNode = getNode('segmentation')
>>> segmentId = 'patient'
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> segmentVoxels = volumeArray[labelArray==labelValue]
>>> import numpy as np
>>> histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1)
(MRMLCorePython.vtkMRMLPlotChartNode)00000222A95B87C8
>>> range = [-5.0, 5.0]
>>> bins = 100
>>> histogramR, histogramBins = np.histogram(arrayFromVolume(volumeNode)[:,:,:,0], bins, range)
>>> histogramA = np.histogram(arrayFromVolume(volumeNode)[:,:,:,1], bins, range)[0]
>>> histogramS = np.histogram(arrayFromVolume(volumeNode)[:,:,:,2], bins, range)[0]
>>> slicer.util.plot(np.vstack([histogramBins[:-1], histogramR, histogramA, histogramS]).T, xColumnIndex = 0, columnNames=['N', 'R', 'A', 'S'], title='Histogram')
(MRMLCorePython.vtkMRMLPlotChartNode)00000222A95B86A8
>>> magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
>>> magnitudesInSegment = magnitudes[arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId) != 0]
Traceback (most recent call last):
File "<console>", line 1, in <module>
IndexError: boolean index did not match indexed array along dimension 0; dimension is 128 but corresponding boolean dimension is 130

```

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [September 7, 2021, 7:04am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/34 "2021-09-07T07:04:28Z")

</div>

> [@lassoan](#):
>
> ```auto
> histogram = np.histogram(magnitudesInSegment, bins=100)
> slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")
> 
> ```

I’ve tried drawing other code, but I still get this error.  
I apologize for the inconvenience, but I would appreciate your advice.

```auto
>>> labelValue = 1
Loading with imageIOName: GDCM
>>> labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
>>> volumeNode = getNode('Displacement Field')
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> segmentationNode = getNode('segmentation')
>>> segmentId = 'GTV'
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> import numpy as np
>>> histogram = np.histogram(arrayFromVolume(volumeNode), bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1)
(MRMLCorePython.vtkMRMLPlotChartNode)000001D612619E88
>>> range = [-5.0, 5.0]
>>> bins = 100
>>> histogramR, histogramBins = np.histogram(arrayFromVolume(volumeNode)[:,:,:,0], bins, range)
>>> histogramA = np.histogram(arrayFromVolume(volumeNode)[:,:,:,1], bins, range)[0]
>>> histogramS = np.histogram(arrayFromVolume(volumeNode)[:,:,:,2], bins, range)[0]
>>> slicer.util.plot(np.vstack([histogramBins[:-1], histogramR, histogramA, histogramS]).T, xColumnIndex = 0, columnNames=['N', 'R', 'A', 'S'], title='Histogram')
(MRMLCorePython.vtkMRMLPlotChartNode)000001D612619DC8
>>> magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
>>> histogram = np.histogram(magnitudes, bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")
(MRMLCorePython.vtkMRMLPlotChartNode)000001D612619EE8
>>> magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
>>> magnitudesInSegment = magnitudes[arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId) != 0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
IndexError: boolean index did not match indexed array along dimension 0; dimension is 128 but corresponding boolean dimension is 123
>>> histogram = np.histogram(magnitudesInSegment, bins=100)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'magnitudesInSegment' is not defined
>>> slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")
(MRMLCorePython.vtkMRMLPlotChartNode)000001D612619E28

```

---

<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:** [September 8, 2021, 6:09pm UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/35 "2021-09-08T18:09:12Z")

</div>

> [@toyama](#):
>
> `IndexError: boolean index did not match indexed array along dimension 0; dimension is 128 but corresponding boolean dimension is 130`

This indicates that the size of the scalar volume and the segmentation labelmap is not the same. Using the latest Slicer Preview Release probably fixes this already, but if not then specify the displacement field as reference volume in [arrayFromSegmentBinaryLabelmap](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromSegmentBinaryLabelmap).

---

<div class="post-metadata">

**Author:** ![toyama](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@toyama](https://discourse.slicer.org/u/toyama)\
**Post date:** [September 9, 2021, 8:31am UTC](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008/36 "2021-09-09T08:31:18Z")

</div>

Thank you very much.  
After installing the preview version, the code also worked correctly and I was able to get the histogram successfully.

```auto
>>> labelValue = 1
Loading with imageIOName: GDCM
>>> labelmapVolumeNode =slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
>>> volumeNode = getNode('Displacement Field')
>>> labelmapVolumeNode = getNode('LabelMapVolume')
>>> segmentationNode = getNode('segmentation')
>>> segmentId = 'patient'
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> import numpy as np
>>> magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
>>> magnitudesInSegment = magnitudes[arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId) != 0]
>>> histogram = np.histogram(magnitudesInSegment, bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")
(MRMLCore.vtkMRMLPlotChartNode)000001BA99908E88
>>> segmentId = 'GTV'
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)
True
>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)
>>> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
>>> import numpy as np
>>> magnitudes = np.linalg.norm(arrayFromVolume(volumeNode), axis=3)
>>> magnitudesInSegment = magnitudes[arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId) != 0]
>>> histogram = np.histogram(magnitudesInSegment, bins=100)
>>> slicer.util.plot(histogram, xColumnIndex = 1, columnNames=['magnitude', 'N'], title="displacement")
(MRMLCore.vtkMRMLPlotChartNode)000001BA99908C48

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/e/8e4f92fcd6048744357c0e715a7fb7fd3d1da2b1.png)

[Previous page](https://discourse.slicer.org/t/negative-values-in-dvf-histogram/19008.md?page=1)
