Beispiel #1
0
'''
Create the calibrated sim for the King County results. Note: this script relies
on kc_synthpops.ppl, which is created by cache_sp_pop.py, as well as several
CSV files.
'''

# Standard packages
import os
import numpy as np
import pandas as pd
import sciris as sc
import covasim as cv

cv.check_save_version('1.5.0', die=True)

# Define the input files
inputs = 'inputs'
epi_data_file = f'{inputs}/20200719trim5_KingCounty_Covasim.csv'
age_data_file = f'{inputs}/20200719trim5_KingCounty_AgeHist.csv'
safegraph_file = f'{inputs}/KC_weeklyinteractions_071520.csv'
popfile_stem = f'{inputs}/kc_synthpops_normal_withstaff_seed'


def make_safegraph(sim):
    ''' Create interventions representing SafeGraph data '''

    # Load data.values
    fn = safegraph_file
    df = pd.read_csv(fn)
    week = df['week']
    w = c = df['p.tot'].valueshiop
'''
Create the calibrated sim for the King County results. Note: this script relies
on kc_synthpops_with_ltcf_seed, which is created by create_sp_pop.py, as well as several
CSV files.
'''

# Standard packages
import os
import numpy as np
import pandas as pd
import sciris as sc
import covasim as cv

cv.check_save_version('1.7.2', die=False)  # was 1.5.0

# Define the input files
inputs = 'inputs'
epi_data_file = f'{inputs}/20200628chop5_KingCounty_Covasim.csv'
age_data_file = f'{inputs}/20200628chop5_KingCounty_AgeHist.csv'
safegraph_file = f'{inputs}/KC_weeklyinteractions_070120.csv'
popfile_stem = f'{inputs}/kc_synthpops_normal_withstaff_seed'
popfile_stem_change = f'{inputs}/kc_synthpops_clustered_withstaff_seed'


def make_safegraph(sim, mobility_file):
    ''' Create interventions representing SafeGraph data '''

    # Load data.values
    if mobility_file is None:
        fn = safegraph_file
    else:
'''
Benchmark the intervention
'''

import os
import sciris as sc
import covasim as cv
import synthpops as sp
import covasim_schools as cvsch
from testing_in_schools import create_sim as cs
from testing_in_schools.testing_scenarios import generate_scenarios, generate_testing
from testing_in_schools.calibrate_model import evaluate_sim

cv.check_save_version('1.7.2', folder='gitinfo', comments={'SynthPops':sc.gitinfo(sp.__file__)})

debug = False
use_intervention = True
# NOTE: The following may be bypassed below by hard-coded pop_size and folder
folder = '../testing_in_schools/v20201015_225k'
pop_size = 2.25e5 # 1e5 2.25e4 2.25e5
calibfile = os.path.join(folder, 'pars_cases_begin=75_cases_end=75_re=1.0_prevalence=0.002_yield=0.024_tests=225_pop_size=225000.json')

def scenario(es, ms, hs):
    return {
        'pk': None,
        'es': sc.dcp(es),
        'ms': sc.dcp(ms),
        'hs': sc.dcp(hs),
        'uv': None,
    }
Beispiel #4
0
def test_misc():
    sc.heading('Testing miscellaneous functions')

    sim_path = 'test_misc.sim'
    json_path = 'test_misc.json'
    gitinfo_path = 'test_misc.gitinfo'
    fig_path = 'test_misc.png'
    fig_comments = 'Test comment'

    # Data loading
    cv.load_data(csv_file)
    cv.load_data(xlsx_file)

    with pytest.raises(NotImplementedError):
        cv.load_data('example_data.unsupported_extension')

    with pytest.raises(ValueError):
        cv.load_data(xlsx_file, columns=['missing_column'])

    # Dates
    d1 = cv.date('2020-04-04')
    d2 = cv.date(sc.readdate('2020-04-04'))
    ds = cv.date('2020-04-04', d2)
    assert d1 == d2
    assert d2 == ds[0]

    with pytest.raises(ValueError):
        cv.date([(2020, 4, 4)])  # Raises a TypeError which raises a ValueError

    with pytest.raises(ValueError):
        cv.date('Not a date')

    cv.daydiff('2020-04-04')

    # Run sim for more investigations
    sim = cv.Sim(pop_size=500, verbose=0)
    sim.run()
    sim.plot(do_show=False)

    # Saving and loading
    cv.savefig(fig_path, comments=fig_comments)
    cv.save(filename=sim_path, obj=sim)
    cv.load(filename=sim_path)

    # Version checks
    cv.check_version('0.0.0')  # Nonsense version
    print('↑ Should complain about version')
    with pytest.raises(ValueError):
        cv.check_version('0.0.0', die=True)

    # Git checks
    cv.git_info(json_path)
    cv.git_info(json_path, check=True)

    # Poisson tests
    c1 = 5
    c2 = 8
    for alternative in ['two-sided', 'larger', 'smaller']:
        cv.poisson_test(c1, c2, alternative=alternative)
    for method in ['score', 'wald', 'sqrt', 'exact-cond']:
        cv.poisson_test(c1, c2, method=method)

    with pytest.raises(ValueError):
        cv.poisson_test(c1, c2, method='not a method')

    # Test locations
    for location in [None, 'viet-nam']:
        cv.data.show_locations(location)

    # Test versions
    with pytest.raises(ValueError):
        cv.check_save_version('1.3.2', die=True)
    cv.check_save_version(cv.__version__,
                          filename=gitinfo_path,
                          comments='Test')

    # Test PNG
    try:
        metadata = cv.get_png_metadata(fig_path, output=True)
        assert metadata['Covasim version'] == cv.__version__
        assert metadata['Covasim comments'] == fig_comments
    except ImportError as E:
        print(
            f'Cannot test PNG function since pillow not installed ({str(E)}), skipping'
        )

    # Tidy up
    remove_files(sim_path, json_path, fig_path, gitinfo_path)

    return
Beispiel #5
0
'''Show optimized runs'''

# %Imports
import os
import sciris as sc
#from SPS_analysis import create_sim as cs
import create_sim as cs
import covasim as cv
import numpy as np
from covasim import utils as cvu
import pandas as pd
import random

cv.check_save_version('1.7.2',
                      die=False)  # Initial analysis conducted using v1.5.0


def school_dict(sim, school_dicts):
    school_results = dict()
    label = sim.base_sim.label
    school_results[label] = dict()
    if len(sim.sims) > 1:  # if running multiple seeds, print all of them
        school_results[label]['num_teachers'] = []
        school_results[label]['num_teachers_tested'] = []
        school_results[label]['num_teachers_test_pos'] = []
        school_results[label]['num_teachers_screen_pos'] = []
        school_results[label]['num_teacher_cases'] = []

        school_results[label]['num_staff'] = []
        school_results[label]['num_staff_tested'] = []
        school_results[label]['num_staff_test_pos'] = []
'''Show optimized runs'''

import sciris as sc
from reopening_scenarios import create_sim as cs
from calibration import plot_calibration as pltcal
import covasim as cv

cv.check_save_version('1.4.7', die=True)

if __name__ == "__main__":

    rerun = True
    do_save = True
    do_plot = True
    n_reps = 20
    date = '2020-06-17'

    if rerun:
        indices = range(n_reps)
        jsonfile = 'optimization_v12_safegraph_061720.json'
        json = sc.loadjson(jsonfile)

        all_sims = []
        for index in indices:
            entry = json[index]
            pars = entry['pars']
            pars['rand_seed'] = int(entry['index'])
            all_sims.append(cs.create_sim(pars=pars))

        msim = cv.MultiSim(sims=all_sims)
        msim.run(reseed=False,
Beispiel #7
0
'''
Trajetories from the prediction
'''

import sciris as sc
import covasim as cv
import pylab as pl
import matplotlib as mplt
# pl.switch_backend('agg')

# Check version
cv.check_save_version('2.0.0', filename='covasim_version.json', die=True)

mplt.rcParams['font.family'] = 'Roboto'

do_plot = 1
do_save = 0
do_show = 1
verbose = 1
seed = 1

scenario = ['2ndwave', '1stwave'
            ][0]  # Set a number to pick a scenario from the available options
tti_scen = ['current'][0]  # Ditt0

version = 'v1'
date = '2020nov29'
folder = f'results_FINAL_{date}'
file_path = f'{folder}/phase_{version}'  # Completed below
data_path = 'UK_Covid_cases_october15.xlsx'  # 'UK_Covid_cases_december6.xlsx'
fig_path = f'{file_path}_{scenario}.png'