# Cannot import numpy array using updateVolumeFromArray

**URL:** <https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091>\
**Category:** Development\
**Created:** [November 8, 2019, 5:45pm UTC](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091 "2019-11-08T17:45:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenagl12](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stevenagl12/32/3391_2.png) [@stevenagl12](https://discourse.slicer.org/u/stevenagl12)\
**Post date:** [November 8, 2019, 5:45pm UTC](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091/1 "2019-11-08T17:45:25Z")

</div>

I have a numpy file that I am trying to import using the updateVolumeFromArray function. The file can load into slicer as a numpy array, but the second I go to update the volume, and display the image (clicking on the volume eye button from the data module), the entire system crashes. I can call the matrix in, and I can perform operations on it such as finding out the unique values, but I cannot display it in slicer? My matrix is a 128X256X256 numpy array with binary values of 0-4.

---

<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:** [November 8, 2019, 6:00pm UTC](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091/2 "2019-11-08T18:00:19Z")

</div>

Can you provide sample code and data set?

---

<div class="post-metadata">

**Author:** ![stevenagl12](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stevenagl12/32/3391_2.png) [@stevenagl12](https://discourse.slicer.org/u/stevenagl12)\
**Post date:** [November 8, 2019, 6:04pm UTC](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091/3 "2019-11-08T18:04:20Z")

</div>

So, I import my volume with `np.load(filepath)['arr_0']`. I then create a volume node with `volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", nodeName)` and update the volume with `updateVolumeFromArray`. The second I go to click on the visualize button the system crashes. Here is a link to my file: [https://drive.google.com/open?id=1C4RmyRS-4YWwO\_c6IhWOGEpV3ImGqVri](https://drive.google.com/open?id=1C4RmyRS-4YWwO_c6IhWOGEpV3ImGqVri)

---

<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:** [November 9, 2019, 1:59pm UTC](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091/4 "2019-11-09T13:59:35Z")

</div>

The link requires authentication. Please approve the read request or share with a public link.

---

<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:** [November 9, 2019, 2:38pm UTC](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091/5 "2019-11-09T14:38:50Z")

</div>

The problem was that the numpy array in that file had “long long” type. You had to cast it to a type that VTK can handle:

```auto
import numpy as np
filepath=r"c:\Users\andra\Downloads\am_output.npz"
volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
voxels=np.load(filepath)['arr_0'].astype(np.int16)
updateVolumeFromArray(volumeNode,voxels)
setSliceViewerLayers(background=volumeNode, fit=True)

```

I have updated `updateVolumeFromArray` to report an error when “long long” array is attempted to be loaded.
