-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbison_baseline.R
225 lines (194 loc) · 10.4 KB
/
bison_baseline.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
# Set directories for script and general and K Cuts and Human Density data files, and results
SOURCE_DIR <- getwd()
DATA_DIR <- file.path(SOURCE_DIR, "data")
K_CUTS_DIR <- file.path(SOURCE_DIR, "k_cuts")
RESULTS_DIR <- file.path(SOURCE_DIR, "results")
# create results dir if needed
if (!dir.exists(RESULTS_DIR)) {
dir.create(RESULTS_DIR, recursive = TRUE)
}
# Parallel cores available on machine
# 128 max
parallel_cores <- 95
# Install paleopop and dependencies
library(poems)
library(paleopop)
library(raster)
library(purrr)
library(stringr)
#### Step 1: Create a simulation model template with a study region ####
##### Settings #####
parallel_cores <- 8
nsims <- 25
burn_in_steps <- 100
timesteps <- 2100 + burn_in_steps
#### Step 1: Static inputs ####
##### Region #####
region <- readRDS(file.path(DATA_DIR, "wisent_paleo_region.RDS"))
raster::plot(region$region_raster, main = "Wisent region (cell indices)",
colNA = "blue")
raster::plot(region$temporal_mask_raster()[[2000]], colNA = "blue")
##### Re-usable distance matrix #####
distance_matrix <- DispersalGenerator$new(region = region, dispersal_max_distance = 500, # in km
distance_scale = 1000)$calculate_distance_matrix()
summary(as.vector(distance_matrix))
##### Distance-based environmental correlation #####
# (via a compacted Cholesky decomposition)
env_corr <- SpatialCorrelation$new(region = region, amplitude = 0.99, breadth = 850, distance_scale = 1000)
compact_decomposition <- env_corr$get_compact_decomposition(distance_matrix = distance_matrix)
##### Friction raster #####
friction <- readRDS(file.path(DATA_DIR, "wisent_conductance_v2.RDS"))[region$region_indices,]
##### Human matrix #####
burn_in <- stack(replicate(100, raster(file.path(DATA_DIR, "humans_mean.grd"))))
humans_mean_raster <- raster::stack(burn_in, raster::stack(file.path(DATA_DIR, "humans_mean.grd")))
raster::plot(humans_mean_raster[[c(1,50,100,1000)]], main = "Human mean density",
xlab = "Longitude (degrees)", ylab = "Latitude (degrees)",
colNA = "blue")
raster::values(humans_mean_raster) <- raster::values(humans_mean_raster) # memory
humans_mean_matrix <- humans_mean_raster[region$region_indices]
humans_mean_matrix[!is.finite(humans_mean_matrix)] <- 0
burn_in <- stack(replicate(100, raster(file.path(DATA_DIR, "humans_sd.grd"))))
humans_sd_raster <- raster::stack(burn_in, raster::stack(file.path(DATA_DIR, "humans_sd.grd")))
raster::values(humans_sd_raster) <- raster::values(humans_sd_raster) # memory
humans_sd_matrix <- humans_sd_raster[region$region_indices]
humans_sd_matrix[!is.finite(humans_sd_matrix)] <- 0
rm(humans_mean_raster, humans_sd_raster); gc()
##### Population model template #####
model_template <- PaleoPopModel$new(
region = region,
time_steps = timesteps, # include burn-in
years_per_step = 10,
populations = region$region_cells,
# initial_abundance: generated
transition_rate = 1.0,
# standard_deviation: sampled
compact_decomposition = compact_decomposition,
# carrying_capacity: generated
density_dependence = "logistic",
# growth_rate_max: sampled
harvest = TRUE,
# harvest_max: sampled
harvest_g = 0.4, # constant
# harvest_z: sampled
# harvest_max_n: sampled
# human_density: generated
dispersal_target_k = 10,
# dispersal_data: generated
# abundance_threshold: sampled,
occupancy_threshold = 1, # this was set to 2 in the mammoth model
# niche_ref: sampled
results_selection = c("abundance", "harvested", "human_density")
)
#### Step 2: Dynamic inputs ####
##### Carrying-capacity generator #####
cuts <- list.files(K_CUTS_DIR) %>% stringr::str_split("_") %>% purrr::map(~.[3:4]) %>% purrr::map_chr(paste0, collapse = "_")
capacity_gen <- Generator$new(description = "capacity",
region = region,
generate_rasters = FALSE, # use but don't generate
burn_in_steps = burn_in_steps,
generative_requirements = list(
hs_matrix = "file",
initial_abundance = "function",
carrying_capacity = "function"),
# these come from the latin hypercube sampler
inputs = c("density_max", "niche_ref"),
outputs = c("initial_abundance", "carrying_capacity"))
# Here we tell the generator to import the HS file and save it as "hs_matrix"
capacity_gen$add_file_template("hs_matrix",
path_template = file.path(K_CUTS_DIR, "clim_suit_%s_21k-10BP_SCALED.RDS"),
path_params = "niche_ref",
file_type = "RDS")
# Here we subset the hs_matrix to have only the region cells, and we add the burn in.
# Also, we tell the generator to generate the carrying_capacity based on "density_max" and "hs_matrix".
capacity_gen$add_function_template("carrying_capacity",
function_def = function(params) {
hs_matrix <- params$hs_matrix[params$region$region_indices,]
hs_matrix[!is.finite(hs_matrix)] <- 0
# repeat the first timestep n times as a burn in
hs_matrix <- cbind(replicate(params$burn_in_steps, hs_matrix[, 1]), hs_matrix)
# round the density values
round(params$density_max*hs_matrix)
},
call_params = c("density_max", "hs_matrix", "burn_in_steps", "region"))
# Here we tell the generator what function to use to generate initial_abundance
# based on the carrying capacity of the first time step
capacity_gen$add_function_template("initial_abundance",
function_def = function(params) {
params$carrying_capacity[, 1]
},
call_params = c("carrying_capacity"))
system.time({test_capacity <- capacity_gen$generate(input_values = list(density_max = 250,
niche_ref = "0.7_101"))}) # ~4
raster::plot(region$raster_from_values(test_capacity$carrying_capacity[,1000]),
colNA = "blue")
##### Dispersal generator #####
known_dispersals <- c(205, 190, 190, 190, 280, 280, 190, 155, 150, 125, 155, 125, 135, 135, 255, 260, 190)
summary(known_dispersals)
b_lookup <- data.frame(d_max = -Inf, b = 0:300)
for (i in 2:300) {
b_lookup$d_max[i] <- which.max(exp(-1*(1:501)/b_lookup$b[i]) <= 0.19)
}
b_lookup$d_max[301] <- 501
dispersal_gen <- DispersalGenerator$new(
region = region,
dispersal_max_distance = 500,
# km
distance_classes = seq(10, 500, 10),
distance_scale = 1000,
# km
dispersal_function_data = b_lookup,
dispersal_friction = DispersalFriction$new(conductance = friction),
inputs = c("dispersal_p",
"dispersal_r"),
decimals = 3
)
dispersal_gen$distance_data <- readRDS(file = file.path(DATA_DIR, "dispersal_distance_data.RDS"))
system.time(test_dispersal <- dispersal_gen$generate(input_values =
list(dispersal_p = 0.5,
dispersal_r = 400))$dispersal_data)
##### Human density generator #####
human_threshold_mean <- quantile(humans_mean_matrix[humans_mean_matrix > 0], 0.95, na.rm = FALSE)
human_density_gen <- Generator$new(description = "Human Density Generator",
humans_abundance = humans_mean_matrix,
humans_var = humans_sd_matrix,
spatial_correlation = env_corr,
human_threshold = human_threshold_mean,
# temporal_correlation = 0,
# time_steps = timesteps,
generate_rasters = FALSE,
generative_requirements = list(p_window = 'function',
human_density = 'distribution'),
inputs = c("p"),
outputs = c("human_density"))
human_density_gen$add_function_template("p_window",
function_def = function(params) {
w <- params$p*10/100
p_lower <- params$p - w
p_upper <- params$p + w
p_lower <- ifelse(p_lower < 0, 0, p_lower)
p_upper <- ifelse(p_upper > 1, 1, p_upper)
return(c(p_lower, p_upper))
},
call_params = c("p"))
human_density_gen$add_distribution_template("human_density",
distr_type = "lognormal",
distr_params = list(mean = "humans_abundance", sd = "humans_var"),
sample = c("p_window"),#list(mid = "p", window = 0.0),
normalize_threshold = "human_threshold")
## test the generator
system.time(test_humans <- human_density_gen$generate(input_values = list(p=0.1)))
raster::plot(region$raster_from_values(test_humans[[1]]), main = "Human density",
colNA = "blue")
rm(test_capacity, test_dispersal, test_humans); gc()
#### Step 3: Simulation ####
sample_data <- read.csv(file.path(DATA_DIR, "selected_parameters.csv"))
sim_manager <- SimulationManager$new(sample_data = sample_data,
model_template = model_template,
generators = list(capacity_gen,
dispersal_gen,
human_density_gen),
parallel_cores = parallel_cores,
results_dir = RESULTS_DIR)
sim_manager$results_filename_attributes <- c("sample", "results")
run_output <- sim_manager$run()
run_output$summary