Skip to content

Commit 866beed

Browse files
committed
add option to see realtime simulation
1 parent cc6ba38 commit 866beed

3 files changed

Lines changed: 169 additions & 68 deletions

File tree

tools/plotu.py

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,82 @@
1212
ut = []
1313
ymt = []
1414
u = concore.initval(init_simtime_u)
15+
16+
# --- Real-time plotting setup ---
17+
realtime = concore.tryparam('realtime', False)
18+
if realtime:
19+
plt.ion()
20+
fig, axs = plt.subplots(3, 2, figsize=(8, 6))
21+
lines = [ax.plot([], [])[0] for ax in axs.flat]
22+
ylabels = ['Pw1 (s)', 'Pf1 (Hz)', 'Pw2 (s)', 'Pf2 (Hz)', 'Pw3 (s)', 'Pf3 (Hz)']
23+
24+
for ax, ylab in zip(axs.flat, ylabels):
25+
ax.set_ylabel(ylab)
26+
axs[2, 0].set_xlabel('Cycles')
27+
axs[2, 1].set_xlabel('Cycles')
28+
plt.tight_layout()
29+
plt.show(block=False)
30+
# --------------------------------
31+
1532
while(concore.simtime<concore.maxtime):
1633
while concore.unchanged():
1734
u = concore.read(1,"u",init_simtime_u)
1835
concore.write(1,"u",u)
1936
logging.debug(f"u={u}")
2037
ut.append(np.array(u).T)
38+
39+
# --- Real-time plot update ---
40+
if realtime and len(u) >= 6:
41+
for i in range(6):
42+
lines[i].set_data(range(len(ut)), [x[i].item() for x in ut])
43+
axs.flat[i].relim()
44+
axs.flat[i].autoscale_view()
45+
fig.canvas.draw()
46+
fig.canvas.flush_events()
47+
# -----------------------------
48+
2149
logging.info(f"retry={concore.retrycount}")
2250

2351
#################
2452

25-
# plot inputs and outputs
26-
u1 = [x[0].item() for x in ut]
27-
u2 = [x[1].item() for x in ut]
28-
u3 = [x[2].item() for x in ut]
29-
u4 = [x[3].item() for x in ut]
30-
u5 = [x[4].item() for x in ut]
31-
u6 = [x[5].item() for x in ut]
32-
33-
Nsim = len(u1)
34-
plt.figure()
35-
plt.subplot(321)
36-
plt.plot(range(Nsim), u1)
37-
plt.ylabel('Pw1 (s)')
38-
plt.subplot(322)
39-
plt.plot(range(Nsim), u2)
40-
plt.ylabel('Pf1 (Hz)')
41-
plt.subplot(323)
42-
plt.plot(range(Nsim), u3)
43-
plt.xlabel('Cycles')
44-
plt.ylabel('Pw2 (s)')
45-
plt.subplot(324)
46-
plt.plot(range(Nsim), u4)
47-
plt.ylabel('Pf2 (Hz)')
48-
plt.subplot(325)
49-
plt.plot(range(Nsim), u5)
50-
plt.ylabel('Pw3 (s)')
51-
plt.subplot(326)
52-
plt.plot(range(Nsim), u6)
53-
plt.xlabel('Cycles')
54-
plt.ylabel('Pf3 (Hz)')
53+
# Finalize rendering
54+
if realtime:
55+
plt.ioff()
56+
else:
57+
# Original static plotting logic
58+
u1 = [x[0].item() for x in ut]
59+
u2 = [x[1].item() for x in ut]
60+
u3 = [x[2].item() for x in ut]
61+
u4 = [x[3].item() for x in ut]
62+
u5 = [x[4].item() for x in ut]
63+
u6 = [x[5].item() for x in ut]
64+
65+
Nsim = len(u1)
66+
plt.figure()
67+
plt.subplot(321)
68+
plt.plot(range(Nsim), u1)
69+
plt.ylabel('Pw1 (s)')
70+
plt.subplot(322)
71+
plt.plot(range(Nsim), u2)
72+
plt.ylabel('Pf1 (Hz)')
73+
plt.subplot(323)
74+
plt.plot(range(Nsim), u3)
75+
plt.xlabel('Cycles')
76+
plt.ylabel('Pw2 (s)')
77+
plt.subplot(324)
78+
plt.plot(range(Nsim), u4)
79+
plt.ylabel('Pf2 (Hz)')
80+
plt.subplot(325)
81+
plt.plot(range(Nsim), u5)
82+
plt.ylabel('Pw3 (s)')
83+
plt.subplot(326)
84+
plt.plot(range(Nsim), u6)
85+
plt.xlabel('Cycles')
86+
plt.ylabel('Pf3 (Hz)')
87+
plt.tight_layout()
88+
89+
# Save and show for both modes
5590
plt.savefig("stim.pdf")
56-
plt.tight_layout()
5791
plt.show()
5892

5993

tools/plotym.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,69 @@
55
import time
66
logging.info("plot ym")
77

8-
concore.delay = 0.005
8+
concore.delay = 0.02
99
concore.default_maxtime(150)
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 = []
10+
init_simtime_u = "[0.0, 0.0]"
11+
init_simtime_ym = "[0.0, 0.0]"
1312
ymt = []
1413
ym = concore.initval(init_simtime_ym)
14+
15+
# 1. Fetch 'realtime' parameter passed from terminal (defaults to False)
16+
realtime = concore.tryparam('realtime', False)
17+
18+
# DEBUG: Check if the parameter was successfully caught
19+
logging.info(f"--- Realtime mode is set to: {realtime} ---")
20+
21+
# 2. Set up interactive plot before the loop if realtime is True
22+
if realtime:
23+
plt.ion() # Turn on interactive mode
24+
fig, ax1 = plt.subplots(1, 1)
25+
line1, = ax1.plot([], [])
26+
27+
ax1.set_ylabel('ym')
28+
ax1.legend(['ym'], loc=0)
29+
ax1.set_xlabel('Cycles')
30+
plt.show(block=False) # Ensure it does not block the script
31+
1532
while(concore.simtime<concore.maxtime):
1633
while concore.unchanged():
1734
ym = concore.read(1,"ym",init_simtime_ym)
1835
concore.write(1,"ym",ym)
1936
logging.debug(f" ym={ym}")
2037
ymt.append(np.array(ym).T)
38+
39+
# 3. Update the plot iteratively during the simulation
40+
if realtime:
41+
ym1 = [x[0].item() for x in ymt]
42+
x_data = range(len(ym1))
43+
44+
line1.set_data(x_data, ym1)
45+
46+
ax1.relim()
47+
ax1.autoscale_view()
48+
49+
# Force the GUI to draw the new data and flush UI events
50+
fig.canvas.draw()
51+
fig.canvas.flush_events()
52+
2153
logging.info(f"retry={concore.retrycount}")
2254

2355
#################
2456

25-
# plot inputs and outputs
26-
ym1 = [x[0].item() for x in ymt]
27-
ym2 = [x[1].item() for x in ymt]
28-
Nsim = len(ym1)
29-
30-
plt.figure()
31-
plt.subplot(211)
32-
plt.plot(range(Nsim), ym1)
33-
plt.ylabel('MAP (mmHg)')
34-
plt.legend(['MAP'], loc=0)
35-
plt.subplot(212)
36-
plt.plot(range(Nsim), ym2)
37-
plt.xlabel('Cycles '+str(concore.params))
38-
plt.ylabel('HR (bpm)')
39-
plt.legend(['HR'], loc=0)
40-
plt.savefig("hrmap.pdf")
41-
plt.show()
57+
# 4. Finalize plotting
58+
if realtime:
59+
plt.ioff() # Turn off interactive mode so the plot stays open at the end
60+
plt.savefig("ym.pdf")
61+
plt.show()
62+
else:
63+
ym1 = [x[0].item() for x in ymt]
64+
65+
Nsim = len(ym1)
66+
plt.figure()
67+
plt.subplot(111)
68+
plt.plot(range(Nsim), ym1)
69+
plt.ylabel('ym')
70+
plt.legend(['ym'], loc=0)
71+
plt.xlabel('Cycles')
72+
plt.savefig("ym.pdf")
73+
plt.show()

tools/plotymlag.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,68 @@
1616
ymt = []
1717
ym = []
1818
for i in range(0,size):
19-
ym.append(concore.initval(init_simtime_ym))
19+
ym.append(concore.initval(init_simtime_ym))
2020
cur = 0
21+
22+
# --- Real-time plotting setup ---
23+
realtime = concore.tryparam('realtime', False)
24+
if realtime:
25+
plt.ion()
26+
fig, axs = plt.subplots(2, 1)
27+
lines = [ax.plot([], [])[0] for ax in axs]
28+
29+
axs[0].set_ylabel('MAP (mmHg)')
30+
axs[0].legend(['MAP'], loc=0)
31+
32+
axs[1].set_xlabel('Cycles ' + str(concore.params))
33+
axs[1].set_ylabel('HR (bpm)')
34+
axs[1].legend(['HR'], loc=0)
35+
plt.tight_layout()
36+
plt.show(block=False)
37+
# --------------------------------
38+
2139
while(concore.simtime<concore.maxtime):
2240
while concore.unchanged():
2341
ym[cur] = concore.read(1,"ym",init_simtime_ym)
2442
concore.write(1,"ym",ym[cur])
2543
logging.debug(f" ym={ym[cur]}")
2644
ymt.append(np.array(ym[(cur-lag) % size]).T)
2745
cur = (cur+1) % size
46+
47+
# --- Real-time plot update ---
48+
if realtime and len(ymt) > 0 and len(ymt[-1]) >= 2:
49+
for i in range(2):
50+
lines[i].set_data(range(len(ymt)), [x[i].item() for x in ymt])
51+
axs[i].relim()
52+
axs[i].autoscale_view()
53+
fig.canvas.draw()
54+
fig.canvas.flush_events()
55+
# -----------------------------
56+
2857
logging.info(f"retry={concore.retrycount}")
2958

3059
#################
3160

3261
# plot inputs and outputs
33-
ym1 = [x[0].item() for x in ymt]
34-
ym2 = [x[1].item() for x in ymt]
35-
Nsim = len(ym1)
36-
37-
plt.figure()
38-
plt.subplot(211)
39-
plt.plot(range(Nsim), ym1)
40-
plt.ylabel('MAP (mmHg)')
41-
plt.legend(['MAP'], loc=0)
42-
plt.subplot(212)
43-
plt.plot(range(Nsim), ym2)
44-
plt.xlabel('Cycles '+str(concore.params))
45-
plt.ylabel('HR (bpm)')
46-
plt.legend(['HR'], loc=0)
62+
if realtime:
63+
plt.ioff()
64+
else:
65+
# Original static plotting logic
66+
ym1 = [x[0].item() for x in ymt]
67+
ym2 = [x[1].item() for x in ymt]
68+
Nsim = len(ym1)
69+
70+
plt.figure()
71+
plt.subplot(211)
72+
plt.plot(range(Nsim), ym1)
73+
plt.ylabel('MAP (mmHg)')
74+
plt.legend(['MAP'], loc=0)
75+
plt.subplot(212)
76+
plt.plot(range(Nsim), ym2)
77+
plt.xlabel('Cycles '+str(concore.params))
78+
plt.ylabel('HR (bpm)')
79+
plt.legend(['HR'], loc=0)
80+
plt.tight_layout()
81+
4782
plt.savefig("hrmap.pdf")
48-
plt.show()
83+
plt.show()

0 commit comments

Comments
 (0)