Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
-
import
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
meta, data = fcsparser.parse(file.name)
|
12 |
-
return pd.DataFrame(data)
|
13 |
-
except Exception as e:
|
14 |
-
raise gr.Error(f"Error parsing .fcs file: {str(e)}")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
raise gr.Error(f"Error plotting data: {str(e)}")
|
39 |
|
40 |
-
# Build the Gradio app
|
41 |
with gr.Blocks() as demo:
|
42 |
-
gr.Markdown("
|
43 |
|
44 |
-
|
45 |
df_state = gr.State()
|
|
|
46 |
|
47 |
with gr.Row():
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
-
plot = gr.Plot
|
|
|
52 |
|
53 |
# Event handlers
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from shapely.geometry import Point, Polygon
|
5 |
|
6 |
+
def load_csv(file):
|
7 |
+
return pd.read_csv(file.name)
|
8 |
+
|
9 |
+
def update_dropdowns(df):
|
10 |
+
return gr.Dropdown(choices=df.columns.tolist()), gr.Dropdown(choices=df.columns.tolist())
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def create_plot(df, x_col, y_col):
|
13 |
+
if df is None or x_col is None or y_col is None:
|
14 |
+
return None
|
15 |
+
return gr.ScatterPlot.update(
|
16 |
+
value=pd.DataFrame({x_col: df[x_col], y_col: df[y_col]}),
|
17 |
+
x=x_col,
|
18 |
+
y=y_col,
|
19 |
+
title=f"{x_col} vs {y_col}",
|
20 |
+
interactive=True,
|
21 |
+
tooltip=[x_col, y_col]
|
22 |
+
)
|
23 |
|
24 |
+
def find_points_in_polygon(selected_data, df, x_col, y_col):
|
25 |
+
if not selected_data or not x_col or not y_col:
|
26 |
+
return pd.DataFrame()
|
27 |
+
|
28 |
+
# Extract polygon points from selection
|
29 |
+
polygon_points = [(point["x"], point["y"]) for point in selected_data["points"]]
|
30 |
+
|
31 |
+
# Create Shapely objects
|
32 |
+
polygon = Polygon(polygon_points)
|
33 |
+
points = [Point(row[x_col], row[y_col]) for _, row in df.iterrows()]
|
34 |
+
|
35 |
+
# Find points inside polygon
|
36 |
+
inside_indices = [i for i, point in enumerate(points) if polygon.contains(point)]
|
37 |
+
|
38 |
+
return df.iloc[inside_indices]
|
|
|
39 |
|
|
|
40 |
with gr.Blocks() as demo:
|
41 |
+
gr.Markdown("## CSV Data Explorer with Polygon Selection")
|
42 |
|
43 |
+
# Store DataFrame and polygon points
|
44 |
df_state = gr.State()
|
45 |
+
polygon_state = gr.State([])
|
46 |
|
47 |
with gr.Row():
|
48 |
+
csv_upload = gr.File(label="Upload CSV", file_types=[".csv"])
|
49 |
+
x_col = gr.Dropdown(label="X Column")
|
50 |
+
y_col = gr.Dropdown(label="Y Column")
|
51 |
|
52 |
+
plot = gr.ScatterPlot(interactive=True, label="Scatter Plot")
|
53 |
+
results = gr.DataFrame(label="Points in Polygon")
|
54 |
|
55 |
# Event handlers
|
56 |
+
csv_upload.upload(
|
57 |
+
load_csv,
|
58 |
+
inputs=csv_upload,
|
59 |
+
outputs=df_state
|
60 |
+
).then(
|
61 |
+
update_dropdowns,
|
62 |
+
inputs=df_state,
|
63 |
+
outputs=[x_col, y_col]
|
64 |
+
)
|
65 |
+
|
66 |
+
x_col.change(
|
67 |
+
create_plot,
|
68 |
+
inputs=[df_state, x_col, y_col],
|
69 |
+
outputs=plot
|
70 |
+
)
|
71 |
+
y_col.change(
|
72 |
+
create_plot,
|
73 |
+
inputs=[df_state, x_col, y_col],
|
74 |
+
outputs=plot
|
75 |
+
)
|
76 |
+
|
77 |
+
plot.select(
|
78 |
+
find_points_in_polygon,
|
79 |
+
inputs=[df_state, x_col, y_col],
|
80 |
+
outputs=results
|
81 |
+
)
|
82 |
|
83 |
demo.launch()
|