-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
152 lines (111 loc) · 4.62 KB
/
Makefile
File metadata and controls
152 lines (111 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
default: endrule
# when testing pipeline, single province target to make
# to override, can provide `ONEPROV=WC` (for example) at end of `make sometarget` invocation
ONEPROV ?= GP
# see example.makefile for notes on how to make this
-include local.makefile
REFDIR ?= local
local:
mkdir -p $@
# structural definitions
DATDIR := ${REFDIR}/data
FIGDIR := ${REFDIR}/figures
OUTDIR := ${REFDIR}/output
# convenience definitions
# use: $(call R[, optional other arguments])
R = $(strip Rscript $^ $(1) $@)
wget = curl -o $@ $(1)
RENV = .Rprofile
# build renv/library & other renv infrastructure
${RENV}: install.R
Rscript --vanilla $^
# for make directory rules
define md
$(1): | ${RENV} ${REFDIR}
mkdir -p $$@
endef
# define all the necessary directory creation & then `md` them
DIRS := ${DATDIR} ${FIGDIR} ${OUTDIR}
$(foreach dir,${DIRS},$(eval $(call md,${dir})))
# source data
DATAURL := https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv
# get the raw data
${DATDIR}/raw.csv: | ${DATDIR}
$(call wget,${DATAURL})
# initial organization + saving as binary; no cleaning, only type conversion
# & pivoting to long
${DATDIR}/intermediate.rds: R/import.R ${DATDIR}/raw.csv | ${DATDIR}
$(call R)
# n.b. raw data also has an UNKNOWN
PROVINCES := GP WC EC KZN FS LP MP NC NW
# Shared inputs
SHARED_INPUTS = R/pipeline_shared_inputs.R
# define all possible extracts
$(foreach agg,daily weekly,$(foreach tar,${PROVINCES},$(eval EXTRACTS += ${DATDIR}/${agg}_${tar}.rds)))
# extraction rule; also cleans data
${EXTRACTS}: R/extract.R ${DATDIR}/intermediate.rds
$(call R,$(subst _, ,$(basename $(notdir $@))))
${DATDIR}/daily_RSA.rds: R/aggregate.R $(filter ${DATDIR}/daily_%.rds,${EXTRACTS})
$(call R)
${DATDIR}/weekly_RSA.rds: R/aggregate.R $(filter ${DATDIR}/weekly_%.rds,${EXTRACTS})
$(call R)
allextracts: ${EXTRACTS} ${DATDIR}/daily_RSA.rds ${DATDIR}/weekly_RSA.rds
# needs some tweaking, but basically right
${FIGDIR}/incidence.png: R/fig_incidence.R ${DATDIR}/intermediate.rds | ${FIGDIR}
$(call R)
${FIGDIR}/daily_vs_weekly_%.png: R/fig_daily_vs_weekly.R ${DATDIR}/daily_%.rds ${DATDIR}/weekly_%.rds | ${FIGDIR}
$(call R)
${FIGDIR}/fig_panel_scores_%.png: \
R/fig_panel_scores.R \
${DATDIR}/daily_%.rds \
${DATDIR}/weekly_%.rds \
${OUTDIR}/score_%.rds \
${OUTDIR}/forecast_daily_%.rds \
${OUTDIR}/forecast_weekly_%.rds \
${OUTDIR}/forecast_rescale_%.rds \
${OUTDIR}/diagnostics_%.csv \
${SHARED_INPUTS} | ${FIGDIR}
$(call R)
${FIGDIR}/fig_panel_diagnostics_%.png: \
R/fig_panel_diagnostics.R \
${DATDIR}/daily_%.rds \
${DATDIR}/weekly_%.rds \
${OUTDIR}/forecast_daily_%.rds \
${OUTDIR}/forecast_weekly_%.rds \
${OUTDIR}/forecast_rescale_%.rds \
${OUTDIR}/diagnostics_%.csv \
${SHARED_INPUTS} | ${FIGDIR}
$(call R)
# pattern = some province
DAILYDAT_PAT = ${DATDIR}/daily_%.rds
WEEKLYDAT_PAT = ${DATDIR}/weekly_%.rds
# pattern = province_(daily|weekly|rescale)
FORECAST_PAT = ${OUTDIR}/forecast_%.rds
${FORECAST_PAT}: R/pipeline_main.R ${DATDIR}/%.rds ${SHARED_INPUTS} | ${OUTDIR}
$(call R)
${OUTDIR}/forecast_rescale_%.rds: R/pipeline_rescaled_weekly.R ${DATDIR}/weekly_%.rds ${SHARED_INPUTS} | ${OUTDIR}
$(call R)
${OUTDIR}/score_%.rds: R/score.R ${DATDIR}/daily_%.rds ${DATDIR}/weekly_%.rds ${OUTDIR}/forecast_daily_%.rds ${OUTDIR}/forecast_weekly_%.rds ${OUTDIR}/forecast_rescale_%.rds ${SHARED_INPUTS}
$(call R)
${OUTDIR}/diagnostics_%.csv: R/diagnostics.R ${OUTDIR}/forecast_daily_%.rds ${OUTDIR}/forecast_weekly_%.rds ${OUTDIR}/forecast_rescale_%.rds ${SHARED_INPUTS}
$(call R)
# all targets at once
all_diagnostics: $(patsubst %,${OUTDIR}/diagnostics_%.csv,${PROVINCES} RSA)
all_forecasts: $(patsubst %,${OUTDIR}/forecast_daily_%.rds,${PROVINCES} RSA) $(patsubst %,${OUTDIR}/forecast_weekly_%.rds,${PROVINCES} RSA) $(patsubst %,${OUTDIR}/forecast_rescale_%.rds,${PROVINCES} RSA)
all_scores: $(patsubst %,${OUTDIR}/score_%.rds,${PROVINCES} RSA)
## Main figure targets
all_scores_panel_figs: $(patsubst %,${FIGDIR}/fig_panel_scores_%.png,${PROVINCES} RSA)
all_diagnostics_panel_figs: $(patsubst %,${FIGDIR}/fig_panel_diagnostics_%.png,${PROVINCES} RSA)
all_dvsw_figs: $(patsubst %,${FIGDIR}/daily_vs_weekly_%.png,${PROVINCES})
# Combined target for all panel figures
all_panel_figs: all_scores_panel_figs all_diagnostics_panel_figs
test: ${FIGDIR}/fig_panel_scores_${ONEPROV}.png ${FIGDIR}/fig_panel_diagnostics_${ONEPROV}.png
# Paper rendering
PAPERDIR := paper
PAPERSRC := ${PAPERDIR}/paper.qmd
PAPEROUT := ${PAPERDIR}/paper.pdf
${PAPEROUT}: ${PAPERSRC} ${PAPERDIR}/bibliography.bib
cd ${PAPERDIR} && quarto render paper.qmd
paper: ${PAPEROUT}
.PHONY: paper
endrule: all_scores_panel_figs