-
Notifications
You must be signed in to change notification settings - Fork 0
/
_targets.R
334 lines (320 loc) · 9.25 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
# Created by use_targets().
# Follow the comments below to fill in this target script.
# Then follow the manual to check and run the pipeline:
# https://books.ropensci.org/targets/walkthrough.html#inspect-the-pipeline
# Load packages required to define the pipeline:
library(targets)
library(tarchetypes)
library(geotargets)
library(crew)
library(crew.cluster)
library(qs) #for format = "qs"
library(nanoparquet) #for format = tar_format_nanoparquet()
# Detect whether you're on HPC & not with an Open On Demand session (which cannot submit SLURM jobs).
slurm_host <- Sys.getenv("SLURM_SUBMIT_HOST")
hpc <- grepl("hpc\\.arizona\\.edu", slurm_host) & !grepl("ood", slurm_host)
controller_hpc_light <-
crew.cluster::crew_controller_slurm(
name = "hpc_light",
workers = 5,
# make workers semi-persistent:
tasks_max = 40, # shut down SLURM job after completing 40 targets
seconds_idle = 300, # or when idle for some time
garbage_collection = TRUE, # run garbage collection between tasks
launch_max = 5L, # number of unproductive launched workers until error
slurm_partition = "standard",
slurm_time_minutes = 60, #wall time for each worker
slurm_log_output = "logs/crew_log_%A.out",
slurm_log_error = "logs/crew_log_%A.err",
slurm_memory_gigabytes_per_cpu = 5,
slurm_cpus_per_task = 3, #use 3 cpus per worker
script_lines = c(
"#SBATCH --account theresam",
#use optimized openBLAS for linear algebra
"export LD_PRELOAD=/opt/ohpc/pub/libs/gnu8/openblas/0.3.7/lib/libopenblas.so",
"module load gdal/3.8.5 R/4.4 eigen/3.4.0"
)
)
controller_hpc_heavy <-
crew.cluster::crew_controller_slurm(
name = "hpc_heavy",
workers = 3,
seconds_idle = 1000,
tasks_max = 20,
garbage_collection = TRUE,
launch_max = 5L,
slurm_partition = "standard",
slurm_time_minutes = 360, #wall time for each worker
slurm_log_output = "logs/crew_log_%A.out",
slurm_log_error = "logs/crew_log_%A.err",
slurm_memory_gigabytes_per_cpu = 5,
slurm_cpus_per_task = 7,
script_lines = c(
"#SBATCH --account theresam",
"export LD_PRELOAD=/opt/ohpc/pub/libs/gnu8/openblas/0.3.7/lib/libopenblas.so",
"module load gdal/3.8.5 R/4.4 eigen/3.4.0"
)
)
controller_local <-
crew::crew_controller_local(
name = "local",
workers = 3,
seconds_idle = 60,
options_local = crew::crew_options_local(
log_directory = "logs/"
)
)
if (isTRUE(hpc)) { #when on HPC, do ALL the thresholds
threshold <- seq(50, 2500, by = 50)
} else { # only do select thresholds
threshold <- c(50, 1250, 2500)
}
# Set target options:
tar_option_set(
trust_timestamps = TRUE, #just check last modified date when deciding whether to re-run
# Packages that your targets need for their tasks.
packages = c(
"fs",
"terra",
"stringr",
"lubridate",
"colorspace",
"purrr",
"ggplot2",
"tidyterra",
"glue",
"car",
"httr2",
"readr",
"sf",
"maps",
"tidyr",
"dplyr",
"broom",
"forcats",
"mgcv"
),
controller = crew::crew_controller_group(
controller_hpc_heavy, controller_hpc_light, controller_local
),
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_light", "local"))
),
#assume workers have access to the _targets/ data store
storage = "worker",
retrieval = "worker",
#allows use of `tar_workspace()` to load dependencies of an errored target for interactive debugging.
workspace_on_error = TRUE
)
# `source()` the R scripts in the R/ folder with your custom functions:
tar_source()
main <- tar_plan(
years = 1981:2023,
tar_target(
name = prism_tmean,
command = get_prism_tmean(years),
pattern = map(years),
deployment = "main", #prevent downloads from running in parallel on distributed workers
format = "file",
description = "download PRISM data"
),
tar_terra_vect(
roi,
make_roi(),
deployment = "main",
description = "vector for North East"
),
tar_map( # for each threshold...
values = list(threshold = threshold),
tar_terra_rast(
gdd_doy,
calc_gdd_doy(rast_dir = prism_tmean, roi = roi, gdd_threshold = threshold),
pattern = map(prism_tmean),
iteration = "list",
description = "calc DOY to reach threshold GDD"
),
# This converts the output of the dynamic branching to be SpatRasters with
# multiple layers instead of lists of SpatRasters. Would love to not have to
# have this target, but there is no way to customize how iteration works.
tar_terra_rast(
gdd_doy_stack,
terra::rast(unname(gdd_doy))
)
)
)
gams <- tar_plan(
#prep data
tar_target(
gam_df_50gdd,
make_gam_df(gdd_doy_stack_50, res = 25000),
format = "qs"
),
tar_target(
gam_df_1250gdd,
make_gam_df(gdd_doy_stack_1250, res = 25000),
format = "qs"
),
tar_target(
gam_df_2500gdd,
make_gam_df(gdd_doy_stack_2500, res = 25000),
format = "qs"
),
#fit gams
tar_target(
gam_50gdd,
fit_bam(gam_df_50gdd, k_spatial = 1000),
format = "qs",
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
)
),
tar_target(
gam_1250gdd,
fit_bam(gam_df_1250gdd, k_spatial = 1000),
format = "qs",
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
)
),
tar_target(
gam_2500gdd,
fit_bam(gam_df_2500gdd, k_spatial = 1000),
format = "qs",
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
)
),
tar_file(
smooths_50gdd,
draw_smooth_estimates(gam_50gdd, roi)
),
tar_file(
smooths_1250gdd,
draw_smooth_estimates(gam_1250gdd, roi)
),
tar_file(
smooths_2500gdd,
draw_smooth_estimates(gam_2500gdd, roi)
),
tar_target(
k_check_50gdd,
check_k(gam_50gdd),
packages = c("mgcv", "dplyr")
),
tar_target(
k_check_1250gdd,
check_k(gam_1250gdd),
packages = c("mgcv", "dplyr")
),
tar_target(
k_check_2500gdd,
check_k(gam_2500gdd),
packages = c("mgcv", "dplyr")
),
tar_target(
k_check_df,
bind_rows(!!!rlang::syms(c(
"k_check_50gdd", "k_check_1250gdd", "k_check_2500gdd"
))),
tidy_eval = TRUE,
description = "Collect results from k_check targets"
),
tar_file(
k_check_df_csv,
tar_write_csv(k_check_df, "output/gams/k_check.csv")
),
tar_target(
slope_newdata,
#doesn't matter which dataset since all that is used is x,y, and year_scaled
#using very coarse newdata regardless of resolution of original data.
make_slope_newdata(gdd_doy_stack_50, res_m = 25000) |>
dplyr::group_by(group) |>
targets::tar_group(),
#grouped by about 1000 pixels per group
iteration = "group",
format = "qs"
),
tar_target(
cities_sf,
make_cities_sf(),
description = "Example cities for plotting fitted trends"
),
tar_map(
values = list(gam = rlang::syms(c("gam_50gdd", "gam_1250gdd", "gam_2500gdd"))),
tar_target(
slopes,
calc_avg_slopes(gam, slope_newdata),
packages = c("marginaleffects", "mgcv"),
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
),
pattern = map(slope_newdata),
format = tar_format_nanoparquet()
),
tar_target(
slope_range,
range(slopes$estimate),
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
),
pattern = map(slopes),
format = "qs"
),
tar_target(
city_plot,
plot_city_trend(gam, cities_sf),
description = "timeseries plot for example cities for each gam"
)
),
tar_target(
slope_range,
range(slope_range_gam_50gdd, slope_range_gam_1250gdd, slope_range_gam_2500gdd),
description = "range across all thresholds for colorbar"
),
tar_map(
values = list(
slopes = rlang::syms(c(
"slopes_gam_50gdd", "slopes_gam_1250gdd", "slopes_gam_2500gdd"
)),
city_plot = rlang::syms(c(
"city_plot_gam_50gdd", "city_plot_gam_1250gdd", "city_plot_gam_2500gdd"
))
),
tar_file(
slopes_plot,
plot_avg_slopes(slopes, slope_range, roi, cities_sf, city_plot),
packages = c("ggpattern", "ggplot2", "terra", "tidyterra", "patchwork"),
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
)
)
)
)
city_slopes <- tar_plan(
tar_map(
values = list(gam = rlang::syms(c("gam_50gdd", "gam_1250gdd", "gam_2500gdd"))),
tar_target(
city_slopes,
calc_city_slopes(cities_sf, gam),
format = tar_format_nanoparquet(),
resources = tar_resources(
crew = tar_resources_crew(controller = ifelse(isTRUE(hpc), "hpc_heavy", "local"))
),
description = "for each GDD threshold, calc avg slope for specific cities"
)
)
)
city_slopes_plot <- tar_plan(
tar_combine(
city_slopes_df,
city_slopes,
format = tar_format_nanoparquet(),
description = "combine predictions from all GDD thresholds for plotting"
)
)
tar_plan(
main,
gams,
city_slopes,
city_slopes_plot
)