kedimestan commited on
Commit
ee16303
·
verified ·
1 Parent(s): 53cac92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -45
app.py CHANGED
@@ -3,62 +3,57 @@ import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import fcsparser
5
 
 
6
  def parse_fcs(file):
 
 
7
  try:
8
- # Parse the FCS file
9
  meta, data = fcsparser.parse(file.name)
10
- df = pd.DataFrame(data)
11
-
12
- # Check if the DataFrame is empty
13
- if df.empty:
14
- raise ValueError("The parsed DataFrame is empty. Please check the .fcs file.")
15
-
16
- return df
17
  except Exception as e:
18
  raise gr.Error(f"Error parsing .fcs file: {str(e)}")
19
 
20
- def plot_fcs(data, x_axis, y_axis):
21
- try:
22
- # Plot the data
23
- plt.figure(figsize=(10, 6))
24
- plt.scatter(data[x_axis], data[y_axis], alpha=0.5)
25
- plt.xlabel(x_axis)
26
- plt.ylabel(y_axis)
27
- plt.title(f"{x_axis} vs {y_axis}")
28
- return plt.gcf() # Return the current figure
29
- except Exception as e:
30
- raise gr.Error(f"Error plotting data: {str(e)}")
31
 
32
- def process_fcs(file):
 
 
 
 
 
 
33
  try:
34
- # Parse the FCS file and get the DataFrame
35
- df = parse_fcs(file)
36
-
37
- # Get the column names for x and y axis choices
38
- columns = df.columns.tolist()
39
-
40
- # Check if columns are available
41
- if not columns:
42
- raise ValueError("No columns found in the DataFrame.")
43
-
44
- # Create a Gradio interface for selecting x and y axis
45
- with gr.Blocks() as demo:
46
- with gr.Row():
47
- x_axis = gr.Dropdown(choices=columns, label="Select X-axis")
48
- y_axis = gr.Dropdown(choices=columns, label="Select Y-axis")
49
- plot = gr.Plot()
50
-
51
- # Update the plot when the dropdowns change
52
- x_axis.change(lambda x, y, df: plot_fcs(df, x, y), inputs=[x_axis, y_axis, gr.State(df)], outputs=plot)
53
- y_axis.change(lambda x, y, df: plot_fcs(df, x, y), inputs=[x_axis, y_axis, gr.State(df)], outputs=plot)
54
-
55
- return demo
56
  except Exception as e:
57
- raise gr.Error(f"Error processing .fcs file: {str(e)}")
58
 
59
- # Gradio interface for uploading the FCS file
60
  with gr.Blocks() as demo:
 
 
61
  file = gr.File(label="Upload FCS File", file_types=[".fcs"])
62
- output = gr.Interface(fn=process_fcs, inputs=file, outputs="component", live=True)
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  demo.launch()
 
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()