You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RDKit中Draw.MolsToGridImage报ArgumentError错误求助

RDKit MolsToGridImage 抛出 ArgumentError 错误

使用RDKit的rdkit.Chem.Draw.MolsToGridImage函数时触发ArgumentError,错误提示MolDraw2DCairo构造函数的Python参数类型与C++签名不匹配——传入了浮点数,但要求整数类型。

错误栈

---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
Cell In [72], line 7
      5 subs = [x[2] for x in reaction_.Substrate()]
      6 prods =  [x[2] for x in reaction_.Product()]
----> 7 display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(1000/len(subs),200), molsPerRow=len(subs)))
      8 display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in prods], legends=prods, subImgSize=(1000/len(prods),200), molsPerRow=len(prods)))

File ~/opt/anaconda3/lib/python3.9/site-packages/rdkit/Chem/Draw/IPythonConsole.py:271, in ShowMols(mols, maxMols, **kwargs)
    268 if "drawOptions" not in kwargs:
    269   kwargs["drawOptions"] = drawOptions
---> 271 res = fn(mols, **kwargs)
    272 if InteractiveRenderer.isEnabled():
    273   return HTML(res)

File ~/opt/anaconda3/lib/python3.9/site-packages/rdkit/Chem/Draw/__init__.py:620, in MolsToGridImage(mols, molsPerRow, subImgSize, legends, highlightAtomLists, highlightBondLists, useSVG, returnPNG, **kwargs)
    616   return _MolsToGridSVG(mols, molsPerRow=molsPerRow, subImgSize=subImgSize, legends=legends,
    617                         highlightAtomLists=highlightAtomLists,
    618                         highlightBondLists=highlightBondLists, **kwargs)
    619 else:
---> 620   return _MolsToGridImage(mols, molsPerRow=molsPerRow, subImgSize=subImgSize, legends=legends,
    621                           highlightAtomLists=highlightAtomLists,
    622                           highlightBondLists=highlightBondLists, returnPNG=returnPNG, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/rdkit/Chem/Draw/__init__.py:554, in _MolsToGridImage(mols, molsPerRow, subImgSize, legends, highlightAtomLists, highlightBondLists, drawOptions, returnPNG, **kwargs)
    552 else:
    553   fullSize = (molsPerRow * subImgSize[0], nRows * subImgSize[1])
---> 554   d2d = rdMolDraw2D.MolDraw2DCairo(fullSize[0], fullSize[1], subImgSize[0], subImgSize[1])
    555   if drawOptions is not None:
    556     d2d.SetDrawOptions(drawOptions)

ArgumentError: Python argument types in
    MolDraw2DCairo.__init__(MolDraw2DCairo, float, int, float, int)
did not match C++ signature:
    __init__(_object*, int width, int height, int panelWidth=-1, int panelHeight=-1, bool noFreetype=False)

相关代码

for reaction_ID in under_predicted_reactions:
    print(reaction_ID)
    print(under_predicted_reactions[reaction_ID])
    reaction_ = recon_reactions[reaction_ID]
    subs = [x[2] for x in reaction_.Substrate()]
    prods =  [x[2] for x in reaction_.Product()]
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(1000/len(subs),200), molsPerRow=len(subs)))
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in prods], legends=prods, subImgSize=(1000/len(prods),200), molsPerRow=len(prods)))

问题原因与解决方法

问题出在subImgSize参数的计算上:1000/len(subs)和1000/len(prods)在Python中会返回浮点数,但RDKit底层的C++构造函数要求传入整数类型的尺寸值。

方案1:使用int()强制转换

for reaction_ID in under_predicted_reactions:
    print(reaction_ID)
    print(under_predicted_reactions[reaction_ID])
    reaction_ = recon_reactions[reaction_ID]
    subs = [x[2] for x in reaction_.Substrate()]
    prods =  [x[2] for x in reaction_.Product()]
    # 转换subImgSize的第一个值为整数
    sub_img_width_subs = int(1000 / len(subs))
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(sub_img_width_subs,200), molsPerRow=len(subs)))
    
    sub_img_width_prods = int(1000 / len(prods))
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in prods], legends=prods, subImgSize=(sub_img_width_prods,200), molsPerRow=len(prods)))

方案2:使用整数除法//

for reaction_ID in under_predicted_reactions:
    print(reaction_ID)
    print(under_predicted_reactions[reaction_ID])
    reaction_ = recon_reactions[reaction_ID]
    subs = [x[2] for x in reaction_.Substrate()]
    prods =  [x[2] for x in reaction_.Product()]
    # 用整数除法直接得到整数结果
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(1000//len(subs),200), molsPerRow=len(subs)))
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in prods], legends=prods, subImgSize=(1000//len(prods),200), molsPerRow=len(prods)))

注意:如果len(subs)或len(prods)为0,需要先做判断避免除以0错误,从代码逻辑看应该不会出现这种情况。

内容的提问来源于stack exchange,提问作者Helena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 12:52:41