File size: 8,476 Bytes
a206d6a
 
985f8fa
a206d6a
 
 
c1a4fec
967d63f
c6a43c4
88a78a4
a206d6a
 
2139b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a206d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e487754
dd65136
 
a206d6a
 
 
 
 
 
 
dd65136
 
a206d6a
 
 
0dc382d
9d6017e
 
 
a206d6a
9d6017e
 
 
d3c4f72
9d6017e
 
 
 
 
 
0dc382d
 
 
 
9d6017e
 
 
 
a206d6a
9d6017e
 
 
 
 
 
a206d6a
9d6017e
8a2bd53
9d6017e
 
 
 
a206d6a
8a2bd53
 
 
a206d6a
 
 
 
 
8a2bd53
 
 
 
 
 
a206d6a
8a2bd53
 
 
 
 
 
a206d6a
8a2bd53
 
 
a206d6a
8a2bd53
 
 
 
a206d6a
8a2bd53
 
 
a206d6a
8a2bd53
 
 
 
a206d6a
8a2bd53
 
 
 
 
 
a206d6a
9d6017e
8a2bd53
9d6017e
a206d6a
8a2bd53
 
 
 
 
 
 
a206d6a
 
 
 
9d6017e
 
 
 
 
a206d6a
8a2bd53
 
 
a206d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28639ea
 
a206d6a
89fd807
 
 
a206d6a
 
 
 
 
 
 
 
89fd807
a206d6a
 
 
 
 
 
89fd807
a206d6a
 
 
 
 
 
46fdaa6
 
 
a206d6a
46fdaa6
a206d6a
46fdaa6
a206d6a
 
edbcfa6
fea9443
a206d6a
c1a4fec
a206d6a
 
967d63f
fea9443
 
28639ea
a206d6a
89fd807
 
 
 
 
 
28639ea
 
 
 
 
a206d6a
 
 
 
89fd807
 
 
 
a206d6a
89fd807
 
a206d6a
 
 
 
89fd807
a206d6a
 
89fd807
 
a206d6a
 
 
 
2608088
 
 
 
985f8fa
 
 
2608088
1
2
3
4
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from collections import OrderedDict

import gradio as gr
import numpy as np
from data import TEST_EQUATIONS
from gradio.components.base import Component
from plots import plot_example_data, plot_pareto_curve
from processing import processing


class ExampleData:
    def __init__(self, demo: gr.Blocks) -> None:
        with gr.Column():
            self.example_plot = gr.Plot()
        with gr.Column():
            self.test_equation = gr.Radio(
                TEST_EQUATIONS, value=TEST_EQUATIONS[0], label="Test Equation"
            )
            self.num_points = gr.Slider(
                minimum=10,
                maximum=1000,
                value=200,
                label="Number of Data Points",
                step=1,
            )
            self.noise_level = gr.Slider(
                minimum=0, maximum=1, value=0.05, label="Noise Level"
            )
            self.data_seed = gr.Number(value=0, label="Random Seed")

        # Set up plotting:

        eqn_components = [
            self.test_equation,
            self.num_points,
            self.noise_level,
            self.data_seed,
        ]
        for eqn_component in eqn_components:
            eqn_component.change(
                plot_example_data,
                eqn_components,
                self.example_plot,
                show_progress=False,
            )

        demo.load(plot_example_data, eqn_components, self.example_plot)


class UploadData:
    def __init__(self) -> None:
        self.file_input = gr.File(label="Upload a CSV File")
        self.label = gr.Markdown(
            "The rightmost column of your CSV file will be used as the target variable."
        )


class Data:
    def __init__(self, demo: gr.Blocks) -> None:
        with gr.Tab("Example Data"):
            self.example_data = ExampleData(demo)
        with gr.Tab("Upload Data"):
            self.upload_data = UploadData()


class BasicSettings:
    def __init__(self) -> None:
        self.binary_operators = gr.CheckboxGroup(
            choices=["+", "-", "*", "/", "^", "max", "min", "mod", "cond"],
            label="Binary Operators",
            value=["+", "-", "*", "/"],
        )
        self.unary_operators = gr.CheckboxGroup(
            choices=[
                "sin",
                "cos",
                "tan",
                "exp",
                "log",
                "square",
                "cube",
                "sqrt",
                "abs",
                "erf",
                "relu",
                "round",
                "sign",
            ],
            label="Unary Operators",
            value=["sin"],
        )
        self.niterations = gr.Slider(
            minimum=1,
            maximum=1000,
            value=40,
            label="Number of Iterations",
            step=1,
        )
        self.maxsize = gr.Slider(
            minimum=7,
            maximum=100,
            value=20,
            label="Maximum Complexity",
            step=1,
        )
        self.parsimony = gr.Number(
            value=0.0032,
            label="Parsimony Coefficient",
        )


class AdvancedSettings:
    def __init__(self) -> None:
        self.populations = gr.Slider(
            minimum=2,
            maximum=100,
            value=15,
            label="Number of Populations",
            step=1,
        )
        self.population_size = gr.Slider(
            minimum=2,
            maximum=1000,
            value=33,
            label="Population Size",
            step=1,
        )
        self.ncycles_per_iteration = gr.Number(
            value=550,
            label="Cycles per Iteration",
        )
        self.elementwise_loss = gr.Radio(
            ["L2DistLoss()", "L1DistLoss()", "LogitDistLoss()", "HuberLoss()"],
            value="L2DistLoss()",
            label="Loss Function",
        )
        self.adaptive_parsimony_scaling = gr.Number(
            value=20.0,
            label="Adaptive Parsimony Scaling",
        )
        self.optimizer_algorithm = gr.Radio(
            ["BFGS", "NelderMead"],
            value="BFGS",
            label="Optimizer Algorithm",
        )
        self.optimizer_iterations = gr.Slider(
            minimum=1,
            maximum=100,
            value=8,
            label="Optimizer Iterations",
            step=1,
        )
        self.batching = gr.Checkbox(
            value=False,
            label="Batching",
        )
        self.batch_size = gr.Slider(
            minimum=2,
            maximum=1000,
            value=50,
            label="Batch Size",
            step=1,
        )


class GradioSettings:
    def __init__(self) -> None:
        self.plot_update_delay = gr.Slider(
            minimum=1,
            maximum=100,
            value=3,
            label="Plot Update Delay",
        )
        self.force_run = gr.Checkbox(
            value=False,
            label="Ignore Warnings",
        )


class Settings:
    def __init__(self):
        with gr.Tab("Basic Settings"):
            self.basic_settings = BasicSettings()
        with gr.Tab("Advanced Settings"):
            self.advanced_settings = AdvancedSettings()
        with gr.Tab("Gradio Settings"):
            self.gradio_settings = GradioSettings()


class Results:
    def __init__(self):
        with gr.Tab("Pareto Front"):
            self.pareto = gr.Plot()
        with gr.Tab("Predictions"):
            self.predictions_plot = gr.Plot()

        self.df = gr.Dataframe(
            headers=["complexity", "loss", "equation"],
            datatype=["number", "number", "str"],
            wrap=True,
            column_widths=[75, 75, 200],
            interactive=False,
        )

        self.messages = gr.Textbox(label="Messages", value="", interactive=False)


def flatten_attributes(
    component_group, absolute_name: str, d: OrderedDict
) -> OrderedDict:
    if not hasattr(component_group, "__dict__"):
        return d

    for name, elem in component_group.__dict__.items():
        new_absolute_name = absolute_name + "." + name
        if name.startswith("_"):
            # Private attribute
            continue
        elif elem in d.values():
            # Don't duplicate any tiems
            continue
        elif isinstance(elem, Component):
            # Only add components to dict
            d[new_absolute_name] = elem
        else:
            flatten_attributes(elem, new_absolute_name, d)

    return d


class AppInterface:
    def __init__(self, demo: gr.Blocks) -> None:
        with gr.Row():
            with gr.Column():
                with gr.Row():
                    self.data = Data(demo)
                with gr.Row():
                    self.settings = Settings()
            with gr.Column():
                self.results = Results()
                self.run = gr.Button()

        # Update plot when dataframe is updated:
        self.results.df.change(
            plot_pareto_curve,
            inputs=[self.results.df, self.settings.basic_settings.maxsize],
            outputs=[self.results.pareto],
            show_progress=False,
        )

        ignore = ["df", "predictions_plot", "pareto", "messages"]
        self.run.click(
            create_processing_function(self, ignore=ignore),
            inputs=[
                v
                for k, v in flatten_attributes(self, "interface", OrderedDict()).items()
                if last_part(k) not in ignore
            ],
            outputs=[
                self.results.df,
                self.results.predictions_plot,
                self.results.messages,
            ],
            show_progress=True,
        )


def last_part(k: str) -> str:
    return k.split(".")[-1]


def create_processing_function(interface: AppInterface, ignore=[]):
    d = flatten_attributes(interface, "interface", OrderedDict())
    keys = [k for k in map(last_part, d.keys()) if k not in ignore]
    _, idx, counts = np.unique(keys, return_index=True, return_counts=True)
    if np.any(counts > 1):
        raise AssertionError("Bad keys: " + ",".join(np.array(keys)[idx[counts > 1]]))

    def f(*components):
        n = len(components)
        assert n == len(keys)
        for output in processing(**{keys[i]: components[i] for i in range(n)}):
            yield output

    return f


def main():
    with gr.Blocks(theme="default") as demo:
        _ = AppInterface(demo)
        demo.launch(debug=True)


if __name__ == "__main__":
    main()