# Extraction the vector components using python

**URL:** <https://discourse.slicer.org/t/extraction-the-vector-components-using-python/10394>\
**Category:** Support\
**Created:** [February 22, 2020, 6:24pm UTC](https://discourse.slicer.org/t/extraction-the-vector-components-using-python/10394 "2020-02-22T18:24:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aseman](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@aseman](https://discourse.slicer.org/u/aseman)\
**Post date:** [February 22, 2020, 6:24pm UTC](https://discourse.slicer.org/t/extraction-the-vector-components-using-python/10394/1 "2020-02-22T18:24:08Z")

</div>

Operating system:Linux  
Slicer version:4.11.0  
Hi 3D Slicer experts and all  
I have a BSplin Transform which contains displacement fields. I want to extract its vector components. for this purpose I converted it to a volume node using Transforms module.[my reference volume is a CT image with image dimensions of 512 \*512 \*173]. Therefore, the result was a volume node (renamed to Voluememasked) with image dimensions 512 \*512 \*173.  
for extraction I used the following commands in Slicer python interactor :

> > > import [scipy.io](http://scipy.io) as sio  
> > > nodeName= ‘Volumemasked’  
> > > voxelArray = array(nodeName)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > File “/home/aseman/Downloads/Slicer-4.11.0-2020-02-19-linux-amd64/bin/Python/slicer/util.py”, line 935, in array  
> > > return arrayFromVolume(node)  
> > > File “/home/aseman/Downloads/Slicer-4.11.0-2020-02-19-linux-amd64/bin/Python/slicer/util.py”, line 971, in arrayFromVolume  
> > > narray = vtk.util.numpy\_support.vtk\_to\_numpy(vimage.GetPointData().GetScalars()).reshape(nshape)  
> > > ValueError: cannot reshape array of size 136052736 into shape (173,512,512)

how can I reshape it? can you help me?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 22, 2020, 6:40pm UTC](https://discourse.slicer.org/t/extraction-the-vector-components-using-python/10394/2 "2020-02-22T18:40:45Z")

</div>

this is a numpy stuf  
`136052736 = 3 * 173 * 512 * 512`  
therefore you have 3 components in each voxel, redefine nshape like  
`nshape = [3, 73, 512, 512]`  
then your reshape will work, to extract the x, y, z components do:  
`x, y, z = narray[0], narray[1], narray[2]`  
if you want the vectors  
vec = zip(x, y, z)

---

<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:** [February 22, 2020, 7:00pm UTC](https://discourse.slicer.org/t/extraction-the-vector-components-using-python/10394/3 "2020-02-22T19:00:18Z")

</div>

Thanks for reporting this, it seems that Slicer’s `array` helper function cannot get vector field from scalar volume. We should be able to fix it within a few days. In the meantime, you can change `arrayFromVolume` function in your util.py file as @Alex_Vergara suggested above.

---

<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:** [February 25, 2020, 12:26am UTC](https://discourse.slicer.org/t/extraction-the-vector-components-using-python/10394/4 "2020-02-25T00:26:57Z")

</div>

I have updated `arrayFromVolume` to work with any number of scalar components, regardless of using a scalar or vector volume. I’ve also updated Transforms module to save displacement vectors if a vector volume is selected as output, and save displacement magnitude values if a scalar volume is selected as output. Changes are available in latest Slicer Preview Release.
