Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,12 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def linear_interpolation(x, y, x_interp):
|
6 |
return np.interp(x_interp, x, y)
|
7 |
|
@@ -23,11 +29,20 @@ def lagrange_interpolation(x, y, x_interp):
|
|
23 |
return y_interp
|
24 |
|
25 |
def interpolate_and_plot(x_input, y_input, x_predict):
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
if len(x) != len(y):
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
x_interp = np.linspace(min(x), max(x), 100)
|
33 |
|
@@ -41,33 +56,39 @@ def interpolate_and_plot(x_input, y_input, x_predict):
|
|
41 |
y_interp = lagrange_interpolation(x, y, x_interp)
|
42 |
method = "Lagrange"
|
43 |
|
44 |
-
plt.
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
# Predict y value for given x
|
54 |
if x_predict is not None:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
return
|
71 |
|
72 |
iface = gr.Interface(
|
73 |
fn=interpolate_and_plot,
|
@@ -78,10 +99,10 @@ iface = gr.Interface(
|
|
78 |
],
|
79 |
outputs=[
|
80 |
gr.Plot(label="Interpolation Plot"),
|
81 |
-
gr.
|
82 |
],
|
83 |
title="Interpolation App",
|
84 |
-
description="Enter x and y values to see the interpolation graph. The method will be chosen based on the number of points:\n2 points: Linear, 3 points: Quadratic, >3 points: Lagrange
|
85 |
)
|
86 |
|
87 |
iface.launch()
|
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
|
5 |
+
def create_error_plot(error_message):
|
6 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
7 |
+
ax.text(0.5, 0.5, error_message, color='red', fontsize=16, ha='center', va='center', wrap=True)
|
8 |
+
ax.axis('off')
|
9 |
+
return fig
|
10 |
+
|
11 |
def linear_interpolation(x, y, x_interp):
|
12 |
return np.interp(x_interp, x, y)
|
13 |
|
|
|
29 |
return y_interp
|
30 |
|
31 |
def interpolate_and_plot(x_input, y_input, x_predict):
|
32 |
+
try:
|
33 |
+
x = np.array([float(val.strip()) for val in x_input.split(',')])
|
34 |
+
y = np.array([float(val.strip()) for val in y_input.split(',')])
|
35 |
+
except ValueError:
|
36 |
+
error_msg = "Error: Invalid input. Please enter comma-separated numbers."
|
37 |
+
return create_error_plot(error_msg), f'<p style="color: red;">{error_msg}</p>'
|
38 |
|
39 |
if len(x) != len(y):
|
40 |
+
error_msg = "Error: Number of x and y values must be the same."
|
41 |
+
return create_error_plot(error_msg), f'<p style="color: red;">{error_msg}</p>'
|
42 |
+
|
43 |
+
if len(x) < 2:
|
44 |
+
error_msg = "Error: At least two points are required for interpolation."
|
45 |
+
return create_error_plot(error_msg), f'<p style="color: red;">{error_msg}</p>'
|
46 |
|
47 |
x_interp = np.linspace(min(x), max(x), 100)
|
48 |
|
|
|
56 |
y_interp = lagrange_interpolation(x, y, x_interp)
|
57 |
method = "Lagrange"
|
58 |
|
59 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
60 |
+
ax.scatter(x, y, color='red', label='Input points')
|
61 |
+
ax.plot(x_interp, y_interp, label=f'{method} interpolant')
|
62 |
+
ax.set_xlabel('x')
|
63 |
+
ax.set_ylabel('y')
|
64 |
+
ax.set_title(f'{method} Interpolation')
|
65 |
+
ax.legend()
|
66 |
+
ax.grid(True)
|
67 |
|
68 |
# Predict y value for given x
|
69 |
if x_predict is not None:
|
70 |
+
try:
|
71 |
+
x_predict = float(x_predict)
|
72 |
+
if x_predict < min(x) or x_predict > max(x):
|
73 |
+
error_msg = f"Error: Prediction x value must be between {min(x)} and {max(x)}."
|
74 |
+
return fig, f'<p style="color: red;">{error_msg}</p>'
|
75 |
+
|
76 |
+
if len(x) == 2:
|
77 |
+
y_predict = linear_interpolation(x, y, [x_predict])[0]
|
78 |
+
elif len(x) == 3:
|
79 |
+
y_predict = quadratic_interpolation(x, y, [x_predict])[0]
|
80 |
+
else:
|
81 |
+
y_predict = lagrange_interpolation(x, y, [x_predict])[0]
|
82 |
+
|
83 |
+
ax.scatter([x_predict], [y_predict], color='green', s=100, label='Predicted point')
|
84 |
+
ax.legend()
|
85 |
+
|
86 |
+
return fig, f"Predicted y value for x = {x_predict}: {y_predict:.4f}"
|
87 |
+
except ValueError:
|
88 |
+
error_msg = "Error: Invalid input for x prediction. Please enter a number."
|
89 |
+
return create_error_plot(error_msg), f'<p style="color: red;">{error_msg}</p>'
|
90 |
|
91 |
+
return fig, None
|
92 |
|
93 |
iface = gr.Interface(
|
94 |
fn=interpolate_and_plot,
|
|
|
99 |
],
|
100 |
outputs=[
|
101 |
gr.Plot(label="Interpolation Plot"),
|
102 |
+
gr.HTML(label="Result or Error Message")
|
103 |
],
|
104 |
title="Interpolation App",
|
105 |
+
description="Enter x and y values to see the interpolation graph. The method will be chosen based on the number of points:\n2 points: Linear, 3 points: Quadratic, >3 points: Lagrange\nOptionally, enter an x value (between min and max of input x values) to predict its corresponding y value."
|
106 |
)
|
107 |
|
108 |
iface.launch()
|