diff options
Diffstat (limited to 'python/astra')
35 files changed, 3967 insertions, 0 deletions
diff --git a/python/astra/ASTRAProjector.py b/python/astra/ASTRAProjector.py new file mode 100644 index 0000000..f282618 --- /dev/null +++ b/python/astra/ASTRAProjector.py @@ -0,0 +1,135 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +import math +from . import creators as ac +from . import data2d + + +class ASTRAProjector2DTranspose(): + """Implements the ``proj.T`` functionality. + + Do not use directly, since it can be accessed as member ``.T`` of + an :class:`ASTRAProjector2D` object. + + """ + def __init__(self, parentProj): + self.parentProj = parentProj + + def __mul__(self, data): + return self.parentProj.backProject(data) + + +class ASTRAProjector2D(object): + """Helps with various common ASTRA Toolbox 2D operations. + + This class can perform several often used toolbox operations, such as: + + * Forward projecting + * Back projecting + * Reconstructing + + Note that this class has a some computational overhead, because it + copies a lot of data. If you use many repeated operations, directly + using the PyAstraToolbox methods directly is faster. + + You can use this class as an abstracted weight matrix :math:`W`: multiplying an instance + ``proj`` of this class by an image results in a forward projection of the image, and multiplying + ``proj.T`` by a sinogram results in a backprojection of the sinogram:: + + proj = ASTRAProjector2D(...) + fp = proj*image + bp = proj.T*sinogram + + :param proj_geom: The projection geometry. + :type proj_geom: :class:`dict` + :param vol_geom: The volume geometry. + :type vol_geom: :class:`dict` + :param proj_type: Projector type, such as ``'line'``, ``'linear'``, ... + :type proj_type: :class:`string` + """ + + def __init__(self, proj_geom, vol_geom, proj_type): + self.vol_geom = vol_geom + self.recSize = vol_geom['GridColCount'] + self.angles = proj_geom['ProjectionAngles'] + self.nDet = proj_geom['DetectorCount'] + nexpow = int(pow(2, math.ceil(math.log(2 * self.nDet, 2)))) + self.filterSize = nexpow / 2 + 1 + self.nProj = self.angles.shape[0] + self.proj_geom = proj_geom + self.proj_id = ac.create_projector(proj_type, proj_geom, vol_geom) + self.T = ASTRAProjector2DTranspose(self) + + def backProject(self, data): + """Backproject a sinogram. + + :param data: The sinogram data or ID. + :type data: :class:`numpy.ndarray` or :class:`int` + :returns: :class:`numpy.ndarray` -- The backprojection. + + """ + vol_id, vol = ac.create_backprojection( + data, self.proj_id, returnData=True) + data2d.delete(vol_id) + return vol + + def forwardProject(self, data): + """Forward project an image. + + :param data: The image data or ID. + :type data: :class:`numpy.ndarray` or :class:`int` + :returns: :class:`numpy.ndarray` -- The forward projection. + + """ + sin_id, sino = ac.create_sino(data, self.proj_id, returnData=True) + data2d.delete(sin_id) + return sino + + def reconstruct(self, data, method, **kwargs): + """Reconstruct an image from a sinogram. + + :param data: The sinogram data or ID. + :type data: :class:`numpy.ndarray` or :class:`int` + :param method: Name of the reconstruction algorithm. + :type method: :class:`string` + :param kwargs: Additional named parameters to pass to :func:`astra.creators.create_reconstruction`. + :returns: :class:`numpy.ndarray` -- The reconstruction. + + Example of a SIRT reconstruction using CUDA:: + + proj = ASTRAProjector2D(...) + rec = proj.reconstruct(sinogram,'SIRT_CUDA',iterations=1000) + + """ + kwargs['returnData'] = True + rec_id, rec = ac.create_reconstruction( + method, self.proj_id, data, **kwargs) + data2d.delete(rec_id) + return rec + + def __mul__(self, data): + return self.forwardProject(data) diff --git a/python/astra/PyAlgorithmFactory.pxd b/python/astra/PyAlgorithmFactory.pxd new file mode 100644 index 0000000..256d7b2 --- /dev/null +++ b/python/astra/PyAlgorithmFactory.pxd @@ -0,0 +1,36 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string +from libcpp cimport bool +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectFactory.h" namespace "astra": + cdef cppclass CAlgorithmFactory: + CAlgorithmFactory() + CAlgorithm *create(string) + +cdef extern from "astra/AstraObjectFactory.h" namespace "astra::CAlgorithmFactory": + cdef CAlgorithmFactory* getSingletonPtr() diff --git a/python/astra/PyAlgorithmManager.pxd b/python/astra/PyAlgorithmManager.pxd new file mode 100644 index 0000000..a99a807 --- /dev/null +++ b/python/astra/PyAlgorithmManager.pxd @@ -0,0 +1,40 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string +from libcpp cimport bool +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CAlgorithmManager: + int store(CAlgorithm *) + CAlgorithm * get(int) + void remove(int) + void clear() + string info() + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CAlgorithmManager": + cdef CAlgorithmManager* getSingletonPtr() + diff --git a/python/astra/PyData2DManager.pxd b/python/astra/PyData2DManager.pxd new file mode 100644 index 0000000..db8ec84 --- /dev/null +++ b/python/astra/PyData2DManager.pxd @@ -0,0 +1,39 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string + +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CData2DManager: + string info() + void clear() + void remove(int i) + int store(CFloat32Data2D *) + CFloat32Data2D * get(int i) + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CData2DManager": + cdef CData2DManager* getSingletonPtr() diff --git a/python/astra/PyData3DManager.pxd b/python/astra/PyData3DManager.pxd new file mode 100644 index 0000000..9264a82 --- /dev/null +++ b/python/astra/PyData3DManager.pxd @@ -0,0 +1,39 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string + +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CData3DManager: + string info() + void clear() + void remove(int i) + int store(CFloat32Data3D *) + CFloat32Data3D * get(int i) + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CData3DManager": + cdef CData3DManager* getSingletonPtr() diff --git a/python/astra/PyIncludes.pxd b/python/astra/PyIncludes.pxd new file mode 100644 index 0000000..7df02c5 --- /dev/null +++ b/python/astra/PyIncludes.pxd @@ -0,0 +1,244 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp cimport bool +from libcpp.string cimport string +from .PyXMLDocument cimport XMLNode + +include "config.pxi" + +cdef extern from "astra/Globals.h" namespace "astra": + ctypedef float float32 + ctypedef double float64 + ctypedef unsigned short int uint16 + ctypedef signed short int sint16 + ctypedef unsigned char uchar8 + ctypedef signed char schar8 + ctypedef int int32 + ctypedef short int int16 + +cdef extern from "astra/Config.h" namespace "astra": + cdef cppclass Config: + Config() + void initialize(string rootname) + XMLNode *self + +cdef extern from "astra/VolumeGeometry2D.h" namespace "astra": + cdef cppclass CVolumeGeometry2D: + bool initialize(Config) + int getGridColCount() + int getGridRowCount() + int getGridTotCount() + float32 getWindowLengthX() + float32 getWindowLengthY() + float32 getWindowArea() + float32 getPixelLengthX() + float32 getPixelLengthY() + float32 getPixelArea() + float32 getWindowMinX() + float32 getWindowMinY() + float32 getWindowMaxX() + float32 getWindowMaxY() + Config* getConfiguration() + + +cdef extern from "astra/Float32VolumeData2D.h" namespace "astra": + cdef cppclass CFloat32VolumeData2D: + CFloat32VolumeData2D(CVolumeGeometry2D*) + CVolumeGeometry2D * getGeometry() + int getWidth() + int getHeight() + void changeGeometry(CVolumeGeometry2D*) + Config* getConfiguration() + + + +cdef extern from "astra/ProjectionGeometry2D.h" namespace "astra": + cdef cppclass CProjectionGeometry2D: + CProjectionGeometry2D() + bool initialize(Config) + int getDetectorCount() + int getProjectionAngleCount() + bool isOfType(string) + float32 getProjectionAngle(int) + float32 getDetectorWidth() + Config* getConfiguration() + +cdef extern from "astra/Float32Data2D.h" namespace "astra::CFloat32Data2D": + cdef enum TWOEDataType "astra::CFloat32Data2D::EDataType": + TWOPROJECTION "astra::CFloat32Data2D::PROJECTION" + TWOVOLUME "astra::CFloat32Data2D::VOLUME" + +cdef extern from "astra/Float32Data3D.h" namespace "astra::CFloat32Data3D": + cdef enum THREEEDataType "astra::CFloat32Data3D::EDataType": + THREEPROJECTION "astra::CFloat32Data3D::PROJECTION" + THREEVOLUME "astra::CFloat32Data3D::VOLUME" + + +cdef extern from "astra/Float32Data2D.h" namespace "astra": + cdef cppclass CFloat32Data2D: + bool isInitialized() + int getSize() + float32 *getData() + float32 **getData2D() + int getWidth() + int getHeight() + TWOEDataType getType() + + + + +cdef extern from "astra/SparseMatrixProjectionGeometry2D.h" namespace "astra": + cdef cppclass CSparseMatrixProjectionGeometry2D: + CSparseMatrixProjectionGeometry2D() + +cdef extern from "astra/FanFlatProjectionGeometry2D.h" namespace "astra": + cdef cppclass CFanFlatProjectionGeometry2D: + CFanFlatProjectionGeometry2D() + +cdef extern from "astra/FanFlatVecProjectionGeometry2D.h" namespace "astra": + cdef cppclass CFanFlatVecProjectionGeometry2D: + CFanFlatVecProjectionGeometry2D() + +cdef extern from "astra/ParallelProjectionGeometry2D.h" namespace "astra": + cdef cppclass CParallelProjectionGeometry2D: + CParallelProjectionGeometry2D() + + +cdef extern from "astra/Float32ProjectionData2D.h" namespace "astra": + cdef cppclass CFloat32ProjectionData2D: + CFloat32ProjectionData2D(CProjectionGeometry2D*) + CProjectionGeometry2D * getGeometry() + void changeGeometry(CProjectionGeometry2D*) + int getDetectorCount() + int getAngleCount() + +cdef extern from "astra/Algorithm.h" namespace "astra": + cdef cppclass CAlgorithm: + bool initialize(Config) + void run(int) + bool isInitialized() + +cdef extern from "astra/ReconstructionAlgorithm2D.h" namespace "astra": + cdef cppclass CReconstructionAlgorithm2D: + bool getResidualNorm(float32&) + +cdef extern from "astra/Projector2D.h" namespace "astra": + cdef cppclass CProjector2D: + bool isInitialized() + CProjectionGeometry2D* getProjectionGeometry() + CVolumeGeometry2D* getVolumeGeometry() + CSparseMatrix* getMatrix() + +cdef extern from "astra/Projector3D.h" namespace "astra": + cdef cppclass CProjector3D: + bool isInitialized() + CProjectionGeometry3D* getProjectionGeometry() + CVolumeGeometry3D* getVolumeGeometry() + +IF HAVE_CUDA==True: + cdef extern from "astra/CudaProjector3D.h" namespace "astra": + cdef cppclass CCudaProjector3D + + cdef extern from "astra/CudaProjector2D.h" namespace "astra": + cdef cppclass CCudaProjector2D + + +cdef extern from "astra/SparseMatrix.h" namespace "astra": + cdef cppclass CSparseMatrix: + CSparseMatrix(unsigned int,unsigned int,unsigned long) + unsigned int m_iWidth + unsigned int m_iHeight + unsigned long m_lSize + bool isInitialized() + float32* m_pfValues + unsigned int* m_piColIndices + unsigned long* m_plRowStarts + +cdef extern from "astra/Float32Data3DMemory.h" namespace "astra": + cdef cppclass CFloat32Data3DMemory: + CFloat32Data3DMemory() + bool isInitialized() + int getSize() + int getWidth() + int getHeight() + int getDepth() + void updateStatistics() + float32 *getData() + float32 ***getData3D() + THREEEDataType getType() + + +cdef extern from "astra/VolumeGeometry3D.h" namespace "astra": + cdef cppclass CVolumeGeometry3D: + CVolumeGeometry3D() + bool initialize(Config) + Config * getConfiguration() + +cdef extern from "astra/ProjectionGeometry3D.h" namespace "astra": + cdef cppclass CProjectionGeometry3D: + CProjectionGeometry3D() + bool initialize(Config) + Config * getConfiguration() + + +cdef extern from "astra/Float32VolumeData3DMemory.h" namespace "astra": + cdef cppclass CFloat32VolumeData3DMemory: + CFloat32VolumeData3DMemory(CVolumeGeometry3D*) + CVolumeGeometry3D* getGeometry() + + +cdef extern from "astra/ParallelProjectionGeometry3D.h" namespace "astra": + cdef cppclass CParallelProjectionGeometry3D: + CParallelProjectionGeometry3D() + +cdef extern from "astra/ParallelVecProjectionGeometry3D.h" namespace "astra": + cdef cppclass CParallelVecProjectionGeometry3D: + CParallelVecProjectionGeometry3D() + +cdef extern from "astra/ConeProjectionGeometry3D.h" namespace "astra": + cdef cppclass CConeProjectionGeometry3D: + CConeProjectionGeometry3D() + bool initialize(Config) + +cdef extern from "astra/ConeVecProjectionGeometry3D.h" namespace "astra": + cdef cppclass CConeVecProjectionGeometry3D: + CConeVecProjectionGeometry3D() + +cdef extern from "astra/Float32ProjectionData3DMemory.h" namespace "astra": + cdef cppclass CFloat32ProjectionData3DMemory: + CFloat32ProjectionData3DMemory(CProjectionGeometry3D*) + CFloat32ProjectionData3DMemory(CConeProjectionGeometry3D*) + CProjectionGeometry3D* getGeometry() + +cdef extern from "astra/Float32Data3D.h" namespace "astra": + cdef cppclass CFloat32Data3D: + CFloat32Data3D() + bool isInitialized() + int getSize() + int getWidth() + int getHeight() + int getDepth() + void updateStatistics() diff --git a/python/astra/PyMatrixManager.pxd b/python/astra/PyMatrixManager.pxd new file mode 100644 index 0000000..b2b84c4 --- /dev/null +++ b/python/astra/PyMatrixManager.pxd @@ -0,0 +1,39 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string + +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CMatrixManager: + string info() + void clear() + void remove(int i) + int store(CSparseMatrix *) + CSparseMatrix * get(int i) + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CMatrixManager": + cdef CMatrixManager* getSingletonPtr() diff --git a/python/astra/PyProjector2DFactory.pxd b/python/astra/PyProjector2DFactory.pxd new file mode 100644 index 0000000..3314544 --- /dev/null +++ b/python/astra/PyProjector2DFactory.pxd @@ -0,0 +1,35 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string +from libcpp cimport bool +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectFactory.h" namespace "astra": + cdef cppclass CProjector2DFactory: + CProjector2D *create(Config) + +cdef extern from "astra/AstraObjectFactory.h" namespace "astra::CProjector2DFactory": + cdef CProjector2DFactory* getSingletonPtr() diff --git a/python/astra/PyProjector2DManager.pxd b/python/astra/PyProjector2DManager.pxd new file mode 100644 index 0000000..92176ba --- /dev/null +++ b/python/astra/PyProjector2DManager.pxd @@ -0,0 +1,39 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string + +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CProjector2DManager: + string info() + void clear() + void remove(int i) + int store(CProjector2D *) + CProjector2D * get(int i) + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CProjector2DManager": + cdef CProjector2DManager* getSingletonPtr() diff --git a/python/astra/PyProjector3DFactory.pxd b/python/astra/PyProjector3DFactory.pxd new file mode 100644 index 0000000..bcbce94 --- /dev/null +++ b/python/astra/PyProjector3DFactory.pxd @@ -0,0 +1,35 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string +from libcpp cimport bool +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectFactory.h" namespace "astra": + cdef cppclass CProjector3DFactory: + CProjector3D *create(Config) + +cdef extern from "astra/AstraObjectFactory.h" namespace "astra::CProjector3DFactory": + cdef CProjector3DFactory* getSingletonPtr() diff --git a/python/astra/PyProjector3DManager.pxd b/python/astra/PyProjector3DManager.pxd new file mode 100644 index 0000000..b1eac6b --- /dev/null +++ b/python/astra/PyProjector3DManager.pxd @@ -0,0 +1,39 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string + +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CProjector3DManager: + string info() + void clear() + void remove(int i) + int store(CProjector3D *) + CProjector3D * get(int i) + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CProjector3DManager": + cdef CProjector3DManager* getSingletonPtr() diff --git a/python/astra/PyXMLDocument.pxd b/python/astra/PyXMLDocument.pxd new file mode 100644 index 0000000..69781f1 --- /dev/null +++ b/python/astra/PyXMLDocument.pxd @@ -0,0 +1,65 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +from libcpp.string cimport string +from libcpp cimport bool +from libcpp.list cimport list +from libcpp.vector cimport vector + +cdef extern from "astra/Globals.h" namespace "astra": + ctypedef float float32 + ctypedef double float64 + ctypedef unsigned short int uint16 + ctypedef signed short int sint16 + ctypedef unsigned char uchar8 + ctypedef signed char schar8 + ctypedef int int32 + ctypedef short int int16 + +cdef extern from "astra/XMLNode.h" namespace "astra": + cdef cppclass XMLNode: + string getName() + XMLNode *addChildNode(string name) + XMLNode *addChildNode(string, string) + void addAttribute(string, string) + void addAttribute(string, float32) + void addOption(string, string) + bool hasOption(string) + string getAttribute(string) + list[XMLNode *] getNodes() + vector[float32] getContentNumericalArray() + string getContent() + bool hasAttribute(string) + +cdef extern from "astra/XMLDocument.h" namespace "astra": + cdef cppclass XMLDocument: + void saveToFile(string sFilename) + XMLNode *getRootNode() + +cdef extern from "astra/XMLDocument.h" namespace "astra::XMLDocument": + cdef XMLDocument *createDocument(string rootname) diff --git a/python/astra/__init__.py b/python/astra/__init__.py new file mode 100644 index 0000000..063dc16 --- /dev/null +++ b/python/astra/__init__.py @@ -0,0 +1,44 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from . import matlab as m +from .creators import astra_dict,create_vol_geom, create_proj_geom, create_backprojection, create_sino, create_reconstruction, create_projector,create_sino3d_gpu, create_backprojection3d_gpu +from .functions import data_op, add_noise_to_sino, clear, move_vol_geom +from .extrautils import clipCircle +from .ASTRAProjector import ASTRAProjector2D +from . import data2d +from . import astra +from . import data3d +from . import algorithm +from . import projector +from . import projector3d +from . import matrix +from . import log + +import os +try: + astra.set_gpu_index(int(os.environ['ASTRA_GPU_INDEX'])) +except KeyError: + pass diff --git a/python/astra/algorithm.py b/python/astra/algorithm.py new file mode 100644 index 0000000..46cfccc --- /dev/null +++ b/python/astra/algorithm.py @@ -0,0 +1,76 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +from . import algorithm_c as a + +def create(config): + """Create algorithm object. + + :param config: Algorithm options. + :type config: :class:`dict` + :returns: :class:`int` -- the ID of the constructed object. + + """ + return a.create(config) + +def run(i, iterations=1): + """Run an algorithm. + + :param i: ID of object. + :type i: :class:`int` + :param iterations: Number of iterations to run. + :type iterations: :class:`int` + + """ + return a.run(i,iterations) + +def get_res_norm(i): + """Get residual norm of algorithm. + + :param i: ID of object. + :type i: :class:`int` + :returns: :class:`float` -- The residual norm. + + """ + + return a.get_res_norm(i) + +def delete(ids): + """Delete a matrix object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return a.delete(ids) + +def clear(): + """Clear all matrix objects.""" + return a.clear() + +def info(): + """Print info on matrix objects in memory.""" + return a.info() diff --git a/python/astra/algorithm_c.pyx b/python/astra/algorithm_c.pyx new file mode 100644 index 0000000..966d3d7 --- /dev/null +++ b/python/astra/algorithm_c.pyx @@ -0,0 +1,104 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +# distutils: language = c++ +# distutils: libraries = astra + +import six +from .PyIncludes cimport * + +cimport PyAlgorithmManager +from .PyAlgorithmManager cimport CAlgorithmManager + +cimport PyAlgorithmFactory +from .PyAlgorithmFactory cimport CAlgorithmFactory + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument + +cimport utils +from .utils import wrap_from_bytes + +cdef CAlgorithmManager * manAlg = <CAlgorithmManager * >PyAlgorithmManager.getSingletonPtr() + +cdef extern from *: + CReconstructionAlgorithm2D * dynamic_cast_recAlg "dynamic_cast<CReconstructionAlgorithm2D*>" (CAlgorithm * ) except NULL + + +def create(config): + cdef Config * cfg = utils.dictToConfig(six.b('Algorithm'), config) + cdef CAlgorithm * alg + alg = PyAlgorithmFactory.getSingletonPtr().create(cfg.self.getAttribute(six.b('type'))) + if alg == NULL: + del cfg + raise Exception("Unknown algorithm.") + if not alg.initialize(cfg[0]): + del cfg + del alg + raise Exception("Algorithm not initialized.") + del cfg + return manAlg.store(alg) + +cdef CAlgorithm * getAlg(i) except NULL: + cdef CAlgorithm * alg = manAlg.get(i) + if alg == NULL: + raise Exception("Unknown algorithm.") + if not alg.isInitialized(): + raise Exception("Algorithm not initialized.") + return alg + + +def run(i, iterations=0): + cdef CAlgorithm * alg = getAlg(i) + alg.run(iterations) + + +def get_res_norm(i): + cdef CReconstructionAlgorithm2D * pAlg2D + cdef CAlgorithm * alg = getAlg(i) + cdef float32 res = 0.0 + pAlg2D = dynamic_cast_recAlg(alg) + if pAlg2D == NULL: + raise Exception("Operation not supported.") + if not pAlg2D.getResidualNorm(res): + raise Exception("Operation not supported.") + return res + + +def delete(ids): + try: + for i in ids: + manAlg.remove(i) + except TypeError: + manAlg.remove(ids) + + +def clear(): + manAlg.clear() + + +def info(): + six.print_(wrap_from_bytes(manAlg.info())) diff --git a/python/astra/astra.py b/python/astra/astra.py new file mode 100644 index 0000000..26b1ff0 --- /dev/null +++ b/python/astra/astra.py @@ -0,0 +1,58 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +from . import astra_c as a + +def credits(): + """Print credits of the ASTRA Toolbox.""" + return a.credits() + + +def use_cuda(): + """Test if CUDA is enabled. + + :returns: :class:`bool` -- ``True`` if CUDA is enabled. + """ + return a.use_cuda() + + +def version(printToScreen=False): + """Check version of the ASTRA Toolbox. + + :param printToScreen: If ``True``, print version string. If ``False``, return version integer. + :type printToScreen: :class:`bool` + :returns: :class:`string` or :class:`int` -- The version string or integer. + + """ + return a.version(printToScreen) + +def set_gpu_index(idx): + """Set default GPU index to use. + + :param idx: GPU index + :type idx: :class:`int` + """ + a.set_gpu_index(idx) diff --git a/python/astra/astra_c.pyx b/python/astra/astra_c.pyx new file mode 100644 index 0000000..342a214 --- /dev/null +++ b/python/astra/astra_c.pyx @@ -0,0 +1,79 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +include "config.pxi" +import six +from .utils import wrap_from_bytes + +from libcpp.string cimport string +from libcpp cimport bool +cdef extern from "astra/Globals.h" namespace "astra": + int getVersion() + string getVersionString() + bool cudaEnabled() + +IF HAVE_CUDA==True: + cdef extern from "../cuda/2d/darthelper.h" namespace "astraCUDA": + bool setGPUIndex(int) +ELSE: + def setGPUIndex(): + pass + +def credits(): + six.print_(""" +All Scale Tomographic Reconstruction Antwerp Toolbox (ASTRA-Toolbox) +was developed at the University of Antwerp by + * Prof. dr. Joost Batenburg + * Andrei Dabravolski + * Gert Merckx + * Willem Jan Palenstijn + * Tom Roelandts + * Prof. dr. Jan Sijbers + * dr. Wim van Aarle + * Sander van der Maar + * dr. Gert Van Gompel + +Python interface written by + * Daniel M. Pelt (CWI, Amsterdam)""") + + +def use_cuda(): + return cudaEnabled() + + +def version(printToScreen=False): + if printToScreen: + six.print_(wrap_from_bytes(getVersionString())) + else: + return getVersion() + +def set_gpu_index(idx): + if use_cuda()==True: + ret = setGPUIndex(idx) + if not ret: + six.print_("Failed to set GPU " + str(idx)) diff --git a/python/astra/creators.py b/python/astra/creators.py new file mode 100644 index 0000000..68bc8a2 --- /dev/null +++ b/python/astra/creators.py @@ -0,0 +1,543 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +import six +import numpy as np +import math +from . import data2d +from . import data3d +from . import projector +from . import projector3d +from . import algorithm + +def astra_dict(intype): + """Creates a dict to use with the ASTRA Toolbox. + + :param intype: Type of the ASTRA object. + :type intype: :class:`string` + :returns: :class:`dict` -- An ASTRA dict of type ``intype``. + + """ + if intype == 'SIRT_CUDA2': + intype = 'SIRT_CUDA' + six.print_('SIRT_CUDA2 has been deprecated. Use SIRT_CUDA instead.') + elif intype == 'FP_CUDA2': + intype = 'FP_CUDA' + six.print_('FP_CUDA2 has been deprecated. Use FP_CUDA instead.') + return {'type': intype} + +def create_vol_geom(*varargin): + """Create a volume geometry structure. + +This method can be called in a number of ways: + +``create_vol_geom(N)``: + :returns: A 2D volume geometry of size :math:`N \\times N`. + +``create_vol_geom((M, N))``: + :returns: A 2D volume geometry of size :math:`M \\times N`. + +``create_vol_geom(M, N)``: + :returns: A 2D volume geometry of size :math:`M \\times N`. + +``create_vol_geom(M, N, minx, maxx, miny, maxy)``: + :returns: A 2D volume geometry of size :math:`M \\times N`, windowed as :math:`minx \\leq x \\leq maxx` and :math:`miny \\leq y \\leq maxy`. + +``create_vol_geom((M, N, Z))``: + :returns: A 3D volume geometry of size :math:`M \\times N \\times Z`. + +``create_vol_geom(M, N, Z)``: + :returns: A 3D volume geometry of size :math:`M \\times N \\times Z`. + +""" + vol_geom = {'option': {}} + # astra_create_vol_geom(row_count) + if len(varargin) == 1 and isinstance(varargin[0], int) == 1: + vol_geom['GridRowCount'] = varargin[0] + vol_geom['GridColCount'] = varargin[0] + vol_geom['option']['WindowMinX'] = -varargin[0] / 2. + vol_geom['option']['WindowMaxX'] = varargin[0] / 2. + vol_geom['option']['WindowMinY'] = -varargin[0] / 2. + vol_geom['option']['WindowMaxY'] = varargin[0] / 2. + # astra_create_vol_geom([row_count col_count]) + elif len(varargin) == 1 and len(varargin[0]) == 2: + vol_geom['GridRowCount'] = varargin[0][0] + vol_geom['GridColCount'] = varargin[0][1] + vol_geom['option']['WindowMinX'] = -varargin[0][1] / 2. + vol_geom['option']['WindowMaxX'] = varargin[0][1] / 2. + vol_geom['option']['WindowMinY'] = -varargin[0][0] / 2. + vol_geom['option']['WindowMaxY'] = varargin[0][0] / 2. + # astra_create_vol_geom([row_count col_count slice_count]) + elif len(varargin) == 1 and len(varargin[0]) == 3: + vol_geom['GridRowCount'] = varargin[0][0] + vol_geom['GridColCount'] = varargin[0][1] + vol_geom['GridSliceCount'] = varargin[0][2] + vol_geom['option']['WindowMinX'] = -varargin[0][1] / 2. + vol_geom['option']['WindowMaxX'] = varargin[0][1] / 2. + vol_geom['option']['WindowMinY'] = -varargin[0][0] / 2. + vol_geom['option']['WindowMaxY'] = varargin[0][0] / 2. + vol_geom['option']['WindowMinZ'] = -varargin[0][2] / 2. + vol_geom['option']['WindowMaxZ'] = varargin[0][2] / 2. + # astra_create_vol_geom(row_count, col_count) + elif len(varargin) == 2: + vol_geom['GridRowCount'] = varargin[0] + vol_geom['GridColCount'] = varargin[1] + vol_geom['option']['WindowMinX'] = -varargin[1] / 2. + vol_geom['option']['WindowMaxX'] = varargin[1] / 2. + vol_geom['option']['WindowMinY'] = -varargin[0] / 2. + vol_geom['option']['WindowMaxY'] = varargin[0] / 2. + # astra_create_vol_geom(row_count, col_count, min_x, max_x, min_y, max_y) + elif len(varargin) == 6: + vol_geom['GridRowCount'] = varargin[0] + vol_geom['GridColCount'] = varargin[1] + vol_geom['option']['WindowMinX'] = varargin[2] + vol_geom['option']['WindowMaxX'] = varargin[3] + vol_geom['option']['WindowMinY'] = varargin[4] + vol_geom['option']['WindowMaxY'] = varargin[5] + # astra_create_vol_geom(row_count, col_count, slice_count) + elif len(varargin) == 3: + vol_geom['GridRowCount'] = varargin[0] + vol_geom['GridColCount'] = varargin[1] + vol_geom['GridSliceCount'] = varargin[2] + return vol_geom + + +def create_proj_geom(intype, *args): + """Create a projection geometry. + +This method can be called in a number of ways: + +``create_proj_geom('parallel', detector_spacing, det_count, angles)``: + +:param detector_spacing: Distance between two adjacent detector pixels. +:type detector_spacing: :class:`float` +:param det_count: Number of detector pixels. +:type det_count: :class:`int` +:param angles: Array of angles in radians. +:type angles: :class:`numpy.ndarray` +:returns: A parallel projection geometry. + + +``create_proj_geom('fanflat', det_width, det_count, angles, source_origin, source_det)``: + +:param det_width: Size of a detector pixel. +:type det_width: :class:`float` +:param det_count: Number of detector pixels. +:type det_count: :class:`int` +:param angles: Array of angles in radians. +:type angles: :class:`numpy.ndarray` +:param source_origin: Position of the source. +:param source_det: Position of the detector +:returns: A fan-beam projection geometry. + +``create_proj_geom('fanflat_vec', det_count, V)``: + +:param det_count: Number of detector pixels. +:type det_count: :class:`int` +:param V: Vector array. +:type V: :class:`numpy.ndarray` +:returns: A fan-beam projection geometry. + +``create_proj_geom('parallel3d', detector_spacing_x, detector_spacing_y, det_row_count, det_col_count, angles)``: + +:param detector_spacing_*: Distance between two adjacent detector pixels. +:type detector_spacing_*: :class:`float` +:param det_row_count: Number of detector pixel rows. +:type det_row_count: :class:`int` +:param det_col_count: Number of detector pixel columns. +:type det_col_count: :class:`int` +:param angles: Array of angles in radians. +:type angles: :class:`numpy.ndarray` +:returns: A parallel projection geometry. + +``create_proj_geom('cone', detector_spacing_x, detector_spacing_y, det_row_count, det_col_count, angles, source_origin, source_det)``: + +:param detector_spacing_*: Distance between two adjacent detector pixels. +:type detector_spacing_*: :class:`float` +:param det_row_count: Number of detector pixel rows. +:type det_row_count: :class:`int` +:param det_col_count: Number of detector pixel columns. +:type det_col_count: :class:`int` +:param angles: Array of angles in radians. +:type angles: :class:`numpy.ndarray` +:param source_origin: Distance between point source and origin. +:type source_origin: :class:`float` +:param source_det: Distance between the detector and origin. +:type source_det: :class:`float` +:returns: A cone-beam projection geometry. + +``create_proj_geom('cone_vec', det_row_count, det_col_count, V)``: + +:param det_row_count: Number of detector pixel rows. +:type det_row_count: :class:`int` +:param det_col_count: Number of detector pixel columns. +:type det_col_count: :class:`int` +:param V: Vector array. +:type V: :class:`numpy.ndarray` +:returns: A cone-beam projection geometry. + +``create_proj_geom('parallel3d_vec', det_row_count, det_col_count, V)``: + +:param det_row_count: Number of detector pixel rows. +:type det_row_count: :class:`int` +:param det_col_count: Number of detector pixel columns. +:type det_col_count: :class:`int` +:param V: Vector array. +:type V: :class:`numpy.ndarray` +:returns: A parallel projection geometry. + +``create_proj_geom('sparse_matrix', det_width, det_count, angles, matrix_id)``: + +:param det_width: Size of a detector pixel. +:type det_width: :class:`float` +:param det_count: Number of detector pixels. +:type det_count: :class:`int` +:param angles: Array of angles in radians. +:type angles: :class:`numpy.ndarray` +:param matrix_id: ID of the sparse matrix. +:type matrix_id: :class:`int` +:returns: A projection geometry based on a sparse matrix. + +""" + if intype == 'parallel': + if len(args) < 3: + raise Exception( + 'not enough variables: astra_create_proj_geom(parallel, detector_spacing, det_count, angles)') + return {'type': 'parallel', 'DetectorWidth': args[0], 'DetectorCount': args[1], 'ProjectionAngles': args[2]} + elif intype == 'fanflat': + if len(args) < 5: + raise Exception('not enough variables: astra_create_proj_geom(fanflat, det_width, det_count, angles, source_origin, source_det)') + return {'type': 'fanflat', 'DetectorWidth': args[0], 'DetectorCount': args[1], 'ProjectionAngles': args[2], 'DistanceOriginSource': args[3], 'DistanceOriginDetector': args[4]} + elif intype == 'fanflat_vec': + if len(args) < 2: + raise Exception('not enough variables: astra_create_proj_geom(fanflat_vec, det_count, V)') + if not args[1].shape[1] == 6: + raise Exception('V should be a Nx6 matrix, with N the number of projections') + return {'type':'fanflat_vec', 'DetectorCount':args[0], 'Vectors':args[1]} + elif intype == 'parallel3d': + if len(args) < 5: + raise Exception('not enough variables: astra_create_proj_geom(parallel3d, detector_spacing_x, detector_spacing_y, det_row_count, det_col_count, angles)') + return {'type':'parallel3d', 'DetectorSpacingX':args[0], 'DetectorSpacingY':args[1], 'DetectorRowCount':args[2], 'DetectorColCount':args[3],'ProjectionAngles':args[4]} + elif intype == 'cone': + if len(args) < 7: + raise Exception('not enough variables: astra_create_proj_geom(cone, detector_spacing_x, detector_spacing_y, det_row_count, det_col_count, angles, source_origin, source_det)') + return {'type': 'cone','DetectorSpacingX':args[0], 'DetectorSpacingY':args[1], 'DetectorRowCount':args[2],'DetectorColCount':args[3],'ProjectionAngles':args[4],'DistanceOriginSource': args[5],'DistanceOriginDetector':args[6]} + elif intype == 'cone_vec': + if len(args) < 3: + raise Exception('not enough variables: astra_create_proj_geom(cone_vec, det_row_count, det_col_count, V)') + if not args[2].shape[1] == 12: + raise Exception('V should be a Nx12 matrix, with N the number of projections') + return {'type': 'cone_vec','DetectorRowCount':args[0],'DetectorColCount':args[1],'Vectors':args[2]} + elif intype == 'parallel3d_vec': + if len(args) < 3: + raise Exception('not enough variables: astra_create_proj_geom(parallel3d_vec, det_row_count, det_col_count, V)') + if not args[2].shape[1] == 12: + raise Exception('V should be a Nx12 matrix, with N the number of projections') + return {'type': 'parallel3d_vec','DetectorRowCount':args[0],'DetectorColCount':args[1],'Vectors':args[2]} + elif intype == 'sparse_matrix': + if len(args) < 4: + raise Exception( + 'not enough variables: astra_create_proj_geom(sparse_matrix, det_width, det_count, angles, matrix_id)') + return {'type': 'sparse_matrix', 'DetectorWidth': args[0], 'DetectorCount': args[1], 'ProjectionAngles': args[2], 'MatrixID': args[3]} + else: + raise Exception('Error: unknown type ' + intype) + + +def create_backprojection(data, proj_id, returnData=True): + """Create a backprojection of a sinogram (2D). + +:param data: Sinogram data or ID. +:type data: :class:`numpy.ndarray` or :class:`int` +:param proj_id: ID of the projector to use. +:type proj_id: :class:`int` +:param returnData: If False, only return the ID of the backprojection. +:type returnData: :class:`bool` +:returns: :class:`int` or (:class:`int`, :class:`numpy.ndarray`) -- If ``returnData=False``, returns the ID of the backprojection. Otherwise, returns a tuple containing the ID of the backprojection and the backprojection itself, in that order. + +""" + proj_geom = projector.projection_geometry(proj_id) + vol_geom = projector.volume_geometry(proj_id) + if isinstance(data, np.ndarray): + sino_id = data2d.create('-sino', proj_geom, data) + else: + sino_id = data + vol_id = data2d.create('-vol', vol_geom, 0) + + if projector.is_cuda(proj_id): + algString = 'BP_CUDA' + else: + algString = 'BP' + + cfg = astra_dict(algString) + cfg['ProjectorId'] = proj_id + cfg['ProjectionDataId'] = sino_id + cfg['ReconstructionDataId'] = vol_id + alg_id = algorithm.create(cfg) + algorithm.run(alg_id) + algorithm.delete(alg_id) + + if isinstance(data, np.ndarray): + data2d.delete(sino_id) + + if returnData: + return vol_id, data2d.get(vol_id) + else: + return vol_id + +def create_backprojection3d_gpu(data, proj_geom, vol_geom, returnData=True): + """Create a backprojection of a sinogram (3D) using CUDA. + +:param data: Sinogram data or ID. +:type data: :class:`numpy.ndarray` or :class:`int` +:param proj_geom: Projection geometry. +:type proj_geom: :class:`dict` +:param vol_geom: Volume geometry. +:type vol_geom: :class:`dict` +:param returnData: If False, only return the ID of the backprojection. +:type returnData: :class:`bool` +:returns: :class:`int` or (:class:`int`, :class:`numpy.ndarray`) -- If ``returnData=False``, returns the ID of the backprojection. Otherwise, returns a tuple containing the ID of the backprojection and the backprojection itself, in that order. + +""" + if isinstance(data, np.ndarray): + sino_id = data3d.create('-sino', proj_geom, data) + else: + sino_id = data + + vol_id = data3d.create('-vol', vol_geom, 0) + + cfg = astra_dict('BP3D_CUDA') + cfg['ProjectionDataId'] = sino_id + cfg['ReconstructionDataId'] = vol_id + alg_id = algorithm.create(cfg) + algorithm.run(alg_id) + algorithm.delete(alg_id) + + if isinstance(data, np.ndarray): + data3d.delete(sino_id) + + if returnData: + return vol_id, data3d.get(vol_id) + else: + return vol_id + + +def create_sino(data, proj_id, returnData=True, gpuIndex=None): + """Create a forward projection of an image (2D). + + :param data: Image data or ID. + :type data: :class:`numpy.ndarray` or :class:`int` + :param proj_id: ID of the projector to use. + :type proj_id: :class:`int` + :param returnData: If False, only return the ID of the forward projection. + :type returnData: :class:`bool` + :param gpuIndex: Optional GPU index. + :type gpuIndex: :class:`int` + :returns: :class:`int` or (:class:`int`, :class:`numpy.ndarray`) + + If ``returnData=False``, returns the ID of the forward + projection. Otherwise, returns a tuple containing the ID of the + forward projection and the forward projection itself, in that + order. +""" + proj_geom = projector.projection_geometry(proj_id) + vol_geom = projector.volume_geometry(proj_id) + + if isinstance(data, np.ndarray): + volume_id = data2d.create('-vol', vol_geom, data) + else: + volume_id = data + sino_id = data2d.create('-sino', proj_geom, 0) + if projector.is_cuda(proj_id): + algString = 'FP_CUDA' + else: + algString = 'FP' + cfg = astra_dict(algString) + cfg['ProjectorId'] = proj_id + if gpuIndex is not None: + cfg['option'] = {'GPUindex': gpuIndex} + cfg['ProjectionDataId'] = sino_id + cfg['VolumeDataId'] = volume_id + alg_id = algorithm.create(cfg) + algorithm.run(alg_id) + algorithm.delete(alg_id) + + if isinstance(data, np.ndarray): + data2d.delete(volume_id) + if returnData: + return sino_id, data2d.get(sino_id) + else: + return sino_id + + + +def create_sino3d_gpu(data, proj_geom, vol_geom, returnData=True, gpuIndex=None): + """Create a forward projection of an image (3D). + +:param data: Image data or ID. +:type data: :class:`numpy.ndarray` or :class:`int` +:param proj_geom: Projection geometry. +:type proj_geom: :class:`dict` +:param vol_geom: Volume geometry. +:type vol_geom: :class:`dict` +:param returnData: If False, only return the ID of the forward projection. +:type returnData: :class:`bool` +:param gpuIndex: Optional GPU index. +:type gpuIndex: :class:`int` +:returns: :class:`int` or (:class:`int`, :class:`numpy.ndarray`) -- If ``returnData=False``, returns the ID of the forward projection. Otherwise, returns a tuple containing the ID of the forward projection and the forward projection itself, in that order. + +""" + + if isinstance(data, np.ndarray): + volume_id = data3d.create('-vol', vol_geom, data) + else: + volume_id = data + sino_id = data3d.create('-sino', proj_geom, 0) + algString = 'FP3D_CUDA' + cfg = astra_dict(algString) + if not gpuIndex==None: + cfg['option']={'GPUindex':gpuIndex} + cfg['ProjectionDataId'] = sino_id + cfg['VolumeDataId'] = volume_id + alg_id = algorithm.create(cfg) + algorithm.run(alg_id) + algorithm.delete(alg_id) + + if isinstance(data, np.ndarray): + data3d.delete(volume_id) + if returnData: + return sino_id, data3d.get(sino_id) + else: + return sino_id + + +def create_reconstruction(rec_type, proj_id, sinogram, iterations=1, use_mask='no', mask=np.array([]), use_minc='no', minc=0, use_maxc='no', maxc=255, returnData=True, filterType=None, filterData=None): + """Create a reconstruction of a sinogram (2D). + +:param rec_type: Name of the reconstruction algorithm. +:type rec_type: :class:`string` +:param proj_id: ID of the projector to use. +:type proj_id: :class:`int` +:param sinogram: Sinogram data or ID. +:type sinogram: :class:`numpy.ndarray` or :class:`int` +:param iterations: Number of iterations to run. +:type iterations: :class:`int` +:param use_mask: Whether to use a mask. +:type use_mask: ``'yes'`` or ``'no'`` +:param mask: Mask data or ID +:type mask: :class:`numpy.ndarray` or :class:`int` +:param use_minc: Whether to force a minimum value on the reconstruction pixels. +:type use_minc: ``'yes'`` or ``'no'`` +:param minc: Minimum value to use. +:type minc: :class:`float` +:param use_maxc: Whether to force a maximum value on the reconstruction pixels. +:type use_maxc: ``'yes'`` or ``'no'`` +:param maxc: Maximum value to use. +:type maxc: :class:`float` +:param returnData: If False, only return the ID of the reconstruction. +:type returnData: :class:`bool` +:param filterType: Which type of filter to use for filter-based methods. +:type filterType: :class:`string` +:param filterData: Optional filter data for filter-based methods. +:type filterData: :class:`numpy.ndarray` +:returns: :class:`int` or (:class:`int`, :class:`numpy.ndarray`) -- If ``returnData=False``, returns the ID of the reconstruction. Otherwise, returns a tuple containing the ID of the reconstruction and reconstruction itself, in that order. + +""" + proj_geom = projector.projection_geometry(proj_id) + if isinstance(sinogram, np.ndarray): + sino_id = data2d.create('-sino', proj_geom, sinogram) + else: + sino_id = sinogram + vol_geom = projector.volume_geometry(proj_id) + recon_id = data2d.create('-vol', vol_geom, 0) + cfg = astra_dict(rec_type) + cfg['ProjectorId'] = proj_id + cfg['ProjectionDataId'] = sino_id + cfg['ReconstructionDataId'] = recon_id + cfg['options'] = {} + if use_mask == 'yes': + if isinstance(mask, np.ndarray): + mask_id = data2d.create('-vol', vol_geom, mask) + else: + mask_id = mask + cfg['options']['ReconstructionMaskId'] = mask_id + if not filterType == None: + cfg['FilterType'] = filterType + if not filterData == None: + if isinstance(filterData, np.ndarray): + nexpow = int( + pow(2, math.ceil(math.log(2 * proj_geom['DetectorCount'], 2)))) + filtSize = nexpow / 2 + 1 + filt_proj_geom = create_proj_geom( + 'parallel', 1.0, filtSize, proj_geom['ProjectionAngles']) + filt_id = data2d.create('-sino', filt_proj_geom, filterData) + else: + filt_id = filterData + cfg['FilterSinogramId'] = filt_id + cfg['options']['UseMinConstraint'] = use_minc + cfg['options']['MinConstraintValue'] = minc + cfg['options']['UseMaxConstraint'] = use_maxc + cfg['options']['MaxConstraintValue'] = maxc + cfg['options']['ProjectionOrder'] = 'random' + alg_id = algorithm.create(cfg) + algorithm.run(alg_id, iterations) + + algorithm.delete(alg_id) + + if isinstance(sinogram, np.ndarray): + data2d.delete(sino_id) + if use_mask == 'yes' and isinstance(mask, np.ndarray): + data2d.delete(mask_id) + if not filterData == None: + if isinstance(filterData, np.ndarray): + data2d.delete(filt_id) + if returnData: + return recon_id, data2d.get(recon_id) + else: + return recon_id + + +def create_projector(proj_type, proj_geom, vol_geom): + """Create a 2D projector. + +:param proj_type: Projector type, such as ``'line'``, ``'linear'``, ... +:type proj_type: :class:`string` +:param proj_geom: Projection geometry. +:type proj_geom: :class:`dict` +:param vol_geom: Volume geometry. +:type vol_geom: :class:`dict` +:returns: :class:`int` -- The ID of the projector. + +""" + if proj_type == 'blob': + raise Exception('Blob type not yet implemented') + cfg = astra_dict(proj_type) + cfg['ProjectionGeometry'] = proj_geom + cfg['VolumeGeometry'] = vol_geom + types3d = ['linear3d', 'linearcone', 'cuda3d'] + if proj_type in types3d: + return projector3d.create(cfg) + else: + return projector.create(cfg) diff --git a/python/astra/data2d.py b/python/astra/data2d.py new file mode 100644 index 0000000..8c4be03 --- /dev/null +++ b/python/astra/data2d.py @@ -0,0 +1,120 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from . import data2d_c as d + +def clear(): + """Clear all 2D data objects.""" + return d.clear() + +def delete(ids): + """Delete a 2D object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return d.delete(ids) + +def create(datatype, geometry, data=None): + """Create a 2D object. + + :param datatype: Data object type, '-vol' or '-sino'. + :type datatype: :class:`string` + :param geometry: Volume or projection geometry. + :type geometry: :class:`dict` + :param data: Data to fill the constructed object with, either a scalar or array. + :type data: :class:`float` or :class:`numpy.ndarray` + :returns: :class:`int` -- the ID of the constructed object. + + """ + return d.create(datatype,geometry,data) + +def store(i, data): + """Fill existing 2D object with data. + + :param i: ID of object to fill. + :type i: :class:`int` + :param data: Data to fill the object with, either a scalar or array. + :type data: :class:`float` or :class:`numpy.ndarray` + + """ + return d.store(i, data) + +def get_geometry(i): + """Get the geometry of a 2D object. + + :param i: ID of object. + :type i: :class:`int` + :returns: :class:`dict` -- The geometry of object with ID ``i``. + + """ + return d.get_geometry(i) + +def change_geometry(i, geom): + """Change the geometry of a 2D object. + + :param i: ID of object. + :type i: :class:`int` + :param geom: new geometry. + :type geom: :class:`dict` + + """ + return d.change_geometry(i, geom) + +def get(i): + """Get a 2D object. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`numpy.ndarray` -- The object data. + + """ + return d.get(i) + +def get_shared(i): + """Get a 2D object with memory shared between the ASTRA toolbox and numpy array. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`numpy.ndarray` -- The object data. + + """ + return d.get_shared(i) + + +def get_single(i): + """Get a 2D object in single precision. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`numpy.ndarray` -- The object data. + + """ + return d.get_single(i) + +def info(): + """Print info on 2D objects in memory.""" + return d.info() diff --git a/python/astra/data2d_c.pyx b/python/astra/data2d_c.pyx new file mode 100644 index 0000000..b9c105e --- /dev/null +++ b/python/astra/data2d_c.pyx @@ -0,0 +1,233 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import six + +cimport cython +from cython cimport view + +cimport PyData2DManager +from .PyData2DManager cimport CData2DManager + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument + +import numpy as np + +cimport numpy as np +np.import_array() + + +from .PyIncludes cimport * +cimport utils +from .utils import wrap_from_bytes + +cdef CData2DManager * man2d = <CData2DManager * >PyData2DManager.getSingletonPtr() + +def clear(): + man2d.clear() + + +def delete(ids): + try: + for i in ids: + man2d.remove(i) + except TypeError: + man2d.remove(ids) + + +def create(datatype, geometry, data=None): + cdef Config *cfg + cdef CVolumeGeometry2D * pGeometry + cdef CProjectionGeometry2D * ppGeometry + cdef CFloat32Data2D * pDataObject2D + if datatype == '-vol': + cfg = utils.dictToConfig(six.b('VolumeGeometry'), geometry) + pGeometry = new CVolumeGeometry2D() + if not pGeometry.initialize(cfg[0]): + del cfg + del pGeometry + raise Exception('Geometry class not initialized.') + pDataObject2D = <CFloat32Data2D * > new CFloat32VolumeData2D(pGeometry) + del cfg + del pGeometry + elif datatype == '-sino': + cfg = utils.dictToConfig(six.b('ProjectionGeometry'), geometry) + tpe = wrap_from_bytes(cfg.self.getAttribute(six.b('type'))) + if (tpe == 'sparse_matrix'): + ppGeometry = <CProjectionGeometry2D * >new CSparseMatrixProjectionGeometry2D() + elif (tpe == 'fanflat'): + ppGeometry = <CProjectionGeometry2D * >new CFanFlatProjectionGeometry2D() + elif (tpe == 'fanflat_vec'): + ppGeometry = <CProjectionGeometry2D * >new CFanFlatVecProjectionGeometry2D() + else: + ppGeometry = <CProjectionGeometry2D * >new CParallelProjectionGeometry2D() + if not ppGeometry.initialize(cfg[0]): + del cfg + del ppGeometry + raise Exception('Geometry class not initialized.') + pDataObject2D = <CFloat32Data2D * > new CFloat32ProjectionData2D(ppGeometry) + del ppGeometry + del cfg + else: + raise Exception("Invalid datatype. Please specify '-vol' or '-sino'.") + + if not pDataObject2D.isInitialized(): + del pDataObject2D + raise Exception("Couldn't initialize data object.") + + fillDataObject(pDataObject2D, data) + + return man2d.store(pDataObject2D) + +cdef fillDataObject(CFloat32Data2D * obj, data): + if data is None: + fillDataObjectScalar(obj, 0) + else: + if isinstance(data, np.ndarray): + fillDataObjectArray(obj, np.ascontiguousarray(data,dtype=np.float32)) + else: + fillDataObjectScalar(obj, np.float32(data)) + +cdef fillDataObjectScalar(CFloat32Data2D * obj, float s): + cdef int i + for i in range(obj.getSize()): + obj.getData()[i] = s + +@cython.boundscheck(False) +@cython.wraparound(False) +cdef fillDataObjectArray(CFloat32Data2D * obj, float [:,::1] data): + if (not data.shape[0] == obj.getHeight()) or (not data.shape[1] == obj.getWidth()): + raise Exception( + "The dimensions of the data do not match those specified in the geometry.") + cdef float [:,::1] cView = <float[:data.shape[0],:data.shape[1]]> obj.getData2D()[0] + cView[:] = data + +cdef CFloat32Data2D * getObject(i) except NULL: + cdef CFloat32Data2D * pDataObject = man2d.get(i) + if pDataObject == NULL: + raise Exception("Data object not found") + if not pDataObject.isInitialized(): + raise Exception("Data object not initialized properly.") + return pDataObject + + +def store(i, data): + cdef CFloat32Data2D * pDataObject = getObject(i) + fillDataObject(pDataObject, data) + + +def get_geometry(i): + cdef CFloat32Data2D * pDataObject = getObject(i) + cdef CFloat32ProjectionData2D * pDataObject2 + cdef CFloat32VolumeData2D * pDataObject3 + if pDataObject.getType() == TWOPROJECTION: + pDataObject2 = <CFloat32ProjectionData2D * >pDataObject + geom = utils.configToDict(pDataObject2.getGeometry().getConfiguration()) + elif pDataObject.getType() == TWOVOLUME: + pDataObject3 = <CFloat32VolumeData2D * >pDataObject + geom = utils.configToDict(pDataObject3.getGeometry().getConfiguration()) + else: + raise Exception("Not a known data object") + return geom + + +def change_geometry(i, geom): + cdef Config *cfg + cdef CVolumeGeometry2D * pGeometry + cdef CProjectionGeometry2D * ppGeometry + cdef CFloat32Data2D * pDataObject = getObject(i) + cdef CFloat32ProjectionData2D * pDataObject2 + cdef CFloat32VolumeData2D * pDataObject3 + if pDataObject.getType() == TWOPROJECTION: + pDataObject2 = <CFloat32ProjectionData2D * >pDataObject + cfg = utils.dictToConfig(six.b('ProjectionGeometry'), geom) + tpe = wrap_from_bytes(cfg.self.getAttribute(six.b('type'))) + if (tpe == 'sparse_matrix'): + ppGeometry = <CProjectionGeometry2D * >new CSparseMatrixProjectionGeometry2D() + elif (tpe == 'fanflat'): + ppGeometry = <CProjectionGeometry2D * >new CFanFlatProjectionGeometry2D() + elif (tpe == 'fanflat_vec'): + ppGeometry = <CProjectionGeometry2D * >new CFanFlatVecProjectionGeometry2D() + else: + ppGeometry = <CProjectionGeometry2D * >new CParallelProjectionGeometry2D() + if not ppGeometry.initialize(cfg[0]): + del cfg + del ppGeometry + raise Exception('Geometry class not initialized.') + if (ppGeometry.getDetectorCount() != pDataObject2.getDetectorCount() or ppGeometry.getProjectionAngleCount() != pDataObject2.getAngleCount()): + del ppGeometry + del cfg + raise Exception( + "The dimensions of the data do not match those specified in the geometry.") + pDataObject2.changeGeometry(ppGeometry) + del ppGeometry + del cfg + elif pDataObject.getType() == TWOVOLUME: + pDataObject3 = <CFloat32VolumeData2D * >pDataObject + cfg = utils.dictToConfig(six.b('VolumeGeometry'), geom) + pGeometry = new CVolumeGeometry2D() + if not pGeometry.initialize(cfg[0]): + del cfg + del pGeometry + raise Exception('Geometry class not initialized.') + if (pGeometry.getGridColCount() != pDataObject3.getWidth() or pGeometry.getGridRowCount() != pDataObject3.getHeight()): + del cfg + del pGeometry + raise Exception( + 'The dimensions of the data do not match those specified in the geometry.') + pDataObject3.changeGeometry(pGeometry) + del cfg + del pGeometry + else: + raise Exception("Not a known data object") + +@cython.boundscheck(False) +@cython.wraparound(False) +def get(i): + cdef CFloat32Data2D * pDataObject = getObject(i) + outArr = np.empty((pDataObject.getHeight(), pDataObject.getWidth()),dtype=np.float32,order='C') + cdef float [:,::1] mView = outArr + cdef float [:,::1] cView = <float[:outArr.shape[0],:outArr.shape[1]]> pDataObject.getData2D()[0] + mView[:] = cView + return outArr + +def get_shared(i): + cdef CFloat32Data2D * pDataObject = getObject(i) + cdef np.npy_intp shape[2] + shape[0] = <np.npy_intp> pDataObject.getHeight() + shape[1] = <np.npy_intp> pDataObject.getWidth() + return np.PyArray_SimpleNewFromData(2,shape,np.NPY_FLOAT32,<void *>pDataObject.getData2D()[0]) + + +def get_single(i): + raise Exception("Not yet implemented") + + +def info(): + six.print_(wrap_from_bytes(man2d.info())) diff --git a/python/astra/data3d.py b/python/astra/data3d.py new file mode 100644 index 0000000..a2e9201 --- /dev/null +++ b/python/astra/data3d.py @@ -0,0 +1,118 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from . import data3d_c as d + +def create(datatype,geometry,data=None): + """Create a 3D object. + + :param datatype: Data object type, '-vol' or '-sino'. + :type datatype: :class:`string` + :param geometry: Volume or projection geometry. + :type geometry: :class:`dict` + :param data: Data to fill the constructed object with, either a scalar or array. + :type data: :class:`float` or :class:`numpy.ndarray` + :returns: :class:`int` -- the ID of the constructed object. + + """ + return d.create(datatype,geometry,data) + +def get(i): + """Get a 3D object. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`numpy.ndarray` -- The object data. + + """ + return d.get(i) + +def get_shared(i): + """Get a 3D object with memory shared between the ASTRA toolbox and numpy array. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`numpy.ndarray` -- The object data. + + """ + return d.get_shared(i) + +def get_single(i): + """Get a 3D object in single precision. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`numpy.ndarray` -- The object data. + + """ + return g.get_single(i) + +def store(i,data): + """Fill existing 3D object with data. + + :param i: ID of object to fill. + :type i: :class:`int` + :param data: Data to fill the object with, either a scalar or array. + :type data: :class:`float` or :class:`numpy.ndarray` + + """ + return d.store(i,data) + +def get_geometry(i): + """Get the geometry of a 3D object. + + :param i: ID of object. + :type i: :class:`int` + :returns: :class:`dict` -- The geometry of object with ID ``i``. + + """ + return d.get_geometry(i) + +def dimensions(i): + """Get dimensions of a 3D object. + + :param i: ID of object. + :type i: :class:`int` + :returns: :class:`tuple` -- dimensions of object with ID ``i``. + + """ + return d.dimensions(i) + +def delete(ids): + """Delete a 2D object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return d.delete(ids) + +def clear(): + """Clear all 3D data objects.""" + return d.clear() + +def info(): + """Print info on 3D objects in memory.""" + return d.info() diff --git a/python/astra/data3d_c.pyx b/python/astra/data3d_c.pyx new file mode 100644 index 0000000..4b069f7 --- /dev/null +++ b/python/astra/data3d_c.pyx @@ -0,0 +1,197 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import six + +cimport cython + +cimport PyData3DManager +from .PyData3DManager cimport CData3DManager + +from .PyIncludes cimport * +import numpy as np + +cimport numpy as np +np.import_array() + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument + +cimport utils +from .utils import wrap_from_bytes + +cdef CData3DManager * man3d = <CData3DManager * >PyData3DManager.getSingletonPtr() + +cdef extern from *: + CFloat32Data3DMemory * dynamic_cast_mem "dynamic_cast<astra::CFloat32Data3DMemory*>" (CFloat32Data3D * ) except NULL + +def create(datatype,geometry,data=None): + cdef Config *cfg + cdef CVolumeGeometry3D * pGeometry + cdef CProjectionGeometry3D * ppGeometry + cdef CFloat32Data3DMemory * pDataObject3D + cdef CConeProjectionGeometry3D* pppGeometry + if datatype == '-vol': + cfg = utils.dictToConfig(six.b('VolumeGeometry'), geometry) + pGeometry = new CVolumeGeometry3D() + if not pGeometry.initialize(cfg[0]): + del cfg + del pGeometry + raise Exception('Geometry class not initialized.') + pDataObject3D = <CFloat32Data3DMemory * > new CFloat32VolumeData3DMemory(pGeometry) + del cfg + del pGeometry + elif datatype == '-sino' or datatype == '-proj3d': + cfg = utils.dictToConfig(six.b('ProjectionGeometry'), geometry) + tpe = wrap_from_bytes(cfg.self.getAttribute(six.b('type'))) + if (tpe == "parallel3d"): + ppGeometry = <CProjectionGeometry3D*> new CParallelProjectionGeometry3D(); + elif (tpe == "parallel3d_vec"): + ppGeometry = <CProjectionGeometry3D*> new CParallelVecProjectionGeometry3D(); + elif (tpe == "cone"): + ppGeometry = <CProjectionGeometry3D*> new CConeProjectionGeometry3D(); + elif (tpe == "cone_vec"): + ppGeometry = <CProjectionGeometry3D*> new CConeVecProjectionGeometry3D(); + else: + raise Exception("Invalid geometry type.") + + if not ppGeometry.initialize(cfg[0]): + del cfg + del ppGeometry + raise Exception('Geometry class not initialized.') + pDataObject3D = <CFloat32Data3DMemory * > new CFloat32ProjectionData3DMemory(ppGeometry) + del ppGeometry + del cfg + elif datatype == "-sinocone": + cfg = utils.dictToConfig(six.b('ProjectionGeometry'), geometry) + pppGeometry = new CConeProjectionGeometry3D() + if not pppGeometry.initialize(cfg[0]): + del cfg + del pppGeometry + raise Exception('Geometry class not initialized.') + pDataObject3D = <CFloat32Data3DMemory * > new CFloat32ProjectionData3DMemory(pppGeometry) + else: + raise Exception("Invalid datatype. Please specify '-vol' or '-proj3d'.") + + if not pDataObject3D.isInitialized(): + del pDataObject3D + raise Exception("Couldn't initialize data object.") + + fillDataObject(pDataObject3D, data) + + pDataObject3D.updateStatistics() + + return man3d.store(<CFloat32Data3D*>pDataObject3D) + +def get_geometry(i): + cdef CFloat32Data3DMemory * pDataObject = dynamic_cast_mem(getObject(i)) + cdef CFloat32ProjectionData3DMemory * pDataObject2 + cdef CFloat32VolumeData3DMemory * pDataObject3 + if pDataObject.getType() == THREEPROJECTION: + pDataObject2 = <CFloat32ProjectionData3DMemory * >pDataObject + geom = utils.configToDict(pDataObject2.getGeometry().getConfiguration()) + elif pDataObject.getType() == THREEVOLUME: + pDataObject3 = <CFloat32VolumeData3DMemory * >pDataObject + geom = utils.configToDict(pDataObject3.getGeometry().getConfiguration()) + else: + raise Exception("Not a known data object") + return geom + +cdef fillDataObject(CFloat32Data3DMemory * obj, data): + if data is None: + fillDataObjectScalar(obj, 0) + else: + if isinstance(data, np.ndarray): + fillDataObjectArray(obj, np.ascontiguousarray(data,dtype=np.float32)) + else: + fillDataObjectScalar(obj, np.float32(data)) + +cdef fillDataObjectScalar(CFloat32Data3DMemory * obj, float s): + cdef int i + for i in range(obj.getSize()): + obj.getData()[i] = s + +@cython.boundscheck(False) +@cython.wraparound(False) +cdef fillDataObjectArray(CFloat32Data3DMemory * obj, float [:,:,::1] data): + if (not data.shape[0] == obj.getDepth()) or (not data.shape[1] == obj.getHeight()) or (not data.shape[2] == obj.getWidth()): + raise Exception( + "The dimensions of the data do not match those specified in the geometry.") + cdef float [:,:,::1] cView = <float[:data.shape[0],:data.shape[1],:data.shape[2]]> obj.getData3D()[0][0] + cView[:] = data + +cdef CFloat32Data3D * getObject(i) except NULL: + cdef CFloat32Data3D * pDataObject = man3d.get(i) + if pDataObject == NULL: + raise Exception("Data object not found") + if not pDataObject.isInitialized(): + raise Exception("Data object not initialized properly.") + return pDataObject + +@cython.boundscheck(False) +@cython.wraparound(False) +def get(i): + cdef CFloat32Data3DMemory * pDataObject = dynamic_cast_mem(getObject(i)) + outArr = np.empty((pDataObject.getDepth(),pDataObject.getHeight(), pDataObject.getWidth()),dtype=np.float32,order='C') + cdef float [:,:,::1] mView = outArr + cdef float [:,:,::1] cView = <float[:outArr.shape[0],:outArr.shape[1],:outArr.shape[2]]> pDataObject.getData3D()[0][0] + mView[:] = cView + return outArr + +def get_shared(i): + cdef CFloat32Data3DMemory * pDataObject = dynamic_cast_mem(getObject(i)) + outArr = np.empty((pDataObject.getDepth(),pDataObject.getHeight(), pDataObject.getWidth()),dtype=np.float32,order='C') + cdef np.npy_intp shape[3] + shape[0] = <np.npy_intp> pDataObject.getDepth() + shape[1] = <np.npy_intp> pDataObject.getHeight() + shape[2] = <np.npy_intp> pDataObject.getWidth() + return np.PyArray_SimpleNewFromData(3,shape,np.NPY_FLOAT32,<void *>pDataObject.getData3D()[0][0]) + +def get_single(i): + raise Exception("Not yet implemented") + +def store(i,data): + cdef CFloat32Data3D * pDataObject = getObject(i) + fillDataObject(dynamic_cast_mem(pDataObject), data) + +def dimensions(i): + cdef CFloat32Data3D * pDataObject = getObject(i) + return (pDataObject.getWidth(),pDataObject.getHeight(),pDataObject.getDepth()) + +def delete(ids): + try: + for i in ids: + man3d.remove(i) + except TypeError: + man3d.remove(ids) + +def clear(): + man3d.clear() + +def info(): + six.print_(wrap_from_bytes(man3d.info())) diff --git a/python/astra/extrautils.pyx b/python/astra/extrautils.pyx new file mode 100644 index 0000000..998f5cb --- /dev/null +++ b/python/astra/extrautils.pyx @@ -0,0 +1,43 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +def clipCircle(img): + cdef int i,j + cdef double x2,y2,mid,bnd + cdef long sz,sz2 + sz = img.shape[0] + sz2 = sz*sz + bnd = sz2/4. + mid = (sz-1.)/2. + nDel=0 + for i in range(sz): + for j in range(sz): + x2 = (i-mid)*(i-mid) + y2 = (j-mid)*(j-mid) + if x2+y2>bnd: + img[i,j]=0 + nDel=nDel+1 + return nDel diff --git a/python/astra/functions.py b/python/astra/functions.py new file mode 100644 index 0000000..4025468 --- /dev/null +++ b/python/astra/functions.py @@ -0,0 +1,270 @@ +#----------------------------------------------------------------------- +# Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +# Author: Daniel M. Pelt +# Contact: D.M.Pelt@cwi.nl +# Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +# This file is part of the Python interface to the +# All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +# The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +# The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +"""Additional functions for PyAstraToolbox. + +.. moduleauthor:: Daniel M. Pelt <D.M.Pelt@cwi.nl> + + +""" + +from . import creators as ac +import numpy as np +from six.moves import range + +from . import data2d +from . import data3d +from . import projector +from . import algorithm + + + +def clear(): + """Clears all used memory of the ASTRA Toolbox. + + .. note:: + This is irreversible. + + """ + data2d.clear() + data3d.clear() + projector.clear() + algorithm.clear() + + +def data_op(op, data, scalar, gpu_core, mask=None): + """Perform data operation on data. + + :param op: Operation to perform. + :param data: Data to perform operation on. + :param scalar: Scalar argument to data operation. + :param gpu_core: GPU core to perform operation on. + :param mask: Optional mask. + + """ + + cfg = ac.astra_dict('DataOperation_CUDA') + cfg['Operation'] = op + cfg['Scalar'] = scalar + cfg['DataId'] = data + if not mask == None: + cfg['MaskId'] = mask + cfg['option']['GPUindex'] = gpu_core + alg_id = algorithm.create(cfg) + algorithm.run(alg_id) + algorithm.delete(alg_id) + + +def add_noise_to_sino(sinogram_in, I0, seed=None): + """Adds Poisson noise to a sinogram. + + :param sinogram_in: Sinogram to add noise to. + :type sinogram_in: :class:`numpy.ndarray` + :param I0: Background intensity. Lower values lead to higher noise. + :type I0: :class:`float` + :returns: :class:`numpy.ndarray` -- the sinogram with added noise. + + """ + + if not seed==None: + curstate = np.random.get_state() + np.random.seed(seed) + + if isinstance(sinogram_in, np.ndarray): + sinogramRaw = sinogram_in + else: + sinogramRaw = data2d.get(sinogram_in) + max_sinogramRaw = sinogramRaw.max() + sinogramRawScaled = sinogramRaw / max_sinogramRaw + # to detector count + sinogramCT = I0 * np.exp(-sinogramRawScaled) + # add poison noise + sinogramCT_C = np.zeros_like(sinogramCT) + for i in range(sinogramCT_C.shape[0]): + for j in range(sinogramCT_C.shape[1]): + sinogramCT_C[i, j] = np.random.poisson(sinogramCT[i, j]) + # to density + sinogramCT_D = sinogramCT_C / I0 + sinogram_out = -max_sinogramRaw * np.log(sinogramCT_D) + + if not isinstance(sinogram_in, np.ndarray): + at.data2d.store(sinogram_in, sinogram_out) + + if not seed==None: + np.random.set_state(curstate) + + return sinogram_out + +def move_vol_geom(geom, pos, is_relative=False): + """Moves center of volume geometry to new position. + + :param geom: Input volume geometry + :type geom: :class:`dict` + :param pos: Tuple (x,y[,z]) for new position, with the center of the image at (0,0[,0]) + :type pos: :class:`tuple` + :param is_relative: Whether new position is relative to the old position + :type is_relative: :class:`bool` + :returns: :class:`dict` -- Volume geometry with the new center + """ + + ret_geom = geom.copy() + ret_geom['option'] = geom['option'].copy() + + if not is_relative: + ret_geom['option']['WindowMinX'] = -geom['GridColCount']/2. + ret_geom['option']['WindowMaxX'] = geom['GridColCount']/2. + ret_geom['option']['WindowMinY'] = -geom['GridRowCount']/2. + ret_geom['option']['WindowMaxY'] = geom['GridRowCount']/2. + if len(pos)>2: + ret_geom['option']['WindowMinZ'] = -geom['GridSliceCount']/2. + ret_geom['option']['WindowMaxZ'] = geom['GridSliceCount']/2. + ret_geom['option']['WindowMinX'] += pos[0] + ret_geom['option']['WindowMaxX'] += pos[0] + ret_geom['option']['WindowMinY'] += pos[1] + ret_geom['option']['WindowMaxY'] += pos[1] + if len(pos)>2: + ret_geom['option']['WindowMinZ'] += pos[2] + ret_geom['option']['WindowMaxZ'] += pos[2] + return ret_geom + + +def geom_size(geom, dim=None): + """Returns the size of a volume or sinogram, based on the projection or volume geometry. + + :param geom: Geometry to calculate size from + :type geometry: :class:`dict` + :param dim: Optional axis index to return + :type dim: :class:`int` + """ + + if 'GridSliceCount' in geom: + # 3D Volume geometry? + s = (geom['GridSliceCount'], geom[ + 'GridRowCount'], geom['GridColCount']) + elif 'GridColCount' in geom: + # 2D Volume geometry? + s = (geom['GridRowCount'], geom['GridColCount']) + elif geom['type'] == 'parallel' or geom['type'] == 'fanflat': + s = (len(geom['ProjectionAngles']), geom['DetectorCount']) + elif geom['type'] == 'parallel3d' or geom['type'] == 'cone': + s = (geom['DetectorRowCount'], len( + geom['ProjectionAngles']), geom['DetectorColCount']) + elif geom['type'] == 'fanflat_vec': + s = (geom['Vectors'].shape[0], geom['DetectorCount']) + elif geom['type'] == 'parallel3d_vec' or geom['type'] == 'cone_vec': + s = (geom['DetectorRowCount'], geom[ + 'Vectors'].shape[0], geom['DetectorColCount']) + + if dim != None: + s = s[dim] + + return s + + +def geom_2vec(proj_geom): + """Returns a vector-based projection geometry from a basic projection geometry. + + :param proj_geom: Projection geometry to convert + :type proj_geom: :class:`dict` + """ + if proj_geom['type'] == 'fanflat': + angles = proj_geom['ProjectionAngles'] + vectors = np.zeros((len(angles), 6)) + for i in range(len(angles)): + + # source + vectors[i, 0] = np.sin(angles[i]) * proj_geom['DistanceOriginSource'] + vectors[i, 1] = -np.cos(angles[i]) * proj_geom['DistanceOriginSource'] + + # center of detector + vectors[i, 2] = -np.sin(angles[i]) * proj_geom['DistanceOriginDetector'] + vectors[i, 3] = np.cos(angles[i]) * proj_geom['DistanceOriginDetector'] + + # vector from detector pixel 0 to 1 + vectors[i, 4] = np.cos(angles[i]) * proj_geom['DetectorWidth'] + vectors[i, 5] = np.sin(angles[i]) * proj_geom['DetectorWidth'] + proj_geom_out = ac.create_proj_geom( + 'fanflat_vec', proj_geom['DetectorCount'], vectors) + + elif proj_geom['type'] == 'cone': + angles = proj_geom['ProjectionAngles'] + vectors = np.zeros((len(angles), 12)) + for i in range(len(angles)): + # source + vectors[i, 0] = np.sin(angles[i]) * proj_geom['DistanceOriginSource'] + vectors[i, 1] = -np.cos(angles[i]) * proj_geom['DistanceOriginSource'] + vectors[i, 2] = 0 + + # center of detector + vectors[i, 3] = -np.sin(angles[i]) * proj_geom['DistanceOriginDetector'] + vectors[i, 4] = np.cos(angles[i]) * proj_geom['DistanceOriginDetector'] + vectors[i, 5] = 0 + + # vector from detector pixel (0,0) to (0,1) + vectors[i, 6] = np.cos(angles[i]) * proj_geom['DetectorSpacingX'] + vectors[i, 7] = np.sin(angles[i]) * proj_geom['DetectorSpacingX'] + vectors[i, 8] = 0 + + # vector from detector pixel (0,0) to (1,0) + vectors[i, 9] = 0 + vectors[i, 10] = 0 + vectors[i, 11] = proj_geom['DetectorSpacingY'] + + proj_geom_out = ac.create_proj_geom( + 'cone_vec', proj_geom['DetectorRowCount'], proj_geom['DetectorColCount'], vectors) + + # PARALLEL + elif proj_geom['type'] == 'parallel3d': + angles = proj_geom['ProjectionAngles'] + vectors = np.zeros((len(angles), 12)) + for i in range(len(angles)): + + # ray direction + vectors[i, 0] = np.sin(angles[i]) + vectors[i, 1] = -np.cos(angles[i]) + vectors[i, 2] = 0 + + # center of detector + vectors[i, 3] = 0 + vectors[i, 4] = 0 + vectors[i, 5] = 0 + + # vector from detector pixel (0,0) to (0,1) + vectors[i, 6] = np.cos(angles[i]) * proj_geom['DetectorSpacingX'] + vectors[i, 7] = np.sin(angles[i]) * proj_geom['DetectorSpacingX'] + vectors[i, 8] = 0 + + # vector from detector pixel (0,0) to (1,0) + vectors[i, 9] = 0 + vectors[i, 10] = 0 + vectors[i, 11] = proj_geom['DetectorSpacingY'] + + proj_geom_out = ac.create_proj_geom( + 'parallel3d_vec', proj_geom['DetectorRowCount'], proj_geom['DetectorColCount'], vectors) + + else: + raise ValueError( + 'No suitable vector geometry found for type: ' + proj_geom['type']) + return proj_geom_out diff --git a/python/astra/log.py b/python/astra/log.py new file mode 100644 index 0000000..3ec0df5 --- /dev/null +++ b/python/astra/log.py @@ -0,0 +1,153 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +from . import log_c as l + +import inspect + +def debug(message): + """Log a debug message. + + :param message: Message to log. + :type message: :class:`string` + """ + prev_f = inspect.getframeinfo(inspect.currentframe().f_back) + l.log_debug(prev_f.filename,prev_f.lineno,message) + +def info(message): + """Log an info message. + + :param message: Message to log. + :type message: :class:`string` + """ + prev_f = inspect.getframeinfo(inspect.currentframe().f_back) + l.log_info(prev_f.filename,prev_f.lineno,message) + +def warn(message): + """Log a warning message. + + :param message: Message to log. + :type message: :class:`string` + """ + prev_f = inspect.getframeinfo(inspect.currentframe().f_back) + l.log_warn(prev_f.filename,prev_f.lineno,message) + +def error(message): + """Log an error message. + + :param message: Message to log. + :type message: :class:`string` + """ + prev_f = inspect.getframeinfo(inspect.currentframe().f_back) + l.log_error(prev_f.filename,prev_f.lineno,message) + +def enable(): + """Enable logging to screen and file.""" + l.log_enable() + +def enableScreen(): + """Enable logging to screen.""" + l.log_enableScreen() + +def enableFile(): + """Enable logging to file (note that a file has to be set).""" + l.log_enableFile() + +def disable(): + """Disable all logging.""" + l.log_disable() + +def disableScreen(): + """Disable logging to screen.""" + l.log_disableScreen() + +def disableFile(): + """Disable logging to file.""" + l.log_disableFile() + +def setFormatFile(fmt): + """Set the format string for log messages. Here are the substitutions you may use: + + %f: Source file name generating the log call. + %n: Source line number where the log call was made. + %m: The message text sent to the logger (after printf formatting). + %d: The current date, formatted using the logger's date format. + %t: The current time, formatted using the logger's time format. + %l: The log level (one of "DEBUG", "INFO", "WARN", or "ERROR"). + %%: A literal percent sign. + + The default format string is "%d %t %f(%n): %l: %m\n". + + :param fmt: Format to use, end with "\n". + :type fmt: :class:`string` + """ + l.log_setFormatFile(fmt) + +def setFormatScreen(fmt): + """Set the format string for log messages. Here are the substitutions you may use: + + %f: Source file name generating the log call. + %n: Source line number where the log call was made. + %m: The message text sent to the logger (after printf formatting). + %d: The current date, formatted using the logger's date format. + %t: The current time, formatted using the logger's time format. + %l: The log level (one of "DEBUG", "INFO", "WARN", or "ERROR"). + %%: A literal percent sign. + + The default format string is "%d %t %f(%n): %l: %m\n". + + :param fmt: Format to use, end with "\n". + :type fmt: :class:`string` + """ + l.log_setFormatScreen(fmt) + +STDOUT=1 +STDERR=2 + +DEBUG=0 +INFO=1 +WARN=2 +ERROR=3 + +def setOutputScreen(fd, level): + """Set which screen to output to, and which level to use. + + :param fd: File descriptor of output screen (STDOUT or STDERR). + :type fd: :class:`int` + :param level: Logging level to use (DEBUG, INFO, WARN, or ERROR). + :type level: :class:`int` + """ + l.log_setOutputScreen(fd, level) + +def setOutputFile(filename, level): + """Set which file to output to, and which level to use. + + :param filename: File name of output file. + :type filename: :class:`string` + :param level: Logging level to use (DEBUG, INFO, WARN, or ERROR). + :type level: :class:`int` + """ + l.log_setOutputFile(filename, level)
\ No newline at end of file diff --git a/python/astra/log_c.pyx b/python/astra/log_c.pyx new file mode 100644 index 0000000..969cc06 --- /dev/null +++ b/python/astra/log_c.pyx @@ -0,0 +1,96 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import six + +cdef extern from "astra/Logging.h" namespace "astra": + cdef enum log_level: + LOG_DEBUG + LOG_INFO + LOG_WARN + LOG_ERROR + +cdef extern from "astra/Logging.h" namespace "astra::CLogger": + void debug(const char *sfile, int sline, const char *fmt, ...) + void info(const char *sfile, int sline, const char *fmt, ...) + void warn(const char *sfile, int sline, const char *fmt, ...) + void error(const char *sfile, int sline, const char *fmt, ...) + void setOutputScreen(int fd, log_level m_eLevel) + void setOutputFile(const char *filename, log_level m_eLevel) + void enable() + void enableScreen() + void enableFile() + void disable() + void disableScreen() + void disableFile() + void setFormatFile(const char *fmt) + void setFormatScreen(const char *fmt) + +def log_debug(sfile, sline, message): + debug(six.b(sfile),sline,six.b(message)) + +def log_info(sfile, sline, message): + info(six.b(sfile),sline,six.b(message)) + +def log_warn(sfile, sline, message): + warn(six.b(sfile),sline,six.b(message)) + +def log_error(sfile, sline, message): + error(six.b(sfile),sline,six.b(message)) + +def log_enable(): + enable() + +def log_enableScreen(): + enableScreen() + +def log_enableFile(): + enableFile() + +def log_disable(): + disable() + +def log_disableScreen(): + disableScreen() + +def log_disableFile(): + disableFile() + +def log_setFormatFile(fmt): + setFormatFile(six.b(fmt)) + +def log_setFormatScreen(fmt): + setFormatScreen(six.b(fmt)) + +enumList = [LOG_DEBUG,LOG_INFO,LOG_WARN,LOG_ERROR] + +def log_setOutputScreen(fd, level): + setOutputScreen(fd, enumList[level]) + +def log_setOutputFile(filename, level): + setOutputFile(six.b(filename), enumList[level])
\ No newline at end of file diff --git a/python/astra/matlab.py b/python/astra/matlab.py new file mode 100644 index 0000000..83b345d --- /dev/null +++ b/python/astra/matlab.py @@ -0,0 +1,112 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +"""This module implements a MATLAB-like interface to the ASTRA Toolbox. + +Note that all functions are called with a :class:`string` as the first +argument, specifying the operation to perform. This un-pythonic way +is used to make transitioning from MATLAB code to Python code easier, as +the MATLAB interface uses the same type of method calling. + +After an initial ``import astra``, these functions can be accessed in the +``astra.m`` module. + +""" + +from . import astra_c +from . import data2d_c +from . import data3d_c +from . import projector_c +from . import algorithm_c +from . import matrix_c +import numpy as np + + +def astra(command, *args): + """MATLAB-like interface to the :mod:`astra.astra` module + + For example: + + ``astra.m.astra('use_cuda')`` -- Check if CUDA is enabled. + + """ + return getattr(astra_c, command)(*args) + + +def data2d(command, *args): + """MATLAB-like interface to the :mod:`astra.data2d` module + + For example: + + ``astra.m.data2d('create',type,geometry,data)`` -- Create a 2D object. + + """ + return getattr(data2d_c, command)(*args) + + +def data3d(command, *args): + """MATLAB-like interface to the :mod:`astra.data3d` module + + For example: + + ``astra.m.data3d('get',i)`` -- Get 3D object data. + + """ + return getattr(data3d_c, command)(*args) + + +def projector(command, *args): + """MATLAB-like interface to the :mod:`astra.projector` module + + For example: + + ``astra.m.projector('volume_geometry',i)`` -- Get volume geometry. + + """ + return getattr(projector_c, command)(*args) + + +def matrix(command, *args): + """MATLAB-like interface to the :mod:`astra.matrix` module + + For example: + + ``astra.m.matrix('delete',i)`` -- Delete a matrix. + + """ + return getattr(matrix_c, command)(*args) + + +def algorithm(command, *args): + """MATLAB-like interface to the :mod:`astra.algorithm` module + + For example: + + ``astra.m.algorithm('run',i,1000)`` -- Run an algorithm with 1000 iterations. + + """ + if command == 'iterate': + command = 'run' + return getattr(algorithm_c, command)(*args) diff --git a/python/astra/matrix.py b/python/astra/matrix.py new file mode 100644 index 0000000..27e4823 --- /dev/null +++ b/python/astra/matrix.py @@ -0,0 +1,86 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from . import matrix_c as m + +def delete(ids): + """Delete a matrix object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return m.delete(ids) + +def clear(): + """Clear all matrix objects.""" + return m.clear() + +def create(data): + """Create matrix object with data. + + :param data: Data to fill the created object with. + :type data: :class:`scipy.sparse.csr_matrix` + :returns: :class:`int` -- the ID of the constructed object. + + """ + return m.create(data) + + +def store(i,data): + """Fill existing matrix object with data. + + :param i: ID of object to fill. + :type i: :class:`int` + :param data: Data to fill the object with. + :type data: :class:`scipy.sparse.csr_matrix` + + """ + return m.store(i,data) + +def get_size(i): + """Get matrix dimensions. + + :param i: ID of object. + :type i: :class:`int` + :returns: :class:`tuple` -- matrix dimensions. + """ + return m.get_size(i) + +def get(i): + """Get a matrix object. + + :param i: ID of object to get. + :type i: :class:`int` + :returns: :class:`scipy.sparse.csr_matrix` -- The object data. + + """ + return m.get(i) + +def info(): + """Print info on matrix objects in memory.""" + return m.info() + + diff --git a/python/astra/matrix_c.pyx b/python/astra/matrix_c.pyx new file mode 100644 index 0000000..b0d8bc4 --- /dev/null +++ b/python/astra/matrix_c.pyx @@ -0,0 +1,116 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import six +from six.moves import range +import numpy as np +import scipy.sparse as ss + +from libcpp cimport bool + +cimport PyMatrixManager +from .PyMatrixManager cimport CMatrixManager +from .PyIncludes cimport * +from .utils import wrap_from_bytes + +cdef CMatrixManager * manM = <CMatrixManager * >PyMatrixManager.getSingletonPtr() + + +def delete(ids): + try: + for i in ids: + manM.remove(i) + except TypeError: + manM.remove(ids) + +def clear(): + manM.clear() + +cdef int csr_matrix_to_astra(data,CSparseMatrix *mat) except -1: + if isinstance(data,ss.csr_matrix): + csrD = data + else: + csrD = data.tocsr() + if not mat.isInitialized(): + raise Exception("Couldn't initialize data object.") + if csrD.nnz > mat.m_lSize or csrD.shape[0] > mat.m_iHeight: + raise Exception("Matrix too large to store in this object.") + for i in range(len(csrD.indptr)): + mat.m_plRowStarts[i] = csrD.indptr[i] + for i in range(csrD.nnz): + mat.m_piColIndices[i] = csrD.indices[i] + mat.m_pfValues[i] = csrD.data[i] + +cdef astra_to_csr_matrix(CSparseMatrix *mat): + indptr = np.zeros(mat.m_iHeight+1,dtype=np.int) + indices = np.zeros(mat.m_plRowStarts[mat.m_iHeight],dtype=np.int) + data = np.zeros(mat.m_plRowStarts[mat.m_iHeight]) + for i in range(mat.m_iHeight+1): + indptr[i] = mat.m_plRowStarts[i] + for i in range(mat.m_plRowStarts[mat.m_iHeight]): + indices[i] = mat.m_piColIndices[i] + data[i] = mat.m_pfValues[i] + return ss.csr_matrix((data,indices,indptr),shape=(mat.m_iHeight,mat.m_iWidth)) + +def create(data): + cdef CSparseMatrix* pMatrix + pMatrix = new CSparseMatrix(data.shape[0], data.shape[1], data.nnz) + if not pMatrix.isInitialized(): + del pMatrix + raise Exception("Couldn't initialize data object.") + try: + csr_matrix_to_astra(data,pMatrix) + except: + del pMatrix + raise Exception("Failed to create data object.") + + return manM.store(pMatrix) + +cdef CSparseMatrix * getObject(i) except NULL: + cdef CSparseMatrix * pDataObject = manM.get(i) + if pDataObject == NULL: + raise Exception("Data object not found") + if not pDataObject.isInitialized(): + raise Exception("Data object not initialized properly.") + return pDataObject + + +def store(i,data): + cdef CSparseMatrix * pDataObject = getObject(i) + csr_matrix_to_astra(data,pDataObject) + +def get_size(i): + cdef CSparseMatrix * pDataObject = getObject(i) + return (pDataObject.m_iHeight,pDataObject.m_iWidth) + +def get(i): + cdef CSparseMatrix * pDataObject = getObject(i) + return astra_to_csr_matrix(pDataObject) + +def info(): + six.print_(wrap_from_bytes(manM.info())) diff --git a/python/astra/projector.py b/python/astra/projector.py new file mode 100644 index 0000000..e370e5a --- /dev/null +++ b/python/astra/projector.py @@ -0,0 +1,110 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from . import projector_c as p + +def create(config): + """Create projector object. + + :param config: Projector options. + :type config: :class:`dict` + :returns: :class:`int` -- the ID of the constructed object. + + """ + return p.create(config) + + +def delete(ids): + """Delete a projector object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return p.delete(ids) + + +def clear(): + """Clear all projector objects.""" + return p.clear() + + +def info(): + """Print info on projector objects in memory.""" + return p.info() + +def projection_geometry(i): + """Get projection geometry of a projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`dict` -- projection geometry + + """ + return p.projection_geometry(i) + + +def volume_geometry(i): + """Get volume geometry of a projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`dict` -- volume geometry + + """ + return p.volume_geometry(i) + + +def weights_single_ray(i, projection_index, detector_index): + return p.weights_single_ray(i, projection_index, detector_index) + + +def weights_projection(i, projection_index): + return p.weights_projection(i, projection_index) + + +def splat(i, row, col): + return p.splat(i, row, col) + +def is_cuda(i): + """Check whether a projector is a CUDA projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`bool` -- True if the projector is a CUDA projector. + + """ + return p.is_cuda(i) + + +def matrix(i): + """Get sparse matrix of a projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`int` -- ID of sparse matrix. + + """ + return p.matrix(i) diff --git a/python/astra/projector3d.py b/python/astra/projector3d.py new file mode 100644 index 0000000..d1086b9 --- /dev/null +++ b/python/astra/projector3d.py @@ -0,0 +1,100 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from . import projector3d_c as p + +def create(config): + """Create projector object. + + :param config: Projector options. + :type config: :class:`dict` + :returns: :class:`int` -- the ID of the constructed object. + + """ + return p.create(config) + + +def delete(ids): + """Delete a projector object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return p.delete(ids) + + +def clear(): + """Clear all projector objects.""" + return p.clear() + + +def info(): + """Print info on projector objects in memory.""" + return p.info() + +def projection_geometry(i): + """Get projection geometry of a projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`dict` -- projection geometry + + """ + return p.projection_geometry(i) + + +def volume_geometry(i): + """Get volume geometry of a projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`dict` -- volume geometry + + """ + return p.volume_geometry(i) + + +def weights_single_ray(i, projection_index, detector_index): + return p.weights_single_ray(i, projection_index, detector_index) + + +def weights_projection(i, projection_index): + return p.weights_projection(i, projection_index) + + +def splat(i, row, col): + return p.splat(i, row, col) + + +def is_cuda(i): + """Check whether a projector is a CUDA projector. + + :param i: ID of projector. + :type i: :class:`int` + :returns: :class:`bool` -- True if the projector is a CUDA projector. + + """ + return p.is_cuda(i) diff --git a/python/astra/projector3d_c.pyx b/python/astra/projector3d_c.pyx new file mode 100644 index 0000000..8b978d7 --- /dev/null +++ b/python/astra/projector3d_c.pyx @@ -0,0 +1,119 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import six +from .PyIncludes cimport * + +cimport utils +from .utils import wrap_from_bytes + +cimport PyProjector3DFactory +from .PyProjector3DFactory cimport CProjector3DFactory + +cimport PyProjector3DManager +from .PyProjector3DManager cimport CProjector3DManager + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument + +cdef CProjector3DManager * manProj = <CProjector3DManager * >PyProjector3DManager.getSingletonPtr() + +include "config.pxi" + +IF HAVE_CUDA: + cdef extern from *: + CCudaProjector3D* dynamic_cast_cuda_projector "dynamic_cast<astra::CCudaProjector3D*>" (CProjector3D*) + + +def create(config): + cdef Config * cfg = utils.dictToConfig(six.b('Projector3D'), config) + cdef CProjector3D * proj + proj = PyProjector3DFactory.getSingletonPtr().create(cfg[0]) + if proj == NULL: + del cfg + raise Exception("Error creating Projector3D.") + del cfg + return manProj.store(proj) + + +def delete(ids): + try: + for i in ids: + manProj.remove(i) + except TypeError: + manProj.remove(ids) + + +def clear(): + manProj.clear() + + +def info(): + six.print_(wrap_from_bytes(manProj.info())) + +cdef CProjector3D * getObject(i) except NULL: + cdef CProjector3D * proj = manProj.get(i) + if proj == NULL: + raise Exception("Projector not initialized.") + if not proj.isInitialized(): + raise Exception("Projector not initialized.") + return proj + + +def projection_geometry(i): + cdef CProjector3D * proj = getObject(i) + return utils.configToDict(proj.getProjectionGeometry().getConfiguration()) + + +def volume_geometry(i): + cdef CProjector3D * proj = getObject(i) + return utils.configToDict(proj.getVolumeGeometry().getConfiguration()) + + +def weights_single_ray(i, projection_index, detector_index): + raise Exception("Not yet implemented") + + +def weights_projection(i, projection_index): + raise Exception("Not yet implemented") + + +def splat(i, row, col): + raise Exception("Not yet implemented") + +def is_cuda(i): + cdef CProjector3D * proj = getObject(i) + IF HAVE_CUDA==True: + cdef CCudaProjector3D * cudaproj = NULL + cudaproj = dynamic_cast_cuda_projector(proj) + if cudaproj==NULL: + return False + else: + return True + ELSE: + return False diff --git a/python/astra/projector_c.pyx b/python/astra/projector_c.pyx new file mode 100644 index 0000000..9aa868e --- /dev/null +++ b/python/astra/projector_c.pyx @@ -0,0 +1,134 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import six +from .PyIncludes cimport * + +cimport utils +from .utils import wrap_from_bytes + +cimport PyProjector2DFactory +from .PyProjector2DFactory cimport CProjector2DFactory + +cimport PyProjector2DManager +from .PyProjector2DManager cimport CProjector2DManager + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument + +cimport PyMatrixManager +from .PyMatrixManager cimport CMatrixManager + +cdef CProjector2DManager * manProj = <CProjector2DManager * >PyProjector2DManager.getSingletonPtr() +cdef CMatrixManager * manM = <CMatrixManager * >PyMatrixManager.getSingletonPtr() + +include "config.pxi" + +IF HAVE_CUDA: + cdef extern from *: + CCudaProjector2D* dynamic_cast_cuda_projector "dynamic_cast<astra::CCudaProjector2D*>" (CProjector2D*) + + +def create(config): + cdef Config * cfg = utils.dictToConfig(six.b('Projector2D'), config) + cdef CProjector2D * proj + proj = PyProjector2DFactory.getSingletonPtr().create(cfg[0]) + if proj == NULL: + del cfg + raise Exception("Error creating projector.") + del cfg + return manProj.store(proj) + + +def delete(ids): + try: + for i in ids: + manProj.remove(i) + except TypeError: + manProj.remove(ids) + + +def clear(): + manProj.clear() + + +def info(): + six.print_(wrap_from_bytes(manProj.info())) + +cdef CProjector2D * getObject(i) except NULL: + cdef CProjector2D * proj = manProj.get(i) + if proj == NULL: + raise Exception("Projector not initialized.") + if not proj.isInitialized(): + raise Exception("Projector not initialized.") + return proj + + +def projection_geometry(i): + cdef CProjector2D * proj = getObject(i) + return utils.configToDict(proj.getProjectionGeometry().getConfiguration()) + + +def volume_geometry(i): + cdef CProjector2D * proj = getObject(i) + return utils.configToDict(proj.getVolumeGeometry().getConfiguration()) + + +def weights_single_ray(i, projection_index, detector_index): + raise Exception("Not yet implemented") + + +def weights_projection(i, projection_index): + raise Exception("Not yet implemented") + + +def splat(i, row, col): + raise Exception("Not yet implemented") + +def is_cuda(i): + cdef CProjector2D * proj = getObject(i) + IF HAVE_CUDA==True: + cdef CCudaProjector2D * cudaproj = NULL + cudaproj = dynamic_cast_cuda_projector(proj) + if cudaproj==NULL: + return False + else: + return True + ELSE: + return False + +def matrix(i): + cdef CProjector2D * proj = getObject(i) + cdef CSparseMatrix * mat = proj.getMatrix() + if mat == NULL: + del mat + raise Exception("Data object not found") + if not mat.isInitialized(): + del mat + raise Exception("Data object not initialized properly.") + return manM.store(mat) diff --git a/python/astra/utils.pxd b/python/astra/utils.pxd new file mode 100644 index 0000000..ca84836 --- /dev/null +++ b/python/astra/utils.pxd @@ -0,0 +1,35 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +from libcpp.string cimport string + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument +from .PyXMLDocument cimport XMLNode + +from .PyIncludes cimport * + +cdef configToDict(Config *) +cdef Config * dictToConfig(string rootname, dc) diff --git a/python/astra/utils.pyx b/python/astra/utils.pyx new file mode 100644 index 0000000..0439f1b --- /dev/null +++ b/python/astra/utils.pyx @@ -0,0 +1,236 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- +# distutils: language = c++ +# distutils: libraries = astra + +import numpy as np +import six +from libcpp.string cimport string +from libcpp.list cimport list +from libcpp.vector cimport vector +from cython.operator cimport dereference as deref, preincrement as inc +from cpython.version cimport PY_MAJOR_VERSION + +cimport PyXMLDocument +from .PyXMLDocument cimport XMLDocument +from .PyXMLDocument cimport XMLNode +from .PyIncludes cimport * + + +cdef Config * dictToConfig(string rootname, dc): + cdef Config * cfg = new Config() + cfg.initialize(rootname) + try: + readDict(cfg.self, dc) + except Exception as e: + del cfg + six.print_(e.strerror) + return NULL + return cfg + +def convert_item(item): + if isinstance(item, six.string_types): + return item.encode('ascii') + + if type(item) is not dict: + return item + + out_dict = {} + for k in item: + out_dict[convert_item(k)] = convert_item(item[k]) + return out_dict + + +def wrap_to_bytes(value): + if isinstance(value, six.binary_type): + return value + s = str(value) + if PY_MAJOR_VERSION == 3: + s = s.encode('ascii') + return s + + +def wrap_from_bytes(value): + s = value + if PY_MAJOR_VERSION == 3: + s = s.decode('ascii') + return s + + +cdef void readDict(XMLNode * root, _dc): + cdef XMLNode * listbase + cdef XMLNode * itm + cdef int i + cdef int j + + dc = convert_item(_dc) + for item in dc: + val = dc[item] + if isinstance(val, np.ndarray): + if val.size == 0: + break + listbase = root.addChildNode(item) + listbase.addAttribute(< string > six.b('listsize'), < float32 > val.size) + index = 0 + if val.ndim == 2: + for i in range(val.shape[0]): + for j in range(val.shape[1]): + itm = listbase.addChildNode(six.b('ListItem')) + itm.addAttribute(< string > six.b('index'), < float32 > index) + itm.addAttribute( < string > six.b('value'), < float32 > val[i, j]) + index += 1 + del itm + elif val.ndim == 1: + for i in range(val.shape[0]): + itm = listbase.addChildNode(six.b('ListItem')) + itm.addAttribute(< string > six.b('index'), < float32 > index) + itm.addAttribute(< string > six.b('value'), < float32 > val[i]) + index += 1 + del itm + else: + raise Exception("Only 1 or 2 dimensions are allowed") + del listbase + elif isinstance(val, dict): + if item == six.b('option') or item == six.b('options') or item == six.b('Option') or item == six.b('Options'): + readOptions(root, val) + else: + itm = root.addChildNode(item) + readDict(itm, val) + del itm + else: + if item == six.b('type'): + root.addAttribute(< string > six.b('type'), <string> wrap_to_bytes(val)) + else: + itm = root.addChildNode(item, wrap_to_bytes(val)) + del itm + +cdef void readOptions(XMLNode * node, dc): + cdef XMLNode * listbase + cdef XMLNode * itm + cdef int i + cdef int j + for item in dc: + val = dc[item] + if node.hasOption(item): + raise Exception('Duplicate Option: %s' % item) + if isinstance(val, np.ndarray): + if val.size == 0: + break + listbase = node.addChildNode(six.b('Option')) + listbase.addAttribute(< string > six.b('key'), < string > item) + listbase.addAttribute(< string > six.b('listsize'), < float32 > val.size) + index = 0 + if val.ndim == 2: + for i in range(val.shape[0]): + for j in range(val.shape[1]): + itm = listbase.addChildNode(six.b('ListItem')) + itm.addAttribute(< string > six.b('index'), < float32 > index) + itm.addAttribute( < string > six.b('value'), < float32 > val[i, j]) + index += 1 + del itm + elif val.ndim == 1: + for i in range(val.shape[0]): + itm = listbase.addChildNode(six.b('ListItem')) + itm.addAttribute(< string > six.b('index'), < float32 > index) + itm.addAttribute(< string > six.b('value'), < float32 > val[i]) + index += 1 + del itm + else: + raise Exception("Only 1 or 2 dimensions are allowed") + del listbase + else: + node.addOption(item, wrap_to_bytes(val)) + +cdef configToDict(Config *cfg): + return XMLNode2dict(cfg.self) + +def castString3(input): + return input.decode('utf-8') + +def castString2(input): + return input + +if six.PY3: + castString = castString3 +else: + castString = castString2 + +def stringToPythonValue(inputIn): + input = castString(inputIn) + # matrix + if ';' in input: + row_strings = input.split(';') + col_strings = row_strings[0].split(',') + nRows = len(row_strings) + nCols = len(col_strings) + + out = np.empty((nRows,nCols)) + for ridx, row in enumerate(row_strings): + col_strings = row.split(',') + for cidx, col in enumerate(col_strings): + out[ridx,cidx] = float(col) + return out + + # vector + if ',' in input: + items = input.split(',') + out = np.empty(len(items)) + for idx,item in enumerate(items): + out[idx] = float(item) + return out + + try: + # integer + return int(input) + except ValueError: + try: + #float + return float(input) + except ValueError: + # string + return str(input) + + +cdef XMLNode2dict(XMLNode * node): + cdef XMLNode * subnode + cdef list[XMLNode * ] nodes + cdef list[XMLNode * ].iterator it + dct = {} + opts = {} + if node.hasAttribute(six.b('type')): + dct['type'] = castString(node.getAttribute(six.b('type'))) + nodes = node.getNodes() + it = nodes.begin() + while it != nodes.end(): + subnode = deref(it) + if castString(subnode.getName())=="Option": + opts[castString(subnode.getAttribute('key'))] = stringToPythonValue(subnode.getAttribute('value')) + else: + dct[castString(subnode.getName())] = stringToPythonValue(subnode.getContent()) + del subnode + inc(it) + if len(opts)>0: dct['options'] = opts + return dct |