def plot_year_on_year_fold_change_by_site(df):

    df = aggregate_counts(df)
    fold_changes = calculate_change_by_month(df)

    palette = load_color_palette()
    fig = plt.figure()
    ax = fig.gca()
    for i, village in enumerate(df['Study Sites'].unique()):
        if village in intervention_villages:
            linestyle = '-o'
            arm = 'int'
        else:
            linestyle = '--o'
            arm = 'con'
        sdf = fold_changes[fold_changes[village] != -1]
        ax.plot(sdf['month'],
                sdf[village] * 100,
                linestyle,
                color=palette[i],
                label='%s %s' % (village, arm))

    ax.set_ylabel('year on year pct change in vector count')
    ax.set_xticklabels(['', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
    ax.legend()

    plt.savefig(
        os.path.join(plotdir, 'HLC_year_on_year_pct_change_by_site.png'))
    plt.show()
Exemplo n.º 2
0
def weekly_deaths():

    df = pd.read_csv(
        os.path.join(box_data_path, 'Cleaned Data',
                     '%s_jg_aggregated_covidregion.csv' % LL_date))
    df['date'] = pd.to_datetime(df['date'])
    df = df[df['date'] >= date(2020, 3, 1)]
    df = df[df['deaths'] > 0]
    firstday = np.min(df['date'])
    df['week'] = df['date'].apply(lambda x: int((x - firstday).days / 7))

    df = df.groupby('week')[['deaths']].agg(np.sum).reset_index()
    df = df.sort_values(by='week')
    df['date'] = df['week'].apply(lambda x: firstday + timedelta(days=7 * x))
    df2 = copy.copy(df[['week', 'deaths']])
    df2['date'] = df2['week'].apply(
        lambda x: firstday + timedelta(days=7 * x + 6))
    df = pd.concat([df, df2])
    df = df.sort_values(by='date')

    palette = load_color_palette('wes')
    formatter = mdates.DateFormatter("%m-%d")
    sns.set_style('whitegrid', {'axes.linewidth': 0.5})
    fig = plt.figure()
    ax = fig.gca()
    ax.plot()

    ax.plot(df['date'], df['deaths'], '-', color=palette[0])
    ax.set_ylabel('deaths')
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())

    fig.savefig(os.path.join(plot_path, 'IL_weekly_deaths_LL%s.png' % LL_date))
    fig.savefig(os.path.join(plot_path, 'IL_weekly_deaths_LL%s.pdf' % LL_date),
                format='PDF')
Exemplo n.º 3
0
def plot_by_intervention(adf, plotname) :

    num_years = len(adf['year'].unique())
    num_targets = len(adf['target'].unique())

    ann_prev_channel = 'annual mean Infected'
    day_prev_channel = 'year_end_Infected'

    sweep_variables = ["x_Temporary_Larval_Habitat",
                       'dirus_Anthropophily',
                       ]

    sns.set_style('whitegrid', {'axes.linewidth' : 0.5})
    palette = load_color_palette('wes')
    for (x_temp, anth), df in adf.groupby(sweep_variables) :
        fig1 = plt.figure('%s Anth%.2f LH%.1f prevalence' % (plotname, anth, x_temp), figsize=(10,6))
        fig1.subplots_adjust(left=0.07, right=0.95)
        axes1 = [fig1.add_subplot(num_targets, num_years, i + 1) for i in range(num_years * num_targets)]
        fig2 = plt.figure('%s Anth%.2f LH%.1f fraction eliminated' % (plotname, anth, x_temp), figsize=(10,6))
        fig2.subplots_adjust(left=0.07, right=0.95)
        axes2 = [fig2.add_subplot(num_targets, num_years, i + 1) for i in range(num_years * num_targets)]
        for i, ((target, year), gdf) in enumerate(df.groupby(['target', 'year'])) :
            ax1 = axes1[i]
            ax2 = axes2[i]
            for j, ((intervention, ivm_duration), ddf) in enumerate(gdf.groupby(['intervention','IVM duration'])) :
                if 'drug' in intervention :
                    if 'ivermectin' not in intervention :
                        label = 'DP'
                    else :
                        label = 'DP + End%d' % ivm_duration
                elif 'ivermectin' in intervention :
                    label = 'End%d' % ivm_duration
                else :
                    label = ''

                sdf = ddf.groupby('coverage')[ann_prev_channel].agg(np.mean).reset_index()
                ax1.plot(sdf['coverage'], sdf[ann_prev_channel], color=palette[j], label=label)
                sdf = ddf.groupby('coverage')[day_prev_channel].agg(count_zero).reset_index()
                ax2.plot(sdf['coverage'], sdf[day_prev_channel], color=palette[j], label=label)

            for ax in [ax1, ax2] :
                ax.set_xlim(0,1)
                if i%num_years > 0 :
                    ax.set_yticklabels([])
                else :
                    ax.set_ylabel(target)
                if i < num_years :
                    ax.set_title('year %d' % year)
                if i < len(axes1)-num_years :
                    ax.set_xticklabels([])
                else :
                    ax.set_xlabel('coverage')
            ax1.set_ylim(0, 0.2)
            ax2.set_ylim(0, 1)
        axes1[-1].legend()
        axes2[-1].legend()
        fig2.savefig(os.path.join(plotdir, '%s Anth%.2f LH%.1f fraction eliminated.png' % (plotname, anth, x_temp)))
        fig2.savefig(os.path.join(plotdir, '%s Anth%.2f LH%.1f fraction eliminated.pdf' % (plotname, anth, x_temp)),
                     format='PDF')
Exemplo n.º 4
0
def plot_county_line_by_region(region_key) :

    df = assign_counties_restore_region()
    df = df.sort_values(by=['update_date', region_key, 'County'])

    regions = pd.DataFrame( { 'region' : df[region_key].unique(),
                              'cindex' : range(len(df[region_key].unique()))})
    regions = regions.set_index('region')

    fig_cases = setup_fig('cases')
    fig_tpr = setup_fig('TPR')
    palette = load_color_palette('wes')

    ci = 0
    for ri, (reg, rdf) in enumerate(df.groupby(region_key)) :
        for county, cdf in rdf.groupby('County') :
            cdf['daily_pos'] = np.insert(np.diff(cdf['Positive_Cases']), 0, 0)
            cdf['daily_test'] = np.insert(np.diff(cdf['Tested']), 0, 0)
            cdf.loc[cdf['daily_test'] == 0, 'daily_test'] = 1
            cdf['daily_tpr'] = cdf['daily_pos']/cdf['daily_test']

            ax = fig_cases.add_subplot(9,12,ci+1)
            colorbin = regions.at[reg, 'cindex']%len(palette)
            if len(cdf) < 10 :
                ax.bar(cdf['update_date'].values[1:], np.diff(cdf['Positive_Cases']),
                       align='center', color=palette[colorbin], linewidth=0, alpha=0.3)
                max_pos = np.max(cdf['Positive_Cases'])
            else :
                cdf['moving_ave'] = cdf['daily_pos'].rolling(window=7, center=False).mean()
                max_pos = np.max(cdf['moving_ave'])
                ax.plot(cdf['update_date'], cdf['moving_ave'], '-', color=palette[colorbin])
                ax.fill_between(cdf['update_date'].values, [0]*len(cdf['moving_ave']), cdf['moving_ave'],
                                linewidth=0, color=palette[colorbin], alpha=0.3)
            format_axis(ax, ci, df, county, max_pos, 'cases')

            ax = fig_tpr.add_subplot(9,12,ci+1)
            cdf = cdf[cdf['Positive_Cases'] <= cdf['Tested']]
            if len(cdf) < 10 :
                ax.plot(cdf['update_date'], cdf['daily_tpr'], '-', color=palette[colorbin])
                ax.fill_between(cdf['update_date'].values, [0] * len(cdf['daily_tpr']), cdf['daily_tpr'],
                                linewidth=0, color=palette[colorbin], alpha=0.3)
                max_pos = np.max(cdf['daily_tpr'])
            else :
                cdf['moving_ave'] = cdf['daily_tpr'].rolling(window=7, center=False).mean()
                ax.plot(cdf['update_date'], cdf['moving_ave'], '-', color=palette[colorbin])
                ax.fill_between(cdf['update_date'].values, [0]*len(cdf['moving_ave']), cdf['moving_ave'],
                                linewidth=0, color=palette[colorbin], alpha=0.3)
                max_pos = np.max(cdf['moving_ave'])
            format_axis(ax, ci, df, county, max_pos, 'TPR')
            ci += 1

    fname = 'region' if region_key == 'restore_region' else 'covid_region'

    fig_cases.savefig(os.path.join(plot_dir, 'idph_public_county_%s_cases.pdf' % fname), format='PDF')
    fig_tpr.savefig(os.path.join(plot_dir, 'idph_public_county_%s_tpr.pdf' % fname), format='PDF')
    plt.close(fig_cases)
    plt.close(fig_tpr)
Exemplo n.º 5
0
def plot_IL_cases() :

    IL_fname = os.path.join(datapath, 'Corona virus reports', 'illinois_public.csv')
    df = pd.read_csv(IL_fname, parse_dates=['update_date'])
    df = df.rename(columns={'update_date' : 'date'})
    df = df.sort_values(by='date')
    df = df.fillna(0)
    palette = load_color_palette('wes')

    formatter = mdates.DateFormatter("%m-%d")
    sns.set_style('whitegrid', {'axes.linewidth' : 0.5})
    fig = plt.figure(figsize=(8,8))
    fig.subplots_adjust(left=0.1, right=0.97, bottom=0.05, top=0.97)

    ax = fig.add_subplot(4,1,1)
    df['daily_pos'] = np.insert(np.diff(df['tests_pos']), 0, 0)
    ax.bar(df['date'].values[1:], np.diff(df['tests_pos']),
           align='center', color=palette[0], linewidth=0, alpha=0.5)
    df['moving_ave'] = df['daily_pos'].rolling(window=7, center=False).mean()
    ax.plot(df['date'], df['moving_ave'], '-', color=palette[0])
    ax.set_ylabel('positives')
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())

    ax = fig.add_subplot(4,1,2)
    ax.bar(df['date'].values[1:], df['new_tests'][1:],
           align='center', color=palette[1], linewidth=0, alpha=0.5)
    df['moving_ave'] = df['new_tests'].rolling(window=7, center=False).mean()
    ax.plot(df['date'], df['moving_ave'], '-', color=palette[1])
    ax.set_ylabel('tests')
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())

    ax = fig.add_subplot(4,1,3)
    df['daily_tpr'] = df['daily_pos']/df['new_tests']
    ax.bar(df['date'].values[1:], df['daily_tpr'][1:],
           align='center', color=palette[2], linewidth=0, alpha=0.5)
    df['moving_ave'] = df['daily_tpr'].rolling(window=7, center=False).mean()
    ax.plot(df['date'], df['moving_ave'], '-', color=palette[2])
    ax.set_ylabel('TPR')
    ax.set_ylim(0, 0.3)
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    print(df[['date', 'daily_tpr']].tail(20))

    ax = fig.add_subplot(4,1,4)
    ax.bar(df['date'].values[1:], np.diff(df['deaths']),
           align='center', color=palette[3], linewidth=0, alpha=0.5)
    df['daily_death'] = np.insert(np.diff(df['deaths']), 0, 0)
    df['moving_ave'] = df['daily_death'].rolling(window=7, center=False).mean()
    ax.plot(df['date'], df['moving_ave'], '-', color=palette[3])
    ax.set_ylabel('deaths')
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    fig.savefig(os.path.join(plot_dir, 'idph_public_cases_and_deaths.pdf'), format='PDF')
    plt.close(fig)
Exemplo n.º 6
0
def plot_by_event_count(adf, plotname) :

    num_years = len(adf['year'].unique())
    num_targets = len(adf['target'].unique())
    day_prev_channel = 'year_end_Infected'

    palette = load_color_palette('wes')

    sns.set_style('whitegrid', {'axes.linewidth' : 0.5})
    fig = plt.figure(plotname, figsize=(10, 6))
    fig.subplots_adjust(left=0.07, right=0.95)
    axes = [fig.add_subplot(num_targets, num_years, i + 1) for i in range(num_years * num_targets)]
    for i, ((target, year), gdf) in enumerate(adf.groupby(['target', 'year'])):
        ax = axes[i]

        for j, ((intervention, ivm_duration), ddf) in enumerate(gdf.groupby(['intervention', 'IVM duration'])):
            if 'drug' in intervention:
                if 'ivermectin' not in intervention:
                    label = 'DP'
                else:
                    label = 'DP + End%d' % ivm_duration
            elif 'ivermectin' in intervention:
                label = 'End%d' % ivm_duration
            else:
                label = ''

            cdf = adf[(adf['target'] == target) & (adf['intervention'] == intervention) &
                      (adf['IVM duration'] == ivm_duration) & (adf['year'] <= year)]
            num_seeds = np.max(ddf['Run_Number'])

            zeroes = ddf.groupby('coverage')[day_prev_channel].agg(count_zero).reset_index()
            events = cdf.groupby('coverage')['Event_Count'].agg(np.sum).reset_index()
            ax.plot(events['Event_Count']/num_seeds, zeroes[day_prev_channel], color=palette[j], label=label)

        if i % num_years > 0:
            ax.set_yticklabels([])
        else:
            ax.set_ylabel(target)
        if i < num_years:
            ax.set_title('year %d' % year)
        if i < len(axes) - num_years:
            ax.set_xticklabels([])
        else:
            ax.set_xlabel('doses distributed')
        ax.set_ylim(0, 1)
        ax.set_xlim(0, 1700)
    axes[-1].legend()
    fig.savefig(os.path.join(plotdir, '%s fraction eliminated v doses.png' % plotname))
    fig.savefig(os.path.join(plotdir, '%s fraction eliminated v doses.pdf' % plotname), format='PDF')
Exemplo n.º 7
0
def plot_number_tested_timeseries(adf):

    cols = ['num_delivered', 'num_tested']
    df = adf.groupby('delivery_date')[cols].agg(np.sum).reset_index()
    df = df.sort_values(by='delivery_date')
    df['excess_delivered'] = df['num_delivered'] - df['num_tested']

    sdf = df[(df['delivery_date'] < date(2020, 8, 10))]
    print('avg daily deliveries during pilot', np.mean(sdf['num_delivered']))
    sdf = df[(df['delivery_date'] >= date(2020, 8, 10))
             & (df['delivery_date'] < date(2021, 1, 10))]
    print('avg daily deliveries during scale up',
          np.mean(sdf['num_delivered']))

    sns.set_style('whitegrid', {'axes.linewidth': 0.5})
    fig = plt.figure(figsize=(8, 4))
    ax = fig.gca()
    palette = [load_color_palette('wes')[x] for x in [3, 4]]
    formatter = mdates.DateFormatter("%m-%d")

    ax.bar(df['delivery_date'],
           df['num_tested'],
           color=palette[1],
           align='center',
           linewidth=0,
           alpha=0.5)
    ax.bar(df['delivery_date'],
           df['excess_delivered'],
           bottom=df['num_tested'],
           color=palette[0],
           align='center',
           linewidth=0,
           alpha=0.5)
    for c, col in enumerate(cols):
        df['moving_ave'] = df[col].rolling(window=7, center=True).mean()
        ax.plot(df['delivery_date'],
                df['moving_ave'],
                '-',
                color=palette[c],
                label=col.split('_')[-1])
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.legend()
    ax.set_ylabel('number of individuals')
    ax.set_xlabel('date of delivery')
    fig.savefig(os.path.join(plotdir, 'daily_number_tested.png'))
    fig.savefig(os.path.join(plotdir, 'daily_number_tested.pdf'), format='PDF')
def plot_hlc_by_site():

    df = load_df()

    palette = load_color_palette()

    fig = plt.figure('HLC vector counts', figsize=(16, 10))
    axes = [fig.add_subplot(4, 4, x + 1) for x in range(14)]
    fig.subplots_adjust(left=0.05, right=0.97, top=0.95, bottom=0.05)
    linestyles = ['-o', '--o']
    for locindex, (location, ldf) in enumerate(df.groupby('Position')):
        grouped = ldf.groupby(['Study Sites', 'date'])
        sdf = grouped['mosquito ID'].agg(len).reset_index()
        sdf.rename(columns={'mosquito ID': 'vector count'}, inplace=True)
        sdf.sort_values(by=['Study Sites', 'date'], inplace=True)

        for s, (site, ssdf) in enumerate(sdf.groupby('Study Sites')):
            ax = axes[s]
            ax.plot(ssdf['date'],
                    ssdf['vector count'],
                    linestyles[locindex],
                    color=palette[s],
                    label=location if s == 0 else '')
            ax.set_title(site)
            ax.set_xlim(np.min(df['date']), np.max(df['date']))
            if s < 10:
                ax.set_xticklabels([])
            else:
                ax.set_xticklabels([
                    'Jun \'16', '', '', 'Dec \'16', '', '', 'Jun \'17', '', '',
                    'Dec \'17', ''
                ])
            if s == 0 and locindex > 0:
                ax.legend()
            if s % 4 == 0:
                ax.set_ylabel('mosquito count')
            if locindex == 1 and site in intervention_villages:
                ymin, ymax = ax.get_ylim()
                ax.fill_between([atsb_start, np.max(df['date'])], [ymin, ymin],
                                [ymax, ymax],
                                color='k',
                                alpha=0.25,
                                linewidth=0)
                ax.set_ylim(ymin, ymax)
    plt.savefig(os.path.join(plotdir, 'HLC_by_site.png'))
    plt.savefig(os.path.join(plotdir, 'HLC_by_site.pdf'), format='PDF')
Exemplo n.º 9
0
def plot_by_anth(adf, plotname) :

    num_years = len(adf['year'].unique())
    num_targets = len(adf['target'].unique())
    day_prev_channel = 'year_end_Infected'

    palette = load_color_palette('wes')

    sns.set_style('whitegrid', {'axes.linewidth' : 0.5})
    fig = plt.figure(plotname, figsize=(10, 6))
    fig.subplots_adjust(left=0.07, right=0.95)
    axes = [fig.add_subplot(num_targets, num_years, i + 1) for i in range(num_years * num_targets)]
    for i, ((target, year), gdf) in enumerate(adf.groupby(['target', 'year'])):
        ax = axes[i]

        for j, ((intervention, ivm_duration), ddf) in enumerate(gdf.groupby(['intervention', 'IVM duration'])):
            if 'drug' in intervention:
                if 'ivermectin' not in intervention:
                    label = 'DP'
                else:
                    label = 'DP + End%d' % ivm_duration
            elif 'ivermectin' in intervention:
                label = 'End%d' % ivm_duration
            else:
                label = ''

            sdf = ddf.groupby('dirus_Anthropophily')[day_prev_channel].agg(count_zero).reset_index()
            ax.plot(sdf['dirus_Anthropophily'], sdf[day_prev_channel], color=palette[j], label=label)

        if i % num_years > 0:
            ax.set_yticklabels([])
        else:
            ax.set_ylabel(target)
        if i < num_years:
            ax.set_title('year %d' % year)
        if i < len(axes) - num_years:
            ax.set_xticklabels([])
        else:
            ax.set_xlabel('dirus anthropophily')
        ax.set_ylim(0, 1)
        ax.set_xlim(0, 1)
    axes[-1].legend()

    fig.savefig(os.path.join(plotdir, '%s fraction eliminated.png' % plotname))
    fig.savefig(os.path.join(plotdir, '%s fraction eliminated.pdf' % plotname), format='PDF')
Exemplo n.º 10
0
def plot_LL_all_IL():

    df = pd.read_csv(
        os.path.join(box_data_path, 'Cleaned Data',
                     '%s_jg_aggregated_covidregion.csv' % LL_date))
    df = df.groupby('date')[['cases', 'deaths',
                             'admissions']].agg(np.sum).reset_index()
    df['date'] = pd.to_datetime(df['date'])
    df = df.sort_values(by='date')
    df = df[df['date'] >= date(2020, 3, 15)]

    palette = load_color_palette('wes')
    formatter = mdates.DateFormatter("%m-%d")
    sns.set_style('whitegrid', {'axes.linewidth': 0.5})
    fig = plt.figure(figsize=(8, 6))
    fig.subplots_adjust(left=0.1, right=0.97, bottom=0.05, top=0.97)

    def plot_data(adf, ax, col, color):
        ax.bar(adf['date'].values,
               adf[col],
               align='center',
               color=color,
               linewidth=0,
               alpha=0.5)
        adf['moving_ave'] = adf[col].rolling(window=7, center=True).mean()
        ax.plot(adf['date'], adf['moving_ave'], '-', color=color)
        ax.set_ylabel('positives')
        ax.xaxis.set_major_formatter(formatter)
        ax.xaxis.set_major_locator(mdates.MonthLocator())
        ax.set_ylabel(col)

    ax = fig.add_subplot(3, 1, 1)
    plot_data(df, ax, 'cases', palette[0])
    ax = fig.add_subplot(3, 1, 2)
    plot_data(df, ax, 'admissions', palette[4])
    ax = fig.add_subplot(3, 1, 3)
    plot_data(df, ax, 'deaths', palette[3])

    fig.savefig(os.path.join(plot_path, 'IL_cases_deaths_LL%s.png' % LL_date))
    fig.savefig(os.path.join(plot_path, 'IL_cases_deaths_LL%s.pdf' % LL_date),
                format='PDF')
Exemplo n.º 11
0
def plot_by_ivm_duration(df, plotname) :

    num_years = len(df['year'].unique())
    num_targets = len(df['target'].unique())
    min_ivm_duration = np.min(df[df['IVM duration'] > 0]['IVM duration'])
    max_ivm_duration = np.max(df['IVM duration'])

    day_prev_channel = 'year_end_Infected'

    sns.set_style('whitegrid', {'axes.linewidth' : 0.5})
    palette = load_color_palette('wes')
    fig = plt.figure(plotname, figsize=(10,6))
    fig.subplots_adjust(left=0.07, right=0.95)
    axes = [fig.add_subplot(num_targets, num_years, i + 1) for i in range(num_years * num_targets)]
    for i, ((target, year), gdf) in enumerate(df.groupby(['target', 'year'])) :
        ax = axes[i]
        for j, (intervention, ddf) in enumerate(gdf.groupby(['intervention'])) :

            sdf = ddf.groupby('IVM duration')[day_prev_channel].agg(count_zero).reset_index()
            if 'ivermectin' not in intervention :
                ax.plot([min_ivm_duration, max_ivm_duration], [sdf[day_prev_channel], sdf[day_prev_channel]],
                        color=palette[j], label=intervention)
            else :
                ax.plot(sdf['IVM duration'], sdf[day_prev_channel], color=palette[j], label=intervention)

        if i%num_years > 0 :
            ax.set_yticklabels([])
        else :
            ax.set_ylabel(target)
        if i < num_years :
            ax.set_title('year %d' % year)
        if i < len(axes)-num_years :
            ax.set_xticklabels([])
        else :
            ax.set_xlabel('IVM duration')
        ax.set_ylim(0, 1)
    axes[-1].legend()
    fig.savefig(os.path.join(plotdir, '%s fraction eliminated v IVM duration.png' % plotname))
    fig.savefig(os.path.join(plotdir, '%s fraction eliminated v IVM duration.pdf' % plotname), format='PDF')
Exemplo n.º 12
0
    def finalize(self, all_data):

        selected = [data for sim, data in all_data.items()]
        if len(selected) == 0:
            print("No data have been returned... Exiting...")
            return

        df = pd.concat(selected).reset_index(drop=True)
        num_nodes = len(df['node'].unique())
        num_channels = len(self.spatial_channels)

        sns.set_style('white', {'axes.linewidth': 0.5})
        palette = load_color_palette()
        fig = plt.figure('Spatial Outputs')
        for c, channel in enumerate(self.spatial_channels):
            gdf = df.groupby(['node',
                              'time'])[channel].agg([np.mean, top95,
                                                     bot5]).reset_index()
            for n, (node, ndf) in enumerate(gdf.groupby('node')):
                ax = fig.add_subplot(num_channels, num_nodes,
                                     c * num_nodes + n + 1)
                ax.plot(ndf['time'], ndf['mean'], color=palette[n], label=node)
                ax.fill_between(ndf['time'],
                                ndf['bot5'],
                                ndf['top95'],
                                color=palette[n],
                                alpha=0.3,
                                linewidth=0)
                if n == 0:
                    ax.set_ylabel(channel)
                if c == 0:
                    ax.set_title('node %d' % node)
                if c == num_channels - 1:
                    ax.set_xlabel('day')

        plt.show()
Exemplo n.º 13
0
def plot_emresource(scale=''):

    ems_regions = {
        'northcentral': [1, 2],
        'northeast': [7, 8, 9, 10, 11],
        'central': [3, 6],
        'southern': [4, 5]
    }

    ref_df = pd.read_csv(
        os.path.join(datapath, 'covid_IDPH', 'Corona virus reports',
                     'emresource_by_region.csv'))

    sxmin = '2020-03-24'
    xmin = datetime.strptime(sxmin, '%Y-%m-%d')
    xmax = datetime.today()
    datetoday = xmax.strftime('%y%m%d')

    ref_df['suspected_and_confirmed_covid_icu'] = ref_df[
        'suspected_covid_icu'] + ref_df['confirmed_covid_icu']
    ref_df['date'] = pd.to_datetime(ref_df['date_of_extract'])
    first_day = datetime.strptime('2020-03-24', '%Y-%m-%d')

    ref_df = ref_df.rename(
        columns={
            'confirmed_covid_deaths_prev_24h': 'deaths',
            'confirmed_covid_icu': 'ICU conf',
            'confirmed_covid_on_vents': 'vents conf',
            'suspected_and_confirmed_covid_icu': 'ICU conf+susp',
            'covid_non_icu': 'non ICU'
        })

    channels = ['ICU conf+susp', 'ICU conf', 'vents conf', 'deaths', 'non ICU']
    ref_df = ref_df[['date', 'covid_region'] + channels]

    palette = load_color_palette('wes')
    formatter = mdates.DateFormatter("%m-%d")

    fig_all = plt.figure(figsize=(10, 8))
    fig = plt.figure(figsize=(14, 10))
    fig.subplots_adjust(left=0.07,
                        right=0.97,
                        top=0.95,
                        bottom=0.05,
                        hspace=0.25)

    def format_plot(ax):
        ax.set_xlim(xmin, )
        ax.xaxis.set_major_formatter(formatter)
        ax.xaxis.set_major_locator(mdates.MonthLocator())
        if scale == 'log':
            ax.set_yscale('log')

    for ri, (restore_region, ems_list) in enumerate(ems_regions.items()):
        ax_all = fig_all.add_subplot(2, 2, ri + 1)
        ax = fig.add_subplot(4, 6, 6 * ri + 1)

        pdf = ref_df[ref_df['covid_region'].isin(ems_list)].groupby(
            'date').agg(np.sum).reset_index()
        for (c, name) in enumerate(channels):
            if name == 'non ICU':
                df = pdf[pdf['date'] >= date(2020, 5, 6)]
            else:
                df = pdf
            df['moving_ave'] = df[name].rolling(window=7, center=True).mean()
            ax_all.plot(df['date'].values,
                        df['moving_ave'],
                        color=palette[c],
                        label=name)
            ax_all.scatter(df['date'].values,
                           df[name],
                           s=10,
                           linewidth=0,
                           color=palette[c],
                           alpha=0.3,
                           label='')
            ax.plot(df['date'].values,
                    df['moving_ave'],
                    color=palette[c],
                    label=name)
            ax.scatter(df['date'].values,
                       df[name],
                       s=10,
                       linewidth=0,
                       color=palette[c],
                       alpha=0.3,
                       label='')
        ax_all.set_title(restore_region)
        format_plot(ax_all)
        if ri == 1:
            ax_all.legend()

        format_plot(ax)
        ax.set_ylabel(restore_region)
        ax.set_title('total')

        for ei, ems in enumerate(ems_list):
            ax = fig.add_subplot(4, 6, 6 * ri + 1 + ei + 1)
            df = ref_df[ref_df['covid_region'] == ems]
            for (c, name) in enumerate(channels):
                df['moving_ave'] = df[name].rolling(window=7,
                                                    center=True).mean()
                ax.plot(df['date'].values,
                        df['moving_ave'],
                        color=palette[c],
                        label=name)
                ax.scatter(df['date'].values,
                           df[name],
                           s=10,
                           linewidth=0,
                           color=palette[c],
                           alpha=0.3,
                           label='')
            ax.set_title('covid region %d' % ems)
            format_plot(ax)
            if ems == 2:
                ax.legend(bbox_to_anchor=(1.5, 1))

    fig_all.savefig(
        os.path.join(plotdir, 'EMResource_by_restore_region_%s.png' % scale))
    fig_all.savefig(os.path.join(
        plotdir, 'EMResource_by_restore_region_%s.pdf' % scale),
                    format='PDF')
    fig.savefig(
        os.path.join(plotdir, 'EMResource_by_covid_region_%s.png' % scale))
    fig.savefig(os.path.join(plotdir,
                             'EMResource_by_covid_region_%s.pdf' % scale),
                format='PDF')
    def finalize(self, all_data):

        selected = [data for sim, data in all_data.items()]
        if len(selected) == 0:
            print("No data have been returned... Exiting...")
            return

        adf = pd.concat(selected).reset_index(drop=True)
        num_migration_types = len(adf['MigrationType'].unique())

        sns.set_style('white', {'axes.linewidth': 0.5})
        palette = load_color_palette()
        fig = plt.figure('Migration', figsize=(16, 7))
        fig.subplots_adjust(right=0.98, left=0.05, wspace=0.25)

        for m, (mig, mdf) in enumerate(adf.groupby('MigrationType')):
            ax = fig.add_subplot(num_migration_types, 4, m * 4 + 1)
            num_events = mdf.groupby('Run_Number')['Event'].agg(len).values
            sns.distplot(num_events, ax=ax, color=palette[m], label=mig)
            ax.set_xlabel('number of trips')
            ax.set_ylabel('frac of sims')
            ax.legend()

            ax = fig.add_subplot(num_migration_types, 4, m * 4 + 2)
            for r, rdf in mdf.groupby('Run_Number'):
                sns.kdeplot(rdf['AgeYears'],
                            ax=ax,
                            color=palette[m],
                            linewidth=0.5,
                            alpha=0.5,
                            label='')
            sns.kdeplot(mdf['AgeYears'], ax=ax, color=palette[m], label='')
            ax.set_xlabel('Traveler age')
            ax.set_ylabel('fraction of trips')
            ax.set_xlim(-5, 205)

            ax = fig.add_subplot(num_migration_types, 4, m * 4 + 3)
            for r, rdf in mdf.groupby('Run_Number'):
                trip_durations = []
                grouped = rdf.groupby('IndividualID')
                for gn, gdf in grouped:
                    t = gdf['Time'].values
                    d = [
                        t[2 * x + 1] - t[2 * x] for x in range(int(len(t) / 2))
                    ]
                    trip_durations.extend(d)

                sns.kdeplot(trip_durations,
                            ax=ax,
                            linewidth=0.5,
                            color=palette[m])
            ax.set_xlabel('days at destination')
            ax.set_ylabel('fraction of trips')

            ax = fig.add_subplot(num_migration_types, 4, m * 4 + 4)
            if mig == 'intervention':
                for r, rdf in mdf.groupby('Run_Number'):
                    sns.kdeplot(
                        rdf[rdf['To_NodeID'] == self.forest_nodeid]['Time'],
                        ax=ax,
                        color=palette[2],
                        label='',
                        linewidth=0.5,
                        alpha=0.5)
                sns.kdeplot(
                    mdf[mdf['To_NodeID'] == self.forest_nodeid]['Time'],
                    ax=ax,
                    color=palette[2],
                    label='to forest')
                for r, rdf in mdf.groupby('Run_Number'):
                    sns.kdeplot(
                        rdf[rdf['From_NodeID'] == self.forest_nodeid]['Time'],
                        ax=ax,
                        color=palette[3],
                        label='',
                        linewidth=0.5,
                        alpha=0.5)
                sns.kdeplot(
                    mdf[mdf['From_NodeID'] == self.forest_nodeid]['Time'],
                    ax=ax,
                    color=palette[3],
                    label='from forest')
            else:
                for r, rdf in mdf.groupby('Run_Number'):
                    sns.kdeplot(rdf['Time'],
                                ax=ax,
                                color=palette[3],
                                label='',
                                linewidth=0.5,
                                alpha=0.5)
                sns.kdeplot(mdf['Time'], ax=ax, label='', color=palette[3])
            ax.set_xlim(-5, 370)
            ax.set_xlabel('travel day')
            ax.set_ylabel('fraction of trips')

        plt.show()
Exemplo n.º 15
0
def plot_emresource(scale='') :

    ems_regions = {
        'northcentral' : [1, 2],
        'northeast' : [7, 8, 9, 10, 11],
        'central' : [3, 6],
        'southern' : [4, 5]
    }

    ref_df = pd.read_csv(os.path.join(datapath, 'covid_IDPH', 'Corona virus reports',
                                      'emresource_by_region.csv'))

    sxmin = '2020-03-24'
    xmin = datetime.strptime(sxmin, '%Y-%m-%d')
    xmax = datetime.today()
    datetoday = xmax.strftime('%y%m%d')

    ref_df['suspected_and_confirmed_covid_icu'] = ref_df['suspected_covid_icu'] + ref_df['confirmed_covid_icu']
    ref_df['date'] = pd.to_datetime(ref_df['date_of_extract'])
    first_day = datetime.strptime('2020-03-24', '%Y-%m-%d')

    ref_df = ref_df.rename(columns={
        'confirmed_covid_deaths_prev_24h' : 'deaths',
        'confirmed_covid_icu' : 'ICU conf',
        'confirmed_covid_on_vents' : 'vents conf',
        'suspected_and_confirmed_covid_icu' : 'ICU conf+susp',
        'covid_non_icu' : 'non ICU'
    })

    # channels = ['ICU conf+susp', 'ICU conf', 'vents conf', 'deaths', 'non ICU']
    channels = ['ICU conf', 'non ICU']
    ref_df = ref_df[['date', 'covid_region'] + channels]

    palette = load_color_palette('wes')
    formatter = mdates.DateFormatter("%m-%d")

    sns.set_style('whitegrid', {'axes.linewidth' : 0.5})
    fig = plt.figure(figsize=(14,10))
    fig.subplots_adjust(left=0.07, right=0.97, top=0.95, bottom=0.05, hspace=0.25)

    # ref_df = ref_df[(ref_df['date'] >= date(2020, 3, 1)) & (ref_df['date'] < date(2021, 1, 1))]

    def format_plot(ax) :
        ax.set_xlim(xmin, )
        ax.xaxis.set_major_formatter(formatter)
        ax.xaxis.set_major_locator(mdates.MonthLocator())
        if scale == 'log' :
            ax.set_yscale('log')

    for ri, (covid_region, pdf) in enumerate(ref_df.groupby('covid_region')) :
        ax = fig.add_subplot(4,3,ri+1)

        for (c,name) in enumerate(channels):
            if name == 'non ICU' :
                df = pdf[pdf['date'] >= date(2020,5,6)]
            else :
                df = pdf
            df['moving_ave'] = df[name].rolling(window = 7, center=True).mean()
            ax.plot(df['date'].values, df['moving_ave'], color=palette[c], label=name)
            ax.scatter(df['date'].values, df[name], s=10, linewidth=0, color=palette[c], alpha=0.3, label='')
            if name == 'non ICU' :
                ax.set_ylim(0,)
            # ax.set_xlim(date(2020,3,25), date(2021,1,1))

        ax.set_title('covid region %d' % covid_region)
        format_plot(ax)

    il_df = ref_df.groupby('date')[channels].agg(np.sum).reset_index()
    # print(il_df.tail(10))
    ax = fig.add_subplot(4, 3, 12)
    for (c, name) in enumerate(channels):
        if name == 'non ICU':
            df = il_df[il_df['date'] >= date(2020, 5, 6)]
        else:
            df = il_df
        df['moving_ave'] = df[name].rolling(window=7, center=True).mean()
        ax.plot(df['date'].values, df['moving_ave'], color=palette[c], label=name)
        ax.scatter(df['date'].values, df[name], s=10, linewidth=0, color=palette[c], alpha=0.3, label='')

    ax.set_title('Illinois')
    format_plot(ax)
    ax.legend()
    # ax.legend(bbox_to_anchor=(1.5, 1))

    fig.savefig(os.path.join(plotdir, 'EMResource_by_covid_region_%s.png' % scale))
    fig.savefig(os.path.join(plotdir, 'EMResource_by_covid_region_%s.pdf' % scale), format='PDF')
Exemplo n.º 16
0
def plot_tpr(adf):

    cols = ['num_positive', 'num_tested']
    df = adf[adf['covid_region'] == 11].groupby('delivery_date')[cols].agg(
        np.sum).reset_index()
    df = df.sort_values(by='delivery_date')
    df = df[df['num_tested'] > 0]
    df['fraction_positive'] = df['num_positive'] / df['num_tested']

    sns.set_style('whitegrid', {'axes.linewidth': 0.5})
    fig = plt.figure(figsize=(8, 4))
    ax = fig.add_subplot(2, 1, 1)
    palette = [load_color_palette('wes')[x] for x in [5, 4]]
    formatter = mdates.DateFormatter("%m-%d")

    # ax.scatter(df['delivery_date'].values, df['fraction_positive'].values, df['num_tested'], color=palette[0],
    #            label='daily')
    df['moving_ave_frac'] = df['fraction_positive'].rolling(
        window=7, center=True).mean()
    ax.plot(df['delivery_date'],
            df['moving_ave_frac'],
            '-',
            color=palette[0],
            label='moving average')

    df['moving_ave_test'] = df['num_tested'].rolling(window=7,
                                                     center=True).sum()
    df['moving_ave_pos'] = df['num_positive'].rolling(window=7,
                                                      center=True).sum()
    lows, highs = [], []
    for r, row in df.iterrows():
        low, high = proportion_confint(row['moving_ave_pos'],
                                       row['moving_ave_test'])
        lows.append(low)
        highs.append(high)

    ax.fill_between(df['delivery_date'].values,
                    lows,
                    highs,
                    color=palette[0],
                    linewidth=0,
                    alpha=0.3)

    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.set_ylabel('percent positive')
    ax.set_title('Region 11')
    ax.set_xlim(date(2020, 6, 10), date(2021, 1, 15))

    ax = fig.add_subplot(2, 1, 2)
    cdf = pd.read_csv(
        os.path.join(datapath, 'Corona virus reports', 'CLI_admissions.csv'))
    cdf = cdf[cdf['region'] == 'Chicago']
    cdf['date'] = pd.to_datetime(cdf['date'])
    cdf = cdf[(cdf['date'] >= np.min(df['delivery_date']) - timedelta(days=3))
              &
              (cdf['date'] <= np.max(df['delivery_date'] + timedelta(days=3)))]
    cdf = cdf.groupby('date')['inpatient'].agg(np.sum).reset_index()
    cdf = cdf.sort_values(by='date')
    cdf['moving_ave_cli'] = cdf['inpatient'].rolling(window=7,
                                                     center=True).mean()
    ax.plot(cdf['date'], cdf['moving_ave_cli'], '-', color=palette[1])
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.set_ylabel('CLI admissions')
    ax.set_xlabel('date')
    ax.set_ylim(0, )
    ax.set_xlim(date(2020, 6, 10), date(2021, 1, 15))

    fig.savefig(os.path.join(plotdir, 'TPR_and_CLI.png'))
    fig.savefig(os.path.join(plotdir, 'TPR_and_CLI.pdf'), format='PDF')
Exemplo n.º 17
0
def plot_data(df,
              tag,
              to_compare=None,
              ymin=None,
              ymax=None,
              savename='None',
              save=1):

    fig = plt.figure(tag, figsize=(5, 5))
    fig.subplots_adjust(left=0.05, right=0.98)
    palette = load_color_palette()

    for i, ia in enumerate(['Total']):
        ax = fig.add_subplot(1, 1, i + 1)
        plt.grid()
        for iarm, (comp, compdf) in enumerate(df.groupby(to_compare)):
            if comp == 'SMC15':
                ax.plot(compdf['Coverage'],
                        compdf[ia + '_mean'],
                        '-',
                        color=palette[iarm],
                        label=comp)
            elif comp == 'SMC10':
                ax.plot(compdf['Coverage'],
                        compdf[ia + '_mean'],
                        '-',
                        color=palette[iarm],
                        label=comp)
            else:
                ax.plot(compdf['Coverage'],
                        compdf[ia + '_mean'],
                        '-',
                        color=palette[iarm],
                        label=comp.split('+')[2])
            ax.fill_between(compdf['Coverage'],
                            compdf[ia + '_mean'] - compdf[ia + '_std'],
                            compdf[ia + '_mean'] + compdf[ia + '_std'],
                            color=palette[iarm],
                            linewidth=0,
                            alpha=0.3)
        if ia == 'Annual EIR':
            ax.set_title('%s' % ia)
        else:
            ax.set_title('clinical cases')
        ax.set_ylim(ymin, ymax)
        ax.set_xlim(0.0, 1.0)
        if i == 0:
            ax.legend(bbox_to_anchor=(1.05, 0.5), )
    fig.suptitle('%s' % savename)
    fig.text(0.5, -0.01, 'Coverage', ha='center', va='center')
    fig.text(0.0,
             0.5,
             '% reduction',
             ha='center',
             va='center',
             rotation='vertical')

    if save:
        save_double(fig, savename=savename)

    return fig
Exemplo n.º 18
0
        '20210205_IL_locale_uniform_range_mr_bvariant_infectivity_0.10_bvariant',
        '20210204_IL_locale_uniform_range_ae_bvariant_infectivity_0.25_bvariant',
        '20210204_IL_locale_uniform_range_ae_bvariant_infectivity_0.5_bvariant',
        '20210204_IL_locale_uniform_range_ae_bvariant_infectivity_0.75_bvariant'
    ]
    labels = ['baseline', '10%', '25%', '50%', '75%']

    col = 'deaths'

    plot_first_day = date(2021, 2, 1)
    plot_last_day = date(2021, 5, 1)

    fig = plt.figure(figsize=(6, 4))
    fig.subplots_adjust(left=0.2)
    ax = fig.gca()
    p = load_color_palette('wes')
    palette = [p[x] for x in [8, 4, 2, 1, 3]]

    for s, scen in enumerate(scen_dirs):
        simpath = os.path.join(projectpath, 'cms_sim', 'simulation_output',
                               scen)
        fname = 'nu_%s_All.csv' % scen.split('_')[0]
        df = pd.read_csv(os.path.join(simpath, fname))
        df['date'] = pd.to_datetime(df['date'])
        df = df[(df['date'] >= plot_first_day) & (df['date'] <= plot_last_day)]
        ax.bar([s],
               np.sum(df['%s_median' % col]),
               align='center',
               color=palette[s],
               label=labels[s])
        ax.plot([s, s],