pyMANGA.ProjectLib.helpers
1import numpy as np 2 3 4def getInputParameters(myself, **tags): 5 """ 6 Read module tags from project file. 7 Args: 8 tags (dict): dictionary containing tags found in the project file as well as required and optional tags of 9 the module under consideration. 10 """ 11 try: 12 prj_file_tags = tags["prj_file"] 13 except KeyError: 14 prj_file_tags = [] 15 print("WARNING: Module attributes are missing.") 16 try: 17 required_tags = tags["required"] 18 except KeyError: 19 required_tags = [] 20 try: 21 optional_tags = tags["optional"] 22 except KeyError: 23 optional_tags = [] 24 case_insensitive = tags.get("case_insensitive", []) 25 26 for arg in prj_file_tags.iterdescendants(): 27 tag = arg.tag 28 for i in range(0, len(required_tags)): 29 if tag == required_tags[i]: 30 try: 31 myself.__setattr__(tag, float(eval(arg.text))) 32 except (ValueError, NameError, SyntaxError): 33 value = str(arg.text) 34 if tag in case_insensitive: 35 value = value.strip().lower() 36 myself.__setattr__(tag, value) 37 try: 38 required_tags.remove(tag) 39 except ValueError: 40 pass 41 42 for i in range(0, len(optional_tags)): 43 if tag == optional_tags[i]: 44 try: 45 myself.__setattr__(tag, float(eval(arg.text))) 46 except (ValueError, NameError, SyntaxError): 47 value = str(arg.text) 48 if tag in case_insensitive: 49 value = value.strip().lower() 50 myself.__setattr__(tag, value) 51 52 if len(required_tags) > 0: 53 string = "" 54 for tag in required_tags: 55 string += tag + " " 56 raise KeyError( 57 "Missing input parameters (in project file) for resource module initialisation: " + string) 58 59 60def setModelDomain(self, x1, x2, y1, y2): 61 """ 62 Adds model domain boundaries to the object. 63 Args: 64 x1 (float): x-coordinate of left bottom border of grid 65 x2 (float): x-coordinate of right bottom border of grid 66 y1 (float): y-coordinate of left top border of grid 67 y2 (float): y-coordinate of right top border of grid 68 """ 69 self.x_1, self.x_2 = x1, x2 70 self.y_1, self.y_2 = y1, y2 71 72 self.l_x = self.x_2 - self.x_1 73 self.l_y = self.y_2 - self.y_1 74 75 76def string_to_function(self, expression): 77 """ 78 Evaluate formula from project file 79 Credits: https://saturncloud.io/blog/pythonnumpyscipy-converting-string-to-mathematical-function/#numpys-frompyfunc-function 80 Args: 81 expression (string): weighting formula (from prj file) 82 Returns: 83 array 84 """ 85 def function(x, y): 86 return eval(expression) 87 88 return np.frompyfunc(function, 2, 1)
def
getInputParameters(myself, **tags):
5def getInputParameters(myself, **tags): 6 """ 7 Read module tags from project file. 8 Args: 9 tags (dict): dictionary containing tags found in the project file as well as required and optional tags of 10 the module under consideration. 11 """ 12 try: 13 prj_file_tags = tags["prj_file"] 14 except KeyError: 15 prj_file_tags = [] 16 print("WARNING: Module attributes are missing.") 17 try: 18 required_tags = tags["required"] 19 except KeyError: 20 required_tags = [] 21 try: 22 optional_tags = tags["optional"] 23 except KeyError: 24 optional_tags = [] 25 case_insensitive = tags.get("case_insensitive", []) 26 27 for arg in prj_file_tags.iterdescendants(): 28 tag = arg.tag 29 for i in range(0, len(required_tags)): 30 if tag == required_tags[i]: 31 try: 32 myself.__setattr__(tag, float(eval(arg.text))) 33 except (ValueError, NameError, SyntaxError): 34 value = str(arg.text) 35 if tag in case_insensitive: 36 value = value.strip().lower() 37 myself.__setattr__(tag, value) 38 try: 39 required_tags.remove(tag) 40 except ValueError: 41 pass 42 43 for i in range(0, len(optional_tags)): 44 if tag == optional_tags[i]: 45 try: 46 myself.__setattr__(tag, float(eval(arg.text))) 47 except (ValueError, NameError, SyntaxError): 48 value = str(arg.text) 49 if tag in case_insensitive: 50 value = value.strip().lower() 51 myself.__setattr__(tag, value) 52 53 if len(required_tags) > 0: 54 string = "" 55 for tag in required_tags: 56 string += tag + " " 57 raise KeyError( 58 "Missing input parameters (in project file) for resource module initialisation: " + string)
Read module tags from project file.
Arguments:
- tags (dict): dictionary containing tags found in the project file as well as required and optional tags of
- the module under consideration.
def
setModelDomain(self, x1, x2, y1, y2):
61def setModelDomain(self, x1, x2, y1, y2): 62 """ 63 Adds model domain boundaries to the object. 64 Args: 65 x1 (float): x-coordinate of left bottom border of grid 66 x2 (float): x-coordinate of right bottom border of grid 67 y1 (float): y-coordinate of left top border of grid 68 y2 (float): y-coordinate of right top border of grid 69 """ 70 self.x_1, self.x_2 = x1, x2 71 self.y_1, self.y_2 = y1, y2 72 73 self.l_x = self.x_2 - self.x_1 74 self.l_y = self.y_2 - self.y_1
Adds model domain boundaries to the object.
Arguments:
- x1 (float): x-coordinate of left bottom border of grid
- x2 (float): x-coordinate of right bottom border of grid
- y1 (float): y-coordinate of left top border of grid
- y2 (float): y-coordinate of right top border of grid
def
string_to_function(self, expression):
77def string_to_function(self, expression): 78 """ 79 Evaluate formula from project file 80 Credits: https://saturncloud.io/blog/pythonnumpyscipy-converting-string-to-mathematical-function/#numpys-frompyfunc-function 81 Args: 82 expression (string): weighting formula (from prj file) 83 Returns: 84 array 85 """ 86 def function(x, y): 87 return eval(expression) 88 89 return np.frompyfunc(function, 2, 1)
Evaluate formula from project file Credits: https://saturncloud.io/blog/pythonnumpyscipy-converting-string-to-mathematical-function/#numpys-frompyfunc-function
Arguments:
- expression (string): weighting formula (from prj file)
Returns:
array