def test_sample_SIRS_seirs_times(): #Pretest Params.clean() SIRS.clean() config_path = os.path.join(os.path.dirname(__file__), "..", "src", "config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__), "..", "src", paramsDataPath), override=True) curr_machine_type = machine_type["SIRS"] p = Person(94) final_states = [] for i in range(1000): table = sample_seir_times(curr_machine_type, p) assert table[0][0] == DiseaseState.LATENT final_states.append(table[-1][0]) cnt_table = Counter(final_states) assert DiseaseState.DECEASED in cnt_table.keys() assert DiseaseState.SUSCEPTIBLE in cnt_table.keys() assert len(cnt_table.keys()) == 2
def test_latent_incubating_critical_deceased(): #Pretest Params.clean() SIRS.clean() config_path = os.path.join( os.path.dirname(__file__), "tests_config_files", "test_latent_incubating_critical_deceased_config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__), "tests_params_files", paramsDataPath), override=True) p = Person(30) for _ in range(100): for cur_type in machine_type: tbl = sample_seir_times(cur_type, p) assert tbl[0][0] == DiseaseState.LATENT, "{}".format(cur_type.name) assert tbl[1][0] == DiseaseState.INCUBATINGPOSTLATENT, "{}".format( cur_type.name) assert tbl[2][ 0] == DiseaseState.SYMPTOMATICINFECTIOUS, "{}".format( cur_type.name) assert tbl[3][0] == DiseaseState.CRITICAL, "{}".format( cur_type.name) assert tbl[4][0] == DiseaseState.DECEASED, "{}".format( cur_type.name)
def infect_and_get_events(self, date, environment, infection_transmitter=None, seir_times=None): """ Infect this person, save his infection data, create and register the events of his state changes. :param date: data of infection data :param environment: where the person got infected :param infection_transmitter: the person who infected this person :param seir_times: The state changes of this person (what they are and how long they last). If this is None, it is sampled with the distribution defined in params.json. :return: infection events """ assert self._disease_state == DiseaseState.SUSCEPTIBLE self.set_disease_state(DiseaseState.LATENT) assert self._infection_data is None, \ "Infecting someone who is already infected" self._infection_data = InfectionData(self, date, environment, infection_transmitter) if seir_times: states_and_times = seir_times else: states_and_times = sample_seir_times(self) return self.gen_and_register_events_from_seir_times( date, states_and_times)
def infect_and_get_events(self, date, environment, infection_transmitter=None, seir_times=None): """ Infect this person, save his infection data, create and register the events of his state changes. :param date: data of infection data :param environment: where the person got infected :param infection_transmitter: the person who infected this person :param seir_times: The state changes of this person (what they are and how long they last). If this is None, it is sampled with the distribution defined in params.json. :return: infection events """ assert self._disease_state == DiseaseState.SUSCEPTIBLE, "person state:" + str( self._disease_state) if seir_times: states_and_times = seir_times elif self._seir_times: states_and_times = self._seir_times else: states_and_times = sample_seir_times(self.state_machine_type, self) #update self._infection_data for state, date_change in states_and_times: if state != DiseaseState.SUSCEPTIBLE: break assert ((self.state_machine_type == machine_type.SIR) and (self._infection_data is None)) or \ (self.state_machine_type == machine_type.SIRS), \ "Infecting someone who is already infected id:{} machine_type:{}".format(str(self.get_id()),self.state_machine_type.name) self.set_disease_state(DiseaseState.LATENT) self._infection_data = InfectionData(self, date + date_change, environment, infection_transmitter) # print("in infect_and_get_events id:{} disease_state:{} states_and_times:{}"\ # .format(self.get_id(),self.get_disease_state(),states_and_times)) #update seir times table self._seir_times = states_and_times return self.gen_and_register_events_from_seir_times( date, states_and_times)
def immune_and_get_events(self, start_date, delta_time, seir_times=None): """ Immune this person, create and register the events of his state changes. :param start_date: date_from which we count the delta_time should be init to INIAL_DATE :param delta_time: time_delta from start_Date of the simulation in which the person will be immuned :param seir_times: The state changes of this person (what they are and how long they last). If this is None, it is sampled with the distribution defined in params.json. :return: tuple first element true if person can be vaccinate second elemnt is infection events """ lastState = self._disease_state assert (self._disease_state == DiseaseState.SUSCEPTIBLE) or (self._disease_state == DiseaseState.LATENT) self.set_disease_state(DiseaseState.LATENT) if seir_times: states_and_times = seir_times elif self._seir_times: states_and_times = self._seir_times else: states_and_times = sample_seir_times(self.state_machine_type, self) #alt_state (alternative state) in case the person won't be immuned alt_state = states_and_times[-1] states_and_times = states_and_times[:-1] #Orgenize the states_and_times dictionaery so that is simulation.current_date == date this person will be immmune # only if he should get immuned i = 0 new_states_and_times = [] cut_in_middle = False zero_days = (delta_time.days == 0) and (states_and_times[0][0] in [ DiseaseState.SUSCEPTIBLE, DiseaseState.LATENT ]) #specify if the person can be vaccinated or we should find a substitute ok = True append_rest_of_table = False if zero_days: new_states_and_times.append( (DiseaseState.IMMUNE, timedelta(days=0))) new_states_and_times.append((DiseaseState.IMMUNE, None)) lastState = DiseaseState.IMMUNE ok = True else: while i < len(states_and_times): if delta_time.days >= states_and_times[i][1].days: new_states_and_times.append(\ (states_and_times[i][0],timedelta(days = states_and_times[i][1].days))) delta_time -= timedelta(days=states_and_times[i][1].days) lastState = states_and_times[i][0] i += 1 if delta_time.days > 0: if lastState in [ DiseaseState.SUSCEPTIBLE, DiseaseState.LATENT ]: new_states_and_times.append( (DiseaseState.IMMUNE, timedelta(delta_time.days))) ok = True else: append_rest_of_table = True ok = False delta_time -= timedelta(delta_time.days) elif delta_time.days == 0: if lastState in [ DiseaseState.SUSCEPTIBLE, DiseaseState.LATENT ]: new_states_and_times.append( (DiseaseState.IMMUNE, timedelta(delta_time.days))) ok = True elif len(new_states_and_times) < len(states_and_times): i -= 1 append_rest_of_table = True ok = False elif delta_time.days < 0: cut_in_middle = True if lastState in [ DiseaseState.SUSCEPTIBLE, DiseaseState.LATENT ]: new_states_and_times[i - 1][0] == DiseaseState.IMMUNE lastState = DiseaseState.IMMUNE ok = True else: #add the rest of the table don't change a thing append_rest_of_table = True ok = False assert delta_time.days >= 0, 'miscalculating days' # if delta_time.days < 0 : # print("i:{} len(states_and_times):{} delta_time.days:{}".format(i,len(states_and_times),delta_time.days)) # print("start_date:{} ,old_delta_time:{} ,delta_time:{}".format(start_date,old_delta_time , delta_time)) # print(states_and_times) # print("-----------------------") if append_rest_of_table: while i < len(states_and_times): new_states_and_times.append( (states_and_times[i][0], timedelta(days=states_and_times[i][1].days))) i += 1 #print("immune_and_get_events id:{} new_states_and_times:{}".format(self.get_id(),new_states_and_times)) #At the End of the table we put immune only if the person was susptible or latent # otherwise he continues his life regularly if (not (zero_days)): if (lastState in [DiseaseState.SUSCEPTIBLE, DiseaseState.LATENT]): new_states_and_times.append((DiseaseState.IMMUNE, None)) ok = True else: new_states_and_times.append(alt_state) ok = False # print(new_states_and_times) #Update person seir_times self._seir_times = new_states_and_times return ok, self.gen_and_register_events_from_seir_times( start_date, new_states_and_times)