def make_dummy_activity_manager(world): timer = Timer( total_days=10, weekday_step_duration=[8, 8, 8], weekend_step_duration=[8, 8, 8], weekday_activities=[ ["primary_activity", "residence"], ["primary_activity", "residence"], ], weekend_activities=[ ["primary_activity", "residence"], ["primary_activity", "residence"], ], ) activity_manager = CampActivityManager( world=world, policies=Policies.from_file(), timer=timer, all_activities=["primary_activity", "residence"], activity_to_super_groups={ "primary_activity": ["learning_centers"], "residence": ["households"], }, ) return activity_manager
def test_time_reset(): timer = Timer(initial_day="2020-03-10", total_days=10) start_time = timer.initial_date next(timer) next(timer) next(timer) next(timer) assert timer.day == 2 timer.reset() assert timer.day == 0 assert timer.shift == 0 assert timer.previous_date == start_time next(timer) next(timer) next(timer) next(timer) assert timer.day == 2
def test_time_is_passing(): timer = Timer(initial_day="2020-03-10", total_days=10) assert timer.now == 0 next(timer) assert timer.now == 0.5 assert timer.previous_date == timer.initial_date next(timer) assert timer.now == 1.0
def test_weekend_transition(): timer = Timer(initial_day="2020-03-10", total_days=10) for _ in range(0, 8): # 5 days for 3 time steps per day next(timer) assert timer.is_weekend is True assert timer.activities == ["residence"] next(timer) assert timer.is_weekend is True assert timer.activities == ["residence"] next(timer) assert timer.is_weekend is False assert timer.activities == ["primary_activity", "residence"]
def test__seed_strength(selector, ): geography = Geography.from_file( filter_key={"super_area": [ "E02004940", "E02004935", "E02004936", ]}) demography = Demography.for_geography(geography) for area in geography.areas: area.populate(demography) n_cases_region = pd.DataFrame({ 'East of England': np.array([100, 200, 300, 400]).astype(np.int), 'date': [ '2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04', ] }) n_cases_region.set_index('date', inplace=True) n_cases_region.index = pd.to_datetime(n_cases_region.index) seed = InfectionSeed.from_file(super_areas=geography.super_areas, selector=selector, n_cases_region=n_cases_region, seed_strength=0.2) timer = Timer( initial_day='2020-02-29', total_days=7, ) while timer.date <= timer.final_date: time = timer.date if (time >= seed.min_date) and (time <= seed.max_date): seed.unleash_virus_per_region(time) next(timer) n_infected = 0 for super_area in geography.super_areas: for person in super_area.people: if person.infected: n_infected += 1 n_cases = (seed.n_cases_region["East of England"].sum()) np.testing.assert_allclose(0.2 * n_cases, n_infected, rtol=0.05)
def test_initial_parameters(): timer = Timer(initial_day="2020-03-10", total_days=10) assert timer.shift == 0 assert timer.is_weekend is False assert timer.day_of_week == "Tuesday"
def from_file( cls, world: World, interaction: Interaction, infection_selector=None, policies: Optional[Policies] = None, infection_seed: Optional[InfectionSeed] = None, leisure: Optional[Leisure] = None, config_filename: str = default_config_filename, save_path: str = "results", ) -> "Simulator": """ Load config for simulator from world.yaml Parameters ---------- save_path leisure infection_seed policies interaction world config_filename The path to the world yaml configuration Returns ------- A Simulator """ with open(config_filename) as f: config = yaml.load(f, Loader=yaml.FullLoader) if world.box_mode: activity_to_groups = None else: activity_to_groups = config["activity_to_groups"] time_config = config["time"] if "checkpoint_dates" in config: if isinstance(config["checkpoint_dates"], datetime.date): checkpoint_dates = [config["checkpoint_dates"]] else: checkpoint_dates = [] for date_str in config["checkpoint_dates"].split(): checkpoint_dates.append( datetime.datetime.strptime(date_str, "%Y-%m-%d").date()) else: checkpoint_dates = None weekday_activities = [ activity for activity in time_config["step_activities"]["weekday"].values() ] weekend_activities = [ activity for activity in time_config["step_activities"]["weekend"].values() ] all_activities = set( chain.from_iterable(weekday_activities + weekend_activities)) cls.check_inputs(time_config) timer = Timer( initial_day=time_config["initial_day"], total_days=time_config["total_days"], weekday_step_duration=time_config["step_duration"]["weekday"], weekend_step_duration=time_config["step_duration"]["weekend"], weekday_activities=time_config["step_activities"]["weekday"], weekend_activities=time_config["step_activities"]["weekend"], ) activity_manager = cls.ActivityManager( world=world, all_activities=all_activities, activity_to_groups=activity_to_groups, leisure=leisure, policies=policies, timer=timer, ) return cls( world=world, activity_manager=activity_manager, timer=timer, infection_selector=infection_selector, infection_seed=infection_seed, save_path=save_path, interaction=interaction, checkpoint_dates=checkpoint_dates, )