diff --git a/demo/plotym.py b/demo/plotym.py index a9d4dafa..d3632455 100644 --- a/demo/plotym.py +++ b/demo/plotym.py @@ -9,6 +9,15 @@ init_simtime_u = "[0.0, 0.0]" init_simtime_ym = "[0.0, 0.0]" ymt = [] + +plt.ion() # enable interactive mode +fig, ax = plt.subplots(1, 1) + +line, = ax.plot([], []) +ax.set_ylabel('ym') +ax.legend(['ym'], loc=0) +ax.set_xlabel('Cycles') + ym = concore.initval(init_simtime_ym) while(concore.simtime= 4: ax.set_xlabel('Cycles') + lines_u.append((ax, line)) + fig_u.tight_layout() +# ------------------------ + u = np.array([concore.initval(init_simtime_u)]).T wallclock1 = time.perf_counter() while(concore.simtime= 4: ax.set_xlabel('Cycles') + lines_u.append((ax, line)) + fig_u.tight_layout() +# ------------------------------------------- + u = np.array([concore.initval(init_simtime_u)]).T wallclock1 = time.perf_counter() while(concore.simtime= 4: ax.set_xlabel('Learn Cycles') + lines_u.append((ax, line)) + fig_u.tight_layout() + + # Setup hrmap figure (Outputs) + fig_ym, (ax_map, ax_hr) = plt.subplots(2, 1) + line_map, = ax_map.plot([], [], label='Learn MAP') + ax_map.set_ylabel('MAP (mmHg)') + ax_map.legend(loc=0) + + line_hr, = ax_hr.plot([], [], label='Learn HR') + ax_hr.set_xlabel('Cycles') + ax_hr.set_ylabel('HR (bpm)') + ax_hr.legend(loc=0) +# ------------------------ + while(concore.simtime oldsimtime: - ut[int(concore.simtime)] = np.array(u).T - ymt[int(concore.simtime)] = np.array(ym).T + curr_idx = int(concore.simtime) + ut[curr_idx] = np.array(u).T + ymt[curr_idx] = np.array(ym).T + + # --- LIVE UPDATE --- + if GENERATE_PLOT == 1: + xdata = range(curr_idx + 1) + + # Update Inputs + for i, (ax, line) in enumerate(lines_u): + line.set_data(xdata, [x[i].item() for x in ut[:curr_idx + 1]]) + ax.relim() + ax.autoscale_view() + + # Update Outputs + line_map.set_data(xdata, [x[0].item() for x in ymt[:curr_idx + 1]]) + line_hr.set_data(xdata, [x[1].item() for x in ymt[:curr_idx + 1]]) + for ax in (ax_map, ax_hr): + ax.relim() + ax.autoscale_view() + + plt.pause(0.001) # Render updates + # ------------------- + oldsimtime = concore.simtime print("retry="+str(concore.retrycount)) ################# -# plot inputs and outputs - +# --- FINAL SAVE & CLEANUP --- if GENERATE_PLOT == 1: - u1 = [x[0].item() for x in ut] - u2 = [x[1].item() for x in ut] - u3 = [x[2].item() for x in ut] - u4 = [x[3].item() for x in ut] - u5 = [x[4].item() for x in ut] - u6 = [x[5].item() for x in ut] - Nsim = len(u1) - plt.figure() - plt.subplot(321) - plt.plot(range(Nsim), u1) - plt.ylabel('Pw1 (s)') - plt.subplot(322) - plt.plot(range(Nsim), u2) - plt.ylabel('Pf1 (Hz)') - plt.subplot(323) - plt.plot(range(Nsim), u3) - plt.xlabel('Learn Cycles') - plt.ylabel('Pw2 (s)') - plt.subplot(324) - plt.plot(range(Nsim), u4) - plt.ylabel('Pf2 (Hz)') - plt.subplot(325) - plt.plot(range(Nsim), u5) - plt.ylabel('Pw3 (s)') - plt.subplot(326) - plt.plot(range(Nsim), u6) - plt.xlabel('Learn Cycles') - plt.ylabel('Pf3 (Hz)') - plt.savefig("stim.pdf") - plt.tight_layout() - - ym1 = [x[0].item() for x in ymt] - ym2 = [x[1].item() for x in ymt] - Nsim = len(ym1) - plt.figure() - plt.subplot(211) - plt.plot(range(Nsim), ym1) - plt.ylabel('MAP (mmHg)') - plt.legend(['Learn MAP'], loc=0) - plt.subplot(212) - plt.plot(range(Nsim), ym2) - plt.xlabel('Cycles') - plt.ylabel('HR (bpm)') - plt.legend(['Learn HR'], loc=0) - plt.savefig("hrmap.pdf") - plt.show() + plt.ioff() + fig_u.savefig("stim.pdf") + fig_ym.savefig("hrmap.pdf") + plt.show() # Keep plot open at the end \ No newline at end of file diff --git a/ratc/learn2.py b/ratc/learn2.py index ae1b3094..28cce3bb 100644 --- a/ratc/learn2.py +++ b/ratc/learn2.py @@ -2,7 +2,8 @@ import numpy as np import matplotlib.pyplot as plt import time -GENERATE_PLOT = 0 + +GENERATE_PLOT = 1 # Set to 1 to enable the live plot fout=open(concore.outpath+'1/history.txt','w') fout2=open('historyfull.txt','a+') concore.delay = 0.002 @@ -14,16 +15,68 @@ ut = (concore.maxtime+1)*[np.array(u).T] ymt = (concore.maxtime+1)*[np.array(ym).T] oldsimtime = concore.simtime + +# --- LIVE PLOT SETUP --- +if GENERATE_PLOT == 1: + plt.ion() # Enable interactive mode + + # Setup stim figure (Inputs) + fig_u, axs_u = plt.subplots(3, 2) + lines_u = [] + labels_u = ['Pw1 (s)', 'Pf1 (Hz)', 'Pw2 (s)', 'Pf2 (Hz)', 'Pw3 (s)', 'Pf3 (Hz)'] + for i, ax in enumerate(axs_u.flat): + line, = ax.plot([], []) + ax.set_ylabel(labels_u[i]) + if i >= 4: ax.set_xlabel('Learn Cycles') + lines_u.append((ax, line)) + fig_u.tight_layout() + + # Setup hrmap figure (Outputs) + fig_ym, (ax_map, ax_hr) = plt.subplots(2, 1) + line_map, = ax_map.plot([], [], label='Learn MAP') + ax_map.set_ylabel('MAP (mmHg)') + ax_map.legend(loc=0) + + line_hr, = ax_hr.plot([], [], label='Learn HR') + ax_hr.set_xlabel('Cycles') + ax_hr.set_ylabel('HR (bpm)') + ax_hr.legend(loc=0) +# ------------------------ + while(concore.simtime oldsimtime: - ut[int(concore.simtime)] = np.array(u).T - ymt[int(concore.simtime)] = np.array(ym).T + curr_idx = int(concore.simtime) + ut[curr_idx] = np.array(u).T + ymt[curr_idx] = np.array(ym).T #fout.write(str(u)+str(ym)+'\n') #fout2.write(str(u)+str(ym)+'\n') + + # --- LIVE UPDATE --- + if GENERATE_PLOT == 1: + xdata = range(curr_idx + 1) + + # Update Inputs + for i, (ax, line) in enumerate(lines_u): + line.set_data(xdata, [x[i].item() for x in ut[:curr_idx + 1]]) + ax.relim() + ax.autoscale_view() + + # Update Outputs + line_map.set_data(xdata, [x[0].item() for x in ymt[:curr_idx + 1]]) + line_hr.set_data(xdata, [x[1].item() for x in ymt[:curr_idx + 1]]) + for ax in (ax_map, ax_hr): + ax.relim() + ax.autoscale_view() + + plt.pause(0.001) # Render updates + # ------------------- + oldsimtime = concore.simtime + print("retry="+str(concore.retrycount)) for i in range(2,concore.maxtime): @@ -33,52 +86,9 @@ fout2.close() ################# -# plot inputs and outputs - +# --- FINAL SAVE & CLEANUP --- if GENERATE_PLOT == 1: - u1 = [x[0].item() for x in ut] - u2 = [x[1].item() for x in ut] - u3 = [x[2].item() for x in ut] - u4 = [x[3].item() for x in ut] - u5 = [x[4].item() for x in ut] - u6 = [x[5].item() for x in ut] - Nsim = len(u1) - plt.figure() - plt.subplot(321) - plt.plot(range(Nsim), u1) - plt.ylabel('Pw1 (s)') - plt.subplot(322) - plt.plot(range(Nsim), u2) - plt.ylabel('Pf1 (Hz)') - plt.subplot(323) - plt.plot(range(Nsim), u3) - plt.xlabel('Learn Cycles') - plt.ylabel('Pw2 (s)') - plt.subplot(324) - plt.plot(range(Nsim), u4) - plt.ylabel('Pf2 (Hz)') - plt.subplot(325) - plt.plot(range(Nsim), u5) - plt.ylabel('Pw3 (s)') - plt.subplot(326) - plt.plot(range(Nsim), u6) - plt.xlabel('Learn Cycles') - plt.ylabel('Pf3 (Hz)') - plt.savefig("stim.pdf") - plt.tight_layout() - - ym1 = [x[0].item() for x in ymt] - ym2 = [x[1].item() for x in ymt] - Nsim = len(ym1) - plt.figure() - plt.subplot(211) - plt.plot(range(Nsim), ym1) - plt.ylabel('MAP (mmHg)') - plt.legend(['Learn MAP'], loc=0) - plt.subplot(212) - plt.plot(range(Nsim), ym2) - plt.xlabel('Cycles') - plt.ylabel('HR (bpm)') - plt.legend(['Learn HR'], loc=0) - plt.savefig("hrmap.pdf") - plt.show() + plt.ioff() + fig_u.savefig("stim.pdf") + fig_ym.savefig("hrmap.pdf") + plt.show() # Keep plot open at the end \ No newline at end of file diff --git a/ratc/learn3.py b/ratc/learn3.py index bbbc5476..3a7824dd 100644 --- a/ratc/learn3.py +++ b/ratc/learn3.py @@ -2,7 +2,8 @@ import numpy as np import matplotlib.pyplot as plt import time -GENERATE_PLOT = 0 + +GENERATE_PLOT = 1 concore.delay = 0.002 concore.simtime = 0 @@ -15,15 +16,65 @@ ym = concore.initval(init_simtime_ym) ut = (concore.maxtime+1)*[np.array(u).T] ymt = (concore.maxtime+1)*[np.array(ym).T] + +# --- LIVE PLOT SETUP --- +if GENERATE_PLOT == 1: + plt.ion() # Enable interactive mode + + # 1. Setup stim figure (Inputs) + fig_u, axs_u = plt.subplots(3, 2) + lines_u = [] + labels_u = ['Pw1 (s)', 'Pf1 (Hz)', 'Pw2 (s)', 'Pf2 (Hz)', 'Pw3 (s)', 'Pf3 (Hz)'] + for i, ax in enumerate(axs_u.flat): + line, = ax.plot([], []) + ax.set_ylabel(labels_u[i]) + if i >= 4: ax.set_xlabel('Learn Cycles') + lines_u.append((ax, line)) + fig_u.tight_layout() + + # 2. Setup hrmap figure (Outputs) + fig_ym, (ax_map, ax_hr) = plt.subplots(2, 1) + line_map, = ax_map.plot([], [], label='Learn MAP') + ax_map.set_ylabel('MAP (mmHg)') + ax_map.legend(loc=0) + + line_hr, = ax_hr.plot([], [], label='Learn HR') + ax_hr.set_xlabel('Cycles') + ax_hr.set_ylabel('HR (bpm)') + ax_hr.legend(loc=0) +# ------------------------ + while(concore.simtime= 4: ax.set_xlabel('Cycles') + lines_u.append((ax, line)) +fig_u.tight_layout() +# ------------------------ + ym = np.array([concore.initval(init_simtime_ym)]).T while(concore.simtime= 4: ax.set_xlabel('Cycles') + lines_u.append(line) +fig_u.tight_layout() + +# Storage arrays for live plotting +map_hist = [] +mhr_hist = [] +u_hist = [] +# ------------------------ + ym = np.array([concore.initval(init_simtime_ym)]).T while(concore.simtime