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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -4,38 +4,57 @@ import matplotlib.pyplot as plt
4
  import fcsparser
5
 
6
  def parse_fcs(file):
7
- # Parse the FCS file
8
- meta, data = fcsparser.parse(file.name)
9
- return pd.DataFrame(data)
 
 
 
 
 
 
 
 
 
10
 
11
  def plot_fcs(data, x_axis, y_axis):
12
- # Plot the data
13
- plt.figure(figsize=(10, 6))
14
- plt.scatter(data[x_axis], data[y_axis], alpha=0.5)
15
- plt.xlabel(x_axis)
16
- plt.ylabel(y_axis)
17
- plt.title(f"{x_axis} vs {y_axis}")
18
- return plt
 
 
 
19
 
20
  def process_fcs(file):
21
- # Parse the FCS file and get the DataFrame
22
- df = parse_fcs(file)
23
-
24
- # Get the column names for x and y axis choices
25
- columns = df.columns.tolist()
26
-
27
- # Create a Gradio interface for selecting x and y axis
28
- with gr.Blocks() as demo:
29
- with gr.Row():
30
- x_axis = gr.Dropdown(choices=columns, label="Select X-axis")
31
- y_axis = gr.Dropdown(choices=columns, label="Select Y-axis")
32
- plot = gr.Plot()
 
 
 
 
 
 
 
 
 
33
 
34
- # Update the plot when the dropdowns change
35
- x_axis.change(plot_fcs, inputs=[gr.State(df), x_axis, y_axis], outputs=plot)
36
- y_axis.change(plot_fcs, inputs=[gr.State(df), x_axis, y_axis], outputs=plot)
37
-
38
- return demo
39
 
40
  # Gradio interface for uploading the FCS file
41
  with gr.Blocks() as demo:
 
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: