linx5o commited on
Commit
6345a5c
·
1 Parent(s): 5c3c458

added all graphs

Browse files
Files changed (1) hide show
  1. app.py +120 -23
app.py CHANGED
@@ -17,6 +17,10 @@ import pytz
17
  experiment = None
18
  running = []
19
  temp_graph = None
 
 
 
 
20
 
21
  REQUEST_INTERVAL = 60
22
 
@@ -99,7 +103,13 @@ def on_message(client, userdata, message):
99
  payload = message.payload.decode("utf-8")
100
  data = json.loads(payload)
101
  global temp_graph
102
- temp_graph = data.get("data", None)
 
 
 
 
 
 
103
 
104
  async def publish_temperature_request(client, experiment, filter_mod_N, lookback, reactor):
105
  """Publish a temperature request to the request topic."""
@@ -113,13 +123,17 @@ async def publish_temperature_request(client, experiment, filter_mod_N, lookback
113
  payload_str = json.dumps(payload)
114
  client.publish("pioreactor/control", payload_str)
115
 
116
- async def periodic_temp_reading(client, reactor, experiment):
117
  try:
118
- await publish_temperature_request(client, experiment, 1, 100000, reactor)
 
 
 
 
119
  except asyncio.CancelledError:
120
  pass
121
 
122
- async def run_mqtt_client(client):
123
  client.subscribe(f"pioreactor/{reactor}/temp_reading")
124
  client.subscribe(f"pioreactor/{reactor}/od_reading")
125
  client.subscribe(f"pioreactor/{reactor}/growth_rate")
@@ -127,7 +141,7 @@ async def run_mqtt_client(client):
127
  client.on_message = on_message
128
 
129
  client.loop_start()
130
- await periodic_temp_reading(client, reactor, st.session_state["experiment"])
131
 
132
  def get_running_jobs(client, reactor):
133
  payload = {
@@ -148,6 +162,19 @@ def get_running_jobs(client, reactor):
148
 
149
  while experiment is None and (time.time() - start_time) < timeout:
150
  time.sleep(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
 
153
  if submit_button:
@@ -705,29 +732,62 @@ if st.session_state["experiment"] is not None:
705
 
706
  st.header("Graphs")
707
 
708
- payload = {
709
- "command": "get_temperature_readings",
710
- "experiment": st.session_state["experiment"],
711
- "filter_mod": 1,
712
- "lookback": 100,
713
- "reactor": reactor
714
- }
715
 
716
  st.session_state["client"].loop_start()
717
- st.session_state["client"].on_message = on_message
718
- payload_str = json.dumps(payload)
719
- st.session_state["client"].subscribe(f"pioreactor/{reactor}/temperature")
720
 
721
  temp_graph_actual = []
722
 
723
  st.subheader("Temperature Graph")
724
- amount_of_data = st.radio("Amount of data", ["1 hour", "24 hours", "All data"], index=1)
725
  placeholder = st.empty()
726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727
 
728
  while True:
 
 
 
 
 
 
 
 
 
729
  temp_graph = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730
 
 
731
  st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
732
 
733
  timeout = 30
@@ -736,7 +796,10 @@ if st.session_state["experiment"] is not None:
736
  while temp_graph is None and (time.time() - start_time) < timeout:
737
  time.sleep(1)
738
 
739
- temp_graph = temp_graph[0]
 
 
 
740
 
741
  for temp in temp_graph:
742
  utc_time = datetime.strptime(temp["x"], "%Y-%m-%dT%H:%M:%S.%fZ")
@@ -745,19 +808,53 @@ if st.session_state["experiment"] is not None:
745
  local_time = utc_time.astimezone(local_tz)
746
  temp["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
747
 
748
- # Filter the data based on the amount of data requested
749
- # Data is in 4 minute intervals
750
- if amount_of_data == "1 hour" and len(temp_graph) > 15:
751
- temp_graph = temp_graph[-15:]
752
- elif amount_of_data == "24 hours" and len(temp_graph) > 360:
753
- temp_graph = temp_graph[-360:]
 
 
 
 
 
 
 
754
 
 
 
 
 
 
 
755
 
 
 
 
756
  df = pd.DataFrame(temp_graph)
757
  df = df.set_index("x")
758
 
 
 
 
 
 
 
 
 
 
759
  placeholder.line_chart(df, x_label="Time", y_label="Temperature (°C)")
 
 
 
 
 
 
 
 
760
 
761
  time.sleep(REQUEST_INTERVAL)
762
 
 
763
 
 
17
  experiment = None
18
  running = []
19
  temp_graph = None
20
+ od_graph = None
21
+ norm_od_graph = None
22
+ growth_rate_graph = None
23
+ all_graph = False
24
 
25
  REQUEST_INTERVAL = 60
26
 
 
103
  payload = message.payload.decode("utf-8")
104
  data = json.loads(payload)
105
  global temp_graph
106
+ global od_graph
107
+ global norm_od_graph
108
+ global growth_rate_graph
109
+ temp_graph = data.get("temperature", None)
110
+ od_graph = data.get("od", None)
111
+ norm_od_graph = data.get("normalized_od", None)
112
+ growth_rate_graph = data.get("growth_rate", None)
113
 
114
  async def publish_temperature_request(client, experiment, filter_mod_N, lookback, reactor):
115
  """Publish a temperature request to the request topic."""
 
123
  payload_str = json.dumps(payload)
124
  client.publish("pioreactor/control", payload_str)
125
 
126
+ async def periodic_temp_reading(client, reactor, experiment, placeholder):
127
  try:
128
+ while True:
129
+ await publish_temperature_request(client, experiment, 1, 100000, reactor)
130
+ global temp_graph
131
+ placeholder.write(temp_graph)
132
+ await asyncio.sleep(REQUEST_INTERVAL)
133
  except asyncio.CancelledError:
134
  pass
135
 
136
+ async def run_mqtt_client(client, placeholder):
137
  client.subscribe(f"pioreactor/{reactor}/temp_reading")
138
  client.subscribe(f"pioreactor/{reactor}/od_reading")
139
  client.subscribe(f"pioreactor/{reactor}/growth_rate")
 
141
  client.on_message = on_message
142
 
143
  client.loop_start()
144
+ await periodic_temp_reading(client, reactor, st.session_state["experiment"], placeholder)
145
 
146
  def get_running_jobs(client, reactor):
147
  payload = {
 
162
 
163
  while experiment is None and (time.time() - start_time) < timeout:
164
  time.sleep(1)
165
+
166
+ async def get_graph(placeholder):
167
+ try:
168
+ st.session_state["client"].loop_start()
169
+ st.session_state["client"].subscribe(f"pioreactor/{reactor}/temperature")
170
+ st.session_state["client"].message_callback_add(f"pioreactor/{reactor}/temperature", on_message)
171
+ while True:
172
+ await publish_temperature_request(st.session_state["client"], st.session_state["experiment"], 1, 100000, reactor)
173
+ await asyncio.sleep(REQUEST_INTERVAL)
174
+ global temp_graph
175
+ placeholder.write(temp_graph)
176
+ except asyncio.CancelledError:
177
+ pass
178
 
179
 
180
  if submit_button:
 
732
 
733
  st.header("Graphs")
734
 
735
+
 
 
 
 
 
 
736
 
737
  st.session_state["client"].loop_start()
738
+ st.session_state["client"].subscribe(f"pioreactor/{reactor}/readings")
739
+ st.session_state["client"].message_callback_add(f"pioreactor/{reactor}/readings", on_message)
 
740
 
741
  temp_graph_actual = []
742
 
743
  st.subheader("Temperature Graph")
744
+ amount_of_data = st.radio("Amount of data", ["1 hour", "24 hours", "All data"], index=1, key="temp")
745
  placeholder = st.empty()
746
 
747
+ st.subheader("OD Reading Graph")
748
+ amount_of_data2 = st.radio("Amount of data", ["1 hour", "24 hours", "All data"], index=1, key="od")
749
+ placeholder2 = st.empty()
750
+
751
+ st.subheader("Normalized OD Reading Graph")
752
+ amount_of_data3 = st.radio("Amount of data", ["1 hour", "24 hours", "All data"], index=1, key="norm_od")
753
+ placeholder3 = st.empty()
754
+
755
+ st.subheader("Growth Rate Graph")
756
+ amount_of_data4 = st.radio("Amount of data", ["1 hour", "24 hours", "All data"], index=1, key="growth_rate")
757
+ placeholder4 = st.empty()
758
+
759
+ # get_graph(placeholder)
760
 
761
  while True:
762
+ if st.session_state.get("df", None) is not None:
763
+ placeholder.line_chart(st.session_state["df"], x_label="Time", y_label="Temperature (°C)")
764
+ if st.session_state.get("df2", None) is not None:
765
+ placeholder2.line_chart(st.session_state["df2"], x_label="Time", y_label="OD Reading")
766
+ if st.session_state.get("df3", None) is not None:
767
+ placeholder3.line_chart(st.session_state["df3"], x_label="Time", y_label="Normalized OD Reading")
768
+ if st.session_state.get("df4", None) is not None:
769
+ placeholder4.line_chart(st.session_state["df4"], x_label="Time", y_label="Growth Rate")
770
+
771
  temp_graph = None
772
+ payload = {
773
+ "command": "get_readings",
774
+ "experiment": st.session_state["experiment"],
775
+ "reactor": reactor,
776
+ "filter_mod": 1,
777
+ "lookback": 100000,
778
+ "filter_mod2": 188,
779
+ "lookback2": 100000,
780
+ "filter_mod3": 188,
781
+ "lookback3": 100000,
782
+ "filter_mod4": 188,
783
+ "lookback4": 100000,
784
+ "amount": amount_of_data,
785
+ "amount2": amount_of_data2,
786
+ "amount3": amount_of_data3,
787
+ "amount4": amount_of_data4
788
+ }
789
 
790
+ payload_str = json.dumps(payload)
791
  st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
792
 
793
  timeout = 30
 
796
  while temp_graph is None and (time.time() - start_time) < timeout:
797
  time.sleep(1)
798
 
799
+ if temp_graph is None or od_graph is None or norm_od_graph is None or growth_rate_graph is None:
800
+ st.error("Failed to retrieve data.")
801
+ st.stop()
802
+
803
 
804
  for temp in temp_graph:
805
  utc_time = datetime.strptime(temp["x"], "%Y-%m-%dT%H:%M:%S.%fZ")
 
808
  local_time = utc_time.astimezone(local_tz)
809
  temp["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
810
 
811
+ for od in od_graph:
812
+ utc_time = datetime.strptime(od["x"], "%Y-%m-%dT%H:%M:%S.%fZ")
813
+ local_tz = pytz.timezone("America/New_York")
814
+ utc_time = utc_time.replace(tzinfo=pytz.utc)
815
+ local_time = utc_time.astimezone(local_tz)
816
+ od["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
817
+
818
+ for norm_od in norm_od_graph:
819
+ utc_time = datetime.strptime(norm_od["x"], "%Y-%m-%dT%H:%M:%S.%fZ")
820
+ local_tz = pytz.timezone("America/New_York")
821
+ utc_time = utc_time.replace(tzinfo=pytz.utc)
822
+ local_time = utc_time.astimezone(local_tz)
823
+ norm_od["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
824
 
825
+ for growth_rate in growth_rate_graph:
826
+ utc_time = datetime.strptime(growth_rate["x"], "%Y-%m-%dT%H:%M:%S.%fZ")
827
+ local_tz = pytz.timezone("America/New_York")
828
+ utc_time = utc_time.replace(tzinfo=pytz.utc)
829
+ local_time = utc_time.astimezone(local_tz)
830
+ growth_rate["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
831
 
832
+ # Filter the data based on the amount of data requested
833
+ # Data is in 4 minute intervals
834
+
835
  df = pd.DataFrame(temp_graph)
836
  df = df.set_index("x")
837
 
838
+ df2 = pd.DataFrame(od_graph)
839
+ df2 = df2.set_index("x")
840
+
841
+ df3 = pd.DataFrame(norm_od_graph)
842
+ df3 = df3.set_index("x")
843
+
844
+ df4 = pd.DataFrame(growth_rate_graph)
845
+ df4 = df4.set_index("x")
846
+
847
  placeholder.line_chart(df, x_label="Time", y_label="Temperature (°C)")
848
+ placeholder2.line_chart(df2, x_label="Time", y_label="OD Reading")
849
+ placeholder3.line_chart(df3, x_label="Time", y_label="Normalized OD Reading")
850
+ placeholder4.line_chart(df4, x_label="Time", y_label="Growth Rate")
851
+
852
+ st.session_state["df"] = df
853
+ st.session_state["df2"] = df2
854
+ st.session_state["df3"] = df3
855
+ st.session_state["df4"] = df4
856
 
857
  time.sleep(REQUEST_INTERVAL)
858
 
859
+
860