Skip to content
Prev 22440 / 29559 Next

parallel raster processing with calc and mc2d monte carlo simulation

Hi Robert,

Thanks for the advice.  So I tried exporting the objects defined in the function (lm.final and lambda_DV) with the following code:

library(raster)
library(rgdal)
library(mc2d)
brick <- brick(BC_BA, BC_BA_SE, SlopePer)  ## Stack three rasters into one RasterBrick
testbrick <- crop(brick, extent(299700, 300100, 1553550, 1553650)) ##Crop Raster Brick to manageable size

####
#### Predict Biomass using final linear model with Monte Carlo Simulation
ndunc(101)
fun.CROP_AGWBC <- function(x) {
  require(mc2d)
  dBC_BA <- mcdata(x[[1]], type="0")
  dBC_BA_SE <- mcdata(x[[2]], type = "0")
  SlopePer <-x[[3]]
  stBA <- mcstoc(rnorm, type = "U", rtrunc = TRUE, 
                 mean = dBC_BA, sd = dBC_BA_SE, linf = 0, lhs = FALSE)
  BC_AGWBC <- lm.final$coefficients[1] + 
    lm.final$coefficients[2]*stBA + 
    lm.final$coefficients[3]*SlopePer
  AGWBC <- (lambda_DV * BC_AGWBC + 1)^(1/lambda_DV)-1
  quantile(AGWBC[], c(0.025, 0.5, 0.975), na.rm=TRUE)
}

####
#### Check function using calc
CROP_AGWBC <- calc(testbrick, fun.CROP_AGWBC)
plot(CROP_AGWBC)
CROP_AGWBC

####
#### Run function using parallel processing 
library(snow)
beginCluster(8)
library(parallel)
cl <- getCluster()
clusterExport(cl, list("lm.final", "lambda_DV"))
clusterR(x = testbrick, fun = fun.CROP_AGWBC)
endCluster()


RESULT: Again, the calc process works fine to produce object CROP_AGWBC but when I try to run the last bit with parallel processing, I still get the same error:
[1] "data should be numeric or logical"
attr(,"class")
[1] "snow-try-error" "try-error"     
Error in clusterR(x = testbrick, fun = fun.CROP_AGWBC) : cluster error


I was suspicious that maybe the issue was still with the export because while ?lambda_DV" is a constant, lm.final is actually an lm model, so I replaced these objects in the function with constants (see code below) but still get the same error:

REVISED CODE WITH CONSTANTS: 
library(raster)
library(rgdal)
library(mc2d)
brick <- brick(BC_BA, BC_BA_SE, SlopePer)  ## Stack three rasters into one RasterBrick
testbrick <- crop(brick, extent(299700, 300100, 1553550, 1553650)) ##Crop Raster Brick to manageable size

####
#### Predict Biomass using final linear model with Monte Carlo Simulation
ndunc(101)
fun2.CROP_AGWBC <- function(x) {
  require(mc2d)
  dBC_BA <- mcdata(x[[1]], type="0")
  dBC_BA_SE <- mcdata(x[[2]], type = "0")
  SlopePer <-x[[3]]
  stBA <- mcstoc(rnorm, type = "U", rtrunc = TRUE, 
                 mean = dBC_BA, sd = dBC_BA_SE, linf = 0, lhs = FALSE)
  BC_AGWBC <- 0.6419 + 
    0.9307*stBA + 
    (-0.0176)*SlopePer
  AGWBC <- (0.2626263 * BC_AGWBC + 1)^(1/0.2626263)-1
  quantile(AGWBC[], c(0.025, 0.5, 0.975), na.rm=TRUE)
}

####
#### Check function using calc
CROP_AGWBC_2 <- calc(testbrick, fun2.CROP_AGWBC)
plot(CROP_AGWBC_2)
CROP_AGWBC_2

####
#### Run function using parallel processing 
library(snow)
beginCluster(8)
clusterR(x = testbrick, fun = fun2.CROP_AGWBC)
endCluster()

RESULT: same error:
[1] "data should be numeric or logical"
attr(,"class")
[1] "snow-try-error" "try-error"     
Error in clusterR(x = testbrick, fun = fun2.CROP_AGWBC) : cluster error


Any other thoughts?  Many thanks,

sean
On Mar 17, 2015, at 8:14 AM, Robert J. Hijmans <r.hijmans at gmail.com> wrote: