Esempio n. 1
0
def main(args):

    # Collect data from results
    df = pd.read_pickle(path='results/fixed_parameters_std-2/8.pkl')

    # Set time range, 1440 = 1 day
    N = 1440
    x = np.arange(N)

    # Prep figure
    pu.figure_setup()
    fig_size = pu.get_fig_size(10, 5)
    fig = plt.figure(figsize=(fig_size))

    ############################################################################
    # Collect glucose data
    gut_compartment = df['gt'].values[:N]
    plasma_compartment = df['mt'].values[:N]
    carb_est = df['carb_ests_'].values[:N]
    # carb_est[carb_est==0] = np.nan

    # Plot Glucose compartments...
    ax = fig.add_subplot(111)
    ax.plot(x, gut_compartment, c='b', lw=pu.plot_lw(), label='Gut glucose')
    ax.plot(x,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Plasma glucose')
    ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$mg/L$')
    ax.set_axisbelow(True)

    # Add second axis
    ax2 = ax.twinx()
    color = 'tab:orange'
    ax2.set_ylabel('$\hat{m}$', color=color)
    ax2.plot(
        x,
        carb_est,  #lw=pu.plot_lw(),
        label='Carbs est.',
        color=color,
        alpha=.5)  #, marker='o', markerfacecolor='None',
    #markersize=5, linewidth=0, alpha=.2)
    ax2.tick_params(axis='y', labelcolor=color)

    # Add legends
    ax.legend(loc='upper left', prop={'size': 5})
    ax2.legend(loc='upper right', prop={'size': 5})

    # Finilize plot
    plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
def plot(ax, x, y):
    # Fit the classifiers
    clf = linear_model.LogisticRegression(C=1e5, solver="lbfgs")
    clf.fit(x, y)

    ols = linear_model.LinearRegression()
    ols.fit(x, y)

    print("LogisticRegression:",
          sum(np.equal(clf.predict(x) > 0.5, y)) / len(y))
    print("LinearRegression:", sum(np.equal(ols.predict(x) > 0.5, y)) / len(y))

    ax.plot(x,
            y,
            "o",
            markersize=pu.plot_lw() * 2,
            zorder=20,
            label="Original data")

    x_ = x_linspace(x)

    loss = model(x_ * clf.coef_ + clf.intercept_).ravel()

    ax.plot(x_, loss, color="red", label="Logistic Regression Model")
    ax.plot(x_,
            ols.coef_ * x_ + ols.intercept_,
            label="Linear Regression Model")
    ax.axhline(0.5, color=".5")
    ax.set_ylabel("$y$")
    ax.set_xlabel("$x$")
    # ax.set_xticks()
    ax.set_yticks([0, 0.5, 1])
    ax.set_ylim(-0.25, 1.25)
    plt.xlim(min(x_), max(x_))
    ax.legend(prop={"size": 6}, loc="lower right")
def main(args):
    x = np.linspace(-6, 6, 200)
    y = 1/(1 + np.exp(-x))

    pu.figure_setup()

    fig_size = pu.get_fig_size(10, 5)
    fig = plt.figure(figsize=fig_size)
    ax = fig.add_subplot(111)

    ax.plot(x, y, c='b', lw=pu.plot_lw())

    ax.set_xlabel('$x$')
    ax.set_ylabel('$\\sigma(x)$')

    ax.set_axisbelow(True)

    plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
Esempio n. 4
0
def main(args):
    x = np.linspace(-6, 6, 200)
    y = 1 / (1 + np.exp(-x))

    pu.figure_setup()

    fig_size = pu.get_fig_size(10, 5)
    fig = plt.figure(figsize=(fig_size))
    ax = fig.add_subplot(111)

    ax.plot(x, y, c='b', lw=pu.plot_lw())

    ax.set_xlabel('$x$')
    ax.set_ylabel('$\\sigma(x)$')

    ax.set_axisbelow(True)

    plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
Esempio n. 5
0
def main(args):

    # Collect data from results
    df = pd.read_pickle(path='results/fixed_parameters_std-2/8.pkl')

    # Set time range, 1440 = 1 day
    N = 1440
    x = np.arange(N)

    # Prep figure
    pu.figure_setup()
    fig_size = pu.get_fig_size(10, 5)
    fig = plt.figure(figsize=(fig_size))

    ############################################################################
    # Collect insulin data
    tissue_compartment = df['s1'].values[:N]
    plasma_compartment = df['s2'].values[:N]
    bolus_data = df['bolus_inputs_'].values[:N]

    # Keep only non-zero bolus data - Set zeros to nan
    bolus_data[bolus_data == 0] = np.nan

    # Plot Insulin Compartment Data
    ax = fig.add_subplot(111)
    ax.plot(x,
            tissue_compartment,
            c='b',
            lw=pu.plot_lw(),
            label='Subcutaneous tissue')
    ax.plot(x,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Blood plasma')
    ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$Insulin, \, mg/L$')
    ax.set_axisbelow(True)

    # Add Bolus Data on second y-axis
    ax2 = ax.twinx()
    color = 'tab:green'
    ax2.set_ylabel('Units', color=color)
    ax2.plot(
        x,
        bolus_data,  #lw=pu.plot_lw(),
        label='Bolus',
        color=color,
        marker='D',
        markerfacecolor='None',
        markersize=5,
        linewidth=0)
    ax2.tick_params(axis='y', labelcolor=color)

    # Show legend
    ax.legend(loc='upper left', prop={'size': 5})
    ax2.legend(loc='upper right', prop={'size': 5})

    plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
def main(args):

    # Collect data from results
    df = pd.read_pickle(path='results/fixed_parameters_std-2/4.pkl')

    # Set time range
    S = 540
    N = 710
    x = np.arange(N - S)

    # Prep figure
    pu.figure_setup()
    fig_size = pu.get_fig_size(10, 5)
    fig = plt.figure(figsize=(fig_size))

    ############################################################################
    # Collect CGM and meal data
    cgm_data = df['cgm_inputs_'].values[S:N]
    cgm_model = df['Gt'].values[S:N]
    meals = df['y_meals'].values[S:N]

    # Make all 0 target meal NaNs
    meals[meals == 0] = np.nan

    # Print meal idxs
    print('Meal idxs: ', np.where(meals == 1.))

    # Plot CGM and Meal data
    ax = fig.add_subplot(211)
    ax.set_xlim(0, N - S)
    ax.set_ylim(0, 250)

    ax.axhline(70, linewidth=1, color='#e3e1e1')
    ax.axhline(180, linewidth=1, color='#e3e1e1')
    # ax.axvspan(125, 170, color='#eeeeee')

    ax.plot(x,
            cgm_data,
            marker='o',
            c='g',
            linewidth=0,
            markersize=1,
            label='CGM')

    ax.plot(x,
            meals * 20,
            marker='o',
            c='purple',
            linewidth=0,
            markersize=4,
            markerfacecolor='None',
            label='Meal')
    ax.set_axisbelow(True)

    ax.legend(loc='upper left', prop={'size': 6})
    ax.set_ylabel('$mg/dl$')

    ############################################################################
    # Collect insulin data
    tissue_compartment = df['s1'].values[S:N]
    plasma_compartment = df['s2'].values[S:N]
    bolus_data = df['bolus_inputs_'].values[S:N]

    # Keep only non-zero bolus data - Set zeros to nan
    bolus_data[bolus_data == 0] = np.nan

    # Print bolus idxs
    print('Bolus idxs: ', np.where(bolus_data > 0.))

    # Plot Insulin Compartment Data
    ax = fig.add_subplot(212, sharex=ax)
    ax.set_xlim(0, N - S)

    ax.plot(x,
            tissue_compartment,
            c='b',
            lw=pu.plot_lw(),
            label='Subcutaneous tissue')
    ax.plot(x,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Blood plasma')

    ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$Insulin, \, mg/L$')

    # plt.grid()
    ax.set_axisbelow(True)

    # Add Bolus Data on second y-axis
    ax2 = ax.twinx()
    color = 'tab:green'
    ax2.set_ylabel('Units', color=color)
    ax2.plot(
        x,
        bolus_data,  #lw=pu.plot_lw(),
        label='Bolus',
        color=color,
        marker='D',
        markerfacecolor='None',
        markersize=4,
        linewidth=0)
    ax2.tick_params(axis='y', labelcolor=color)
    ax2.set_axisbelow(True)

    # Show legend
    ax.legend(loc='upper left', prop={'size': 6})
    ax2.legend(loc='upper right', prop={'size': 6})

    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
def main(args):

    # Collect data from results
    df = pd.read_pickle(path='results/fixed_parameters_std-2/4.pkl')

    # Set time range
    S = 540
    N = 695
    H = 30
    x = np.arange(N - S)

    # Prep figure
    pu.figure_setup()
    fig_size = pu.get_fig_size(10, 5)
    fig = plt.figure(figsize=(fig_size))

    ############################################################################

    # Collect CGM and meal data
    cgm_data = df['cgm_inputs_'].values[S:N]
    cgm_model = df['Gt'].values[S:N]
    meals = df['y_meals'].values[S:N]

    # Make all 0 target meal NaNs
    meals[meals == 0] = np.nan
    print('Logged Meal idxs:', np.where(meals > 0))

    # Get meal predictions
    meal_preds = df['meal_preds_'].values[S:N]
    meal_preds[meal_preds == 0] = np.nan

    # Plot CGM and Meal data
    ax = fig.add_subplot(211)
    ax.set_xlim(0, N - S)
    ax.set_ylim(0, 250)

    ax.axhline(70, linewidth=1, color='#ececec')
    ax.axhline(180, linewidth=1, color='#ececec')

    ax.plot(x,
            cgm_data,
            marker='o',
            c='g',
            linewidth=0,
            markersize=1,
            label='CGM')
    ax.plot(x,
            meals * 20,
            marker='o',
            c='purple',
            linewidth=0,
            markersize=4,
            markerfacecolor='None',
            label='Meal')
    ax.plot(x,
            meal_preds * 5,
            marker='^',
            c='magenta',
            linewidth=0,
            markersize=4,
            label='Predicted meal')

    ax.set_axisbelow(True)

    ax.legend(loc='upper left', prop={'size': 6})
    ax.set_ylabel('$mg/dl$')

    ############################################################################
    # Glucose and Meal Detection

    # Get glucose data
    carb_est = df['carb_ests_'].values[S:N]
    carb_mean = df['carb_mean_'].values[S:N]
    carb_stds = df['carb_stds_'].values[S:N]

    # Plot
    ax = fig.add_subplot(212, sharex=ax)
    ax.set_xlim(0, N - S)

    for it, m in enumerate(meal_preds):
        if m == 1:
            print('Meal prediction idx: ', it)
            ax.axvline(it, c='magenta')

    ax.plot(x, carb_est, c='orange', lw=pu.plot_lw(), label="$\hat{m}_t'$")
    ax.plot(x, carb_mean, c='b', lw=.8, label="$\\overline{\hat{m'}}_{0:t}$")
    ax.plot(x,
            carb_stds,
            c='b',
            lw=.8,
            label="$2 \cdot \sigma$",
            linestyle='-.')

    ax.legend(loc='upper left', prop={'size': 6})
    ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$Carbs, \, mg$')
    ax.set_axisbelow(True)

    # plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
def main(args):

    # Collect data from results
    df = pd.read_pickle(path='./../results/2019-06-15_17:48/4.pkl')

    # Set time range, 1440 = 1 day
    N = 3000
    x = np.arange(N)

    # Prep figure
    pu.figure_setup()
    fig_size = pu.get_fig_size(10, 50)
    fig = plt.figure(figsize=(fig_size))

    ############################################################################
    # Collect CGM and meal data

    cgm_data = df['cgm_inputs_'].values[:N]
    cgm_model = df['Gt'].values[:N]
    meals = df['y_meals'].values[:N]

    # Make all 0 target meal NaNs
    meals[meals == 0] = np.nan

    # Plot CGM and Meal data
    ax = fig.add_subplot(411)
    ax.axhspan(70, 180, alpha=0.1, color='black')
    ax.set_ylim(0, 350)
    ax.plot(x,
            cgm_data,
            marker='o',
            c='g',
            linewidth=0,
            markersize=1,
            label='CGM')
    ax.plot(x,
            cgm_model,
            c='black',
            linewidth=1,
            linestyle='dashed',
            label='Modeled CGM')
    ax.plot(x,
            meals * 20,
            marker='*',
            c='purple',
            linewidth=0,
            markersize=4,
            label='Meal')

    # ax.set_xlabel('$t (minutes)$')
    ax.legend(loc='upper left', prop={'size': 6})
    ax.set_ylabel('$mg/dl$')

    meal_preds = df['meal_preds_'].values[:N]
    meal_preds[meal_preds == 0] = np.nan

    for it, m in enumerate(meal_preds):
        if m == 1:
            ax.axvline(it, c='magenta', alpha=.1)

    # Add CGM prediction
    # horizon = 45
    # start_time = 488
    # x_pred = np.arange(start_time,start_time+horizon+1)
    # cgm_pred = cgm_preds[start_time]-46
    # ax.plot(x_pred, cgm_pred, c='black', linewidth=1, linestyle='dashed')

    ############################################################################
    # Collect insulin data

    tissue_compartment = df['s1'].values[:N]
    plasma_compartment = df['s2'].values[:N]
    bolus_data = df['bolus_inputs_'].values[:N]

    # Keep only non-zero bolus data - Set zeros to nan
    bolus_data[bolus_data == 0] = np.nan

    # Plot Insulin Compartment Data
    ax = fig.add_subplot(412)
    ax.plot(x,
            tissue_compartment,
            c='b',
            lw=pu.plot_lw(),
            label='Subcutaneous tissue')
    ax.plot(x,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Blood plasma')

    # ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$Insulin, \, mg/L$')
    ax.set_axisbelow(True)

    # Add Bolus Data on second y-axis
    ax2 = ax.twinx()
    color = 'tab:green'
    ax2.set_ylabel('Units', color=color)
    ax2.plot(
        x,
        bolus_data,  #lw=pu.plot_lw(),
        label='Bolus',
        color=color,
        marker='D',
        markerfacecolor='None',
        markersize=5,
        linewidth=0)
    ax2.tick_params(axis='y', labelcolor=color)

    # Show legend
    ax.legend(loc='upper left', prop={'size': 6})
    # ax2.legend(loc='upper right', prop={'size': 6})

    ############################################################################

    # Collect glucose data
    gut_compartment = df['gt'].values[:N]
    plasma_compartment = df['mt'].values[:N]

    # Plot Glucose compartments...
    ax = fig.add_subplot(413)
    ax.plot(x, gut_compartment, c='b', lw=pu.plot_lw(), label='Gut glucose')
    ax.plot(x,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Plasma glucose')

    # ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$mg/L$')
    ax.set_axisbelow(True)
    ax.legend(loc='upper left', prop={'size': 6})

    ############################################################################
    # Glucose and Meal Detection

    # Get glucose data
    carb_est = df['carb_ests_'].values[:N]
    carb_mean = df['carb_mean_'].values[:N]
    carb_stds = df['carb_stds_'].values[:N]

    # Get meal predictions
    meal_preds = df['meal_preds_'].values[:N]
    meal_preds[meal_preds == 0] = np.nan

    # Plot
    ax = fig.add_subplot(414)
    ax.plot(x, carb_est, c='orange', lw=pu.plot_lw())
    # ax.plot(x, carb_mean, c='b', lw=pu.plot_lw(), linestyle='dashed')
    # ax.plot(x, carb_mean, c='b', lw=pu.plot_lw(), linestyle='dashed')
    ax.plot(x,
            meal_preds * carb_est,
            marker='*',
            c='magenta',
            linewidth=0,
            markersize=4,
            label='Predicted meal')

    ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$Carbs, \, mg$')
    ax.set_axisbelow(True)

    plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()
Esempio n. 9
0
def main(args):

    # Collect data from results
    df = pd.read_pickle(path='results/fixed_parameters_std-2/4.pkl')

    # Set time range
    S = 540
    N = 695
    H = 30
    x = np.arange(N - S)

    # Prep figure
    pu.figure_setup()
    fig_size = pu.get_fig_size(10, 8)
    fig = plt.figure(figsize=(fig_size))

    ############################################################################

    # Collect CGM and meal data
    cgm_data = df['cgm_inputs_'].values[S:N]
    cgm_model = df['Gt'].values[S:N]
    meals = df['y_meals'].values[S:N]

    # Set up prediction line
    pred_t = 124
    pred = df['cgm_preds_'][pred_t]
    x_pred = np.arange(pred_t, pred_t + len(pred))

    # Make all 0 target meal NaNs
    meals[meals == 0] = np.nan

    # Print meal idxs
    print('Meal idxs: ', np.where(meals == 1.))

    # Plot CGM and Meal data
    ax = fig.add_subplot(411)
    ax.set_xlim(0, N - S)
    ax.set_ylim(0, 250)

    ax.axhline(70, linewidth=1, color='#ececec')
    ax.axhline(180, linewidth=1, color='#ececec')
    ax.axvspan(pred_t, pred_t + H + 1, color='#f6f6f6')

    ax.plot(x,
            cgm_data,
            marker='o',
            c='g',
            linewidth=0,
            markersize=1,
            label='CGM')
    ax.plot(x_pred,
            pred,
            c='black',
            linewidth=1,
            linestyle='dashed',
            label='Modeled CGM')
    ax.plot(x,
            meals * 20,
            marker='o',
            c='purple',
            linewidth=0,
            markersize=4,
            markerfacecolor='None',
            label='Meal')
    plt.vlines(x=pred_t + H,
               ymin=pred[-1],
               ymax=cgm_data[pred_t + H - 1],
               color='r',
               linewidth=2)

    ax.set_axisbelow(True)

    ax.legend(loc='upper left', prop={'size': 6})
    ax.set_ylabel('$mg/dl$')

    ############################################################################

    # Collect insulin data
    tissue_compartment = df['s1'].values[S:N]
    plasma_compartment = df['s2'].values[S:N]
    bolus_data = df['bolus_inputs_'].values[S:N]

    # Keep only non-zero bolus data - Set zeros to nan
    bolus_data[bolus_data == 0] = np.nan

    # Print bolus idxs
    print('Bolus idxs: ', np.where(bolus_data > 0.))

    # Plot Insulin Compartment Data
    ax = fig.add_subplot(412, sharex=ax)
    ax.set_xlim(0, N - S)
    ax.axvspan(pred_t, pred_t + H + 1, color='#f6f6f6')

    ax.plot(x,
            tissue_compartment,
            c='b',
            lw=pu.plot_lw(),
            label='Subcutaneous tissue')
    ax.plot(x,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Blood plasma')

    ax.set_ylabel('$Insulin, \, mg/L$')

    # plt.grid()
    ax.set_axisbelow(True)

    # Add Bolus Data on second y-axis
    ax2 = ax.twinx()
    color = 'tab:green'
    ax2.set_ylabel('Units', color=color)
    ax2.plot(
        x,
        bolus_data,  #lw=pu.plot_lw(),
        label='Bolus',
        color=color,
        marker='D',
        markerfacecolor='None',
        markersize=4,
        linewidth=0)
    ax2.tick_params(axis='y', labelcolor=color)
    ax2.set_axisbelow(True)

    # Show legend
    ax.legend(loc='upper left', prop={'size': 6})
    ax2.legend(loc='upper right', prop={'size': 6})

    ############################################################################

    # Collect glucose data
    gut_compartment = df['gt'].values[S:N - H]
    plasma_compartment = df['mt'].values[S:N - H]
    xx = np.arange(N - S - H)

    # Collect "prediction" data
    gut_compartment_ff = df['gt'].values[N - H:N]
    plasma_compartment_ff = df['mt'].values[N - H:N]
    xx_ff = np.arange(N - S - H, N - S)

    # Plot Glucose compartments...
    ax = fig.add_subplot(413, sharex=ax)
    ax.set_xlim(0, N - S)
    ax.axvspan(pred_t, pred_t + H + 1, color='#f6f6f6')

    ax.plot(xx, gut_compartment, c='b', lw=pu.plot_lw(), label='Gut glucose')
    ax.plot(xx,
            plasma_compartment,
            c='r',
            lw=pu.plot_lw(),
            label='Plasma glucose')

    ax.plot(xx_ff,
            gut_compartment_ff,
            c='b',
            lw=pu.plot_lw(),
            linestyle='dashed')
    ax.plot(xx_ff,
            plasma_compartment_ff,
            c='r',
            lw=pu.plot_lw(),
            linestyle='dashed')

    ax.set_ylabel('$mg/L$')
    ax.set_axisbelow(True)
    ax.legend(loc='upper left', prop={'size': 6})

    ############################################################################
    # Glucose and Meal Detection

    # Get glucose data
    carb_est = df['carb_ests_'].values[S:N - H]
    carb_mean = df['carb_mean_'].values[S:N - H]
    carb_stds = df['carb_stds_'].values[S:N - H]
    xx_carbs = np.arange(N - S - H)

    # Get "future" glucose data
    carb_est_ff = df['carb_ests_'].values[N - H:N]
    xx_carbs_ff = np.arange(N - S - H, N - S)

    # Get meal predictions
    meal_preds = df['meal_preds_'].values[S:N - H]
    meal_preds[meal_preds == 0] = np.nan

    # Plot
    ax = fig.add_subplot(414, sharex=ax)
    ax.set_xlim(0, N - S)
    ax.axvspan(pred_t, pred_t + H + 1, color='#f6f6f6')

    ax.plot(xx_carbs,
            carb_est,
            c='orange',
            lw=pu.plot_lw(),
            label='Carbs est.')
    ax.plot(xx_carbs_ff,
            carb_est_ff,
            c='orange',
            lw=pu.plot_lw(),
            linestyle='dashed')

    # ax.plot(x, carb_mean, c='b', lw=0.5, label)
    # ax.plot(xx_carbs, meal_preds, marker='^', c='magenta', linewidth=0,
    #         markersize=3, label='Predicted meal')

    ax.legend(loc='upper left', prop={'size': 6})
    ax.set_xlabel('$t \, (minutes)$')
    ax.set_ylabel('$Carbs, \, mg$')
    ax.set_axisbelow(True)

    # plt.grid()
    plt.tight_layout()

    if args.save:
        pu.save_fig(fig, args.save)
    else:
        plt.show()