Пример #1
0
def test_parse_artifact_path_config(base_config, test_data_dir):
    artifact_path = test_data_dir / 'artifact.hdf'
    base_config.update({'input_data': {
        'artifact_path': str(artifact_path)
    }}, **metadata(str(Path('/'))))

    assert parse_artifact_path_config(base_config) == str(artifact_path)
Пример #2
0
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
Пример #3
0
def test_parse_artifact_path_relative(base_config, test_data_dir):
    base_config.update(
        {'input_data': {
            'artifact_path': '../../test_data/artifact.hdf'
        }}, **metadata(__file__))
    assert parse_artifact_path_config(base_config) == str(test_data_dir /
                                                          'artifact.hdf')
Пример #4
0
def test_parse_artifact_path_config_fail_relative(base_config):
    base_config.update(
        {'input_data': {
            'artifact_path': './not_an_artifact.hdf'
        }}, **metadata(__file__))

    with pytest.raises(FileNotFoundError):
        parse_artifact_path_config(base_config)
Пример #5
0
def test_parse_artifact_path_config_fail(base_config):
    artifact_path = Path(__file__).parent / 'not_an_artifact.hdf'
    base_config.update({'input_data': {
        'artifact_path': str(artifact_path)
    }}, **metadata(str(Path('/'))))

    with pytest.raises(FileNotFoundError):
        parse_artifact_path_config(base_config)
Пример #6
0
def test_get_age_bins(builder, base_config, age_start, exit_age,
                      result_age_end_values, result_age_start_values):
    base_config.update(
        {'population': {
            'age_start': age_start,
            'exit_age': exit_age
        }}, **metadata(__file__))
    builder.configuration = base_config
    df = get_age_bins(builder)
    assert set(df.age_end) == result_age_end_values
    assert set(df.age_start) == result_age_start_values
Пример #7
0
def test_dwell_time(assign_cause_mock, base_config, base_plugins, disease,
                    base_data):

    time_step = 10
    assign_cause_mock.side_effect = lambda population, *args: pd.DataFrame(
        {'condition_state': 'healthy'}, index=population.index)

    base_config.update(
        {
            'time': {
                'step_size': time_step
            },
            'population': {
                'population_size': 10
            }
        }, **metadata(__file__))

    healthy_state = BaseDiseaseState('healthy')
    data_function = base_data(0)
    data_function['dwell_time'] = lambda _, __: pd.Timedelta(days=28)
    data_function['disability_weight'] = lambda _, __: 0.0
    event_state = DiseaseState('event', get_data_functions=data_function)
    done_state = BaseDiseaseState('sick')

    healthy_state.add_transition(event_state)
    event_state.add_transition(done_state)

    model = DiseaseModel(disease,
                         initial_state=healthy_state,
                         states=[healthy_state, event_state, done_state])

    simulation = InteractiveContext(components=[TestPopulation(), model],
                                    configuration=base_config,
                                    plugin_configuration=base_plugins)

    # Move everyone into the event state
    simulation.step()
    event_time = simulation._clock.time
    assert np.all(simulation.get_population()[disease] == 'event')

    simulation.step()
    simulation.step()
    # Not enough time has passed for people to move out of the event state, so they should all still be there
    assert np.all(simulation.get_population()[disease] == 'event')

    simulation.step()
    # Now enough time has passed so people should transition away
    assert np.all(simulation.get_population()[disease] == 'sick')
    assert np.all(simulation.get_population().event_event_time ==
                  pd.to_datetime(event_time))
    assert np.all(simulation.get_population().event_event_count == 1)
Пример #8
0
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)
Пример #9
0
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
Пример #10
0
def base_config():
    config = build_simulation_configuration()
    config.update(
        {
            'time': {
                'start': {
                    'year': 1990,
                },
                'end': {
                    'year': 2010
                },
                'step_size': 30.5
            }
        }, **metadata(__file__))
    return config
Пример #11
0
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
Пример #12
0
def test_dwell_time_with_mortality(base_config, base_plugins, disease):
    year_start = base_config.time.start.year
    year_end = base_config.time.end.year

    time_step = 10
    pop_size = 100
    base_config.update(
        {
            'time': {
                'step_size': time_step
            },
            'population': {
                'population_size': pop_size
            }
        }, **metadata(__file__))
    healthy_state = BaseDiseaseState('healthy')

    mort_get_data_funcs = {
        'dwell_time':
        lambda _, __: pd.Timedelta(days=14),
        'excess_mortality_rate':
        lambda _, __: build_table(0.7, year_start - 1, year_end),
        'disability_weight':
        lambda _, __: 0.0
    }

    mortality_state = DiseaseState('event',
                                   get_data_functions=mort_get_data_funcs)
    done_state = BaseDiseaseState('sick')

    healthy_state.add_transition(mortality_state)
    mortality_state.add_transition(done_state)

    model = DiseaseModel(disease,
                         initial_state=healthy_state,
                         states=[healthy_state, mortality_state, done_state])
    mortality = Mortality()
    simulation = InteractiveContext(
        components=[TestPopulation(), model, mortality],
        configuration=base_config,
        plugin_configuration=base_plugins)

    # Move everyone into the event state
    simulation.step()
    assert np.all(simulation.get_population()[disease] == 'event')

    simulation.step()
    # Not enough time has passed for people to move out of the event state, so they should all still be there
    assert np.all(simulation.get_population()[disease] == 'event')

    simulation.step()

    # Make sure some people have died and remained in event state
    assert (simulation.get_population()['alive'] == 'alive').sum() < pop_size

    assert ((simulation.get_population()['alive'] == 'dead').sum() == (
        simulation.get_population()[disease] == 'event').sum())

    # enough time has passed so living people should transition away to sick
    assert ((simulation.get_population()['alive'] == 'alive').sum() == (
        simulation.get_population()[disease] == 'sick').sum())