Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/53 convert to package #54

Merged
merged 42 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ebae0f5
Add basic package infrastructure
AlexAxthelm May 13, 2024
6174d9d
Add authors
AlexAxthelm May 13, 2024
f9d27df
Add R package Actions
AlexAxthelm May 13, 2024
a77dde7
Increment version number to 0.0.0.9001
AlexAxthelm May 13, 2024
14993d1
Reindent DESCRIPTION
AlexAxthelm May 13, 2024
6ec71ec
Move script files
AlexAxthelm May 13, 2024
3fba9e0
Convert scripts to functions
AlexAxthelm May 13, 2024
fda5aec
Use new invokation pattern in docker image
AlexAxthelm May 13, 2024
dd89f4d
Fix issues found in local testing
AlexAxthelm May 13, 2024
b90a3f3
Merge branch 'main' into refactor/53-convert-to-package
AlexAxthelm May 13, 2024
fd5dfaa
Add logger functions to namespace and use in code
AlexAxthelm May 13, 2024
cd26297
Low-hanging fruit linting
AlexAxthelm May 13, 2024
f3b0449
linting in run_audit
AlexAxthelm May 13, 2024
0ffcd81
Add minimal loggin to run_pacta
AlexAxthelm May 13, 2024
a0003cf
Low-fruit linting in run_analysis
AlexAxthelm May 13, 2024
39bdd4d
namespace dependencies
AlexAxthelm May 13, 2024
193913c
linting in run_analysis function
AlexAxthelm May 13, 2024
3b29c13
Update docker-compose
AlexAxthelm May 13, 2024
c9137e7
Use branch of pacta.portfolio.audit
AlexAxthelm May 13, 2024
83e6b41
Fix non-standard ref syntax
AlexAxthelm May 13, 2024
a199959
update Rbuildignore
AlexAxthelm May 14, 2024
2243118
reflow comment
AlexAxthelm May 14, 2024
41b933c
Reflow long line
AlexAxthelm May 14, 2024
7c931fc
Refactor dplyr::mutate to use base commands
AlexAxthelm May 14, 2024
bb60f15
Shorten variable name
AlexAxthelm May 14, 2024
e65e5cc
Extract Equity calculation to separate function
AlexAxthelm May 14, 2024
58c8178
Initial genericization
AlexAxthelm May 14, 2024
48a1266
Extract Bonds calculations to new function
AlexAxthelm May 14, 2024
c428d36
Initial generalization
AlexAxthelm May 14, 2024
18e956d
Generalize filenames
AlexAxthelm May 14, 2024
33d4e45
genericize filenames
AlexAxthelm May 14, 2024
d895f43
add check for contents prior to tech share
AlexAxthelm May 14, 2024
4ff2a8d
Include data check before geography
AlexAxthelm May 14, 2024
b65d638
Do not run ownership calculations for bonds
AlexAxthelm May 14, 2024
0702748
Merge branch 'extract-bonds' into extract-equity
AlexAxthelm May 14, 2024
2ee3ced
Light cleaning
AlexAxthelm May 14, 2024
6d7eba4
Factor CFG out of internal function
AlexAxthelm May 14, 2024
3402a0a
Render Docs
AlexAxthelm May 14, 2024
c2d0fba
Update dependency to use main branch
AlexAxthelm May 20, 2024
e0c6964
Update docker-compose command
AlexAxthelm May 20, 2024
22d9efa
Remove param reading from lower function
AlexAxthelm May 20, 2024
6de8f86
factor param parsing from lower function
AlexAxthelm May 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
^.dockerignore$
^.lintr$
^.github/
^Dockerfile$
^LICENSE\.md$
^docker-compose.yml$
^input_dir/
^output_dir/
^run-pacta.sh$
^workflow.pacta.Rproj$
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
**/Dockerfile
**/*.md
**/*.Rproj
output_dir/**/*
output_dir
pacta-data/**/*
pacta-data
AlexAxthelm marked this conversation as resolved.
Show resolved Hide resolved
docker-compose.yml
22 changes: 22 additions & 0 deletions .github/workflows/R.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
# This example file will enable R language checks on push or PR to the main
# branch.
# It will also run the checks every weeknight at midnight UTC
#
# Note the @main in `uses:` on the last line. This will call the latest version
# of the workflow from the `main` brnach in the RMI-PACTA/actions repo. You can
# also specify a tag from that repo, or a commit SHA to pin action versions.
on:
pull_request:
push:
branches: [main]
schedule:
- cron: '0 0 * * 1,2,3,4,5'
workflow_dispatch:

name: R

jobs:
R-package:
name: R Package Checks
uses: RMI-PACTA/actions/.github/workflows/R.yml@main
55 changes: 39 additions & 16 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
Package: workflow.pacta
Version: 0.0.0.9000
Type: PACTA
Description: Run PACTA.
Depends:
dplyr,
jsonlite,
logger,
pacta.portfolio.allocate,
pacta.portfolio.audit,
pacta.portfolio.import,
pacta.portfolio.utils,
readr
Title: Run PACTA
Version: 0.0.0.9001
Authors@R:
c(person(given = "CJ",
family = "Yetman",
role = c("aut", "ctr"),
email = "[email protected]",
comment = c(ORCID = "0000-0001-5099-9500")),
person(given = "Jackson",
family = "Hoffart",
email = "[email protected]",
role = c("aut", "ctr"),
comment = c(ORCID = "0000-0002-8600-5042")),
person(given = "Alex",
family = "Axthelm",
role = c("aut", "cre", "ctr"),
email = "[email protected]",
comment = c(ORCID = "0000-0001-8579-8565")),
person(given = "RMI",
role = c("cph", "fnd"),
email = "[email protected]"))
Description: Run the PACTA Analysis.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
dplyr,
jsonlite,
logger,
pacta.portfolio.allocate,
pacta.portfolio.audit,
pacta.portfolio.import,
pacta.portfolio.utils,
readr
Remotes:
RMI-PACTA/pacta.portfolio.allocate,
RMI-PACTA/pacta.portfolio.audit,
RMI-PACTA/pacta.portfolio.import,
RMI-PACTA/pacta.portfolio.utils
RMI-PACTA/pacta.portfolio.allocate,
RMI-PACTA/pacta.portfolio.audit@fix/45-pass-currencies-explicitly,
AlexAxthelm marked this conversation as resolved.
Show resolved Hide resolved
RMI-PACTA/pacta.portfolio.import,
RMI-PACTA/pacta.portfolio.utils
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ RUN Rscript -e "\
pak::pak(c(gh_pkgs)); \
"

COPY . /
COPY . /workflow.pacta/

RUN Rscript -e "pak::local_install(root = '/workflow.pacta')"

# set default run behavior
ENTRYPOINT ["/run-pacta.sh"]
CMD ["input_dir/default_config.json"]
ENTRYPOINT ["/workflow.pacta/run-pacta.sh"]
CMD ["/workflow.pacta/input_dir/default_config.json"]
23 changes: 2 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
MIT License

Copyright (c) 2023 RMI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
YEAR: 2024
COPYRIGHT HOLDER: RMI
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2024 RMI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by roxygen2: do not edit by hand

importFrom(logger,log_debug)
importFrom(logger,log_error)
importFrom(logger,log_info)
importFrom(logger,log_trace)
importFrom(logger,log_warn)
172 changes: 172 additions & 0 deletions R/calc_weights_and_outputs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
calc_weights_and_outputs <- function(
total_portfolio,
portfolio_type,
output_dir,
data_dir,
equity_market_list,
scenario_sources_list,
scenario_geographies_list,
sector_list,
has_map
) {

log_info("Starting {portfolio_type} calculations.")

log_debug("Subsetting {portfolio_type} portfolio.")
port_raw_all <- pacta.portfolio.allocate::create_portfolio_subset(
portfolio = total_portfolio,
portfolio_type = portfolio_type
)

if (pacta.portfolio.utils::data_check(port_raw_all)) {
log_info(
"{portfolio_type} portfolio has data. ",
"Beginning {portfolio_type} calculations."
)

log_debug("Calculating {portfolio_type} portfolio weights.")
port <- pacta.portfolio.allocate::calculate_weights(
portfolio = port_raw_all,
portfolio_type = portfolio_type
)

log_debug(
"Merging ABCD data from database into {portfolio_type} portfolio."
)
if (portfolio_type == "Bonds") {
id_col <- "credit_parent_ar_company_id"
} else {
id_col <- "id"
}
id_col <- "credit_parent_ar_company_id"
port <- pacta.portfolio.allocate::merge_abcd_from_db(
portfolio = port,
portfolio_type = portfolio_type,
db_dir = data_dir,
equity_market_list = equity_market_list,
scenario_sources_list = scenario_sources_list,
scenario_geographies_list = scenario_geographies_list,
sector_list = sector_list,
id_col = id_col
)

# Portfolio weight methodology
log_info("Calculating portfolio weight methodology.")
log_debug("Calculating portfolio weight allocation.")
port_pw <- pacta.portfolio.allocate::port_weight_allocation(port)

log_debug("Aggregating companies for portfolio weight calculation.")
company_pw <- pacta.portfolio.allocate::aggregate_company(port_pw)

log_debug("Aggregating portfolio for portfolio weight calculation.")
port_pw <- pacta.portfolio.allocate::aggregate_portfolio(company_pw)

if (portfolio_type == "Bonds") {
log_info("Ownership weight calculation not defined for Bonds. Skipping.")
port_own <- NULL
company_own <- NULL
} else {
# Ownership weight methodology
log_info("Calculating ownership methodology.")
log_debug("Calculating ownership allocation.")
port_own <- pacta.portfolio.allocate::ownership_allocation(port)

log_debug("Aggregating companies for ownership calculation.")
company_own <- pacta.portfolio.allocate::aggregate_company(port_own)

log_debug("Aggregating portfolio for ownership calculation.")
port_own <- pacta.portfolio.allocate::aggregate_portfolio(company_own)
}

# Create combined outputs
log_debug("Creating combined {portfolio_type} company outputs.")
company_all <- dplyr::bind_rows(company_pw, company_own)

log_debug("Creating combined {portfolio_type} portfolio outputs.")
port_all <- dplyr::bind_rows(port_pw, port_own)

if (has_map && pacta.portfolio.utils::data_check(company_all)) {
log_debug("Creating {portfolio_type} map outputs.")
abcd_raw <- pacta.portfolio.allocate::get_abcd_raw(portfolio_type)
log_debug("Merging geography data into {portfolio_type} map outputs.")
map <- pacta.portfolio.allocate::merge_in_geography(
portfolio = company_all,
ald_raw = abcd_raw
)
log_trace("Removing abcd_raw object from memory.")
rm(abcd_raw)

log_debug("Aggregating {portfolio_type} map data.")
map <- pacta.portfolio.allocate::aggregate_map_data(map)
}

# Technology Share Calculation
if (nrow(port_all) > 0L) {
log_debug("Calculating {portfolio_type} portfolio technology share.")
port_all <- pacta.portfolio.allocate::calculate_technology_share(
df = port_all
)
}

if (nrow(company_all) > 0L) {
log_debug("Calculating {portfolio_type} company technology share.")
company_all <- pacta.portfolio.allocate::calculate_technology_share(
df = company_all
)
}

# Scenario alignment calculations
log_debug("Calculating {portfolio_type} portfolio scenario alignment.")
port_all <- pacta.portfolio.allocate::calculate_scenario_alignment(
df = port_all
)

log_debug("Calculating {portfolio_type} company scenario alignment.")
company_all <- pacta.portfolio.allocate::calculate_scenario_alignment(
df = company_all
)

results_company_filename <- file.path(
output_dir,
paste0(portfolio_type, "_results_company.rds")
)
if (pacta.portfolio.utils::data_check(company_all)) {
log_debug("Saving {portfolio_type} company results.")
saveRDS(
company_all,
results_company_filename
)
}

results_portfolio_filename <- file.path(
output_dir,
paste0(portfolio_type, "_results_portfolio.rds")
)
if (pacta.portfolio.utils::data_check(port_all)) {
log_debug("Saving {portfolio_type} portfolio results.")
saveRDS(
port_all,
results_portfolio_filename
)
}

if (has_map && pacta.portfolio.utils::data_check(map)) {
log_debug("Saving {portfolio_type} map results.")
results_map_filename <- file.path(
output_dir,
paste0(portfolio_type, "_results_map.rds")
)
saveRDS(
map,
results_map_filename
)
}

} else {
log_trace(
"{portfolio_type} portfolio has no rows. ",
"Skipping {portfolio_type} calculations."
)
}

}
Loading
Loading