From 1d851f0a8fa093e044c7264569cc6f88df39a16e Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Thu, 26 Jan 2017 14:57:42 +0100 Subject: Remove unused 3d global min/max --- python/astra/data3d_c.pyx | 1 - 1 file changed, 1 deletion(-) (limited to 'python/astra/data3d_c.pyx') diff --git a/python/astra/data3d_c.pyx b/python/astra/data3d_c.pyx index 915c60d..1b9293a 100644 --- a/python/astra/data3d_c.pyx +++ b/python/astra/data3d_c.pyx @@ -132,7 +132,6 @@ def create(datatype,geometry,data=None, link=False): if not link: fillDataObject(pDataObject3D, data) - pDataObject3D.updateStatistics() return man3d.store(pDataObject3D) -- cgit v1.2.3 From ebd5fe932fd2d6c4a516bc1cdc69e37aa4f5cf55 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 8 Feb 2017 10:43:35 +0100 Subject: Make python -sinocone a synonym of -sino. The matlab interface already behaves like this. --- python/astra/data3d_c.pyx | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'python/astra/data3d_c.pyx') diff --git a/python/astra/data3d_c.pyx b/python/astra/data3d_c.pyx index 1b9293a..3934f22 100644 --- a/python/astra/data3d_c.pyx +++ b/python/astra/data3d_c.pyx @@ -86,7 +86,7 @@ def create(datatype,geometry,data=None, link=False): pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry) del cfg del pGeometry - elif datatype == '-sino' or datatype == '-proj3d': + elif datatype == '-sino' or datatype == '-proj3d' or datatype == '-sinocone': cfg = utils.dictToConfig(six.b('ProjectionGeometry'), geometry) tpe = wrap_from_bytes(cfg.self.getAttribute(six.b('type'))) if (tpe == "parallel3d"): @@ -111,18 +111,6 @@ def create(datatype,geometry,data=None, link=False): pDataObject3D = 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.') - if link: - pCustom = new CFloat32CustomPython(data) - pDataObject3D = new CFloat32ProjectionData3DMemory(pppGeometry, pCustom) - else: - pDataObject3D = new CFloat32ProjectionData3DMemory(pppGeometry) else: raise Exception("Invalid datatype. Please specify '-vol' or '-proj3d'.") -- cgit v1.2.3 From d85a660f064e8130b27e11c7fd762221c754c315 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Thu, 26 Jan 2017 14:57:57 +0100 Subject: Start work on CFloat32Data3DGPU to allow persistent/external GPU memory --- python/astra/data3d_c.pyx | 53 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 13 deletions(-) (limited to 'python/astra/data3d_c.pyx') diff --git a/python/astra/data3d_c.pyx b/python/astra/data3d_c.pyx index 3934f22..56247de 100644 --- a/python/astra/data3d_c.pyx +++ b/python/astra/data3d_c.pyx @@ -45,12 +45,17 @@ from .PyXMLDocument cimport XMLDocument cimport utils from .utils import wrap_from_bytes -from .pythonutils import geom_size +from .pythonutils import geom_size, GPULink import operator from six.moves import reduce +include "config.pxi" + +cdef extern from "Python.h": + void* PyLong_AsVoidPtr(object) + cdef CData3DManager * man3d = PyData3DManager.getSingletonPtr() @@ -65,12 +70,19 @@ def create(datatype,geometry,data=None, link=False): cdef Config *cfg cdef CVolumeGeometry3D * pGeometry cdef CProjectionGeometry3D * ppGeometry - cdef CFloat32Data3DMemory * pDataObject3D + cdef CFloat32Data3D * pDataObject3D cdef CConeProjectionGeometry3D* pppGeometry - cdef CFloat32CustomMemory * pCustom - - if link and data.shape!=geom_size(geometry): - raise Exception("The dimensions of the data do not match those specified in the geometry.") + cdef CFloat32CustomMemory * pCustom = NULL + IF HAVE_CUDA==True: + cdef MemHandle3D hnd + + if link: + if isinstance(data, GPULink): + s = geom_size(geometry) + if geom_size(geometry) != ( data.z, data.y, data.x ): + raise Exception("The dimensions of the data do not match those specified in the geometry.") + elif data.shape!=geom_size(geometry): + raise Exception("The dimensions of the data do not match those specified in the geometry.") if datatype == '-vol': cfg = utils.dictToConfig(six.b('VolumeGeometry'), geometry) @@ -80,10 +92,18 @@ def create(datatype,geometry,data=None, link=False): del pGeometry raise Exception('Geometry class not initialized.') if link: - pCustom = new CFloat32CustomPython(data) - pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry, pCustom) + if isinstance(data, GPULink): + IF HAVE_CUDA==True: + s = geom_size(geometry) + hnd = wrapHandle(PyLong_AsVoidPtr(data.ptr), data.x, data.y, data.z, data.pitch/4) + pDataObject3D = new CFloat32VolumeData3DGPU(pGeometry, hnd) + ELSE: + raise NotImplementedError("CUDA support is not enabled in ASTRA") + else: + pCustom = new CFloat32CustomPython(data) + pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry, pCustom) else: - pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry) + pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry) del cfg del pGeometry elif datatype == '-sino' or datatype == '-proj3d' or datatype == '-sinocone': @@ -105,8 +125,16 @@ def create(datatype,geometry,data=None, link=False): del ppGeometry raise Exception('Geometry class not initialized.') if link: - pCustom = new CFloat32CustomPython(data) - pDataObject3D = new CFloat32ProjectionData3DMemory(ppGeometry, pCustom) + if isinstance(data, GPULink): + IF HAVE_CUDA==True: + s = geom_size(geometry) + hnd = wrapHandle(PyLong_AsVoidPtr(data.ptr), data.x, data.y, data.z, data.pitch/4) + pDataObject3D = new CFloat32ProjectionData3DGPU(ppGeometry, hnd) + ELSE: + raise NotImplementedError("CUDA support is not enabled in ASTRA") + else: + pCustom = new CFloat32CustomPython(data) + pDataObject3D = new CFloat32ProjectionData3DMemory(ppGeometry, pCustom) else: pDataObject3D = new CFloat32ProjectionData3DMemory(ppGeometry) del ppGeometry @@ -118,8 +146,7 @@ def create(datatype,geometry,data=None, link=False): del pDataObject3D raise Exception("Couldn't initialize data object.") - if not link: fillDataObject(pDataObject3D, data) - + if not link: fillDataObject(dynamic_cast_mem(pDataObject3D), data) return man3d.store(pDataObject3D) -- cgit v1.2.3 From ae33f713a2dea236e28145dcd6007589feb618ed Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 8 Feb 2017 10:47:22 +0100 Subject: Make typechecks in data3d.create more robust --- python/astra/data3d_c.pyx | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'python/astra/data3d_c.pyx') diff --git a/python/astra/data3d_c.pyx b/python/astra/data3d_c.pyx index 56247de..73e75b9 100644 --- a/python/astra/data3d_c.pyx +++ b/python/astra/data3d_c.pyx @@ -77,12 +77,15 @@ def create(datatype,geometry,data=None, link=False): cdef MemHandle3D hnd if link: - if isinstance(data, GPULink): + if isinstance(data, np.ndarray): + if data.shape != geom_size(geometry): + raise Exception("The dimensions of the data do not match those specified in the geometry.") + elif isinstance(data, GPULink): s = geom_size(geometry) if geom_size(geometry) != ( data.z, data.y, data.x ): raise Exception("The dimensions of the data do not match those specified in the geometry.") - elif data.shape!=geom_size(geometry): - raise Exception("The dimensions of the data do not match those specified in the geometry.") + else: + raise TypeError("data should be a numpy.ndarray or a GPULink object") if datatype == '-vol': cfg = utils.dictToConfig(six.b('VolumeGeometry'), geometry) @@ -92,7 +95,10 @@ def create(datatype,geometry,data=None, link=False): del pGeometry raise Exception('Geometry class not initialized.') if link: - if isinstance(data, GPULink): + if isinstance(data, np.ndarray): + pCustom = new CFloat32CustomPython(data) + pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry, pCustom) + elif isinstance(data, GPULink): IF HAVE_CUDA==True: s = geom_size(geometry) hnd = wrapHandle(PyLong_AsVoidPtr(data.ptr), data.x, data.y, data.z, data.pitch/4) @@ -100,8 +106,7 @@ def create(datatype,geometry,data=None, link=False): ELSE: raise NotImplementedError("CUDA support is not enabled in ASTRA") else: - pCustom = new CFloat32CustomPython(data) - pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry, pCustom) + raise TypeError("data should be a numpy.ndarray or a GPULink object") else: pDataObject3D = new CFloat32VolumeData3DMemory(pGeometry) del cfg @@ -125,7 +130,10 @@ def create(datatype,geometry,data=None, link=False): del ppGeometry raise Exception('Geometry class not initialized.') if link: - if isinstance(data, GPULink): + if isinstance(data, np.ndarray): + pCustom = new CFloat32CustomPython(data) + pDataObject3D = new CFloat32ProjectionData3DMemory(ppGeometry, pCustom) + elif isinstance(data, GPULink): IF HAVE_CUDA==True: s = geom_size(geometry) hnd = wrapHandle(PyLong_AsVoidPtr(data.ptr), data.x, data.y, data.z, data.pitch/4) @@ -133,8 +141,7 @@ def create(datatype,geometry,data=None, link=False): ELSE: raise NotImplementedError("CUDA support is not enabled in ASTRA") else: - pCustom = new CFloat32CustomPython(data) - pDataObject3D = new CFloat32ProjectionData3DMemory(ppGeometry, pCustom) + raise TypeError("data should be a numpy.ndarray or a GPULink object") else: pDataObject3D = new CFloat32ProjectionData3DMemory(ppGeometry) del ppGeometry @@ -146,7 +153,8 @@ def create(datatype,geometry,data=None, link=False): del pDataObject3D raise Exception("Couldn't initialize data object.") - if not link: fillDataObject(dynamic_cast_mem(pDataObject3D), data) + if not link: + fillDataObject(dynamic_cast_mem(pDataObject3D), data) return man3d.store(pDataObject3D) -- cgit v1.2.3 From 2af27e7ba08a65cce15c7e0658712fbba8f447fd Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 8 Feb 2017 11:31:31 +0100 Subject: Improve data2d/data3d error messages --- python/astra/data3d_c.pyx | 63 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-) (limited to 'python/astra/data3d_c.pyx') diff --git a/python/astra/data3d_c.pyx b/python/astra/data3d_c.pyx index 73e75b9..78ed620 100644 --- a/python/astra/data3d_c.pyx +++ b/python/astra/data3d_c.pyx @@ -77,15 +77,15 @@ def create(datatype,geometry,data=None, link=False): cdef MemHandle3D hnd if link: + geom_shape = geom_size(geometry) if isinstance(data, np.ndarray): - if data.shape != geom_size(geometry): - raise Exception("The dimensions of the data do not match those specified in the geometry.") + data_shape = data.shape elif isinstance(data, GPULink): - s = geom_size(geometry) - if geom_size(geometry) != ( data.z, data.y, data.x ): - raise Exception("The dimensions of the data do not match those specified in the geometry.") + data_shape = ( data.z, data.y, data.x ) else: raise TypeError("data should be a numpy.ndarray or a GPULink object") + if geom_shape != data_shape: + raise ValueError("The dimensions of the data do not match those specified in the geometry: {} != {}".format(data_shape, geom_shape)) if datatype == '-vol': cfg = utils.dictToConfig(six.b('VolumeGeometry'), geometry) @@ -93,7 +93,7 @@ def create(datatype,geometry,data=None, link=False): if not pGeometry.initialize(cfg[0]): del cfg del pGeometry - raise Exception('Geometry class not initialized.') + raise RuntimeError('Geometry class not initialized.') if link: if isinstance(data, np.ndarray): pCustom = new CFloat32CustomPython(data) @@ -123,12 +123,12 @@ def create(datatype,geometry,data=None, link=False): elif (tpe == "cone_vec"): ppGeometry = new CConeVecProjectionGeometry3D(); else: - raise Exception("Invalid geometry type.") + raise ValueError("Invalid geometry type.") if not ppGeometry.initialize(cfg[0]): del cfg del ppGeometry - raise Exception('Geometry class not initialized.') + raise RuntimeError('Geometry class not initialized.') if link: if isinstance(data, np.ndarray): pCustom = new CFloat32CustomPython(data) @@ -147,11 +147,11 @@ def create(datatype,geometry,data=None, link=False): del ppGeometry del cfg else: - raise Exception("Invalid datatype. Please specify '-vol' or '-proj3d'.") + raise ValueError("Invalid datatype. Please specify '-vol' or '-proj3d'.") if not pDataObject3D.isInitialized(): del pDataObject3D - raise Exception("Couldn't initialize data object.") + raise RuntimeError("Couldn't initialize data object.") if not link: fillDataObject(dynamic_cast_mem(pDataObject3D), data) @@ -169,7 +169,7 @@ def get_geometry(i): pDataObject3 = pDataObject geom = utils.configToDict(pDataObject3.getGeometry().getConfiguration()) else: - raise Exception("Not a known data object") + raise RuntimeError("Not a known data object") return geom def change_geometry(i, geom): @@ -190,18 +190,18 @@ def change_geometry(i, geom): elif (tpe == "cone_vec"): ppGeometry = new CConeVecProjectionGeometry3D(); else: - raise Exception("Invalid geometry type.") + raise ValueError("Invalid geometry type.") if not ppGeometry.initialize(cfg[0]): del cfg del ppGeometry - raise Exception('Geometry class not initialized.') + raise RuntimeError('Geometry class not initialized.') del cfg - if (ppGeometry.getDetectorColCount() != pDataObject2.getDetectorColCount() or \ - ppGeometry.getProjectionCount() != pDataObject2.getAngleCount() or \ - ppGeometry.getDetectorRowCount() != pDataObject2.getDetectorRowCount()): + geom_shape = (ppGeometry.getDetectorRowCount(), ppGeometry.getProjectionCount(), ppGeometry.getDetectorColCount()) + obj_shape = (pDataObject2.getDetectorRowCount(), pDataObject2.getAngleCount(), pDataObject2.getDetectorColCount()) + if geom_shape != obj_shape: del ppGeometry - raise Exception( - "The dimensions of the data do not match those specified in the geometry.") + raise ValueError( + "The dimensions of the data do not match those specified in the geometry: {} != {}".format(obj_shape, geom_shape)) pDataObject2.changeGeometry(ppGeometry) del ppGeometry @@ -212,19 +212,19 @@ def change_geometry(i, geom): if not pGeometry.initialize(cfg[0]): del cfg del pGeometry - raise Exception('Geometry class not initialized.') + raise RuntimeError('Geometry class not initialized.') del cfg - if (pGeometry.getGridColCount() != pDataObject3.getColCount() or \ - pGeometry.getGridRowCount() != pDataObject3.getRowCount() or \ - pGeometry.getGridSliceCount() != pDataObject3.getSliceCount()): + geom_shape = (pGeometry.getGridSliceCount(), pGeometry.getGridRowCount(), pGeometry.getGridColCount()) + obj_shape = (pDataObject3.getSliceCount(), pDataObject3.getRowCount(), pDataObject3.getColCount()) + if geom_shape != obj_shape: del pGeometry - raise Exception( - "The dimensions of the data do not match those specified in the geometry.") + raise ValueError( + "The dimensions of the data do not match those specified in the geometry.".format(obj_shape, geom_shape)) pDataObject3.changeGeometry(pGeometry) del pGeometry else: - raise Exception("Not a known data object") + raise RuntimeError("Not a known data object") cdef fillDataObject(CFloat32Data3DMemory * obj, data): @@ -232,6 +232,10 @@ cdef fillDataObject(CFloat32Data3DMemory * obj, data): fillDataObjectScalar(obj, 0) else: if isinstance(data, np.ndarray): + obj_shape = (obj.getDepth(), obj.getHeight(), obj.getWidth()) + if data.shape != obj_shape: + raise ValueError( + "The dimensions of the data do not match those specified in the geometry: {} != {}".format(data.shape, obj_shape)) fillDataObjectArray(obj, np.ascontiguousarray(data,dtype=np.float32)) else: fillDataObjectScalar(obj, np.float32(data)) @@ -244,18 +248,15 @@ cdef fillDataObjectScalar(CFloat32Data3DMemory * obj, float 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 = 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") + raise ValueError("Data object not found") if not pDataObject.isInitialized(): - raise Exception("Data object not initialized properly.") + raise RuntimeError("Data object not initialized properly.") return pDataObject @cython.boundscheck(False) @@ -278,7 +279,7 @@ def get_shared(i): return np.PyArray_SimpleNewFromData(3,shape,np.NPY_FLOAT32,pDataObject.getData3D()[0][0]) def get_single(i): - raise Exception("Not yet implemented") + raise NotImplementedError("Not yet implemented") def store(i,data): cdef CFloat32Data3D * pDataObject = getObject(i) -- cgit v1.2.3