Skip to content

Commit

Permalink
Update I/O paths to new information architecture (#156)
Browse files Browse the repository at this point in the history
* export four user facing functions

* better name to avoid cnflicts

* update

* update cookbook

* update pkgdown

* improve names of exported functions

* update io paths based on new ia

* rm dead code

* update example.config.yml

* update vignette config_yml

* resolve NOTE

* rm dead code

* update error messages
  • Loading branch information
jacobvjk authored Sep 25, 2024
1 parent b58c728 commit a886d51
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 233 deletions.
59 changes: 39 additions & 20 deletions R/load_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ load_config <- function(config) {
config
}

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

get_loanbook_dir <- function(params) {
file.path(
params[["directories"]][["dir_input"]],
"loanbooks"
)
}

get_abcd_filename <- function(params) {
Expand All @@ -15,20 +22,36 @@ get_abcd_sheet <- function(params) {
params[["file_names"]][["sheet_abcd"]]
}

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

get_output_prepare_dir <- function(params) {
file.path(
params[["directories"]][["dir_output"]],
"prepare_abcd"
)
}

get_raw_dir <- function(params) {
params[["directories"]][["dir_raw"]]
get_output_matched_loanbooks_dir <- function(params) {
file.path(
params[["directories"]][["dir_output"]],
"matched_loanbooks"
)
}

get_scenario_dir <- function(params) {
params[["directories"]][["dir_scenario"]]
get_output_prio_diagnostics_dir <- function(params) {
file.path(
params[["directories"]][["dir_output"]],
"prioritized_loanbooks_and_diagnostics"
)
}

get_output_dir <- function(params) {
params[["directories"]][["dir_output"]]
get_output_analysis_dir <- function(params) {
file.path(
params[["directories"]][["dir_output"]],
"analysis"
)
}

get_scenario_tms_filename <- function(params) {
Expand Down Expand Up @@ -123,38 +146,34 @@ get_use_manual_sector_classification <- function(params) {
params[["matching"]][["manual_sector_classification"]][["use_manual_sector_classification"]]
}

get_manual_sector_classification_dir <- function(params) {
params[["matching"]][["manual_sector_classification"]][["dir_manual_sector_classification"]]
}

get_manual_sector_classification_filename <- function(params) {
params[["matching"]][["manual_sector_classification"]][["filename_manual_sector_classification"]]
}

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

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

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

get_advanced_company_indicators_path <- function(config) {
file.path(
config[["sector_split"]][["dir_advanced_company_indicators"]],
get_input_dir(config),
config[["sector_split"]][["filename_advanced_company_indicators"]]
)
}
Expand All @@ -165,14 +184,14 @@ get_advanced_company_indicators_sheet <- function(config) {

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

get_scenario_sda_path <- function(config) {
file.path(
get_scenario_dir(config),
get_input_dir(config),
get_scenario_sda_filename(config)
)
}
37 changes: 19 additions & 18 deletions R/match_loanbooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
match_loanbooks <- function(config) {
config <- load_config(config)

dir_raw <- get_raw_dir(config)
abcd_dir <- get_abcd_dir(config)
dir_matched <- get_matched_dir(config)
input_loanbooks_dir <- get_loanbook_dir(config)
output_prepare_dir <- get_output_prepare_dir(config)
output_matched_loanbooks_dir <- get_output_matched_loanbooks_dir(config)
dir.create(output_matched_loanbooks_dir, recursive = TRUE, showWarnings = FALSE)

matching_by_sector <- get_match_by_sector(config)
matching_min_score <- get_match_min_score(config)
Expand All @@ -38,18 +39,18 @@ match_loanbooks <- function(config) {
}

# validate config values----
stop_if_not_length(dir_raw, 1L)
stop_if_not_inherits(dir_raw, "character")
stop_if_dir_not_found(dir_raw, desc = "Raw loanbook")
stop_if_not_length(input_loanbooks_dir, 1L)
stop_if_not_inherits(input_loanbooks_dir, "character")
stop_if_dir_not_found(input_loanbooks_dir, desc = "Input - loanbooks")

stop_if_not_length(abcd_dir, 1L)
stop_if_not_inherits(abcd_dir, "character")
stop_if_dir_not_found(abcd_dir, desc = "ABCD data")
stop_if_file_not_found(file.path(abcd_dir, "abcd_final.csv"), desc = "ABCD final")
stop_if_not_length(output_prepare_dir, 1L)
stop_if_not_inherits(output_prepare_dir, "character")
stop_if_dir_not_found(output_prepare_dir, desc = "Output - prepare ABCD")
stop_if_file_not_found(file.path(output_prepare_dir, "abcd_final.csv"), desc = "ABCD final")

stop_if_not_length(dir_matched, 1L)
stop_if_not_inherits(dir_matched, "character")
stop_if_dir_not_found(dir_matched, desc = "Matched loanbook")
stop_if_not_length(output_matched_loanbooks_dir, 1L)
stop_if_not_inherits(output_matched_loanbooks_dir, "character")
stop_if_dir_not_found(output_matched_loanbooks_dir, desc = "Output - Matched loanbooks")

stop_if_not_length(matching_by_sector, 1L)
stop_if_not_inherits(matching_by_sector, "logical")
Expand Down Expand Up @@ -85,7 +86,7 @@ match_loanbooks <- function(config) {

## load abcd----
abcd <- readr::read_csv(
file.path(abcd_dir, "abcd_final.csv"),
file.path(output_prepare_dir, "abcd_final.csv"),
col_select = dplyr::all_of(cols_abcd),
col_types = col_types_abcd_final
)
Expand All @@ -100,11 +101,11 @@ match_loanbooks <- function(config) {
}

## load raw loan books----
list_raw <- list.files(path = dir_raw, pattern = "[.]csv$")
stop_if_no_files_found(list_raw, dir_raw, "dir_raw", "raw loan book CSVs")
list_raw <- list.files(path = input_loanbooks_dir, pattern = "[.]csv$")
stop_if_no_files_found(list_raw, input_loanbooks_dir, "dir_input", "raw loan book CSVs")

raw_lbk <- readr::read_csv(
file = file.path(dir_raw, list_raw),
file = file.path(input_loanbooks_dir, list_raw),
col_types = col_types_raw,
id = "group_id"
) %>%
Expand Down Expand Up @@ -154,7 +155,7 @@ match_loanbooks <- function(config) {
## write matched data to file----
matched_lbk_i %>%
readr::write_csv(
file = file.path(dir_matched, glue::glue("matched_lbk_{group_name}.csv")),
file = file.path(output_matched_loanbooks_dir, glue::glue("matched_lbk_{group_name}.csv")),
na = ""
)
cli::cli_progress_update()
Expand Down
47 changes: 23 additions & 24 deletions R/plot_aggregate_loanbooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ plot_aggregate_loanbooks <- function(config) {
config <- load_config(config)

# paths
input_path_matched <- get_matched_dir(config)
output_path <- get_output_dir(config)
output_path_aggregated <- file.path(output_path, "aggregated")
output_analysis_dir <- get_output_analysis_dir(config)
output_analysis_aggregated_dir <- file.path(output_analysis_dir, "aggregated")

# project parameters
scenario_source_input <- get_scenario_source(config)
Expand All @@ -30,13 +29,13 @@ plot_aggregate_loanbooks <- function(config) {

# 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")
output_analysis_aggregated_dir <- file.path(output_analysis_dir, sector_split_type_select, "aggregated")
}

by_group <- get_by_group(config)
by_group <- check_and_prepare_by_group(by_group)

dir.create(output_path_aggregated, recursive = TRUE, showWarnings = FALSE)
dir.create(output_analysis_aggregated_dir, recursive = TRUE, showWarnings = FALSE)


# load required data----
Expand Down Expand Up @@ -70,7 +69,7 @@ plot_aggregate_loanbooks <- function(config) {
company_aggregated_alignment_net <-
readr::read_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("company_exposure_net_aggregate_alignment{file_by_group}.csv")
),
col_types = col_types_company_aggregated_alignment,
Expand All @@ -80,7 +79,7 @@ plot_aggregate_loanbooks <- function(config) {
company_aggregated_alignment_bo_po <-
readr::read_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("company_exposure_bo_po_aggregate_alignment{file_by_group}.csv")
),
col_types = col_types_company_aggregated_alignment,
Expand All @@ -91,7 +90,7 @@ plot_aggregate_loanbooks <- function(config) {
loanbook_exposure_aggregated_alignment_bo_po <-
readr::read_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("loanbook_exposure_bo_po_aggregate_alignment{file_by_group}.csv")
),
col_types = readr::cols(
Expand All @@ -111,7 +110,7 @@ plot_aggregate_loanbooks <- function(config) {
loanbook_exposure_aggregated_alignment_net <-
readr::read_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("loanbook_exposure_net_aggregate_alignment{file_by_group}.csv")
),
col_types = readr::cols(
Expand Down Expand Up @@ -158,7 +157,7 @@ plot_aggregate_loanbooks <- function(config) {
data_sankey_sector %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("data_{output_file_sankey_sector}.csv")
),
na = ""
Expand All @@ -167,7 +166,7 @@ plot_aggregate_loanbooks <- function(config) {
plot_sankey(
data_sankey_sector,
group_var = by_group,
save_png_to = path.expand(output_path_aggregated),
save_png_to = path.expand(output_analysis_aggregated_dir),
png_name = glue::glue("plot_{output_file_sankey_sector}.png"),
nodes_order_from_data = TRUE
)
Expand Down Expand Up @@ -200,7 +199,7 @@ plot_aggregate_loanbooks <- function(config) {
data_sankey_company_sector %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("data_{output_file_sankey_company_sector}.csv")
),
na = ""
Expand All @@ -209,7 +208,7 @@ plot_aggregate_loanbooks <- function(config) {
plot_sankey(
data_sankey_company_sector,
group_var = by_group,
save_png_to = path.expand(output_path_aggregated),
save_png_to = path.expand(output_analysis_aggregated_dir),
png_name = glue::glue("plot_{output_file_sankey_company_sector}.png")
)
}
Expand Down Expand Up @@ -243,7 +242,7 @@ plot_aggregate_loanbooks <- function(config) {
data_scatter_alignment_exposure %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("data_{output_file_alignment_exposure}.csv")
),
na = ""
Expand All @@ -259,7 +258,7 @@ plot_aggregate_loanbooks <- function(config) {

ggplot2::ggsave(
filename = glue::glue("plot_{output_file_alignment_exposure}.png"),
path = output_path_aggregated,
path = output_analysis_aggregated_dir,
width = 8,
height = 5,
dpi = 300,
Expand Down Expand Up @@ -302,7 +301,7 @@ plot_aggregate_loanbooks <- function(config) {
data_scatter_automotive_group %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("data_{output_file_scatter_sector}.csv")
),
na = ""
Expand All @@ -319,7 +318,7 @@ plot_aggregate_loanbooks <- function(config) {
)
ggplot2::ggsave(
filename = glue::glue("plot_{output_file_scatter_sector}.png"),
path = output_path_aggregated,
path = output_analysis_aggregated_dir,
width = 8,
height = 5
)
Expand Down Expand Up @@ -356,7 +355,7 @@ plot_aggregate_loanbooks <- function(config) {
data_scatter_power_group %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
glue::glue("data_{output_file_scatter_sector}.csv")
),
na = ""
Expand All @@ -374,7 +373,7 @@ plot_aggregate_loanbooks <- function(config) {

ggplot2::ggsave(
filename = glue::glue("plot_{output_file_scatter_sector}.png"),
path = output_path_aggregated,
path = output_analysis_aggregated_dir,
width = 8,
height = 5
)
Expand All @@ -399,7 +398,7 @@ plot_aggregate_loanbooks <- function(config) {
unique()

for (i in dirs_for_by_group) {
dir.create(file.path(output_path_aggregated, i), recursive = TRUE, showWarnings = FALSE)
dir.create(file.path(output_analysis_aggregated_dir, i), recursive = TRUE, showWarnings = FALSE)
}
}

Expand Down Expand Up @@ -441,7 +440,7 @@ plot_aggregate_loanbooks <- function(config) {
data_scatter_automotive_company_i %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
i,
glue::glue("data_scatter_automotive_company_by_{by_group}_{i}.csv")
),
Expand All @@ -462,7 +461,7 @@ plot_aggregate_loanbooks <- function(config) {

ggplot2::ggsave(
filename = glue::glue("plot_scatter_automotive_company_by_{by_group}_{i}.png"),
path = file.path(output_path_aggregated, i),
path = file.path(output_analysis_aggregated_dir, i),
width = 8,
height = 5
)
Expand Down Expand Up @@ -504,7 +503,7 @@ plot_aggregate_loanbooks <- function(config) {
data_scatter_power_company_i %>%
readr::write_csv(
file = file.path(
output_path_aggregated,
output_analysis_aggregated_dir,
i,
glue::glue("data_scatter_power_company_by_{by_group}_{i}.csv")
),
Expand All @@ -525,7 +524,7 @@ plot_aggregate_loanbooks <- function(config) {

ggplot2::ggsave(
filename = glue::glue("plot_scatter_power_company_by_{by_group}_{i}.png"),
path = file.path(output_path_aggregated, i),
path = file.path(output_analysis_aggregated_dir, i),
width = 8,
height = 5
)
Expand Down
Loading

0 comments on commit a886d51

Please sign in to comment.