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
25    for arg in prj_file_tags.iterdescendants():
26        tag = arg.tag
27        for i in range(0, len(required_tags)):
28            if tag == required_tags[i]:
29                try:
30                    myself.__setattr__(tag, float(eval(arg.text)))
31                except (ValueError, NameError, SyntaxError):
32                    myself.__setattr__(tag, str(arg.text))
33        try:
34            required_tags.remove(tag)
35        except ValueError:
36            pass
37
38        for i in range(0, len(optional_tags)):
39            if tag == optional_tags[i]:
40                try:
41                    myself.__setattr__(tag, float(eval(arg.text)))
42                except (ValueError, NameError, SyntaxError):
43                    myself.__setattr__(tag, str(arg.text))
44
45    if len(required_tags) > 0:
46        string = ""
47        for tag in required_tags:
48            string += tag + " "
49        raise KeyError(
50            "Missing input parameters (in project file) for resource module initialisation: " + string)
51
52
53def setModelDomain(self, x1, x2, y1, y2):
54    """
55    Adds model domain boundaries to the object.
56    Args:
57        x1 (float): x-coordinate of left bottom border of grid
58        x2 (float): x-coordinate of right bottom border of grid
59        y1 (float): y-coordinate of left top border of grid
60        y2 (float): y-coordinate of right top border of grid
61    """
62    self.x_1, self.x_2 = x1, x2
63    self.y_1, self.y_2 = y1, y2
64
65    self.l_x = self.x_2 - self.x_1
66    self.l_y = self.y_2 - self.y_1
67
68
69def string_to_function(self, expression):
70    """
71    Evaluate formula from project file
72    Credits: https://saturncloud.io/blog/pythonnumpyscipy-converting-string-to-mathematical-function/#numpys-frompyfunc-function
73    Args:
74        expression (string): weighting formula (from prj file)
75    Returns:
76        array
77    """
78    def function(x, y):
79        return eval(expression)
80
81    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
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                    myself.__setattr__(tag, str(arg.text))
34        try:
35            required_tags.remove(tag)
36        except ValueError:
37            pass
38
39        for i in range(0, len(optional_tags)):
40            if tag == optional_tags[i]:
41                try:
42                    myself.__setattr__(tag, float(eval(arg.text)))
43                except (ValueError, NameError, SyntaxError):
44                    myself.__setattr__(tag, str(arg.text))
45
46    if len(required_tags) > 0:
47        string = ""
48        for tag in required_tags:
49            string += tag + " "
50        raise KeyError(
51            "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):
54def setModelDomain(self, x1, x2, y1, y2):
55    """
56    Adds model domain boundaries to the object.
57    Args:
58        x1 (float): x-coordinate of left bottom border of grid
59        x2 (float): x-coordinate of right bottom border of grid
60        y1 (float): y-coordinate of left top border of grid
61        y2 (float): y-coordinate of right top border of grid
62    """
63    self.x_1, self.x_2 = x1, x2
64    self.y_1, self.y_2 = y1, y2
65
66    self.l_x = self.x_2 - self.x_1
67    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):
70def string_to_function(self, expression):
71    """
72    Evaluate formula from project file
73    Credits: https://saturncloud.io/blog/pythonnumpyscipy-converting-string-to-mathematical-function/#numpys-frompyfunc-function
74    Args:
75        expression (string): weighting formula (from prj file)
76    Returns:
77        array
78    """
79    def function(x, y):
80        return eval(expression)
81
82    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