-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_targets.R
351 lines (307 loc) · 11.9 KB
/
_targets.R
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#'========================================================================
# makefile using targets
#'========================================================================
#
# by Mark R Payne
# DTU-Aqua, Kgs. Lyngby, Denmark
# http://www.staff.dtu.dk/mpay
#
# Created Thu Aug 20 17:03:55 2020
#
# MA makefile using the targets package
#
# This work is subject to a Creative Commons "Attribution" "ShareALike" License.
# You are largely free to do what you like with it, so long as you "attribute"
# me for my contribution. See the fine print at the end for exact details.
#
# To do:
#
# Notes:
#
#'========================================================================
#'========================================================================
# Initialise system ####
#'========================================================================
cat(sprintf("\n%s\n","PredEng makefile"))
cat(sprintf("Analysis performed %s\n\n",base::date()))
suppressPackageStartupMessages({
library(targets)
library(callr)
library(PredEng)
library(here)
library(assertthat)
})
pcfg <- PE.load.config()
#'========================================================================
# Configure ####
#'========================================================================
#Take input arguments, if any
PE.config.summary(pcfg)
tar_option_set(error = "workspace")
tar.l <- list()
#'========================================================================
# Helper functions ####
#'========================================================================
# Helper functions
run.extern.script <-
function(this.scp,...,args=NULL) {
assert_that(!is.null(names(args)) | is.null(args),msg="args variable must be a named vector")
log.fname <-
file.path(here("_targets","logs"),
paste(substr(basename(dirname(this.scp)),1,1),
gsub("\\.r$","",basename(this.scp)),
paste(args,collapse="."),
sep="."))
callr::rscript(this.scp,
cmdargs=args,
stdout=log.fname,
stderr="2>&1",
show=FALSE)
return(bind_cols(tibble(script=this.scp,
finished=Sys.time()),
as.list(args)))
}
#Links targets without evaluating them
lazy.collator.fn <- function(...) {return(NULL)}
#'========================================================================
# Data Extraction ####
#'========================================================================
#Choose extraction script
run.extraction.script <- function(this.src) {
this.extract.script <-
switch(paste(this.src@type,this.src@name,sep="/"),
"Observations/EN4"="A2.EN4_extraction.r",
"Observations/HadISST"="A1.HadISST_data.r",
"Observations/HadSLP2"="A3.HadSLP2.r",
"B1.CDO_based_extraction.r")
run.extern.script(here("src/B.Extract/",this.extract.script),
args=c(srcType=this.src@type,
srcName=this.src@name))
}
#Reference model
tar.l$cfg.reference.src <-
tar_target(cfg.reference.src,
pcfg@reference)
#Data sources
#Only run if we need to do some local extraction
tar.l$data.srcs <-
tar_target(cfg.datasrcs,
iteration = "list")
tar.l$datasrc.extracts <-
tar_target(datasrc.extracts,
run.extraction.script(cfg.datasrcs),
pattern=map(cfg.datasrcs))
tar.l$datasrc.realmeans <-
tar_target(datasrc.realmeans,
run.extern.script(here("src/B.Extract/Z1.Calculate_realmeans.r"),
datasrc.extracts,
args=c(srcType=datasrc.extracts$srcType,
srcName=datasrc.extracts$srcName)),
pattern=map(datasrc.extracts))
tar.l$local.data <-
tar_target(local.data,
datasrc.realmeans)
#'========================================================================
# Calibration ####
#'========================================================================
#Extraction databases
#We merge in external extractions at this point
get.extraction.databases <- function(object,...) {
tibble(path=PE.db.list(object,PE.cfg$db$extract),
checksum=tools::md5sum(path),
datetime=file.info(path)$mtime) %>%
extract(path,into="srcTypeName",regex="^.*_Extraction_(.*).sqlite$",remove = FALSE) %>%
separate(srcTypeName,
c("srcType","srcName"),
sep="_",
extra="drop")
}
tar.l$extraction.databases <-
tar_target(extraction.databases,
get.extraction.databases(pcfg),
cue=tar_cue("always"))
#Climatology
tar.l$cfg.clim.years <-
tar_target(cfg.clim.years,
tar.l$clim <-
tar_target(clim,
run.extern.script(here("src/C.Calibrate/A1.Climatological_statistics.r"),
extraction.databases,
cfg.clim.years,
args=c(srcType=extraction.databases$srcType,
srcName=extraction.databases$srcName)),
pattern=map(extraction.databases))
#Calibration
tar.l$calibration <-
tar_target(calibration,
run.extern.script(here("src/C.Calibrate/B1.Mean_adjustment.r"),
clim, cfg.reference.src,
args=c(srcType=clim$srcType,
srcName=clim$srcName)),
pattern=map(clim))
#
# if(any(pcfg@calibrationMethods=="NAOmatching")) {
# tar.l$NAOmatching <-
# tar_target(NAOmatching,
# run.extern.script(here("src/C.Calibrate/B2.NAO_matching.r"),
# calibration))
# }
#
#Ensemble and Grand means
if([email protected]) {
tar.l$stat.srcs <-
tar_target(stat.srcs,
calibration)
} else {
tar.l$ensmean.l <-
tar_target(ensmean.src,
calibration %>%
filter(srcType!="Observations") %>%
nest(members=c(-srcType)))
tar.l$ensmean <-
tar_target(ensmeans,
run.extern.script(here("src/C.Calibrate/C1.Ensemble_means.r"),
args=c(srcType=ensmean.src$srcType,
srcName="ensmean")),
pattern=map(ensmean.src))
tar.l$GrandEns <-
tar_target(grandens,
run.extern.script(here("src/C.Calibrate/C2.Grand_ensemble.r"),
args=c(srcType=ensmean.src$srcType,
srcName="grandens")),
pattern=map(ensmean.src))
tar.l$stat.srcs <-
tar_target(stat.srcs,
bind_rows(ensmeans,grandens,calibration))
}
#'========================================================================
# Stats ####
#'========================================================================
#Get stat jobs to process
stat.jobs.fn <- function(...){
#Check that we have at least some statistics
assert_that(length(pcfg@statistics)>0,
msg="No statistics defined. Must be at least one.")
#Extract statistics
these.stats <-
tibble(st=pcfg@[email protected]) %>%
mutate(statName=map_chr(st,slot,"name"),
st.uses.globalROI=map_lgl(st,slot,"use.globalROI"),
st.request=map(st,slot,"spatial.polygons"),
st.request.is.mt=map_lgl(st.request, ~length(.x)==0),
st.request.is.na=map_lgl(st.request, ~any(is.na(.x))),
#Set spatial polygons by merging in defaults
spName=map_if(st.request,st.request.is.mt, ~ [email protected]$name),
spName=map_if(spName,st.request.is.na,~ NULL),
spName=map_if(spName,st.uses.globalROI, ~ c(.x,PE.cfg$misc$globalROI))) %>%
unnest(spName)
return(these.stats)
}
tar.l$stat.jobs <-
tar_target(statJobs,
stat.jobs.fn(pcfg))
#Setup landmask
if([email protected]) {
tar.l$landmask <-
tar_target(landmask,
run.extern.script(here("src/D.Statistics/A1.Generate_landmask.r"),
clim))
}
#Process stats
tar.l$stats <-
tar_target(stats,
run.extern.script(here("src/D.Statistics/B1.Calculate_stats.r"),
landmask,
args=c(spName=statJobs$spName,
statName=statJobs$statName,
srcType=stat.srcs$srcType,
srcName=stat.srcs$srcName)),
pattern=cross(statJobs,stat.srcs))
#Stat median / means
tar.l$stat_median_means <-
tar_target(stat_median_means,
run.extern.script("src/D.Statistics/B2.Statmeans.r",
stats))
#Rolling means
tar.l$rollmean <-
tar_target(rollmean,
run.extern.script("src/D.Statistics/B3.Rolling_means.r",
stat_median_means))
#Calculation skill metrics
tar.l$cfg.comp.years <-
tar_target(cfg.comp.years,
tar.l$scalar.metrics <-
tar_target(scalar.metrics,
run.extern.script(here("src/E.Skill/A1.Calculate_scalar_skill_metrics.r"),
stats,rollmean,cfg.comp.years))
tar.l$field.metrics <-
tar_target(field.metrics,
run.extern.script(here("src/E.Skill/A2.Calculate_field_skill_metrics.r"),
stats,rollmean,cfg.comp.years))
#'========================================================================
# Outputs ####
#'========================================================================
#Pointwise extraction
# tar.l$points <-
# tar_target(points,
# slot(pcfg,"pt.extraction"))
#
# tar.l$pointwise <-
# tar_target(pointwise,
# run.extern.script(here("src/F.Postprocessing/A1.Pointwise_extraction.r"),
# points,stats)) #Stats is a included as a second dependency for cases when
# #there are no metrics to calculate
#Markdown report
tar.l$report <-
tar_target(report,
run.extern.script(here("src/E.Skill/B1.Visualise_scalar_skill_metrics.r"),
scalar.metrics))
#Final target of this branch
tar.l$analysis <-
tar_target(analysis,
lazy.collator.fn(report,field.metrics,rollmean))
#'========================================================================
# Make a Plan! And then change it. ####
#'========================================================================
#Synchronise list and target names
names(tar.l) <- map_chr(tar.l,~get("name",envir=.x$settings))
#If only running obs, reduce the number of targets
#Note that we can also have observations in the Model slots though.
if([email protected]) {
obs.only.drop <-
c("report","scalar.metrics","field.metrics")
tar.l <- tar.l[!(names(tar.l) %in% obs.only.drop)]
}
#Turn off multimodel aspects if there are no models
if(length([email protected])==0) {
no.models.drop <-
c("datasrcs","datasrc.extracts","datasrc.realmeans")
tar.l <- tar.l[!(names(tar.l) %in% no.models.drop)]
}
#Turn off field metrics if there aren't any
if(!any(map_lgl(pcfg@statistics,slot,"do.field.metrics"))) {
tar.l <- tar.l[(names(tar.l)!="field.metrics")]
}
#Turn off scalar metrics if there aren't any
if(!any(map_lgl(pcfg@statistics,returns.scalar))) {
tar.l <- tar.l[!(names(tar.l) %in% c("scalar.metrics","report"))]
}
#Done!
tar.l
# .............
# This work by Mark R Payne is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 Unported License.
# For details, see http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
# Basically, this means that you are free to "share" and "remix" for
# non-commerical purposes as you see fit, so long as you "attribute" me for my
# contribution. Derivatives can be distributed under the same or
# similar license.
#
# This work comes with ABSOLUTELY NO WARRANTY or support.
#
# This work should also be considered as BEER-WARE. For details, see
# http://en.wikipedia.org/wiki/Beerware
# .............