Skip to content

Commit

Permalink
output sub directory for every user facing function (#179)
Browse files Browse the repository at this point in the history
* output sub directory for every user facing function

* align names

* input checks

* update references to output directories in documentation

---------

Co-authored-by: CJ Yetman <[email protected]>
  • Loading branch information
jacobvjk and cjyetman authored Oct 30, 2024
1 parent 15bcce5 commit 7f441a3
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 55 deletions.
13 changes: 13 additions & 0 deletions R/analyse.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
#' @examples
#' # TODO
analyse <- function(config) {
config <- load_config(config)

output_analysis_dir <- get_output_analysis_dir(config)

stop_if_not_length(output_analysis_dir, 1L)
stop_if_not_inherits(output_analysis_dir, "character")

if (dir.exists(output_analysis_dir)) {
warning("Output directory `dir_analysis` already exists. The existing directory will be removed and replaced with the output of the current run.")
unlink(output_analysis_dir, recursive = TRUE)
}
dir.create(output_analysis_dir, recursive = TRUE, showWarnings = FALSE)

run_pacta(config)
run_aggregate_alignment_metric(config)
plot_aggregate_loanbooks(config)
Expand Down
24 changes: 4 additions & 20 deletions R/load_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,20 @@ get_abcd_sheet <- function(params) {
params[["file_names"]][["sheet_abcd"]]
}

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

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

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

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

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

get_scenario_tms_filename <- function(params) {
Expand Down
10 changes: 10 additions & 0 deletions R/match_loanbooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@
match_loanbooks <- function(config) {
config <- load_config(config)

# input paths for match_loanbooks
input_loanbooks_dir <- get_loanbook_dir(config)
output_prepare_dir <- get_output_prepare_dir(config)

# output path for match_loanbooks
output_matched_loanbooks_dir <- get_output_matched_loanbooks_dir(config)

if (dir.exists(output_matched_loanbooks_dir)) {
warning(
"Output directory `dir_matched_loanbooks` already exists. The existing directory will be removed and replaced with the output of the current run. NOTE: This includes any manually matched loan book files you may have placed in this directory."
)
unlink(output_matched_loanbooks_dir, recursive = TRUE)
}
dir.create(output_matched_loanbooks_dir, recursive = TRUE, showWarnings = FALSE)

matching_by_sector <- get_match_by_sector(config)
Expand Down
3 changes: 0 additions & 3 deletions R/plot_aggregate_loanbooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ plot_aggregate_loanbooks <- function(config) {
by_group <- get_by_group(config)
by_group <- check_and_prepare_by_group(by_group)

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


# load required data----

if (is.null(by_group)) {
Expand Down
13 changes: 13 additions & 0 deletions R/prepare_abcd.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
#' @examples
#' # TODO
prepare_abcd <- function(config) {
config <- load_config(config)

output_prepare_dir <- get_output_prepare_dir(config)

stop_if_not_length(output_prepare_dir, 1L)
stop_if_not_inherits(output_prepare_dir, "character")

if (dir.exists(output_prepare_dir)) {
warning("Output directory `dir_prepared_abcd` already exists. The existing directory will be removed and replaced with the output of the current run.")
unlink(output_prepare_dir, recursive = TRUE)
}
dir.create(output_prepare_dir, recursive = TRUE, showWarnings = FALSE)

remove_inactive_companies(config)
prepare_sector_split(config)
}
5 changes: 1 addition & 4 deletions R/prepare_sector_split.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
prepare_sector_split <- function(config) {
config <- load_config(config)

# input/output paths for prepare_sector_split
output_prepare_dir <- get_output_prepare_dir(config)

dir.create(output_prepare_dir, recursive = TRUE, showWarnings = FALSE)

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)

Expand Down
13 changes: 13 additions & 0 deletions R/prioritise_and_diagnose.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
#' @examples
#' # TODO
prioritise_and_diagnose <- function(config) {
config <- load_config(config)

output_prio_diagnostics_dir <- get_output_prio_diagnostics_dir(config)

stop_if_not_length(output_prio_diagnostics_dir, 1L)
stop_if_not_inherits(output_prio_diagnostics_dir, "character")

if (dir.exists(output_prio_diagnostics_dir)) {
warning("Output directory `dir_prioritized_loanbooks_and_diagnostics` already exists. The existing directory will be removed and replaced with the output of the current run.")
unlink(output_prio_diagnostics_dir, recursive = TRUE)
}
dir.create(output_prio_diagnostics_dir, recursive = TRUE, showWarnings = FALSE)

run_match_prioritize(config)
run_calculate_match_success_rate(config)
run_calculate_loanbook_coverage(config)
Expand Down
4 changes: 2 additions & 2 deletions R/remove_inactive_companies.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
remove_inactive_companies <- function(config) {
config <- load_config(config)

# input path for remove_inactive_companies
path_abcd <- get_abcd_path(config)
sheet_abcd <- get_abcd_sheet(config)

# output path for remove_inactive_companies
output_prepare_dir <- get_output_prepare_dir(config)

dir.create(output_prepare_dir, recursive = TRUE, showWarnings = FALSE)

remove_inactive_companies <- get_remove_inactive_companies(config)
start_year <- get_start_year(config)
time_frame <- get_time_frame(config)
Expand Down
1 change: 1 addition & 0 deletions R/run_calculate_loanbook_coverage.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
run_calculate_loanbook_coverage <- function(config) {
config <- load_config(config)

# input/output paths for loan book coverage
output_prepare_dir <- get_output_prepare_dir(config)
output_prio_diagnostics_dir <- get_output_prio_diagnostics_dir(config)

Expand Down
1 change: 1 addition & 0 deletions R/run_calculate_match_success_rate.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
run_calculate_match_success_rate <- function(config) {
config <- load_config(config)

# input/output paths for match success rate
dir_input <- get_input_dir(config)
input_loanbooks_dir <- get_loanbook_dir(config)
output_prio_diagnostics_dir <- get_output_prio_diagnostics_dir(config)
Expand Down
3 changes: 2 additions & 1 deletion R/run_match_prioritize.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
run_match_prioritize <- function(config) {
config <- load_config(config)

# input paths for match prioritize
output_matched_loanbooks_dir <- get_output_matched_loanbooks_dir(config)
output_prepare_dir <- get_output_prepare_dir(config)

# output path for match prioritize
output_prio_diagnostics_dir <- get_output_prio_diagnostics_dir(config)
dir.create(output_prio_diagnostics_dir, recursive = TRUE, showWarnings = FALSE)

match_prio_priority <- get_match_priority(config)

Expand Down
5 changes: 4 additions & 1 deletion example.config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
default:
directories:
dir_input: "path/to/input_folder"
dir_output: "path/to/output_folder"
dir_prepared_abcd: "path/to/prepared_abcd"
dir_matched_loanbooks: "path/to/matched_loanbooks"
dir_prioritized_loanbooks_and_diagnostics: "path/to/prioritized_loanbooks_and_diagnostics"
dir_analysis: "path/to/analysis"
file_names:
filename_scenario_tms: "scenarios_2022_tms.csv"
filename_scenario_sda: "scenarios_2022_sda.csv"
Expand Down
35 changes: 31 additions & 4 deletions vignettes/config_yml.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ The `directories`{.yaml} section contains options to define locally accessible p
```yaml
directories:
dir_input: "~/Desktop/test/input"
dir_output: "~/Desktop/test/output"
dir_prepared_abcd: "~/Desktop/test/prepared_abcd"
dir_matched_loanbooks: "~/Desktop/test/matched_loanbooks"
dir_prioritized_loanbooks_and_diagnostics: "~/Desktop/test/prioritized_loanbooks_and_diagnostics"
dir_analysis: "~/Desktop/test/analysis"
```
#### dir_input
Expand All @@ -44,12 +47,36 @@ The `directories`{.yaml} section contains options to define locally accessible p
dir_input: "~/Desktop/test/input"
```

#### dir_output
#### dir_prepared_abcd

`dir_output`{.yaml} is a path to a directory where the outputs should be saved. It must be a single string/character value, and it must refer to a valid, accessible, local directory. As an example:
`dir_prepared_abcd`{.yaml} is a path to a directory where the outputs of the function `prepare_abcd()` should be saved. It must be a single string/character value, and it must refer to a valid, accessible, local directory. As an example:

```yaml
dir_output: "~/Desktop/test/output"
dir_prepared_abcd: "~/Desktop/test/prepared_abcd"
```

#### dir_matched_loanbooks

`dir_matched_loanbooks`{.yaml} is a path to a directory where the outputs of the function `match_loanbooks()` should be saved. It must be a single string/character value, and it must refer to a valid, accessible, local directory. As an example:

```yaml
dir_matched_loanbooks: "~/Desktop/test/matched_loanbooks"
```

#### dir_prioritized_loanbooks_and_diagnostics

`dir_prioritized_loanbooks_and_diagnostics`{.yaml} is a path to a directory where the outputs of the function `prioritise_and_diagnose()` should be saved. It must be a single string/character value, and it must refer to a valid, accessible, local directory. As an example:

```yaml
dir_prioritized_loanbooks_and_diagnostics: "~/Desktop/test/prioritized_loanbooks_and_diagnostics"
```

#### dir_analysis

`dir_analysis`{.yaml} is a path to a directory where the outputs of the function `analyse()` should be saved. It must be a single string/character value, and it must refer to a valid, accessible, local directory. As an example:

```yaml
dir_analysis: "~/Desktop/test/analysis"
```

## file_names:
Expand Down
Loading

0 comments on commit 7f441a3

Please sign in to comment.