R 中大栅格的并行处理 (windows)
Parallel processing of big rasters in R (windows)
我正在使用 doSNOW 包,更具体地说是 parLapply 函数对大型栅格数据集列表 (OS: Windows x64) 执行重新分类(以及随后的其他操作)。
代码看起来有点像这个简约示例:
library(raster)
library(doSNOW)
#create list containing test rasters
x <- raster(ncol=10980,nrow=10980)
x <- setValues(x,1:ncell(x))
list.x <- replicate( 9 , x )
#setting up cluster
NumberOfCluster <- 8
cl <- makeCluster(NumberOfCluster)
registerDoSNOW(cl)
junk <- clusterEvalQ(cl,library(raster))
#perform calculations on each raster
list.x <- parLapply(cl,list.x,function(x) calc(x,function(x) { x * 10 }))
#stop cluster
stopCluster(cl)
代码实际上按预期工作。当我想继续处理结果时出现问题。我收到此错误消息:
> plot(list.x[[1]])
Error in file(fn, "rb") : cannot open the connection
In addition: Warning message:
In file(fn, "rb") :
cannot open file 'C:\Users\*****\AppData\Local\Temp\RtmpyKYdpY\raster\r_tmp_2016-02-29_133158_752_67867.gri': No such file or directory
据我了解,由于栅格非常大,因此它们保存在磁盘上的临时文件中。当我关闭 snow 集群时,这些文件将无法再访问。
所以我的问题是,集群关闭后如何访问数据?我可以继续使用这个方法吗?
谢谢!
您可以将特定的文件名传递给 calc
(或者,例如 reclassify
),并让您的函数 return 将这些文件名作为要读入堆栈的向量:
ff <- parSapply(cl, list.x, function(x) {
calc(x, function(x) x*10, filename=f <- tempfile(fileext='.tif'))
f
})
s <- stack(ff)
但还要看看 ?clusterR
- 我怀疑它会与 reclassify
一起使用。来自文档:
This function only works with functions that have a Raster* object as first argument and that operate on a cell by cell basis (i.e., there is no effect of neigboring cells) and return an object with the same number of cells as the input raster object. The first argument of the function called must be a Raster* object. There can only be one Raster* object argument. For example, it works with calc and it also works with overlay as long as you provide a single RasterStack or RasterBrick as the first argument.
我在 运行 R 集群内的光栅化功能时遇到了这个问题。
所有测试都运行良好,但是当我放大到非常大且分辨率很高的栅格时,我反复遇到有关临时文件的错误,我什至无法在我的计算机上找到这些错误。我需要合并并写为 1 个光栅的列表对象在 R 中,但我无能为力。
在集群 运行 查看临时文件目录后,我注意到关闭集群将 auto-delete 创建所有临时文件,因此我必须在集群内执行合并和 writeRaster 函数,否则它会因与您的错误非常相似的错误而失败。
我正在使用 doSNOW 包,更具体地说是 parLapply 函数对大型栅格数据集列表 (OS: Windows x64) 执行重新分类(以及随后的其他操作)。
代码看起来有点像这个简约示例:
library(raster)
library(doSNOW)
#create list containing test rasters
x <- raster(ncol=10980,nrow=10980)
x <- setValues(x,1:ncell(x))
list.x <- replicate( 9 , x )
#setting up cluster
NumberOfCluster <- 8
cl <- makeCluster(NumberOfCluster)
registerDoSNOW(cl)
junk <- clusterEvalQ(cl,library(raster))
#perform calculations on each raster
list.x <- parLapply(cl,list.x,function(x) calc(x,function(x) { x * 10 }))
#stop cluster
stopCluster(cl)
代码实际上按预期工作。当我想继续处理结果时出现问题。我收到此错误消息:
> plot(list.x[[1]])
Error in file(fn, "rb") : cannot open the connection
In addition: Warning message:
In file(fn, "rb") :
cannot open file 'C:\Users\*****\AppData\Local\Temp\RtmpyKYdpY\raster\r_tmp_2016-02-29_133158_752_67867.gri': No such file or directory
据我了解,由于栅格非常大,因此它们保存在磁盘上的临时文件中。当我关闭 snow 集群时,这些文件将无法再访问。
所以我的问题是,集群关闭后如何访问数据?我可以继续使用这个方法吗?
谢谢!
您可以将特定的文件名传递给 calc
(或者,例如 reclassify
),并让您的函数 return 将这些文件名作为要读入堆栈的向量:
ff <- parSapply(cl, list.x, function(x) {
calc(x, function(x) x*10, filename=f <- tempfile(fileext='.tif'))
f
})
s <- stack(ff)
但还要看看 ?clusterR
- 我怀疑它会与 reclassify
一起使用。来自文档:
This function only works with functions that have a Raster* object as first argument and that operate on a cell by cell basis (i.e., there is no effect of neigboring cells) and return an object with the same number of cells as the input raster object. The first argument of the function called must be a Raster* object. There can only be one Raster* object argument. For example, it works with calc and it also works with overlay as long as you provide a single RasterStack or RasterBrick as the first argument.
我在 运行 R 集群内的光栅化功能时遇到了这个问题。
所有测试都运行良好,但是当我放大到非常大且分辨率很高的栅格时,我反复遇到有关临时文件的错误,我什至无法在我的计算机上找到这些错误。我需要合并并写为 1 个光栅的列表对象在 R 中,但我无能为力。
在集群 运行 查看临时文件目录后,我注意到关闭集群将 auto-delete 创建所有临时文件,因此我必须在集群内执行合并和 writeRaster 函数,否则它会因与您的错误非常相似的错误而失败。