pythonpython-3.9tomography-reconstructiontigre

How to save ndarray or show anim?


I am using TIGRE (an open source toolbox for reconstruction provided by CERN). The original code was written for python 3.7 and is struggling to run correctly on 3.9.

This is the original code and the error it produces.

#%% Initialize
import tigre
import numpy as np
from tigre.utilities import sample_loader
from tigre.utilities import CTnoise
import tigre.algorithms as algs

#%% Geometry
geo = tigre.geometry_default(high_resolution=False)

#%% Load data and generate projections
# define angles
angles = np.linspace(0, 2 * np.pi, 100)
# Load thorax phatom data
head = sample_loader.load_head_phantom(geo.nVoxel)
# generate projections
projections = tigre.Ax(head, geo, angles)
# add noise
noise_projections = CTnoise.add(projections, Poisson=1e5, Gaussian=np.array([0, 10]))

#%% Reconstruct image using OS-SART and FDK

# FDK
imgFDK = algs.fdk(noise_projections, geo, angles)
# OS-SART

niter = 50
imgOSSART = algs.ossart(noise_projections, geo, angles, niter)

imgOSSART.save

#%% Show the results
tigre.plotimg(np.concatenate([imgFDK, imgOSSART], axis=1), dim="z")

Error:

  File ~\TIGRE\Python\demos\d04_SimpleReconstruction.py:54 in <module>
    imgOSSART.save

AttributeError: 'numpy.ndarray' object has no attribute 'save'

When I remove the imgOSSART.save, the program runs but returns an empty figure:

<Figure size 432x288 with 0 Axes>
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\animation.py:889: UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. `anim`, that exists until you have outputted the Animation using `plt.show()` or `anim.save()`.
  warnings.warn(

Does anyone know a way to tackle this or to get python 3.7?


Solution

  • In numpy ndarrays are saved using the numpy.save function. So, try something like this instead

    np.save('imgOSSART.npy', imgOSSART)
    

    However, I don't think this solves the problem.


    The code snippet seems to be from here, which does not include np.save.

    To save the animation as a gif, you can try adding savegif parameter

    tigre.plotimg(
       np.concatenate([imgFDK, imgOSSART], axis=1), 
       dim="z", 
       savegif='filename.gif'
    )