def test_plugins(): iterator = f"{script_dir}/plugins/_iterator.py" extractor = f"{script_dir}/plugins/_extractor.py" mover = f"{script_dir}/plugins/_mover.py" mixer = f"{script_dir}/plugins/_mixer.py" params = Parameters() params.set_disease("ncov") population = Population(initial=1000) print("Building the network...") network = Network.single(params=params, population=population) outdir = os.path.join(script_dir, "test_plugins") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=population, output_dir=output_dir, iterator=iterator, extractor=extractor, mixer=mixer, mover=mover, nthreads=1) OutputFiles.remove(outdir, prompt=None) p = trajectory[-1] assert p.advance_test == "advance_test_value" assert p.output_test == "output_test_value" assert p.merge_test == "merge_test_value" assert p.go_test == "go_test_value"
def test_go_partial(prompt=None, nthreads=1): import random seed = random.randint(100000, 1000000) # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease(os.path.join(script_dir, "data", "ncov.json")) params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # the size of the starting population population = Population(initial=57104043) nsteps = 20 demographics = Demographics.load(redblue_json) print("Building the network...") network = Network.build(params=params) network = network.specialise(demographics, nthreads=nthreads) outdir = os.path.join(script_dir, "test_go_to") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, mixer=mix_evenly, mover=move_partial, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) # 70% of the population (roughly) should be in "blue" pop = trajectory[-1] print(pop) frac0 = pop.subpops[0].population / pop.population frac1 = pop.subpops[1].population / pop.population print(frac0, frac1) # pytest.approx error is percent, e.g. 0.05 is within 5% assert pytest.approx(frac0, 0.1) == 0.3 assert pytest.approx(frac1, 0.05) == 0.7 assert pytest.approx(frac0+frac1, 0.01) == 1.0
def test_openfiles(prompt=None): outdir = os.path.join(script_dir, "test_openfiles_output") if os.path.exists(outdir): OutputFiles.remove(outdir, prompt=prompt) of = OutputFiles(outdir) assert of.is_open() assert not of.is_closed() with pytest.raises(ValueError): of.open("../test.txt") with pytest.raises(ValueError): of.open("/test.txt") FILE = of.open("test.txt") assert of.is_open() FILE.write("hello\n") of.close() assert of.is_closed() assert open(os.path.join(outdir, "test.txt")).readline() == "hello\n" with pytest.raises(FileExistsError): OutputFiles(outdir, prompt=None) with OutputFiles(outdir, force_empty=True, prompt=None) as of: FILE = of.open("test.txt") assert of.is_open() assert of.get_filename("test.txt").endswith("test.txt") FILE.write("goodbye\n") FILE = of.open("test2.txt", auto_bzip=True) FILE.write("hello ") assert of.get_filename("test2.txt").endswith("test2.txt.bz2") FILE = of.open("test2.txt") FILE.write("world\n") assert open(os.path.join(outdir, "test.txt")).readline() == "goodbye\n" import bz2 line = bz2.open(os.path.join(outdir, "test2.txt.bz2"), "rt").readline() assert line == "hello world\n" OutputFiles.remove(outdir, prompt=None)
def test_move_isolate(prompt=None, nthreads=1): # user input parameters import random seed = random.randint(100000, 1000000) # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease(os.path.join(script_dir, "data", "ncov.json")) params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # the size of the starting population population = Population(initial=57104043) nsteps = 20 demographics = Demographics.load(isolate_json) print("Building the network...") network = Network.build(params=params) network = network.specialise(demographics, nthreads=nthreads) outdir = os.path.join(script_dir, "test_zero_demographic_output") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, profiler=None, mixer=mix_isolate, mover=move_isolate, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) # there should be no latents in the 'isolate' demographic assert trajectory[-1].subpops[1].latent == 0 print("End of the run")
def test_go_stage(prompt=None, nthreads=1): seed = 797747 # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease(os.path.join(script_dir, "data", "ncov.json")) params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # the size of the starting population population = Population(initial=57104043) nsteps = 20 demographics = Demographics.load(redblue_json) print("Building the network...") network = Network.build(params=params) network = network.specialise(demographics, nthreads=nthreads) outdir = os.path.join(script_dir, "test_go_to") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, mixer=mix_evenly, mover=move_stage, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) # red demographic was seeded, but all moved to blue, so should have # no outbreak pop = trajectory[-1] print(pop)
def test_output(): try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease("ncov") params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # the size of the starting population population = Population(initial=57104043) print("Building the network...") network = Network.build(params=params) from metawards.iterators import build_custom_iterator iterator = build_custom_iterator(iterate_debug, __name__) print("Run the model...") outdir = os.path.join(script_dir, "test_output") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: with Console.redirect_output(outdir, auto_bzip=True): trajectory = network.run(population=population, output_dir=output_dir, iterator=iterator, nthreads=1) OutputFiles.remove(outdir, prompt=None) print(trajectory[-1]) print("End of the run")
def test_bg_foi(): bristol = Ward("Bristol") london = Ward("London") oxford = Ward("Oxford") bristol.set_num_players(100) london.set_num_players(100) oxford.set_num_players(100) bristol.set_bg_foi(10) london.set_bg_foi(1) wards = bristol + london + oxford lurgy = Disease("lurgy") lurgy.add("E", beta=0.0, progress=1.0) lurgy.add("I", beta=0.0, progress=1.0) lurgy.add("R") params = Parameters() params.set_disease(lurgy) network = Network.from_wards(wards, params=params) outdir = os.path.join(script_dir, "test_bg_foi") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.run(population=Population(), output_dir=output_dir, extractor=_check_infections) OutputFiles.remove(outdir, prompt=None) # the only source of infection should be the bg. Should have # no infections in oxford, some in london and most in bristol print(trajectory[-1]) assert trajectory[-1].recovereds <= 200 assert trajectory[-1].susceptibles >= 100
def test_go_record(): bristol = Ward("bristol") london = Ward("london") oxford = Ward("oxford") bristol.set_num_players(100) london.set_num_players(100) oxford.set_num_players(100) bristol.add_workers(0, destination=london) bristol.add_workers(0, destination=oxford) oxford.add_workers(0, destination=bristol) oxford.add_workers(0, destination=london) london.add_workers(0, destination=bristol) london.add_workers(0, destination=oxford) disease = Disease(name="lurgy") disease.add(name="E", beta=0.5, progress=0.5) disease.add(name="I", beta=0.8, progress=0.25) disease.add(name="R") disease.assert_sane() params = Parameters() params.set_disease(disease) params.add_seeds("1 20 bristol") outdir = os.path.join(script_dir, "test_go_ward_output") network = Network.from_wards(bristol + london + oxford, params=params) with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, mover=move_bristol) OutputFiles.remove(outdir, prompt=None) # despite the moves, only Bristolians can be infected as there # are no work movements assert trajectory[-1].recovereds <= 100
def test_mixer_none(prompt=None, nthreads=1): # user input parameters import random seed = random.randint(100000, 1000000) outdir = os.path.join(script_dir, "test_integration_output") network = _get_network(nthreads) # the size of the starting population population = Population(initial=57104043) nsteps = 20 print("Running mix_none...") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: t_mix_none = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, extractor=extract_none, mixer=mix_none, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) print("Running mix_matrix_none...") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: t_mix_m_none = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, extractor=extract_none, mixer=mix_matrix_none, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) print(t_mix_none) print(t_mix_m_none) assert t_mix_none == t_mix_m_none
def test_integration_pox(prompt=None): """This test repeats main_RepeatsNcov.c and validates that the various stages report the same results as the original C code for the POX """ # user input parameters seed = 15324 inputfile = ncovparams_csv line_num = 0 UV = 1.0 # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease("pox") params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # start from the parameters in the specified line number of the # provided input file variables = params.read_variables(inputfile, line_num) # extra parameters that are set params.UV = UV params.static_play_at_home = 0 params.play_to_work = 0 params.work_to_play = 0 params.daily_imports = 0.0 # the size of the starting population population = Population(initial=57104043) profiler = Profiler() print("Building the network...") network = Network.build(params=params, profiler=profiler) params = params.set_variables(variables[0]) network.update(params, profiler=profiler) outdir = os.path.join(script_dir, "test_integration_output") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: print("Run the model...") trajectory = network.run(population=population, seed=seed, output_dir=output_dir, nsteps=31, profiler=profiler, nthreads=1) OutputFiles.remove(outdir, prompt=None) print("End of the run") Console.print_profiler(profiler) Console.rule("Model output") Console.print_population(trajectory[-1]) # The original C code has this expected population after 47 steps expected = Population(initial=57104043, susceptibles=56080780, latent=374, total=370, recovereds=553, n_inf_wards=289, day=31) Console.rule("Expected output") Console.print_population(expected) assert trajectory[-1] == expected
def test_iterator(): """This test repeats main_RepeatsNcov.c and validates that the various stages report the same results as the original C code for ncov, when using a custom integrator that just calls iterate_weekday """ prompt = None # user input parameters seed = 15324 inputfile = ncovparams_csv line_num = 0 UV = 1.0 # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease("ncov") params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # start from the parameters in the specified line number of the # provided input file variables = params.read_variables(inputfile, line_num) # extra parameters that are set params.UV = UV params.static_play_at_home = 0 params.play_to_work = 0 params.work_to_play = 0 params.daily_imports = 0.0 # the size of the starting population population = Population(initial=57104043) profiler = Profiler() print("Building the network...") network = Network.build(params=params, profiler=profiler) params = params.set_variables(variables[0]) network.update(params, profiler=profiler) # Here is a custom integrator function that just calls # iterate_weekday after 'print_hello' def print_hello(**kwargs): print(f"Hello") def my_iterator(**kwargs): from metawards.iterators import iterate_weekday return [print_hello] + iterate_weekday(**kwargs) from metawards.iterators import build_custom_iterator iterator = build_custom_iterator(my_iterator, __name__) print("Run the model...") outdir = os.path.join(script_dir, "test_integrator_output") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.run(population=population, seed=seed, output_dir=output_dir, nsteps=29, profiler=profiler, iterator=iterator, nthreads=1) OutputFiles.remove(outdir, prompt=None) print("End of the run") print(f"Model output: {trajectory}") print(profiler) # The original C code has this expected population after 47 steps expected = Population(initial=57104043, susceptibles=56081710, latent=145, total=48, recovereds=174, n_inf_wards=39, day=29) print(f"Expect output: {expected}") assert trajectory[-1] == expected
def test_pathway(): demographics = Demographics.load(demographics_json) assert len(demographics) == 2 disease_home = Disease.load(filename=home_json) disease_super = Disease.load(filename=super_json) assert demographics[1].disease is None assert demographics[0].disease == disease_super params = Parameters.load() params.set_disease(disease_home) params.set_input_files("single") params.add_seeds("ExtraSeedsOne.dat") network = Network.build(params) print(network.params.disease_params) print(disease_home) assert network.params.disease_params == disease_home network = network.specialise(demographics) print(network.params.disease_params) print(disease_home) assert network.params.disease_params == disease_home print(network.subnets[1].params.disease_params) print(disease_home) assert network.subnets[1].params.disease_params == disease_home print(network.subnets[0].params.disease_params) print(disease_super) assert network.subnets[0].params.disease_params == disease_super infections = network.initialise_infections() assert infections.N_INF_CLASSES == disease_home.N_INF_CLASSES() assert \ infections.subinfs[1].N_INF_CLASSES == disease_home.N_INF_CLASSES() assert \ infections.subinfs[0].N_INF_CLASSES == disease_super.N_INF_CLASSES() assert disease_super.N_INF_CLASSES() != disease_home.N_INF_CLASSES() outdir = os.path.join(script_dir, "test_pathway") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: results = network.copy().run(population=Population(), output_dir=output_dir, mixer=mix_evenly, nthreads=1, seed=36538943) # using one thread, but if use 2 then have a system crash after # any other test that uses the big network. This is because we # have intialised some global data that assumes a large network, # which then fails for the small network OutputFiles.remove(outdir, prompt=None) print(results[-1]) print(results[-1].initial) expected = Population(susceptibles=519, latent=0, total=0, recovereds=481, n_inf_wards=0, day=90) print(expected) assert results[-1].has_equal_SEIR(expected) assert results[-1].day == expected.day with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: results = network.copy().run(population=Population(), output_dir=output_dir, mixer=mix_evenly, nthreads=1, seed=36538943) OutputFiles.remove(outdir, prompt=None) print(results[-1]) print(results[-1].initial) print(expected) assert results[-1].has_equal_SEIR(expected) assert results[-1].day == expected.day variables = VariableSet() print("\nUpdate with null variables") oldparams = network.params params = network.params.set_variables(variables) network.update(params) assert oldparams == network.params print(network.params.disease_params) print(disease_home) assert network.params.disease_params == disease_home print(network.subnets[1].params.disease_params) print(disease_home) assert network.subnets[1].params.disease_params == disease_home print(network.subnets[0].params.disease_params) print(disease_super) assert network.subnets[0].params.disease_params == disease_super infections = network.initialise_infections() assert infections.N_INF_CLASSES == disease_home.N_INF_CLASSES() assert \ infections.subinfs[1].N_INF_CLASSES == disease_home.N_INF_CLASSES() assert \ infections.subinfs[0].N_INF_CLASSES == disease_super.N_INF_CLASSES() assert disease_super.N_INF_CLASSES() != disease_home.N_INF_CLASSES() outdir = os.path.join(script_dir, "test_pathway") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: results = network.copy().run(population=Population(), output_dir=output_dir, mixer=mix_evenly, nthreads=1, seed=36538943) OutputFiles.remove(outdir, prompt=None) print(results[-1]) print(expected) assert results[-1].has_equal_SEIR(expected) assert results[-1].day == expected.day
def test_demographics_reset(prompt=None, nthreads=1, force_multi=False): """This test runs several runs one after another with the expectation that they should all give the same result. This tests that the network is being correctly reset after each run. This test uses a mixer and demographics to show that these can be reset """ # user input parameters import random seed = random.randint(100000, 1000000) inputfile = ncovparams_csv line_num = 0 UV = 0.0 # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease(os.path.join(script_dir, "data", "ncov.json")) params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # start from the parameters in the specified line number of the # provided input file variables = params.read_variables(inputfile, line_num) # extra parameters that are set params.UV = UV params.static_play_at_home = 0 params.play_to_work = 0 params.work_to_play = 0 params.daily_imports = 0.0 # the size of the starting population population = Population(initial=57104043) profiler = Profiler() nsteps = 20 demographics = Demographics.load(redblue_json) print("Building the network...") network = Network.build(params=params, profiler=profiler) network = network.specialise(demographics, nthreads=2, profiler=profiler) outdir = os.path.join(script_dir, "test_integration_output") if can_run_multiprocessing(force_multi): print("Running parallel...") variable = variables[0] variables = VariableSets() variables.append(variable) variables = variables.repeat(3) params = params.set_variables(variables[0]) network.update(params, profiler=profiler) with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: results = run_models(network=network, mixer=mix_shield, output_dir=output_dir, variables=variables, population=population, nsteps=nsteps, nthreads=nthreads, nprocs=2, seed=seed, debug_seeds=True) OutputFiles.remove(outdir, prompt=None) assert len(results) == 3 print(f"Result 1\n{results[0][1][-1]}") print(f"Result 2\n{results[1][1][-1]}") print(f"Result 3\n{results[2][1][-1]}") assert results[0][1] == results[1][1] assert results[0][1] == results[2][1] print("Running model 1...") network.update(params, profiler=profiler) with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory1 = network.run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, profiler=None, mixer=mix_shield, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) # this should reset the network print("Running model 2...") network.update(params, profiler=profiler) with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory2 = network.run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, profiler=None, mixer=mix_shield, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) # this should reset the network print("Running model 3...") network.update(params, profiler=profiler) with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory3 = network.run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, profiler=None, mixer=mix_shield, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) print("End of the run") print(profiler) print(f"Model 1 output: {trajectory1}") print(f"Model 2 output: {trajectory2}") print(f"Model 3 output: {trajectory3}") assert trajectory1 == trajectory2 assert trajectory1 == trajectory3 if can_run_multiprocessing(force_multi): # this should also be the same result as the multiprocessing run assert trajectory1 == results[0][1]
def test_not_infected(): # create a disease where it will finish with everyone in the V state lurgy = Disease("lurgy") lurgy.add("E", beta=0.0, progress=1.0) lurgy.add("I1", beta=0.4, progress=0.2) lurgy.add("I2", beta=0.5, progress=0.5, too_ill_to_move=0.5) lurgy.add("I3", beta=0.5, progress=0.8, too_ill_to_move=0.8) lurgy.add("V", beta=0.0, progress=0.0, is_infected=False) lurgy.add("R") params = Parameters() params.set_input_files("single") params.add_seeds("5") params.set_disease(lurgy) network = Network.build(params=params, population=Population(1000)) outdir = os.path.join(script_dir, "test_not_infected") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, nsteps=1000, nthreads=1) # should finish at about 60 days and not run to 1000 p = trajectory[-1] assert p.day < 900 # there should be no recovereds, as all who were ill went to 'others' assert p.recovereds == 0 assert p.others == p.population - p.susceptibles # Now repeat with more threads with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, nsteps=1000, nthreads=8) # should finish at about 60 days and not run to 1000 p = trajectory[-1] assert p.day < 900 # there should be no recovereds, as all who were ill went to 'others' assert p.recovereds == 0 assert p.others == p.population - p.susceptibles # now repeat with demographics demographics = Demographics.load(redblue_json) network.params.add_seeds("demographic,number\\n1,10") network = network.specialise(demographics) with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, nsteps=1000) # should finish at about 60 days and not run to 1000 p = trajectory[-1] assert p.day < 900 assert p.recovereds == 0 assert p.others > 0 assert p.others == p.population - p.susceptibles p_red = p.subpops[0] p_blue = p.subpops[1] assert p_red.day < 900 assert p_red.recovereds == 0 assert p_red.others > 0 assert p_red.others == p_red.population - p_red.susceptibles assert p_blue.day < 900 assert p_blue.recovereds == 0 assert p_blue.others > 0 assert p_blue.others == p_blue.population - p_blue.susceptibles OutputFiles.remove(outdir, prompt=None)
def test_go_ward(): bristol = Ward("bristol") london = Ward("london") oxford = Ward("oxford") bristol.set_num_players(100) london.set_num_players(100) oxford.set_num_players(100) bristol.add_workers(0, destination=london) bristol.add_workers(0, destination=oxford) oxford.add_workers(0, destination=bristol) oxford.add_workers(0, destination=london) london.add_workers(0, destination=bristol) london.add_workers(0, destination=oxford) disease = Disease(name="lurgy") disease.add(name="E", beta=0.5, progress=0.5) disease.add(name="I", beta=0.8, progress=0.25) disease.add(name="R") disease.assert_sane() params = Parameters() params.set_disease(disease) params.add_seeds("1 20 bristol") outdir = os.path.join(script_dir, "test_go_ward_output") network = Network.from_wards(bristol + london + oxford, params=params) with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, nthreads=1) print(trajectory[-1]) # there are no workers, so can only infect the 100 Bristolians assert trajectory[-1].recovereds <= 100 # Repeat with a different number of threads to test parallel code outdir = os.path.join(script_dir, "test_go_ward_output") with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, nthreads=4) print(trajectory[-1]) # there are no workers, so can only infect the 100 Bristolians assert trajectory[-1].recovereds <= 100 # now do a cyclic move, from "R" to "S" with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, mover=move_cycle, nsteps=200) print(trajectory[-1]) # we should have completed all 200 steps as this will never end assert trajectory[-1].day == 200 assert trajectory[-1].recovereds <= 100 # move everyone to Bristol with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, mover=move_bristol) print(trajectory[-1]) assert trajectory[-1].recovereds > 100 # now move through all of bristol, london, oxford with OutputFiles(outdir, force_empty=True, prompt=None) as output_dir: trajectory = network.copy().run(population=Population(), output_dir=output_dir, mover=move_travel) print(trajectory[-1]) assert trajectory[-1].recovereds > 100 OutputFiles.remove(outdir, prompt=None)
def test_local(): prompt = None # user input parameters seed = 15324 UV = 1.0 # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease("ncov") params.set_input_files("2011Data") params.add_seeds("1 5 2124") # seeding 5 into ward 2124 # extra parameters that are set params.UV = UV params.static_play_at_home = 0 params.play_to_work = 0 params.work_to_play = 0 params.daily_imports = 0.0 # the size of the starting population population = Population(initial=57104043) print("Building the network...") network = Network.build(params=params) outdir = os.path.join(script_dir, "test_local_output") # First check that setting the local cutoff has the same effect # as setting the global cutoff with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=50, iterator=iterate_cutoff, extractor=extract_cutoff, nthreads=1) OutputFiles.remove(outdir, prompt=None) # run setting the global dyn_dist_cutoff to zero with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: net2 = network.copy() net2.params.dyn_dist_cutoff = 0.0 trajectory2 = net2.run(population=population, seed=seed, output_dir=output_dir, nsteps=50, extractor=extract_cutoff, nthreads=1) OutputFiles.remove(outdir, prompt=None) print(f"Model output: {trajectory}") print(f"Model output: {trajectory2}") assert trajectory == trajectory2 # now test that setting the scale_uv has the same effect as # setting the global scale_uv with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=50, iterator=iterate_scale, extractor=extract_scale, nthreads=1) OutputFiles.remove(outdir, prompt=None) # run setting the global dyn_dist_cutoff to zero with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: net2 = network.copy() pop2 = deepcopy(population) pop2.scale_uv = 0.0 trajectory2 = net2.run(population=pop2, seed=seed, output_dir=output_dir, nsteps=50, extractor=extract_scale, nthreads=1) OutputFiles.remove(outdir, prompt=None) print(f"Model output: {trajectory}") print(f"Model output: {trajectory2}") p = trajectory[-1] p2 = trajectory2[-1] # won't be identical as different scale_uv causes different order # of random numbers - but should still have 0 infections assert p.has_equal_SEIR(p2) # now test that setting both to non-zero values has the same effect with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: trajectory = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=50, iterator=iterate_both, nthreads=1) OutputFiles.remove(outdir, prompt=None) # run setting the global dyn_dist_cutoff to zero with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: net2 = network.copy() net2.params.dyn_dist_cutoff = 42.0 pop2 = deepcopy(population) pop2.scale_uv = 0.5 trajectory2 = net2.run(population=pop2, seed=seed, output_dir=output_dir, nsteps=50, nthreads=1) OutputFiles.remove(outdir, prompt=None) print(f"Model output: {trajectory}") print(f"Model output: {trajectory2}") p = trajectory[-1] p2 = trajectory2[-1] p2.scale_uv = 1 assert p == p2
def test_network_copy(prompt=None, nthreads=1): # user input parameters import random seed = random.randint(100000, 1000000) UV = 0.0 # load all of the parameters try: params = Parameters.load(parameters="march29") except Exception as e: print(f"Unable to load parameter files. Make sure that you have " f"cloned the MetaWardsData repository and have set the " f"environment variable METAWARDSDATA to point to the " f"local directory containing the repository, e.g. the " f"default is $HOME/GitHub/MetaWardsData") raise e # load the disease and starting-point input files params.set_disease(os.path.join(script_dir, "data", "ncov.json")) params.set_input_files("2011Data") params.add_seeds("ExtraSeedsBrighton.dat") # extra parameters that are set params.UV = UV params.static_play_at_home = 0 params.play_to_work = 0 params.work_to_play = 0 params.daily_imports = 0.0 # the size of the starting population population = Population(initial=57104043) nsteps = 20 print("Building the network...") network = Network.build(params=params) outdir = os.path.join(script_dir, "test_network_copy") print("Run 1") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: t1 = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, extractor=extract_none, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) print("Run 2") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: t2 = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, extractor=extract_none, nthreads=nthreads) print("Run 3") with OutputFiles(outdir, force_empty=True, prompt=prompt) as output_dir: t3 = network.copy().run(population=population, seed=seed, output_dir=output_dir, nsteps=nsteps, extractor=extract_none, nthreads=nthreads) OutputFiles.remove(outdir, prompt=None) print(t1) print(t2) print(t3) assert t1 == t2 assert t1 == t3