# Importing Numpy & NPZ files into Slicer

**URL:** <https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497>\
**Category:** Development\
**Created:** [September 19, 2019, 3:05pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497 "2019-09-19T15:05:57Z")\
**Posts on this page:** 16\
**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:** [September 19, 2019, 3:05pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/1 "2019-09-19T15:05:57Z")

</div>

Hi, I have been working in python on a new deep learning algorithm. I would like to use Slicer to display the output to evaluate, but I am not sure how to import the output npz files into slicer for display, nor how to convert it into a SimpleITK image for proper orientation in Slicer and create a mesh from the image?

---

<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 19, 2019, 6:52pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/2 "2019-09-19T18:52:34Z")

</div>

You can set voxels in a volume node from numpy array by calling [`updateVolumeFromArray`](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.updateVolumeFromArray).

---

<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:** [September 19, 2019, 7:08pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/3 "2019-09-19T19:08:55Z")

</div>

How do I import the npz file in the first place though? When I drag and drop or direct the data import to the files, it doesn’t import anything.

---

<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:** [September 19, 2019, 7:10pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/4 "2019-09-19T19:10:25Z")

</div>

Also, I’m assuming this method correct the spacing in the normal simpleITK way? Where z,x,y get converted to x,y,z dimensions?

---

<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 19, 2019, 7:28pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/5 "2019-09-19T19:28:26Z")

</div>

You can load npz file into a numpy array using standard numpy commands (`numpy.load`).

If you load plain numpy arrays then you need to store metadata (such as origin, spacing, axis directions) before numpy array conversion, then retrieve it when needed. This may become easier in the future (see this [discussion](https://discourse.itk.org/t/images-in-physical-space-in-python/2124/15)), but for now there is no standard solution.

---

<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:** [September 19, 2019, 8:14pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/6 "2019-09-19T20:14:25Z")

</div>

I don’t understand. I looked at the documentation, but how do yoy store the metadata information to convert?

---

<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:** [September 19, 2019, 8:15pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/7 "2019-09-19T20:15:16Z")

</div>

Also, is there a way to import .nii.gz files into slicer?

---

<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 19, 2019, 11:07pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/8 "2019-09-19T23:07:12Z")

</div>

> [@stevenagl12](#):
>
> I don’t understand. I looked at the documentation, but how do yoy store the metadata information to convert?

If you load an image (nrrd, nift, etc.) then you have all the basic metadata. If you decide to convert to numpy then it is your responsibility to store the metadata somewhere where you can later find it (e.g., write to csv file that contains metadata for each file).

> [@stevenagl12](#):
>
> Also, is there a way to import .nii.gz files into slicer?

That’s a nifti file, you can load it as any other supported volume file formats: drag-and-drop the image file to the application window, using menu / File / Add data, or calling `slicer.util.loadVolume`.

---

<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:** [September 20, 2019, 3:45pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/9 "2019-09-20T15:45:08Z")

</div>

I have the metadata information from the original files, but when I convert them to numpy/tensor and back for pytorch, I would like to import them into slicer. So if I call in the file with np.load, and then use the updateVolumeFromArray command, how can I add the metadata to get the appropriate image?

---

<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 20, 2019, 5:33pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/10 "2019-09-20T17:33:13Z")

</div>

You can set origin, spacing, axis directions on the MRML volume node that you created from the numpy array.

---

<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:** [September 23, 2019, 3:01pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/11 "2019-09-23T15:01:33Z")

</div>

I have loaded in my numpy arrays, but when I go to run the updateVolumeFromArray, I am getting the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘GetImageData’. I have tried to change it to a SimpleITK image, and this is not working either.

---

<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 23, 2019, 3:10pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/12 "2019-09-23T15:10:26Z")

</div>

> [@stevenagl12](#):
>
> I am getting the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘GetImageData’.

You need to provide the output MRML node as first atgument and input numpy array as second argument. See [updateVolumeFromArray documentation](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.updateVolumeFromArray).

---

<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:** [September 23, 2019, 3:33pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/13 "2019-09-23T15:33:56Z")

</div>

I have been using that exact syntax. I created an empty volume called Mask and I have used the update volume from array function. This is the output:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/6/66043988ff7574be9c6fdcc66061d0231b0c784e.png)  
I have tried with quotations and without. As you can see I have the volume present in the data module, but it is saying there is no volume.

---

<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:** [September 23, 2019, 3:43pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/14 "2019-09-23T15:43:05Z")

</div>

Do we have to create a volume node prior to manipulating it with this function? Or can this function create the volume node as well?

---

<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 23, 2019, 3:50pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/15 "2019-09-23T15:50:30Z")

</div>

You have a different error now. You pass a “str” object instead of a volume node object. You need to provide a valid volume node as input to store the results. You can create a volume node as shown in [this example](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_a_new_volume).

---

<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, 5:59pm UTC](https://discourse.slicer.org/t/importing-numpy-npz-files-into-slicer/8497/16 "2019-11-08T17:59:52Z")

</div>

A post was split to a new topic: [Cannot import numpy array using updateVolumeFromArray](https://discourse.slicer.org/t/cannot-import-numpy-array-using-updatevolumefromarray/9091)
