Importing Numpy & NPZ files into Slicer

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?

You can set voxels in a volume node from numpy array by calling updateVolumeFromArray.

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.

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?

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), but for now there is no standard solution.

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

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

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).

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.

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?

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

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.

You need to provide the output MRML node as first atgument and input numpy array as second argument. See updateVolumeFromArray documentation.

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
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.

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?

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.

A post was split to a new topic: Cannot import numpy array using updateVolumeFromArray