python - 无法为数组分配内存,rdkit 转换为 numpy 数组错误
问题描述
我有一个编码为 2048 位向量的 2215 个分子的列表。我想做的是从中创建二维数组。我正在使用rdkit
库转换为 numpy 数组。几周前代码运行没有问题,现在出现内存错误,但我不知道为什么。任何人都可以提供解决方案吗?
我试图使列表更小,并将其减少到两个向量。我认为这会有所帮助,但经过一段时间的处理后仍然会出现错误。这使我相信我实际上确实有足够的记忆力。
# red_fp is the list of bit vectors
def rdkit_numpy_convert(red_fp):
output = []
for f in fp:
arr = np.zeros((1,))
DataStructs.ConvertToNumpyArray(f, arr)
output.append(arr)
return np.asarray(output)
# this one line causes the problem
x = rdkit_numpy_convert(red_fp)
这是错误:
MemoryError Traceback (most recent call last)
MemoryError: cannot allocate memory for array
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<ipython-input-14-91594513666c> in <module>
----> 1 x = rdkit_numpy_convert(red_fp)
<ipython-input-13-78d1c9fdd07e> in rdkit_numpy_convert(red_fp)
4 for f in fp:
5 arr = np.zeros((1,))
----> 6 DataStructs.ConvertToNumpyArray(f, arr)
7 output.append(arr)
8 return np.asarray(output)
SystemError: <Boost.Python.function object at 0x55a2a5743520> returned a result with an error set
解决方案
我相信您的问题是您使用的指纹与这种转换为 numpy 数组的方法不兼容。
我不确定您使用的是什么类型的指纹,但假设您使用的是摩根指纹,我做了一些快速实验,当我使用“GetMorganFingerprint”方法与“GetMorganFingerprintAsBitVect”方法时,这种方法似乎挂起。我不确定为什么会出现这个问题,但我认为这是由于第一种方法产生 UIntSparseIntVect 与 ExplicitBitVect 的事实,尽管我发现当我尝试使用由“GetHashedMorganFingerprint”产生的指纹的相同方法时,它也返回一个 UIntSparseIntVect 它工作正常。
如果您使用摩根指纹,我建议您尝试“GetMorganFingerprintAsBitVect”方法
编辑:
我又做了几个实验
mol = Chem.MolFromSmiles('c1ccccc1')
fp = AllChem.GetMorganFingerprint(mol, 2)
print(fp.GetLength())
'4294967295'
fp1 = AllChem.GetMorganFingerprintAsBitVect(mol, 2)
print(fp1.GetNumBits())
'2048'
fp2 = AllChem.GetHashedMorganFingerprint(mol, 2)
print(fp2.GetLength())
'2048'
如您所见,第一种方法的指纹很大,我最初的想法是该指纹处于展开状态,因此使用了稀疏数据结构,这可以解释为什么您在尝试为指纹分配内存时遇到问题这个维度。
推荐阅读
- excel - 使用 excel 公式将列中的内容移过空列
- html - 在单独的 Div 中选择元素的多个通用同级元素
- javascript - 获取 api 时未调用 useEffect 且未更新状态
- python - 从熊猫数据框到以n维数组为特征的数据框
- bash - 为什么 bash 脚本在接收 Sigterm 时退出父脚本之前不等待其子进程完成?
- javascript - 另一个数组中的方法中的数组 Javascript/动作脚本
- hdfs - 使用来自 HDFS NFS 挂载的 mssql-tools bcp
- servicenow - 从应用程序日志生成 ServiceNow 票证
- linux - 如何同步目标文件夹中存在的所有文件夹
- validation - 如何修复颤振流输入验证中的“未定义名称”?