def set_occupation_network_table(self, df_occupation_networks, df_occupation_network_properties): n_total = len(df_occupation_networks.index) if n_total != self.get_param("n_total"): raise ParameterException( "df_occupation_networks must have n_total rows") n_networks = df_occupation_networks['network_no'].max() + 1 covid19.set_occupation_network_table(self.c_params, int(n_total), int(n_networks)) [ covid19.set_indiv_occupation_network_property( self.c_params, int(row[0]), int(row[1]), float(row[2]), float(row[3]), int(row[4]), str(row[5])) for row in df_occupation_network_properties[[ 'network_no', 'age_type', 'mean_work_interaction', 'lockdown_multiplier', 'network_id', 'network_name' ]].values ] ID = df_occupation_networks['ID'].to_list() network_no = df_occupation_networks['network_no'].to_list() ID_c = covid19.longArray(n_total) network_no_c = covid19.longArray(n_total) for idx in range(n_total): ID_c[idx] = ID[idx] network_no_c[idx] = network_no[idx] covid19.set_indiv_occupation_network(self.c_params, n_total, ID_c, network_no_c)
def set_app_users(self,df_app_users): if {'ID', 'app_user'}.issubset(df_app_users.columns) == False: raise ModelParameterException( "df_app_user must contain the columns ID and app_user") # first turn on the users in the list app_on = df_app_users[df_app_users["app_user"]==True]["ID"].to_list() n_users = len(app_on) if n_users > 0 : users = covid19.longArray(n_users) for idx in range(n_users): users[idx]=app_on[idx] res = covid19.set_app_users(self.c_model,users,n_users,True) if res == False : raise ModelParameterException( "Failed to update new app_users" ) # first turn off the users in the list app_off = df_app_users[df_app_users["app_user"]==False]["ID"].to_list() n_users = len(app_off) if n_users > 0 : users = covid19.longArray(n_users) for idx in range(n_users): users[idx]=app_off[idx] res = covid19.set_app_users(self.c_model,users,n_users,False) if res == False : raise ModelParameterException( "Failed to remove old app_users" )
def set_demographic_household_table(self, df_demo_house): n_total = len(df_demo_house.index) if n_total != self.get_param("n_total"): raise ParameterException("df_demo_house must have n_total rows") if not 'ID' in df_demo_house.columns: raise ParameterException("df_demo_house must have column ID") if not 'age_group' in df_demo_house.columns: raise ParameterException( "df_demo_house must have column age_group") if not 'house_no' in df_demo_house.columns: raise ParameterException("df_demo_house must have column house_no") n_households = df_demo_house['house_no'].max() + 1 ID = df_demo_house["ID"].to_list() ages = df_demo_house["age_group"].to_list() house_no = df_demo_house["house_no"].to_list() ID_c = covid19.longArray(n_total) ages_c = covid19.longArray(n_total) house_no_c = covid19.longArray(n_total) for idx in range(n_total): ID_c[idx] = ID[idx] ages_c[idx] = ages[idx] house_no_c[idx] = house_no[idx] covid19.set_demographic_house_table(self.c_params, int(n_total), int(n_households), ID_c, ages_c, house_no_c)
def add_user_network(self, df_network, interaction_type=1, skip_hospitalised=True, skip_quarantine=True, daily_fraction=1.0, name="user_network"): n_edges = len(df_network.index) n_total = self._params_obj.get_param("n_total") if not 'ID_1' in df_network.columns: raise ParameterException("df_network must have column ID_1") if not 'ID_1' in df_network.columns: raise ParameterException("df_network must have column ID_1") if not interaction_type in [0, 1, 2]: raise ParameterException( "interaction_type must be 0 (household), 1 (occupation) or 2 (random)" ) if (daily_fraction > 1) or (daily_fraction < 0): raise ParameterException( "daily fraction must be in the range 0 to 1") if not skip_hospitalised in [True, False]: raise ParameterException("skip_hospitalised must be True or False") if not skip_quarantine in [True, False]: raise ParameterException("skip_quarantine must be True or False") ID_1 = df_network["ID_1"].to_list() ID_2 = df_network["ID_2"].to_list() if (max(ID_1) >= n_total) or (min(ID_1) < 0): raise ParameterException( "all values of ID_1 must be between 0 and n_total-1") if (max(ID_2) >= n_total) or (min(ID_2) < 0): raise ParameterException( "all values of ID_2 must be between 0 and n_total-1") ID_1_c = covid19.longArray(n_edges) ID_2_c = covid19.longArray(n_edges) for idx in range(n_edges): ID_1_c[idx] = ID_1[idx] ID_2_c[idx] = ID_2[idx] covid19.add_user_network(self.c_model, interaction_type, skip_hospitalised, skip_quarantine, daily_fraction, n_edges, ID_1_c, ID_2_c, name)
def __init__( self, frac_0_9 = 0, frac_10_19 = 0, frac_20_29 = 0, frac_30_39 = 0, frac_40_49 = 0, frac_50_59 = 0, frac_60_69 = 0, frac_70_79 = 0, frac_80 = 0, vaccine_type = 0, efficacy = 1.0, time_to_protect = 15, vaccine_protection_period = 365 ): fraction_to_vaccinate = [ frac_0_9, frac_10_19, frac_20_29, frac_30_39, frac_40_49, frac_50_59, frac_60_69, frac_70_79, frac_80, ] self.c_fraction_to_vaccinate = covid19.doubleArray( len(AgeGroupEnum) ) for age in AgeGroupEnum: self.c_fraction_to_vaccinate[ age.value ] = fraction_to_vaccinate[ age.value ] self.vaccine_type = vaccine_type self.efficacy = efficacy self.time_to_protect = time_to_protect self.vaccine_protection_period = vaccine_protection_period self.c_total_vaccinated = covid19.longArray( len(AgeGroupEnum) ) for age in AgeGroupEnum: self.c_total_vaccinated[ age.value ] = 0
def create_c_array(array, ctype): """ Create a C version of a numpy array or Python list array : list or numpy array Array for which a C representation is wanted Returns ------- array_c : C array to which the Python array is to be copied """ N = len(array) if ctype == "double": array_c = covid19.doubleArray(N) elif ctype == "long": array_c = covid19.longArray(N) elif ctype == "int": array_c = covid19.intArray(N) else: print("Error: unknown data type") return for i in range(N): array_c[i] = array[i] return (array_c)
def __init__( self, frac_0_9 = 0, frac_10_19 = 0, frac_20_29 = 0, frac_30_39 = 0, frac_40_49 = 0, frac_50_59 = 0, frac_60_69 = 0, frac_70_79 = 0, frac_80 = 0, vaccine = -1, ): fraction_to_vaccinate = [ frac_0_9, frac_10_19, frac_20_29, frac_30_39, frac_40_49, frac_50_59, frac_60_69, frac_70_79, frac_80, ] self.c_fraction_to_vaccinate = covid19.doubleArray( len(AgeGroupEnum) ) for age in AgeGroupEnum: self.c_fraction_to_vaccinate[ age.value ] = fraction_to_vaccinate[ age.value ] if not isinstance( vaccine, Vaccine ) : ModelException( "argument vaccine must be an object of type Vaccine, add one using model.add_vaccine()") self.vaccine = vaccine self.c_total_vaccinated = covid19.longArray( len(AgeGroupEnum) ) for age in AgeGroupEnum: self.c_total_vaccinated[ age.value ] = 0
def get_individuals(self): """ Return dataframe of population """ n_total = self.c_model.params.n_total ids = covid19.longArray(n_total) statuses = covid19.intArray(n_total) age_groups = covid19.intArray(n_total) occupation_networks = covid19.intArray(n_total) house_ids = covid19.longArray(n_total) infection_counts = covid19.intArray(n_total) vaccine_statuses = covid19.shortArray(n_total) n_total = covid19.get_individuals(self.c_model, ids, statuses, age_groups, occupation_networks, house_ids, infection_counts, vaccine_statuses) list_ids = [None] * n_total list_statuses = [None] * n_total list_age_groups = [None] * n_total list_occupation_networks = [None] * n_total list_house_ids = [None] * n_total list_infection_counts = [None] * n_total list_vaccine_statuses = [None] * n_total for idx in range(n_total): list_ids[idx] = ids[idx] list_statuses[idx] = statuses[idx] list_age_groups[idx] = age_groups[idx] list_occupation_networks[idx] = occupation_networks[idx] list_house_ids[idx] = house_ids[idx] list_infection_counts[idx] = infection_counts[idx] list_vaccine_statuses[idx] = vaccine_statuses[idx] df_popn = pd.DataFrame({ 'ID': list_ids, 'current_status': list_statuses, 'age_group': list_age_groups, 'occupation_network': list_occupation_networks, 'house_no': list_house_ids, 'infection_count': list_infection_counts, 'vaccine_status': list_vaccine_statuses }) return df_popn
def get_network(self): """Return pandas.DataFrame of the network""" n_edges = self.n_edges() id1 = covid19.longArray(n_edges) id2 = covid19.longArray(n_edges) return_status = covid19.get_network(self.c_network, id1, id2) list_id1 = [None] * n_edges list_id2 = [None] * n_edges for idx in range(n_edges): list_id1[idx] = id1[idx] list_id2[idx] = id2[idx] df_network = pd.DataFrame({'ID1': list_id1, 'ID2': list_id2}) return df_network
def add_user_network_random(self, df_interactions, skip_hospitalised=True, skip_quarantine=True, name="user_network"): """[summary] adds a bespoke user random network from a dataframe of people and number of interactions the network is regenerates each day, but the number of interactions per person is statitc hospitalsed and quarantined people can be skipped Arguments: df_interactions {[dataframe]} -- [list of indviduals and interactions, with 2 columns ID and N] skip_hospitalised {[boolean]} -- [skip interaction if either person is in hospital] skip_quarantine{[boolean]} -- [skip interaction if either person is in quarantined] name{[char]} -- [the name of the network] """ n_indiv = len(df_interactions.index) n_total = self._params_obj.get_param("n_total") if not 'ID' in df_interactions.columns: raise ParameterException("df_interactions must have column ID") if not 'N' in df_interactions.columns: raise ParameterException("df must have column N") if not skip_hospitalised in [True, False]: raise ParameterException("skip_hospitalised must be True or False") if not skip_quarantine in [True, False]: raise ParameterException("skip_quarantine must be True or False") ID = df_interactions["ID"].to_list() N = df_interactions["N"].to_list() if (max(ID) >= n_total) or (min(ID) < 0): raise ParameterException( "all values of ID must be between 0 and n_total-1") if (min(N) < 1): raise ParameterException("all values of N must be greater than 0") ID_c = covid19.longArray(n_indiv) N_c = covid19.intArray(n_indiv) for idx in range(n_indiv): ID_c[idx] = ID[idx] N_c[idx] = N[idx] id = covid19.add_user_network_random(self.c_model, skip_hospitalised, skip_quarantine, n_indiv, ID_c, N_c, name) return Network(self, id)
def get_app_users(self): n_total = self.c_model.params.n_total users = covid19.longArray(n_total) res = covid19.get_app_users(self.c_model,users) if res == 0: raise ModelParameterException( "Failed to get risk") list_users = [None]*n_total for idx in range(n_total): list_users[idx]=users[idx] df_users = pd.DataFrame( {'ID':range(n_total), 'app_user':list_users}) return df_users
def add_user_network( self, df_network, interaction_type = covid19.OCCUPATION, skip_hospitalised = True, skip_quarantine = True, construction = covid19.NETWORK_CONSTRUCTION_BESPOKE, daily_fraction = 1.0, name = "user_network" ): """[summary] adds as bespoke user network from a dataframe of edges the network is static with the exception of skipping hospitalised and quarantined people Arguments: df_network {[dataframe]} -- [list of edges, with 2 columns ID_1 and ID_2] interaction {[int]} -- [type of interaction (e.g. household/occupation/random)] skip_hospitalised {[boolean]} -- [skip interaction if either person is in hospital] skip_quarantine{[boolean]} -- [skip interaction if either person is in quarantined] construction{[int]} -- [the method used for network construction] daily_fraction{[double]} -- [the fraction of edges on the network present each day (i.e. down-sampling the network)] name{[char]} -- [the name of the network] """ n_edges = len( df_network.index ) n_total = self._params_obj.get_param("n_total") if not 'ID_1' in df_network.columns: raise ParameterException( "df_network must have column ID_1" ) if not 'ID_2' in df_network.columns: raise ParameterException( "df_network must have column ID_2" ) if not interaction_type in [0,1,2]: raise ParameterException( "interaction_type must be 0 (household), 1 (occupation) or 2 (random)" ) if (daily_fraction > 1) or( daily_fraction < 0): raise ParameterException( "daily fraction must be in the range 0 to 1" ) if not skip_hospitalised in [ True, False ]: raise ParameterException( "skip_hospitalised must be True or False" ) if not skip_quarantine in [ True, False ]: raise ParameterException( "skip_quarantine must be True or False" ) ID_1 = df_network[ "ID_1" ].to_list() ID_2 = df_network[ "ID_2" ].to_list() if (max( ID_1 ) >= n_total) or (min( ID_1 ) < 0): raise ParameterException( "all values of ID_1 must be between 0 and n_total-1" ) if (max( ID_2 ) >= n_total) or (min( ID_2 ) < 0): raise ParameterException( "all values of ID_2 must be between 0 and n_total-1" ) ID_1_c = covid19.longArray(n_edges) ID_2_c = covid19.longArray(n_edges) for idx in range(n_edges): ID_1_c[idx] = ID_1[idx] ID_2_c[idx] = ID_2[idx] id = covid19.add_user_network(self.c_model,interaction_type,skip_hospitalised,skip_quarantine,construction,daily_fraction, n_edges,ID_1_c, ID_2_c, name) return Network( self, id )