Esempio n. 1
0
def test_regression():
    sc.heading('Testing regression...')

    sim1 = cv.load(filename)
    sim2 = make_sim()

    # Check that they match
    cv.diff_sims(sim1, sim2, skip_key_diffs=True, die=True)

    # Confirm that non-matching sims don't match
    sim3 = make_sim(beta=0.02123)
    with pytest.raises(ValueError):
        cv.diff_sims(sim1, sim3, skip_key_diffs=True, die=True)

    return sim1, sim2
Esempio n. 2
0
def test_baseline():
    ''' Compare the current default sim against the saved baseline '''

    # Load existing baseline
    baseline = sc.loadjson(baseline_filename)
    old = baseline['summary']

    # Calculate new baseline
    new = make_sim()
    new.run()

    # Compute the comparison
    cv.diff_sims(old, new, die=True)

    return new
Esempio n. 3
0
def test_basepeople():
    sc.heading('Testing base.py people and contacts...')

    # Create a small sim for later use
    sim = cv.Sim(pop_size=100, verbose=verbose)
    sim.initialize()

    # BasePeople methods
    ppl = sim.people
    ppl.get(['susceptible', 'infectious'])
    ppl.keys()
    ppl.person_keys()
    ppl.state_keys()
    ppl.date_keys()
    ppl.dur_keys()
    ppl.indices()
    ppl._resize_arrays(
        new_size=200
    )  # This only resizes the arrays, not actually create new people
    ppl._resize_arrays(new_size=100)  # Change back
    ppl.to_df()
    ppl.to_arr()
    ppl.person(50)
    people = ppl.to_people()
    ppl.from_people(people)
    ppl.make_edgelist([{'new_key': [0, 1, 2]}])
    ppl.brief()

    # Contacts methods
    contacts = ppl.contacts
    df = contacts['a'].to_df()
    ppl.remove_duplicates(df)
    with pytest.raises(sc.KeyNotFoundError):
        contacts['invalid_key']
    contacts.values()
    len(contacts)
    print(contacts)
    print(contacts['a'])

    # Layer methods
    hospitals_layer = cv.Layer()
    contacts.add_layer(hospitals=hospitals_layer)
    contacts.pop_layer('hospitals')
    df = hospitals_layer.to_df()
    hospitals_layer.from_df(df)

    # Generate an average of 10 contacts for 1000 people
    n = 10_000
    n_people = 1000
    p1 = np.random.randint(n_people, size=n)
    p2 = np.random.randint(n_people, size=n)
    beta = np.ones(n)
    layer = cv.Layer(p1=p1, p2=p2, beta=beta)

    # Convert one layer to another with extra columns
    index = np.arange(n)
    self_conn = p1 == p2
    layer2 = cv.Layer(**layer, index=index, self_conn=self_conn)
    assert len(layer2) == n
    assert len(layer2.keys()) == 5

    # Test dynamic layers, plotting, and stories
    pars = dict(pop_size=100,
                n_days=10,
                verbose=verbose,
                pop_type='hybrid',
                beta=0.02)
    s1 = cv.Sim(pars, dynam_layer={'c': 1})
    s1.run()
    s1.people.plot()
    for person in [0, 50]:
        s1.people.story(person)

    # Run without dynamic layers and assert that the results are different
    s2 = cv.Sim(pars, dynam_layer={'c': 0})
    s2.run()
    assert cv.diff_sims(s1, s2, output=True)

    # Create a bare People object
    ppl = cv.People(100)
    with pytest.raises(sc.KeyNotFoundError):  # Need additional parameters
        ppl.initialize()

    return