dotBlood / app.py
kedimestan's picture
Update app.py
53cac92 verified
raw
history blame
2.24 kB
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import fcsparser
def parse_fcs(file):
try:
# Parse the FCS file
meta, data = fcsparser.parse(file.name)
df = pd.DataFrame(data)
# Check if the DataFrame is empty
if df.empty:
raise ValueError("The parsed DataFrame is empty. Please check the .fcs file.")
return df
except Exception as e:
raise gr.Error(f"Error parsing .fcs file: {str(e)}")
def plot_fcs(data, x_axis, y_axis):
try:
# Plot the data
plt.figure(figsize=(10, 6))
plt.scatter(data[x_axis], data[y_axis], alpha=0.5)
plt.xlabel(x_axis)
plt.ylabel(y_axis)
plt.title(f"{x_axis} vs {y_axis}")
return plt.gcf() # Return the current figure
except Exception as e:
raise gr.Error(f"Error plotting data: {str(e)}")
def process_fcs(file):
try:
# Parse the FCS file and get the DataFrame
df = parse_fcs(file)
# Get the column names for x and y axis choices
columns = df.columns.tolist()
# Check if columns are available
if not columns:
raise ValueError("No columns found in the DataFrame.")
# Create a Gradio interface for selecting x and y axis
with gr.Blocks() as demo:
with gr.Row():
x_axis = gr.Dropdown(choices=columns, label="Select X-axis")
y_axis = gr.Dropdown(choices=columns, label="Select Y-axis")
plot = gr.Plot()
# Update the plot when the dropdowns change
x_axis.change(lambda x, y, df: plot_fcs(df, x, y), inputs=[x_axis, y_axis, gr.State(df)], outputs=plot)
y_axis.change(lambda x, y, df: plot_fcs(df, x, y), inputs=[x_axis, y_axis, gr.State(df)], outputs=plot)
return demo
except Exception as e:
raise gr.Error(f"Error processing .fcs file: {str(e)}")
# Gradio interface for uploading the FCS file
with gr.Blocks() as demo:
file = gr.File(label="Upload FCS File", file_types=[".fcs"])
output = gr.Interface(fn=process_fcs, inputs=file, outputs="component", live=True)
demo.launch()