def residuals(df, forecast, skip_first=0, skip_last=0, title="Patiend ID CGM", savefig=False): """Plot the input residuals. Parameters ------------------- df : DataFrame, the output returned by gluco_extract(..., return_df=True) forecast : array of float, the prediction for the given time-series skip_first : number, the number of initial samples to exclude skip_last : number, the number of final samples to exclude title : string, the title of the plot (optional) savefig : bool, if True save title.png """ # Evaluate the residuals (exclude learning and open loop ph samples) residuals = df.as_matrix()[skip_first:-skip_last].ravel( ) - forecast[skip_first:-skip_last] plt.figure(figsize=(12, 4), dpi=300) plt.plot(df.index[skip_first:-skip_last], residuals) DW = sm.stats.durbin_watson(residuals) plt.title('Durbin-Watson: {:.3f}'.format(DW)) if savefig: plt.savefig(title + '_residuals.png')
def cgm(df, gluco_fit=None, hypo=70, hyper=126, title="Patiend ID CGM", savefig=False): """Plot the CGM signal on an input time span. Parameters ------------------- df : DataFrame, the output returned by gluco_extract(..., return_df=True) gluco_fit : array of float, the results of a fitted model (optional) hypo : number, hypoglicaemia threshold in mg/dl (default is 70, i.e. 3.9 mmol/l) hyper : number, hyperglicaemia threshold in mg/dl (default is 126, i.e. 7 mmol/l) title : string, the title of the plot (optional) savefig : bool, if True save title.png """ plt.figure(figsize=(10, 6), dpi=300) plt.hlines(hypo, df.index[0], df.index[-1], linestyles='dashed', label='hypoglicaemia') plt.hlines(hyper, df.index[0], df.index[-1], linestyles='dotted', label='hyperglicaemia') plt.ylim([10, 410]) plt.plot_date(df.index, df.as_matrix(), '-', label='real CGM') if gluco_fit is not None: plt.plot(df.index, gluco_fit, '--', label='predicted CGM') plt.title(title) plt.ylabel('mg/dL') plt.xticks(rotation='vertical') plt.legend(bbox_to_anchor=(1.1, 1.0)) if savefig: plt.savefig(title + '_fit.png')
obs_errors = [100, 1.0, 1.0, 1.0, 0.001] levels = [0.95, 0.835, 0.685, 0.51, 0.34, 0.2, 0.095, 0.025] plt.rc('lines', linewidth=1.0) plt.figure(figsize=(9, 3), facecolor='white') dirs = ['double'] labels = ['64 bits'] handles = [] for d, l in zip(dirs, labels): print(f'Plotting {d}') handles += plot(d, l) plt.ylim([0, 3.5]) plt.xlabel('Time') plt.ylabel(f'Total analysis RMSE') plt.title('') # Add legend leg = plt.legend(handles=handles, frameon=True, ncol=2) rect = leg.get_frame() rect.set_linewidth(0.0) rect.set_alpha(0.7) plt.savefig(f'total_error_compare.pdf', bbox_inches='tight') plt.show()
# Get axis limits and equalise xlim = plt.gca().get_xlim() ylim = plt.gca().get_ylim() xlim = ylim = [min(xlim[0], ylim[0]), max(xlim[1], ylim[1])] plt.xlim(xlim) plt.ylim(ylim) # Plot diagonal line plt.plot(xlim, ylim, ls="--", c=".3") plt.title('') plt.xlabel('RMSE') plt.ylabel('Spread') # Legend for symbol for field in fields: plt.scatter(-1, -1, s=90, marker=field[2], color='.3', label=field[1]) # Legend for color plot_lines = [plt.plot([-1,-1.5], [-1,-1.5], color=colors[0])[0]] plot_lines.append(plt.plot([-1,-1.5], [-1,-1.5], color=colors[1])[0]) col_leg = plt.legend(plot_lines, labels, loc=6, fontsize=12) # Add both legends plt.gca().add_artist(col_leg) plt.legend(loc=2, fontsize=12) plt.savefig(f'scatter_all_fields.pdf', bbox_inches='tight') plt.show()
first_guess[:, var + 1], '--', label="first guess", alpha=1.0) axarr[i].plot(final_guess[:, 0], final_guess[:, var + 1], ':', label="final guess") plt.tight_layout() leg = plt.figlegend(axarr[len(display_vars) - 1].get_lines(), ['truth', 'observations', 'first guess', 'final guess'], loc='lower center', ncol=2) plt.xlabel('Time (nondimensional units)') f.subplots_adjust(bottom=0.17) plt.savefig('vars.pdf', bbox_extra_artists=(leg, ), bbox_inches='tight') # Plot diagnostics plt.figure(figsize=(6, 3)) diagn = loadtxt('diagnostics.txt') length = where(diagn <= 0)[0] length = diagn.shape[0] if len(length) == 0 else length[0] plt.semilogy(diagn[:length - 1, 0], diagn[:length - 1, 1]) plt.xlabel('Iterations') plt.ylabel('Cost function') plt.tight_layout() plt.savefig('cost_function.pdf', bbox_inches='tight') plt.show()
} plt.rc('lines', linewidth=1.0) plt.style.use('ggplot') plt.figure(figsize=(9, 3), facecolor='white') dirs = ['double'] labels = ['64 bits'] handles = [] for d, l in zip(dirs, labels): print(f'Plotting {d}') handles += plot(d, fields[argv[2]][0], l) plt.ylim([0, 3.5]) plt.xlabel('Time') plt.ylabel(f'{fields[argv[2]][1]}') plt.title('') # Add legend leg = plt.legend(handles=handles, frameon=True, ncol=2) rect = leg.get_frame() rect.set_linewidth(0.0) rect.set_alpha(0.7) plt.savefig(f'error_compare_{argv[2]}.pdf', bbox_inches='tight') plt.show()
} vert_secs = [(1.0, 0.5), (0.5, 0.2), (0.2, 0.0)] field = ' '.join(fields[argv[2]]) plt.figure(figsize=(5, 5), facecolor='white') dirs = ['double', 'reduced'] labels = ['64 bits', '22 bits'] handles = [] for d, l in zip(dirs, labels): print(f'Plotting {d}') handles += plot(d, field, vert_secs[int(argv[3])], l) plt.xlabel('Latitude') plt.ylabel(f'{fields[argv[2]][0]} {fields[argv[2]][1]}') plt.title('') # Add legend leg = plt.legend(handles=handles, frameon=True, ncol=2, loc='lower center') rect = leg.get_frame() rect.set_linewidth(0.0) rect.set_alpha(0.7) plt.savefig(f'latitudinal_error_{argv[2]}_{argv[3]}.pdf', bbox_inches='tight') plt.show()
'q': ('Specific Humidity [kg/kg]', 'Specific humidity (kg/kg)') } plt.figure(figsize=(5, 5), facecolor='white') dirs = ['double', 'reduced'] labels = ['64 bits', '22 bits'] handles = [] for d, l in zip(dirs, labels): print(f'Plotting {d}') handles += plot(d, fields[argv[2]][0], l) plt.xlabel(f'{fields[argv[2]][1]}') plt.ylabel('Sigma coordinate') plt.ylim([1.0, 0.0]) plt.xlim([0.0, max(1.0, plt.gca().get_xlim()[1])]) plt.gca().set_xlim(left=0) plt.title('') # Add legend leg = plt.legend(handles=handles, frameon=True, ncol=2, loc='lower center') rect = leg.get_frame() rect.set_linewidth(0.0) rect.set_alpha(0.7) plt.savefig(f'vertical_error_{argv[2]}.pdf', bbox_inches='tight') plt.show()