Running raster::stackApply() function in parallel

旧时模样 提交于 2020-01-03 04:45:14

问题


I'm trying to parallelize this example.

I have a bunch of rasters that I am trying to aggregate by week of the year. Here is what this looks like in series:

# create a raster stack from list of GeoTiffs
tifs <- list.files(path = "./inputData/", pattern = "\\.tif$", full.names = TRUE)
r <- stack(tifs)

# get the date from the names of the layers and extract the week
indices <- format(as.Date(names(r), format = "X%Y.%m.%d"), format = "%U")
indices <- as.numeric(indices)

# calculate weekly means
r_week <- stackApply(r, indices, function(x) mean(x, na.rm = TRUE))

This is my attempt at parallelization using snow and pbapply.

# aggregate rasters in parallel
no_cores <- parallel::detectCores() - 1 

tryCatch({
  cl <- snow::makeCluster(no_cores, "SOCK")
  snow::clusterEvalQ(cl, {
    require(pacman)
    p_load(dplyr
           ,rts
           ,raster
           ,stringr
           ,pbapply
           ,parallel)
  })
  parallel::clusterExport(cl = cl, varlist = list("r", "indices"))
  r_week <-  pbapply::pbsapply(r, indices, stackApply(r, indices, function(x) mean(x, na.rm = TRUE)), simplify = TRUE, USE.NAMES = TRUE, cl = cl)
  snow::stopCluster(cl)
}, error=function(e){
  snow::stopCluster(cl)
  return(e)
}, finally = {
  try(snow::stopCluster(cl), silent = T)
})

The stackApply() method does not take a cluster argument, so I'm trying to wrap it in a pbsapply(). This returns the following error:

<simpleError in get(as.character(FUN), mode = "function", envir = envir): object 'indices' of mode 'function' was not found>

回答1:


I think I found a workaround using the raster::clusterR() method. It doesn't provide a progress bar though. It would be great to see if someone knows how to do this with snow and pbapply.

tryCatch({
  system.time({
  no_cores <- parallel::detectCores() - 1
  raster::beginCluster(no_cores)
  myFun <- function(x, ...) {
    mean(!is.na(x))
  }
  r_week <- raster::clusterR(r, stackApply, args=list(indices = indices, fun = myFun, na.rm = TRUE))
  raster::endCluster()})
}, error = function(e) {
  raster::endCluster()
  return(e)
}, finally = {
  try(raster::endCluster())
})



回答2:


Try adding progress='text' to the stackApply arguments. It works fine in the non-parallelized version. Good luck!



来源:https://stackoverflow.com/questions/52393703/running-rasterstackapply-function-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!