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_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)