Skip to content

Commit

Permalink
minimized setup (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman authored Oct 17, 2023
1 parent 4286bab commit 7ca5f73
Show file tree
Hide file tree
Showing 29 changed files with 274 additions and 4,077 deletions.
10 changes: 10 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Type: PACTA
Description: Run PACTA.
Depends:
dplyr,
jsonlite,
pacta.portfolio.allocate,
pacta.portfolio.audit,
pacta.portfolio.import,
pacta.portfolio.utils,
readr
73 changes: 24 additions & 49 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
# https://rocker-project.org/images/versioned/r-ver.html
#
# sets CRAN repo to use Posit Package Manager to freeze R package versions to
# those available on 2023-06-30
# those available on 2023-09-29
# https://packagemanager.posit.co/client/#/repos/2/overview
# https://packagemanager.posit.co/cran/__linux__/jammy/2023-06-30
# https://packagemanager.posit.co/cran/__linux__/jammy/2023-09-29

# set proper base image
ARG PLATFORM="linux/amd64"
ARG R_VERS="4.3.1"
FROM --platform=$PLATFORM rocker/r-ver:$R_VERS

# set Docker image labels
LABEL org.opencontainers.image.source=https://github.com/RMI-PACTA/workflow.pacta
LABEL org.opencontainers.image.description="Docker image to run PACTA"
LABEL org.opencontainers.image.licenses=MIT

ARG CRAN_REPO="https://packagemanager.posit.co/cran/__linux__/jammy/2023-06-30"
RUN echo "options(repos = c(CRAN = '$CRAN_REPO'))" >> "${R_HOME}/etc/Rprofile.site"
LABEL org.opencontainers.image.title=""
LABEL org.opencontainers.image.revision=""
LABEL org.opencontainers.image.version=""
LABEL org.opencontainers.image.vendor=""
LABEL org.opencontainers.image.base.name=""
LABEL org.opencontainers.image.ref.name=""
LABEL org.opencontainers.image.authors=""

# set apt-get to noninteractive mode
ARG DEBIAN_FRONTEND="noninteractive"
Expand All @@ -35,73 +41,42 @@ RUN apt-get update \
&& chmod -R a+rwX /root \
&& rm -rf /var/lib/apt/lists/*

# install system dependencies for R packages
ARG R_PKG_SYS_DEPS="\
libfontconfig1-dev \
libfreetype6-dev \
libfribidi-dev \
libgit2-dev \
libharfbuzz-dev \
libicu-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libxml2-dev \
libxt6 \
make \
pandoc \
perl \
zlib1g-dev \
"
RUN apt-get update \
&& apt-get install -y --no-install-recommends $R_PKG_SYS_DEPS \
&& rm -rf /var/lib/apt/lists/*
# set frozen CRAN repo
ARG CRAN_REPO="https://packagemanager.posit.co/cran/__linux__/jammy/2023-09-29"
RUN echo "options(repos = c(CRAN = '$CRAN_REPO'))" >> "${R_HOME}/etc/Rprofile.site"

# install pak for R package installation
# install packages for dependency resolution and installation
RUN Rscript -e "install.packages('pak')"
RUN Rscript -e "pak::pkg_install('renv')"

# copy in scripts from this repo
ARG WORKFLOW_DIR="/workflow.pacta"
COPY . $WORKFLOW_DIR
WORKDIR $WORKFLOW_DIR
# copy in everything from this repo
COPY . /

# PACTA R package tags
ARG summary_tag="/tree/main"
ARG allocate_tag="/tree/main"
ARG audit_tag="/tree/main"
ARG import_tag="/tree/main"
ARG report_tag="/tree/main"
ARG utils_tag="/tree/main"

ARG summary_url="https://github.com/rmi-pacta/pacta.executive.summary"
ARG allocate_url="https://github.com/rmi-pacta/pacta.portfolio.allocate"
ARG audit_url="https://github.com/rmi-pacta/pacta.portfolio.audit"
ARG import_url="https://github.com/rmi-pacta/pacta.portfolio.import"
ARG report_url="https://github.com/rmi-pacta/pacta.portfolio.report"
ARG utils_url="https://github.com/rmi-pacta/pacta.portfolio.utils"

# install R package dependencies
RUN Rscript -e "\
gh_pkgs <- \
c( \
paste0('$summary_url', '$summary_tag'), \
paste0('$allocate_url', '$allocate_tag'), \
paste0('$audit_url', '$audit_tag'), \
paste0('$import_url', '$import_tag'), \
paste0('$report_url', '$report_tag'), \
paste0('$utils_url', '$utils_tag') \
); \
source('pkg_deps.R'); \
pak::pak(c(gh_pkgs, pkg_deps)); \
workflow_pkgs <- renv::dependencies('DESCRIPTION')[['Package']]; \
workflow_pkgs <- grep('^pacta[.]', workflow_pkgs, value = TRUE, invert = TRUE); \
pak::pak(c(gh_pkgs, workflow_pkgs)); \
"

# set permissions for PACTA repos that need local content
RUN chmod -R a+rwX $WORKFLOW_DIR

# set the build_version environment variable
ARG image_tag
ENV build_version=$image_tag
ARG head_hashes
ENV head_hashes=$head_hashes

CMD ["/bin/sh"]
# set default run behavior
ENTRYPOINT ["/run-pacta.sh"]
CMD ["input_dir/default_config.json"]
7 changes: 0 additions & 7 deletions bin/run-r-scripts-results-only

This file was deleted.

12 changes: 12 additions & 0 deletions input_dir/default_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data_dir": "../pacta-data/2021Q4",
"portfolio_path": "input_dir/default_portfolio.csv",
"output_dir": "output_dir",
"start_year": 2021,
"equity_market_list": ["GlobalMarket", "DevelopedMarket", "EmergingMarket"],
"scenario_sources_list": ["GECO2022", "IPR2021", "ISF2021", "WEO2022"],
"scenario_geographies_list": ["Global", "GlobalAggregate", "NonOECD", "OECD"],
"sector_list": ["Power", "Automotive", "Oil&Gas", "Coal", "Steel", "Aviation", "Cement"],
"has_map": false,
"project_code": "GENERAL"
}
91 changes: 91 additions & 0 deletions input_dir/default_portfolio.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
isin,market_value,currency
XS1297557172,6821206,USD
DE000A1R07L4,4724723,USD
US345370BV11,5619252,USD
CND10001QD60,1124438,USD
XS2356040357,3076756,USD
US31428XAY22,8235607,USD
FR0012650281,641235,USD
FR001400F2Q0,4190307,USD
USQ94606AE22,4118654,USD
US911312BB15,5414864,USD
INE331A07273,2972779,USD
INE481G07166,3244497,USD
INE010A08065,4488737,USD
US86614RAF47,5793655,USD
FR0124261621,1581767,USD
US60937CAA36,900439,USD
US93317QAD79,9993273,USD
TH0148034703,8657460,USD
XS2325566847,9459870,USD
US02076XAC65,3952496,USD
US880779BA01,3506143,USD
USU88125AC04,3401211,USD
INE208A07281,8670483,USD
US69373UAA51,267878,USD
US12594KAA07,4094599,USD
US205677AB38,3309593,USD
US69370RAK32,3519158,USD
US707887A@47,1577541,USD
US166764AA86,6575752,USD
US71654QCP54,7084677,USD
INE206D08162,6210214,USD
US745310AB85,709698,USD
CA44882ZXZ12,2809584,USD
JP360540AR10,9641731,USD
CA44885ZEQ96,9683822,USD
INE017A08235,3910673,USD
XS1555576641,2598631,USD
XS1381693248,2117715,USD
NO0010892870,8899756,USD
NO0010806912,7280862,USD
USU85795AQ29,2560264,USD
JP338603AA35,6806168,USD
DE000A2GESQ1,1567613,USD
INE114A07802,8605761,USD
US03938LAA26,2698943,USD
KR7005380001,9338795,USD
CNE100000Q35,1068423,USD
KYG3777B1032,9637388,USD
US5787871038,5467105,USD
SE0016844831,4168401,USD
TW0002646007,6371201,USD
INE646L01027,8355228,USD
US38045R2067,7952384,USD
PHY1234G1032,180886,USD
US00910M1009,6809079,USD
INE070A01015,1013567,USD
CNE000000XV4,8873793,USD
PK0059501012,5829360,USD
INE583C01021,6059486,USD
MYL5000OO004,6433976,USD
AU000000BCB5,9186193,USD
US03940R1077,265189,USD
ID1000162902,1712519,USD
US03485P3001,4712749,USD
CNE0000017V2,5989615,USD
NL0010545661,3343735,USD
CNE000000FH0,8915246,USD
US6937181088,9800223,USD
US9288541082,7226287,USD
CA62910L1022,9449577,USD
IL0011419699,7746343,USD
SE0020180917,1788562,USD
AU000000BUY9,1578448,USD
KR7096770003,860209,USD
US45325E2019,4071578,USD
CA45790B7088,8510107,USD
US0188021085,7030638,USD
JP3981200003,344741,USD
GB00B1VNSX38,6493591,USD
BRENGICDAM16,9257903,USD
MHY271836006,1571628,USD
MHY2687W1241,2627572,USD
TH0363010R10,8711798,USD
JP3247600004,4447848,USD
IL0065100930,1121308,USD
QA000A0KD6K3,6405719,USD
US92023R4074,8204370,USD
JP3342000001,8564047,USD
US88031M1099,149263,USD
TW0009957001,4538558,USD
18 changes: 0 additions & 18 deletions inst/rmd/user_errors.Rmd

This file was deleted.

File renamed without changes.
90 changes: 90 additions & 0 deletions pacta_01.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
suppressPackageStartupMessages({
library(pacta.portfolio.utils)
library(pacta.portfolio.import)
library(pacta.portfolio.audit)
library(dplyr)
library(readr)
library(jsonlite)
})


# -------------------------------------------------------------------------

cfg_path <- commandArgs(trailingOnly = TRUE)
if (length(cfg_path) == 0 || cfg_path == "") { cfg_path <- "input_dir/default_config.json" }
cfg <- fromJSON(cfg_path)


# load necessary input data ----------------------------------------------------

currencies <- readRDS(file.path(cfg$data_dir, "currencies.rds"))

fund_data <- readRDS(file.path(cfg$data_dir, "fund_data.rds"))
total_fund_list <- readRDS(file.path(cfg$data_dir, "total_fund_list.rds"))
isin_to_fund_table <- readRDS(file.path(cfg$data_dir, "isin_to_fund_table.rds"))

fin_data <- readRDS(file.path(cfg$data_dir, "financial_data.rds"))

entity_info <- get_entity_info(dir = cfg$data_dir)

abcd_flags_equity <- readRDS(file.path(cfg$data_dir, "abcd_flags_equity.rds"))
abcd_flags_bonds <- readRDS(file.path(cfg$data_dir, "abcd_flags_bonds.rds"))

entity_emission_intensities <- readRDS(file.path(cfg$data_dir, "iss_entity_emission_intensities.rds"))
average_sector_emission_intensities <- readRDS(file.path(cfg$data_dir, "iss_average_sector_emission_intensities.rds"))


# Portfolios -------------------------------------------------------------------

portfolio_raw <- read_portfolio_csv(cfg$portfolio_path)

portfolio <- process_raw_portfolio(
portfolio_raw = portfolio_raw,
fin_data = fin_data,
fund_data = fund_data,
entity_info = entity_info,
currencies = currencies,
total_fund_list = total_fund_list,
isin_to_fund_table = isin_to_fund_table
)

# FIXME: this is necessary because pacta.portfolio.allocate::add_revenue_split()
# was removed in #142, but later we realized that it had a sort of hidden
# behavior where if there is no revenue data it maps the security_mapped_sector
# column of the portfolio data to financial_sector, which is necessary later
portfolio <-
portfolio %>%
mutate(
has_revenue_data = FALSE,
financial_sector = .data$security_mapped_sector
)

portfolio <- create_ald_flag(portfolio, comp_fin_data = abcd_flags_equity, debt_fin_data = abcd_flags_bonds)

portfolio_total <- add_portfolio_flags(portfolio)

portfolio_overview <- portfolio_summary(portfolio_total)

audit_file <- create_audit_file(portfolio_total, has_revenue = FALSE)

emissions_totals <- calculate_portfolio_financed_emissions(
portfolio_total,
entity_info,
entity_emission_intensities,
average_sector_emission_intensities
)


# Saving -----------------------------------------------------------------------

export_audit_information_data(
audit_file_ = audit_file,
portfolio_total_ = portfolio_total,
folder_path = cfg$output_dir
)

saveRDS(portfolio_total, file.path(cfg$output_dir, "total_portfolio.rds"))
saveRDS(portfolio_overview, file.path(cfg$output_dir, "overview_portfolio.rds"))
saveRDS(audit_file, file.path(cfg$output_dir, "audit_file.rds"))
write_csv(audit_file, file.path(cfg$output_dir, "audit_file.csv"))
saveRDS(emissions_totals, file.path(cfg$output_dir, "emissions.rds"))
Loading

0 comments on commit 7ca5f73

Please sign in to comment.