Skip to content

Manager

Manager

Source code in phaseportrait/utils/manager.py
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class Manager:
    def __init__(self, portrait):
        self.portrait = portrait

    def plot_update(self, configuration):
        """Updates the plot with the new configuration configuration

        Args:
            configuration (dict): For more information check api examples

        Returns:
            (int): 0 if successfull. 1 otherwise.
        """
        dimension = configuration.get('dimension')

        # Parameters
        kargs = ['MeshDim', 'dF_args', 'Density', 'Polar', 'Title', 'xScale',  'yScale', 'xlabel', 'ylabel', 'color'] + \
            (['zScale', 'zlabel'] if dimension==3 else [])

        for k,v in configuration.items():
            if k in kargs:
                setattr(self.portrait, k, v)

        range = configuration.get('Range')
        try:
            if range:
                if dimension == 2:
                    self.portrait.Range = [[range['x_min'],range['x_max']],
                                        [range['y_min'],range['y_max']]]
                if dimension == 3:
                    self.portrait.Range = [[range['x_min'],range['x_max']],
                                        [range['y_min'],range['y_max']],
                                        [range['z_min'], range['z_max']]]
        except TypeError:
            return 1

        # Nullclines not implemented in 3d plot
        if (nc:=configuration.get('nullcline')) and dimension != 3:
            self.portrait.add_nullclines(**nc)
        else:
            self.portrait.nullclines = []

        if sls:=configuration.get('sliders'):
            for sl_name in sls:
                # If a slider is already created with same name ends are updated and value is changed
                if slider:=self.portrait.sliders.get(sl_name):
                        slider.update_slider_ends(sls[sl_name]["min"], sls[sl_name]["max"])
                        slider.value = configuration["dF_args"][sl_name]
                        slider.slider.set_val(configuration["dF_args"][sl_name])

                else:
                    self.portrait.add_slider(sl_name, 
                        valinit=configuration["dF_args"][sl_name],
                        valinterval=[sls[sl_name]["min"], sls[sl_name]["max"]],
                        valstep= (sls[sl_name]["max"]-sls[sl_name]["min"])/50
                        )


        if new_dF := configuration.get('dF'):
            self.portrait.dF_args = {}
            try:
                match = re.search(r"def\s+(\w+)\(", new_dF)
                function_name = match.group(1)
                # match = re.search(r"(\s+)def", new_dF)
                # if match is not None:
                #     new_dF = new_dF.replace(match.group(0), "\ndef")
                # del match

                exec(new_dF, globals())
                self.portrait.dF = globals()[function_name]
            except  Exception as e:
                return 1

        # Clean axis and replot
        try:
            self.portrait.ax.cla()
        except Exception as e:
            for ax in self.portrait.ax.values():
                ax.cla()

        if 'Cobweb' in self.portrait._name_:
            self.portrait.update_dF_args()


        colorbar_check = configuration.get("Colorbar", False)
        self.portrait.colorbar(toggle=colorbar_check)

        self.portrait.plot()

        self.portrait.fig.tight_layout()
        # self.portrait.fig.subplots_adjust(left=0.2, bottom=0.2, right=1, top=0.9, wspace=0.01, hspace=0.01)
        return 0


    def equivalent_code(self, configuration):
        """Returns equivalent code of given configuration

        Args:
            configuration (dict): For more information check api examples

        Returns:
            (str): equivalent code to the given configuration
        """

        match = re.search(r"def\s+(\w+)\(", configuration['dF'])
        function_name = match.group(1)
        dimension = configuration['dimension']

        header = f"""from phaseportrait import *\nimport matplotlib.pyplot as plt"""

        function = f"""\n{configuration['dF']}"""

        portrait = f"""\nphase_diagram = PhasePortrait{dimension}D({function_name}, [[{configuration['Range']['x_min']},{configuration['Range']['x_max']}],[{configuration['Range']['y_min']},{configuration['Range']['y_max']}]]"""
        if dimension == 3:
            portrait = portrait[:-1] + f""",[{configuration['Range']['z_min']},{configuration['Range']['z_max']}]]"""

        portrait_kargs = ""
        # kargs = ['MeshDim', 'dF_args', 'Density', 'Polar', 'Title', 'xlabel', 'ylabel', 'color']
        kargs = ['MeshDim', 'dF_args', 'Density', 'Polar', 'Title', 'xScale',  'yScale', 'xlabel', 'ylabel', 'color'] + \
            (['zScale', 'zlabel'] if dimension==3 else [])
        first = True
        for k in configuration.keys():
            if k in kargs:
                if configuration[k]:
                    if first:
                        portrait += ","
                        first = False
                    if isinstance(configuration[k], str):
                        portrait_kargs += f"\t{k} = '{configuration[k]}',\n"
                    else:
                        portrait_kargs += f"\t{k} = {configuration[k]},\n"

        if first:
            portrait += ')\n'
        else:
            portrait_kargs += ')\n'


        nullcline = ''
        if (nc := configuration.get('nullcline')) and dimension != 3:
            nullcline = f"\nphase_diagram.add_nullclines(precision={nc['precision']}, offset={nc['offset']})"

        finisher = '\nphase_diagram.plot()\nplt.show()'

        return header +"\n\t\n"+ function +"\n\t\n"+ portrait +"\n"+ portrait_kargs +"\n\t\n"+ nullcline +"\n\t\n"+ finisher


    def handle_json(self, message):
        if message["phaseportrait_request"] == '--plot':
            return self.plot_update(message)

        if message["phaseportrait_request"] == '--code':
            return self.equivalent_code(message)

equivalent_code(configuration)

Returns equivalent code of given configuration

Parameters:

Name Type Description Default
configuration dict

For more information check api examples

required

Returns:

Type Description
str

equivalent code to the given configuration

Source code in phaseportrait/utils/manager.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def equivalent_code(self, configuration):
    """Returns equivalent code of given configuration

    Args:
        configuration (dict): For more information check api examples

    Returns:
        (str): equivalent code to the given configuration
    """

    match = re.search(r"def\s+(\w+)\(", configuration['dF'])
    function_name = match.group(1)
    dimension = configuration['dimension']

    header = f"""from phaseportrait import *\nimport matplotlib.pyplot as plt"""

    function = f"""\n{configuration['dF']}"""

    portrait = f"""\nphase_diagram = PhasePortrait{dimension}D({function_name}, [[{configuration['Range']['x_min']},{configuration['Range']['x_max']}],[{configuration['Range']['y_min']},{configuration['Range']['y_max']}]]"""
    if dimension == 3:
        portrait = portrait[:-1] + f""",[{configuration['Range']['z_min']},{configuration['Range']['z_max']}]]"""

    portrait_kargs = ""
    # kargs = ['MeshDim', 'dF_args', 'Density', 'Polar', 'Title', 'xlabel', 'ylabel', 'color']
    kargs = ['MeshDim', 'dF_args', 'Density', 'Polar', 'Title', 'xScale',  'yScale', 'xlabel', 'ylabel', 'color'] + \
        (['zScale', 'zlabel'] if dimension==3 else [])
    first = True
    for k in configuration.keys():
        if k in kargs:
            if configuration[k]:
                if first:
                    portrait += ","
                    first = False
                if isinstance(configuration[k], str):
                    portrait_kargs += f"\t{k} = '{configuration[k]}',\n"
                else:
                    portrait_kargs += f"\t{k} = {configuration[k]},\n"

    if first:
        portrait += ')\n'
    else:
        portrait_kargs += ')\n'


    nullcline = ''
    if (nc := configuration.get('nullcline')) and dimension != 3:
        nullcline = f"\nphase_diagram.add_nullclines(precision={nc['precision']}, offset={nc['offset']})"

    finisher = '\nphase_diagram.plot()\nplt.show()'

    return header +"\n\t\n"+ function +"\n\t\n"+ portrait +"\n"+ portrait_kargs +"\n\t\n"+ nullcline +"\n\t\n"+ finisher

plot_update(configuration)

Updates the plot with the new configuration configuration

Parameters:

Name Type Description Default
configuration dict

For more information check api examples

required

Returns:

Type Description
int

0 if successfull. 1 otherwise.

Source code in phaseportrait/utils/manager.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def plot_update(self, configuration):
    """Updates the plot with the new configuration configuration

    Args:
        configuration (dict): For more information check api examples

    Returns:
        (int): 0 if successfull. 1 otherwise.
    """
    dimension = configuration.get('dimension')

    # Parameters
    kargs = ['MeshDim', 'dF_args', 'Density', 'Polar', 'Title', 'xScale',  'yScale', 'xlabel', 'ylabel', 'color'] + \
        (['zScale', 'zlabel'] if dimension==3 else [])

    for k,v in configuration.items():
        if k in kargs:
            setattr(self.portrait, k, v)

    range = configuration.get('Range')
    try:
        if range:
            if dimension == 2:
                self.portrait.Range = [[range['x_min'],range['x_max']],
                                    [range['y_min'],range['y_max']]]
            if dimension == 3:
                self.portrait.Range = [[range['x_min'],range['x_max']],
                                    [range['y_min'],range['y_max']],
                                    [range['z_min'], range['z_max']]]
    except TypeError:
        return 1

    # Nullclines not implemented in 3d plot
    if (nc:=configuration.get('nullcline')) and dimension != 3:
        self.portrait.add_nullclines(**nc)
    else:
        self.portrait.nullclines = []

    if sls:=configuration.get('sliders'):
        for sl_name in sls:
            # If a slider is already created with same name ends are updated and value is changed
            if slider:=self.portrait.sliders.get(sl_name):
                    slider.update_slider_ends(sls[sl_name]["min"], sls[sl_name]["max"])
                    slider.value = configuration["dF_args"][sl_name]
                    slider.slider.set_val(configuration["dF_args"][sl_name])

            else:
                self.portrait.add_slider(sl_name, 
                    valinit=configuration["dF_args"][sl_name],
                    valinterval=[sls[sl_name]["min"], sls[sl_name]["max"]],
                    valstep= (sls[sl_name]["max"]-sls[sl_name]["min"])/50
                    )


    if new_dF := configuration.get('dF'):
        self.portrait.dF_args = {}
        try:
            match = re.search(r"def\s+(\w+)\(", new_dF)
            function_name = match.group(1)
            # match = re.search(r"(\s+)def", new_dF)
            # if match is not None:
            #     new_dF = new_dF.replace(match.group(0), "\ndef")
            # del match

            exec(new_dF, globals())
            self.portrait.dF = globals()[function_name]
        except  Exception as e:
            return 1

    # Clean axis and replot
    try:
        self.portrait.ax.cla()
    except Exception as e:
        for ax in self.portrait.ax.values():
            ax.cla()

    if 'Cobweb' in self.portrait._name_:
        self.portrait.update_dF_args()


    colorbar_check = configuration.get("Colorbar", False)
    self.portrait.colorbar(toggle=colorbar_check)

    self.portrait.plot()

    self.portrait.fig.tight_layout()
    # self.portrait.fig.subplots_adjust(left=0.2, bottom=0.2, right=1, top=0.9, wspace=0.01, hspace=0.01)
    return 0