Spaces:
Sleeping
Sleeping
File size: 6,240 Bytes
09447d8 d808656 09447d8 48664e3 09447d8 48664e3 09447d8 48664e3 09447d8 48664e3 09447d8 48664e3 09447d8 48664e3 09447d8 48664e3 09447d8 48664e3 d808656 48664e3 09447d8 d808656 09447d8 d808656 09447d8 |
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 |
import gradio as gr
import numpy as np
import sympy as sp
import seaborn as sns
from matplotlib import pyplot as plt
sns.set_style(style="darkgrid")
sns.set_context(context="notebook", font_scale=0.7)
MAX_NOISE = 20
DEFAULT_NOISE = 6
SLIDE_NOISE_STEP = 2
MAX_POINTS = 100
DEFAULT_POINTS = 20
SLIDE_POINTS_STEP = 5
def generate_equation(process_params):
process_params = process_params.astype(float).values.tolist()
# Define symbols
x = sp.symbols('x')
coefficients = sp.symbols('a b c d e')
# Create the polynomial expression
polynomial_expression = None
for i, coef in enumerate(reversed(coefficients)):
polynomial_expression = polynomial_expression + coef * x**i if polynomial_expression else coef * x**i
# Parameter mapping
parameters = {coef: value for coef, value in zip(coefficients, process_params[0])}
# Substitute parameter values into the expression
polynomial_with_values = polynomial_expression.subs(parameters)
latex_representation = sp.latex(polynomial_with_values)
return fr"Underlying process $${latex_representation}$$"
def true_process(x, process_params):
"""The true process we want to model."""
process_params = process_params.astype(float).values.tolist()
return (
process_params[0][0] * (x ** 4)
+ process_params[0][1] * (x ** 3)
+ process_params[0][2] * (x ** 2)
+ process_params[0][3] * x
+ process_params[0][4]
)
def generate_data(num_points, noise_level, process_params):
# x is the list of input values
input_values = np.linspace(-5, 2, num_points)
input_values_dense = np.linspace(-5, 2, MAX_POINTS)
# y = f(x) is the underlying process we want to model
y = [true_process(x, process_params) for x in input_values]
y_dense = [true_process(x, process_params) for x in input_values_dense]
# however, we can only observe a noisy version of f(x)
noise = np.random.normal(0, noise_level, len(input_values))
y_noisy = y + noise
return input_values, input_values_dense, y, y_dense, y_noisy
def make_plot(
num_points, noise_level, process_params,
show_true_process, show_original_points, show_added_noise, show_noisy_points,
):
x, x_dense, y, y_dense, y_noisy = generate_data(num_points, noise_level, process_params)
fig = plt.figure(dpi=300)
if show_true_process:
plt.plot(
x_dense, y_dense, "-", color="#363A4F",
label="True Process",
lw=1.5,
)
if show_added_noise:
plt.vlines(
x, y, y_noisy, color="#556D9A",
linestyles="dashed",
alpha=0.75,
lw=1,
label="Added Noise",
)
if show_original_points:
plt.plot(
x, y, "-o", color="none",
ms=6,
markerfacecolor="white",
markeredgecolor="#556D9A",
markeredgewidth=1.2,
label="Original Points",
)
if show_noisy_points:
plt.plot(
x, y_noisy, "-o", color="none",
ms=6.5,
markerfacecolor="#556D9A",
markeredgecolor="none",
markeredgewidth=1.5,
alpha=1,
label="Noisy Points",
)
plt.xlabel("\nx")
plt.ylabel("y")
plt.legend(fontsize=7.5)
plt.tight_layout()
plt.show()
return fig
# Force main column to be 100 pixels wide, knowing that the parent is a flex container with column direction
css = """
.gradio-container {
width: min(1000px, 50%)!important;
min-width: 800px;
}
.main-plot {
}
"""
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column():
with gr.Row():
process_params = gr.DataFrame(
value=[[0.5, 2, -0.5, -2, 1]],
label="Underlying Process Coefficients",
type="pandas",
column_widths=("2", "1", "1", "1", "1w"),
headers=["x ** 4", "x ** 3", "x ** 2", "x", "1"],
interactive=True
)
equation = gr.Markdown()
with gr.Row():
with gr.Column():
num_points = gr.Slider(
minimum=5,
maximum=MAX_POINTS,
value=DEFAULT_POINTS,
step=SLIDE_POINTS_STEP,
label="Number of Points"
)
with gr.Column():
noise_level = gr.Slider(
minimum=0,
maximum=MAX_NOISE,
value=DEFAULT_NOISE,
step=SLIDE_NOISE_STEP,
label="Noise Level"
)
show_params = []
with gr.Row():
with gr.Column():
show_params.append(gr.Checkbox(label="Show Underlying Process", value=True))
show_params.append(gr.Checkbox(label="Show Original Points", value=True))
with gr.Column():
show_params.append(gr.Checkbox(label="Show Added Noise", value=True))
show_params.append(gr.Checkbox(label="Show Noisy Points", value=True))
scatter_plot = gr.Plot(elem_classes=["main-plot"])
num_points.change(fn=make_plot, inputs=[num_points, noise_level, process_params, *show_params], outputs=scatter_plot)
noise_level.change(fn=make_plot, inputs=[num_points, noise_level, process_params, *show_params], outputs=scatter_plot)
process_params.change(fn=make_plot, inputs=[num_points, noise_level, process_params, *show_params], outputs=scatter_plot)
process_params.change(fn=generate_equation, inputs=[process_params], outputs=equation)
for component in show_params:
component.change(fn=make_plot, inputs=[num_points, noise_level, process_params, *show_params], outputs=scatter_plot)
demo.load(fn=make_plot, inputs=[num_points, noise_level, process_params, *show_params], outputs=scatter_plot)
demo.load(fn=generate_equation, inputs=[process_params], outputs=equation)
if __name__ == "__main__":
demo.launch()
|