Ejemplo n.º 1
0
def beta_rho_plot(var):
    if var == 'beta':
        data = pd.read_csv('beta_compare.csv', index_col='beta')
    if var == 'rho':
        data = pd.read_csv('rho_compare.csv', index_col='rho')
    data_eff = data.loc[:, data.columns.str.startswith('eff')]
    data_std = data.loc[:, data.columns.str.startswith('std')]
    data_pool = data.loc[:, data.columns.str.startswith('pool')]
    num_c = len(data_eff.columns)
    fig, axes = plt.subplots(1, 2, figsize=(20, 8))
    colors = cm.Dark2_r(np.linspace(0, 1, num_c))
    for i in range(len(data_eff.columns)):
        axes[0].plot(
            data_pool.index,
            data_eff.iloc[:, i],
            color=colors[i],
            label=data_pool.columns[i],
            linewidth=2,
        )
        axes[1].plot(
            data_eff.index,
            data_eff.iloc[:, i],
            color=colors[i],
            label=data_eff.columns[i],
            linewidth=2,
        )
        axes[1].fill_between(data_pool.index,
                             data_eff.iloc[:, i] - data_std.iloc[:, i],
                             data_eff.iloc[:, i] + data_std.iloc[:, i],
                             alpha=0.2,
                             facecolor=colors[i])

    axes[0].legend()
    axes[0].spines['right'].set_visible(False)
    axes[0].spines['top'].set_visible(False)
    axes[1].legend()
    axes[1].spines['right'].set_visible(False)
    axes[1].spines['top'].set_visible(False)
    if var == 'beta':
        axes[0].set_xlabel('Infection rate')
        axes[0].set_ylabel('Pool size')
        axes[1].set_xlabel('Infection rate')
        axes[1].set_ylabel('Efficiency')
        fig.savefig('beta_compare.jpg')
    if var == 'rho':
        axes[0].set_xlabel('Prevalence')
        axes[0].set_ylabel('Pool size')
        axes[1].set_xlabel('Prevalence')
        axes[1].set_ylabel('Efficiency')
        axes[0].set_xscale('log')
        axes[1].set_xscale('log')
        fig.savefig('rho_compare.jpg')
    fig.show()
Ejemplo n.º 2
0
def plot_scatter_line( data, frac, axes=None, scatters=False, line_style=None, smooth = None,
                      s=None, k=None, colors=None, wind=11, degree=3):
    """
    Plot smooth interpolation of scatter data, the default method is lowess
    :param colors:
    :param split: where to perform subplots
    :param data: pd.Dataframe with x, y in columns
    :param frac: frac for lowess
    :param title: title of the output figure
    :param x_axis: column name of independent variables
    :param scatters: whether show scatters
    :param line_style: linestyle of smooth lines
    :param cubicspl: whether use cubic spline
    :param s: smooth factor for cubic spline
    :param k: k=3 gives cubic spline, k=2 gives quadratic spline, k =1 gives linear
    :return: after run this function, use 'plt' to continue edite the figure.
    """
    if axes is None:
        fig, axes = plt.subplots(figsize=fig_size)
    x_axis = data.columns[0]

    smoo = pd.DataFrame(data[x_axis])
    for col in data.columns[1:]:
        if smooth is 'cubicspline':
            smoo[col] = spline(data[x_axis], data[col], s=s, k=k)
        elif smooth is 'sgf':
            smoo[col] = sg_filter(data[x_axis], data[col], wind=wind, degree=degree)
        elif smooth is 'lowess':
            smoo[col] = lowess(data[x_axis], data[col], frac=frac)
        else:
            pass
    if colors is None:
        colors = cm.Dark2_r(np.linspace(0, 1, len(data.columns[1:])))
    if smooth is not None:
        for i in range(1, len(data.columns)):
            axes.plot(data[x_axis], smoo.iloc[:, i], color=colors[i - 1], label=data.columns[i],
                      linewidth=line_width, linestyle=line_style[i - 1] if line_style else 'solid')
        if scatters:
            axes.scatter(data[x_axis], data.iloc[:, i], color=colors[i - 1], s=scatter_width)
    else:
        for i in range(1, len(data.columns)):
            sns.regplot(x=data[x_axis], y=data.iloc[:, i], color=colors[i - 1],
                        line_kws={'linewidth': line_width}, ax=axes, x_estimator=np.mean)


    plt.xlabel(x_axis)
    plt.legend()
    return fig, axes
Ejemplo n.º 3
0
def scatter_hue(x, y, labels, disc=True, c_label=''):
    """Creates a wall of light curves plot with real and reconstruction
    sequences, paper-ready.

    Parameters
    ----------
    x      : array
        data to be plotted in horizontal axis
    y      : array
        data to be plotted in vertical axis
    labels : list, optional
        list with corresponding lables to be displayed as legends
    disc : bool, optional
        wheather the axis used for coloring is discrete or not
    c_label    : bool, optional
        name of color dimension

    Returns
    -------
        display figure
    """

    fig = plt.figure(figsize=(12, 9))
    if disc:
        c = cm.Dark2_r(np.linspace(0, 1, len(set(labels))))
        for i, cls in enumerate(set(labels)):
            idx = np.where(labels == cls)[0]
            plt.scatter(x[idx],
                        y[idx],
                        marker='.',
                        s=20,
                        color=c[i],
                        alpha=.7,
                        label=cls)
    else:
        plt.scatter(x,
                    y,
                    marker='.',
                    s=20,
                    c=labels,
                    cmap='coolwarm_r',
                    alpha=.7)
        plt.colorbar(label=c_label)

    plt.xlabel('embedding 1')
    plt.ylabel('embedding 2')
    plt.legend(loc='best', fontsize='x-large')
    plt.show()
Ejemplo n.º 4
0
def compare_plots(ac='ac'):
    """
    read and plot comparison data
    :param ac: 'ac' or 'inac'
    """
    simulations = pd.read_csv(ac + 'sim_rho{}_beta{}.csv'.format(
        paras_sample['rho'], paras_sample['beta']))
    num_methods = int((len(simulations.columns) - 1) / 3)
    data_eff = pd.concat([
        simulations['pool_size'],
        simulations.loc[:, simulations.columns.str.startswith('eff')]
    ],
                         axis=1)
    data_fnr = pd.concat([
        simulations['pool_size'],
        simulations.loc[:, simulations.columns.str.startswith('fnr')]
    ],
                         axis=1)
    data_fpr = pd.concat([
        simulations['pool_size'],
        simulations.loc[:, simulations.columns.str.startswith('fpr')]
    ],
                         axis=1)

    # efficiency
    eff_mean = data_eff.groupby(['pool_size']).mean()
    eff_std = data_eff.groupby(['pool_size']).std()
    colors = cm.Dark2_r(np.linspace(0, 1, num_methods))
    fig, axes = plt.subplots(1, 3, figsize=(30, 8))
    for i in range(len(eff_mean.columns)):
        # data_smooth = sg_filter(eff_mean.index, eff_mean.iloc[:, i], wind=11, degree=3)
        axes[0].plot(
            eff_mean.index,
            eff_mean.iloc[:, i],
            color=colors[i],
            label=eff_mean.columns[i],
            linewidth=2,
        )
        axes[0].fill_between(eff_mean.index,
                             eff_mean.iloc[:, i] - eff_std.iloc[:, i],
                             eff_mean.iloc[:, i] + eff_std.iloc[:, i],
                             alpha=0.2,
                             facecolor=colors[i])
    axes[0].legend()

    for col in eff_mean.columns:
        max_eff = round(eff_mean[col].max(), 1)
        arg_eff = eff_mean[col].idxmax()
        if col != 'eff_Individual':
            axes[0].annotate('{}: Efficiency={},k={}'.format(
                col, max_eff, arg_eff),
                             xy=(arg_eff, max_eff))
    axes[0].set_xlabel('Pool size')
    axes[0].set_ylabel('Efficiency')
    axes[0].legend()
    axes[0].set_title('Efficiency( prevalence={}, infection rate={})'.format(
        paras_sample['rho'], paras_sample['beta']))

    # plot fnr
    for i in range(len(data_fnr.columns) - 1):
        sns.regplot(data_fnr['pool_size'],
                    data_fnr.iloc[:, i + 1],
                    color=colors[i],
                    label=data_fnr.columns[i + 1],
                    ax=axes[1],
                    x_bins=10,
                    x_ci=75,
                    truncate=True)
    axes[1].set_xlabel('Pool size')
    axes[1].set_ylabel('FNR')
    axes[1].legend()
    axes[1].set_title('FNR( prevalence={}, infection rate={})'.format(
        paras_sample['rho'], paras_sample['beta']))

    # plot fpr
    for i in range(len(data_fpr.columns) - 1):
        sns.regplot(data_fpr['pool_size'],
                    data_fpr.iloc[:, i + 1],
                    color=colors[i],
                    label=data_fpr.columns[i + 1],
                    ax=axes[2],
                    x_bins=10,
                    x_ci=75,
                    truncate=True)
    axes[2].set_xlabel('Pool size')
    axes[2].set_ylabel('FPR')
    axes[2].legend()
    axes[2].set_title('FPR( prevalence={}, infection rate={})'.format(
        paras_sample['rho'], paras_sample['beta']))
    fig.savefig('Compare_rho{}_beta{}.jpg'.format(paras_sample['rho'],
                                                  paras_sample['beta']))
    fig.show()