Skip to content

Commit

Permalink
Merge pull request GEO-BON#110 from GEO-BON/sdm
Browse files Browse the repository at this point in the history
Maxent pipeline: some docs, bug fix, defaults and simplified create background function
  • Loading branch information
glaroc authored Nov 14, 2023
2 parents 1a12a53 + b98ca25 commit 70ba4bd
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 184 deletions.
27 changes: 21 additions & 6 deletions pipelines/SDM/SDM_maxEnt.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@
"data": {
"type": "text[]",
"value": [
"L",
"LQ",
"LQHP"
]
Expand All @@ -384,8 +383,7 @@
"type": "float[]",
"value": [
0.5,
1,
2
1
]
}
},
Expand Down Expand Up @@ -763,7 +761,8 @@
"random",
"inclusion_buffer",
"weighted_raster",
"unweighted_raster"
"unweighted_raster",
"thickening"
],
"example": "random"
},
Expand Down Expand Up @@ -866,14 +865,18 @@
"type": "text[]",
"example": [
"chelsa-clim|bio1",
"chelsa-clim|bio2"
"chelsa-clim|bio7",
"chelsa-clim|bio12",
"earthenv_topography|elevation_median",
"earthenv_landcover|class_3",
"earthenv_landcover|class_12"
]
},
"data>loadFromStac.yml@119|spatial_res": {
"description": "Integer, spatial resolution of the rasters",
"label": "spatial resolution",
"type": "float",
"example": 1000.0
"example": 5000.0
},
"data>loadFromStac.yml@119|mask": {
"description": "Shapefile, used to mask the output rasters",
Expand All @@ -896,5 +899,17 @@
1
]
}
},
"metadata": {
"name": "Maxent SDM pipeline",
"description": "A pipeline to produce species distribution models using Maxent",
"author": [
{
"name": "Sarah Valentin",
"identifier": "https://orcid.org/0000-0002-9028-681X"
}
],
"license": null,
"external_link": null
}
}
12 changes: 6 additions & 6 deletions scripts/SDM/runMaxent.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ print("Inputs : ")
print(input)


presence_background <- read.table(file = input$presence_background, sep = '\t', header = TRUE)
presence_background <- read.table(file = input$presence_background, sep = '\t', header = TRUE, check.names = FALSE)
predictors <- terra::rast(unlist(input$predictors))
mod_tuning <- run_maxent(presence_background,
with_raster = F, # can be set to F to speed up
Expand All @@ -52,7 +52,7 @@ mod_tuning <- run_maxent(presence_background,
res_tuning <- mod_tuning@results
tuned_param <- select_param(res_tuning, method = input$method_select_params, list = T)

predictors <- raster::stack(predictors)
#predictors <- raster::stack(predictors)

sdms <- predict_maxent(presence_background,
algorithm = "maxent.jar",
Expand All @@ -73,17 +73,17 @@ names(sdm_pred) <- "prediction"
sdm_runs <- sdms[["pred_runs"]]

pred.output <- file.path(outputFolder, "sdm_pred.tif")
runs.output <- paste0(outputFolder,"/sdm_runs_", 1:nlayers(sdm_runs), ".tif")
runs.output <- paste0(outputFolder,"/sdm_runs_", 1:nlyr(sdm_runs), ".tif")
#runs.output <- file.path(outputFolder, "sdm_runs.tif")

sdm_pred<-project(rast(sdm_pred),crs(input$proj)) ##Temporary fix while maxent transitions to terra
sdm_pred<-project(sdm_pred,crs(input$proj)) ##Temporary fix while maxent transitions to terra
terra::writeRaster(x = sdm_pred,
filename = pred.output,
filetype = "COG",
wopt= list(gdal=c("COMPRESS=DEFLATE")),
overwrite = TRUE)
for (i in 1:nlayers(sdm_runs)){
thisrun<-project(rast(sdm_runs[[i]]),crs(input$proj)) ##Temporary fix while maxent transitions to terra
for (i in 1:nlyr(sdm_runs)){
thisrun<-project(sdm_runs[[i]],crs(input$proj)) ##Temporary fix while maxent transitions to terra
terra::writeRaster(x = thisrun,
filename = file.path(outputFolder, paste0("/sdm_runs_", i, ".tif")),
filetype = "COG",
Expand Down
30 changes: 18 additions & 12 deletions scripts/SDM/runMaxentFunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ run_maxent <- function(presence.bg, with_raster = F,
parallelType = "doParallel"
) {

presence <- presence.bg |> dplyr::filter(pa == 1) |> data.frame()
background <- presence.bg |> dplyr::filter(pa == 0) |> data.frame()
presence <- presence.bg |> dplyr::filter(pa == 1) |> as.data.frame()
background <- presence.bg |> dplyr::filter(pa == 0) |> as.data.frame()

if (with_raster) {
ENMmodel <- ENMeval::ENMevaluate(occs = presence[, c("lon", "lat")],
Expand Down Expand Up @@ -259,8 +259,8 @@ predict_maxent <- function(presence_background,

# We calculate the prediction with the whole dataset

presence <- presence_background |> dplyr::filter(pa == 1) |> data.frame()
background <- presence_background |> dplyr::filter(pa == 0) |> data.frame()
presence <- presence_background |> dplyr::filter(pa == 1) |> as.data.frame()
background <- presence_background |> dplyr::filter(pa == 0) |> as.data.frame()


mod_tuning <- ENMeval::ENMevaluate(occs = presence[, c("lon", "lat", layers)],
Expand Down Expand Up @@ -304,8 +304,8 @@ runs <- c("run_1")
backg_test <- backgr[bg.grp == g, ]
presence_bg_train <- group.all[which(group.all[,1] == g),]

presence <- presence_bg_train |> dplyr::filter(pa == 1) |> data.frame()
background <- presence_bg_train |> dplyr::filter(pa == 0) |> data.frame()
presence <- presence_bg_train |> dplyr::filter(pa == 1) |> as.data.frame()
background <- presence_bg_train |> dplyr::filter(pa == 0) |> as.data.frame()


mod_tuning <- ENMeval::ENMevaluate(occs = presence[, c("lon", "lat", layers)],
Expand All @@ -322,12 +322,18 @@ runs <- c("run_1")
pred_pres <- dismo::predict(mod_tuning@models[[tuned_param]], predictors,
args = sprintf("outputformat=%s", type))

if (inherits(pred_runs, "RasterLayer") || inherits(pred_runs, "RasterStack")) {
pred_runs <- raster::stack(pred_runs, pred_pres)

} else {
pred_runs <- pred_pres
}
#if (inherits(pred_runs, "RasterLayer") || inherits(pred_runs, "RasterStack")) {
# pred_runs <- raster::stack(pred_runs, pred_pres)
#} else {
# pred_runs <- pred_pres
#}
if (is.null(pred_runs)) {
pred_runs <- pred_pres
} else {
pred_runs <- c(pred_runs, pred_pres)
}



}

Expand Down
1 change: 1 addition & 0 deletions scripts/SDM/selectBackground.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ inputs:
- inclusion_buffer
- weighted_raster
- unweighted_raster
- thickening
example: random
n_background:
label: number of background points
Expand Down
Loading

0 comments on commit 70ba4bd

Please sign in to comment.