import gradio as gr import numpy as np import pandas as pd import plotly.graph_objects as go from fcsparser import parse from matplotlib.path import Path def parse_fcs(file): _, data = parse(file.name, reformat_meta=True) return data def update_params(data): if data is None: return [gr.Dropdown.update(choices=[])] * 2 return [gr.Dropdown.update(choices=data.columns.tolist())] * 2 def create_plot(data, x_param, y_param): if data is None or not x_param or not y_param: return None fig = go.Figure(data=go.Scattergl( x=data[x_param], y=data[y_param], mode='markers', marker=dict(size=2, opacity=0.5) )) fig.update_layout( title=f"{x_param} vs {y_param}", dragmode='drawclosedpath', width=800, height=600 ) return fig def apply_gate(data, x_param, y_param, fig): if data is None or fig is None: return "Önce veri yükleyin ve grafik oluşturun", None # Plotly şekil verisini çıkar shapes = fig.get('layout', {}).get('shapes', []) if not shapes: return "Lütfen önce polygon çizin", None # Son çizilen şekli al last_shape = shapes[-1] path = last_shape.get('path', '') # Path verisini işle (M=move, L=line, Z=close) vertices = [] for part in path.split(' '): if part.startswith('M') or part.startswith('L'): x, y = part[1:].split(',') vertices.append((float(x), float(y))) if len(vertices) < 3: return "Geçerli bir polygon çizilmedi", None # Matplotlib Path oluştur polygon = Path(vertices) points = np.vstack([data[x_param], data[y_param]]).T # İçerde kalan noktaları bul mask = polygon.contains_points(points) gated_data = data[mask] # Sonuçları hazırla stats = f""" Toplam Hücre: {len(data):,} Geçitlenen Hücre: {len(gated_data):,} (%{len(gated_data)/len(data)*100:.1f}) İlk 10 Hücre İndeksi: {gated_data.index[:10].tolist()} """ return stats, gated_data with gr.Blocks(title="FCS Analiz Uygulaması") as demo: gr.Markdown("## FCS Dosya Analizi ve Hücre Geçitleme") with gr.Row(): with gr.Column(): file_input = gr.File(label="FCS Dosyası Yükle", type="file") x_param = gr.Dropdown(label="X Parametresi") y_param = gr.Dropdown(label="Y Parametresi") plot_btn = gr.Button("Grafik Oluştur") with gr.Column(): plot_output = gr.Plot(label="Dağılım Grafiği") gate_btn = gr.Button("Geçitlemeyi Uygula") result_output = gr.Textbox(label="Sonuçlar") data_output = gr.DataFrame(label="Geçitlenen Veriler", visible=False) # State management data_state = gr.State() fig_state = gr.State() # Event handlers file_input.change( fn=parse_fcs, inputs=file_input, outputs=data_state ).then( fn=update_params, inputs=data_state, outputs=[x_param, y_param] ) plot_btn.click( fn=create_plot, inputs=[data_state, x_param, y_param], outputs=[plot_output] ).then( lambda fig: fig, inputs=plot_output, outputs=fig_state ) gate_btn.click( fn=apply_gate, inputs=[data_state, x_param, y_param, fig_state], outputs=[result_output, data_output] ) if __name__ == "__main__": demo.launch()