summaryrefslogtreecommitdiffstats
path: root/python/astra/utils.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'python/astra/utils.pyx')
-rw-r--r--python/astra/utils.pyx61
1 files changed, 39 insertions, 22 deletions
diff --git a/python/astra/utils.pyx b/python/astra/utils.pyx
index ddb37aa..34d1902 100644
--- a/python/astra/utils.pyx
+++ b/python/astra/utils.pyx
@@ -1,37 +1,41 @@
-#-----------------------------------------------------------------------
-#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam
+# -----------------------------------------------------------------------
+# Copyright: 2010-2016, iMinds-Vision Lab, University of Antwerp
+# 2013-2016, CWI, Amsterdam
#
-#Author: Daniel M. Pelt
-#Contact: D.M.Pelt@cwi.nl
-#Website: http://dmpelt.github.io/pyastratoolbox/
+# Contact: astra@uantwerpen.be
+# Website: http://sf.net/projects/astra-toolbox
#
+# This file is part of the ASTRA Toolbox.
#
-#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 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.
+# 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/>.
+# You should have received a copy of the GNU General Public License
+# along with the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.
+#
+# -----------------------------------------------------------------------
#
-#-----------------------------------------------------------------------
# distutils: language = c++
# distutils: libraries = astra
cimport numpy as np
import numpy as np
import six
+if six.PY3:
+ import builtins
+else:
+ import __builtin__ as builtins
from libcpp.string cimport string
-from libcpp.list cimport list
from libcpp.vector cimport vector
+from libcpp.list cimport list
from cython.operator cimport dereference as deref, preincrement as inc
from cpython.version cimport PY_MAJOR_VERSION
@@ -91,11 +95,14 @@ cdef void readDict(XMLNode root, _dc):
dc = convert_item(_dc)
for item in dc:
val = dc[item]
+ if isinstance(val, builtins.list) or isinstance(val, tuple):
+ val = np.array(val,dtype=np.float64)
if isinstance(val, np.ndarray):
if val.size == 0:
break
listbase = root.addChildNode(item)
- data = <double*>np.PyArray_DATA(np.ascontiguousarray(val,dtype=np.float64))
+ contig_data = np.ascontiguousarray(val,dtype=np.float64)
+ data = <double*>np.PyArray_DATA(contig_data)
if val.ndim == 2:
listbase.setContent(data, val.shape[1], val.shape[0], False)
elif val.ndim == 1:
@@ -112,6 +119,8 @@ cdef void readDict(XMLNode root, _dc):
if item == six.b('type'):
root.addAttribute(< string > six.b('type'), <string> wrap_to_bytes(val))
else:
+ if isinstance(val, builtins.bool):
+ val = int(val)
itm = root.addChildNode(item, wrap_to_bytes(val))
cdef void readOptions(XMLNode node, dc):
@@ -124,12 +133,15 @@ cdef void readOptions(XMLNode node, dc):
val = dc[item]
if node.hasOption(item):
raise Exception('Duplicate Option: %s' % item)
+ if isinstance(val, builtins.list) or isinstance(val, tuple):
+ val = np.array(val,dtype=np.float64)
if isinstance(val, np.ndarray):
if val.size == 0:
break
listbase = node.addChildNode(six.b('Option'))
listbase.addAttribute(< string > six.b('key'), < string > item)
- data = <double*>np.PyArray_DATA(np.ascontiguousarray(val,dtype=np.float64))
+ contig_data = np.ascontiguousarray(val,dtype=np.float64)
+ data = <double*>np.PyArray_DATA(contig_data)
if val.ndim == 2:
listbase.setContent(data, val.shape[1], val.shape[0], False)
elif val.ndim == 1:
@@ -137,6 +149,8 @@ cdef void readOptions(XMLNode node, dc):
else:
raise Exception("Only 1 or 2 dimensions are allowed")
else:
+ if isinstance(val, builtins.bool):
+ val = int(val)
node.addOption(item, wrap_to_bytes(val))
cdef configToDict(Config *cfg):
@@ -202,7 +216,10 @@ cdef XMLNode2dict(XMLNode node):
while it != nodes.end():
subnode = deref(it)
if castString(subnode.getName())=="Option":
- opts[castString(subnode.getAttribute('key'))] = stringToPythonValue(subnode.getAttribute('value'))
+ if subnode.hasAttribute('value'):
+ opts[castString(subnode.getAttribute('key'))] = stringToPythonValue(subnode.getAttribute('value'))
+ else:
+ opts[castString(subnode.getAttribute('key'))] = stringToPythonValue(subnode.getContent())
else:
dct[castString(subnode.getName())] = stringToPythonValue(subnode.getContent())
inc(it)