def test_wrong_array_from_string(self): """Test that parsing an array from string is throwing the expected exception when wrong input string""" row = {'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'quantifier': 'manual', 'elementType': 'float', 'type': 'array', 'options': None, 'name': 'test'} input_data_string = '[ [1,2 3] [4,5,6]]' self.assertRaises(ValueError, string2array, input_data_string, ',', row['elementType']) input_data_string = '[ [1,2,wrong], [4, 5, 6]]' self.assertRaises(ValueError, string2array, input_data_string, ',', row['elementType']) row = {'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'quantifier': 'manual', 'elementType': 'str', 'type': 'array', 'options': None, 'name': 'test'} output = string2array(input_data_string, ',', row['elementType']) self.assertEqual(output.shape, (2, 3)) self.assertEqual(output[0][2], 'wrong', 'String data not converted properly') input_data_string = '[ [1,2 3] [4,5,6]]' output = string2array(input_data_string, ',', row['elementType']) self.assertEqual(output[0][1], '2 3')
def test_array_from_string(self): """ Simple test for parse array on 1d, 2d and 3d array. """ row = { 'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'quantifier': 'manual', 'elementType': 'float', 'type': 'array', 'options': None, 'name': 'test' } input_data_string = '[ [1 2 3] [4 5 6]]' output = string2array(input_data_string, ' ', row['elementType']) self.assertEqual(output.shape, (2, 3), "Dimensions not properly parsed") for i in output[0]: self.assertTrue(i in [1, 2, 3]) for i in output[1]: self.assertTrue(i in [4, 5, 6]) input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) self.assertEqual(output.shape, (6, ), "Dimensions not properly parsed") for i in output: self.assertTrue(i in [1, 2, 3, 4, 5, 6]) input_data_string = '[ [ [1,1], [2, 2] ], [ [3 ,3], [4,4] ] ]' output = string2array(input_data_string, ',', row['elementType']) self.assertEqual(output.shape, (2, 2, 2), "Wrong dimensions.") for i in output[0][0]: self.assertTrue(i == 1) for i in output[0][1]: self.assertTrue(i == 2) for i in output[1][0]: self.assertTrue(i == 3) for i in output[1][1]: self.assertTrue(i == 4) row = { 'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'quantifier': 'manual', 'elementType': 'str', 'type': 'array', 'options': None, 'name': 'test' } input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) for i in output: self.assertTrue(i in [1, 2, 3, 4, 5, 6])
def test_array_from_string(self): """ Simple test for parse array on 1d, 2d and 3d array. """ row = { "description": "test.", "default": "None", "required": True, "label": "test: ", "attributes": None, "quantifier": "manual", "elementType": "float", "type": "array", "options": None, "name": "test", } input_data_string = "[ [1 2 3] [4 5 6]]" output = string2array(input_data_string, " ", row["elementType"]) self.assertEqual(output.shape, (2, 3), "Dimensions not properly parsed") for i in output[0]: self.assertTrue(i in [1, 2, 3]) for i in output[1]: self.assertTrue(i in [4, 5, 6]) input_data_string = "[1, 2, 3, 4, 5, 6]" output = string2array(input_data_string, ",", row["elementType"]) self.assertEqual(output.shape, (6,), "Dimensions not properly parsed") for i in output: self.assertTrue(i in [1, 2, 3, 4, 5, 6]) input_data_string = "[ [ [1,1], [2, 2] ], [ [3 ,3], [4,4] ] ]" output = string2array(input_data_string, ",", row["elementType"]) self.assertEqual(output.shape, (2, 2, 2), "Wrong dimensions.") for i in output[0][0]: self.assertTrue(i == 1) for i in output[0][1]: self.assertTrue(i == 2) for i in output[1][0]: self.assertTrue(i == 3) for i in output[1][1]: self.assertTrue(i == 4) row = { "description": "test.", "default": "None", "required": True, "label": "test: ", "attributes": None, "quantifier": "manual", "elementType": "str", "type": "array", "options": None, "name": "test", } input_data_string = "[1, 2, 3, 4, 5, 6]" output = string2array(input_data_string, ",", row["elementType"]) for i in output: self.assertTrue(i in [1, 2, 3, 4, 5, 6])
def test_array_from_string(self): """ Simple test for parse array on 1d, 2d and 3d array. """ row = { 'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'elementType': 'float', 'type': 'array', 'options': None, 'name': 'test' } input_data_string = '[ [1 2 3] [4 5 6]]' output = string2array(input_data_string, ' ', row['elementType']) assert output.shape, (2, 3) == "Dimensions not properly parsed" for i in output[0]: assert i in [1, 2, 3] for i in output[1]: assert i in [4, 5, 6] input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) assert output.shape == (6, ), "Dimensions not properly parsed" for i in output: assert i in [1, 2, 3, 4, 5, 6] input_data_string = '[ [ [1,1], [2, 2] ], [ [3 ,3], [4,4] ] ]' output = string2array(input_data_string, ',', row['elementType']) assert output.shape == (2, 2, 2), "Wrong dimensions." for i in output[0][0]: assert i == 1 for i in output[0][1]: assert i == 2 for i in output[1][0]: assert i == 3 for i in output[1][1]: assert i == 4 row = { 'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'elementType': 'str', 'type': 'array', 'options': None, 'name': 'test' } input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) for i in output: assert i in [1, 2, 3, 4, 5, 6]
def test_array_from_string(self): """ Simple test for parse array on 1d, 2d and 3d array. """ row = {'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'quantifier': 'manual', 'elementType': 'float', 'type': 'array', 'options': None, 'name': 'test'} input_data_string = '[ [1 2 3] [4 5 6]]' output = string2array(input_data_string, ' ', row['elementType']) self.assertEqual(output.shape, (2, 3), "Dimensions not properly parsed") for i in output[0]: self.assertTrue(i in [1, 2, 3]) for i in output[1]: self.assertTrue(i in [4, 5, 6]) input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) self.assertEqual(output.shape, (6,), "Dimensions not properly parsed") for i in output: self.assertTrue(i in [1, 2, 3, 4, 5, 6]) input_data_string = '[ [ [1,1], [2, 2] ], [ [3 ,3], [4,4] ] ]' output = string2array(input_data_string, ',', row['elementType']) self.assertEqual(output.shape, (2, 2, 2), "Wrong dimensions.") for i in output[0][0]: self.assertTrue(i == 1) for i in output[0][1]: self.assertTrue(i == 2) for i in output[1][0]: self.assertTrue(i == 3) for i in output[1][1]: self.assertTrue(i == 4) row = {'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'quantifier': 'manual', 'elementType': 'str', 'type': 'array', 'options': None, 'name': 'test'} input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) for i in output: self.assertTrue(i in [1, 2, 3, 4, 5, 6])
def test_array_from_string(self): """ Simple test for parse array on 1d, 2d and 3d array. """ row = {'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'elementType': 'float', 'type': 'array', 'options': None, 'name': 'test'} input_data_string = '[ [1 2 3] [4 5 6]]' output = string2array(input_data_string, ' ', row['elementType']) assert output.shape, (2, 3) == "Dimensions not properly parsed" for i in output[0]: assert i in [1, 2, 3] for i in output[1]: assert i in [4, 5, 6] input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) assert output.shape == (6,), "Dimensions not properly parsed" for i in output: assert i in [1, 2, 3, 4, 5, 6] input_data_string = '[ [ [1,1], [2, 2] ], [ [3 ,3], [4,4] ] ]' output = string2array(input_data_string, ',', row['elementType']) assert output.shape == (2, 2, 2), "Wrong dimensions." for i in output[0][0]: assert i == 1 for i in output[0][1]: assert i == 2 for i in output[1][0]: assert i == 3 for i in output[1][1]: assert i == 4 row = {'description': 'test.', 'default': 'None', 'required': True, 'label': 'test: ', 'attributes': None, 'elementType': 'str', 'type': 'array', 'options': None, 'name': 'test'} input_data_string = '[1, 2, 3, 4, 5, 6]' output = string2array(input_data_string, ',', row['elementType']) for i in output: assert i in [1, 2, 3, 4, 5, 6]
def _convert_to_array(self, input_data, row): """ Method used when the type of an input is array, to parse or read. If the user set an equation for computing a model parameter then the value of that parameter will be a dictionary which contains all the data needed for computing that parameter for each vertex from the used surface. """ if KEY_EQUATION in str(input_data) and KEY_FOCAL_POINTS in str( input_data) and KEY_SURFACE_GID in str(input_data): try: input_data = eval(str(input_data)) # TODO move at a different level equation_type = input_data.get(KEY_DTYPE) if equation_type is None: self.log.warning( "Cannot figure out type of equation from input dictionary: %s. " "Returning []." % input_data) return [] eq_class = get_class_by_name(equation_type) equation = eq_class.from_json(input_data[KEY_EQUATION]) focal_points = json.loads(input_data[KEY_FOCAL_POINTS]) surface_gid = input_data[KEY_SURFACE_GID] surface = load_entity_by_gid(surface_gid) return surface.compute_equation(focal_points, equation) except Exception: self.log.exception( "The parameter %s was ignored. None value was returned.", row['name']) return None dtype = None if KEY_DTYPE in row: dtype = row[KEY_DTYPE] return string2array(str(input_data), ",", dtype)
def _convert_to_array(self, input_data, row): """ Method used when the type of an input is array, to parse or read. If the user set an equation for computing a model parameter then the value of that parameter will be a dictionary which contains all the data needed for computing that parameter for each vertex from the used surface. """ if KEY_EQUATION in str(input_data) and KEY_FOCAL_POINTS in str(input_data) and KEY_SURFACE_GID in str(input_data): try: input_data = eval(str(input_data)) # TODO move at a different level equation_type = input_data.get(KEY_DTYPE) if equation_type is None: self.log.warning("Cannot figure out type of equation from input dictionary: %s. " "Returning []." % input_data) return [] eq_class = get_class_by_name(equation_type) equation = eq_class.from_json(input_data[KEY_EQUATION]) focal_points = json.loads(input_data[KEY_FOCAL_POINTS]) surface_gid = input_data[KEY_SURFACE_GID] surface = load_entity_by_gid(surface_gid) return surface.compute_equation(focal_points, equation) except Exception: self.log.exception("The parameter %s was ignored. None value was returned.", row['name']) return None dtype = None if KEY_DTYPE in row: dtype = row[KEY_DTYPE] return string2array(str(input_data), ",", dtype)
def _convert_to_array(self, input_data, row): """ Method used when the type of an input is array, to parse or read. If the user set an equation for computing a model parameter then the value of that parameter will be a dictionary which contains all the data needed for computing that parameter for each vertex from the used surface. """ if KEY_EQUATION in str(input_data) and KEY_FOCAL_POINTS in str(input_data) and KEY_SURFACE_GID in str(input_data): try: input_data = eval(str(input_data)) # TODO move at a different level equation_type = input_data.get(KEY_DTYPE, None) if equation_type is None: self.log.warning("Cannot figure out type of equation from input dictionary: %s. " "Returning []." % (str(input_data, ))) return [] splitted_class = equation_type.split('.') module = '.'.join(splitted_class[:-1]) classname = splitted_class[-1] eq_module = __import__(module, globals(), locals(), [classname]) eq_class = eval('eq_module.' + classname) equation = eq_class.from_json(input_data[KEY_EQUATION]) focal_points = json.loads(input_data[KEY_FOCAL_POINTS]) surface_gid = input_data[KEY_SURFACE_GID] surface = load_entity_by_gid(surface_gid) return surface.compute_equation(focal_points, equation) except Exception: self.log.exception("The parameter '" + str(row['name']) + "' was ignored. None value was returned.") return None if xml_reader.ATT_QUATIFIER in row: quantifier = row[xml_reader.ATT_QUATIFIER] dtype = None if KEY_DTYPE in row: dtype = row[KEY_DTYPE] if quantifier == xml_reader.QUANTIFIER_MANUAL: return string2array(str(input_data), ",", dtype) elif quantifier == xml_reader.QUANTIFIER_UPLOAD: input_str = open(input_data, 'r').read() return string2array(input_str, " ", dtype) elif quantifier == xml_reader.QUANTIFIER_FUNTION: return input_data return None
def test_string2array(self): """ Check the string2array method for various inputs """ test_string_arrays = ['[1,2,3]', '1,2,3', '1 2 3'] array_separators = [',', ',', ' '] for idx in xrange(len(test_string_arrays)): result_array = string2array(test_string_arrays[idx], array_separators[idx]) self.assertEqual(len(result_array), 3) self.assertEqual(result_array[0], 1) self.assertEqual(result_array[1], 2) self.assertEqual(result_array[2], 3)
def test_string2array(self): """ Check the string2array method for various inputs """ test_string_arrays = ['[1,2,3]', '1,2,3', '1 2 3'] array_separators = [',', ',', ' '] for idx in range(len(test_string_arrays)): result_array = string2array(test_string_arrays[idx], array_separators[idx]) assert len(result_array) == 3 assert result_array[0] == 1 assert result_array[1] == 2 assert result_array[2] == 3
def test_wrong_array_from_string(self): """Test that parsing an array from string is throwing the expected exception when wrong input string""" row = { "description": "test.", "default": "None", "required": True, "label": "test: ", "attributes": None, "quantifier": "manual", "elementType": "float", "type": "array", "options": None, "name": "test", } input_data_string = "[ [1,2 3] [4,5,6]]" self.assertRaises(ValueError, string2array, input_data_string, ",", row["elementType"]) input_data_string = "[ [1,2,wrong], [4, 5, 6]]" self.assertRaises(ValueError, string2array, input_data_string, ",", row["elementType"]) row = { "description": "test.", "default": "None", "required": True, "label": "test: ", "attributes": None, "quantifier": "manual", "elementType": "str", "type": "array", "options": None, "name": "test", } output = string2array(input_data_string, ",", row["elementType"]) self.assertEqual(output.shape, (2, 3)) self.assertEqual(output[0][2], "wrong", "String data not converted properly") input_data_string = "[ [1,2 3] [4,5,6]]" output = string2array(input_data_string, ",", row["elementType"]) self.assertEqual(output[0][1], "2 3")
def _get_dictionary(row, **kwargs): """ Find all key/value pairs for the dictionary represented by name. """ if InputTreeManager._is_parent_not_submitted(row, kwargs): return {}, [] name = row[xml_reader.ATT_NAME] result_dict = {} taken_keys = [] for key in kwargs: if name in key and name != key: taken_keys.append(key) if KEY_DTYPE in row: if row[KEY_DTYPE] == 'array': val = string2array(kwargs[key], " ", "float") else: val = eval(row[KEY_DTYPE] + "('" + kwargs[key] + "')") else: val = str(kwargs[key]) result_dict[key.split(KEYWORD_PARAMS[1:])[-1]] = val return result_dict, taken_keys
def _get_dictionary(row, **kwargs): """ Find all key/value pairs for the dictionary represented by name. """ if InputTreeManager._is_parent_not_submitted(row, kwargs): return {}, [] name = row[KEY_NAME] result_dict = {} taken_keys = [] for key in kwargs: if name in key and name != key: taken_keys.append(key) if KEY_DTYPE in row: if row[KEY_DTYPE] == 'array': val = string2array(kwargs[key], " ", "float") else: val = eval(row[KEY_DTYPE])(kwargs[key]) else: val = str(kwargs[key]) result_dict[key.split(KEYWORD_PARAMS[1:])[-1]] = val return result_dict, taken_keys