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()
#pl.plot(x, 1/(N-1)*np.sqrt((1-covariance)/covariance) * np.exp(-t**2/2/covariance+np.sqrt(2)*t*a0)* np.exp(-np.sqrt(2)*fac*t*s)) #pl.plot(x, 1/(N-1)*np.sqrt((1-covariance)/covariance) * np.exp(-t**2/2/covariance)*\ # np.exp(-fac*np.sqrt(2*np.log((N-1)/x))) # ) rho = covariance prefac = 1 / (N - 1) * np.sqrt((1 - rho) / rho) * np.exp(-t**2 / 2 / rho) pl.plot(x, pk(x, N, t, covariance), label='asymptotic solution Eq. (29)') pl.plot(x, 1 / x**(np.sqrt(2)) * 0.1, label='$1/k^{\sqrt{2}}$') #pl.plot(x, prefac*np.exp(np.sqrt(2)*t*1/(x/(N-1))**0.5/3.25),label='stretched exponential') pl.plot(x, prefac * np.exp(np.sqrt(2) * t * np.sqrt(2 * np.log( (N - 1) / x))) / 15, label='mu log') pl.legend() pl.xscale('log') pl.yscale('log') pl.xlabel('degree $k$') pl.ylabel('probability') fig.tight_layout() fig.savefig('degree_asymptotic.png', dpi=200) pl.show() #print(np.array(k).mean()) #print("Transitivity from networks =", np.array(C1).mean()) #print("Transitivity from 3-cliques =", A.estimate_transitivity(int(binom(N,3))*50))
'facecolor': 'w', 'edgecolor': 'w', 'pad': 0 }) pl.plot(t, cases, marker=markers[i + 2], c=colors[i + 2], label='data', mfc='None') pl.plot(tt, f(tt, *p), c='k', lw=1, label='$Q_I$') _c = i % (n_col) _r = i // (n_col) if _r == n_row - 1: pl.xlabel('days since Jan. 20th') if _c == 0 and _r == 0: pl.ylabel('confirmed cases', ) pl.gca().yaxis.set_label_coords(-0.3, -0.2) pl.xlim([1, 30]) pl.xscale('log') pl.yscale('log') ylim = ax[i].set_ylim([1, 2e3]) ylim = ax[i].get_ylim() ax[i].plot([12, 12], ylim, ':') ax[i].text(0.03, 0.97, "{}".format(roman[i]), transform=ax[i].transAxes,
params['I0_factor'], ) X = result[2,:]*N I = result[1,:]*N #S = result[0,:]*N pl.plot(t, cases,marker=markers[i],c=colors[i],label='data',mfc='None') pl.plot(tt, X,c='k',label='$Q_I$ (detected and quarantined)') pl.plot(tt, I,'--',c=colors[2],lw=1.5,label='$I$ (undected infected)') #pl.plot(tt, S,label='model') _c = i % n_col _r = i // n_col if _r == n_row-1: pl.xlabel('days since Jan. 21st') if _c == 0: pl.ylabel('confirmed') #pl.title(titlemap[province]) ax[i].text(0.03,0.97, "{}.{}".format(letter[_r], roman[_c]), transform=ax[i].transAxes, ha='left', va='top', fontweight='bold', fontsize=12, bbox={'facecolor':'w','edgecolor':'w','pad':0} ) ax[i].text(0.03,0.8, titlemap[province], transform=ax[i].transAxes,
### variance plot n = 10**5 NMAX = 20 rho = np.linspace(0.0, 0.5, 200) k = 2**np.arange(0, 10, 2) k = np.array(k, dtype=float) if not data_exists: variances = np.zeros((len(rho), len(k))) for i in range(len(rho)): for j in range(len(k)): t = solve_t(k[j], n) variances[i, j] = variance(t, rho[i], n, NMAX) fig, ax = pl.subplots(1, 1, figsize=(4, 2.5)) pl.xlabel(r'local edge weight correlation $\rho$') pl.ylabel(r'node degree variance $\mathrm{Var}(k)$') for i in range(len(k)): pl.plot(rho, variances[:, i], c=bp.brewer_qualitative[1]) bp.strip_axis(ax) ax.set_yscale('log') #ax.set_xscale('log') ax.set_xlim([0, 0.5]) fig.tight_layout() for i in range(len(k)): bp.add_curve_label(ax, rho, variances[:, i],
I0 = 0.01 model.set_initial_conditions({S: 1 - I0, I: I0}) import numpy as np from bfmplot import pl as plt 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),
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()
import numpy as np #import matplotlib.pyplot as plt from bfmplot import pl as plt from ThredgeCorr.basic_patterns import * ### mean degree plots n = 10**5 t = np.linspace(-6, 6, 800) density = np.array([edges(tt) for tt in t]).astype(float) k = (n - 1) * density fig = plt.figure(1, figsize=(8, 4)) ax = fig.add_subplot(1, 2, 1) ax.plot(t, density) plt.xlim(-3, 3) plt.xlabel(r'$t$') plt.ylabel(r'$1 - \Phi\left( t \right)$') ax = fig.add_subplot(1, 2, 2) ax.plot(t, k) plt.xlim(2.5, 4.5) plt.ylim(float((n - 1) * edges(4.5) * 0.9), float(1.1 * (n - 1) * edges(2.5))) ax.set_yscale('log') plt.xlabel(r'$t$') plt.ylabel(r'$E \left[ k \right]$') plt.savefig('figures/edges.pdf') ### variance plot n = 10**5 NMAX = 20
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()
N_meas = 10000 for meas in tqdm(range(N_meas)): tau_con.append(conservative_method(t0, y0)) tau_new.append(new_method(t0, y0)) from bfmplot import pl from scipy.integrate import cumtrapz pl.hist(tau_con, bins=200, density=True, histtype='step') pl.hist(tau_new, bins=200, density=True, histtype='step') t = np.linspace(0, 120, 1000) lamb = np.array([get_event_rates(_t, y0).sum() for _t in t]) integral = cumtrapz(lamb, x=t, initial=0) p = lamb * np.exp(-integral) pl.plot(t, p) pl.xlabel('tau') pl.ylabel('pdf') #pl.yscale('log') pl.gcf().tight_layout() pl.figure() pl.plot(*cum_hist(tau_con)) pl.plot(*cum_hist(tau_new)) pl.plot(t, np.exp(-integral)) pl.ylabel('P(t>tau)') pl.xlabel('tau') #pl.yscale('log') pl.gcf().tight_layout() tau_con = []