kedimestan commited on
Commit
9239ab3
·
verified ·
1 Parent(s): ee16303

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -43
app.py CHANGED
@@ -1,59 +1,83 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
- import fcsparser
5
 
6
- # Function to parse the FCS file
7
- def parse_fcs(file):
8
- if file is None:
9
- return pd.DataFrame()
10
- try:
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
- # Function to populate the dropdown choices
17
- def populate_dropdowns(df):
18
- if df is None or df.empty:
19
- return gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
20
- columns = df.columns.tolist()
21
- return gr.Dropdown.update(choices=columns), gr.Dropdown.update(choices=columns)
 
 
 
 
 
22
 
23
- # Function to plot the data
24
- def plot_fcs(df, x, y):
25
- if df is None or df.empty or x not in df.columns or y not in df.columns:
26
- fig = plt.figure()
27
- plt.text(0.5, 0.5, "Please select valid columns.", ha='center', va='center')
28
- plt.axis('off')
29
- return fig
30
- try:
31
- fig = plt.figure(figsize=(10, 6))
32
- plt.scatter(df[x], df[y], alpha=0.5)
33
- plt.xlabel(x)
34
- plt.ylabel(y)
35
- plt.title(f"{x} vs {y}")
36
- return fig
37
- except Exception as e:
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("<h1>FCS File Uploader and Plotter</h1>")
43
 
44
- file = gr.File(label="Upload FCS File", file_types=[".fcs"])
45
  df_state = gr.State()
 
46
 
47
  with gr.Row():
48
- x_axis = gr.Dropdown(label="Select X-axis")
49
- y_axis = gr.Dropdown(label="Select Y-axis")
 
50
 
51
- plot = gr.Plot()
 
52
 
53
  # Event handlers
54
- file.change(parse_fcs, file, df_state)
55
- file.change(populate_dropdowns, df_state, [x_axis, y_axis])
56
- x_axis.change(plot_fcs, [df_state, x_axis, y_axis], plot)
57
- y_axis.change(plot_fcs, [df_state, x_axis, y_axis], plot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()