def SIS(cause: str) -> DiseaseModel: healthy = SusceptibleState(cause) infected = DiseaseState(cause) healthy.allow_self_transitions() healthy.add_transition(infected, source_data_type='rate') infected.allow_self_transitions() infected.add_transition(healthy, source_data_type='rate') return DiseaseModel(cause, states=[healthy, infected])
def SIS_fixed_duration(cause: str, duration: str) -> DiseaseModel: duration = pd.Timedelta(days=float(duration) // 1, hours=(float(duration) % 1) * 24.0) healthy = SusceptibleState(cause) infected = DiseaseState(cause, get_data_functions={'dwell_time': lambda _, __: duration}) healthy.allow_self_transitions() healthy.add_transition(infected, source_data_type='rate') infected.add_transition(healthy) infected.allow_self_transitions() return DiseaseModel(cause, states=[healthy, infected])
def NeonatalSIS(cause): with_condition_data_functions = {'birth_prevalence': lambda cause, builder: builder.data.load(f"cause.{cause}.birth_prevalence")} healthy = SusceptibleState(cause) with_condition = DiseaseState(cause, get_data_functions=with_condition_data_functions) healthy.allow_self_transitions() healthy.add_transition(with_condition, source_data_type='rate') with_condition.allow_self_transitions() with_condition.add_transition(healthy, source_data_type='rate') return DiseaseModel(cause, states=[healthy, with_condition])
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)
def Stroke(stroke_name): stroke_types = [ 'ischemic_stroke', 'subarachnoid_hemorrhage', 'intracerebral_hemorrhage' ] if stroke_name not in stroke_types: raise ValueError( f'Stroke name must be one of {stroke_types}. You supplied {stroke_name}' ) susceptible = SusceptibleState(stroke_name) data_funcs = {'dwell_time': lambda *args: pd.Timedelta(days=28)} acute = DiseaseState(f'acute_{stroke_name}', cause_type='sequela', get_data_functions=data_funcs) #data_funcs = {'dwell_time': lambda *args: pd.Timedelta(days=365.25)} post = RelapseState(f'post_{stroke_name}', cause_type='sequela') susceptible.allow_self_transitions() data_funcs = { 'incidence_rate': lambda _, builder: builder.data.load( f'cause.{stroke_name}.incidence_rate') } susceptible.add_transition(acute, source_data_type='rate', get_data_functions=data_funcs) acute.allow_self_transitions() acute.add_transition(post) post.allow_self_transitions() data_funcs = { 'relapse_rate': lambda _, builder: builder.data.load( f'cause.{stroke_name}.incidence_rate') } post.add_transition(acute, source_data_type='rate', get_data_functions=data_funcs) #post.add_transition(susceptible, source_data_type='time') return DiseaseModel(stroke_name, states=[susceptible, acute, post])
def IschemicHeartDisease(): susceptible = SusceptibleState('ischemic_heart_disease') data_funcs = {'dwell_time': lambda *args: pd.Timedelta(days=28)} acute_mi = DiseaseState('acute_myocardial_infarction', cause_type='sequela', get_data_functions=data_funcs) #data_funcs = {'dwell_time': lambda *args: pd.Timedelta(days=365.25)} post_mi = RelapseState( 'post_myocardial_infarction', cause_type='sequela', ) susceptible.allow_self_transitions() data_funcs = { 'incidence_rate': lambda _, builder: builder.data.load( 'cause.ischemic_heart_disease.incidence_rate') } susceptible.add_transition(acute_mi, source_data_type='rate', get_data_functions=data_funcs) acute_mi.allow_self_transitions() acute_mi.add_transition(post_mi) post_mi.allow_self_transitions() data_funcs = { 'relapse_rate': lambda _, builder: builder.data.load( 'cause.ischemic_heart_disease.incidence_rate') } post_mi.add_transition(acute_mi, source_data_type='rate', get_data_functions=data_funcs) #post_mi.add_transition(susceptible, source_data_type='time') return DiseaseModel('ischemic_heart_disease', states=[susceptible, acute_mi, post_mi])
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())