def plot_ts(x, y, title='', savefig=None): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure(figsize=(10, 3)) ax = fig.add_subplot((111)) x = x.round(2) #n = y_lin[0] # zero at the begining n = y_lin[0] + (y_lin[-1] - y_lin[0]) / 2. # zero at the middle plt.plot(x, y - n, linewidth=3) #plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin - n, 'r') viz.add_inner_title(ax, 'dh/dt = %.1f cm/yr' % (m * 100), loc=1) viz.add_inner_title(ax, title, loc=2) plt.ylim(-.3, .3) plt.xlim(2003.5, 2010.1) #plt.xlabel('years') plt.ylabel('Elevation change (m)') #fig.autofmt_xdate() if savefig is not None: pass #plt.savefig(savefig+'_ts.png') #np.savetxt(savefig+'_ts.txt', np.column_stack((x, y))) return fig
def plot_ts(table): """ Plot dh and dAGC time series and the correlation dAGC x dh. """ sys.path.append('/Users/fpaolo/code/misc') from util import poly_fit # load data from Table time2 = table.cols.time2[:] month = table.cols.month[:] dh_mean = table.cols.dh_mean[:] dh_error = table.cols.dh_error[:] dg_mean = table.cols.dg_mean[:] dg_error = table.cols.dg_error[:] dates = [dt.datetime(y, m, 15) for y, m in zip(time2, month)] # plot TS fig = plt.figure() plt.subplot(211) plt.errorbar(dates, dh_mean, yerr=dh_error, linewidth=2) plt.ylabel('dh (m)') plt.subplot(212) plt.errorbar(dates, dg_mean, yerr=dg_error, linewidth=2) plt.ylabel('dAGC (dB)') fig.autofmt_xdate() # plot correlation dg_fit, dh_fit, _ = poly_fit(dg_mean, dh_mean) plt.figure() plt.plot(dg_mean, dh_mean, 'o') plt.plot(dg_fit, dh_fit, linewidth=2.5) plt.xlabel('dAGC (dB)') plt.ylabel('dh (m)') corr = np.corrcoef(dg_mean, dh_mean)[0,1] print 'correlation = %.2f' % corr
def plot_ts(x, y, title='', savefig=None): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure(figsize=(10,3)) ax = fig.add_subplot((111)) x = x.round(2) #n = y_lin[0] # zero at the begining n = y_lin[0] + (y_lin[-1] - y_lin[0])/2. # zero at the middle plt.plot(x, y-n, linewidth=3) #plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin-n, 'r') viz.add_inner_title(ax, 'dh/dt = %.1f cm/yr' % (m*100), loc=1) viz.add_inner_title(ax, title, loc=2) plt.ylim(-.3, .3) plt.xlim(2003.5, 2010.1) #plt.xlabel('years') plt.ylabel('Elevation change (m)') #fig.autofmt_xdate() if savefig is not None: pass #plt.savefig(savefig+'_ts.png') #np.savetxt(savefig+'_ts.txt', np.column_stack((x, y))) return fig
def plot_ts(x, y, title='', savefig=None): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure(figsize=(10,3)) ax = fig.add_subplot((111)) x = x.round(2) #n = y_lin[0] # reference to zero nmin = y_lin.min() # reference to center nmax = y_lin.max() n = nmin + (nmax - nmin)/2. plt.plot(x, y-n, linewidth=3) plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin-n) viz.add_inner_title(ax, 'dh/dt = %.1f cm/yr' % (m*100), loc=4) #viz.add_inner_title(ax, 'dAGC/dt = %.1f dB/yr' % (m*100), loc=4) viz.add_inner_title(ax, title, loc=1) #plt.ylim(-3.5, 3.5) plt.xlim(1992, 2012.7) ax.xaxis.set_ticks(range(1992, 2013, 2)) #plt.xlabel('time') plt.ylabel('dh [m]') #plt.ylabel('dAGC [dB]') fig.autofmt_xdate() if savefig is not None: plt.savefig(savefig) plt.show() return fig
def plot_ts(x, y, title='', savefig=None): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure(figsize=(10,3)) ax = fig.add_subplot((111)) x = x.round(2) n = y_lin[0] # reference to zero plt.plot(x, y-n, linewidth=3) #plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin-n, 'r') viz.add_inner_title(ax, 'dh/dt = %.1f cm/yr' % (m*100), loc=4) viz.add_inner_title(ax, title, loc=2) #plt.ylim(-0.1, 0.1) plt.xlim(2003.5, 2009.6) #plt.xlabel('time') plt.ylabel('dh (m)') #fig.autofmt_xdate() if savefig is not None: plt.savefig(savefig) plt.show() return fig
def plot_ts(x, y, title='', savefig=None): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure(figsize=(10, 3)) ax = fig.add_subplot((111)) x = x.round(2) #n = y_lin[0] # reference to zero nmin = y_lin.min() # reference to center nmax = y_lin.max() n = nmin + (nmax - nmin) / 2. plt.plot(x, y - n, linewidth=3) plt.plot(x_pol, y_pol - n) plt.plot(x_lin, y_lin - n) viz.add_inner_title(ax, 'dh/dt = %.1f cm/yr' % (m * 100), loc=4) #viz.add_inner_title(ax, 'dAGC/dt = %.1f dB/yr' % (m*100), loc=4) viz.add_inner_title(ax, title, loc=1) #plt.ylim(-3.5, 3.5) plt.xlim(1992, 2012.7) ax.xaxis.set_ticks(range(1992, 2013, 2)) #plt.xlabel('time') plt.ylabel('dh [m]') #plt.ylabel('dAGC [dB]') fig.autofmt_xdate() if savefig is not None: plt.savefig(savefig) plt.show() return fig
def plot_ts(table): """ Plot dh and dAGC time series and the correlation dAGC x dh. """ sys.path.append('/Users/fpaolo/code/misc') from util import poly_fit # load data from Table time2 = table.cols.time2[:] month = table.cols.month[:] dh_mean = table.cols.dh_mean[:] dh_error = table.cols.dh_error[:] dg_mean = table.cols.dg_mean[:] dg_error = table.cols.dg_error[:] dates = [dt.datetime(y, m, 15) for y, m in zip(time2, month)] # plot TS fig = plt.figure() plt.subplot(211) plt.errorbar(dates, dh_mean, yerr=dh_error, linewidth=2) plt.ylabel('dh (m)') plt.subplot(212) plt.errorbar(dates, dg_mean, yerr=dg_error, linewidth=2) plt.ylabel('dAGC (dB)') fig.autofmt_xdate() # plot correlation dg_fit, dh_fit, _ = poly_fit(dg_mean, dh_mean) plt.figure() plt.plot(dg_mean, dh_mean, 'o') plt.plot(dg_fit, dh_fit, linewidth=2.5) plt.xlabel('dAGC (dB)') plt.ylabel('dh (m)') corr = np.corrcoef(dg_mean, dh_mean)[0, 1] print 'correlation = %.2f' % corr
def plot_ts(ax, x, y, num=1): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) x = x.round(2) n = y_lin[0] # reference to zero ax.plot(x, y-n, linewidth=2.5) #ax.plot(x_pol, y_pol-n) ax.plot(x_lin, y_lin-n, 'r') if num != 7: viz.add_inner_title(ax, '(%d)' % num, loc=2) viz.add_inner_title(ax, 'trend = %.1f cm/yr' % (m*100), loc=3) return ax
def plot_ts(x, y): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure() ax = fig.add_subplot((111)) x = x.round(2) n = y_lin[0] # reference to zero plt.plot(x, y-n, linewidth=2) plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin-n) viz.add_inner_title(ax, 'linear trend = %.1f cm/yr' % (m*100), loc=2) #plt.ylim(-0.8, 0.8) plt.xlabel('time') plt.ylabel('dh (m)') plt.show() return fig
def plot_ts(x, y): x_pol, y_pol = util.poly_fit(x, y, order=3) x_lin, y_lin = linear_fit(x, y) m, c = linear_fit(x, y, return_coef=True) fig = plt.figure() ax = fig.add_subplot((111)) x = x.round(2) n = y_lin[0] # reference to zero plt.plot(x, y - n, linewidth=2) plt.plot(x_pol, y_pol - n) plt.plot(x_lin, y_lin - n) viz.add_inner_title(ax, 'linear trend = %.1f cm/yr' % (m * 100), loc=2) #plt.ylim(-0.8, 0.8) plt.xlabel('time') plt.ylabel('dh (m)') plt.show() return fig
def plot_ts_mean(t, Y): y = np.ma.masked_invalid(Y) ts = np.mean(np.mean(y, axis=1), axis=1) #x_pol, y_pol = spline_interp(t, ts, smooth=0.1) x_pol, y_pol = util.poly_fit(t, ts, order=3) x_lin, y_lin = linear_fit(t, ts) m, c = linear_fit(t, ts, return_coef=True) fig = plt.figure() ax = fig.add_subplot((111)) n = y_lin[0] # shift plt.plot(t, ts-n, linewidth=2) plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin-n) viz.add_inner_title(ax, 'linear trend = %.1f cm/yr' % (m*100), loc=2) #plt.ylim(ts.min(), ts.max()) plt.xlabel('time') plt.ylabel('dh (m)') plt.title('Mean TS') plt.show() return fig
def plot_ts_mean(t, Y): y = np.ma.masked_invalid(Y) ts = np.mean(np.mean(y, axis=1), axis=1) #x_pol, y_pol = spline_interp(t, ts, smooth=0.1) x_pol, y_pol = util.poly_fit(t, ts, order=3) x_lin, y_lin = linear_fit(t, ts) m, c = linear_fit(t, ts, return_coef=True) fig = plt.figure() ax = fig.add_subplot((111)) n = y_lin[0] # shift plt.plot(t, ts - n, linewidth=2) plt.plot(x_pol, y_pol - n) plt.plot(x_lin, y_lin - n) viz.add_inner_title(ax, 'linear trend = %.1f cm/yr' % (m * 100), loc=2) #plt.ylim(ts.min(), ts.max()) plt.xlabel('time') plt.ylabel('dh (m)') plt.title('Mean TS') plt.show() return fig
#plt.ylim(ts.min(), ts.max()) plt.xlabel('time') plt.ylabel('dh (m)') plt.title('Mean TS') plt.show() return fig def plot_ts_full(t, Y, (i,j)): y = np.ma.masked_invalid(Y) mean_t = np.mean(np.mean(y, axis=1), axis=1) mean_xy = np.mean(y, axis=0) ts = mean_t + mean_xy[i,j] + Y[:,i,j] #x_pol, y_pol = spline_interp(t, ts, smooth=0.1) x_pol, y_pol = util.poly_fit(t, ts, order=3) x_lin, y_lin = linear_fit(t, ts) m, c = linear_fit(t, ts, return_coef=True) fig = plt.figure() ax = fig.add_subplot((111)) n = y_lin[0] # shift plt.plot(t, ts-n, linewidth=2) plt.plot(x_pol, y_pol-n) plt.plot(x_lin, y_lin-n) viz.add_inner_title(ax, 'linear trend = %.1f cm/yr' % (m*100), loc=2) #plt.ylim(ts.min(), ts.max()) plt.xlabel('time') plt.ylabel('dh (m)')
def get_arterial_compliance( flow, low_low_pass_gage, low_pass_gage, amb_temperature, amb_pressure, MAP=None, SBP=None, DBP=None, cba=None ): pressure = amb_pressure + low_pass_gage flow_fit, cba = flow_of_gage(flow, low_low_pass_gage, cba) corrected_flow = flow_correction(flow_fit, low_low_pass_gage, amb_pressure, amb_temperature) t = arange(len(corrected_flow)) * defaults["dt"] wvol = cumsum(flow_fit) * defaults["dt"] / 60.0 ### best fit t = arange(len(wvol)) * defaults["dt"] keep = low_low_pass_gage < 30 x = flow / 60.0 y = low_low_pass_gage k = 3.0 A = x ** k b = dot(A, y) / dot(A, A) yfit = A * b xfit = (y / b) ** (1.0 / k) t = arange(len(y)) * defaults["dt"] my_gage = arange(160) c, b, a = cba cuff_compl = corrected_flow[:-1] * (defaults["dt"] / diff(low_low_pass_gage)) / 60.0 bpf = low_pass_gage - low_low_pass_gage v_of_p = interp.interp1d(low_low_pass_gage[::-1], wvol[::-1], bounds_error=False) peaks, troughs = util.find_pulse_peaks_and_troughs(bpf, 1, 0.005) if peaks[0] < troughs[0]: # peak follows trough peaks = peaks[1:] if troughs[-1] > peaks[-1]: # peak follows trough troughs = troughs[:-1] hills = [] for trough, peak in zip(troughs, peaks): ## filter loong pulses if (peak - trough) * defaults["dt"] < MAX_PULSE: hills.append((trough, peak)) assert len(peaks) == len(troughs) hills = array(hills) troughs = hills[:, 0] peaks = hills[:, 1] delta_vs = [] deltav_ps = [] deltav_ts = [] cuff_pressures = [] for t, p in hills: cuff_pressure = (low_low_pass_gage[p] + low_low_pass_gage[t]) / 2.0 delta_v = ideal_gas_compliance(low_low_pass_gage[p], wvol[-1]) * (bpf[p] - bpf[t]) delta_vs.append(delta_v) cuff_pressures.append(cuff_pressure) p = arange(v_of_p.x[0], v_of_p.x[-1], 1) dv_dp_cuff = diff(v_of_p(p)) / diff(p) A = 1.0 / p[1:] y = dv_dp_cuff c = dot(A, y) / dot(A, A) ### find largest delta for p6 fit. deltas = bpf[peaks] - bpf[troughs] candidate_deltas = [d for idx, d in zip(peaks, deltas) if low_low_pass_gage[idx] > 60] ## MAP must be above 60mmhg. n_peak = argmax(candidate_deltas) * 2 + 1 # n_peak = len(peaks) - 1 if n_peak >= len(peaks): n_peak = len(peaks) # n_peak = argmax(bpf[peaks] - bpf[troughs]) * 3 if n_peak < 15: if len(peaks) < 15: n_peak = len(peaks) - 1 else: n_peak = 15 assert n_peak <= len(peaks) deltas = bpf[peaks] - bpf[troughs] idx = arange(len(deltas)) # plot(idx, deltas) if False: ## filter out noisy peaks? keep = util.filter_deltas(idx, deltas, 0.3) else: # keep all keep = idx # plot(keep, deltas[keep]) hills = hills[keep] troughs = troughs[keep] peaks = peaks[keep] deltas = deltas[keep] delta_vs = array(delta_vs)[keep] max_delta_i = argmax(deltas) if MAP is None: # MAP = low_pass_gage[hills[max_delta_i][1]] ### compute map6 MAP_ii = argmax(deltas) MAP_i = hills[MAP_ii][1] # MAP = low_low_pass_gage[MAP_i] if True: p6 = util.poly_fit(troughs[:n_peak] * defaults["dt"], deltas[:n_peak], 6) ### Actual # p6 = util.poly_fit(troughs[:] * defaults['dt'], deltas[:], 6) ### Test else: ### put a zero in the first element to encourage a negative leading coeff. _peaks = zeros(n_peak + 1) _deltas = zeros(n_peak + 1) _peaks[1:] = peaks[:n_peak] _deltas[1:] = deltas[:n_peak] p6 = util.poly_fit(_peaks * defaults["dt"], _deltas, 6) figure(620) ax = subplot(311) ylabel("Gage mmHG") plot(arange(len(bpf)) * defaults["dt"], low_pass_gage) plot(arange(len(bpf)) * defaults["dt"], low_low_pass_gage) subplot(313, sharex=ax) ylabel("Bandpass mmHG") plot(arange(len(bpf)) * defaults["dt"], bpf) plot(peaks * defaults["dt"], bpf[peaks], "ro") plot(troughs * defaults["dt"], bpf[troughs], "bo") subplot(312, sharex=ax) ylabel("Deltas mmHG") xlabel("Time Seconds") plot(peaks * defaults["dt"], deltas) plot(peaks[:n_peak] * defaults["dt"], deltas[:n_peak], "bd") ylim(0, 6) p5 = util.poly_der(p6) # map_time = util.find_zero(p5, hills[MAP_ii][1] * defaults['dt']) assert n_peak <= len(peaks), "n_peaks > len(peaks)" t = arange(peaks[0] * defaults["dt"], peaks[n_peak - 1] * defaults["dt"], 0.1) y = util.poly_eval(p6, t) map_time = t[argmax(y)] plot([map_time], [max(y)], "ro") # t = arange(0, 3 * map_time, .1) # y = util.poly_eval(p6, t) plot(t, y, "r-") map_idx = int(map_time / defaults["dt"]) MAP6 = low_low_pass_gage[map_idx] sbp_target = 0.55 * util.poly_eval(p6, map_time) dbp_target = 0.85 * util.poly_eval(p6, map_time) plot([map_time, map_time], [0, util.poly_eval(p6, map_time)], "r--") plot([0, t[-1]], [dbp_target, dbp_target], "r--") plot([0, t[-1]], [sbp_target, sbp_target], "r--") #### find SBP time t = arange(map_time, 0, -0.01) ### time going backwards from MAP pt = util.poly_eval(p6, t) - sbp_target sbp_time = t[where(pt < 0)[0][0]] #### find DBP time t = arange(map_time, 30 * map_time, 0.01) pt = util.poly_eval(p6, t) - dbp_target dbp_time = t[where(pt < 0)[0][0]] plot([dbp_time, dbp_time], [0, dbp_target], "r--") plot([sbp_time, sbp_time], [0, sbp_target], "r--") assert sbp_time < map_time, "sbp_time = %s > %s = MAP" % (sbp_time, map_time) assert map_time < dbp_time, "dbp_time = %s < %s = MAP" % (dbp_time, map_time) sbp_idx = int(sbp_time / defaults["dt"]) dbp_idx = int(dbp_time / defaults["dt"]) figure(124) plot(peaks[:n_peak] * defaults["dt"], deltas[:n_peak]) plot([map_time], [util.poly_eval(p6, map_time)], "go") plot([dbp_time], [util.poly_eval(p6, dbp_time)], "bo") plot([sbp_time], [util.poly_eval(p6, sbp_time)], "ro") # plot(arange(0, peaks[n_peak] * defaults['dt'], 1), util.poly_eval(p6, arange(0, peaks[n_peak] * defaults['dt'], 1))) plot([0, 25], [dbp_target, dbp_target], "g--") plot([0, 25], [sbp_target, sbp_target], "r--") __SBP = low_low_pass_gage[sbp_idx] __DBP = low_low_pass_gage[dbp_idx] figure(620) subplot(311) plot([sbp_idx * defaults["dt"], sbp_idx * defaults["dt"]], [0, __SBP], "r--") plot([dbp_idx * defaults["dt"], dbp_idx * defaults["dt"]], [0, __DBP], "r--") plot([0, sbp_time], [low_low_pass_gage[sbp_idx], low_low_pass_gage[sbp_idx]], "r--") plot([0, dbp_time], [low_low_pass_gage[dbp_idx], low_low_pass_gage[dbp_idx]], "r--") figure(620) subplot(311) title("%d/%d" % (__SBP, __DBP)) d = os.path.split(filename)[0] d_bp_fn = os.path.join(d, "derived_BP.png") savefig(d_bp_fn) if False: subplot(312) plot([sbp_idx * defaults["dt"], sbp_idx * defaults["dt"]], [-3, 3]) plot([dbp_idx * defaults["dt"], dbp_idx * defaults["dt"]], [-3, 3]) # assert abs(util.poly_eval(p6, sbp_time) - sbp_target) < 1e-6 ii = util.edit_deltas(delta_vs) ii = range(len(delta_vs)) delta_vs = [delta_vs[i] for i in ii] cuff_pressures = [cuff_pressures[i] for i in ii] cuff_pressures = array(cuff_pressures) if MAP is None: MAP = MAP6 SBP = __SBP DBP = __DBP transmuralP = MAP - cuff_pressures __deltas, __MAP, __SBP, __DBP, HR = util.get_edited_deltas(low_pass_gage, bpf, PEAK_HEIGHT) if SBP is None: SBP = __SBP if DBP is None: DBP = __DBP assert SBP > DBP, "%.2f = SBP <= DBP = %.2f" % (SBP, DBP) arterial_compliance = abs(array(delta_vs) / (SBP - DBP)) figure(7) plot(transmuralP, arterial_compliance) return arterial_compliance, transmuralP, SBP, DBP, MAP, HR, cba
#plt.ylim(ts.min(), ts.max()) plt.xlabel('time') plt.ylabel('dh (m)') plt.title('Mean TS') plt.show() return fig def plot_ts_full(t, Y, (i, j)): y = np.ma.masked_invalid(Y) mean_t = np.mean(np.mean(y, axis=1), axis=1) mean_xy = np.mean(y, axis=0) ts = mean_t + mean_xy[i, j] + Y[:, i, j] #x_pol, y_pol = spline_interp(t, ts, smooth=0.1) x_pol, y_pol = util.poly_fit(t, ts, order=3) x_lin, y_lin = linear_fit(t, ts) m, c = linear_fit(t, ts, return_coef=True) fig = plt.figure() ax = fig.add_subplot((111)) n = y_lin[0] # shift plt.plot(t, ts - n, linewidth=2) plt.plot(x_pol, y_pol - n) plt.plot(x_lin, y_lin - n) viz.add_inner_title(ax, 'linear trend = %.1f cm/yr' % (m * 100), loc=2) #plt.ylim(ts.min(), ts.max()) plt.xlabel('time') plt.ylabel('dh (m)')