【问题标题】:How to chunk array processing in R?如何在 R 中对数组进行分块处理?
【发布时间】:2013-01-31 20:10:12
【问题描述】:

我正在计算两个数据集之间的相关性,但由于数据量很大(10 GB),而我的 RAM 只有 6 GB,我面临内存问题。我想知道如何分块我的代码?

dir1 <- list.files("D:sdr", "*.bin", full.names = TRUE)
dir2 <- list.files("D:dsa", "*.img", full.names = TRUE)
file_tot<-array(dim=c(1440,720,664,2))
for(i in 1:length(dir1)){
  file_tot[,,i,1] <- readBin(dir1[i], numeric(), size = 4 ,n = 1440 * 720 , signed = T)
  file_tot[,,i,2] <- readBin(dir2[i], integer(), size = 2 ,n = 1440 * 720 , signed = F)
  file_tot[,,i,2] <- file_tot[,,i,2]*0.000030518594759971
  file_tot[,,i,2][file_tot[,,i,2] ==  9999 ] <- NA
}
result<-apply(file_tot,c(1,2),function(x){cor(x[,1],x[,2])})

但是得到了这个错误:

 Error: cannot allocate vector of size 10.3 Gb
In addition: Warning messages:
 1: In file_tot[, , i, 1] <- readBin(dir1[i], numeric(), size = 4, n = 1440 *  :
Reached total allocation of 16367Mb: see help(memory.size)
2: In file_tot[, , i, 1] <- readBin(dir1[i], numeric(), size = 4, n = 1440 *  :
Reached total allocation of 16367Mb: see help(memory.size)
3: In file_tot[, , i, 1] <- readBin(dir1[i], numeric(), size = 4, n = 1440 *  :
Reached total allocation of 16367Mb: see help(memory.size)
4: In file_tot[, , i, 1] <- readBin(dir1[i], numeric(), size = 4, n = 1440 *  :
Reached total allocation of 16367Mb: see help(memory.size)

【问题讨论】:

    标签: r correlation chunking chunks


    【解决方案1】:

    如果您只是计算这种相关性,您实际上不需要切换到ffbigmemory 等软件包。您可以分块处理文件。当您计划使用其中一种大数据包进行更多分析时可能会很有用。

    这是一个如何分块处理文件的示例:

    # Generate some data; in this case I only use 7 columns,
    # but it should scale to any number of columns (except 
    # perhaps generating the files)
    dim <- c(1440, 7, 664, 2)
    # The last line should be replaced by the next for the data in 
    # the question
    # dim <- c(1440, 770, 664, 2)
    for (i in seq_len(dim[3])) {
      dat <- rnorm(dim[1]*dim[2])
      writeBin(dat, paste0("file", i, ".bin"), size = 4)
      dat <- rnorm(dim[1]*dim[2])
      writeBin(dat, paste0("file", i, ".img"), size = 4)
    }
    
    dir1 <- list.files("./", "*.bin", full.names = TRUE)
    dir2 <- list.files("./", "*.img", full.names = TRUE)
    
    result <- array(dim=c(dim[1], dim[2]))
    file_tot<-array(dim=c(dim[1], dim[3], dim[4]))
    
    # Proces the files column by column
    for (j in seq_len(dim[2])) {
      for(i in 1:length(dir1)){
        # Open first file
        con <- file(dir1[i], 'rb')
        # Skip to the next column
        seek(con, (j-1)*dim[1]*4)
        # Read colum
        file_tot[,i,1] <- readBin(con, numeric(), size = 4 ,n = dim[1])
        close(con)
    
        # And repeat for the next file
        con <- file(dir2[i], 'rb')
        seek(con, (j-1)*dim[1]*4)
        file_tot[,i,2] <- readBin(con, numeric(), size = 4 ,n = dim[1])
        # For the datasets in the example the previous line should be replaced
        # by the next three:
        #file_tot[,i,2] <- readBin(con, integer(), size = 2 ,n = dim[1] , signed = F)
        #file_tot[,i,2] <- file_tot[,i,2]*0.000030518594759971
        #file_tot[,i,2][file_tot[,i,2] ==  9999 ] <- NA
        close(con)
      }
      result[,j] <-apply(file_tot,c(1),function(x){cor(x[,1],x[,2])})
    }
    

    【讨论】:

    • 如果我把 7 改成 10,我还需要改什么吗?
    • 你的意思是7到720?是的,应该是这样,并且您的 img 文件是 2 字节整数,并且您使用缺失值执行某些操作。我把它漏掉了。
    • 不,我的意思是列数,增加列数以节省时间,它是 1440 而不是 1140
    • 我编辑了我的答案以指明应该更改哪些内容以运行数据集的代码
    • 我运行代码,它没有错误地工作。所以接下来我再次运行接下来的 7 列还是什么?。如果代码没有块,我将结果写为 ####### 'to.write = file(paste("C:\\corrCCIandSMOS2010.bin",sep=""),"wb")#################### writeBin(as.double(result), to.write, size = 4)############但是使用chunk的时候怎么写
    【解决方案2】:

    处理大数据时非常常见的问题。幸运的是,有几种解决方案:

    1. 使用大数据包,如 rhadoop。
    2. 使用滚动窗口文件读取包,如 ff 和 filehash。
    3. 使用 bigmemory 和相关包,请在下面查找链接。

    您可能会发现有用的链接:

    difference between ff and filehash package in R

    In R which packages for loading larger data quickly

    Example of bigmemory and friends with file backing

    Work in R with very large data set

    另外,I'd suggest you did this, but I did it for you.

    希望一些研究可以解决这个问题!祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      相关资源
      最近更新 更多