Skip to content

Commit

Permalink
Update tmfpython.sh
Browse files Browse the repository at this point in the history
Simplified the wrapper script a bit, getting rid of the branch-specific bits.
  • Loading branch information
abbyevewilliams authored Aug 14, 2024
1 parent cdc949b commit 8b71666
Showing 1 changed file with 40 additions and 56 deletions.
96 changes: 40 additions & 56 deletions scripts/tmfpython.sh
Original file line number Diff line number Diff line change
@@ -1,70 +1,73 @@
#!/bin/bash

#run with command: scripts/tmfpython.sh -p 1113 -t 2010 ...
#p: project ID
#run with command: scripts/tmfpython.sh -i 'maps/aew85/projects' -o '/maps/aew85/tmf_pipe_out' -p 1113 -t 2010 ...
#i: input dir - directory containing project shapefiles
#o: output dir - directory containing pipeline outputs
#p: project name/ID - must match name of shapefile
#t: year of project start (t0)
#e: evaluation year (default: 2022)
#r: whether to run an ex-post evaluation and knit the results in an R notebook (true/false, default: false).
#a: whether to run an ex-ante evaluation and knit the results in an R notebook (true/false, default: false).
#v: verbose - whether to run an ex-post evaluation and knit the results in an R notebook (true/false, default: false).

#NB running evaluations requires the evaluations code

# Check which branch is currently checked out
current_branch=$(git rev-parse --abbrev-ref HEAD)
#current_branch=$(git rev-parse --abbrev-ref HEAD)

set -e

############ DEFAULTS ###############

input_dir="/maps/aew85/projects"
output_dir="/maps/aew85/tmf_pipe_out"
input_dir=""
output_dir=""
eval_year=2022
ex_post=false
ex_ante=false
verbose=false

#####################################

function display_help() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -p <project> Project name"
echo " -i <input_dir> Input directory"
echo " -o <output_dir> Output directory"
echo " -p <proj> Project name"
echo " -t <t0> Start year"
echo " -e <year> Evaluation year"
echo " -r <ex_post> Knit ex post evaluation? (true/false)"
echo " -a <ex_ante> Knit ex ante evaluation? (true/false)"
echo " -v <verbose> Knit ex post evaluation as .Rmd? (true/false)"
echo " -h Display this help message"
echo
echo "Example:"
echo " $0 -p 'gola' -t 2012 -e 2021 -r true -a true"
}

# Parse arguments
while getopts "p:t:e:r:a:h" flag
while getopts "i:o:p:t:e:v:h" flag
do
case "${flag}" in
p) input_dir=${OPTARG};;
t) output_dir=${OPTARG};;
p) proj=${OPTARG};;
t) t0=${OPTARG};;
e) eval_year=${OPTARG};;
r) ex_post=${OPTARG};;
r) verbose=${OPTARG};;
a) ex_ante=${OPTARG};;
h) display_help; exit 0;;
*) echo "Invalid option: -${OPTARG}" >&2; display_help; exit 1;;
esac
done

echo "Input directory: $input_dir"
echo "Output directory: $output_dir"
echo "Project: $proj"
echo "t0: $t0"
echo "Evaluation year: $eval_year"
echo "Ex-post evaluation: $ex_post"
echo "Ex-ante evaluation: $ex_ante"
echo "Ex-post evaluation: $verbose"

if [ $# -eq 0 ]; then
display_help
exit 1
fi

# Make project output directory
# Make project output folder
mkdir -p "${output_dir}/${proj}"
echo "--Folder created.--"

Expand Down Expand Up @@ -182,46 +185,27 @@ echo "--Set M created.--"

#Matching: find pairs
tmfpython3 -m methods.matching.find_pairs \
--k "${output_dir}/${proj}/k.parquet" \
--m "${output_dir}/${proj}/matches.parquet" \
--start_year "$t0" \
--output "${output_dir}/${proj}/pairs" \
--seed 42 \
-j 1
echo "--Pairs matched.--"
--k "${output_dir}/${proj}/k.parquet" \
--m "${output_dir}/${proj}/matches.parquet" \
--start_year "$t0" \
--output "${output_dir}/${proj}/pairs" \
--seed 42 \
-j 1
echo "--Pairs matched.--"

#Calculate additionality
if [ "$current_branch" == "mwd-check-stopping-criteria" ]; then
tmfpython3 -m methods.outputs.calculate_additionality \
--project "${input_dir}/${proj}.geojson" \
--project_start "$t0" \
--evaluation_year "$eval_year" \
--density "${output_dir}/${proj}/carbon-density.csv" \
--matches "${output_dir}/${proj}/pairs" \
--output "${output_dir}/${proj}/additionality.csv" \
--stopping "${output_dir}/${proj}/stopping.csv"
echo "--Additionality and stopping criteria calculated.--"
else
tmfpython3 -m methods.outputs.calculate_additionality \
--project "${input_dir}/${proj}.geojson" \
--project_start "$t0" \
--evaluation_year "$eval_year" \
--density "${output_dir}/${proj}/carbon-density.csv" \
--matches "${output_dir}/${proj}/pairs" \
--output "${output_dir}/${proj}/additionality.csv"
echo "--Additionality calculated.--"
fi
tmfpython3 -m methods.outputs.calculate_additionality \
--project "${input_dir}/${proj}.geojson" \
--project_start "$t0" \
--evaluation_year "$eval_year" \
--density "${output_dir}/${proj}/carbon-density.csv" \
--matches "${output_dir}/${proj}/pairs" \
--output "${output_dir}/${proj}/additionality.csv"
echo "--Additionality calculated.--"

# Run ex post evaluation
if [ "$ex_post" == "true" ]; then
evaluations_dir="~/evaluations"
ep_output_file="${evaluations_dir}/${proj}_ex_post_evaluation.html"
Rscript -e "rmarkdown::render(input='~/evaluations/R/ex_post_evaluation_template.Rmd',output_file='${ep_output_file}',params=list(proj='${proj}',t0='${t0}',eval_year='${eval_year}',input_dir='${input_dir}',output_dir='${output_dir}',evaluations_dir='${evaluations_dir}'))"
if [ "$verbose" == "true" ]; then
evaluations_dir="~/evaluations"
ep_output_file="${evaluations_dir}/${proj}_ex_post_evaluation.html"
Rscript -e "rmarkdown::render(input='~/evaluations/R/ex_post_evaluation_template.Rmd',output_file='${ep_output_file}',params=list(proj='${proj}',t0='${t0}',eval_year='${eval_year}',input_dir='${input_dir}',output_dir='${output_dir}'))"
fi

# Run ex-ante evaluation
if [ "$ex_ante" == "true" ]; then
evaluations_dir="~/evaluations"
ea_output_file="${evaluations_dir}/${proj}_ex_ante_evaluation.html"
Rscript -e "rmarkdown::render(input='~/evaluations/R/ex_ante_evaluation_template.Rmd',output_file='${ea_output_file}',params=list(proj='${proj}',t0='${t0}',eval_year='${eval_year}',input_dir='${input_dir}',output_dir='${output_dir}',evaluations_dir='${evaluations_dir}'))"
fi

0 comments on commit 8b71666

Please sign in to comment.