def gluco_plot(time, gluco, hypo=70, hyper=126, title="Patiend ID glucose level"): """Plot the glucose level on a time span. Parameters ------------------- time : array of datetimes, the horizontal axis gluco : array of float, the correspondent glucose level on the vertical axis 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) """ plt.figure(figsize=(10, 6)) plt.hlines(hypo, time[0], time[-1], linestyles='dashed', label='hypoglicaemia') plt.hlines(hyper, time[0], time[-1], linestyles='dotted', label='hyperglicaemia') plt.ylim([10, 410]) plt.plot_date(time, gluco, '-', label='glucose level') plt.title(title) plt.ylabel('mg/dL') plt.xticks(rotation='vertical') plt.legend()
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')
# In[36]: # ---------- Plot ------------------------ # plt.subplot(211) plt.plot(ts, ys, '-', label='noisy') # noisy measures plt.plot(ts, y, '--', label='filtered') # filtered measures plt.grid() plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.) plt.subplot(212) residuals = ys.ravel()[0:Ns] - y.ravel()[0:Ns] plt.plot(ts, residuals) plt.title('Durbin-Watson: {:.3f}'.format(sm.stats.durbin_watson(residuals))) plt.tight_layout() plt.grid() # ## Translate `pykalman` names into our convention # # **Parameter Name** (Notation) # - initial_state_mean (`X0`) # - initial_state_covariance (`P0`) # - transition_matrices (`F`) # - transition_offsets (0?) # - transition_covariance (`Q`) # - observation_matrices (`X`) # - observation_offsets (0?) # - observation_covariance (`R`)
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()
import pandas as pd from nlpia.plots import scatter_3d np = pd.np df = pd.DataFrame(pd.np.random.randn(1000, 10)) np = pd.np plt.figure() f = plt.figure() ax = f.add_subplot(111) axes = df.plot(kind='scatter', x=0, y=1, ax=ax) plt.title('2D Normally Distributed') plt.tight_layout() plt.show() scatter_3d(df) plt.show() df.values.dot(df.values) np.do(df.values, df.values) np.dot(df.values, df.values) np.dot(df.values, df.values.T) np.dot(df.values, df.values.T).shape np.dot(df.values, df.values.T).shape.sum() np.dot(df.values.T, df.values) np.dot(df.values.T, df.values).shape norms = pd.DataFrame([[np.linalg.norm(row[:i]) for i in range(2,11)] for row in df]) row