Skip to content

Commit

Permalink
Merge pull request #141 from ricardogsilva/117-add-bootstrap-config-f…
Browse files Browse the repository at this point in the history
…or-related-coverage-configurations

Added related and uncertainty configurations for bootstrapped coverages
  • Loading branch information
francbartoli authored Jun 14, 2024
2 parents 36101aa + 6f00d9b commit 01b4bc2
Show file tree
Hide file tree
Showing 11 changed files with 1,303 additions and 54 deletions.
127 changes: 107 additions & 20 deletions arpav_ppcv/bootstrapper/cliapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
from ..schemas.coverages import (
ConfigurationParameterCreate,
ConfigurationParameterValueCreateEmbeddedInConfigurationParameter,
ConfigurationParameterPossibleValueUpdate,
CoverageConfigurationUpdate,
)

from .coverage_configurations.cdd import generate_cdd_configurations
from .coverage_configurations.fd import generate_fd_configurations
from .coverage_configurations.pr import generate_pr_configurations
from .coverage_configurations.r95ptot import generate_r95ptot_configurations
from .coverage_configurations.snwdays import generate_snwdays_configurations
from .coverage_configurations.su30 import generate_su30_configurations
from .coverage_configurations.tas import generate_tas_configurations
from .coverage_configurations.tasmax import generate_tasmax_configurations
from .coverage_configurations.tasmin import generate_tasmin_configurations
from .coverage_configurations.tr import generate_tr_configurations
from .coverage_configurations import (
cdd,
fd,
pr,
r95ptot,
snwdays,
su30,
tas,
tasmax,
tasmin,
tr,
)

app = typer.Typer()

Expand Down Expand Up @@ -200,29 +204,29 @@ def bootstrap_coverage_configurations(
for pv in all_conf_param_values
}
coverage_configurations = []
coverage_configurations.extend(generate_cdd_configurations(conf_param_values))
coverage_configurations.extend(cdd.generate_configurations(conf_param_values))
coverage_configurations.extend(
generate_fd_configurations(conf_param_values, variables)
fd.generate_configurations(conf_param_values, variables)
)
coverage_configurations.extend(
generate_pr_configurations(conf_param_values, variables)
pr.generate_configurations(conf_param_values, variables)
)
coverage_configurations.extend(generate_r95ptot_configurations(conf_param_values))
coverage_configurations.extend(generate_snwdays_configurations(conf_param_values))
coverage_configurations.extend(r95ptot.generate_configurations(conf_param_values))
coverage_configurations.extend(snwdays.generate_configurations(conf_param_values))
coverage_configurations.extend(
generate_su30_configurations(conf_param_values, variables)
su30.generate_configurations(conf_param_values, variables)
)
coverage_configurations.extend(
generate_tas_configurations(conf_param_values, variables)
tas.generate_configurations(conf_param_values, variables)
)
coverage_configurations.extend(
generate_tasmax_configurations(conf_param_values, variables)
tasmax.generate_configurations(conf_param_values, variables)
)
coverage_configurations.extend(
generate_tasmin_configurations(conf_param_values, variables)
tasmin.generate_configurations(conf_param_values, variables)
)
coverage_configurations.extend(
generate_tr_configurations(conf_param_values, variables)
tr.generate_configurations(conf_param_values, variables)
)

for cov_conf_create in coverage_configurations:
Expand All @@ -237,3 +241,86 @@ def bootstrap_coverage_configurations(
f"{cov_conf_create.name!r}: {err}"
)
session.rollback()

print("Creating related coverage relationships...")
all_cov_confs = {
cc.name: cc for cc in database.collect_all_coverage_configurations(session)
}

to_update = {}
for name, related_names in {
**cdd.get_related_map(),
**fd.get_related_map(),
**pr.get_related_map(),
**r95ptot.get_related_map(),
**snwdays.get_related_map(),
**su30.get_related_map(),
**tas.get_related_map(),
**tasmax.get_related_map(),
**tasmin.get_related_map(),
**tr.get_related_map(),
}.items():
to_update[name] = {
"related": related_names,
}

for name, uncertainties in {
**cdd.get_uncertainty_map(),
**fd.get_uncertainty_map(),
**pr.get_uncertainty_map(),
**r95ptot.get_uncertainty_map(),
**snwdays.get_uncertainty_map(),
**su30.get_uncertainty_map(),
**tas.get_uncertainty_map(),
**tasmax.get_uncertainty_map(),
**tasmin.get_uncertainty_map(),
**tr.get_uncertainty_map(),
}.items():
info = to_update.setdefault(name, {})
info["uncertainties"] = uncertainties

for name, info in to_update.items():
main_cov_conf = all_cov_confs[name]
secondaries = info.get("related")
uncertainties = info.get("uncertainties")
update_kwargs = {}
if secondaries is not None:
secondary_cov_confs = [
cc for name, cc in all_cov_confs.items() if name in secondaries
]
update_kwargs["secondary_coverage_configurations_ids"] = [
cc.id for cc in secondary_cov_confs
]
if uncertainties is not None:
lower_uncert_id = [
cc.id for name, cc in all_cov_confs.items() if name == uncertainties[0]
][0]
upper_uncert_id = [
cc.id for name, cc in all_cov_confs.items() if name == uncertainties[1]
][0]
update_kwargs.update(
uncertainty_lower_bounds_coverage_configuration_id=lower_uncert_id,
uncertainty_upper_bounds_coverage_configuration_id=upper_uncert_id,
)
cov_update = CoverageConfigurationUpdate(
**main_cov_conf.model_dump(
exclude={
"uncertainty_lower_bounds_coverage_configuration_id",
"uncertainty_upper_bounds_coverage_configuration_id",
"secondary_coverage_configurations_ids",
"possible_values",
}
),
**update_kwargs,
possible_values=[
ConfigurationParameterPossibleValueUpdate(
configuration_parameter_value_id=pv.configuration_parameter_value_id
)
for pv in main_cov_conf.possible_values
],
)
database.update_coverage_configuration(
session,
main_cov_conf,
cov_update,
)
65 changes: 58 additions & 7 deletions arpav_ppcv/bootstrapper/coverage_configurations/cdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)


def generate_cdd_configurations(
def generate_configurations(
conf_param_values,
) -> list[CoverageConfigurationCreate]:
return [
Expand All @@ -22,7 +22,7 @@ def generate_cdd_configurations(
netcdf_main_dataset_name="cdd",
thredds_url_pattern="ensembletwbc/std/clipped/eca_cdd_an_avgagree_{time_window}_{scenario}_{year_period}_ls_VFVGTAA.nc",
unit="gg",
palette="uncert-stippled/div-BrBg-inv",
palette="uncert-stippled/div-BrBG-inv",
color_scale_min=-40,
color_scale_max=40,
possible_values=[
Expand Down Expand Up @@ -78,7 +78,7 @@ def generate_cdd_configurations(
netcdf_main_dataset_name="cdd",
thredds_url_pattern="indici5rcm/clipped/eca_cdd_an_EC-EARTH_CCLM4-8-17_{scenario}_{year_period}_{time_window}_ls_VFVGTAA.nc",
unit="gg",
palette="default/div-BrBg-inv",
palette="default/div-BrBG-inv",
color_scale_min=-40,
color_scale_max=40,
possible_values=[
Expand Down Expand Up @@ -134,7 +134,7 @@ def generate_cdd_configurations(
netcdf_main_dataset_name="cdd",
thredds_url_pattern="indici5rcm/clipped/eca_cdd_an_EC-EARTH_RACMO22E_{scenario}_{year_period}_{time_window}_ls_VFVGTAA.nc",
unit="gg",
palette="default/div-BrBg-inv",
palette="default/div-BrBG-inv",
color_scale_min=-40,
color_scale_max=40,
possible_values=[
Expand Down Expand Up @@ -190,7 +190,7 @@ def generate_cdd_configurations(
netcdf_main_dataset_name="cdd",
thredds_url_pattern="indici5rcm/clipped/eca_cdd_an_EC-EARTH_RCA4_{scenario}_{year_period}_{time_window}_ls_VFVGTAA.nc",
unit="gg",
palette="default/div-BrBg-inv",
palette="default/div-BrBG-inv",
color_scale_min=-40,
color_scale_max=40,
possible_values=[
Expand Down Expand Up @@ -246,7 +246,7 @@ def generate_cdd_configurations(
netcdf_main_dataset_name="cdd",
thredds_url_pattern="indici5rcm/clipped/eca_cdd_an_HadGEM2-ES_RACMO22E_{scenario}_{year_period}_{time_window}_ls_VFVGTAA.nc",
unit="gg",
palette="default/div-BrBg-inv",
palette="default/div-BrBG-inv",
color_scale_min=-40,
color_scale_max=40,
possible_values=[
Expand Down Expand Up @@ -302,7 +302,7 @@ def generate_cdd_configurations(
netcdf_main_dataset_name="cdd",
thredds_url_pattern="indici5rcm/clipped/eca_cdd_an_MPI-ESM-LR_REMO2009_{scenario}_{year_period}_{time_window}_ls_VFVGTAA.nc",
unit="gg",
palette="default/div-BrBg-inv",
palette="default/div-BrBG-inv",
color_scale_min=-40,
color_scale_max=40,
possible_values=[
Expand Down Expand Up @@ -354,3 +354,54 @@ def generate_cdd_configurations(
],
),
]


def get_related_map() -> dict[str, list[str]]:
return {
"cdd_30yr_anomaly_annual_agree_model_ensemble": [
"cdd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"cdd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"cdd_30yr_anomaly_annual_model_ec_earth_rca4",
"cdd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"cdd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"cdd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17": [
"cdd_30yr_anomaly_annual_agree_model_ensemble",
"cdd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"cdd_30yr_anomaly_annual_model_ec_earth_rca4",
"cdd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"cdd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"cdd_30yr_anomaly_annual_model_ec_earth_racmo22e": [
"cdd_30yr_anomaly_annual_agree_model_ensemble",
"cdd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"cdd_30yr_anomaly_annual_model_ec_earth_rca4",
"cdd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"cdd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"cdd_30yr_anomaly_annual_model_ec_earth_rca4": [
"cdd_30yr_anomaly_annual_agree_model_ensemble",
"cdd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"cdd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"cdd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"cdd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"cdd_30yr_anomaly_annual_model_hadgem2_es_racmo22e": [
"cdd_30yr_anomaly_annual_agree_model_ensemble",
"cdd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"cdd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"cdd_30yr_anomaly_annual_model_ec_earth_rca4",
"cdd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"cdd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009": [
"cdd_30yr_anomaly_annual_agree_model_ensemble",
"cdd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"cdd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"cdd_30yr_anomaly_annual_model_ec_earth_rca4",
"cdd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
],
}


def get_uncertainty_map() -> dict[str, tuple[str, str]]:
return {}
100 changes: 99 additions & 1 deletion arpav_ppcv/bootstrapper/coverage_configurations/fd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)


def generate_fd_configurations(
def generate_configurations(
conf_param_values, variables
) -> list[CoverageConfigurationCreate]:
return [
Expand Down Expand Up @@ -477,3 +477,101 @@ def generate_fd_configurations(
],
),
]


def get_related_map() -> dict[str, list[str]]:
return {
"fd_annual_absolute_model_ensemble": [
"fd_annual_absolute_model_ec_earth_cclm4_8_17",
"fd_annual_absolute_model_ec_earth_racmo22e",
"fd_annual_absolute_model_ec_earth_rca4",
"fd_annual_absolute_model_hadgem2_es_racmo22e",
"fd_annual_absolute_model_mpi_esm_lr_remo2009",
],
"fd_annual_absolute_model_ec_earth_cclm4_8_17": [
"fd_annual_absolute_model_ensemble",
"fd_annual_absolute_model_ec_earth_racmo22e",
"fd_annual_absolute_model_ec_earth_rca4",
"fd_annual_absolute_model_hadgem2_es_racmo22e",
"fd_annual_absolute_model_mpi_esm_lr_remo2009",
],
"fd_annual_absolute_model_ec_earth_racmo22e": [
"fd_annual_absolute_model_ensemble",
"fd_annual_absolute_model_ec_earth_cclm4_8_17",
"fd_annual_absolute_model_ec_earth_rca4",
"fd_annual_absolute_model_hadgem2_es_racmo22e",
"fd_annual_absolute_model_mpi_esm_lr_remo2009",
],
"fd_annual_absolute_model_ec_earth_rca4": [
"fd_annual_absolute_model_ensemble",
"fd_annual_absolute_model_ec_earth_cclm4_8_17",
"fd_annual_absolute_model_ec_earth_racmo22e",
"fd_annual_absolute_model_hadgem2_es_racmo22e",
"fd_annual_absolute_model_mpi_esm_lr_remo2009",
],
"fd_annual_absolute_model_hadgem2_es_racmo22e": [
"fd_annual_absolute_model_ensemble",
"fd_annual_absolute_model_ec_earth_cclm4_8_17",
"fd_annual_absolute_model_ec_earth_racmo22e",
"fd_annual_absolute_model_ec_earth_rca4",
"fd_annual_absolute_model_mpi_esm_lr_remo2009",
],
"fd_annual_absolute_model_mpi_esm_lr_remo2009": [
"fd_annual_absolute_model_ensemble",
"fd_annual_absolute_model_ec_earth_cclm4_8_17",
"fd_annual_absolute_model_ec_earth_racmo22e",
"fd_annual_absolute_model_ec_earth_rca4",
"fd_annual_absolute_model_hadgem2_es_racmo22e",
],
"fd_30yr_anomaly_annual_agree_model_ensemble": [
"fd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"fd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"fd_30yr_anomaly_annual_model_ec_earth_rca4",
"fd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"fd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"fd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17": [
"fd_30yr_anomaly_annual_agree_model_ensemble",
"fd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"fd_30yr_anomaly_annual_model_ec_earth_rca4",
"fd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"fd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"fd_30yr_anomaly_annual_model_ec_earth_racmo22e": [
"fd_30yr_anomaly_annual_agree_model_ensemble",
"fd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"fd_30yr_anomaly_annual_model_ec_earth_rca4",
"fd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"fd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"fd_30yr_anomaly_annual_model_ec_earth_rca4": [
"fd_30yr_anomaly_annual_agree_model_ensemble",
"fd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"fd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"fd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
"fd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"fd_30yr_anomaly_annual_model_hadgem2_es_racmo22e": [
"fd_30yr_anomaly_annual_agree_model_ensemble",
"fd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"fd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"fd_30yr_anomaly_annual_model_ec_earth_rca4",
"fd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009",
],
"fd_30yr_anomaly_annual_model_mpi_esm_lr_remo2009": [
"fd_30yr_anomaly_annual_agree_model_ensemble",
"fd_30yr_anomaly_annual_model_ec_earth_cclm4_8_17",
"fd_30yr_anomaly_annual_model_ec_earth_racmo22e",
"fd_30yr_anomaly_annual_model_ec_earth_rca4",
"fd_30yr_anomaly_annual_model_hadgem2_es_racmo22e",
],
}


def get_uncertainty_map() -> dict[str, tuple[str, str]]:
return {
"fd_annual_absolute_model_ensemble": (
"fd_annual_absolute_model_ensemble_upper_uncertainty",
"fd_annual_absolute_model_ensemble_lower_uncertainty",
),
}
Loading

0 comments on commit 01b4bc2

Please sign in to comment.