sgbaird commited on
Commit
113b2f8
1 Parent(s): a662a5b

chore: Add function to plot spectra in app.py

Browse files
Files changed (3) hide show
  1. app.py +65 -1
  2. requirements.txt +1 -0
  3. scripts/plotting.py +77 -0
app.py CHANGED
@@ -3,6 +3,9 @@ import queue
3
 
4
  import paho.mqtt.client as mqtt
5
  import streamlit as st
 
 
 
6
 
7
  # Initialize Streamlit app
8
  st.title("Light-mixing Control Panel")
@@ -96,12 +99,72 @@ def send_and_receive(client, command_topic, msg, queue_timeout=60):
96
  return sensor_data
97
 
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  # Publish button
100
- if st.button("Send RGB Command") and not st.session_state.locked:
101
  if not PICO_ID or not HIVEMQ_HOST or not HIVEMQ_USERNAME or not HIVEMQ_PASSWORD:
102
  st.error("Please enter all required fields.")
103
  else:
104
  st.session_state.locked = True
 
105
 
106
  client = get_paho_client(
107
  sensor_data_topic,
@@ -119,6 +182,7 @@ if st.button("Send RGB Command") and not st.session_state.locked:
119
 
120
  st.session_state.locked = False
121
  st.success("Command sent successfully!")
 
122
  st.write("Sensor Data Received:", sensor_data)
123
 
124
  # Display received messages
 
3
 
4
  import paho.mqtt.client as mqtt
5
  import streamlit as st
6
+ import matplotlib.pyplot as plt
7
+ import numpy as np
8
+ from matplotlib.patches import Rectangle
9
 
10
  # Initialize Streamlit app
11
  st.title("Light-mixing Control Panel")
 
99
  return sensor_data
100
 
101
 
102
+ # Function to plot spectra
103
+ def plot_spectra(sensor_data):
104
+ """https://chatgpt.com/share/210d2fee-ca64-45a5-866e-e6df6e56bd1c"""
105
+ wavelengths = np.array([410, 440, 470, 510, 550, 583, 620, 670])
106
+ intensities = np.array(
107
+ [
108
+ sensor_data["ch410"],
109
+ sensor_data["ch440"],
110
+ sensor_data["ch470"],
111
+ sensor_data["ch510"],
112
+ sensor_data["ch550"],
113
+ sensor_data["ch583"],
114
+ sensor_data["ch620"],
115
+ sensor_data["ch670"],
116
+ ]
117
+ )
118
+
119
+ fig, ax = plt.subplots(figsize=(10, 6))
120
+
121
+ num_points = 100 # for "fake" color bar effect
122
+
123
+ # Adding rectangles for color bar effect
124
+ dense_wavelengths = np.linspace(wavelengths.min(), wavelengths.max(), num_points)
125
+ rect_height = max(intensities) * 0.02 # Height of the rectangles
126
+
127
+ for dw in dense_wavelengths:
128
+ rect = Rectangle(
129
+ (
130
+ dw - (wavelengths.max() - wavelengths.min()) / num_points / 2,
131
+ -rect_height * 2,
132
+ ),
133
+ (wavelengths.max() - wavelengths.min()) / num_points,
134
+ rect_height * 3,
135
+ color=plt.cm.rainbow(
136
+ (dw - wavelengths.min()) / (wavelengths.max() - wavelengths.min())
137
+ ),
138
+ edgecolor="none",
139
+ )
140
+ ax.add_patch(rect)
141
+
142
+ # Main scatter plot
143
+ scatter = ax.scatter(
144
+ wavelengths, intensities, c=wavelengths, cmap="rainbow", edgecolor="k"
145
+ )
146
+
147
+ # Adding vertical lines from the x-axis to each point
148
+ for wavelength, intensity in zip(wavelengths, intensities):
149
+ ax.vlines(wavelength, 0, intensity, color="gray", linestyle="--", linewidth=1)
150
+
151
+ ax.set_xlim(wavelengths.min() - 10, wavelengths.max() + 10)
152
+ ax.set_ylim(0, max(intensities) + 10) # Ensure the lower y limit is 0
153
+ ax.set_xticks(wavelengths)
154
+ ax.set_xlabel("Wavelength (nm)")
155
+ ax.set_ylabel("Intensity")
156
+ ax.set_title("Spectral Intensity vs. Wavelength")
157
+
158
+ plt.show()
159
+
160
+
161
  # Publish button
162
+ if st.button("Send RGB Command", disabled=st.session_state.locked):
163
  if not PICO_ID or not HIVEMQ_HOST or not HIVEMQ_USERNAME or not HIVEMQ_PASSWORD:
164
  st.error("Please enter all required fields.")
165
  else:
166
  st.session_state.locked = True
167
+ st.success("Please wait while the command is sent...")
168
 
169
  client = get_paho_client(
170
  sensor_data_topic,
 
182
 
183
  st.session_state.locked = False
184
  st.success("Command sent successfully!")
185
+ plot_spectra(sensor_data)
186
  st.write("Sensor Data Received:", sensor_data)
187
 
188
  # Display received messages
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  paho-mqtt
 
2
  streamlit
 
1
  paho-mqtt
2
+ matplotlib
3
  streamlit
scripts/plotting.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """https://chatgpt.com/share/210d2fee-ca64-45a5-866e-e6df6e56bd1c"""
2
+
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from matplotlib.patches import Rectangle
6
+
7
+
8
+ # Function to plot spectra
9
+ def plot_spectra(sensor_data, num_points=100):
10
+ wavelengths = np.array([410, 440, 470, 510, 550, 583, 620, 670])
11
+ intensities = np.array(
12
+ [
13
+ sensor_data["ch410"],
14
+ sensor_data["ch440"],
15
+ sensor_data["ch470"],
16
+ sensor_data["ch510"],
17
+ sensor_data["ch550"],
18
+ sensor_data["ch583"],
19
+ sensor_data["ch620"],
20
+ sensor_data["ch670"],
21
+ ]
22
+ )
23
+
24
+ fig, ax = plt.subplots(figsize=(10, 6))
25
+
26
+ # Adding rectangles for color bar effect
27
+ dense_wavelengths = np.linspace(wavelengths.min(), wavelengths.max(), num_points)
28
+ rect_height = max(intensities) * 0.02 # Height of the rectangles
29
+
30
+ for dw in dense_wavelengths:
31
+ rect = Rectangle(
32
+ (
33
+ dw - (wavelengths.max() - wavelengths.min()) / num_points / 2,
34
+ -rect_height * 2,
35
+ ),
36
+ (wavelengths.max() - wavelengths.min()) / num_points,
37
+ rect_height * 3,
38
+ color=plt.cm.rainbow(
39
+ (dw - wavelengths.min()) / (wavelengths.max() - wavelengths.min())
40
+ ),
41
+ edgecolor="none",
42
+ )
43
+ ax.add_patch(rect)
44
+
45
+ # Main scatter plot
46
+ scatter = ax.scatter(
47
+ wavelengths, intensities, c=wavelengths, cmap="rainbow", edgecolor="k"
48
+ )
49
+
50
+ # Adding vertical lines from the x-axis to each point
51
+ for wavelength, intensity in zip(wavelengths, intensities):
52
+ ax.vlines(wavelength, 0, intensity, color="gray", linestyle="--", linewidth=1)
53
+
54
+ ax.set_xlim(wavelengths.min() - 10, wavelengths.max() + 10)
55
+ ax.set_ylim(0, max(intensities) + 10) # Ensure the lower y limit is 0
56
+ ax.set_xticks(wavelengths)
57
+ ax.set_xlabel("Wavelength (nm)")
58
+ ax.set_ylabel("Intensity")
59
+ ax.set_title("Spectral Intensity vs. Wavelength")
60
+
61
+ plt.show()
62
+
63
+
64
+ # Simulated sensor data
65
+ sensor_data = {
66
+ "ch410": 10,
67
+ "ch440": 20,
68
+ "ch470": 15,
69
+ "ch510": 30,
70
+ "ch550": 25,
71
+ "ch583": 40,
72
+ "ch620": 35,
73
+ "ch670": 50,
74
+ }
75
+
76
+ # Run the plot function with 100 points for the rectangles
77
+ plot_spectra(sensor_data, num_points=100)