Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,125 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import pandas as pd
|
3 |
-
import plotly.
|
|
|
|
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
df = pd.read_csv(csv_file.name)
|
9 |
-
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
10 |
-
return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols)
|
11 |
|
12 |
-
def
|
13 |
-
if
|
14 |
-
return
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
y=y_col,
|
24 |
-
tooltip=df.columns.tolist(),
|
25 |
-
title="Interactive Scatter Plot",
|
26 |
-
color_legend_title="Values",
|
27 |
-
interactive=True,
|
28 |
-
brush_radius=5
|
29 |
-
),
|
30 |
-
df
|
31 |
)
|
32 |
-
|
33 |
-
def get_selected_points(evt: gr.SelectData, df):
|
34 |
-
if evt is None or df is None:
|
35 |
-
return pd.DataFrame()
|
36 |
|
37 |
-
|
38 |
-
selected_indices = [i["index"] for i in evt.selected]
|
39 |
-
return df.iloc[selected_indices]
|
40 |
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
inputs=csv_upload,
|
58 |
-
outputs=[x_dropdown, y_dropdown]
|
59 |
-
)
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
outputs=[scatter_plot, df_state]
|
65 |
-
)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
outputs=[scatter_plot, df_state]
|
71 |
-
)
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
import pandas as pd
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
from fcsparser import parse
|
6 |
+
from matplotlib.path import Path
|
7 |
|
8 |
+
def parse_fcs(file):
|
9 |
+
_, data = parse(file.name, reformat_meta=True)
|
10 |
+
return data
|
|
|
|
|
|
|
11 |
|
12 |
+
def update_params(data):
|
13 |
+
if data is None:
|
14 |
+
return [gr.Dropdown.update(choices=[])] * 2
|
15 |
+
return [gr.Dropdown.update(choices=data.columns.tolist())] * 2
|
16 |
+
|
17 |
+
def create_plot(data, x_param, y_param):
|
18 |
+
if data is None or not x_param or not y_param:
|
19 |
+
return None
|
20 |
|
21 |
+
fig = go.Figure(data=go.Scattergl(
|
22 |
+
x=data[x_param],
|
23 |
+
y=data[y_param],
|
24 |
+
mode='markers',
|
25 |
+
marker=dict(size=2, opacity=0.5)
|
26 |
+
))
|
27 |
|
28 |
+
fig.update_layout(
|
29 |
+
title=f"{x_param} vs {y_param}",
|
30 |
+
dragmode='drawclosedpath',
|
31 |
+
width=800,
|
32 |
+
height=600
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
)
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
return fig
|
|
|
|
|
36 |
|
37 |
+
def apply_gate(data, x_param, y_param, fig):
|
38 |
+
if data is None or fig is None:
|
39 |
+
return "Önce veri yükleyin ve grafik oluşturun", None
|
40 |
|
41 |
+
# Plotly şekil verisini çıkar
|
42 |
+
shapes = fig.get('layout', {}).get('shapes', [])
|
43 |
+
if not shapes:
|
44 |
+
return "Lütfen önce polygon çizin", None
|
45 |
|
46 |
+
# Son çizilen şekli al
|
47 |
+
last_shape = shapes[-1]
|
48 |
+
path = last_shape.get('path', '')
|
49 |
|
50 |
+
# Path verisini işle (M=move, L=line, Z=close)
|
51 |
+
vertices = []
|
52 |
+
for part in path.split(' '):
|
53 |
+
if part.startswith('M') or part.startswith('L'):
|
54 |
+
x, y = part[1:].split(',')
|
55 |
+
vertices.append((float(x), float(y)))
|
56 |
|
57 |
+
if len(vertices) < 3:
|
58 |
+
return "Geçerli bir polygon çizilmedi", None
|
|
|
|
|
|
|
59 |
|
60 |
+
# Matplotlib Path oluştur
|
61 |
+
polygon = Path(vertices)
|
62 |
+
points = np.vstack([data[x_param], data[y_param]]).T
|
|
|
|
|
63 |
|
64 |
+
# İçerde kalan noktaları bul
|
65 |
+
mask = polygon.contains_points(points)
|
66 |
+
gated_data = data[mask]
|
|
|
|
|
67 |
|
68 |
+
# Sonuçları hazırla
|
69 |
+
stats = f"""
|
70 |
+
Toplam Hücre: {len(data):,}
|
71 |
+
Geçitlenen Hücre: {len(gated_data):,} (%{len(gated_data)/len(data)*100:.1f})
|
72 |
+
İlk 10 Hücre İndeksi: {gated_data.index[:10].tolist()}
|
73 |
+
"""
|
74 |
+
|
75 |
+
return stats, gated_data
|
76 |
+
|
77 |
+
with gr.Blocks(title="FCS Analiz Uygulaması") as demo:
|
78 |
+
gr.Markdown("## FCS Dosya Analizi ve Hücre Geçitleme")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column():
|
82 |
+
file_input = gr.File(label="FCS Dosyası Yükle", type="file")
|
83 |
+
x_param = gr.Dropdown(label="X Parametresi")
|
84 |
+
y_param = gr.Dropdown(label="Y Parametresi")
|
85 |
+
plot_btn = gr.Button("Grafik Oluştur")
|
86 |
+
|
87 |
+
with gr.Column():
|
88 |
+
plot_output = gr.Plot(label="Dağılım Grafiği")
|
89 |
+
gate_btn = gr.Button("Geçitlemeyi Uygula")
|
90 |
+
result_output = gr.Textbox(label="Sonuçlar")
|
91 |
+
data_output = gr.DataFrame(label="Geçitlenen Veriler", visible=False)
|
92 |
+
|
93 |
+
# State management
|
94 |
+
data_state = gr.State()
|
95 |
+
fig_state = gr.State()
|
96 |
+
|
97 |
+
# Event handlers
|
98 |
+
file_input.change(
|
99 |
+
fn=parse_fcs,
|
100 |
+
inputs=file_input,
|
101 |
+
outputs=data_state
|
102 |
+
).then(
|
103 |
+
fn=update_params,
|
104 |
+
inputs=data_state,
|
105 |
+
outputs=[x_param, y_param]
|
106 |
+
)
|
107 |
+
|
108 |
+
plot_btn.click(
|
109 |
+
fn=create_plot,
|
110 |
+
inputs=[data_state, x_param, y_param],
|
111 |
+
outputs=[plot_output]
|
112 |
+
).then(
|
113 |
+
lambda fig: fig,
|
114 |
+
inputs=plot_output,
|
115 |
+
outputs=fig_state
|
116 |
+
)
|
117 |
+
|
118 |
+
gate_btn.click(
|
119 |
+
fn=apply_gate,
|
120 |
+
inputs=[data_state, x_param, y_param, fig_state],
|
121 |
+
outputs=[result_output, data_output]
|
122 |
)
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
+
demo.launch()
|