Skip to content
Snippets Groups Projects
Commit c9e2f0d5 authored by Himanen, Lauri (himanel1)'s avatar Himanen, Lauri (himanel1)
Browse files

Added type and dimension checking for the LocalBackend. Very useful for testing.

parent 233b8267
Branches
Tags
No related merge requests found
......@@ -9,7 +9,7 @@ logger = logging.getLogger(__name__)
class LocalBackend(object):
"""A backend that outputs results into a regular python dictionary.
"""
def __init__(self, metaInfoEnv, fileOut=StringIO.StringIO()):
def __init__(self, metaInfoEnv, fileOut=StringIO.StringIO(), debug=True):
self.__metaInfoEnv = metaInfoEnv
self.fileOut = fileOut
self.__gIndex = -1
......@@ -19,6 +19,7 @@ class LocalBackend(object):
self.results = Results(metaInfoEnv)
self.dataManagers = {}
self.sectionManagers = {}
self.debug = debug
for ikNames, ik in metaInfoEnv.infoKinds.items():
if ik.kindStr == "type_section":
......@@ -48,13 +49,109 @@ class LocalBackend(object):
manager.closeSection(self, gIndex)
def addValue(self, metaName, value, gIndex=-1):
dataManager = self.dataManagers[metaName]
if self.debug:
# Check that the value is actually of scalar type
value_type = type(value)
if value_type not in [float, int, bool, str, unicode]:
raise TypeError("The value for metainfo '{}' is not of scalar nature.".format(metaName))
# Check that the metainfo should be scalar
metainfo_shape = dataManager.metaInfo.shape
if metainfo_shape is not None:
if len(metainfo_shape) != 0:
raise TypeError("The metainfo '{}' does not support scalar values. Check the shape attribute of the metainfo and use the function addArrayValues() instead if the value should be an array.".format(metaName))
# Check the type
dtype_str = dataManager.metaInfo.dtypeStr
numpy_types = self.python_type_for_metainfo_type(dtype_str)
actual_numpy_type = type(value)
if actual_numpy_type not in numpy_types:
raise TypeError("The given value for metainfo '{}' is of incorrrect type. The type was '{}' when it should be one of '{}'".format(metaName, actual_numpy_type, numpy_types))
dataManager.superSectionManager.addValue(dataManager.metaInfo, value, gIndex)
def addArrayValues(self, metaName, values, gIndex=-1):
dataManager = self.dataManagers[metaName]
if self.debug:
# Check that the value is actually a numpy array
if not isinstance(values, np.ndarray):
raise TypeError("The value provided for '{}' is not a valid numpy array. Please only push numpy arrays with the backend function addArrayValues().".format(metaName))
# Check that the metainfo should be an array
metainfo_shape = dataManager.metaInfo.shape
if len(metainfo_shape) == 0:
raise TypeError("The metainfo '{}' does not support arrays. Check the shape attribute of the metainfo and use the function addValue() instead if the value should be scalar.".format(metaName))
# Check the number of dimensions
array_shape = values.shape
len_meta_dim = len(metainfo_shape)
len_array_dim = len(array_shape)
if len_array_dim != len_meta_dim:
raise TypeError("Incompatible shape provided for metainfo '{}'. The shape was '{}' whereas it should be '{}'. Check the shape attribute of the metainfo".format(metaName, array_shape, metainfo_shape))
# If the shapes are given as integers in the metainfo we can also
# check the number of values in each dimension
try:
[int(x) for x in metainfo_shape]
except Exception:
pass
else:
for index in range(len_meta_dim):
array_dim = array_shape[index]
metainfo_dim = metainfo_shape[index]
if array_dim != metainfo_dim:
raise TypeError("Incompatible shape provided for metainfo '{}'. The shape was '{}' whereas it should be '{}'. Check the shape attribute of the metainfo".format(metaName, array_shape, metainfo_shape))
# Check the type
dtype_str = dataManager.metaInfo.dtypeStr
numpy_types = self.numpy_type_for_metainfo_type(dtype_str)
actual_numpy_type = values.dtype.type
if actual_numpy_type not in numpy_types:
raise TypeError("The given array for metainfo '{}' has incorrect type of values in it.".format(metaName))
dataManager.superSectionManager.addArrayValues(dataManager.metaInfo, values, gIndex)
def numpy_type_for_metainfo_type(self, dtypeStr):
"""Returns a list of numpy types correspoding to the dtypeStr of a
metainfo.
"""
if dtypeStr == "f":
return [np.float_, np.int_]
elif dtypeStr == "i":
return [np.int_]
elif dtypeStr == "b":
return [np.bool_]
elif dtypeStr == "C":
return [np.string_, np.unicode_]
elif dtypeStr == "r":
return [np.int_]
else:
raise TypeError("Could not determine the numpy type for metainfo type '{}'".format(dtypeStr))
def python_type_for_metainfo_type(self, dtypeStr):
"""Returns a list of numpy types correspoding to the dtypeStr of a
metainfo.
"""
if dtypeStr == "f":
return [float, int]
elif dtypeStr == "i":
return [int]
elif dtypeStr == "b":
return [bool]
elif dtypeStr == "C":
return [str, unicode]
elif dtypeStr == "r":
return [int]
else:
raise TypeError("Could not determine the type for metainfo type '{}'".format(dtypeStr))
def setArrayValues(self, metaName, values, offset=None, gIndex=-1, unit=None):
"""Adds values to the last array added, array must be a numpy array
"""
......@@ -335,7 +432,7 @@ class SectionManager(object):
gI = gIndex
try:
self.openSections[gI].addValue(valueMetaInfo, value)
except:
except KeyError:
raise Exception("Cannot add value for metadata %s to section %d (%d) of %s, as it is not open" % (valueMetaInfo.name, gI, gIndex, self.metaInfo.name))
def setArrayValues(self, valueMetaInfo, value, offset=None, gIndex=-1):
......@@ -345,7 +442,7 @@ class SectionManager(object):
gI = gIndex
try:
self.openSections[gI].setArrayValues(valueMetaInfo, value, offset)
except:
except KeyError:
raise Exception("Cannot set array values for metadata %s to section %d (%d) of %s, as it is not open" % (valueMetaInfo.name, gI, gIndex, self.metaInfo.name))
def addArrayValues(self, valueMetaInfo, value, offset=None, gIndex=-1):
......@@ -355,7 +452,7 @@ class SectionManager(object):
gI = gIndex
try:
self.openSections[gI].addArrayValues(valueMetaInfo, value)
except:
except KeyError:
raise Exception("Cannot add array values for metadata %s to section %d (%d) of %s, as it is not open" % (valueMetaInfo.name, gI, gIndex, self.metaInfo.name))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment