def fixaxis(sim, useSI=True, boxoff=False): ''' Make the plotting more consistent -- add a legend and ensure the axes start at 0 ''' delta = 0.5 pl.legend() # Add legend sc.setylim() # Rescale y to start at 0 pl.xlim((0, sim['n_days'] + delta)) if boxoff: sc.boxoff() # Turn off top and right lines return
def format_ax(ax, sim, key=None): @ticker.FuncFormatter def date_formatter(x, pos): return (sim['start_day'] + dt.timedelta(days=x)).strftime('%b') ax.xaxis.set_major_formatter(date_formatter) pl.xlim([0, sim.day(calibration_end)]) sc.boxoff() return
def format_ax(ax, sim, key=None): @ticker.FuncFormatter def date_formatter(x, pos): return (sim['start_day'] + dt.timedelta(days=x)).strftime('%b-%d') ax.xaxis.set_major_formatter(date_formatter) pl.xlim([0, 213]) # pl.xlim([0, sim['n_days']]) sc.boxoff() return
def format_ax(ax, sim, key=None): ''' Format the axes nicely ''' @ticker.FuncFormatter def date_formatter(x, pos): return (sim['start_day'] + dt.timedelta(days=x)).strftime('%b-%d') ax.xaxis.set_major_formatter(date_formatter) if key != 'r_eff': sc.commaticks() pl.xlim([0, sim['n_days']]) sc.boxoff() return
def format_axs(axs, key=None): ''' Format axes nicely ''' @ticker.FuncFormatter def date_formatter(x, pos): # print(x) return (refsim['start_day'] + dt.timedelta(days=x)).strftime('%b-%d') for i, ax in enumerate(axs): bbox = None if i != 1 else (1.05, 1.05) # Move legend up a bit day_stride = 21 xmin, xmax = ax.get_xlim() ax.set_xticks(np.arange(xmin, xmax, day_stride)) ax.xaxis.set_major_formatter(date_formatter) ax.legend(frameon=False, bbox_to_anchor=bbox) sc.boxoff(ax=ax) sc.setylim(ax=ax) sc.commaticks(ax=ax) return
X = np.arange(len(x)) XX = X + w - off # Diagnoses ax[0] = pl.axes([xl, yb, dx, dy]) c1 = [0.3, 0.3, 0.6] # diags c2 = [0.6, 0.7, 0.9] #diags pl.bar(X, pos, width=w, label='Data', facecolor=c1) pl.bar(XX, mpbest, width=w, label='Model', facecolor=c2) for i, ix in enumerate(XX): pl.plot([ix, ix], [mplow[i], mphigh[i]], c='k') ax[0].set_xticks((X + XX) / 2) ax[0].set_xticklabels(x) pl.xlabel('Age') pl.ylabel('Diagnoses') sc.boxoff(ax[0]) pl.legend(frameon=False, bbox_to_anchor=(0.3, 0.7)) # Deaths ax[1] = pl.axes([xl + dx + xm, yb, dx, dy]) c1 = [0.5, 0.0, 0.0] # deaths c2 = [0.9, 0.4, 0.3] # deaths pl.bar(X, deaths, width=w, label='Data', facecolor=c1) pl.bar(XX, mdbest, width=w, label='Model', facecolor=c2) for i, ix in enumerate(XX): pl.plot([ix, ix], [mdlow[i], mdhigh[i]], c='k') ax[1].set_xticks((X + XX) / 2) ax[1].set_xticklabels(x) pl.xlabel('Age') pl.ylabel('Deaths') sc.boxoff(ax[1])
ms=20, elinewidth=3, capsize=0) box_ax.set_xticks(x - 0.15) #box_ax.set_xticklabels(labels) @ticker.FuncFormatter def date_formatter(x, pos): return (cv.date('2021-01-12') + dt.timedelta(days=x * 7)).strftime('%d-%b') box_ax.xaxis.set_major_formatter(date_formatter) pl.ylabel('Estimated daily infections (000s)') sc.boxoff(ax=box_ax) sc.commaticks() box_ax.legend(frameon=False) # B. Cumulative total infections width = 0.8 # the width of the bars x = [0, 1, 2] data = np.array([ msims[sn].results['cum_infections'].values[-1] - msims[sn].results['cum_infections'].values[sim.day('2021-01-04')] for sn in scenarios ]) bar_ax = pl.axes([xgapl + xgapm + dx1, ygapb, dx2, dy]) for sn, scen in enumerate(scenarios): bar_ax.bar(x[sn], data[sn] / 1e3, width, color=colors[sn], alpha=1.0)
alpha=0.75, label='Data') toplot = plotdict['new_diagnoses'][l][date_inds[0]:date_inds[1]] pl.plot(tvec, toplot, c=colors[i], label=l, lw=4, alpha=1.0) #low = plotdict_l['new_diagnoses'][l][date_inds[0]:date_inds[1]] #high = plotdict_h['new_diagnoses'][l][date_inds[0]:date_inds[1]] #pl.fill_between(tvec, low, high, facecolor=colors[i], alpha=0.2) pl.ylabel('Daily new infections') ax = pl.gca() ax.set_xticks(datemarks) cv.date_formatter(start_day=start_day, ax=ax, dateformat=dateformat) sc.setylim() sc.commaticks() pl.legend(frameon=False) sc.boxoff() # Plot B: R_eff pl.subplot(2, 2, 2) colors = pl.cm.GnBu([0.9, 0.6, 0.3]) for i, l in enumerate(labels): toplot = plotdict['r_eff'][l][date_inds[0]:date_inds[1]] pl.plot(tvec, toplot, c=colors[i], label=l, lw=4, alpha=1.0) low = plotdict_l['r_eff'][l][date_inds[0]:date_inds[1]] high = plotdict_h['r_eff'][l][date_inds[0]:date_inds[1]] pl.fill_between(tvec, low, high, facecolor=colors[i], alpha=0.2) pl.ylabel('R') pl.axhline(1, linestyle=':', c='k', alpha=0.3) ax = pl.gca() ax.set_xticks(datemarks) cv.date_formatter(start_day=start_day, ax=ax, dateformat=dateformat)
def plot(): fig = pl.figure(num='Fig. 2: Transmission dynamics', figsize=(20,14)) piey, tsy, r3y = 0.68, 0.50, 0.07 piedx, tsdx, r3dx = 0.2, 0.9, 0.25 piedy, tsdy, r3dy = 0.2, 0.47, 0.35 pie1x, pie2x = 0.12, 0.65 tsx = 0.07 dispx, cumx, sympx = tsx, 0.33+tsx, 0.66+tsx ts_ax = pl.axes([tsx, tsy, tsdx, tsdy]) pie_ax1 = pl.axes([pie1x, piey, piedx, piedy]) pie_ax2 = pl.axes([pie2x, piey, piedx, piedy]) symp_ax = pl.axes([sympx, r3y, r3dx, r3dy]) disp_ax = pl.axes([dispx, r3y, r3dx, r3dy]) cum_ax = pl.axes([cumx, r3y, r3dx, r3dy]) off = 0.06 txtdispx, txtcumx, txtsympx = dispx-off, cumx-off, sympx-off+0.02 tsytxt = tsy+tsdy r3ytxt = r3y+r3dy labelsize = 40-wf pl.figtext(txtdispx, tsytxt, 'a', fontsize=labelsize) pl.figtext(txtdispx, r3ytxt, 'b', fontsize=labelsize) pl.figtext(txtcumx, r3ytxt, 'c', fontsize=labelsize) pl.figtext(txtsympx, r3ytxt, 'd', fontsize=labelsize) #%% Fig. 2A -- Time series plot layer_keys = list(sim.layer_keys()) layer_mapping = {k:i for i,k in enumerate(layer_keys)} n_layers = len(layer_keys) colors = sc.gridcolors(n_layers) layer_counts = np.zeros((sim.npts, n_layers)) for source_ind, target_ind in tt.count_transmissions(): dd = tt.detailed[target_ind] date = dd['date'] layer_num = layer_mapping[dd['layer']] layer_counts[date, layer_num] += sim.rescale_vec[date] mar12 = cv.date('2020-03-12') mar23 = cv.date('2020-03-23') mar12d = sim.day(mar12) mar23d = sim.day(mar23) labels = ['Household', 'School', 'Workplace', 'Community', 'LTCF'] for l in range(n_layers): ts_ax.plot(sim.datevec, layer_counts[:,l], c=colors[l], lw=3, label=labels[l]) sc.setylim(ax=ts_ax) sc.boxoff(ax=ts_ax) ts_ax.set_ylabel('Transmissions per day') ts_ax.set_xlim([sc.readdate('2020-01-18'), sc.readdate('2020-06-09')]) ts_ax.xaxis.set_major_formatter(mdates.DateFormatter('%b-%d')) ts_ax.set_xticks([sim.date(d, as_date=True) for d in np.arange(0, sim.day('2020-06-09'), 14)]) ts_ax.legend(frameon=False, bbox_to_anchor=(0.85,0.1)) color = [0.2, 0.2, 0.2] ts_ax.axvline(mar12, c=color, linestyle='--', alpha=0.4, lw=3) ts_ax.axvline(mar23, c=color, linestyle='--', alpha=0.4, lw=3) yl = ts_ax.get_ylim() labely = yl[1]*1.015 ts_ax.text(mar12, labely, 'Schools close ', color=color, alpha=0.9, style='italic', horizontalalignment='center') ts_ax.text(mar23, labely, ' Stay-at-home', color=color, alpha=0.9, style='italic', horizontalalignment='center') #%% Fig. 2A inset -- Pie charts pre_counts = layer_counts[0:mar12d, :].sum(axis=0) post_counts = layer_counts[mar23d:, :].sum(axis=0) pre_counts = pre_counts/pre_counts.sum()*100 post_counts = post_counts/post_counts.sum()*100 lpre = [ f'Household\n{pre_counts[0]:0.1f}%', f'School\n{pre_counts[1]:0.1f}% ', f'Workplace\n{pre_counts[2]:0.1f}% ', f'Community\n{pre_counts[3]:0.1f}%', f'LTCF\n{pre_counts[4]:0.1f}%', ] lpost = [ f'Household\n{post_counts[0]:0.1f}%', f'School\n{post_counts[1]:0.1f}%', f'Workplace\n{post_counts[2]:0.1f}%', f'Community\n{post_counts[3]:0.1f}%', f'LTCF\n{post_counts[4]:0.1f}%', ] pie_ax1.pie(pre_counts, colors=colors, labels=lpre, **pieargs) pie_ax2.pie(post_counts, colors=colors, labels=lpost, **pieargs) pie_ax1.text(0, 1.75, 'Transmissions by layer\nbefore schools closed', style='italic', horizontalalignment='center') pie_ax2.text(0, 1.75, 'Transmissions by layer\nafter stay-at-home', style='italic', horizontalalignment='center') #%% Fig. 2B -- histogram by overdispersion # Process targets n_targets = tt.count_targets(end_day=mar12) # Handle bins max_infections = n_targets.max() edges = np.arange(0, max_infections+2) # Analysis counts = np.histogram(n_targets, edges)[0] bins = edges[:-1] # Remove last bin since it's an edge norm_counts = counts/counts.sum() raw_counts = counts*bins total_counts = raw_counts/raw_counts.sum()*100 n_bins = len(bins) index = np.linspace(0, 100, len(n_targets)) sorted_arr = np.sort(n_targets) sorted_sum = np.cumsum(sorted_arr) sorted_sum = sorted_sum/sorted_sum.max()*100 change_inds = sc.findinds(np.diff(sorted_arr) != 0) pl.set_cmap('Spectral_r') sscolors = sc.vectocolor(n_bins) width = 1.0 for i in range(n_bins): disp_ax.bar(bins[i], total_counts[i], width=width, facecolor=sscolors[i]) disp_ax.set_xlabel('Number of transmissions per case') disp_ax.set_ylabel('Proportion of transmissions (%)') sc.boxoff() disp_ax.set_xlim([0.5, 32.5]) disp_ax.set_xticks(np.arange(0, 32.5, 4)) sc.boxoff(ax=disp_ax) dpie_ax = pl.axes([dispx+0.05, 0.20, 0.2, 0.2]) trans1 = total_counts[1:3].sum() trans2 = total_counts[3:5].sum() trans3 = total_counts[5:8].sum() trans4 = total_counts[8:].sum() labels = [ f'1-2:\n{trans1:0.0f}%', f' 3-4:\n {trans2:0.0f}%', f'5-7: \n{trans3:0.0f}%\n', f'>7: \n{trans4:0.0f}%\n', ] dpie_args = sc.mergedicts(pieargs, dict(labeldistance=1.2)) # Slightly smaller label distance dpie_ax.pie([trans1, trans2, trans3, trans4], labels=labels, colors=sscolors[[0,4,7,12]], **dpie_args) #%% Fig. 2C -- cumulative distribution function rev_ind = 100 - index n_change_inds = len(change_inds) change_bins = bins[counts>0][1:] for i in range(n_change_inds): ib = int(change_bins[i]) ci = change_inds[i] ici = index[ci] sci = sorted_sum[ci] color = sscolors[ib] if i>0: cim1 = change_inds[i-1] icim1 = index[cim1] scim1 = sorted_sum[cim1] cum_ax.plot([icim1, ici], [scim1, sci], lw=4, c=color) cum_ax.scatter([ici], [sci], s=150, zorder=50-i, c=[color], edgecolor='w', linewidth=0.2) if ib<=6 or ib in [8, 10, 25]: xoff = 5 - 2*(ib==1) + 3*(ib>=10) + 1*(ib>=20) yoff = 2*(ib==1) cum_ax.text(ici-xoff, sci+yoff, ib, fontsize=18-wf, color=color) cum_ax.set_xlabel('Proportion of primary infections (%)') cum_ax.set_ylabel('Proportion of transmissions (%)') xmin = -2 ymin = -2 cum_ax.set_xlim([xmin, 102]) cum_ax.set_ylim([ymin, 102]) sc.boxoff(ax=cum_ax) # Draw horizontal lines and annotations ancol1 = [0.2, 0.2, 0.2] ancol2 = sscolors[0] ancol3 = sscolors[6] i01 = sc.findlast(sorted_sum==0) i20 = sc.findlast(sorted_sum<=20) i50 = sc.findlast(sorted_sum<=50) cum_ax.plot([xmin, index[i01]], [0, 0], '--', lw=2, c=ancol1) cum_ax.plot([xmin, index[i20], index[i20]], [20, 20, ymin], '--', lw=2, c=ancol2) cum_ax.plot([xmin, index[i50], index[i50]], [50, 50, ymin], '--', lw=2, c=ancol3) # Compute mean number of transmissions for 80% and 50% thresholds q80 = sc.findfirst(np.cumsum(total_counts)>20) # Count corresponding to 80% of cumulative infections (100-80) q50 = sc.findfirst(np.cumsum(total_counts)>50) # Count corresponding to 50% of cumulative infections n80, n50 = [sum(bins[q:]*norm_counts[q:]/norm_counts[q:].sum()) for q in [q80, q50]] # Plot annotations kw = dict(bbox=dict(facecolor='w', alpha=0.9, lw=0), fontsize=20-wf) cum_ax.text(2, 3, f'{index[i01]:0.0f}% of infections\ndo not transmit', c=ancol1, **kw) cum_ax.text(8, 23, f'{rev_ind[i20]:0.0f}% of infections cause\n80% of transmissions\n(mean: {n80:0.1f} per infection)', c=ancol2, **kw) cum_ax.text(14, 53, f'{rev_ind[i50]:0.0f}% of infections cause\n50% of transmissions\n(mean: {n50:0.1f} per infection)', c=ancol3, **kw) #%% Fig. 2D -- histogram by date of symptom onset # Calculate asymp_count = 0 symp_counts = {} minind = -5 maxind = 15 for _, target_ind in tt.transmissions: dd = tt.detailed[target_ind] date = dd['date'] delta = sim.rescale_vec[date] # Increment counts by this much if dd['s']: if tt.detailed[dd['source']]['date'] <= date: # Skip dynamical scaling reinfections sdate = dd['s']['date_symptomatic'] if np.isnan(sdate): asymp_count += delta else: ind = int(date - sdate) if ind not in symp_counts: symp_counts[ind] = 0 symp_counts[ind] += delta # Convert to an array xax = np.arange(minind-1, maxind+1) sympcounts = np.zeros(len(xax)) for i,val in symp_counts.items(): if i<minind: ind = 0 elif i>maxind: ind = -1 else: ind = sc.findinds(xax==i)[0] sympcounts[ind] += val # Plot total_count = asymp_count + sympcounts.sum() sympcounts = sympcounts/total_count*100 presymp = sc.findinds(xax<=0)[-1] colors = ['#eed15b', '#ee943a', '#c3211a'] asymp_frac = asymp_count/total_count*100 pre_frac = sympcounts[:presymp].sum() symp_frac = sympcounts[presymp:].sum() symp_ax.bar(xax[0]-2, asymp_frac, label='Asymptomatic', color=colors[0]) symp_ax.bar(xax[:presymp], sympcounts[:presymp], label='Presymptomatic', color=colors[1]) symp_ax.bar(xax[presymp:], sympcounts[presymp:], label='Symptomatic', color=colors[2]) symp_ax.set_xlabel('Days since symptom onset') symp_ax.set_ylabel('Proportion of transmissions (%)') symp_ax.set_xticks([minind-3, 0, 5, 10, maxind]) symp_ax.set_xticklabels(['Asymp.', '0', '5', '10', f'>{maxind}']) sc.boxoff(ax=symp_ax) spie_ax = pl.axes([sympx+0.05, 0.20, 0.2, 0.2]) labels = [f'Asymp-\ntomatic\n{asymp_frac:0.0f}%', f' Presymp-\n tomatic\n {pre_frac:0.0f}%', f'Symp-\ntomatic\n{symp_frac:0.0f}%'] spie_ax.pie([asymp_frac, pre_frac, symp_frac], labels=labels, colors=colors, **pieargs) return fig
date = dd['date'] layer_num = layer_mapping[dd['layer']] layer_counts[date, layer_num] += sim.rescale_vec[date] lockdown1 = [sc.readdate('2020-03-23'),sc.readdate('2020-05-31')] lockdown2 = [sc.readdate('2020-11-05'),sc.readdate('2020-12-03')] lockdown3 = [sc.readdate('2021-01-04'),sc.readdate('2021-02-08')] labels = ['Household', 'School', 'Workplace', 'Community'] for l in range(n_layers): ax.plot(sim.datevec, layer_counts[:,l], c=colors[l], lw=3, label=labels[l]) ax.axvspan(lockdown1[0], lockdown1[1], color='steelblue', alpha=0.2, lw=0) ax.axvspan(lockdown2[0], lockdown2[1], color='steelblue', alpha=0.2, lw=0) ax.axvspan(lockdown3[0], lockdown3[1], color='lightblue', alpha=0.2, lw=0) sc.setylim(ax=ax) sc.boxoff(ax=ax) ax.set_ylabel('Transmissions per day') ax.set_xlim([sc.readdate('2020-01-21'), sc.readdate('2021-03-01')]) ax.xaxis.set_major_formatter(mdates.DateFormatter('%b\n%y')) datemarks = pl.array([sim.day('2020-02-01'), sim.day('2020-03-01'), sim.day('2020-04-01'), sim.day('2020-05-01'), sim.day('2020-06-01'), sim.day('2020-07-01'), sim.day('2020-08-01'), sim.day('2020-09-01'), sim.day('2020-10-01'), sim.day('2020-11-01'), sim.day('2020-12-01'), sim.day('2021-01-01'), sim.day('2021-02-01'), sim.day('2021-03-01')]) ax.set_xticks([sim.date(d, as_date=True) for d in datemarks]) ax.legend(frameon=False) yl = ax.get_ylim() labely = yl[1]*1.015 cv.savefig(f'{figsfolder}/fig_trans.png', dpi=100)
def plot(self, which=None, n=None, axsize=None, figsize=None): ''' Create a bar plot of the top causes of burden. By default, plots the top 10 causes of DALYs. Version: 2018sep27 ''' # Set labels titles = { 'dalys': 'Top causes of DALYs', 'deaths': 'Top causes of mortality', 'prevalence': 'Most prevalent conditions' } # Handle options if which is None: which = list(titles.keys()) if n is None: n = 10 if axsize is None: axsize = (0.65, 0.15, 0.3, 0.8) if figsize is None: figsize = (7, 4) barw = 0.8 # Pull out data df = sc.dcp(self.data) nburdens = df.nrows colors = sc.gridcolors(nburdens + 2, asarray=True)[2:] colordict = sc.odict() for c, cause in enumerate(df[self.colnames['cause']]): colordict[cause] = colors[c] # Convert to list if not isinstance(which, list): asarray = False whichlist = sc.promotetolist(which) else: asarray = True whichlist = which # Loop over each option (may only be one) figs = [] for which in whichlist: colname = self.colnames[which] try: thistitle = titles[which] thisxlabel = colname except Exception as E: errormsg = '"%s" not found, "which" must be one of %s (%s)' % ( which, ', '.join(list(titles.keys())), str(E)) raise Exception(errormsg) # Process data df.sort(col=colname, reverse=True) topdata = df[:n] try: barvals = hp.arr(topdata[colname]) except Exception as E: for r in range(topdata.nrows): try: float(topdata[colname, r]) except Exception as E2: if topdata[colname, r] in ['', None]: errormsg = 'For cause "%s", the "%s" value is missing or empty' % ( topdata[self.colnames['cause'], r], colname) else: errormsg = 'For cause "%s", could not convert "%s" value "%s" to number: %s' % ( topdata[self.colnames['cause'], r], colname, topdata[colname, r], str(E2)) raise Exception(errormsg) errormsg = 'An exception was encountered, but could not be reproduced: %s' % str( E) raise Exception(errormsg) barlabels = topdata[self.colnames['cause']].tolist() # Figure out the units largestval = barvals[0] if largestval > 1e6: barvals /= 1e6 unitstr = ' (millions)' elif largestval > 1e3: barvals /= 1e3 unitstr = ' (thousands)' else: unitstr = '' # Create plot fig = pl.figure(facecolor='none', figsize=figsize) ax = fig.add_axes(axsize) ax.set_facecolor('none') yaxis = pl.arange(n, 0, -1) for i in range(n): thiscause = topdata[self.colnames['cause'], i] color = colordict[thiscause] pl.barh(yaxis[i], barvals[i], height=barw, facecolor=color, edgecolor='none') ax.set_yticks(pl.arange(10, 0, -1)) ax.set_yticklabels(barlabels) sc.SIticks(ax=ax, axis='x') ax.set_xlabel(thisxlabel + unitstr) ax.set_title(thistitle) sc.boxoff() figs.append(fig) if asarray: return figs else: return figs[0]
def plot(): fig = pl.figure(num='Fig. 4: Suppression scenarios', figsize=(figw, figh)) rx = 0.07 r1y = 0.74 rdx = 0.26 r1dy = 0.20 rδ = 0.30 r1δy = 0.29 r1ax = {} for i in range(6): xi = i % 3 yi = i // 3 r1ax[i] = pl.axes([rx + rδ * xi, r1y - r1δy * yi, rdx, r1dy]) r2y = 0.05 r2dy = rdx * figw / figh # To ensure square r2ax = {} for i in range(3): r2ax[i] = pl.axes([rx + rδ * i, r2y, rdx, r2dy]) cax = pl.axes([0.96, r2y, 0.01, r2dy]) # Labels lx = 0.015 pl.figtext(lx, r1y + r1dy + 0.02, 'a', fontsize=40) pl.figtext(lx, r2y + r2dy + 0.02, 'b', fontsize=40) slopes = sc.objdict().make(keys=df1map.keys(), vals=[]) slopes2 = sc.objdict().make(keys=df1map.keys(), vals=[]) for plotnum, key, label in df1map.enumitems(): cv.set_seed(plotnum) ax = r1ax[plotnum] for ei in eis: theseinds = sc.findinds(~np.isnan(df1[key].values)) if sepinds: ei_ok = sc.findinds(df1['eind'].values == ei) theseinds = np.intersect1d(theseinds, ei_ok) x = xvals[key][theseinds] rawy = df1[ykey].values[theseinds] y = rawy / kcpop * 100 if logy else rawy xm = x.max() if key in ['testdelay', 'trtime']: xnoise = 0.01 ynoise = 0.05 else: xnoise = 0 ynoise = 0 rndx = (np.random.randn(len(x))) * xm * xnoise rndy = (np.random.randn(len(y))) * ynoise ax.scatter(x + rndx, y * (1 + rndy), alpha=0.2, c=[cols[ei]], edgecolor='w') # Calculate slopes slopey = np.log(rawy) if logy else rawy slopex = xvals[key].values[theseinds] tmp, res = np.polyfit(slopex, slopey, 1, cov=True) fitm, fitb = tmp factor = np.exp(fitm * slopepoint[key] + fitb) / slopedenom[key] * slopex.max() slope = fitm * factor slopes[key].append(slope) # Calculate slopes, method 2 -- used for std calculation X = sm.add_constant(slopex) mod = sm.OLS(slopey, X) res = mod.fit() conf = res.conf_int(alpha=0.05, cols=None) best = res.params[1] * factor high = conf[1, 1] * factor slopes2[key].append(res) # Plot fit line bflx = np.array([x.min(), x.max()]) ploty = np.log10(y) if logy else y plotm, plotb = np.polyfit(x, ploty, 1) bfly = plotm * bflx + plotb if logy: ax.semilogy(bflx, 10**(bfly), lw=3, c=c2) else: ax.plot(bflx, bfly, lw=3, c=c2) plot_baseinds = False if plot_baseinds: default_x = default_xvals[key][baseinds] default_rawy = df1[ykey].values[baseinds] default_y = default_rawy / kcpop * 100 if logy else default_rawy ax.scatter(default_x, default_y, marker='x', alpha=1.0, c=[cols[i]]) if verbose: print( f'Slope for {key:10s}: {np.mean(slopes[key]):0.3f} ± {high-best:0.3f}' ) sc.boxoff(ax=ax) ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) if plotnum in [0, 3]: if logy: ax.set_ylabel('Attack rate (%)') ax.set_yticks((0.01, 0.1, 1.0, 10, 100)) ax.set_yticklabels(('0.01', '0.1', '1.0', '10', '100')) else: ax.set_ylabel(r'$R_{e}$') ax.set_xlabel(xlabelmap[key]) if key in ['iqfactor']: ax.set_xticks(np.linspace(0, 100, 6)) elif key in ['trprob']: ax.set_xticks(np.linspace(0, 10, 6)) elif key in ['testprob']: ax.set_xticks(np.arange(7)) elif key in ['testqprob']: ax.set_xticks(np.arange(7)) else: ax.set_xticks(np.arange(8)) if logy: ax.set_ylim([0.1, 100]) else: ax.set_ylim([0.7, 1.7]) xl = ax.get_xlim() if logy: ypos1 = 150 ypos2 = 40 else: ypos1 = 1.9 ypos2 = 1.7 xlpos = dict( iqfactor=0.86, testprob=0.13, trprob=0.83, testqprob=0.86, testdelay=0.13, trtime=0.00, ) if key in ['iqfactor', 'testqprob', 'trprob']: align = 'right' else: align = 'left' ax.text((xl[0] + xl[1]) / 2, ypos1, label, fontsize=26, horizontalalignment='center') ax.text(xlpos[key] * xl[1], ypos2, f'{abs(best):0.2f} ± {high-best:0.2f} {slopelabels[key]}', color=pointcolor, horizontalalignment=align) ax.axvline(slopepoint[key], ymax=0.83, linestyle='--', c=pointcolor, alpha=0.5, lw=2) reop = [0.6, 0.8, 1.0] for ri, r in enumerate(reop): dfr = df2[df2['reopen'] == r] im = plot_surface(r2ax[ri], dfr, col=ri, colval=r) bbox = dict(facecolor='w', alpha=0.0, edgecolor='none') pointsize = 150 if ri == 0: dotx = 1900 * 1000 / kcpop doty = 0.06 r2ax[ri].scatter([dotx], [doty], c='k', s=pointsize, zorder=10, marker='d') r2ax[ri].text(dotx * 1.20, doty * 1.50, 'Estimated\nconditions\non June 1', bbox=bbox) if ri == 1: dotx = 3000 * 1000 / kcpop doty = 0.20 r2ax[ri].scatter([dotx], [doty], c=[pointcolor2], s=pointsize, zorder=10, marker='d') r2ax[ri].text(dotx * 1.10, doty * 0.20, 'Estimated\nconditions\non July 15', color=pointcolor2, bbox=bbox) if ri == 2: dotx = 2.80 # 7200*1000/kcpop doty = 0.70 # 0.66 r2ax[ri].scatter([dotx], [doty], c=[pointcolor], s=pointsize, zorder=10, marker='d') r2ax[ri].text(dotx * 1.05, doty * 1.05, 'High mobility,\nhigh test + trace\nscenario', color=pointcolor, bbox=bbox) cbar = pl.colorbar(im, ticks=np.linspace(0.4, 1.6, 7), cax=cax) cbar.ax.set_title('$R_{e}$', rotation=0, pad=20, fontsize=24) return fig
def plot(): # Create the figure fig = pl.figure(num='Fig. 1: Calibration', figsize=(24, 20)) tx1, ty1 = 0.005, 0.97 tx2, ty2 = 0.545, 0.66 ty3 = 0.34 fsize = 40 pl.figtext(tx1, ty1, 'a', fontsize=fsize) pl.figtext(tx1, ty2, 'b', fontsize=fsize) pl.figtext(tx2, ty1, 'c', fontsize=fsize) pl.figtext(tx2, ty2, 'd', fontsize=fsize) pl.figtext(tx1, ty3, 'e', fontsize=fsize) pl.figtext(tx2, ty3, 'f', fontsize=fsize) #%% Fig. 1A: diagnoses x0, y0, dx, dy = 0.055, 0.73, 0.47, 0.24 ax1 = pl.axes([x0, y0, dx, dy]) format_ax(ax1, base_sim) plotter('cum_diagnoses', sims, ax1, calib=True, label='Model', ylabel='Cumulative diagnoses') pl.legend(loc='lower right', frameon=False) #%% Fig. 1B: deaths y0b = 0.42 ax2 = pl.axes([x0, y0b, dx, dy]) format_ax(ax2, base_sim) plotter('cum_deaths', sims, ax2, calib=True, label='Model', ylabel='Cumulative deaths') pl.legend(loc='lower right', frameon=False) #%% Fig. 1A-B inserts (histograms) agehists = [] for s, sim in enumerate(sims): agehist = sim['analyzers'][0] if s == 0: age_data = agehist.data agehists.append(agehist.hists[-1]) # Observed data x = age_data['age'].values pos = age_data['cum_diagnoses'].values death = age_data['cum_deaths'].values # Model outputs mposlist = [] mdeathlist = [] for hists in agehists: mposlist.append(hists['diagnosed']) mdeathlist.append(hists['dead']) mposarr = np.array(mposlist) mdeatharr = np.array(mdeathlist) low_q = 0.025 high_q = 0.975 mpbest = pl.median(mposarr, axis=0) mplow = pl.quantile(mposarr, q=low_q, axis=0) mphigh = pl.quantile(mposarr, q=high_q, axis=0) mdbest = pl.median(mdeatharr, axis=0) mdlow = pl.quantile(mdeatharr, q=low_q, axis=0) mdhigh = pl.quantile(mdeatharr, q=high_q, axis=0) w = 4 off = 2 # Insets x0s, y0s, dxs, dys = 0.11, 0.84, 0.17, 0.13 ax1s = pl.axes([x0s, y0s, dxs, dys]) c1 = [0.3, 0.3, 0.6] c2 = [0.6, 0.7, 0.9] xx = x + w - off pl.bar(x - off, pos, width=w, label='Data', facecolor=c1) pl.bar(xx, mpbest, width=w, label='Model', facecolor=c2) for i, ix in enumerate(xx): pl.plot([ix, ix], [mplow[i], mphigh[i]], c='k') ax1s.set_xticks(np.arange(0, 81, 20)) pl.xlabel('Age') pl.ylabel('Cases') sc.boxoff(ax1s) pl.legend(frameon=False, bbox_to_anchor=(0.7, 1.1)) y0sb = 0.53 ax2s = pl.axes([x0s, y0sb, dxs, dys]) c1 = [0.5, 0.0, 0.0] c2 = [0.9, 0.4, 0.3] pl.bar(x - off, death, width=w, label='Data', facecolor=c1) pl.bar(x + w - off, mdbest, width=w, label='Model', facecolor=c2) for i, ix in enumerate(xx): pl.plot([ix, ix], [mdlow[i], mdhigh[i]], c='k') ax2s.set_xticks(np.arange(0, 81, 20)) pl.xlabel('Age') pl.ylabel('Deaths') sc.boxoff(ax2s) pl.legend(frameon=False) sc.boxoff(ax1s) #%% Fig. 1C: infections x0, dx = 0.60, 0.38 ax3 = pl.axes([x0, y0, dx, dy]) format_ax(ax3, sim) # Plot SCAN data pop_size = 2.25e6 scan = pd.read_csv(scan_file) for i, r in scan.iterrows(): label = "Data" if i == 0 else None ts = np.mean(sim.day(r['since'], r['to'])) low = r['lower'] * pop_size high = r['upper'] * pop_size mean = r['mean'] * pop_size ax3.plot([ts] * 2, [low, high], alpha=1.0, color='k', zorder=1000) ax3.plot(ts, mean, 'o', markersize=7, color='k', alpha=0.5, label=label, zorder=1000) # Plot simulation plotter('cum_infections', sims, ax3, calib=True, label='Cumulative\ninfections\n(modeled)', ylabel='Infections') plotter('n_infectious', sims, ax3, calib=True, label='Active\ninfections\n(modeled)', ylabel='Infections', flabel=False) pl.legend(loc='upper left', frameon=False) pl.ylim([0, 130e3]) plot_intervs(sim) #%% Fig. 1C: R_eff ax4 = pl.axes([x0, y0b, dx, dy]) format_ax(ax4, sim, key='r_eff') plotter('r_eff', sims, ax4, calib=True, label='$R_{eff}$ (modeled)', ylabel=r'Effective reproduction number') pl.axhline(1, linestyle='--', lw=3, c='k', alpha=0.5) pl.legend(loc='upper right', frameon=False) plot_intervs(sim) #%% Fig. 1E # Do the plotting pl.subplots_adjust(left=0.04, right=0.52, bottom=0.03, top=0.35, wspace=0.12, hspace=0.50) for i, k in enumerate(keys): eax = pl.subplot(2, 2, i + 1) c1 = [0.2, 0.5, 0.8] c2 = [1.0, 0.5, 0.0] c3 = [0.1, 0.6, 0.1] sns.kdeplot(df1[k], shade=1, linewidth=3, label='', color=c1, alpha=0.5) sns.kdeplot(df2[k], shade=0, linewidth=3, label='', color=c2, alpha=0.5) pl.title(mapping[k]) pl.xlabel('') pl.yticks([]) if not i % 4: pl.ylabel('Density') yfactor = 1.3 yl = pl.ylim() pl.ylim([yl[0], yl[1] * yfactor]) m1 = np.median(df1[k]) m2 = np.median(df2[k]) m1std = df1[k].std() m2std = df2[k].std() pl.axvline(m1, c=c1, ymax=0.9, lw=3, linestyle='--') pl.axvline(m2, c=c2, ymax=0.9, lw=3, linestyle='--') def fmt(lab, val, std=-1): if val < 0.1: valstr = f'{lab} = {val:0.4f}' elif val < 1.0: valstr = f'{lab} = {val:0.2f}±{std:0.2f}' else: valstr = f'{lab} = {val:0.1f}±{std:0.1f}' if std < 0: valstr = valstr.split('±')[0] # Discard STD if not used return valstr if k.startswith('bc'): pl.xlim([0, 100]) elif k == 'beta': pl.xlim([3, 5]) elif k.startswith('tn'): pl.xlim([0, 50]) else: raise Exception(f'Please assign key {k}') xl = pl.xlim() xfmap = dict( beta=0.15, bc_wc1=0.30, bc_lf=0.35, tn=0.55, ) xf = xfmap[k] x0 = xl[0] + xf * (xl[1] - xl[0]) ypos1 = yl[1] * 0.97 ypos2 = yl[1] * 0.77 ypos3 = yl[1] * 0.57 if k == 'beta': # Use 2 s.f. instead of 1 pl.text(x0, ypos1, f'M: {m1:0.2f} ± {m1std:0.2f}', c=c1) pl.text(x0, ypos2, f'N: {m2:0.2f} ± {m2std:0.2f}', c=c2) pl.text(x0, ypos3, rf'$\Delta$: {(m2-m1):0.2f} ± {(m1std+m2std):0.2f}', c=c3) else: pl.text(x0, ypos1, f'M: {m1:0.1f} ± {m1std:0.1f}', c=c1) pl.text(x0, ypos2, f'N: {m2:0.1f} ± {m2std:0.1f}', c=c2) pl.text(x0, ypos3, rf'$\Delta$: {(m2-m1):0.1f} ± {(m1std+m2std):0.1f}', c=c3) sc.boxoff(ax=eax) #%% Fig. 1F: SafeGraph x0, y0c, dyc = 0.60, 0.03, 0.30 ax5 = pl.axes([x0, y0c, dx, dyc]) format_ax(ax5, sim, key='r_eff') fn = safegraph_file df = pd.read_csv(fn) week = df['week'] days = sim.day(week.values.tolist()) s = df['p.tot.schools'].values * 100 w = df['p.tot.no.schools'].values * 100 # From Fig. 2 colors = sc.gridcolors(5) wcolor = colors[3] # Work color/community scolor = colors[1] # School color pl.plot(days, w, 'd-', c=wcolor, markersize=15, lw=3, alpha=0.9, label='Workplace and\ncommunity mobility data') pl.plot(days, s, 'd-', c=scolor, markersize=15, lw=3, alpha=0.9, label='School mobility data') sc.setylim() xmin, xmax = ax5.get_xlim() ax5.set_xticks(np.arange(xmin, xmax, day_stride)) pl.ylabel('Relative mobility (%)') pl.legend(loc='upper right', frameon=False) plot_intervs(sim) return fig