Skip to content

Commit

Permalink
Config loading utils (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman authored Aug 19, 2024
1 parent 1321707 commit c3ca19d
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 165 deletions.
178 changes: 178 additions & 0 deletions R/load_config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
load_config <- function(config) {
if (is.character(config)) return(config::get(file = config))
config
}

get_abcd_dir <- function(params) {
params[["directories"]][["dir_abcd"]]
}

get_abcd_filename <- function(params) {
params[["file_names"]][["filename_abcd"]]
}

get_abcd_sheet <- function(params) {
params[["file_names"]][["sheet_abcd"]]
}

get_matched_dir <- function(params) {
params[["directories"]][["dir_matched"]]
}

get_raw_dir <- function(params) {
params[["directories"]][["dir_raw"]]
}

get_scenario_dir <- function(params) {
params[["directories"]][["dir_scenario"]]
}

get_output_dir <- function(params) {
params[["directories"]][["dir_output"]]
}

get_scenario_tms_filename <- function(params) {
params[["file_names"]][["filename_scenario_tms"]]
}

get_scenario_sda_filename <- function(params) {
params[["file_names"]][["filename_scenario_sda"]]
}

get_start_year <- function(params) {
params[["project_parameters"]][["start_year"]]
}

get_time_frame <- function(params) {
params[["project_parameters"]][["time_frame"]]
}

get_remove_inactive_companies <- function(params) {
params[["project_parameters"]][["remove_inactive_companies"]]
}

get_scenario_source <- function(params) {
params[["project_parameters"]][["scenario_source"]]
}

get_scenario_select <- function(params) {
params[["project_parameters"]][["scenario_select"]]
}

get_region_select <- function(params) {
params[["project_parameters"]][["region_select"]]
}

get_aggregate_alignment_metric_by_group <- function(params) {
params[["aggregate_alignment_metric"]][["by_group"]]
}

get_match_priority <- function(params) {
params[["match_prioritize"]][["priority"]]
}

get_match_by_sector <- function(params) {
params[["matching"]][["params_match_name"]][["by_sector"]]
}

get_match_min_score <- function(params) {
params[["matching"]][["params_match_name"]][["min_score"]]
}

get_match_method <- function(params) {
params[["matching"]][["params_match_name"]][["method"]]
}

get_match_p <- function(params) {
params[["matching"]][["params_match_name"]][["p"]]
}

get_match_overwrite <- function(params) {
params[["matching"]][["params_match_name"]][["overwrite"]]
}

get_match_join_id <- function(params) {
params[["matching"]][["params_match_name"]][["join_id"]]
}

get_match_plot_width <- function(params) {
params[["match_success_rate"]][["plot_width"]]
}

get_match_plot_height <- function(params) {
params[["match_success_rate"]][["plot_height"]]
}

get_match_plot_units <- function(params) {
params[["match_success_rate"]][["plot_units"]]
}

get_match_plot_resolution <- function(params) {
params[["match_success_rate"]][["plot_resolution"]]
}

get_apply_sector_split <- function(params) {
params[["sector_split"]][["apply_sector_split"]]
}

get_sector_split_type <- function(params) {
params[["sector_split"]][["sector_split_type"]]
}

get_use_maunal_sector_classification <- function(params) {
params[["matching"]][["own_sector_classification"]][["use_own_sector_classification"]]
}

get_manual_sector_classification_dir <- function(params) {
params[["matching"]][["own_sector_classification"]][["dir_own_sector_classification"]]
}

get_manual_sector_classification_filename <- function(params) {
params[["matching"]][["own_sector_classification"]][["filename_own_sector_classification"]]
}

get_manual_sector_classification_path <- function(params) {
file.path(
get_manual_sector_classification_dir(params),
get_manual_sector_classification_filename(params)
)
}

get_abcd_path <- function(config) {
file.path(
get_abcd_dir(config),
get_abcd_filename(config)
)
}

get_sector_split_path <- function(config) {
file.path(
config[["sector_split"]][["dir_split_company_id"]],
config[["sector_split"]][["filename_split_company_id"]]
)
}

get_advanced_company_indicators_path <- function(config) {
file.path(
config[["sector_split"]][["dir_advanced_company_indicators"]],
config[["sector_split"]][["filename_advanced_company_indicators"]]
)
}

get_advanced_company_indicators_sheet <- function(config) {
config[["sector_split"]][["sheet_advanced_company_indicators"]]
}

get_scenario_tms_path <- function(config) {
file.path(
get_scenario_dir(config),
get_scenario_tms_filename(config)
)
}

get_scenario_sda_path <- function(config) {
file.path(
get_scenario_dir(config),
get_scenario_sda_filename(config)
)
}
30 changes: 16 additions & 14 deletions R/plot_aggregate_loanbooks.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
#' plot_aggregate_loanbooks
#'
#' @param config either a single string defining the path to a config YML file
#' or a list object that contains the appropriate config params
#'
#' @return `NULL` (called for side effects)
#'
#' @importFrom dplyr %>%
#' @importFrom rlang .data .env

plot_aggregate_loanbooks <- function() {
# load config----
config <- config::get()
plot_aggregate_loanbooks <- function(config) {
config <- load_config(config)

# paths
input_path_matched <- config$directories$dir_matched
output_path <- config$directories$dir_output
input_path_matched <- get_matched_dir(config)
output_path <- get_output_dir(config)
output_path_aggregated <- file.path(output_path, "aggregated")

# project parameters
scenario_source_input <- config$project_parameters$scenario_source
scenario_select <- config$project_parameters$scenario_select
region_select <- config$project_parameters$region_select
start_year <- config$project_parameters$start_year
time_frame_select <- config$project_parameters$time_frame
apply_sector_split <- config$sector_split$apply_sector_split
scenario_source_input <- get_scenario_source(config)
scenario_select <- get_scenario_select(config)
region_select <- get_region_select(config)
start_year <- get_start_year(config)
time_frame_select <- get_time_frame(config)
apply_sector_split <- get_apply_sector_split(config)
if (is.null(apply_sector_split)) { apply_sector_split <- FALSE }
sector_split_type_select <- config$sector_split$sector_split_type
remove_inactive_companies <- config$prepare_abcd$remove_inactive_companies
sector_split_type_select <- get_sector_split_type(config)
remove_inactive_companies <- get_remove_inactive_companies(config)
if (is.null(remove_inactive_companies)) { remove_inactive_companies <- FALSE }

# if a sector split is applied, write results into a directory that states the type
if (apply_sector_split) {
output_path_aggregated <- file.path(output_path, sector_split_type_select, "aggregated")
}

by_group <- config$aggregate_alignment_metric$by_group
by_group <- get_aggregate_alignment_metric_by_group(config)
if (!is.null(by_group) && by_group == "NULL") { by_group <- NULL }
if (length(by_group) >= 1) {
by_group <- gsub(" ", "", unlist(strsplit(by_group, split = ",")))
Expand Down
46 changes: 20 additions & 26 deletions R/prepare_abcd.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
prepare_abcd <- function() {
# load config----
config_dir <- config::get("directories")
config_files <- config::get("file_names")
prepare_abcd <- function(config) {
config <- load_config(config)

path_abcd <- file.path(config_dir$dir_abcd, config_files$filename_abcd)
sheet_abcd <- config_files$sheet_abcd
abcd_dir <- get_abcd_dir(config)
path_abcd <- get_abcd_path(config)
sheet_abcd <- get_abcd_sheet(config)

config_prepare_abcd <- config::get("prepare_abcd")

prepare_abcd_rm_inactive_companies <- config_prepare_abcd$remove_inactive_companies

config_project_parameters <- config::get("project_parameters")

project_parameters_start_year <- config_project_parameters$start_year
project_parameters_time_frame <- config_project_parameters$time_frame
remove_inactive_companies <- get_remove_inactive_companies(config)
start_year <- get_start_year(config)
time_frame <- get_time_frame(config)

# validate config values----
stop_if_not_length(path_abcd, 1L)
Expand All @@ -24,16 +18,16 @@ prepare_abcd <- function() {
stop_if_not_inherits(sheet_abcd, "character")
stop_if_sheet_not_found(sheet_abcd, path_abcd)

if (!is.null(prepare_abcd_rm_inactive_companies)) {
stop_if_not_length(prepare_abcd_rm_inactive_companies, 1L)
stop_if_not_inherits(prepare_abcd_rm_inactive_companies, "logical")
if (!is.null(remove_inactive_companies)) {
stop_if_not_length(remove_inactive_companies, 1L)
stop_if_not_inherits(remove_inactive_companies, "logical")
}

stop_if_not_length(project_parameters_start_year, 1L)
stop_if_not_inherits(project_parameters_start_year, "integer")
stop_if_not_length(start_year, 1L)
stop_if_not_inherits(start_year, "integer")

stop_if_not_length(project_parameters_time_frame, 1L)
stop_if_not_inherits(project_parameters_time_frame, "integer")
stop_if_not_length(time_frame, 1L)
stop_if_not_inherits(time_frame, "integer")


# load data----
Expand Down Expand Up @@ -120,11 +114,11 @@ prepare_abcd <- function() {
return(data)
}

if (prepare_abcd_rm_inactive_companies) {
if (remove_inactive_companies) {
abcd_keep <- abcd %>%
rm_inactive_companies(
start_year = project_parameters_start_year,
time_frame = project_parameters_time_frame
start_year = start_year,
time_frame = time_frame
)

abcd_removed <- abcd %>%
Expand All @@ -136,7 +130,7 @@ prepare_abcd <- function() {
# write removed inactive companies to file for inspection
abcd_removed %>%
readr::write_csv(
file.path(config_dir$dir_abcd, "abcd_removed_inactive_companies.csv"),
file.path(abcd_dir, "abcd_removed_inactive_companies.csv"),
na = ""
)

Expand All @@ -148,7 +142,7 @@ prepare_abcd <- function() {
# write final version of abcd to file for use PACTA analysis
abcd %>%
readr::write_csv(
file.path(config_dir$dir_abcd, "abcd_final.csv"),
file.path(abcd_dir, "abcd_final.csv"),
na = ""
)
}
40 changes: 15 additions & 25 deletions R/prepare_sector_split.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,37 @@
# The intended use case is to provide a rule by which to allocate loans to
# multiple sectors, for loan books where such loan allocation is ambiguous.

prepare_sector_split <- function() {
# load config----
config_dir <- config::get("directories")
config_files <- config::get("file_names")
config_project_parameters <- config::get("project_parameters")
config_prepare_sector_split <- config::get("sector_split")
prepare_sector_split <- function(config) {
config <- load_config(config)

dir_matched <- config_dir$dir_matched
dir_matched <- get_matched_dir(config)
abcd_dir <- get_abcd_dir(config)

path_sector_split <- file.path(
config_prepare_sector_split$dir_split_company_id,
config_prepare_sector_split$filename_split_company_id
)

path_advanced_company_indicators <- file.path(
config_prepare_sector_split$dir_advanced_company_indicators,
config_prepare_sector_split$filename_advanced_company_indicators
)

sheet_advanced_company_indicators <- config_prepare_sector_split$sheet_advanced_company_indicators
sector_split_path <- get_sector_split_path(config)
advanced_company_indicators_path <- get_advanced_company_indicators_path(config)
sheet_advanced_company_indicators <- get_advanced_company_indicators_sheet(config)

start_year <- config_project_parameters$start_year
time_frame <- config_project_parameters$time_frame
start_year <- get_start_year(config)
time_frame <- get_time_frame(config)
remove_inactive_companies <- get_remove_inactive_companies(config)

## load input data----
advanced_company_indicators_raw <- readxl::read_xlsx(
path = path_advanced_company_indicators,
path = advanced_company_indicators_path,
sheet = sheet_advanced_company_indicators
)

company_ids_primary_energy_split <- readr::read_csv(
path_sector_split,
sector_split_path,
col_types = readr::cols_only(company_id = "d"),
col_select = "company_id"
) %>%
dplyr::pull(.data$company_id)

# optional: remove inactive companies
if (config_project_parameters$remove_inactive_companies) {
if (remove_inactive_companies) {
abcd_removed_inactive_companies <- readr::read_csv(
file.path(config_dir$dir_abcd, "abcd_removed_inactive_companies.csv"),
file.path(abcd_dir, "abcd_removed_inactive_companies.csv"),
col_select = dplyr::all_of(cols_abcd),
col_types = col_types_abcd_final
)
Expand Down Expand Up @@ -174,7 +164,7 @@ prepare_sector_split <- function() {
dplyr::filter(.data$year == .env$start_year)

# optional: remove inactive companies
if (config_project_parameters$remove_inactive_companies) {
if (remove_inactive_companies) {
advanced_company_indicators <- advanced_company_indicators %>%
dplyr::anti_join(abcd_removed_inactive_companies, by = "company_id")
}
Expand Down
Loading

0 comments on commit c3ca19d

Please sign in to comment.