【发布时间】:2016-06-11 00:38:24
【问题描述】:
我的目标
因此,我尝试使用 巨大 矩阵上的 rpy2 包从 python3 脚本中的 preprocessCore R 包 (R-3.2.1) 调用 normalize_quantiles 函数( 10 GB 以上的文件)。我几乎有无限的记忆。当我通过 R 本身运行它时,我能够完成规范化并打印到输出:
require(preprocessCore);
all <- data.matrix(read.table("data_table.txt",sep="\t",header=TRUE));
all[,6:57]=normalize.quantiles(all[,6:57]);
write.table(all,"QN_data_table.txt",sep="\t",row.names=FALSE);
我正在尝试将它构建到一个 python 脚本中,该脚本还使用rpy2 python 包执行其他操作,但我在构建矩阵的方式上遇到了问题。下面是一个例子:
matrix = sample_list # My 2-d python array containing the data.
v = robjects.FloatVector([ element for col in matrix for element in col ])
m = robjects.r['matrix'](v, ncol = len(matrix), byrow=False)
print("Performing quantile normalization.")
Rnormalized_matrix = preprocessCore.normalize_quantiles(m)
norm_matrix = np.array(Rnormalized_matrix)
return header, pos_list, norm_matrix
问题
这适用于较小的文件,但是当我在我的大文件上运行它时,它会因错误而死:rpy2.rinterface.RRuntimeError: Error: cannot allocate vector of size 9.7 Gb
我知道 R 的向量的最大大小是 8 Gb,这就解释了为什么会抛出上述错误。 rpy2 docs 说:
“矩阵是数组的一种特殊情况。与数组一样,必须记住这只是一个具有维度属性(行数、列数)的向量。”
我有点想知道它是如何严格遵守这一点的,所以我更改了我的代码以初始化我想要的大小的矩阵,然后遍历并将元素分配给值:
matrix = sample_list # My 2-d python array of data.
m_count = 1
m = robjects.r['matrix'](0.0, ncol=len(matrix), nrow=len(matrix[0]))
for samp in matrix:
i_count = 1
for entry in samp:
m.rx[i_count, m_count] = entry # Assign the data to the element.
i_count += 1
m_count += 1
print("Performing quantile normalization.")
Rnormalized_matrix = preprocessCore.normalize_quantiles(m)
norm_matrix = np.array(Rnormalized_matrix)
return header, pos_list, norm_matrix
同样,这适用于较小的文件,但会因与之前相同的错误而崩溃。
所以我的问题是允许在 R 中分配巨大的矩阵但在 rpy 中导致问题的根本区别是什么?我需要用不同的方法来解决这个问题吗?我应该把它吸起来并在R中做吗?或者有没有办法绕过我遇到的问题?
【问题讨论】:
标签: r python-3.x memory matrix rpy2