def test_no_null_transition(base_config): base_config.update({'population': {'population_size': 10000}}) a_state = State('a') b_state = State('b') start_state = State('start') a_transition = Transition( start_state, a_state, probability_func=lambda index: pd.Series(0.5, index=index)) b_transition = Transition( start_state, b_state, probability_func=lambda index: pd.Series(0.5, index=index)) start_state.transition_set.allow_null_transition = False start_state.transition_set.extend((a_transition, b_transition)) machine = Machine('state') machine.states.extend([start_state, a_state, b_state]) simulation = InteractiveContext( components=[machine, _population_fixture('state', 'start')], configuration=base_config) event_time = simulation._clock.time + simulation._clock.step_size machine.transition(simulation.get_population().index, event_time) a_count = (simulation.get_population().state == 'a').sum() assert round(a_count / len(simulation.get_population()), 1) == 0.5
def test_lookup_table_interpolated_return_types(base_config): year_start = base_config.time.start.year year_end = base_config.time.end.year data = build_table(lambda age, sex, year: year, year_start, year_end) simulation = InteractiveContext(components=[TestPopulation()], configuration=base_config) manager = simulation._tables table = (manager._build_table(data, key_columns=['sex'], parameter_columns=['age', 'year'], value_columns=None)( simulation.get_population().index)) # make sure a single value column is returned as a series assert isinstance(table, pd.Series) # now add a second value column to make sure the result is a df data['value2'] = data.value table = (manager._build_table(data, key_columns=['sex'], parameter_columns=['age', 'year'], value_columns=None)( simulation.get_population().index)) assert isinstance(table, pd.DataFrame)
def build_sample_history_single_scenario(output_path: Union[str, Path], location: str, scenario: int): # NOTE: This is 100% necessary or the qsub will fail location = location.strip('"') output_path = Path(output_path) sim = InteractiveContext(paths.MODEL_SPEC_DIR / f'{location}.yaml', setup=False) sim.add_components([SampleHistoryObserver()]) sim.configuration.update({ 'ldlc_treatment_algorithm': { 'scenario': scenario }, 'metrics': { 'disability': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'mortality': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'ischemic_heart_disease_observer': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'ischemic_stroke_observer': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'diabetes_mellitus_observer': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'chronic_kidney_disease_observer': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'miscellaneous_observer': { 'by_age': False, 'by_sex': False, 'by_year': False, }, 'sample_history_observer': { 'path': f'{output_path}/{scenario}_sample_history.hdf' }, }, }) sim.setup() sim.run() sim.finalize() logger.info('**DONE**')
def test_transition(): done_state = State('done') start_state = State('start') start_state.add_transition(done_state) machine = Machine('state', states=[start_state, done_state]) simulation = InteractiveContext( components=[machine, _population_fixture('state', 'start')]) event_time = simulation._clock.time + simulation._clock.step_size machine.transition(simulation.get_population().index, event_time) assert np.all(simulation.get_population().state == 'done')
def test_lookup_table_scalar_from_single_value(base_config): simulation = InteractiveContext(components=[TestPopulation()], configuration=base_config) manager = simulation._tables table = (manager._build_table(1, key_columns=None, parameter_columns=None, value_columns=['a'])( simulation.get_population().index)) assert isinstance(table, pd.Series) assert np.all(table == 1)
def test_side_effects(): class DoneState(State): @property def name(self): return "test_done_state" def setup(self, builder): super().setup(builder) self.population_view = builder.population.get_view(['count']) def _transition_side_effect(self, index, event_time): pop = self.population_view.get(index) self.population_view.update(pop['count'] + 1) done_state = DoneState('done') start_state = State('start') start_state.add_transition(done_state) done_state.add_transition(start_state) machine = Machine('state', states=[start_state, done_state]) simulation = InteractiveContext(components=[ machine, _population_fixture('state', 'start'), _population_fixture('count', 0) ]) event_time = simulation._clock.time + simulation._clock.step_size machine.transition(simulation.get_population().index, event_time) assert np.all(simulation.get_population()['count'] == 1) machine.transition(simulation.get_population().index, event_time) assert np.all(simulation.get_population()['count'] == 1) machine.transition(simulation.get_population().index, event_time) assert np.all(simulation.get_population()['count'] == 2)
def test_lookup_table_scalar_from_list(base_config): simulation = InteractiveContext(components=[TestPopulation()], configuration=base_config) manager = simulation._tables table = (manager._build_table( (1, 2), key_columns=None, parameter_columns=None, value_columns=['a', 'b'])(simulation.get_population().index)) assert isinstance(table, pd.DataFrame) assert table.columns.values.tolist() == ['a', 'b'] assert np.all(table.a == 1) assert np.all(table.b == 2)
def test_prevalence_multiple_sequelae(base_config, base_plugins, disease, base_data, test_prevalence_level): year_start = base_config.time.start.year year_end = base_config.time.end.year healthy = BaseDiseaseState('healthy') sequela = dict() for i, p in enumerate(test_prevalence_level): data_funcs = base_data(p) data_funcs.update({'disability_weight': lambda _, __: 0.0}) sequela[i] = DiseaseState('sequela' + str(i), get_data_functions=data_funcs) model = DiseaseModel(disease, initial_state=healthy, states=[healthy, sequela[0], sequela[1], sequela[2]]) base_config.update({'population': { 'population_size': 100000 }}, **metadata(__file__)) simulation = InteractiveContext(components=[TestPopulation(), model], configuration=base_config, plugin_configuration=base_plugins) error_message = "initial sequela status of simulants should be matched to the prevalence data." assert np.allclose([ get_test_prevalence(simulation, 'sequela0'), get_test_prevalence(simulation, 'sequela1'), get_test_prevalence(simulation, 'sequela2') ], test_prevalence_level, .02), error_message
def test_null_transition(base_config): base_config.update({'population': {'population_size': 10000}}) a_state = State('a') start_state = State('start') start_state.add_transition( a_state, probability_func=lambda agents: np.full(len(agents), 0.5)) start_state.allow_self_transitions() machine = Machine('state', states=[start_state, a_state]) simulation = InteractiveContext( components=[machine, _population_fixture('state', 'start')], configuration=base_config) event_time = simulation._clock.time + simulation._clock.step_size machine.transition(simulation.get_population().index, event_time) a_count = (simulation.get_population().state == 'a').sum() assert round(a_count / len(simulation.get_population()), 1) == 0.5
def test_incidence(base_config, base_plugins, disease): year_start = base_config.time.start.year year_end = base_config.time.end.year time_step = pd.Timedelta(days=base_config.time.step_size) healthy = BaseDiseaseState('healthy') sick = BaseDiseaseState('sick') key = f"sequela.acute_myocardial_infarction_first_2_days.incidence_rate" transition = RateTransition(input_state=healthy, output_state=sick, get_data_functions={ 'incidence_rate': lambda _, builder: builder.data.load(key) }) healthy.transition_set.append(transition) model = DiseaseModel(disease, initial_state=healthy, states=[healthy, sick]) simulation = InteractiveContext(components=[TestPopulation(), model], configuration=base_config, plugin_configuration=base_plugins, setup=False) simulation._data.write(key, 0.7) simulation.setup() incidence_rate = simulation._values.get_value('sick.incidence_rate') simulation.step() assert np.allclose(from_yearly(0.7, time_step), incidence_rate(simulation.get_population().index), atol=0.00001)
def test_age_out_simulants(config, base_plugins): start_population_size = 10000 num_days = 600 time_step = 100 # Days config.update( { 'population': { 'population_size': start_population_size, 'age_start': 4, 'age_end': 4, 'exit_age': 5, }, 'time': { 'step_size': time_step } }, layer='override') components = [bp.BasePopulation()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins) time_start = simulation._clock.time assert len(simulation.get_population()) == len( simulation.get_population().age.unique()) simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() assert len(pop) == len(pop[~pop.tracked]) exit_after_300_days = pop.exit_time >= time_start + pd.Timedelta(300, unit='D') exit_before_400_days = pop.exit_time <= time_start + pd.Timedelta(400, unit='D') assert len(pop) == len(pop[exit_after_300_days & exit_before_400_days])
def test_Mortality(config, base_plugins): num_days = 365 components = [TestPopulation(), Mortality()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins, setup=False) df = pd.read_csv(config.path_to_mortality_file) # to save time, only look at locatiosn existing on the test dataset. mortality_rate_df = df[df['LAD.code'] == 'E08000032'] asfr_data = transform_rate_table(mortality_rate_df, 2011, 2012, config.population.age_start, config.population.age_end) simulation._data.write("cause.all_causes.cause_specific_mortality_rate", asfr_data) simulation.setup() simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() print('alive', len(pop[pop['alive'] == 'alive'])) print('dead', len(pop[pop['alive'] != 'alive'])) assert (np.all(pop.alive == 'alive') == False)
def test_invalid_data_type_build_table(base_config): simulation = InteractiveContext(components=[TestPopulation()], configuration=base_config) manager = simulation._tables with pytest.raises(TypeError): manager._build_table('break', key_columns=None, parameter_columns=None, value_columns=None)
def get_age_bin(config: Path, age_group_id: int) -> pd.Interval: sim = InteractiveContext(config, setup=False) artifact_path = sim.configuration.input_data.artifact_path artifact = Artifact(artifact_path) age_bins = artifact.load(data_keys.POPULATION.AGE_BINS).reset_index().set_index('age_group_id') age_bin = pd.Interval(age_bins.loc[age_group_id, 'age_start'], age_bins.loc[age_group_id, 'age_end']) return age_bin
def test_interpolated_tables__exact_values_at_input_points(base_config): year_start = base_config.time.start.year year_end = base_config.time.end.year years = build_table(lambda age, sex, year: year, year_start, year_end) input_years = years.year_start.unique() base_config.update({'population': {'population_size': 10000}}) simulation = InteractiveContext(components=[TestPopulation()], configuration=base_config) manager = simulation._tables years = manager._build_table(years, key_columns=['sex'], parameter_columns=['age', 'year'], value_columns=None) for year in input_years: simulation._clock._time = pd.Timestamp(year, 1, 1) assert np.allclose(years(simulation.get_population().index), simulation._clock.time.year + 1 / 365)
def test_FertilityCrudeBirthRate_extrapolate_fail(config, base_plugins): config.update({ 'interpolation': { 'extrapolate': False }, 'time': { 'start': {'year': 2016}, 'end': {'year': 2025}, }, }) components = [TestPopulation(), FertilityCrudeBirthRate()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins, setup=False) simulation._data.write("covariate.live_births_by_sex.estimate", crude_birth_rate_data()) with pytest.raises(ValueError): simulation.setup()
def test_FertilityDeterministic(config): pop_size = config.population.population_size annual_new_simulants = 1000 step_size = config.time.step_size num_days = 100 config.update({ 'fertility': { 'number_of_new_simulants_each_year': annual_new_simulants } }, **metadata(__file__)) components = [TestPopulation(), FertilityDeterministic()] simulation = InteractiveContext(components=components, configuration=config) num_steps = simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() assert num_steps == num_days // step_size assert np.all(pop.alive == 'alive') assert int(num_days * annual_new_simulants / utilities.DAYS_PER_YEAR) == len(pop.age) - pop_size
def test_interpolated_tables_without_uninterpolated_columns(base_config): year_start = base_config.time.start.year year_end = base_config.time.end.year years = build_table(lambda age, sex, year: year, year_start, year_end) del years['sex'] years = years.drop_duplicates() base_config.update({ 'population': { 'population_size': 10000 }, 'interpolation': { 'order': 1 } }) # the results we're checking later assume interp order 1 simulation = InteractiveContext(components=[TestPopulation()], configuration=base_config) manager = simulation._tables years = manager.build_table(years, key_columns=(), parameter_columns=( 'year', 'age', ), value_columns=None) result_years = years(simulation.get_population().index) fractional_year = simulation._clock.time.year fractional_year += simulation._clock.time.timetuple().tm_yday / 365.25 assert np.allclose(result_years, fractional_year) simulation._clock._time += pd.Timedelta(30.5 * 125, unit='D') result_years = years(simulation.get_population().index) fractional_year = simulation._clock.time.year fractional_year += simulation._clock.time.timetuple().tm_yday / 365.25 assert np.allclose(result_years, fractional_year)
def test_mortality_rate(base_config, base_plugins, disease): year_start = base_config.time.start.year year_end = base_config.time.end.year time_step = pd.Timedelta(days=base_config.time.step_size) healthy = BaseDiseaseState('healthy') mort_get_data_funcs = { 'dwell_time': lambda _, __: pd.Timedelta(days=0), 'disability_weight': lambda _, __: 0.0, 'prevalence': lambda _, __: build_table(0.000001, year_start - 1, year_end, ['age', 'year', 'sex', 'value']), 'excess_mortality_rate': lambda _, __: build_table(0.7, year_start - 1, year_end), } mortality_state = DiseaseState('sick', get_data_functions=mort_get_data_funcs) healthy.add_transition(mortality_state) model = DiseaseModel(disease, initial_state=healthy, states=[healthy, mortality_state]) simulation = InteractiveContext( components=[TestPopulation(), model, Mortality()], configuration=base_config, plugin_configuration=base_plugins) mortality_rate = simulation._values.get_value('mortality_rate') simulation.step() # Folks instantly transition to sick so now our mortality rate should be much higher assert np.allclose( from_yearly(0.7, time_step), mortality_rate(simulation.get_population().index)['sick'])
def test_acmr(): sim = InteractiveContext( 'src/vivarium_conic_lsff/model_specifications/india.yaml') sim.step() # make sure everything is setup pop = sim.get_population() age_groups = pd.cut(pop.age, bins=[0, 7 / 365, 28 / 365, 1, 5], right=False) mr_pipeline = sim.get_value('mortality_rate') acmr_orig = mr_pipeline.source(pop.index).sum(axis=1) acmr_w_risk = mr_pipeline( pop.index).sum(axis=1) * 365 # convert back to "per person-year" # confirm that they are *not* identical at the individual level assert not np.allclose( acmr_orig, acmr_w_risk, rtol=.05), 'expect acmr to be quite different for some individuals' # but close at pop level assert np.allclose( acmr_orig.groupby(age_groups).median(), acmr_w_risk.groupby(age_groups).median(), rtol=.1 ), 'expect acmr to be within 10% of original at population level'
def test_fertility_module(config, base_plugins): start_population_size = config.population.population_size num_days = 365 * 3 components = [TestPopulation(), FertilityAgeSpecificRates()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins, setup=False) df = pd.read_csv(config.path_to_fertility_file) # to save time, only look at locatiosn existing on the test dataset. fertility_rate_df = df[(df['LAD.code'] == 'E08000032')] asfr_data = transform_rate_table(fertility_rate_df, 2011, 2012, 10, 50, [2]) # Mock Fertility Data simulation._data.write("covariate.age_specific_fertility_rate.estimate", asfr_data) simulation.setup() time_start = simulation._clock.time assert 'last_birth_time' in simulation.get_population().columns, \ 'expect Fertility module to update state table.' assert 'parent_id' in simulation.get_population().columns, \ 'expect Fertility module to update state table.' simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() print(pop) # No death in this model. assert np.all(pop.alive == 'alive'), 'expect all simulants to be alive' # TODO: Write a more rigorous test. assert len(pop.age) > start_population_size, 'expect new simulants' for i in range(start_population_size, len(pop)): assert pop.loc[pop.iloc[i].parent_id].last_birth_time >= time_start, 'expect all children to have mothers who' \ ' gave birth after the simulation starts.'
def test_risk_deletion(base_config, base_plugins, disease): time_step = base_config.time.step_size time_step = pd.Timedelta(days=time_step) year_start = base_config.time.start.year year_end = base_config.time.end.year base_rate = 0.7 paf = 0.1 healthy = BaseDiseaseState('healthy') sick = BaseDiseaseState('sick') key = "sequela.acute_myocardial_infarction_first_2_days.incidence_rate" transition = RateTransition(input_state=healthy, output_state=sick, get_data_functions={ 'incidence_rate': lambda _, builder: builder.data.load(key) }) healthy.transition_set.append(transition) model = DiseaseModel(disease, initial_state=healthy, states=[healthy, sick]) class PafModifier: @property def name(self): return 'paf_modifier' def setup(self, builder): builder.value.register_value_modifier( 'sick.incidence_rate.paf', modifier=simulation._tables.build_table( build_table(paf, year_start, year_end), key_columns=('sex', ), parameter_columns=['age', 'year'], value_columns=None)) simulation = InteractiveContext( components=[TestPopulation(), model, PafModifier()], configuration=base_config, plugin_configuration=base_plugins, setup=False) simulation._data.write(key, base_rate) simulation.setup() incidence_rate = simulation._values.get_value('sick.incidence_rate') simulation.step() expected_rate = base_rate * (1 - paf) assert np.allclose(from_yearly(expected_rate, time_step), incidence_rate(simulation.get_population().index), atol=0.00001)
def test_BasePopulation(config, base_plugins, generate_population_mock): num_days = 600 time_step = 100 # Days sims = make_full_simulants() start_population_size = len(sims) generate_population_mock.return_value = sims.drop(columns=['tracked']) base_pop = bp.BasePopulation() components = [base_pop] config.update( { 'population': { 'population_size': start_population_size }, 'time': { 'step_size': time_step } }, layer='override') simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins) time_start = simulation._clock.time pop_structure = simulation._data.load('population.structure') pop_structure['location'] = simulation.configuration.input_data.location uniform_pop = dt.assign_demographic_proportions(pop_structure) assert base_pop.population_data.equals(uniform_pop) age_params = { 'age_start': config.population.age_start, 'age_end': config.population.age_end } sub_pop = bp.BasePopulation.select_sub_population_data( uniform_pop, time_start.year) generate_population_mock.assert_called_once() # Get a dictionary of the arguments used in the call mock_args = generate_population_mock.call_args[1] assert mock_args[ 'creation_time'] == time_start - simulation._clock.step_size assert mock_args['age_params'] == age_params assert mock_args['population_data'].equals(sub_pop) assert mock_args['randomness_streams'] == base_pop.randomness pop = simulation.get_population() for column in pop: assert pop[column].equals(sims[column]) final_ages = pop.age + num_days / utilities.DAYS_PER_YEAR simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() assert np.allclose(pop.age, final_ages, atol=0.5 / utilities.DAYS_PER_YEAR) # Within a half of a day.
def get_relative_risks(config: Path, input_draw: int, random_seed: int, age_group_id: int) -> pd.DataFrame: sim = InteractiveContext(config, setup=False) artifact_path = sim.configuration.input_data.artifact_path artifact = Artifact(artifact_path) age_bins = artifact.load(data_keys.POPULATION.AGE_BINS).reset_index().set_index('age_group_id') age_start = age_bins.loc[age_group_id, 'age_start'] age_end = age_bins.loc[age_group_id, 'age_end'] year_start = 2019 year_end = 2020 sim.configuration.update({ 'input_data': { 'input_draw_number': input_draw, }, 'randomness': { 'random_seed': random_seed, }, 'population': { 'age_start': age_start, 'age_end': age_end } }) sim.setup() pop = sim.get_population() gestational_ages = sim.get_value('short_gestation.exposure')(pop.index) birth_weights = sim.get_value('low_birth_weight.exposure')(pop.index) interpolators = artifact.load(data_keys.LBWSG.RELATIVE_RISK_INTERPOLATOR) def calculate_rr_by_sex(sex: str) -> float: sex_mask = pop['sex'] == sex row_index = (sex, age_start, age_end, year_start, year_end, 'diarrheal_diseases', 'excess_mortality_rate') interpolator = pickle.loads(bytes.fromhex( interpolators.loc[row_index, f'draw_{input_draw}'] )) rrs = np.exp(interpolator(gestational_ages[sex_mask], birth_weights[sex_mask], grid=False)) return rrs lbwsg_rrs = pd.DataFrame({'relative_risk': 1.0}, index=pop.index) lbwsg_rrs['sex'] = pop['sex'] lbwsg_rrs.loc[lbwsg_rrs['sex'] == 'Female', 'relative_risk'] = calculate_rr_by_sex('Female') lbwsg_rrs.loc[lbwsg_rrs['sex'] == 'Male', 'relative_risk'] = calculate_rr_by_sex('Male') return lbwsg_rrs
def test_FertilityCrudeBirthRate(config, base_plugins): pop_size = config.population.population_size num_days = 100 components = [TestPopulation(), FertilityCrudeBirthRate()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins, setup=False) simulation._data.write("covariate.live_births_by_sex.estimate", crude_birth_rate_data()) simulation.setup() simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() assert np.all(pop.alive == 'alive') assert len(pop.age) > pop_size
def test_prevalence_birth_prevalence_initial_assignment( base_config, base_plugins, disease): healthy = BaseDiseaseState('healthy') data_funcs = { 'prevalence': lambda _, __: 1, 'birth_prevalence': lambda _, __: 0.5, 'disability_weight': lambda _, __: 0 } with_condition = DiseaseState('with_condition', get_data_functions=data_funcs) model = DiseaseModel(disease, initial_state=healthy, states=[healthy, with_condition]) base_config.update( { 'population': { 'population_size': 1000, 'age_start': 0, 'age_end': 5 } }, **metadata(__file__)) simulation = InteractiveContext(components=[TestPopulation(), model], configuration=base_config, plugin_configuration=base_plugins) # prevalence should be used for assigning initial status at sim start assert np.isclose(get_test_prevalence(simulation, "with_condition"), 1) # birth prevalence should be used for assigning initial status to newly-borns on time steps simulation._clock.step_forward() simulation.simulant_creator(1000, population_configuration={ 'age_start': 0, 'age_end': 0, 'sim_state': 'time_step' }) assert np.isclose(get_test_prevalence(simulation, "with_condition"), 0.75, 0.01) # and prevalence should be used for ages not start = end = 0 simulation._clock.step_forward() simulation.simulant_creator(1000, population_configuration={ 'age_start': 0, 'age_end': 5, 'sim_state': 'time_step' }) assert np.isclose(get_test_prevalence(simulation, "with_condition"), 0.83, 0.01)
def test_fertility_module(base_config, base_plugins): start_population_size = 1000 num_days = 1000 time_step = 10 # Days base_config.update({ 'population': { 'population_size': start_population_size, 'age_start': 0, 'age_end': 125}, 'time': {'step_size': time_step} }, layer='override') components = [TestPopulation(), FertilityAgeSpecificRates()] simulation = simulation = InteractiveContext(components=components, configuration=base_config, plugin_configuration=base_plugins, setup=False) asfr_data = build_table(0.05, 1990, 2017).rename(columns={'value': 'mean_value'}) simulation._data.write("covariate.age_specific_fertility_rate.estimate", asfr_data) simulation.setup() time_start = simulation._clock.time assert 'last_birth_time' in simulation.get_population().columns,\ 'expect Fertility module to update state table.' assert 'parent_id' in simulation.get_population().columns, \ 'expect Fertility module to update state table.' simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() # No death in this model. assert np.all(pop.alive == 'alive'), 'expect all simulants to be alive' # TODO: Write a more rigorous test. assert len(pop.age) > start_population_size, 'expect new simulants' for i in range(start_population_size, len(pop)): assert pop.loc[pop.iloc[i].parent_id].last_birth_time >= time_start, 'expect all children to have mothers who' \ ' gave birth after the simulation starts.'
def test_prevalence_single_state_with_migration(base_config, base_plugins, disease, base_data, test_prevalence_level): """ Test the prevalence for the single state over newly migrated population. Start with the initial population, check the prevalence for initial assignment. Then add new simulants and check whether the initial status is properly assigned to new simulants based on the prevalence data and pre-existing simulants status """ year_start = base_config.time.start.year year_end = base_config.time.end.year healthy = BaseDiseaseState('healthy') data_funcs = base_data(test_prevalence_level) data_funcs.update({'disability_weight': lambda _, __: 0.0}) sick = DiseaseState('sick', get_data_functions=data_funcs) model = DiseaseModel(disease, initial_state=healthy, states=[healthy, sick]) base_config.update({'population': { 'population_size': 50000 }}, **metadata(__file__)) simulation = InteractiveContext(components=[TestPopulation(), model], configuration=base_config, plugin_configuration=base_plugins) error_message = "initial status of simulants should be matched to the prevalence data." assert np.isclose(get_test_prevalence(simulation, 'sick'), test_prevalence_level, 0.01), error_message simulation._clock.step_forward() assert np.isclose(get_test_prevalence(simulation, 'sick'), test_prevalence_level, .01), error_message simulation.simulant_creator(50000, population_configuration={ 'age_start': 0, 'age_end': 5, 'sim_state': 'time_step' }) assert np.isclose(get_test_prevalence(simulation, 'sick'), test_prevalence_level, .01), error_message simulation._clock.step_forward() simulation.simulant_creator(50000, population_configuration={ 'age_start': 0, 'age_end': 5, 'sim_state': 'time_step' }) assert np.isclose(get_test_prevalence(simulation, 'sick'), test_prevalence_level, .01), error_message
def test_emigration(config, base_plugins): start_population_size = config.population.population_size num_days = 365 * 10 components = [TestPopulation(), Emigration()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins, setup=False) # setup emigration rates df_emigration = pd.read_csv(config.path_to_emigration_file) df_total_population = pd.read_csv(config.path_to_total_population_file) df_emigration = df_emigration[(df_emigration['LAD.code'] == 'E08000032') | (df_emigration['LAD.code'] == 'E08000032')] df_total_population = df_total_population[ (df_total_population['LAD'] == 'E08000032') | (df_total_population['LAD'] == 'E08000032')] asfr_data_emigration = compute_migration_rates(df_emigration, df_total_population, 2011, 2012, config.population.age_start, config.population.age_end, aggregate_over=75) # Mock emigration Data simulation._data.write("covariate.age_specific_migration_rate.estimate", asfr_data_emigration) simulation.setup() simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() print('emigrated', len(pop[pop['alive'] == 'emigrated'])) print('remaining population', len(pop[pop['emigrated'] == 'no_emigrated'])) assert (np.all(pop.alive == 'alive') == False) assert len(pop[pop['emigrated'] == 'Yes']) > 0, 'expect migration'
def test_Immigration(config, base_plugins): num_days = 10 components = [TestPopulation(), Immigration()] simulation = InteractiveContext(components=components, configuration=config, plugin_configuration=base_plugins, setup=False) df_total_population = pd.read_csv(config.path_to_total_population_file) df_total_population = df_total_population[ (df_total_population['LAD'] == 'E08000032')] # setup immigration rates df_immigration = pd.read_csv(config.path_to_immigration_file) df_immigration = df_immigration[ (df_immigration['LAD.code'] == 'E08000032')] asfr_data_immigration = compute_migration_rates(df_immigration, df_total_population, 2011, 2012, config.population.age_start, config.population.age_end, normalize=False ) # setup immigration rates df_immigration_MSOA = pd.read_csv(config.path_to_immigration_MSOA) # read total immigrants from the file total_immigrants = int(df_immigration[df_immigration.columns[4:]].sum().sum()) simulation._data.write("cause.all_causes.cause_specific_immigration_rate", asfr_data_immigration) simulation._data.write("cause.all_causes.cause_specific_total_immigrants_per_year", total_immigrants) simulation._data.write("cause.all_causes.immigration_to_MSOA", df_immigration_MSOA) simulation.setup() simulation.run_for(duration=pd.Timedelta(days=num_days)) pop = simulation.get_population() assert (len(pop["entrance_time"].value_counts()) > 1) print (pop)