Exemple #1
0
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()
Exemple #2
0
def set_n_ticks(ax=None, nx=3, ny=2):
    """Set the number of major ticks for each axis"""
    if ax is None:
        ax = pl.gca()
    old_ax = pl.gca()
    pl.sca(ax)

    if ny is not None:
        pl.locator_params(axis='y', nbins=ny)

    if nx is not None:
        pl.locator_params(axis='x', nbins=nx)

    pl.sca(old_ax)
Exemple #3
0
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()
Exemple #4
0
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()
Exemple #5
0
    edges = get_fast_edge_list(N, covariance, t)
    ks = get_degrees_from_edge_list(N, edges).tolist()
    k1.extend(ks)

k1 = np.array(k1, dtype=int)
k1pos = k1[k1 >= 1]

import powerlaw
results = powerlaw.Fit(k1pos, discrete=True, xmin=1)

fig = pl.figure()
powerlaw.plot_ccdf(k1pos)
#powerlaw.plot_pdf(k1)
#pl.hist(k1,bins=np.arange(1,max(k1)+1),histtype='step',density=True)
x = np.arange(1, max(k1pos))
results.lognormal.plot_ccdf(ax=pl.gca())
#results.lognormal.plot_pdf(ax=pl.gca())

pl.xscale('log')
pl.yscale('log')

fig = pl.figure()
pl.hist(
    k1,
    bins=np.arange(max(k1) + 1),
    histtype='step',
    density=True,
    label=r'10 networks, $N=10^4,\rho=0.5,\left\langle k\right\rangle = 2$')
x = np.arange(1, max(k1pos))

a0 = 2.30753
               })
    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,
               ha='left',
               va='top',
               fontweight='bold',
expected_counts[model.get_compartment_id(B)] = N_measurements * rateB / (rateB + rateE + rateA)
expected_counts[model.get_compartment_id(E)] = N_measurements * rateE / (rateB + rateE + rateA)

expected_counts[model.get_compartment_id(C0)] = N_measurements * ((N-1)*rateA / (rateB + rateE + rateA) * probAC0)
expected_counts[model.get_compartment_id(C1)] = N_measurements * ((N-1)*rateA / (rateB + rateE + rateA) * probAC1)

expected_counts[model.get_compartment_id(D)] = N_measurements * ((N-1)*rateB / (rateB + rateE + rateA) * probBD)

expected_counts[model.get_compartment_id(F)] = (N-1) * expected_counts[model.get_compartment_id(E)]

expected_counts[model.get_compartment_id(S)] = N_measurements * N - expected_counts.sum()

pl.bar(x+width/2, expected_counts, width)

pl.xticks(x)
pl.gca().set_xticklabels(model.compartments)

pl.figure()

ndx = np.where(expected_counts==0)
counts[ndx] = 1
expected_counts[ndx] = 1

pl.plot(x, np.abs(1-counts/expected_counts))

from scipy.stats import entropy

_counts = np.delete(counts,1)
_exp_counts = np.delete(expected_counts,1)

print(entropy(_counts, _exp_counts))
               0.03,
               r"$P=%4.2f$" %
               ((params['kappa0'].value) /
                (params['kappa'].value + params['kappa0'].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, 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)
    #       va='bottom',
    #       bbox={'facecolor':'w','edgecolor':'w','pad':0}
    #       )

    #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,
            va=max_dates_va[i],
            color=colors[0],
            fontsize=9,
            bbox={'facecolor':'w','edgecolor':'w','pad':0}
            )
    ax[i].plot([22.5,22.5], [0,cases[-1]],':',c=colors[0],lw=1.5)

#pl.plot(tt, S,label='model')
    
    _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.title(titlemap[province])
    ax[i].text(0.03,0.97,
            "{}".format(roman[i]),
            transform=ax[i].transAxes,
            ha='left',
            va='top',
            fontweight='bold',
            fontsize=10,
            bbox={'facecolor':'w','edgecolor':'w','pad':0}
            )
    ax[i].text(0.03,0.8,
            titlemap[province],
            transform=ax[i].transAxes,
            ha='left',
            va='top',
Exemple #11
0
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),
    (I, quarantine_rate, Q),
])
model.set_initial_conditions({S: N - I0, I: I0})
Exemple #12
0
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()