Skip to content

Commit 2ecf0fc

Browse files
committed
update static end result plotting to real time plotting
1 parent e15cf19 commit 2ecf0fc

16 files changed

Lines changed: 747 additions & 523 deletions

File tree

demo/plotym.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,38 @@
99
init_simtime_u = "[0.0, 0.0]"
1010
init_simtime_ym = "[0.0, 0.0]"
1111
ymt = []
12+
13+
plt.ion() # enable interactive mode
14+
fig, ax = plt.subplots(1, 1)
15+
16+
line, = ax.plot([], [])
17+
ax.set_ylabel('ym')
18+
ax.legend(['ym'], loc=0)
19+
ax.set_xlabel('Cycles')
20+
1221
ym = concore.initval(init_simtime_ym)
1322
while(concore.simtime<concore.maxtime):
1423
while concore.unchanged():
1524
ym = concore.read(1,"ym",init_simtime_ym)
1625
concore.write(1,"ym",ym)
1726
print("ym="+str(ym))
1827
ymt.append(np.array(ym).T)
28+
29+
#real-time update
30+
Nsim = len(ymt)
31+
32+
#update line dynamically
33+
line.set_data(range(Nsim), [x[0].item() for x in ymt])
34+
ax.relim()
35+
ax.autoscale_view()
36+
37+
plt.pause(0.001) # Render update
38+
1939
print("retry="+str(concore.retrycount))
2040

2141
#################
2242

23-
# plot inputs and outputs
24-
ym1 = [x[0].item() for x in ymt]
25-
26-
Nsim = len(ym1)
27-
plt.figure()
28-
plt.subplot(111)
29-
plt.plot(range(Nsim), ym1)
30-
plt.ylabel('ym')
31-
plt.legend(['ym'], loc=0)
32-
plt.xlabel('Cycles')
43+
# Final Save & cleanup
44+
plt.ioff()
3345
plt.savefig("ym.pdf")
3446
plt.show()

example/plotym.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
11
import concore
2+
import logging
23
import numpy as np
34
import matplotlib.pyplot as plt
45
import time
5-
print("plotym")
6+
logging.info("plot ym")
67

7-
concore.delay = 0.02
8+
concore.delay = 0.005
89
concore.default_maxtime(150)
9-
init_simtime_u = "[0.0, 0.0]"
10-
init_simtime_ym = "[0.0, 0.0]"
10+
init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
11+
init_simtime_ym = "[0.0, 0.0, 0.0]"
12+
ut = []
1113
ymt = []
14+
plt.ion() # Enable interactive mode
15+
fig, (ax1, ax2) = plt.subplots(2, 1)
16+
17+
line1, = ax1.plot([], [])
18+
ax1.set_ylabel('MAP (mmHg)')
19+
ax1.legend(['MAP'], loc=0)
20+
21+
line2, = ax2.plot([], [])
22+
ax2.set_xlabel('Cycles '+str(concore.params))
23+
ax2.set_ylabel('HR (bpm)')
24+
ax2.legend(['HR'], loc=0)
25+
1226
ym = concore.initval(init_simtime_ym)
1327
while(concore.simtime<concore.maxtime):
1428
while concore.unchanged():
1529
ym = concore.read(1,"ym",init_simtime_ym)
1630
concore.write(1,"ym",ym)
17-
print("ym="+str(ym))
31+
logging.debug(f" ym={ym}")
1832
ymt.append(np.array(ym).T)
19-
print("retry="+str(concore.retrycount))
33+
34+
#real-time update
35+
Nsim = len(ymt)
36+
xdata = range(Nsim)
37+
38+
# Extract columns and update lines directly
39+
line1.set_data(xdata, [x[0].item() for x in ymt])
40+
line2.set_data(xdata, [x[1].item() for x in ymt])
41+
42+
for ax in (ax1, ax2):
43+
ax.relim()
44+
ax.autoscale_view()
45+
46+
plt.pause(0.001) # Render update
47+
48+
logging.info(f"retry={concore.retrycount}")
2049

2150
#################
2251

23-
# plot inputs and outputs
24-
ym1 = [x[0].item() for x in ymt]
25-
26-
Nsim = len(ym1)
27-
plt.figure()
28-
plt.subplot(111)
29-
plt.plot(range(Nsim), ym1)
30-
plt.ylabel('ym')
31-
plt.legend(['ym'], loc=0)
32-
plt.xlabel('Cycles')
33-
plt.savefig("ym.pdf")
52+
# Final Save & cleanup
53+
plt.ioff()
54+
plt.savefig("hrmap.pdf")
3455
plt.show()

ratc/cvxpymatcore.py

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,36 @@ def MPC(x0, u, y, Pd, X):
118118
concore.delay = 0.02
119119
init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
120120
init_simtime_ym = "[0.0, 0.0, 0.0]"
121+
122+
# --- LIVE PLOT SETUP ---
123+
if GENERATE_PLOT == 1:
124+
plt.ion() # Enable interactive mode
125+
126+
# 1. Setup hrmap figure (Outputs & Setpoints)
127+
fig_ym, (ax_map, ax_hr) = plt.subplots(2, 1)
128+
line_map_m, = ax_map.plot([], [], label='MAPm')
129+
line_map_sp, = ax_map.plot([], [], label='MAPsp')
130+
ax_map.set_ylabel('MAP (mmHg)')
131+
ax_map.legend(loc=0)
132+
133+
line_hr_m, = ax_hr.plot([], [], label='HRm')
134+
line_hr_sp, = ax_hr.plot([], [], label='HRsp')
135+
ax_hr.set_xlabel('Cycles')
136+
ax_hr.set_ylabel('HR (bpm)')
137+
ax_hr.legend(loc=0)
138+
139+
# 2. Setup stim figure (Inputs)
140+
fig_u, axs_u = plt.subplots(3, 2)
141+
lines_u = []
142+
labels_u = ['Pw1 (s)', 'Pf1 (Hz)', 'Pw2 (s)', 'Pf2 (Hz)', 'Pw3 (s)', 'Pf3 (Hz)']
143+
for i, ax in enumerate(axs_u.flat):
144+
line, = ax.plot([], [])
145+
ax.set_ylabel(labels_u[i])
146+
if i >= 4: ax.set_xlabel('Cycles')
147+
lines_u.append((ax, line))
148+
fig_u.tight_layout()
149+
# ------------------------
150+
121151
u = np.array([concore.initval(init_simtime_u)]).T
122152
wallclock1 = time.perf_counter()
123153
while(concore.simtime<concore.maxtime):
@@ -129,8 +159,34 @@ def MPC(x0, u, y, Pd, X):
129159
ymt.append(ym)
130160
u, xc, Pd = MPC(xc, u, ym, Pd, X)
131161
#################
162+
163+
# --- LIVE UPDATE ---
164+
if GENERATE_PLOT == 1:
165+
Nsim = len(ymt)
166+
xdata = range(Nsim)
167+
168+
# Update hrmap figure
169+
line_map_m.set_data(xdata, [x[0].item() for x in ymt])
170+
line_map_sp.set_data(xdata, np.tile(X['ysp'][0], Nsim))
171+
line_hr_m.set_data(xdata, [x[1].item() for x in ymt])
172+
line_hr_sp.set_data(xdata, np.tile(X['ysp'][1], Nsim))
173+
174+
for ax in (ax_map, ax_hr):
175+
ax.relim()
176+
ax.autoscale_view()
177+
178+
# Update stim figure
179+
for i, (ax, line) in enumerate(lines_u):
180+
line.set_data(xdata, [x[i].item() for x in ut])
181+
ax.relim()
182+
ax.autoscale_view()
183+
184+
plt.pause(0.001) # Render updates
185+
# -------------------
186+
132187
print("ym="+str(ym)+" u="+str(u));
133188
concore.write(1,"u",list(u.T[0]));
189+
134190
wallclock2 = time.perf_counter()
135191
#concore.write(1,"u",init_simtime_u)
136192
print("retry="+str(concore.retrycount))
@@ -139,55 +195,8 @@ def MPC(x0, u, y, Pd, X):
139195
if GENERATE_PLOT == 0:
140196
quit()
141197

142-
# plot inputs and outputs
143-
ym1 = [x[0].item() for x in ymt]
144-
ym2 = [x[1].item() for x in ymt]
145-
u1 = [x[0].item() for x in ut]
146-
u2 = [x[1].item() for x in ut]
147-
u3 = [x[2].item() for x in ut]
148-
u4 = [x[3].item() for x in ut]
149-
u5 = [x[4].item() for x in ut]
150-
u6 = [x[5].item() for x in ut]
151-
152-
Nsim = len(ym1)
153-
plt.figure()
154-
plt.subplot(211)
155-
plt.plot(range(Nsim), ym1)
156-
plt.plot(range(Nsim), np.tile(X['ysp'][0], Nsim))
157-
plt.ylabel('MAP (mmHg)')
158-
plt.legend(['MAPm', 'MAPsp'], loc=0)
159-
plt.subplot(212)
160-
plt.plot(range(Nsim), ym2)
161-
plt.plot(range(Nsim), np.tile(X['ysp'][1], Nsim))
162-
plt.xlabel('Cycles')
163-
plt.ylabel('HR (bpm)')
164-
plt.legend(['HRm', 'HRsp'], loc=0)
165-
plt.savefig("hrmap.pdf")
166-
#plt.tight_layout()
167-
168-
plt.figure()
169-
plt.subplot(321)
170-
plt.plot(range(Nsim), u1)
171-
plt.ylabel('Pw1 (s)')
172-
plt.subplot(322)
173-
plt.plot(range(Nsim), u2)
174-
plt.ylabel('Pf1 (Hz)')
175-
plt.subplot(323)
176-
plt.plot(range(Nsim), u3)
177-
plt.xlabel('Cycles')
178-
plt.ylabel('Pw2 (s)')
179-
plt.subplot(324)
180-
plt.plot(range(Nsim), u4)
181-
plt.ylabel('Pf2 (Hz)')
182-
plt.subplot(325)
183-
plt.plot(range(Nsim), u5)
184-
plt.ylabel('Pw3 (s)')
185-
plt.subplot(326)
186-
plt.plot(range(Nsim), u6)
187-
plt.xlabel('Cycles')
188-
plt.ylabel('Pf3 (Hz)')
189-
plt.savefig("stim.pdf")
190-
plt.tight_layout()
191-
plt.show()
192-
193-
198+
# --- FINAL SAVE & CLEANUP ---
199+
plt.ioff()
200+
fig_ym.savefig("hrmap.pdf")
201+
fig_u.savefig("stim.pdf")
202+
plt.show() # Keep plots open at the end

0 commit comments

Comments
 (0)