-
Notifications
You must be signed in to change notification settings - Fork 7
/
load-forecasts.R
165 lines (152 loc) · 6.25 KB
/
load-forecasts.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
#' Fetch the output from previously-run forecasting using [run_forecasts()]
#'
#' @details If the forecasts directory does not exist or there is a problem
#' loading the forecasts, return `NA`
#'
#' @param model The SS model output as loaded by [create_rds_file()]
#' @param first Load this many of the files. If a non-positive number, load
#' them all. Used for debugging purposes to cut down the size of the
#' lists used
#' @param forecast_yrs A vector of forecast years
#' @param verbose Logical. If `TRUE`, show messages
#' @param ... Absorbs arguments intended for other functions
#'
#' @return A list of forecast outputs as read in by [r4ss::SSgetMCMC()]
#' @export
load_forecasts <- function(model_path = NULL,
first = 0,
forecast_yrs = get_assess_yr():(get_assess_yr() + 3),
verbose = TRUE,
...){
if(!check_forecasts(model_path, ...)){
stop("The forecasts do not appear to have been run, have been run ",
"incorrectly, or only been run partially. Re-run using ",
"`run_forecasts` before trying to attach forecasting")
}
fore_fullpath <- file.path(model_path, forecasts_path)
if(verbose){
message("\nLoading forecast data from directory `", fore_fullpath,
"`\n")
}
ct_levels_lst <- load_ct_levels(model_path, ...)
ct_levels <- ct_levels_lst$ct_levels
# Extract the catch level names from the list into a vector
ct_levels_catch <- map(ct_levels, ~{.x[[1]]})
ct_levels_desc <- map_chr(ct_levels, ~{.x[[2]]})
ct_levels_names <- map_chr(ct_levels, ~{.x[[3]]})
# Get the directory listing and choose the last one for loading
dir_nms <- list.files(fore_fullpath)
dir_nms_fullpath <- file.path(model_path, forecasts_path, dir_nms)
# Make sure only year directories exist here
pat <- paste0("^", forecasts_prepend, "20[0-9]{2}$")
num_forecasts <- length(grep(pat, dir_nms))
if(num_forecasts != length(forecast_yrs)){
stop("The number of actual forecast years run (", num_forecasts, ") ",
"does not match the number of forecast years specified in the ",
"model (", length(forecast_yrs), "). Check the contents of the ",
"directory\n`", fore_fullpath, "`\n")
}
if(supportsMulticore()){
plan("multicore", workers = length(forecast_yrs))
}else{
message(paste0("`create_rds_files_retro()`: ", parallelism_warning))
if(interactive()){
message("\nContinue in sequential mode? (choose a number)")
ans <- menu(c("Yes", "No"))
if(ans == 2){
message("`create_rds_files_retro()`: Bailing out at the user's ",
"request")
return(invisible())
}
}
plan("sequential")
}
lst <- future_map(dir_nms_fullpath, \(fore_path){
# Get the directory listing of the forecast directory and make sure
# it matches what the catch levels are.currently set to in
# forecast-catch-levels.R`
fore_subdir <- dir(fore_path)
# Eliminate extra forecast runs, possibly done after the document was
# created, for the JMC meeting or some other reason
fore_paths <- ct_levels_names[dir_nms %in% ct_levels_names]
lvls_lst <- imap(ct_levels, \(catch_level, catch_level_ind){
fore_level_path <- file.path(fore_path, catch_level[[3]])
message("Loading from ", fore_level_path)
mcmc_out <- SSgetMCMC(dir = fore_level_path,
writecsv = FALSE,
verbose = FALSE)
if(first > 0){
mcmc_out <- mcmc_out |>
slice_head(n = first)
}
# Get the values of interest, namely Spawning biomass and SPR for the two
# decision tables in the executive summary
sb <- mcmc_out %>%
select(grep("Bratio_", names(.)))
spr <- mcmc_out %>%
select(grep("SPRratio_", names(.)))
sbzero <- mcmc_out %>%
select(grep("SSB_Initial", names(.))) |>
mutate(across(everything(), ~{.x = .x / 1e6}))
depl <- sb |>
as_tibble() |>
bind_cols(sbzero) |>
mutate(across(everything(), ~{.x = .x / SSB_Initial})) |>
select(-SSB_Initial)
# Strip out the Bratio_ and SPRratio_ headers so columns are years only
names(sb) <- gsub("Bratio_", "", names(sb))
names(spr) <- gsub("SPRratio_", "", names(spr))
names(depl) <- gsub("Bratio_", "", names(depl))
# Now, filter out the projected years only
sb_proj_cols <- sb |>
select(all_of(as.character(forecast_yrs)))
spr_proj_cols <- spr |>
select(all_of(as.character(forecast_yrs)))
depl_proj_cols <- depl |>
select(all_of(as.character(forecast_yrs)))
sb_proj_cols <- na.omit(sb_proj_cols)
spr_proj_cols <- na.omit(spr_proj_cols)
depl_proj_cols <- na.omit(depl_proj_cols)
# Get forecast catches from the forecast.ss files
case_dir_name <- ct_levels_names[catch_level_ind]
fore <- SS_readforecast(file.path(fore_path,
case_dir_name,
forecast_fn),
verbose = FALSE)
fore_catch <- fore$ForeCatch |>
as_tibble() |>
transmute(year = Year, catch = `Catch or F`) |>
mutate(catch = ifelse(catch < 1, 0, catch))
list(biomass = apply(sb_proj_cols,
2,
quantile,
probs = probs_forecast,
na.rm = TRUE) |>
t() |>
as_tibble(rownames = "yr") |>
mutate(yr = as.numeric(yr)),
depl = apply(depl_proj_cols,
2,
quantile,
probs = probs_forecast,
na.rm = TRUE),
spr = apply(spr_proj_cols,
2,
quantile,
probs = probs_forecast,
na.rm = TRUE) |>
t() |>
as_tibble(rownames = "yr") |>
mutate(yr = as.numeric(yr)),
mcmccalcs = calc_mcmc(mcmc_out),
outputs = mcmc_out,
fore_catch = fore_catch)
})
message("\n")
names(lvls_lst) <- ct_levels_names
lvls_lst
})
names(lst) <- forecast_yrs
message("Finished loading forecasts")
lst
}