From 492c0211608fa756ba6642ff7ae3b479765a955b Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 23 Jun 2020 15:19:06 +0200 Subject: Check numpy array type --- python/astra/pythonutils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'python/astra/pythonutils.py') diff --git a/python/astra/pythonutils.py b/python/astra/pythonutils.py index 715df30..ef49f97 100644 --- a/python/astra/pythonutils.py +++ b/python/astra/pythonutils.py @@ -29,6 +29,8 @@ """ +import numpy as np + def geom_size(geom, dim=None): """Returns the size of a volume or sinogram, based on the projection or volume geometry. @@ -62,6 +64,19 @@ def geom_size(geom, dim=None): return s +def checkArrayForLink(data): + """Check if a numpy array is suitable for direct usage (contiguous, etc.) + + This function raises an exception if not. + """ + + if not isinstance(data, np.ndarray): + raise ValueError("Numpy array should be numpy.ndarray") + if data.dtype != np.float32: + raise ValueError("Numpy array should be float32") + if not (data.flags['C_CONTIGUOUS'] and data.flags['ALIGNED']): + raise ValueError("Numpy array should be C_CONTIGUOUS and ALIGNED") + class GPULink(object): """Utility class for astra.data3d.link with a CUDA pointer -- cgit v1.2.3