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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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
from collections import deque
from pathlib import Path
import pickle

import gradio as gr
import inspect
import io
from jinja2 import Template
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import numpy as np
import numexpr
import pandas as pd
from PIL import Image
import plotly.graph_objects as go
import sklearn
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.datasets import load_iris
from sklearn.metrics import classification_report, mean_squared_error, mean_absolute_error
from sklearn.datasets import make_regression
from sklearn.linear_model import ElasticNet
import ast

import traceback
import yaml

from sklearn.preprocessing import StandardScaler, MinMaxScaler, MaxAbsScaler, Normalizer
from sklearn.gaussian_process import GaussianProcessRegressor, GaussianProcessClassifier
from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel, ConstantKernel, RBF, Matern, RationalQuadratic, ExpSineSquared

import logging
logging.basicConfig(
    level=logging.INFO,  # set minimum level to capture (DEBUG, INFO, WARNING, ERROR, CRITICAL)
    format="%(asctime)s [%(levelname)s] %(message)s",  # log format
)
logger = logging.getLogger("ELVIS")


NUMEXPR_CONSTANTS = {
    'pi': np.pi,
    'PI': np.pi,
    'e': np.e,
}


def eval_kernel(kernel_str):
    # List of allowed kernel constructors
    allowed_names = {
        'RBF': RBF,
        'Matern': Matern,
        'RationalQuadratic': RationalQuadratic,
        'ExpSineSquared': ExpSineSquared,
        'DotProduct': DotProduct,
        'WhiteKernel': WhiteKernel,
        'ConstantKernel': ConstantKernel,
    }

    # Parse and check the syntax safely
    try:
        tree = ast.parse(kernel_str, mode='eval')
    except SyntaxError as e:
        raise ValueError(f"Invalid syntax: {e}")

    # Evaluate in restricted namespace
    try:
        result = eval(compile(tree, '<string>', 'eval'),
                      {"__builtins__": None}, # disable access to Python builtins like open
                      allowed_names # only allow things in this list
                     )
    except Exception as e:
        raise ValueError(f"Error evaluating kernel: {e}")

    return result


def get_function(function, xlim=(-1, 1), nsample=100):
    x = np.linspace(xlim[0], xlim[1], nsample)
    y = numexpr.evaluate(function, local_dict={'x': x, **NUMEXPR_CONSTANTS})
    x = x.reshape(-1, 1)
    return x, y


def get_data_points(function, xlim=(-1, 1), nsample=10, sigma=0, seed=0):
    num_points_to_generate = 100
    if nsample > num_points_to_generate:
        raise ValueError(f"nsample too large, limit to {num_points_to_generate}")

    rng = np.random.default_rng(seed)
    x = rng.uniform(xlim[0], xlim[1], size=num_points_to_generate)
    x = x[:nsample]
    x = np.sort(x)

    rng = np.random.default_rng(seed)
    noise = sigma * rng.standard_normal(nsample)
    y = numexpr.evaluate(function, local_dict={'x': x, **NUMEXPR_CONSTANTS}) + noise

    x = x.reshape(-1, 1)
    return x, y


def make_sine(xlim=(0,1), nsample=20, sigma=0.1, uniform=False, sort=True):
    np.random.seed(42)
    if uniform:
        X = np.linspace(xlim[0], xlim[1], nsample)
    else:
        X = xlim[0] + (xlim[1]-xlim[0])*np.random.rand(nsample)

    if sort:
        X = np.sort(X)

    y = np.sin(2*np.pi*X) + sigma*np.random.randn(nsample)
    X = X.reshape(-1, 1)

    return X, y


class GPVisualizer:
    DEFAULT_KERNEL = "RBF() + WhiteKernel()"
    DEFAULT_FUNCTION = "sin(2 * pi * x)"

    def _init_state(self):
        self.data_options = {
            "function": self.DEFAULT_FUNCTION,
            "nsample": 30,
            "sigma": 0,
            "seed": 0,
            "x_min": -1,
            "x_max": 1,
        }

        self.kernel = eval_kernel(self.DEFAULT_KERNEL)

        self.x_train, self.y_train = self.generate_data()
        self.model = self.train_model(self.kernel, self.x_train, self.y_train)

        self.plot_options = {
            "show_training_data": True,
            "show_confidence_interval": True,
            "show_true_function": True,
            "show_predictions": True,
        }

        self.num_y_samples = 0
        self._y_samples_cache = []

    def __init__(self, width, height):
        self.canvas_width = width
        self.canvas_height = height

        self._init_state()

        self.plot_cmap = plt.get_cmap("tab20")

        self.css = """
.hidden-button {
    display: none;
}"""

    def on_load(self):
        self._init_state()

    def generate_data(self):
        function = self.data_options["function"]
        nsample = self.data_options["nsample"]
        sigma = self.data_options["sigma"]
        x_min = self.data_options["x_min"]
        x_max = self.data_options["x_max"]

        return get_data_points(function, xlim=(x_min, x_max), nsample=nsample, sigma=sigma, seed=self.data_options["seed"])

    def train_model(self, kernel, x_train, y_train):
        gpr = GaussianProcessRegressor(kernel=kernel, random_state=0)
        logger.info('fitting ' + str(gpr))
        if len(x_train) > 0:
            gpr.fit(x_train, y_train)
        return gpr

    def plot(self):
        '''
        '''

        logger.info("Initializing figure")
        fig = plt.figure(figsize=(self.canvas_width/100., self.canvas_height/100.0), dpi=100)
        # set entire figure to be the canvas to allow simple conversion of mouse
        # position to coordinates in the figure
        ax = fig.add_axes([0., 0., 1., 1.]) # 
        ax.margins(x=0, y=0) # no padding in both directions

        x_test, y_test = get_function(self.data_options["function"], xlim=(-2, 2), nsample=100)
        y_pred, y_std = self.model.predict(x_test, return_std=True)

        # plot
        fig, ax = plt.subplots(figsize=(8, 8))
        ax.set_title("")
        ax.set_xlabel("x")
        ax.set_ylabel("y")

        if len(self.x_train) > 1:
            R2 = self.model.score(self.x_train, self.y_train)

        if self.plot_options["show_training_data"]:
            if len(self.x_train) > 1:
                plt.scatter(self.x_train.flatten(), self.y_train, label='training data (R2=%.2f)' % (R2), color=self.plot_cmap(0))
            else:
                plt.scatter(self.x_train.flatten(), self.y_train, label='training data', color=self.plot_cmap(0))

        if self.plot_options["show_true_function"]:
            plt.plot(x_test.flatten(), y_test, label='true function', color=self.plot_cmap(1))

        if self.plot_options["show_predictions"]:
            plt.plot(x_test.flatten(), y_pred, linestyle="--", label='mean prediction', color=self.plot_cmap(2))

        if self.plot_options["show_confidence_interval"]:
            plt.fill_between(
                x_test.flatten(), 
                y_pred - 1.96 * y_std, 
                y_pred + 1.96 * y_std, 
                alpha=0.2,
                label='95% confidence interval', 
                color=self.plot_cmap(3)
            )

        for i in range(self.num_y_samples):
            if i < len(self._y_samples_cache):
                y_sample = self._y_samples_cache[i]
            else:
                y_sample = self.model.sample_y(x_test, random_state=i).flatten()
                self._y_samples_cache.append(y_sample)
            plt.plot(x_test.flatten(), y_sample, linestyle=":", label=f"sample {i}", color=self.plot_cmap(4))


        plt.legend()

        buf = io.BytesIO()
        fig.savefig(buf, format="png", bbox_inches="tight", pad_inches=0)
        plt.close(fig)
        buf.seek(0)
        img = Image.open(buf)

        return img

    def _update_data_seed(self):
        self.data_options["seed"] += 1
        self.x_train, self.y_train = self.generate_data()
        self.update_model()
        return self.plot()

    def update_model(self):
        self.model = self.train_model(self.kernel, self.x_train, self.y_train)
        self.clear_y_samples()

    def update_data_options(self, **kwargs):
        for key, value in kwargs.items():
            if key in self.data_options:

                # if function - test if valid
                if key == "function":
                    try:
                        x = np.linspace(-1, 1, 10)
                        y = numexpr.evaluate(value, local_dict={'x': x, **NUMEXPR_CONSTANTS})
                    except Exception as e:
                        raise ValueError(f"Invalid function: {e}")

                self.data_options[key] = value

        # reset data and model
        self.x_train, self.y_train = self.generate_data()
        self.update_model()

        return self.plot()

    def update_kernel_spec(self, kernel_spec):
        self.kernel = eval_kernel(kernel_spec)
        self.update_model()
        return self.plot()

    def update_plot_options(self, **kwargs):
        for key, value in kwargs.items():
            if key in self.plot_options:
                self.plot_options[key] = value
        return self.plot()

    def add_y_sample(self):
        self.num_y_samples += 1
        return self.plot()

    def clear_y_samples(self):
        self.num_y_samples = 0
        self._y_samples_cache.clear()
        return self.plot()

    def launch(self):
        # build the Gradio interface
        with gr.Blocks(css=self.css) as demo:
            # app title
            gr.HTML("<div style='text-align:left; font-size:40px; font-weight: bold;'>Gaussian Process Visualizer</div>")

            # GUI elements and layout 
            with gr.Row():
                with gr.Column(scale=2):
                    self.canvas = gr.Image(value=self.plot(), 
                                           show_download_button=False,
                                           container=True)

                with gr.Column(scale=1): 
                    with gr.Tab("Dataset"):
                        dataset_radio = gr.Radio(
                            ["Generate", "Upload"], 
                            value="Generate", 
                            label="Dataset", 
                        )

                        with gr.Column():
                            function_box = gr.Textbox(
                                label="Function",
                                placeholder="function of x",
                                value=self.DEFAULT_FUNCTION,
                                interactive=True,
                            )
                            with gr.Row():
                                x_min = gr.Number(
                                    label="Min x",
                                    value=-1,
                                    interactive=True,
                                )
                                x_max = gr.Number(
                                    label="Max x",
                                    value=1,
                                    interactive=True,
                                )
                            with gr.Row():
                                noise_value = gr.Number(
                                    label="Gaussian noise standard deviation",
                                    value=0,
                                    interactive=True,
                                )
                                num_points_slider = gr.Slider(
                                    label="Number of data points",
                                    minimum=0,
                                    maximum=100,
                                    step=1,
                                    value=30,
                                    interactive=True,
                                )

                            regenerate_button = gr.Button("Regenerate Data")

                        # upload data
                        file_chooser = gr.File(label="Choose a file", visible=False, elem_id="rowheight")
                        self.file_chooser = file_chooser

                    with gr.Tab("Model"):
                        # kernel spec
                        kernel_spec = gr.Textbox(
                            label="Kernel",
                            placeholder="sklearn kernel code",
                            value=self.DEFAULT_KERNEL,
                            interactive=True,
                        )

                    with gr.Tab("Plot"):
                        # plot show options
                        with gr.Column():
                            with gr.Row():
                                show_training_data = gr.Checkbox(label="Show training data", value=True)
                                show_true_function = gr.Checkbox(label="Show true function", value=True)
                            with gr.Row():
                                show_predictions = gr.Checkbox(label="Show mean prediction", value=True)
                                show_confidence_interval = gr.Checkbox(label="Show confidence interval", value=True)

                            #gr.Markdown(''.join(open('kernel_examples.md', 'r').readlines()))

                        # sampling from GP
                        sample_button = gr.Button("Sample from GP")
                        clear_samples_button = gr.Button("Clear samples from GP")

                    with gr.Tab("Export"):
                        # use hidden download button to generate files on the fly
                        # https://github.com/gradio-app/gradio/issues/9230#issuecomment-2323771634

                        btn_export_data = gr.Button("Data")
                        btn_export_data_hidden = gr.DownloadButton(label="You should not see this", elem_id="btn_export_data_hidden", elem_classes="hidden-button")
                        
                        btn_export_model = gr.Button('Model')
                        btn_export_model_hidden = gr.DownloadButton(label="You should not see this", elem_id="btn_export_model_hidden", elem_classes="hidden-button")

                        btn_export_code = gr.Button('Code')
                        btn_export_code_hidden = gr.DownloadButton(label="You should not see this", elem_id="btn_export_code_hidden", elem_classes="hidden-button")

                    with gr.Tab("Usage"):
                        gr.Markdown(''.join(open('usage.md', 'r').readlines()))

                    # data options
                    function_box.submit(
                        fn=lambda function: self.update_data_options(function=function),
                        inputs=function_box,
                        outputs=[self.canvas],
                    )
                    x_min.submit(
                        fn=lambda xmin: self.update_data_options(x_min=xmin),
                        inputs=x_min,
                        outputs=[self.canvas],
                    )
                    x_max.submit(
                        fn=lambda xmax: self.update_data_options(x_max=xmax),
                        inputs=x_max,
                        outputs=[self.canvas],
                    )
                    num_points_slider.change(
                        fn=lambda nsample: self.update_data_options(nsample=nsample),
                        inputs=num_points_slider,
                        outputs=[self.canvas],
                    )
                    noise_value.submit(
                        fn=lambda sigma: self.update_data_options(sigma=sigma),
                        inputs=noise_value,
                        outputs=[self.canvas],
                    )
                    regenerate_button.click(
                        fn=self._update_data_seed,
                        outputs=[self.canvas],
                    )
            
                    # model options
                    kernel_spec.submit(
                        fn=self.update_kernel_spec,
                        inputs=kernel_spec,
                        outputs=[self.canvas],
                    )

                    # plot options
                    show_training_data.change(
                        fn=lambda show: self.update_plot_options(show_training_data=show),
                        inputs=show_training_data,
                        outputs=[self.canvas],
                    )
                    show_confidence_interval.change(
                        fn=lambda show: self.update_plot_options(show_confidence_interval=show),
                        inputs=show_confidence_interval,
                        outputs=[self.canvas],
                    )
                    show_true_function.change(
                        fn=lambda show: self.update_plot_options(show_true_function=show),
                        inputs=show_true_function,
                        outputs=[self.canvas],
                    )
                    show_predictions.change(
                        fn=lambda show: self.update_plot_options(show_predictions=show),
                        inputs=show_predictions,
                        outputs=[self.canvas],
                    )

                    # sampling from GP
                    sample_button.click(
                        fn=self.add_y_sample,
                        outputs=[self.canvas],
                    )
                    clear_samples_button.click(
                        fn=self.clear_y_samples,
                        outputs=[self.canvas],
                    )

            demo.load(self.on_load)

        demo.launch()

visualizer = GPVisualizer(width=1200, height=900)
visualizer.launch()