def sin_test(n=8,text_position='start'): """Test the stuff from the modules""" from bfmplot import pl import bfmplot as bp import numpy as np #bp.set_color_cycle(bp.cccs_colors) #pl.figure(figsize=bp.phys_rev_column()) pl.figure(figsize=bp.golden_ratio(5)) x = np.linspace(1,5*np.pi,100) for i in range(n): pl.plot(x, 1-np.sin(x[::-1]/np.sqrt(i+1)), marker=bp.markers[i],mfc='w',label='$i=%d$'%i) bp.strip_axis(pl.gca()) leg = pl.legend() bp.align_legend_right(leg) bp.arrow(pl.gca(), r'$i$', (3, 1.8), (6, 0.8), text_position=text_position) pl.xlabel('hello') pl.ylabel('hello') bp.set_n_ticks(pl.gca(), 3, 2) #pl.xscale('log') pl.gcf().tight_layout()
def sin_test(n=8,text_position='start'): pl.figure(figsize=bp.golden_ratio(5)) x = np.linspace(0,5*np.pi,100) for i in range(n): pl.plot(x, 1-np.sin(x[::-1]/np.sqrt(i+1)), marker=bp.markers[i],mfc='w',label='$i=%d$'%i) bp.strip_axis(pl.gca()) leg = pl.legend() bp.align_legend_right(leg) bp.arrow(pl.gca(), r'$i$', (14, 0.8), (10, 0.15), text_position=text_position, rad=0.3) pl.xlabel('this is the x-label') pl.ylabel('this is the y-label') pl.gcf().tight_layout()
def sin_test(n=8, text_position='start', with_legend=True): pl.figure(figsize=bp.golden_ratio(5)) x = np.linspace(0, 5 * np.pi, 100) for i in range(n): pl.plot(x, 1 - np.sin(x[::-1] / np.sqrt(i + 1)), marker=bp.markers[i], mfc='w', label='$i=%d$' % i) bp.strip_axis(pl.gca()) if with_legend: leg = pl.legend() bp.align_legend_right(leg) pl.xlabel('this is the x-label') pl.ylabel('this is the y-label') pl.gca().set_title(name) pl.gcf().tight_layout()
va='bottom', bbox={ 'facecolor': 'w', 'edgecolor': 'w', 'pad': 0 }) #pl.xscale('log') #pl.yscale('log') ylim = pl.gca().get_ylim() min_ylim = 10**np.floor(np.log(ylim[0]) / np.log(10)) max_ylim = 10**np.ceil(np.log(ylim[1]) / np.log(10)) if min_ylim < 1: min_ylim = 1 #pl.ylim([min_ylim, max_ylim]) pl.xlim([1, 32]) if _r < n_row - 1: [x.set_visible(False) for x in ax[i].xaxis.get_major_ticks()] bp.strip_axis(pl.gca()) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.34, hspace=0.3) pl.gcf().savefig("model_fit_figures/all_confirmed_fit.png", dpi=300) pl.gcf().savefig("model_fit_figures/Fig_03.png", dpi=300) if not loaded_fits: with open('fit_parameters/all_provinces.p', 'wb') as f: pickle.dump(fit_parameters, f) pl.show()
r"$N_\mathrm{eff}=%d$" %(params['N'].value), transform=ax[i].transAxes, ha='right', va='bottom', bbox={'facecolor':'w','edgecolor':'w','pad':0} ) pl.xscale('log') pl.yscale('log') ylim = pl.gca().get_ylim() min_ylim = 10**np.floor(np.log(ylim[0])/np.log(10)) max_ylim = 10**np.ceil(np.log(ylim[1])/np.log(10)) if min_ylim < 1: min_ylim = 1 pl.ylim([min_ylim, max_ylim]) pl.xlim([1,35]) if _r < n_row-1: [ x.set_visible(False) for x in ax[i].xaxis.get_major_ticks() ] bp.strip_axis(pl.gca()) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.34,hspace=0.3) pl.gcf().savefig("model_fit_figures/quarantine_model_all_confirmed_fit_after_feb_11.png",dpi=300) pl.gcf().savefig("model_fit_figures/SI_Fig_03.png",dpi=300) if not loaded_fits: with open('fit_parameters/quarantined_model_all_provinces_after_feb_12.p','wb') as f: pickle.dump(fit_parameters,f) pl.show()
ylim = pl.gca().get_ylim() min_ylim = 10**np.floor(np.log(ylim[0]) / np.log(10)) max_ylim = 10**np.ceil(np.log(ylim[1]) / np.log(10)) if min_ylim < 1: min_ylim = 1 if not LINLIN: pl.ylim([min_ylim, max_ylim]) pl.xlim([1, 60]) else: pl.xlim([0, 40]) if not LINLIN: if _r < n_row - 1: [x.set_visible(False) for x in ax[i].xaxis.get_major_ticks()] bp.strip_axis(pl.gca()) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.34, hspace=0.3) fn = 'all_provinces_after_feb_12.p' #fn = 'different_initial_conds_after_feb_12.p' name = 'linlin' if LINLIN else 'loglog' pl.gcf().savefig( "model_fit_figures/all_confirmed_fit_after_feb_12_{}.png".format(name), dpi=300) if LINLIN: pl.gcf().savefig("model_fit_figures/SI_Fig_02.png", dpi=300) else: pl.gcf().savefig("model_fit_figures/SI_Fig_01.png", dpi=300) if not loaded_fits: with open('fit_parameters/{}'.format(fn), 'wb') as f: pickle.dump(fit_parameters, f)
ax[0].text(-0.18, 1.03, 'A', fontsize=14, fontweight='bold', transform=ax[0].transAxes, va='top') ax[1].text(-0.21, 1.03, 'B', fontsize=14, fontweight='bold', transform=ax[1].transAxes, va='top') pl.gcf().tight_layout() #bp.add_curve_label(ax[0], # curve_x=curves[0]['I']['x'], # curve_y=curves[0]['I']['y'], # label='$I$', # label_pos_rel=0.2, # angle=0, # #color=colors[2], # fontsize=12 # ) # #bp.add_curve_label(ax[0], # curve_x=curves[0]['S']['x'], # curve_y=curves[0]['S']['y'], # label='$S$',
#ax[i].text(0.7,0.03, # titlemap[province], # transform=ax[i].transAxes, # ha='right', # va='bottom', # bbox={'facecolor':'w','edgecolor':'w','pad':0} # ) if _r < n_row - 1: [x.set_visible(False) for x in ax[i].xaxis.get_major_ticks()] ax[i].set_yticks([10, 100, 1000, 10000]) bp.strip_axis(pl.gca()) #ax[0].text(-0.4,1.1, # 'C', # transform=ax[0].transAxes, # ha='left', # va='top', # fontweight='bold', # fontsize=14, # bbox={'facecolor':'w','edgecolor':'w','pad':0} # ) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.4, hspace=0.3) pl.gcf().savefig("powerlaw_fit_figures/fit_powerlaw_small_hubei_china.png", dpi=300, transparent=True) pl.show()
import numpy as np from scipy.ndimage import imread from bfmplot import pl def rgb2grey(rgb): r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] grey = 0.2989 * r + 0.5870 * g + 0.1140 * b return grey A = imread('column.png') A = rgb2grey(A) pl.imshow(A) pl.gcf().savefig('column_cividis.png') pl.show()
bp.strip_axis(ax[0]) bp.strip_axis(ax[1]) #bp.align_legend_right(leg) #ax[i].set_yticks([0,10000,20000,30000,40000]) if i == 0: ax[i].set_yticks([1e2, 1e3, 1e4, 1e5]) else: ax[i].set_yticks([1e1, 1e2, 1e3, 1e4]) #bp.humanify_yticks(ax[i],precision=0 if i == 0 else 1) ax[i].set_xlabel('days since Jan. 20th') #ax[0].text(-0.18,1.03,'A',fontsize=14,fontweight='bold',transform=ax[0].transAxes,va='top') #ax[1].text(-0.21,1.03,'B',fontsize=14,fontweight='bold',transform=ax[1].transAxes,va='top') pl.gcf().tight_layout() #bp.add_curve_label(ax[0], # curve_x=curves[0]['I']['x'], # curve_y=curves[0]['I']['y'], # label='$I$', # label_pos_rel=0.2, # angle=0, # #color=colors[2], # fontsize=12 # ) # #bp.add_curve_label(ax[0], # curve_x=curves[0]['S']['x'], # curve_y=curves[0]['S']['y'], # label='$S$',
import bfmplot as bp t = np.linspace(0, 100, 1000) result = model.integrate(t) plt.figure() for compartment, incidence in result.items(): plt.plot(t, incidence, label=compartment) plt.xlabel('time [days]') plt.ylabel('incidence') plt.legend() bp.strip_axis(plt.gca()) plt.gcf().tight_layout() plt.savefig('SEIRQ.png', dpi=300) N = 1000 I0 = 100 model = epk.EpiModel([S, E, I, R, Q], initial_population_size=N) model.set_processes([ (S, I, infection_rate, E, I), (E, symptomatic_rate, I), (I, recovery_rate, R), (I, quarantine_rate, Q), ]) model.set_initial_conditions({S: N - I0, I: I0}) t, result = model.simulate(100)
import bfmplot as bp data = np.array([ (0.0, 2.00), (0.75, 2.68), (1.5, 3.00), (2.25, 2.78), (3.0, 2.14), (3.75, 1.43), (4.5, 1.02), (5.25, 1.14), (6.0, 1.72), ]) times, rates = data[:, 0], data[:, 1] x2 = np.linspace(times[0], times[-1], 1000) interp_modes = ['zero', 'linear', 'nearest', 'quadratic'] pl.figure() pl.plot(times, rates, 's', label='data') for kind in interp_modes: f = interp1d(times, rates, kind=kind) pl.plot(x2, f(x2), label=kind) pl.legend(frameon=False) bp.strip_axis(pl.gca()) pl.xlabel('time') pl.ylabel('value') pl.gcf().tight_layout() pl.gcf().savefig('interp1d.png', dpi=300) pl.show()
for colors, name in zip( [ bp.mpl_default_colors, bp.new_colors, bp.brewer_qualitative, bp.cccs_colors, bp.get_cividis_colors(8), bp.wong, ], [ 'bfmplot.mpl_default_colors', 'bfmplot.new_colors', 'bfmplot.brewer_qualitative', 'bfmplot.cccs_colors', 'bfmplot.get_cividis_colors(8)', 'bfmplot.wong', ], ): bp.set_color_cycle(colors) sin_test(n=8, text_position='start', with_legend=False) bp.set_n_ticks(pl.gca(), nx=4, ny=4) name = name.replace('(', '_') name = name.replace(')', '_') pl.gcf().savefig(name + ".png", dpi=100) pl.show()
model.set_processes(quadratic_processes + linear_processes) initial_conditions = {(node, "S"): 1.0 for node in nodes} initial_conditions[(nodes[0], "S")] = 0.8 initial_conditions[(nodes[0], "I")] = 0.2 model.set_initial_conditions(initial_conditions, allow_nonzero_column_sums=True) # set compartments for which you want to obtain the # result plot_compartments = [(node, "I") for node in nodes] # integrate import numpy as np t = np.linspace(0, 12, 1000) result = model.integrate(t, return_compartments=plot_compartments) # plot result from bfmplot import pl as plt plt.figure() for (node, _), concentration in result.items(): plt.plot(t, concentration, label='node: ' + str(node)) plt.xlabel("time") plt.ylabel("I") plt.legend() plt.gcf().tight_layout() plt.gcf().savefig("chain_I.png", dpi=300) plt.show()
(S, I, alpha, I, I), (I, beta, R), (None, gamma, S), ]) result = model.integrate(t) for iC, (C, res) in enumerate(result.items()): pl.plot(t, res, c=bp.colors[iC], ls='-.') bp.strip_axis(pl.gca()) pl.xlim([0, 150]) pl.ylim([0, 1000]) pl.xlabel('time [days]') pl.ylabel('incidence') pl.legend() pl.gcf().tight_layout() pl.gcf().savefig('SIR_birth_model_reimports_zoom.png', dpi=300) pl.xlim([0, max(t)]) pl.ylim([0, max(result_sim['S'])]) pl.gcf().savefig('SIR_birth_model_reimports_all.png', dpi=300) pl.yscale('log') pl.ylim([min(result['I']), max(result_sim['S'])]) pl.gcf().savefig('SIR_birth_model_reimports_all_log.png', dpi=300) pl.show()
bbox={ 'facecolor': 'w', 'edgecolor': 'w', 'pad': 0 }) if _r < n_row - 1: [x.set_visible(False) for x in ax[i].xaxis.get_major_ticks()] ax[i].set_yticks([1, 10, 100, 1000]) bp.strip_axis(pl.gca()) ax[0].text(-0.4, 1.1, 'C', transform=ax[0].transAxes, ha='left', va='top', fontweight='bold', fontsize=14, bbox={ 'facecolor': 'w', 'edgecolor': 'w', 'pad': 0 }) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.3, hspace=0.3) pl.gcf().savefig("powerlaw_fit_figures/fit_powerlaw_500.png", dpi=300) pl.show()
# bbox={'facecolor':'w','edgecolor':'w','pad':0} # ) #pl.xscale('log') #pl.yscale('log') ylim = pl.gca().get_ylim() min_ylim = 10**np.floor(np.log(ylim[0]) / np.log(10)) max_ylim = 10**np.ceil(np.log(ylim[1]) / np.log(10)) if min_ylim < 1: min_ylim = 1 #pl.ylim([min_ylim, max_ylim]) #if _r < n_row-1: # [ x.set_visible(False) for x in ax[i].xaxis.get_major_ticks() ] pl.xlim([0, 35]) pl.xticks([0, 10, 20, 30]) bp.strip_axis(pl.gca()) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.34, hspace=0.3) pl.gcf().savefig( "model_fit_figures/linlin_shutdown_model_all_confirmed_fit_after_feb_12.png", dpi=300) pl.gcf().savefig("model_fit_figures/SI_Fig_06.png", dpi=300) if not loaded_fits: with open('fit_parameters/shutdown_model_all_provinces_after_feb_12.p', 'wb') as f: pickle.dump(fit_parameters, f) pl.show()
min_ylim = 1 #pl.ylim([min_ylim, max_ylim]) pl.xlim([0,30]) if _r < n_row-1: ax[i].set_xticklabels('') # [ x.set_visible(False) for x in ax[i].xaxis.get_major_ticks() ] ax[i].set_yticks([0,500,1000,1500]) ax[i].set_yticklabels(['0','0.5k','1.0k','1.5k']) bp.strip_axis(pl.gca()) #bp.humanify_yticks(ax[i],precision=1) ax[0].text(-0.4,1.1, 'C', transform=ax[0].transAxes, ha='left', va='top', fontweight='bold', fontsize=14, bbox={'facecolor':'w','edgecolor':'w','pad':0} ) pl.gcf().tight_layout() pl.gcf().subplots_adjust(wspace=0.3,hspace=0.3) pl.gcf().savefig("model_fit_figures/model_fit_confirmed_500.png",dpi=300) if not loaded_fits: with open('fit_parameters/confirmed_cases_500.p','wb') as f: pickle.dump(fit_parameters,f) pl.show()
N = pdata['population'] / 1e6 print(p['kappa0'],p['kappa']) Q = (k+k0)/(b+k+k0) P = k0/(k+k0) R0eff = a / (b+k+k0) tabledata.append([titlemap[province], N, Q, P, R0eff, k, k0, I0f, ]) ks.append(k) k0s.append(k0) for i, (_k0, _ks) in enumerate(zip(k0s, ks)): pl.plot([_k0],[_ks],marker=markers[i],color=colors[i],markersize=7) pl.xlim([0,0.14]) pl.ylim([-.05,0.55]) pl.gcf().savefig('model_fit_figures/'+fn+'.png',dpi=300) pl.show() headers = [ 'Province', '$N/10^6$','$Q$', '$P$', '$R_{0,\mathrm{eff}}$', '$\kappa$ [$\mathrm{d}^{-1}$]', '$\kappa_{0}$ [$\mathrm{d}^{-1}$]', '$I_0/X_0$', ] floatfmt = ( "", ".1f",