-
Notifications
You must be signed in to change notification settings - Fork 1
/
parBIOMOD_Modeling.R
145 lines (141 loc) · 8.41 KB
/
parBIOMOD_Modeling.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
parBIOMOD_Modeling <- function(data, models = c("GLM", "GBM", "GAM", "CTA", "ANN",
"SRE", "FDA", "MARS", "RF", "MAXENT.Phillips", "MAXENT.Phillips.2"),
models.options = NULL, NbRunEval = 1, DataSplit = 100, Yweights = NULL,
Prevalence = NULL, VarImport = 0, models.eval.meth = c("KAPPA",
"TSS", "ROC"), SaveObj = TRUE, rescal.all.models = FALSE,
do.full.models = TRUE, modeling.id = as.character(format(Sys.time(),
"%s")), ...)
{
.Models.dependencies(silent = TRUE, models.options = models.options)
args <- .Models.check.args(data, models, models.options,
NbRunEval, DataSplit, Yweights, VarImport, models.eval.meth,
Prevalence, do.full.models, SaveObj, ...)
models <- args$models
models.options <- args$models.options
NbRunEval <- args$NbRunEval
DataSplit <- args$DataSplit
Yweights <- args$Yweights
VarImport <- args$VarImport
models.eval.meth <- args$models.eval.meth
Prevalence <- args$Prevalence
do.full.models <- args$do.full.models
DataSplitTable <- args$DataSplitTable
SaveObj <- args$SaveObj
compress.arg = TRUE
rm(args)
models.out <- new("BIOMOD.models.out", sp.name = data@sp.name,
modeling.id = modeling.id, expl.var.names = colnames(data@data.env.var),
has.evaluation.data = data@has.data.eval, rescal.all.models = rescal.all.models)
.Models.prepare.workdir(data@sp.name, models.out@modeling.id)
if (SaveObj) {
save(data, file = file.path(models.out@sp.name, ".BIOMOD_DATA",
models.out@modeling.id, "formated.input.data"),
compress = compress.arg)
models.out@formated.input.data@inMemory <- FALSE
models.out@formated.input.data@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "formated.input.data")
save(models.options, file = file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.options"),
compress = compress.arg)
models.out@models.options@inMemory <- FALSE
models.out@models.options@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.options")
}
mod.prep.dat <- .Models.prepare.data(data, NbRunEval, DataSplit,
Yweights, Prevalence, do.full.models, DataSplitTable)
rm(data)
calib.lines <- mod.prep.dat[[1]]$calibLines
if (length(mod.prep.dat) > 1) {
for (pa in 2:length(mod.prep.dat)) {
calib.lines <- abind(calib.lines, mod.prep.dat[[pa]]$calibLines,
along = 3)
}
}
save(calib.lines, file = file.path(models.out@sp.name, ".BIOMOD_DATA",
models.out@modeling.id, "calib.lines"), compress = compress.arg)
models.out@calib.lines@inMemory <- FALSE
models.out@calib.lines@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "calib.lines")
rm(calib.lines)
.Models.print.modeling.summary(mod.prep.dat, models)
#### snowfall - Parallelization
# require(snowfall)
# sfInit(parallel=T, cpu=32)
# modeling.out <- sfLapply(mod.prep.dat, .Biomod.Models.loop,
# modeling.id = models.out@modeling.id, Model = models,
# Options = models.options, VarImport = VarImport, mod.eval.method = models.eval.meth,
# SavePred = SaveObj, scal.models = rescal.all.models)
# sfStop(nostop=F)
#### parallel - Parallelization
require(parallel)
elves <- makeCluster(20); clusterExport(elves, c('mod.prep.dat',
'.Biomod.Models.loop',
'models.out',
'models',
'models.options',
'VarImport',
'models.eval.meth',
'SaveObj',
'rescal.all.models'),
envir = environment()); clusterEvalQ(elves, library(biomod2))
modeling.out <- parLapply(elves, mod.prep.dat, .Biomod.Models.loop,
modeling.id = models.out@modeling.id, Model = models,
Options = models.options, VarImport = VarImport, mod.eval.method = models.eval.meth,
SavePred = SaveObj, scal.models = rescal.all.models)
closeAllConnections()
models.out@models.computed <- .transform.outputs.list(modeling.out,
out = "models.run")
models.out@models.failed <- .transform.outputs.list(modeling.out,
out = "calib.failure")
if (SaveObj) {
models.evaluation <- .transform.outputs.list(modeling.out,
out = "evaluation")
save(models.evaluation, file = file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.evaluation"),
compress = compress.arg)
models.out@models.evaluation@inMemory <- TRUE
models.out@models.evaluation@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.evaluation")
models.out@models.evaluation@val <- models.evaluation
rm(models.evaluation)
if (VarImport > 0) {
variables.importances <- .transform.outputs.list(modeling.out,
out = "var.import")
save(variables.importances, file = file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "variables.importance"),
compress = compress.arg)
models.out@variables.importances@inMemory <- TRUE
models.out@variables.importances@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "variables.importance")
models.out@variables.importances@val <- variables.importances
rm(variables.importances)
}
models.prediction <- .transform.outputs.list(modeling.out,
out = "prediction")
save(models.prediction, file = file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.prediction"),
compress = compress.arg)
models.out@models.prediction@inMemory <- FALSE
models.out@models.prediction@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.prediction")
rm(models.prediction)
models.prediction.eval <- .transform.outputs.list(modeling.out,
out = "prediction.eval")
save(models.prediction.eval, file = file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.prediction.eval"),
compress = compress.arg)
models.out@models.prediction.eval@inMemory <- FALSE
models.out@models.prediction.eval@link <- file.path(models.out@sp.name,
".BIOMOD_DATA", models.out@modeling.id, "models.prediction.eval")
rm(models.prediction.eval)
}
rm(modeling.out)
models.out@link <- file.path(models.out@sp.name, paste(models.out@sp.name,
".", models.out@modeling.id, ".models.out", sep = ""))
assign(x = paste(models.out@sp.name, ".", models.out@modeling.id,
".models.out", sep = ""), value = models.out)
save(list = paste(models.out@sp.name, ".", models.out@modeling.id,
".models.out", sep = ""), file = models.out@link)
.bmCat("Done")
return(models.out)
}