并行分类栅格

时间:2014-04-28 14:37:54

标签: r parallel-processing classification r-raster

我想使用模型nb.mdl和栅格堆栈inStk对R中的某些栅格进行分类,但存在群集错误:

  

clus.classif中的错误(inStk,nb.mdl," test.tif"):群集错误

有关它来自哪里的任何想法?

clus.classif <- function(x, mdl, filename="") {
  out <- raster(x)
  cl <- getCluster()
  on.exit(returnCluster())

  nodes <- length(cl)
  bs <- blockSize(x, minblocks=nodes*4)
  pb <- pbCreate(bs$n)

  # the function to be used (simple example)
  clFun <- function(i) {

    v <- getValues(x, bs$row[i], bs$nrows[i])
    v.out <- predict(mdl,v)
    return(v.out)
  }

  # get all nodes going
  for (i in 1:nodes) {
    sendCall(cl[[i]], clFun, i, tag=i)
  }

  filename <- trim(filename)
  if (!canProcessInMemory(out) & filename == "") {
    filename <- rasterTmpFile()
  }

  if (filename != "") {
    out <- writeStart(out, filename=filename,overwrite=TRUE,options=c("COMPRESS=LZW",datatype='INT2S' ))
  } else {
    vv <- matrix(ncol=nrow(out), nrow=ncol(out))
  }

  for (i in 1:bs$n) {
    # receive results from a node
    d <- recvOneData(cl)
    # error?
    if (! d$value$success) {
      stop("cluster error")
    }
    # which block is this?
    b <- d$value$tag
    cat("received block: ",b,"\n"); flush.console();

    if (filename != "") {
      out <- writeValues(out, d$value$value, bs$row[b])
    } else {
      cols <- bs$row[b]:(bs$row[b] + bs$nrows[b]-1)
      vv[,cols] <- matrix(d$value$value, nrow=out@ncols)
    }

    # need to send more data?
    ni <- nodes + i
    if (ni <= bs$n) {
      sendCall(cl[[d$node]], clFun, ni, tag=ni)
    }
    pbStep(pb)
  }
  if (filename != "") {
    out <- writeStop(out)
  } else {
    out <- setValues(out, as.vector(vv))
  }
  pbClose(pb)
  return(out)
}



beginCluster(n=2,type="SOCK",nice=18)
print('Starting classification')
rr <- clus.classif(inStk,nb.mdl,'test.tif')
endCluster()

0 个答案:

没有答案