Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -72,8 +72,16 @@ def newton_backward_interpolation(x, y, x_interp):
|
|
72 |
|
73 |
return np.array([newton_backward(xi) for xi in x_interp])
|
74 |
|
75 |
-
def create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label, y_label, legend_position, label_size, x_predict=None, y_predict=None):
|
76 |
fig, ax = plt.subplots(figsize=(10, 6))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
ax.scatter(x, y, color='red', label='Input points')
|
78 |
ax.plot(x_interp, y_interp, label=f'{method} interpolant')
|
79 |
ax.set_xlabel(x_label, fontsize=label_size)
|
@@ -89,7 +97,7 @@ def create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label,
|
|
89 |
|
90 |
return fig
|
91 |
|
92 |
-
def interpolate_and_plot(x_input, y_input, x_predict, method, plot_title, x_label, y_label, legend_position, label_size):
|
93 |
try:
|
94 |
x = np.array([float(val.strip()) for val in x_input.split(',')])
|
95 |
y = np.array([float(val.strip()) for val in y_input.split(',')])
|
@@ -144,34 +152,55 @@ def interpolate_and_plot(x_input, y_input, x_predict, method, plot_title, x_labe
|
|
144 |
|
145 |
y_predict = np.interp(x_predict, x_interp, y_interp)
|
146 |
|
147 |
-
fig = create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label, y_label, legend_position, label_size, x_predict, y_predict)
|
148 |
return fig, f"Predicted y value for x = {x_predict}: {y_predict:.4f}"
|
149 |
except ValueError:
|
150 |
error_msg = "Error: Invalid input for x prediction. Please enter a number."
|
151 |
return create_error_plot(error_msg), f'<p style="color: red;">{error_msg}</p>'
|
152 |
|
153 |
-
fig = create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label, y_label, legend_position, label_size)
|
154 |
return fig, None
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
gr.
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
-
iface.launch()
|
|
|
72 |
|
73 |
return np.array([newton_backward(xi) for xi in x_interp])
|
74 |
|
75 |
+
def create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label, y_label, legend_position, label_size, log_x, x_predict=None, y_predict=None):
|
76 |
fig, ax = plt.subplots(figsize=(10, 6))
|
77 |
+
|
78 |
+
if log_x:
|
79 |
+
# Ensure all x-values are positive before setting log scale
|
80 |
+
if np.any(np.array(x) <= 0):
|
81 |
+
return create_error_plot("Error: All x values must be positive for logarithmic scale."), \
|
82 |
+
'<p style="color: red;">Error: All x values must be positive for logarithmic scale.</p>'
|
83 |
+
ax.set_xscale('log')
|
84 |
+
|
85 |
ax.scatter(x, y, color='red', label='Input points')
|
86 |
ax.plot(x_interp, y_interp, label=f'{method} interpolant')
|
87 |
ax.set_xlabel(x_label, fontsize=label_size)
|
|
|
97 |
|
98 |
return fig
|
99 |
|
100 |
+
def interpolate_and_plot(x_input, y_input, x_predict, method, plot_title, x_label, y_label, legend_position, label_size, log_x):
|
101 |
try:
|
102 |
x = np.array([float(val.strip()) for val in x_input.split(',')])
|
103 |
y = np.array([float(val.strip()) for val in y_input.split(',')])
|
|
|
152 |
|
153 |
y_predict = np.interp(x_predict, x_interp, y_interp)
|
154 |
|
155 |
+
fig = create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label, y_label, legend_position, label_size, log_x, x_predict, y_predict)
|
156 |
return fig, f"Predicted y value for x = {x_predict}: {y_predict:.4f}"
|
157 |
except ValueError:
|
158 |
error_msg = "Error: Invalid input for x prediction. Please enter a number."
|
159 |
return create_error_plot(error_msg), f'<p style="color: red;">{error_msg}</p>'
|
160 |
|
161 |
+
fig = create_and_edit_plot(x, y, x_interp, y_interp, method, plot_title, x_label, y_label, legend_position, label_size, log_x)
|
162 |
return fig, None
|
163 |
|
164 |
+
def toggle_plot_options(show_options):
|
165 |
+
return not show_options, gr.update(visible=not show_options)
|
166 |
+
|
167 |
+
with gr.Blocks() as iface:
|
168 |
+
gr.Markdown("# Interpolation App")
|
169 |
+
gr.Markdown("Enter x and y values to see the interpolation graph. Choose the interpolation method using the radio buttons. Optionally, enter an x value (between min and max of input x values) to predict its corresponding y value. Note: Newton Forward and Backward methods require uniform x spacing.")
|
170 |
+
|
171 |
+
show_options = gr.State(False)
|
172 |
+
|
173 |
+
with gr.Row():
|
174 |
+
with gr.Column():
|
175 |
+
x_input = gr.Textbox(label="X values (comma-separated)")
|
176 |
+
y_input = gr.Textbox(label="Y values (comma-separated)")
|
177 |
+
x_predict = gr.Number(label="X value to predict (optional)", value=lambda: None)
|
178 |
+
method = gr.Radio(["Linear", "Quadratic", "Lagrange", "Newton Forward", "Newton Backward"], label="Interpolation Method", value="Linear")
|
179 |
+
submit_btn = gr.Button("Generate Plot", variant="primary", elem_id="submit-btn")
|
180 |
+
edit_plot_btn = gr.Button("Edit Plot", variant="secondary")
|
181 |
+
|
182 |
+
with gr.Column():
|
183 |
+
plot_output = gr.Plot(label="Interpolation Plot")
|
184 |
+
result_output = gr.HTML(label="Result or Error Message")
|
185 |
+
|
186 |
+
plot_options = gr.Column(visible=False)
|
187 |
+
with plot_options:
|
188 |
+
plot_title = gr.Textbox(label="Plot Title", value="Interpolation Plot")
|
189 |
+
x_label = gr.Textbox(label="X-axis Label", value="x")
|
190 |
+
y_label = gr.Textbox(label="Y-axis Label", value="y")
|
191 |
+
legend_position = gr.Dropdown(["best", "upper right", "upper left", "lower left", "lower right", "right", "center left", "center right", "lower center", "upper center", "center"], label="Legend Position", value="best")
|
192 |
+
label_size = gr.Slider(minimum=8, maximum=24, step=1, label="Label Size", value=14)
|
193 |
+
log_x = gr.Checkbox(label="Log scale for X-axis", value=False)
|
194 |
+
|
195 |
+
edit_plot_btn.click(
|
196 |
+
toggle_plot_options,
|
197 |
+
inputs=[show_options],
|
198 |
+
outputs=[show_options, plot_options]
|
199 |
+
)
|
200 |
+
|
201 |
+
inputs = [x_input, y_input, x_predict, method, plot_title, x_label, y_label, legend_position, label_size, log_x]
|
202 |
+
outputs = [plot_output, result_output]
|
203 |
+
|
204 |
+
submit_btn.click(interpolate_and_plot, inputs=inputs, outputs=outputs)
|
205 |
|
206 |
+
iface.launch()
|