awslabs / dgl-lifesci

Python package for graph neural networks in chemistry and biology
Apache License 2.0
730 stars 151 forks source link

Error in AttentiveFPBondFeaturizer #123

Closed xnuohz closed 3 years ago

xnuohz commented 3 years ago

When self_loop in AttentiveFPBondFeaturizer is True, I got dgl._ffi.base.DGLError: Expect number of features to match number of edges. Got 87 and 60 instead.

Code:

import dgl

from dgllife.utils import smiles_to_bigraph, AttentiveFPAtomFeaturizer, AttentiveFPBondFeaturizer
from dgllife.model.model_zoo.attentivefp_predictor import AttentiveFPPredictor

config = {
    'node_feat': AttentiveFPAtomFeaturizer(),
    'edge_feat': AttentiveFPBondFeaturizer(self_loop=True)
}

smiles_lst = [
    'CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC=C4)N',
    'CC(C)(C)C1=CC(=NO1)NC(=O)NC2=CC=C(C=C2)C3=CN4C5=C(C=C(C=C5)OCCN6CCOCC6)SC4=N3',
    'CCN1CCN(CC1)CC2=C(C=C(C=C2)NC(=O)NC3=CC=C(C=C3)OC4=NC=NC(=C4)NC)C(F)(F)F'
]

gs = [smiles_to_bigraph(smiles,
                        node_featurizer=config['node_feat'],
                        edge_featurizer=config['edge_feat']) for smiles in smiles_lst]

gs = dgl.batch(gs)

model = AttentiveFPPredictor(node_feat_size=config['node_feat'].feat_size(),
                             edge_feat_size=config['edge_feat'].feat_size())

node_feats = gs.ndata.pop('h')
edge_feats = gs.edata.pop('e')

res = model(gs, node_feats, edge_feats)

print(res.size())
mufeili commented 3 years ago

You can fix the issue by replacing

gs = [smiles_to_bigraph(smiles,
                        node_featurizer=config['node_feat'],
                        edge_featurizer=config['edge_feat']) for smiles in smiles_lst]

with

gs = [smiles_to_bigraph(smiles,
                        add_self_loop=True,
                        node_featurizer=config['node_feat'],
                        edge_featurizer=config['edge_feat']) for smiles in smiles_lst]
xnuohz commented 3 years ago

Thanks:)