Пример #1
0
def test_initial_prior(doplot=False):
    sc.heading('Create prior distributions')
    width = 0.1
    B = pe.BINNTS(func=objective, x=x, xmin=xmin, xmax=xmax)
    prior_dist_u = B.initialize_priors(prior='uniform')
    print(f'Uniform prior distribution is:\n{prior_dist_u}')
    B.initialize_priors(width=width)
    prior_dist = B.priorpars
    print(f'"Best" prior distribution for x={B.x} is:\n{prior_dist}')
    
    # Check that not found distributions crash
    with pytest.raises(NotImplementedError):
        B.initialize_priors(prior='something_mistyped')
    
    # Optionally plot
    if doplot:
        pl.figure(figsize=figsize)
        for i in range(B.npars):
            bp = prior_dist[i]
            xvec = pl.linspace(B.xmin[i], B.xmax[i])
            priordist = pe.beta_pdf(bp, xvec)
            pl.plot(xvec, priordist, label=f'x={B.x[i]}, alpha={bp[0]:0.2f}, beta={bp[1]:0.2f}')
            pl.legend()
        pl.show()
        
    return prior_dist
Пример #2
0
def test_simple_interv(doplot=False):  # If being run via pytest, turn off
    sc.heading('Test simple intervention')

    def close_schools(sim, t):
        if t == 10:
            print(f'Closing schools on day {t}...')
            sim['beta_pop']['S'] = 0
        return sim

    basepars = {
        'n': 2000,
        'n_infected': 100,
        'n_days': 60,
        'interv_func': close_schools,
        'usepopdata': 'bayesian',
    }

    sim = cova.Sim()
    sim.update_pars(basepars)
    sim.run()

    if doplot:
        sim.plot()

    return sim
Пример #3
0
def test_suggest():
    sc.heading('test_suggest()')
    string = 'foo'
    ex1 = ['Foo', 'Bar']
    ex2 = ['FOO', 'Foo']
    ex3 = ['Foo', 'boo']
    ex4 = ['asldfkj', 'aosidufasodiu']
    ex5 = ['foo', 'fou', 'fol', 'fal', 'fil']
    res1 = sc.suggest(string, ex1)
    res2 = sc.suggest(string, ex2)
    res3 = sc.suggest(string, ex3)
    res4 = sc.suggest(string, ex4, threshold=4)
    with pytest.raises(Exception):
        sc.suggest(string, ex1, threshold=4, die=True)
    res5a = sc.suggest(string, ex5, n=3)
    res5b = sc.suggest(string, ex5, fulloutput=True)
    assert res1 == 'Foo'
    assert res2 == 'Foo'
    assert res3 == 'Foo'
    assert res4 == None
    assert res5a == ['foo', 'fou', 'fol']
    assert res5b == {
        'foo': 0.0,
        'fou': 1.0,
        'fol': 1.0,
        'fal': 2.0,
        'fil': 2.0
    }
    print(res1)
    print(res2)
    print(res3)
    print(res4)
    print(res5a)
    print(res5b)
    return
Пример #4
0
def test_multisim_combine(
        do_plot=do_plot):  # If being run via pytest, turn off
    sc.heading('Combine results test')

    n_runs = 3
    pop_infected = 10

    print('Running first sim...')
    sim = cv.Sim(pop_size=pop_size, pop_infected=pop_infected, verbose=verbose)
    msim = cv.MultiSim(sim)
    msim.run(n_runs=n_runs, keep_people=True)
    sim1 = msim.combine(output=True)
    assert sim1['pop_size'] == pop_size * n_runs

    print(
        'Running second sim, results should be similar but not identical (stochastic differences)...'
    )
    sim2 = cv.Sim(pop_size=pop_size * n_runs,
                  pop_infected=pop_infected * n_runs)
    sim2.run(verbose=verbose)

    if do_plot:
        msim.plot()
        sim2.plot()

    return msim
Пример #5
0
def test_interventions():
    sc.heading('Testing interventions')

    # Create sim
    sim = cv.Sim(pop_size=100, n_days=60, datafile=csv_file, verbose=verbose)

    # Intervention conversion
    ce = cv.InterventionDict(**{
        'which': 'clip_edges',
        'pars': {
            'days': [10, 30],
            'changes': [0.5, 1.0]
        }
    })
    print(ce)
    with pytest.raises(sc.KeyNotFoundError):
        cv.InterventionDict(**{
            'which': 'invalid',
            'pars': {
                'days': 10,
                'changes': 0.5
            }
        })

    # Test numbers and contact tracing
    tn1 = cv.test_num(10, start_day=3, end_day=20)
    tn2 = cv.test_num(daily_tests=sim.data['new_tests'])
    ct = cv.contact_tracing()

    # Create and run
    sim['interventions'] = [ce, tn1, tn2, ct]
    sim.run()

    return
def test_all_tutorials():

    # Get and run tests
    filenames = sc.getfilelist(tex.examples_dir, pattern='t*.py', nopath=True)
    for filename in filenames:
        if filename[1] in '0123456789': # Should have format e.g. t05_foo.py, not test_foo.py
            sc.heading(f'Running {filename}...')
            try:
                tex.run_example(filename)
            except (pickle.PicklingError, NameError) as E: # Ignore these: issue with how the modules are loaded in the run_example function
                print(f'Skipping {filename} due to known pickling error: {E}')
        else:
            print(f'[Skipping "{filename}" since does not match pattern]')

    # Tidy up
    testfiles = sc.getfilelist(tex.examples_dir, pattern='my-*.*')

    sc.heading('Tidying...')
    print(f'Deleting:')
    for filename in testfiles:
        print(f'  {filename}')
    print('in 3 seconds...')
    sc.timedsleep(3)
    for filename in testfiles:
        os.remove(filename)
        print(f'  Deleted {filename}')

    return
Пример #7
0
def test_pop_options(doplot=False):  # If being run via pytest, turn off
    sc.heading('Basic populations tests')

    # popchoices = ['microstructure', 'random']
    popchoices = ['random', 'microstructure']

    basepars = {'n': 5000, 'n_infected': 10, 'n_days': 30}

    sims = sc.objdict()
    for popchoice in popchoices:
        sc.heading(f'Running {popchoice}')
        sims[popchoice] = cv.Sim()
        sims[popchoice].update_pars(basepars)
        sims[popchoice]['usepopdata'] = popchoice
        sims[popchoice].run()

    if doplot:
        for key, sim in sims.items():
            sim.plot()
            try:
                pl.gcf().axes[0].set_title(f'Counts: {key}')
            except:
                pass

    return sims
Пример #8
0
def test_start_stop():  # If being run via pytest, turn off
    sc.heading('Test starting and stopping')

    pars = {'pop_size': 1000}

    # Create and run a basic simulation
    sim1 = cv.Sim(pars)
    sim1.run(verbose=0)

    # Test that step works
    sim2 = cv.Sim(pars)
    sim2.initialize()
    for n in range(sim2.npts):
        sim2.step()
    sim2.finalize()

    # Test that until works
    sim3 = cv.Sim(pars)
    sim3.run(until=20)
    sim3.run()

    # Compare results
    key = 'cum_infections'
    assert (sim1.results[key][:] == sim2.results[key][:]
            ).all(), 'Next values do not match'
    assert (sim1.results[key][:] == sim3.results[key][:]
            ).all(), 'Until values do not match'

    return sim2
Пример #9
0
def test_age_hist():
    sc.heading('Testing age histogram')

    day_list = ["2020-03-20", "2020-04-20"]
    age_analyzer = cv.age_histogram(days=day_list)
    sim = cv.Sim(pars, analyzers=age_analyzer)
    sim.run()

    # Checks to see that compute windows returns correct number of results
    sim.make_age_histogram()  # Show post-hoc example
    agehist = sim.get_analyzer()
    agehist.compute_windows()
    agehist.get()  # Not used, but check get
    agehist.get(day_list[1])
    assert len(agehist.window_hists) == len(
        day_list), "Number of histograms should equal number of days"

    # Check plot()
    if do_plot:
        plots = agehist.plot(windows=True)
        assert len(plots) == len(
            day_list), "Number of plots generated should equal number of days"

    # Check daily age histogram
    daily_age = cv.daily_age_stats()
    sim = cv.Sim(pars, analyzers=daily_age)
    sim.run()

    return agehist
Пример #10
0
def test_choose():
    sc.heading('Choose people')
    x1 = cv.choose(10, 5)
    with pytest.raises(Exception):
        cv.choose_w(10, 5)  # Requesting mroe people than are available
    print(f'Uniform sample from 0-9: {x1}')
    return x1
Пример #11
0
def test_msim(do_plot=False):
    sc.heading('Testing multisim...')

    # basic test for vaccine
    b117 = cv.variant('b117', days=0)
    sim = cv.Sim(use_waning=True, variants=[b117], **base_pars)
    msim = cv.MultiSim(sim, n_runs=2)
    msim.run()
    msim.reduce()

    to_plot = sc.objdict({
        'Total infections': ['cum_infections'],
        'New infections per day': ['new_infections'],
        'New Re-infections per day': ['new_reinfections'],
    })

    if do_plot:
        msim.plot(to_plot=to_plot,
                  do_save=0,
                  do_show=1,
                  legend_args={'loc': 'upper left'},
                  axis_args={'hspace': 0.4},
                  interval=35)

    return msim
Пример #12
0
def test_generate_household_sizes(location='seattle_metro', state_location='Washington', country_location='usa'):
    sc.heading('Generate household sizes')

    Nhomes_to_sample_smooth = 1000
    household_size_distr = sp.get_household_size_distr(datadir, location, state_location, country_location)
    hh_sizes = sp.generate_household_sizes(Nhomes_to_sample_smooth, household_size_distr)
    assert len(hh_sizes) == 7
Пример #13
0
def test_plotting():
    sc.heading('Testing plotting')

    fig_paths = ['plotting_test1.png', 'plotting_test2.png']

    # Create sim with data and interventions
    ce = cv.clip_edges(**{'days': 10, 'changes': 0.5})
    sim = cv.Sim(pop_size=100,
                 n_days=60,
                 datafile=csv_file,
                 interventions=ce,
                 verbose=verbose)
    sim.run(do_plot=True)

    # Handle lesser-used plotting options
    sim.plot(to_plot=['cum_deaths', 'new_infections'],
             sep_figs=True,
             log_scale=['Number of new infections'],
             do_save=True,
             fig_path=fig_paths)

    # Handle Plotly functions
    try:
        cv.plotly_sim(sim)
        cv.plotly_people(sim)
        cv.plotly_animate(sim)
    except Exception as E:
        print(
            f'Plotly plotting failed ({str(E)}), but not essential so continuing'
        )

    # Tidy up
    remove_files(*fig_paths)

    return
Пример #14
0
def test_population():
    sc.heading('Testing the population')

    pop_path = 'pop_test.pop'

    # Test locations, including ones that don't work
    cv.Sim(pop_size=100, pop_type='hybrid', location='nigeria').initialize()
    cv.Sim(pop_size=100, pop_type='hybrid', location='not_a_location').initialize()
    print('↑ Should complain about location not found')
    cv.Sim(pop_size=100, pop_type='random', location='lithuania').initialize()
    print('↑ Should complain about missing h layer')

    # Test synthpops
    try:
        sim = cv.Sim(pop_size=500, pop_type='synthpops')
        sim.initialize()
    except Exception as E:
        errormsg = f'Synthpops test did not pass:\n{str(E)}\nNote: synthpops is optional so this exception is OK.'
        print(errormsg)

    # Not working
    with pytest.raises(ValueError):
        sim = cv.Sim(pop_type='not_an_option')
        sim.initialize()

    # Save/load
    sim = cv.Sim(pop_size=100, popfile=pop_path, save_pop=True)
    sim.initialize()
    cv.Sim(pop_size=100, popfile=pop_path, load_pop=True)
    with pytest.raises(ValueError):
        cv.Sim(pop_size=101, popfile=pop_path, load_pop=True)

    remove_files(pop_path)

    return
Пример #15
0
def test_scenarios(doplot=False):
    sc.heading('Scenarios test')
    scens = cv.Scenarios()
    scens.run()
    if doplot:
        scens.plot()
    return scens
Пример #16
0
 def print_heading(string):
     ''' Choose whether to print a heading, regular text, or nothing '''
     if verbose >= 2:
         sc.heading(string)
     elif verbose == 1:
         print(string)
     return
Пример #17
0
def test_n_single_ages(n_people=1e4,
                       location='seattle_metro',
                       state_location='Washington',
                       country_location='usa'):

    sc.heading('Running single ages')
    sp.validate()
    datadir = sp.datadir

    age_bracket_distr = sp.read_age_bracket_distr(datadir, location,
                                                  state_location,
                                                  country_location)
    gender_fraction_by_age = sp.read_gender_fraction_by_age_bracket(
        datadir, location, state_location, country_location)
    age_brackets_filepath = sp.get_census_age_brackets_path(
        datadir, state_location, country_location)
    age_brackets = sp.get_age_brackets_from_df(age_brackets_filepath)

    # ## Test selecting an age and sex for an individual ###
    a, s = sp.get_age_sex(gender_fraction_by_age, age_bracket_distr,
                          age_brackets)
    print(a, s)

    n_people = int(n_people)
    ages, sexes = [], []
    for p in range(n_people):
        a, s = sp.get_age_sex(gender_fraction_by_age, age_bracket_distr,
                              age_brackets)
        ages.append(a)
        sexes.append(s)

    return
Пример #18
0
def test_fileio():
    sc.heading('Test file saving')

    json_path = 'test_covasim.json'
    xlsx_path = 'test_covasim.xlsx'

    # Create and run the simulation
    sim = cv.Sim()
    sim['n_days'] = 20
    sim['pop_size'] = 1000
    sim.run(verbose=0)

    # Create objects
    json = sim.to_json()
    xlsx = sim.to_excel()
    print(xlsx)

    # Save files
    sim.to_json(json_path)
    sim.to_excel(xlsx_path)

    for path in [json_path, xlsx_path]:
        print(f'Removing {path}')
        os.remove(path)

    return json
Пример #19
0
def test_json():
    sc.heading('Testing JSON read/write functions')

    not_jsonifiable = sc.Blobject(
    )  # Create an object that can't be JSON serialized

    print('Testing jsonifying a NON-jsonifiable object:')
    notjson = sc.jsonify(not_jsonifiable,
                         die=False)  # Will return a string representation
    sc.sanitizejson(not_jsonifiable,
                    die=True)  # Will still not die thanks to jsonpickle

    jsonifiable = sc.objdict().make(keys=['a', 'b'], vals=pl.rand(10))
    json_obj = sc.jsonify(jsonifiable)
    json_str = sc.jsonify(jsonifiable, tostring=True,
                          indent=2)  # kwargs are passed to json.dumps()

    print('Not-a-JSON as sanitized object:')
    print(notjson)
    print('JSON as sanitized object:')
    print(json_obj)
    print('JSON as string:')
    print(json_str)

    return json_str
Пример #20
0
def test_plotting():
    sc.heading('Testing plotting')

    fig_path = 'plotting_test.png'

    # Create sim with data and interventions
    ce = cv.clip_edges(**{'days': 10, 'changes': 0.5})
    sim = cv.Sim(pop_size=100,
                 n_days=60,
                 datafile=csv_file,
                 interventions=ce,
                 verbose=verbose)
    sim.run(do_plot=True)

    # Handle lesser-used plotting options
    sim.plot(to_plot=['cum_deaths', 'new_infections'],
             sep_figs=True,
             font_family='Arial',
             log_scale=['Number of new infections'],
             interval=5,
             do_save=True,
             fig_path=fig_path)
    print('↑ May print a warning about zero values')

    # Handle Plotly functions
    cv.plotly_sim(sim)
    cv.plotly_people(sim)
    cv.plotly_animate(sim)

    # Tidy up
    remove_files(fig_path)

    return
Пример #21
0
def test_migration():
    sc.heading('Testing migration...')

    # Create sim and people
    base = make_sim()
    base.people.version = version
    sim = cv.load(filename)
    sim.people = base.people

    # Create msim
    msim = cv.MultiSim(base_sim=sim)
    del msim.version  # To simulate <2.0.0
    msim.init_sims()

    # Create scenarios
    scens = cv.Scenarios(sim=sim)
    del scens.version  # To simulate <2.0.0

    # Try migrations
    new_sim = cv.migrate(sim, die=True)
    new_msim = cv.migrate(msim, die=True)
    new_scens = cv.migrate(scens, die=True)

    # Try something un-migratable
    with pytest.raises(TypeError):
        cv.migrate('Strings are not migratable', die=True)

    return new_sim, new_msim, new_scens
Пример #22
0
def test_promotetolist():
    sc.heading('test_promotetolist()')
    ex0 = 1
    ex1 = 'a'
    ex2 = {'a', 'b'}
    ex3 = np.array([0, 1, 2])
    ex4 = [1, 2, 3]
    res0 = sc.promotetolist(ex0, int)
    res1 = sc.promotetolist(ex1)
    res2a = sc.promotetolist(ex2)
    res2b = sc.promotetolist(ex2, objtype='str')
    res3a = sc.promotetolist(ex3)
    res3b = sc.promotetolist(ex3, objtype='number')
    with pytest.raises(TypeError):
        sc.promotetolist(ex0, str)
    with pytest.raises(TypeError):
        sc.promotetolist(ex1, int)
    with pytest.raises(TypeError):
        sc.promotetolist(ex3, objtype='str')
    with pytest.raises(TypeError):
        sc.promotetolist(ex4, objtype='str')
    assert res0 == [1]
    assert res1 == ['a']
    assert res2a == [{'a', 'b'}]
    assert sorted(res2b) == ['a', 'b']  # Sets randomize the order...
    assert repr(res3a) == repr([np.array(
        [0, 1, 2])])  # Direct quality comparison fails due to the array
    assert res3b == [0, 1, 2]
    print(res1)
    print(res2a)
    print(res2b)
    print(res3a)
    print(res3b)
    return
Пример #23
0
def test_creation():
    sc.heading('Create class')
    B = pe.BINNTS(func=objective, x=x, xmin=xmin, xmax=xmax)
    assert B.iteration == 0 # Only one of various things that could be tested
    output = B.func(x)
    print(f'Default output is: {output}')
    return B
Пример #24
0
def test_multirun(do_plot=do_plot):  # If being run via pytest, turn off
    sc.heading('Multirun test')

    n_days = 60

    # Method 1 -- Note: this runs 3 simulations, not 3x3!
    iterpars = {
        'beta': [0.015, 0.025, 0.035],
        'iso_factor': [0.1, 0.5, 0.9],
    }
    sim = cv.Sim(n_days=n_days, pop_size=pop_size)
    sims = cv.multi_run(sim=sim, iterpars=iterpars, verbose=verbose)

    # Method 2 -- run a list of sims
    simlist = []
    for i in range(len(iterpars['beta'])):
        sim = cv.Sim(n_days=n_days,
                     pop_size=pop_size,
                     beta=iterpars['beta'][i],
                     iso_factor=iterpars['iso_factor'][i])
        simlist.append(sim)
    sims2 = cv.multi_run(sim=simlist, verbose=verbose)

    # Run in serial for debugging
    cv.multi_run(sim=cv.Sim(n_days=n_days, pop_size=pop_size),
                 n_runs=2,
                 parallel=False)

    if do_plot:
        for sim in sims + sims2:
            sim.plot()

    return sims
Пример #25
0
def test_pop_options(doplot=False):  # If being run via pytest, turn off
    sc.heading('Basic populations tests')

    popchoices = ['random', 'bayesian']
    if sp.config.full_data_available:
        popchoices.append('data')

    basepars = {'n': 3000, 'n_infected': 10, 'contacts': 20, 'n_days': 90}

    sims = sc.objdict()
    for popchoice in popchoices:
        sc.heading(f'Running {popchoice}')
        sims[popchoice] = cova.Sim()
        sims[popchoice].update_pars(basepars)
        sims[popchoice]['usepopdata'] = popchoice
        sims[popchoice].run()

    if doplot:
        for key, sim in sims.items():
            sim.plot()
            try:
                pl.gcf().axes[0].set_title(f'Counts: {key}')
            except:
                pass

    return sims
Пример #26
0
def test_beds(do_plot=False, do_show=True, do_save=False, fig_path=None):
    sc.heading('Test of bed capacity estimation')

    sc.heading('Setting up...')

    sc.tic()

    n_runs = 3
    verbose = 1

    basepars = {'n': 1000}
    metapars = {'n_runs': n_runs}

    sim = cv.Sim()

    # Define the scenarios
    scenarios = {
        'baseline': {
            'name': 'No bed constraints',
            'pars': {
                'n_infected': 100
            }
        },
        'bedconstraint': {
            'name': 'Only 10 beds available',
            'pars': {
                'n_infected': 100,
                'n_beds': 10,
            }
        },
        'bedconstraint2': {
            'name':
            'Only 1 bed available, people are 10x more likely to die if not hospitalized',
            'pars': {
                'n_infected': 100,
                'n_beds': 1,
                'OR_no_treat': 10.,
            }
        },
    }

    scens = cv.Scenarios(sim=sim,
                         basepars=basepars,
                         metapars=metapars,
                         scenarios=scenarios)
    scens.run(verbose=verbose, debug=debug)

    if do_plot:
        to_plot = sc.odict({
            'cum_deaths': 'Cumulative deaths',
            #            'bed_capacity': 'People needing beds / beds',
            'n_severe': 'Number of cases requiring hospitalization',
            'n_critical': 'Number of cases requiring ICU',
        })
        scens.plot(to_plot=to_plot,
                   do_save=do_save,
                   do_show=do_show,
                   fig_path=fig_path)

    return scens
Пример #27
0
def test_scenarios(do_plot=False):
    sc.heading('Scenarios test')
    basepars = {'n':1000}
    scens = cv.Scenarios(basepars=basepars)
    scens.run()
    if do_plot:
        scens.plot()
    return scens
Пример #28
0
def test_insert():
    sc.heading('Insert:')
    z = sc.odict()
    z['foo'] = 1492
    z.insert(1604)
    z.insert(0, 'ganges', 1444)
    z.insert(2, 'midway', 1234)
    printexamples([z])
Пример #29
0
def test_make_popdict_generic(n=default_n):
    sc.heading(f'Making popdict for {n} people')
    n = int(n)

    popdict = sp.make_population(
        n=n, use_demography=False)  # Non-USA not implemented

    return popdict
Пример #30
0
def test_people():
    sc.heading('Testing people (dynamic layers)')

    # Test dynamic layers
    sim = cv.Sim(pop_size=100, n_days=10, verbose=verbose, dynam_layer={'a':1})
    sim.run()

    return