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()
Example #2
0
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')
Example #3
0
def plot_kim_curve(tmp):
    sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 5})
    sns.set_style("darkgrid")

    plt.figure(figsize=(20, 10))
    plt.hold('on')
    plt.plot(np.linspace(0, 0.3, 100), tmp['kc_avg'])
    plt.ylim([0, 1])
Example #4
0
def scatter_3d(df, labels=None, depthshade=True):
    df = getattr(df, 'embedding_', df)
    labels = df[labels] if (isinstance(labels, (int, str, bytes)) and labels
                            in getattr(df, 'columns', set())) else labels
    labels = np.array(
        np.zeros(shape=(len(df), )) if labels is None else labels)
    try:
        labels = labels.astype(int)  # TODO: use LabelEncoder
    except (TypeError, AttributeError):
        pass
    if str(labels.dtype).startswith('int'):
        labels = np.array(list('grbkcym'))[labels % 7]

    try:
        df = df.values
    except:
        pass

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(df[:, 0],
               df[:, 1],
               df[:, 2],
               zdir='z',
               s=20,
               c=labels,
               depthshade=depthshade)
    return fig
def plot_aucs(test_name, train_aucs, test_aucs):
    fig = plt.figure(figsize=(8, 8))
    axes = fig.gca()
    axes.plot(
        np.arange(N_EPOCHS), train_aucs)
    axes.plot(
        np.arange(N_EPOCHS), test_aucs)
    plt.xlabel("epoch")
    plt.ylabel("AUC")
    plt.xlim(0, 15)
    plt.ylim(0.5, 1.0)
    plt.legend(["train", "test (%s)" % test_name])
    fig.savefig("auc_%s.png" % test_name)
Example #6
0
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')
def autocorr_plot(ts, lags=None):
    """Plot the (partial) autocorrelation of a given time-series.
    
    Parameters
    -------------------
    ts : array of float, time-series values
    lags : array of int, lag values on horizontal axis.
    """
    fig = plt.figure(figsize=(12, 8))
    ax1 = fig.add_subplot(211)
    fig = sm.graphics.tsa.plot_acf(ts, lags=lags, ax=ax1)
    ax2 = fig.add_subplot(212)
    fig = sm.graphics.tsa.plot_pacf(ts, lags=lags, ax=ax2)
    plt.suptitle('Lags: {}'.format(lags))
Example #8
0
FUTURE.netcdf_promote = True

# Change to relevant experiment directory
chdir(f'../experiments/{argv[1]}')

fields = [
    'Surface Pressure [Pa]', 'U-wind [m/s]', 'V-wind [m/s]', 'Temperature [K]',
    'Specific Humidity [kg/kg]'
]

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('')
Example #9
0
chdir(f'../experiments/{argv[1]}')

# Get colors
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

# Define levels and variables
levels = [0.095, 0.34, 0.51, 0.835, 0.95]
fields = [
        ('Surface Pressure [Pa]', 'Surface pressure', '*', 100.0),
        ('U-wind [m/s]', 'Zonal wind', '^', 1.0),
        ('V-wind [m/s]', 'Meriodional wind', 'v', 1.0),
        ('Temperature [K]', 'Temperature', 'o', 1.0),
        ('Specific Humidity [kg/kg]', 'Specific humidity', 'D', 0.001)
]

fig = plt.figure(figsize=(5,5), facecolor='white')

dirs = ['double', 'reduced']
labels= ['64 bits', '22 bits']

for d, c in zip(dirs, colors):
    print(f'Plotting {d}')

    plot_fields(d, c)

# 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)
                                     d=d,
                                     q=q,
                                     start_params=None,
                                     verbose=True)

# In[13]:

# plot results
gluco_plot(df.index, df.as_matrix(), title='Patient ' + str(k))
plt.plot(df.index, forecast['ts'], linestyle='dashed', label='forecast')
plt.legend(bbox_to_anchor=(1.2, 1.0))
MAE_6 = np.mean(errs['err_6'])
MAE_12 = np.mean(errs['err_12'])
MAE_18 = np.mean(errs['err_18'])
RMSE_6 = np.linalg.norm(errs['err_6']) / np.sqrt(len(errs['err_6']))
RMSE_12 = np.linalg.norm(errs['err_12']) / np.sqrt(len(errs['err_12']))
RMSE_18 = np.linalg.norm(errs['err_18']) / np.sqrt(len(errs['err_18']))
print("MAE (30') = {:2.3f}\t|\tMAE (60') = {:2.3f}\t|\tMAE (90') = {:2.3f}".
      format(MAE_6, MAE_12, MAE_18))
print("RMSE (30') = {:2.3f}\t|\tRMSE (60') = {:2.3f}\t|\tRMSE (90') = {:2.3f}".
      format(RMSE_6, RMSE_12, RMSE_18))

# In[14]:

residuals = df.as_matrix()[w_size:-ph].ravel() - forecast['ts'][w_size:-ph]
fig = plt.figure(figsize=(12, 4))
plt.plot(df.index[w_size:-ph], residuals)
plt.title('Durbin-Watson: {:.3f}'.format(sm.stats.durbin_watson(residuals)))

# In[ ]:
Example #11
0
                  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()
Example #12
0
from seaborn import plt
from mpl_toolkits.mplot3d import   # noqa

import pandas as pd

from plyfile import PlyData

np = pd.np


horse = PlyData.read('horse.ply')
points = np.array(horse.elements[0].data)
# facets = np.array(horse.elements[1].data)
points = np.array([row.tolist() for row in points])
# pd.DataFrame(points, columns='x y z alpha r g b'.split())
df = pd.DataFrame(points[:, :3], columns='x y z'.split())
# df.to_csv('horse.csv')


h = pd.read_csv('horse.csv', header=None).values[:, :3]
h = pd.DataFrame(h, columns='x y z'.split())
h = h.sample(1000)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(h.x, h.y, h.z, zdir='z', s=20, c=None, depthshade=True)
plt.show()
Example #13
0
>>> scatter_3d(df, labels='c')
>>> plt.show()
"""
from seaborn import plt

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()