linx5o commited on
Commit
c5b039d
·
1 Parent(s): 376e644

fixed bug with code, now the graphs are interactive

Browse files
Files changed (1) hide show
  1. app.py +51 -39
app.py CHANGED
@@ -8,6 +8,7 @@ import numpy as np
8
  from datetime import datetime
9
  import pytz
10
  import pandas as pd
 
11
 
12
 
13
  HOST = os.environ.get("host")
@@ -621,7 +622,7 @@ def change_experiment(ch_exp, host, port, username, password, pioreactor, exp):
621
  }
622
  client_custom.publish(f"pioreactor/control", json.dumps(payload))
623
 
624
- def on_message(client, userdata, message):
625
  payload = message.payload.decode("utf-8")
626
  data = json.loads(payload)
627
  global temp_graph
@@ -651,11 +652,10 @@ def get_data_default(time_scale, exp):
651
  "amount2": time_scale,
652
  "amount3": time_scale,
653
  "amount4": time_scale,
 
654
  }
655
 
656
- client.publish(f"pioreactor/control", json.dumps(payload))
657
-
658
- client.on_message = on_message
659
  client.subscribe(f"pioreactor/{PIOREACTOR}/readings")
660
 
661
  timeout = 10
@@ -671,6 +671,8 @@ def get_data_default(time_scale, exp):
671
  norm_od_graph = None
672
  growth_rate_graph = None
673
 
 
 
674
  while temp_graph is None and (time.time() - start) < timeout:
675
  time.sleep(0.1)
676
 
@@ -685,14 +687,17 @@ def get_data_default(time_scale, exp):
685
  temp["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
686
  df = pd.DataFrame(temp_graph)
687
  df = df.set_index("x")
688
-
689
- plt.figure()
690
- plt.plot(df.index, df["y"])
691
- plt.xlabel("Time")
692
- plt.ylabel("Temperature")
693
- plt.title("Temperature vs Time")
694
- plot1 = plt.gcf()
695
- plt.close()
 
 
 
696
  else:
697
  plot1 = None
698
 
@@ -706,13 +711,16 @@ def get_data_default(time_scale, exp):
706
  df = pd.DataFrame(od_graph)
707
  df = df.set_index("x")
708
 
709
- plt.figure()
710
- plt.plot(df.index, df["y"])
711
- plt.xlabel("Time")
712
- plt.ylabel("OD")
713
- plt.title("OD vs Time")
714
- plot2 = plt.gcf()
715
- plt.close()
 
 
 
716
  else:
717
  plot2 = None
718
 
@@ -727,13 +735,16 @@ def get_data_default(time_scale, exp):
727
  df = pd.DataFrame(norm_od_graph)
728
  df = df.set_index("x")
729
 
730
- plt.figure()
731
- plt.plot(df.index, df["y"])
732
- plt.xlabel("Time")
733
- plt.ylabel("Normalized OD")
734
- plt.title("Normalized OD vs Time")
735
- plot3 = plt.gcf()
736
- plt.close()
 
 
 
737
  else:
738
  plot3 = None
739
 
@@ -748,13 +759,16 @@ def get_data_default(time_scale, exp):
748
  df = pd.DataFrame(growth_rate_graph)
749
  df = df.set_index("x")
750
 
751
- plt.figure()
752
- plt.plot(df.index, df["y"])
753
- plt.xlabel("Time")
754
- plt.ylabel("Growth Rate")
755
- plt.title("Growth Rate vs Time")
756
- plot4 = plt.gcf()
757
- plt.close()
 
 
 
758
  else:
759
  plot4 = None
760
 
@@ -982,12 +996,10 @@ with gr.Blocks() as demo:
982
  time_scale = gr.Radio(label="Time Scale", choices=["1 hour", "24 hour", "All Data"])
983
  get_graphs = gr.Button("Get Graphs")
984
 
985
- with gr.Row():
986
- plot1 = gr.Plot()
987
- plot2 = gr.Plot()
988
- with gr.Row():
989
- plot3 = gr.Plot()
990
- plot4 = gr.Plot()
991
 
992
  get_graphs.click(
993
  fn=get_data_default,
 
8
  from datetime import datetime
9
  import pytz
10
  import pandas as pd
11
+ import plotly.graph_objs as go
12
 
13
 
14
  HOST = os.environ.get("host")
 
622
  }
623
  client_custom.publish(f"pioreactor/control", json.dumps(payload))
624
 
625
+ def on_reading(client, userdata, message):
626
  payload = message.payload.decode("utf-8")
627
  data = json.loads(payload)
628
  global temp_graph
 
652
  "amount2": time_scale,
653
  "amount3": time_scale,
654
  "amount4": time_scale,
655
+ "reactor": PIOREACTOR
656
  }
657
 
658
+ client.on_message = on_reading
 
 
659
  client.subscribe(f"pioreactor/{PIOREACTOR}/readings")
660
 
661
  timeout = 10
 
671
  norm_od_graph = None
672
  growth_rate_graph = None
673
 
674
+ client.publish(f"pioreactor/control", json.dumps(payload))
675
+
676
  while temp_graph is None and (time.time() - start) < timeout:
677
  time.sleep(0.1)
678
 
 
687
  temp["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
688
  df = pd.DataFrame(temp_graph)
689
  df = df.set_index("x")
690
+
691
+ # plt.figure()
692
+ # plt.plot(df.index, df["y"])
693
+ # plt.xlabel("Time")
694
+ # plt.ylabel("Temperature")
695
+ # plt.title("Temperature vs Time")
696
+ # plot1 = plt.gcf()
697
+ # plt.close()
698
+ plot1 = go.Figure()
699
+ plot1.add_trace(go.Scatter(x=df.index, y=df["y"], mode="lines", name="Temperature"))
700
+ plot1.update_layout(title="Temperature vs Time", xaxis_title="Time", yaxis_title="Temperature")
701
  else:
702
  plot1 = None
703
 
 
711
  df = pd.DataFrame(od_graph)
712
  df = df.set_index("x")
713
 
714
+ # plt.figure()
715
+ # plt.plot(df.index, df["y"])
716
+ # plt.xlabel("Time")
717
+ # plt.ylabel("OD")
718
+ # plt.title("OD vs Time")
719
+ # plot2 = plt.gcf()
720
+ # plt.close()
721
+ plot2 = go.Figure()
722
+ plot2.add_trace(go.Scatter(x=df.index, y=df["y"], mode="lines", name="OD"))
723
+ plot2.update_layout(title="OD vs Time", xaxis_title="Time", yaxis_title="OD")
724
  else:
725
  plot2 = None
726
 
 
735
  df = pd.DataFrame(norm_od_graph)
736
  df = df.set_index("x")
737
 
738
+ # plt.figure()
739
+ # plt.plot(df.index, df["y"])
740
+ # plt.xlabel("Time")
741
+ # plt.ylabel("Normalized OD")
742
+ # plt.title("Normalized OD vs Time")
743
+ # plot3 = plt.gcf()
744
+ # plt.close()
745
+ plot3 = go.Figure()
746
+ plot3.add_trace(go.Scatter(x=df.index, y=df["y"], mode="lines", name="Normalized OD"))
747
+ plot3.update_layout(title="Normalized OD vs Time", xaxis_title="Time", yaxis_title="Normalized OD")
748
  else:
749
  plot3 = None
750
 
 
759
  df = pd.DataFrame(growth_rate_graph)
760
  df = df.set_index("x")
761
 
762
+ # plt.figure()
763
+ # plt.plot(df.index, df["y"])
764
+ # plt.xlabel("Time")
765
+ # plt.ylabel("Growth Rate")
766
+ # plt.title("Growth Rate vs Time")
767
+ # plot4 = plt.gcf()
768
+ # plt.close()
769
+ plot4 = go.Figure()
770
+ plot4.add_trace(go.Scatter(x=df.index, y=df["y"], mode="lines", name="Growth Rate"))
771
+ plot4.update_layout(title="Growth Rate vs Time", xaxis_title="Time", yaxis_title="Growth Rate")
772
  else:
773
  plot4 = None
774
 
 
996
  time_scale = gr.Radio(label="Time Scale", choices=["1 hour", "24 hour", "All Data"])
997
  get_graphs = gr.Button("Get Graphs")
998
 
999
+ plot1 = gr.Plot()
1000
+ plot2 = gr.Plot()
1001
+ plot3 = gr.Plot()
1002
+ plot4 = gr.Plot()
 
 
1003
 
1004
  get_graphs.click(
1005
  fn=get_data_default,