How to use bezoek method in SeleniumBase

Best Python code snippet using SeleniumBase

google_loader.py

Source:google_loader.py Github

copy

Full Screen

1import pandas as pd2import mezuro_preprocessing as mp3import math4import copy5import datetime as dt6'''7Reduce normal mobility by a factor based on Google mobility data8Input: Mezuro mobility dataframe, historical days ago on which the prediction should be based9Output: Mezuro mobility dataframe where mobility is reduced by prediction of the Google rate10Warning: both functions take quite long to execute (preprocess: approx. 20s per date; apply: approx. 2 min. per date)11'''12#Load Google data13google_mobility=pd.read_csv(14 'data/2020_NL_Region_Mobility_Report.csv'15 ).rename(columns={'retail_and_recreation_percent_change_from_baseline': 'retail',16 'grocery_and_pharmacy_percent_change_from_baseline': 'grocery',17 'parks_percent_change_from_baseline': 'parks',18 'transit_stations_percent_change_from_baseline': 'transit',19 'workplaces_percent_change_from_baseline': 'workplaces',20 'residential_percent_change_from_baseline': 'residential'})21#Re-format province and municipality names to match Mezuro records22google_mobility["sub_region_1"] = google_mobility["sub_region_1"].replace(23 ['North Holland','South Holland','North Brabant'],24 ['Noord-Holland','Zuid-Holland','Noord-Brabant']25 )26google_mobility["sub_region_2"] = google_mobility["sub_region_2"].replace(27 ['Government of Rotterdam','Government of Amsterdam','s-Hertogenbosch','The Hague','Eijsden','Reeuwijk','Flushing'],28 ['Rotterdam','Amsterdam',"'s-Hertogenbosch","'s-Gravenhage",'Eijsden-Margraten','Bodegraven-Reeuwijk','Vlissingen']29 )30google_mobility.loc[(google_mobility["sub_region_1"] == 'Limburg') & (google_mobility["sub_region_2"] == 'Bergen'), ("sub_region_2")] = 'Bergen (L.)'31google_mobility.loc[(google_mobility["sub_region_1"] == 'Noord-Holland')& (google_mobility["sub_region_2"] == 'Bergen'), ("sub_region_2")] = 'Bergen (NH.)'32# The gemeente naming that must be used in the outputed file33year_gemeenten_used = '2020'34def mezuro_convert_to_date(mobility,y = year_gemeenten_used):35 36 mobility = mobility.reset_index()37 mobility_overig = mobility.loc[mobility['woon'] == 'Overige gebieden']38 mobility_known = mobility.loc[mobility['woon'] != 'Overige gebieden']39 mobility_known['woon'] = mobility_known['woon'].map(lambda name: mp.koppeltabel.loc[name,'rivm'+y])40 mobility_known['bezoek'] = mobility_known['bezoek'].map(lambda name: mp.koppeltabel.loc[name,'rivm'+y])41 mobility_overig['bezoek'] = mobility_overig['bezoek'].map(lambda name: mp.koppeltabel.loc[name,'rivm'+y])42 mobility = mobility_known.append(mobility_overig)43 '''44 for index, row in mp.gemeente2gemeente.iterrows():45 print(index)46 if index[0] == 'Overige gebieden':47 mobility.rename(index={index: (index[0] ,mp.koppeltabel.loc[index[1],'rivm'+y] ,index[2]) })48 else:49 mobility.rename(index={index: (mp.koppeltabel.loc[index[0],'rivm'+y] ,mp.koppeltabel.loc[index[1],'rivm'+y] ,index[2]) })50 mobility = mobility.reset_index51 mobility = mobility[mobility['woon'] != mobility['bezoek']]52 '''53 mobility = mobility.set_index(['woon','bezoek','datum'])54 mobility = mobility.groupby(['woon','bezoek','datum']).agg({'bezoek_gemeente_id':'mean','woon_gemeente_id':'mean', 'totaal_aantal_bezoekers':'sum', 'incidentele_bezoeker':'sum', 'regelmatige_bezoeker':'sum', 'frequente_bezoeker':'sum' })55 return mobility56#mezuro_updated = mezuro_convert_to_date(mp.gemeente2gemeente)57def preprocess_rates(start_date,end_date):58 #Transform start and today date into datetime object:59 date_format = '%d-%m-%Y'60 start_date_number = dt.datetime.strptime(start_date,date_format)61 end_date_number = dt.datetime.strptime(end_date,date_format)62 63 #Create empty to-be-filled dataframes64 Reduction_rates_google = pd.DataFrame(columns = ['country_region_code', 65 'country_region', 66 'sub_region_1', 67 'sub_region_2', 68 'metro_area', 69 'iso_3166_2_code', 70 'census_fips_code', 71 'date',72 'retail', 73 'grocery', 74 'parks', 75 'transit',76 'workplaces',77 'residential'])78 79 Reduction_rates_google_prov = pd.DataFrame(columns = ['country_region_code', 80 'country_region', 81 'sub_region_1', 82 'sub_region_2', 83 'metro_area', 84 'iso_3166_2_code', 85 'census_fips_code', 86 'date',87 'retail', 88 'grocery', 89 'parks', 90 'transit',91 'workplaces',92 'residential'])93 94 95 for t in range(1, (end_date_number - start_date_number).days + 2):96 #Calculate new date97 day_difference = dt.timedelta(days = t-1)98 current_date_number = start_date_number + day_difference99 current_date = current_date_number.strftime('%d-%m-%Y')100 current_date_google_format = current_date_number.strftime('%Y-%m-%d')101 #Extract part of Google dataframe corresponding to the current date (first only municipalities, then only provinces)102 google_mob2 = google_mobility.loc[google_mobility["sub_region_2"].notnull()].groupby('date').get_group(current_date_google_format)103 google_mob_prov = google_mobility.loc[(google_mobility["sub_region_2"].isnull()) & (google_mobility["sub_region_1"].notnull())].groupby('date').get_group(current_date_google_format)104 row_NL = google_mobility.loc[(google_mobility["sub_region_1"].isnull()) & (google_mobility["date"] == current_date_google_format) & google_mobility["sub_region_2"].isnull()] 105 106 print(current_date)107 108 #Fill up missing values in the province data109 for index,row in google_mob_prov.iterrows():110 google_mob_prov.at[index,'date'] = current_date111 for col in ['retail','grocery','parks','transit','workplaces','residential']:112 if math.isnan(row[col]):113 reduction_value = row_NL[col]114 google_mob_prov.at[index,col] = reduction_value115 116 #Fill up missing values in the municipality data117 for index,row in google_mob2.iterrows():118 row_province = google_mobility.loc[(google_mobility["sub_region_1"] == row["sub_region_1"]) & (google_mobility["date"] == current_date_google_format) & google_mobility["sub_region_2"].isnull()]119 google_mob2.at[index,'date'] = current_date120 for col in ['retail','grocery','parks','transit','workplaces','residential']:121 if math.isnan(row[col]):122 reduction_value = row_province[col]123 if math.isnan(reduction_value):124 reduction_value = row_NL[col]125 google_mob2.at[index,col] = reduction_value126 127 #Add filled-up data frames128 Reduction_rates_google = Reduction_rates_google.append(google_mob2)129 Reduction_rates_google_prov = Reduction_rates_google_prov.append(google_mob_prov)130 131 #Include municipalities that do not have an entry in the Google data for the current date132 for index, row in mp.koppeltabel.iterrows():133 if len(Reduction_rates_google.loc[(Reduction_rates_google["sub_region_2"] == row["rivm"+year_gemeenten_used]) & (Reduction_rates_google["date"] == current_date)]) == 0:134 province = mp.gemeente_shapes_old.at[index,'prov_name']135 df_help_add = copy.copy(Reduction_rates_google_prov.groupby("sub_region_1").get_group(province).groupby("date").get_group(current_date))136 df_help_add["sub_region_2"] = df_help_add["sub_region_2"].fillna(row["rivm"+year_gemeenten_used])137 Reduction_rates_google = Reduction_rates_google.append(df_help_add)138 139 print(len(Reduction_rates_google))140 #Include missing municipalities from Mezuro data:141 #First: get list of municipalities in Google data:142 df_help_gemeentes = Reduction_rates_google.groupby('date').get_group(end_date) 143 for index, row in mp.koppeltabel.iterrows():144 if len(Reduction_rates_google.loc[Reduction_rates_google["sub_region_2"] == row['rivm'+year_gemeenten_used]]) == 0:145 province = mp.gemeente_shapes.at[index,'prov_name']146 df_help_add = copy.copy(Reduction_rates_google_prov.groupby("sub_region_1").get_group(province))147 df_help_add["sub_region_2"] = df_help_add["sub_region_2"].fillna(row['rivm'+year_gemeenten_used])148 Reduction_rates_google = Reduction_rates_google.append(df_help_add)149 #Note: removal of rows might not be as handy with regard to merging muniipalities150 print(len(Reduction_rates_google)) 151 #Remove rows if municipality in Google data is not in rivm data:152 for index,row in df_help_gemeentes.iterrows():153 if len(mp.koppeltabel.loc[mp.koppeltabel['rivm'+year_gemeenten_used] == row["sub_region_2"]]) == 0:154 Reduction_rates_google = Reduction_rates_google[Reduction_rates_google["sub_region_2"] != row['sub_region_2']]155 print(len(Reduction_rates_google))156 return Reduction_rates_google, Reduction_rates_google_prov157 #Apply the preprocessed Google mobility rates to the Mezuro data to obtain an estimation of the actual mobility158def apply_rates(start_date,end_date,Reduction_rates_march,Reduction_rates):159 date_format = '%d-%m-%Y'160 start_date_number = dt.datetime.strptime(start_date,date_format)161 end_date_number = dt.datetime.strptime(end_date,date_format)162 163 #Create new mobility file based on data from March 1-14164 165 #First: determine which days are which:166 list_day_2019 = [['04-03-2019','11-03-2019'],167 ['05-03-2019','12-03-2019'],168 ['06-03-2019','13-03-2019'],169 ['07-03-2019','14-03-2019'],170 ['01-03-2019','08-03-2019'],171 ['02-03-2019','09-03-2019'],172 ['03-03-2019','10-03-2019']173 ]174 175 list_day_2020 = [['02-03-2020','09-03-2020'],176 ['03-03-2020','10-03-2020'],177 ['04-03-2020','11-03-2020'],178 ['05-03-2020','12-03-2020'],179 ['06-03-2020','13-03-2020'],180 ['07-03-2020','14-03-2020'],181 ['01-03-2020','08-03-2020']182 ]183 184 df_mobility = pd.DataFrame(columns = ['woon',185 'bezoek',186 'datum',187 'bezoek_gemeente_id',188 'woon_gemeente_id',189 'totaal_aantal_bezoekers',190 'incidentele_bezoeker',191 'regelmatige_bezoeker',192 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek','datum'])193 194 for t in range(0,(end_date_number - start_date_number).days + 1):195 day_difference = dt.timedelta(days = t)196 current_date_number = start_date_number + day_difference197 current_date = current_date_number.strftime('%d-%m-%Y')198 199 print(current_date)200 201 weekday = current_date_number.weekday()202 df_help_collect = pd.DataFrame(columns = ['woon',203 'bezoek',204 'datum',205 'bezoek_gemeente_id',206 'woon_gemeente_id',207 'totaal_aantal_bezoekers',208 'incidentele_bezoeker',209 'regelmatige_bezoeker',210 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek','datum'])211 for index, i in enumerate(list_day_2019[weekday]):212 print(index,i)213 df_help_date = copy.copy(mezuro_updated.groupby("datum").get_group(i))214 215 for index2, row in df_help_date.iterrows():216 '''217 #Make exception for overige gebieden: in that case, take mobility rates from destination gemeente - should probably be different in future iterations of the model218 df_help_date = df_help_date.reset_index()219 df_help_date_overig = df_help_date.loc[df_help_date['woon'] == 'Overige gebieden']220 df_help_date_known = df_help_date.loc[df_help_date['woon'] != 'Overige gebieden']221 df_help_date_overig['incidentele_bezoeker'] = df_help_date_overig['incidentele_bezoeker'].map(lambda value: value / (1+ float((Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == df_help_date_overig['bezoek']) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])])['retail']) / 100) * (1+float((Reduction_rates.loc[(Reduction_rates["sub_region_2"] == df_help_date_overig['bezoek']) & (Reduction_rates["date"] == current_date)])['retail']) / 100) )222 df_help_date_overig['regelmatige_bezoeker'] = df_help_date_overig['regelmatige_bezoeker'].map(lambda value: value / (1+ float((Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == df_help_date_overig['bezoek']) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])])['retail']) / 100) * (1+float((Reduction_rates.loc[(Reduction_rates["sub_region_2"] == df_help_date_overig['bezoek']) & (Reduction_rates["date"] == current_date)])['retail']) / 100) )223 df_help_date_overig['frequente_bezoeker'] = df_help_date_overig['frequente_bezoeker'].map(lambda value: value / (1+ float((Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == df_help_date_overig['bezoek']) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])])['workplaces']) / 100) * (1+float((Reduction_rates.loc[(Reduction_rates["sub_region_2"] == df_help_date_overig['bezoek']) & (Reduction_rates["date"] == current_date)])['workplaces']) / 100) )224 df_help_date_known['incidentele_bezoeker'] = df_help_date_known['incidentele_bezoeker'].map(lambda value: value / (1+ float((Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == df_help_date_overig['woon']) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])])['retail']) / 100) * (1+float((Reduction_rates.loc[(Reduction_rates["sub_region_2"] == df_help_date_overig['woon']) & (Reduction_rates["date"] == current_date)])['retail']) / 100) )225 df_help_date_known['regelmatige_bezoeker'] = df_help_date_known['regelmatige_bezoeker'].map(lambda value: value / (1+float((Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == df_help_date_overig['woon']) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])])['retail']) / 100) * (1+float((Reduction_rates.loc[(Reduction_rates["sub_region_2"] == df_help_date_overig['woon']) & (Reduction_rates["date"] == current_date)])['retail']) / 100) )226 df_help_date_known['frequente_bezoeker'] = df_help_date_known['frequente_bezoeker'].map(lambda value: value / (1+ float((Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == df_help_date_overig['woon']) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])])['workplaces']) / 100) * (1+float((Reduction_rates.loc[(Reduction_rates["sub_region_2"] == df_help_date_overig['woon']) & (Reduction_rates["date"] == current_date)])['workplaces']) / 100) )227 228 df_help_date = df_help_date_known.append(df_help_date_overig)229 df_help_date['totaal_aantal_bezoekers'] = df_help_date['incidentele_bezoeker'] + df_help_date['regelmatige_bezoeker'] + df_help_date['frequente_bezoeker']230 df_help_date = df_help_date.set_index(['woon','bezoek','datum'])231 232 233 '''234 if index2[0] == 'Overige gebieden':235 rivm_gemeente = index2[1]236 #rivm_gemeente = mp.koppeltabel.at[index2[1],'rivm'+year_gemeenten_used]237 else:238 #rivm_gemeente = mp.koppeltabel.at[index2[0],'rivm'+year_gemeenten_used]239 rivm_gemeente = index2[0]240 241 rate_row = Reduction_rates_march.loc[(Reduction_rates_march["sub_region_2"] == rivm_gemeente) & (Reduction_rates_march["date"] == list_day_2020[weekday][index])]242 rate_row2 = Reduction_rates.loc[(Reduction_rates["sub_region_2"] == rivm_gemeente) & (Reduction_rates["date"] == current_date)]243 244 df_help_date.at[index2,'incidentele_bezoeker'] /= ((1 + float(rate_row["retail"]) / 100) / (1 + float(rate_row2["retail"]) / 100) )245 df_help_date.at[index2,'regelmatige_bezoeker'] /= ((1 + float(rate_row["retail"]) / 100) / (1 + float(rate_row2["retail"]) / 100) )246 df_help_date.at[index2,'frequente_bezoeker'] /= ((1 + float(rate_row["workplaces"]) / 100) / (1 + float(rate_row2["workplaces"]) / 100) )247 #df_help_date.at[index2,'totaal_aantal_bezoekers'] = df_help_date.at[index2,'incidentele_bezoeker'] + df_help_date.at[index2,'regelmatige_bezoeker'] + df_help_date.at[index2,'frequente_bezoeker']248 249 df_help_collect = df_help_collect.append(df_help_date)250 251 df_help_collect = df_help_collect.groupby(['woon','bezoek']).agg({'bezoek_gemeente_id':'mean','woon_gemeente_id':'mean','totaal_aantal_bezoekers':'mean', 'incidentele_bezoeker':'mean', 'regelmatige_bezoeker':'mean', 'frequente_bezoeker':'mean' })252 df_help_collect = df_help_collect.reset_index()253 df_help_collect["datum"] = current_date254 df_help_collect = df_help_collect.set_index(['woon','bezoek','datum'])255 df_mobility = df_mobility.append(df_help_collect)256 257 df_mobility['totaal_aantal_bezoekers'] = df_mobility['incidentele_bezoeker'] + df_mobility['regelmatige_bezoeker'] + df_mobility['frequente_bezoeker']258 '''259 Mobility_df = copy.copy(df_mobility)260 '''261 '''262 #Finally, apply new reduction rates!263 for index, row in Mobility_df.iterrows():264 if index[0] == 'Overige gebieden':265 rivm_gemeente = mp.koppeltabel.at[index[1],'rivm'+year_gemeenten_used]266 print(index)267 else:268 rivm_gemeente = mp.koppeltabel.at[index[0],'rivm'+year_gemeenten_used]269 rate_row = Reduction_rates.loc[(Reduction_rates["sub_region_2"] == rivm_gemeente) & (Reduction_rates["date"] == index[2])]270 Mobility_df.at[index,'incidentele_bezoeker'] *= (1 + rate_row["retail"] / 100) 271 Mobility_df.at[index,'regelmatige_bezoeker'] *= (1 + rate_row["retail"] / 100)272 Mobility_df.at[index,'frequente_bezoeker'] *= (1 + rate_row["workplaces"] / 100)273 Mobility_df.at[index,'totaal_aantal_bezoekers'] = Mobility_df.at[index,'incidentele_bezoeker'] + Mobility_df.at[index,'regelmatige_bezoeker'] + Mobility_df.at[index,'frequente_bezoeker']274 275 return Mobility_df276 '''277 return df_mobility278 #Create new mobility file based on data from March 1-14 without Google rates279def create_mobility_per_date(start_date,end_date):280 date_format = '%d-%m-%Y'281 start_date_number = dt.datetime.strptime(start_date,date_format)282 end_date_number = dt.datetime.strptime(end_date,date_format)283 284 285 #First: determine which days are which:286 list_day_2019 = [['04-03-2019','11-03-2019'],287 ['05-03-2019','12-03-2019'],288 ['06-03-2019','13-03-2019'],289 ['07-03-2019','14-03-2019'],290 ['01-03-2019','08-03-2019'],291 ['02-03-2019','09-03-2019'],292 ['03-03-2019','10-03-2019']293 ]294 295 list_day_2020 = [['02-03-2020','09-03-2020'],296 ['03-03-2020','10-03-2020'],297 ['04-03-2020','11-03-2020'],298 ['05-03-2020','12-03-2020'],299 ['06-03-2020','13-03-2020'],300 ['07-03-2020','14-03-2020'],301 ['01-03-2020','08-03-2020']302 ]303 304 df_mobility = pd.DataFrame(columns = ['woon',305 'bezoek',306 'datum',307 'bezoek_gemeente_id',308 'woon_gemeente_id',309 'totaal_aantal_bezoekers',310 'incidentele_bezoeker',311 'regelmatige_bezoeker',312 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek','datum'])313 314 for t in range(0,(end_date_number - start_date_number).days + 1):315 day_difference = dt.timedelta(days = t)316 current_date_number = start_date_number + day_difference317 current_date = current_date_number.strftime('%d-%m-%Y')318 print(current_date)319 weekday = current_date_number.weekday()320 df_help_collect = pd.DataFrame(columns = ['woon',321 'bezoek',322 'datum',323 'bezoek_gemeente_id',324 'woon_gemeente_id',325 'totaal_aantal_bezoekers',326 'incidentele_bezoeker',327 'regelmatige_bezoeker',328 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek','datum'])329 for index, i in enumerate(list_day_2019[weekday]):330 df_help_date = mp.gemeente2gemeente.groupby("datum").get_group(i)331 df_help_collect = df_help_collect.append(df_help_date)332 df_help_collect = df_help_collect.groupby(['woon','bezoek']).agg({'totaal_aantal_bezoekers':'mean', 'incidentele_bezoeker':'mean', 'regelmatige_bezoeker':'mean', 'frequente_bezoeker':'mean' })333 df_help_collect = df_help_collect.reset_index()334 df_help_collect["datum"] = current_date335 df_help_collect = df_help_collect.set_index(['woon','bezoek','datum'])336 df_mobility = df_mobility.append(df_help_collect)337 338 Mobility_df = copy.copy(df_mobility)339 return Mobility_df340def create_CBS_OD_matrix():341 CBS_work_mobility=pd.read_csv(342 'data/Woon_werkafstand_werknemers__regio.csv',delimiter = ';').rename(columns={'Banen van werknemers (x 1 000)':'banen'})343 344 CBS_work_mobility["Woonregio's"] = CBS_work_mobility["Woonregio's"].replace(345 ['Groningen (gemeente)','Utrecht (gemeente)','Hengelo (O.)',"'s-Gravenhage (gemeente)",'Beek (L.)','Laren (NH.)','Middelburg (Z.)','Rijswijk (ZH.)','Stein (L.)'],346 ['Groningen','Utrecht','Hengelo',"'s-Gravenhage",'Beek','Laren','Middelburg','Rijswijk','Stein']347 ) 348 CBS_work_mobility["Werkregio's"] = CBS_work_mobility["Werkregio's"].replace(349 ['Groningen (gemeente)','Utrecht (gemeente)','Hengelo (O.)',"'s-Gravenhage (gemeente)",'Beek (L.)','Laren (NH.)','Middelburg (Z.)','Rijswijk (ZH.)','Stein (L.)'],350 ['Groningen','Utrecht','Hengelo',"'s-Gravenhage",'Beek','Laren','Middelburg','Rijswijk','Stein']351 )352 CBS_work_mobility['banen'] = CBS_work_mobility['banen'].astype('float')353 CBS_work_mobility = CBS_work_mobility.set_index(["Woonregio's","Werkregio's"])354 355 df_mobility = pd.DataFrame(columns = ['woon',356 'bezoek',357 'bezoek_gemeente_id',358 'woon_gemeente_id',359 'totaal_aantal_bezoekers',360 'incidentele_bezoeker',361 'regelmatige_bezoeker',362 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek'])363 364 for index, row in CBS_work_mobility.iterrows():365 if index[0] != index[1] and index[0] in mp.gemeenten_list.index and index[1] in mp.gemeenten_list.index:366 if row['banen'] != 0:367 new_row = {'woon':index[0],368 'bezoek':index[1],369 'bezoek_gemeente_id':mp.gemeenten_list.at[index[0],'Gemeentecode'],370 'woon_gemeente_id':mp.gemeenten_list.at[index[1],'Gemeentecode'],371 'totaal_aantal_bezoekers':row['banen'] * 1000,372 'incidentele_bezoeker':0,373 'regelmatige_bezoeker':0,374 'frequente_bezoeker':row['banen'] * 1000375 }376 df_mobility = df_mobility.append(new_row, ignore_index = True)377 print(index[0],index[1])378 return df_mobility379def preprocess_rates_CBS(start_date,end_date):380 #Transform start and today date into datetime object:381 date_format = '%d-%m-%Y'382 start_date_number = dt.datetime.strptime(start_date,date_format)383 end_date_number = dt.datetime.strptime(end_date,date_format)384 385 #Create empty to-be-filled dataframes386 Reduction_rates_google = pd.DataFrame(columns = ['country_region_code', 387 'country_region', 388 'sub_region_1', 389 'sub_region_2', 390 'metro_area', 391 'iso_3166_2_code', 392 'census_fips_code', 393 'date',394 'retail', 395 'grocery', 396 'parks', 397 'transit',398 'workplaces',399 'residential'])400 401 Reduction_rates_google_prov = pd.DataFrame(columns = ['country_region_code', 402 'country_region', 403 'sub_region_1', 404 'sub_region_2', 405 'metro_area', 406 'iso_3166_2_code', 407 'census_fips_code', 408 'date',409 'retail', 410 'grocery', 411 'parks', 412 'transit',413 'workplaces',414 'residential'])415 416 417 for t in range(1, (end_date_number - start_date_number).days + 2):418 #Calculate new date419 day_difference = dt.timedelta(days = t-1)420 current_date_number = start_date_number + day_difference421 current_date = current_date_number.strftime('%d-%m-%Y')422 current_date_google_format = current_date_number.strftime('%Y-%m-%d')423 #Extract part of Google dataframe corresponding to the current date (first only municipalities, then only provinces)424 google_mob2 = google_mobility.loc[google_mobility["sub_region_2"].notnull()].groupby('date').get_group(current_date_google_format)425 google_mob_prov = google_mobility.loc[(google_mobility["sub_region_2"].isnull()) & (google_mobility["sub_region_1"].notnull())].groupby('date').get_group(current_date_google_format)426 row_NL = google_mobility.loc[(google_mobility["sub_region_1"].isnull()) & (google_mobility["date"] == current_date_google_format) & google_mobility["sub_region_2"].isnull()] 427 428 print(current_date)429 430 #Fill up missing values in the province data431 for index,row in google_mob_prov.iterrows():432 google_mob_prov.at[index,'date'] = current_date433 for col in ['retail','grocery','parks','transit','workplaces','residential']:434 if math.isnan(row[col]):435 reduction_value = row_NL[col]436 google_mob_prov.at[index,col] = reduction_value437 438 #Fill up missing values in the municipality data439 for index,row in google_mob2.iterrows():440 row_province = google_mobility.loc[(google_mobility["sub_region_1"] == row["sub_region_1"]) & (google_mobility["date"] == current_date_google_format) & google_mobility["sub_region_2"].isnull()]441 google_mob2.at[index,'date'] = current_date442 for col in ['retail','grocery','parks','transit','workplaces','residential']:443 if math.isnan(row[col]):444 reduction_value = row_province[col]445 if math.isnan(reduction_value):446 reduction_value = row_NL[col]447 google_mob2.at[index,col] = reduction_value448 449 #Add filled-up data frames450 Reduction_rates_google = Reduction_rates_google.append(google_mob2)451 Reduction_rates_google_prov = Reduction_rates_google_prov.append(google_mob_prov)452 453 #Include municipalities that do not have an entry in the Google data for the current date454 for index, row in mp.koppeltabel.iterrows():455 if len(Reduction_rates_google.loc[(Reduction_rates_google["sub_region_2"] == row["rivm"+year_gemeenten_used]) & (Reduction_rates_google["date"] == current_date)]) == 0:456 province = mp.gemeente_shapes_old.at[index,'prov_name']457 df_help_add = copy.copy(Reduction_rates_google_prov.groupby("sub_region_1").get_group(province).groupby("date").get_group(current_date))458 df_help_add["sub_region_2"] = df_help_add["sub_region_2"].fillna(row["rivm"+year_gemeenten_used])459 Reduction_rates_google = Reduction_rates_google.append(df_help_add)460 461 print(len(Reduction_rates_google))462 #Include missing municipalities from Mezuro data:463 #First: get list of municipalities in Google data:464 df_help_gemeentes = Reduction_rates_google.groupby('date').get_group(end_date) 465 for index, row in mp.koppeltabel.iterrows():466 if len(Reduction_rates_google.loc[Reduction_rates_google["sub_region_2"] == row['rivm'+year_gemeenten_used]]) == 0:467 province = mp.gemeente_shapes.at[index,'prov_name']468 df_help_add = copy.copy(Reduction_rates_google_prov.groupby("sub_region_1").get_group(province))469 df_help_add["sub_region_2"] = df_help_add["sub_region_2"].fillna(row['rivm'+year_gemeenten_used])470 Reduction_rates_google = Reduction_rates_google.append(df_help_add)471 #Note: removal of rows might not be as handy with regard to merging muniipalities472 print(len(Reduction_rates_google)) 473 #Remove rows if municipality in Google data is not in rivm data:474 for index,row in df_help_gemeentes.iterrows():475 if len(mp.koppeltabel.loc[mp.koppeltabel['rivm'+year_gemeenten_used] == row["sub_region_2"]]) == 0:476 Reduction_rates_google = Reduction_rates_google[Reduction_rates_google["sub_region_2"] != row['sub_region_2']]477 print(len(Reduction_rates_google))478 return Reduction_rates_google, Reduction_rates_google_prov479def apply_rates_CBS(start_date,end_date,Reduction_rates,df):480 date_format = '%d-%m-%Y'481 start_date_number = dt.datetime.strptime(start_date,date_format)482 end_date_number = dt.datetime.strptime(end_date,date_format)483 484 Reduction_rates = Reduction_rates.set_index(['sub_region_2','date'])485 df_mobility = pd.DataFrame(columns = ['woon',486 'bezoek',487 'datum',488 'bezoek_gemeente_id',489 'woon_gemeente_id',490 'totaal_aantal_bezoekers',491 'incidentele_bezoeker',492 'regelmatige_bezoeker',493 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek','datum'])494 495 496 for t in range(0,(end_date_number - start_date_number).days + 1):497 day_difference = dt.timedelta(days = t)498 current_date_number = start_date_number + day_difference499 current_date = current_date_number.strftime('%d-%m-%Y')500 print('Current date:',current_date)501 df_help_date = copy.copy(df).set_index(['woon','bezoek'])502 503 504 #Determine weekend-factor:505 Weekday_factor = 1506 '''507 508 if current_date_number.weekday() == 5:509 Weekday_factor = .36*6.2 / 31510 if current_date_number.weekday() == 6:511 Weekday_factor = .27*6.2 / 31512 if current_date_number.weekday() != 5 and current_date_number.weekday() != 6:513 Weekday_factor = 1 - (.36+.27)*6.2 / 31514 '''515 516 for index2, row in df_help_date.iterrows():517 #print(index2)518 #Make exception for overige gebieden: in that case, take mobility rates from destination gemeente - should probably be different in future iterations of the model519 if index2[0] == 'Overige gebieden':520 rivm_gemeente = mp.koppeltabel.at[index2[1],'rivm2020']521 else:522 #rivm_gemeente = mp.koppeltabel.at[index2[0],'rivm2020']523 rivm_gemeente = index2[0]524 Target_rate_retail = Reduction_rates.at[(rivm_gemeente,current_date),'retail']525 Target_rate_workplaces = Reduction_rates.at[(rivm_gemeente,current_date),'workplaces'] 526 527 528 df_help_date.at[index2,'incidentele_bezoeker'] *= (1 + Target_rate_retail / 100) * Weekday_factor529 df_help_date.at[index2,'regelmatige_bezoeker'] *= (1 + Target_rate_retail / 100) * Weekday_factor530 df_help_date.at[index2,'frequente_bezoeker'] *= (1 + Target_rate_workplaces / 100) * Weekday_factor531 532 df_help_date['datum'] = current_date533 df_help_date['totaal_aantal_bezoekers'] = df_help_date['incidentele_bezoeker'] + df_help_date['regelmatige_bezoeker'] + df_help_date['frequente_bezoeker']534 #df_help_date = df_help_date.reset_index535 #df_help_date = df_help_date.set_index(['woon','bezoek','datum'])536 537 df_help_date = df_help_date.reset_index()538 539 df_mobility = df_mobility.append(df_help_date)540 541 542 return df_mobility.set_index(['woon','bezoek','datum'])543 544def create_2019_CBS(df):545 start_date = '01-03-2019'546 end_date = '14-03-2019'547 start_date_nr = dt.datetime.strptime(start_date,'%d-%m-%Y')548 end_date_nr = dt.datetime.strptime(end_date,'%d-%m-%Y')549 550 df_mobility = pd.DataFrame(columns = ['woon',551 'bezoek',552 'datum',553 'bezoek_gemeente_id',554 'woon_gemeente_id',555 'totaal_aantal_bezoekers',556 'incidentele_bezoeker',557 'regelmatige_bezoeker',558 'frequente_bezoeker'], dtype = float).set_index(['woon','bezoek','datum'])559 560 561 for t in range(0,14):562 Current_date_nr = start_date_nr + dt.timedelta(days = t)563 Current_date = Current_date_nr.strftime('%d-%m-%Y')564 565 df_HELP = copy.copy(df)566 df_HELP['datum'] = Current_date567 df_mobility = df_mobility.append(df_HELP)568 return df_mobility.set_index(['woon','bezoek','datum'])569 ...

Full Screen

Full Screen

synthesevraag2.py

Source:synthesevraag2.py Github

copy

Full Screen

1from random import randint2def basisscore_berekenen(naam, aantal_bezoek, geboortejaar):3 basisscore = 04 komt_voor = ""5 som = 06 for i in range(len(naam)):7 if naam[i] in "cinema":8 #komt_voor += str(naam[i])9 som += (ord(naam[i]) * (i + 1))10 som += geboortejaar11 #som *= aantal_bezoek12 return som13def gewonnen_tickets(randomgetal):14 som_tickets = 015 string_random = str(randomgetal)16 for letter in string_random:17 som_tickets += int(letter)18 randomgetall = int(string_random)19 return som_tickets20def is_slechteklant(snacks, aantal_bezoek):21 if snacks.lower() == "n" and (aantal_bezoek == "2" or aantal_bezoek == "1"):22 return True23 else:24 return False25def bezoek_korting(aantalbezoeken, score):26 if aantalbezoeken == 1:27 score /= 228 elif aantalbezoeken == 2:29 score *= 230 elif aantalbezoeken == 3:31 score *= 332 return score33def main():34 naam = input("Naam: ")35 winnaar = ""36 score = 037 random_getal = randint(0, 10000)38 som_gewonnengetal = 039 bezoek_per_maand = 040 while naam != "xxx" and naam != "qqq":41 geboortejaar = int(input("Geboortejaar: "))42 bezoek_per_maand = int(input("Hoevaak per maand bezoekt u de kinepolis? (1 = weinig, 2 = matig, 3 = veel): "))43 versnaperingen = input("Wat neemt u als versnapering? (p = popcorn, c = chips, n = niets): ")44 som = basisscore_berekenen(naam, bezoek_per_maand, geboortejaar)45 print(naam, ": ", som)46 if is_slechteklant(versnaperingen, bezoek_per_maand):47 som -= 105048 if som > score:49 winnaar = naam50 score = som51 naam = input("Naam: ")52 while random_getal < 5000 and random_getal % 10 != 0:53 if random_getal % 2 != 0:54 random_getal = randint(0, 10000)55 if winnaar != "":56 print(winnaar, ": Jij hebt gewonnen!")57 print("Jouw score is: ", bezoek_korting(bezoek_per_maand, score))58 print("Het random gegenereerde getal is: ", random_getal)59 print(winnaar, ", Jij wint hierbij", gewonnen_tickets(random_getal), "filmtickets!.")60if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful