def plot_multilocation_mobility(plotter: StreamlitPlotter, app: AppRegion): params = app.params["default"] values, days = get_mobility_data( params["country"]["iso3"], params["mobility"]["region"], BASE_DATE, params["mobility"]["google_mobility_locations"], ) plots.model.plots.plot_time_varying_multi_input(plotter, values, days, is_logscale=False)
def __init__( self, country_iso3: str, region: str, mixing: dict, npi_effectiveness_params: dict, google_mobility_locations: dict, is_periodic_intervention: bool, periodic_int_params: dict, periodic_end_time: float, microdistancing_params: dict, ): """Build the time variant location adjustment functions""" # Load mobility data google_mobility_values, google_mobility_days = get_mobility_data( country_iso3, region, BASE_DATETIME, google_mobility_locations) # Build mixing data timeseries mixing = update_mixing_data( mixing, npi_effectiveness_params, google_mobility_values, google_mobility_days, is_periodic_intervention, periodic_int_params, periodic_end_time, ) # Build the time variant location adjustment functions from mixing timeseries mixing_locations = [loc for loc in LOCATIONS if loc in mixing] self.loc_adj_funcs = {} for loc_key in mixing_locations: loc_times = mixing[loc_key]["times"] loc_vals = mixing[loc_key]["values"] self.loc_adj_funcs[loc_key] = scale_up_function(loc_times, loc_vals, method=4) # Work out microdistancing function to be applied to all non-household locations if not microdistancing_params: self.microdistancing_function = None elif microdistancing_params["function_type"] == "tanh": self.microdistancing_function = tanh_based_scaleup( **microdistancing_params["parameters"]) elif microdistancing_params["function_type"] == "empiric": micro_times = microdistancing_params["parameters"]["times"] micro_vals = microdistancing_params["parameters"]["values"] self.microdistancing_function = scale_up_function(micro_times, micro_vals, method=4) # Load all location-specific mixing info. self.matrix_components = {} for sheet_type in ["all_locations"] + LOCATIONS: # Loads a 16x16 ndarray self.matrix_components[sheet_type] = get_country_mixing_matrix( sheet_type, country_iso3)
def plot_multicluster_mobility( plotter: StreamlitPlotter, calib_dir_path: str, mcmc_tables: List[pd.DataFrame], mcmc_params: List[pd.DataFrame], targets: dict, app_name: str, region: str, ): app = covid_19.app.get_region("victoria") params = app.params["default"] all_cluster_mobility_values = {} fig, axes, max_dims, n_rows, n_cols, _ = plotter.get_figure() for i_region in Region.VICTORIA_METRO + Region.VICTORIA_RURAL: google_mobility_values, google_mobility_days = get_mobility_data( params["country"]["iso3"], i_region.replace("-", "_").upper(), BASE_DATE, params["mobility"]["google_mobility_locations"]) all_cluster_mobility_values[i_region] = google_mobility_values for i_region in Region.VICTORIA_METRO: axes.plot(google_mobility_days, apply_moving_average( all_cluster_mobility_values[i_region]["work"], 7), color="k", alpha=0.5) axes.plot(google_mobility_days, apply_moving_average( all_cluster_mobility_values[i_region]["other_locations"], 7), color="g", alpha=0.5) for i_region in Region.VICTORIA_RURAL: axes.plot(google_mobility_days, apply_moving_average( all_cluster_mobility_values[i_region]["work"], 7), color="b", alpha=0.5) axes.plot(google_mobility_days, apply_moving_average( all_cluster_mobility_values[i_region]["other_locations"], 7), color="brown", alpha=0.5) axes.set_xlim(left=STANDARD_X_LIMITS[0], right=STANDARD_X_LIMITS[1]) axes.set_ylim(top=1.) change_xaxis_to_date(axes, REF_DATE, rotation=0) plotter.save_figure(fig, filename=f"multi_cluster_mobility", title_text="Google mobility")
def plot_mobility_raw(plotter: StreamlitPlotter, app: AppRegion): params = app.params["default"] values, days = get_mobility_data( params["country"]["iso3"], params["mobility"]["region"], BASE_DATE, params["mobility"]["google_mobility_locations"], ) options = list(params["mobility"]["google_mobility_locations"].keys()) loc_key = st.sidebar.selectbox("Select location", options) values_lookup = {days[i]: values[loc_key][i] for i in range(len(days))} loc_func = lambda t: values_lookup[t] plots.model.plots.plot_time_varying_input(plotter, loc_key, loc_func, days, is_logscale=False)
def test_get_mobility_data(): google_mobility_locations = { "work": ["workplaces"], "other_locations": [ "retail_and_recreation", "grocery_and_pharmacy", "parks", "transit_stations", ], } base_date = datetime(2020, 1, 1, 0, 0, 0) loc_mobility, days = get_mobility_data("AUS", "Victoria", base_date, google_mobility_locations) loc_mobility = { k: [round(i, 2) for i in v] for k, v in loc_mobility.items() } assert days[:10] == [45, 46, 47, 48, 49, 50, 51, 52, 53, 54] assert loc_mobility["work"][:10] == [ 1.03, 0.98, 1.18, 1.13, 1.13, 1.14, 1.15, 1.04, 0.97, 1.17, ] assert loc_mobility["other_locations"][:10] == [ 0.96, 1.02, 1.09, 0.92, 0.96, 1.01, 1.05, 1.13, 1.09, 1.05, ]
def get_mobility_funcs( country: Country, region: str, mixing: Dict[str, MixingLocation], google_mobility_locations: List[str], npi_effectiveness_params: Dict[str, float], square_mobility_effect: bool, smooth_google_data: bool, ) -> Dict[str, Callable[[float], float]]: """ Loads google mobility data, combines it with user requested timeseries data and then returns a mobility function for each location. """ google_mobility_values, google_mobility_days = get_mobility_data( country.iso3, region, BASE_DATETIME, google_mobility_locations) if smooth_google_data: for loc in google_mobility_values: google_mobility_values[loc] = apply_moving_average( google_mobility_values[loc], 7) # Build mixing data timeseries mixing = update_mixing_data( {k: v.dict() for k, v in mixing.items()}, npi_effectiveness_params, google_mobility_values, google_mobility_days, ) # Build the time variant location-specific macrodistancing adjustment functions from mixing timeseries mobility_funcs = {} for location, timeseries in mixing.items(): if square_mobility_effect: loc_vals = [v**2 for v in timeseries["values"]] else: loc_vals = timeseries["values"] mobility_funcs[location] = scale_up_function(timeseries["times"], loc_vals, method=4) return mobility_funcs