"""This file defines the functions that maps the supported perils with the correlation settings. This data is usuallyobtained from the model_settings."""fromtypingimportOptionalimportpandasaspd
[docs]defmap_data(data:Optional[dict],logger)->Optional[pd.DataFrame]:""" Maps data from the model settings to to have Peril ID, peril_correlation_group, and damage_correlation_value. Args: data: (dict) the data loaded from the model settings Returns: (pd.DataFrame) the mapped data """ifdataisnotNone:supported_perils=data.get("lookup_settings",{}).get("supported_perils",[])correlations_legacy=data.get("correlation_settings",[])correlation_settings=data.get("model_settings",{}).get("correlation_settings",correlations_legacy)forsupported_perilinsupported_perils:# supported_perils is expected to be a list of dictsupported_peril["peril_correlation_group"]=supported_peril.get("peril_correlation_group",0)supported_perils_df=pd.DataFrame(supported_perils)correlation_settings_df=pd.DataFrame(correlation_settings)iflen(correlation_settings_df)>0:# correlations_settings are definedif"damage_correlation_value"notincorrelation_settings_df.columns:logger.info("Correlation settings: No `damage_correlation_value` found")correlation_settings_df["damage_correlation_value"]=0if"hazard_correlation_value"notincorrelation_settings_df.columns:logger.info("Correlation settings: No `hazard_correlation_value` found")correlation_settings_df["hazard_correlation_value"]=0# merge allows duplicates of the "peril_correlation_group" in the supported perils# merge does not allow duplicates of the "peril_correlation_group" in the correlation settingsiflen(supported_perils_df)>0andlen(correlation_settings_df)>0:mapped_data=pd.merge(supported_perils_df,correlation_settings_df,on="peril_correlation_group")returnmapped_data