dgl-lifesci icon indicating copy to clipboard operation
dgl-lifesci copied to clipboard

How to obtain the categorical node features for GIN and GNNOGB?

Open Wang-Lin-boop opened this issue 1 year ago • 3 comments

When calling the GIN or GNNOGB modules, the forward function requires a LongTensor storing the node categorical features, which looks like it may be an attributes of DGLGraph, but I can't find a way to get it in the DGL documentation.

If I can get a dictionary from BaseAtomFeaturizer to store node categorical ID and its features, a function that seems easy to implement manually as well, does BaseAtomFeaturizer support getting a dictionary of a node's categorical features?

Progressively refining this dictionary by analyzing the nodes of each batch seems like a viable option, but it doesn't seem elegant enough that I can't believe it's the right one. Can anyone tell me how you solved this problem? I'am looking forward to you reply.

Thanks!

Wang-Lin-boop avatar Jul 26 '23 08:07 Wang-Lin-boop

When calling the GIN or GNNOGB modules, the forward function requires a LongTensor storing the node categorical features, which looks like it may be an attributes of DGLGraph, but I can't find a way to get it in the DGL documentation.

In the context of molecules, most likely they are atom types. It depends on if your graph already has atom types extracted and stored.

If I can get a dictionary from BaseAtomFeaturizer to store node categorical ID and its features, a function that seems easy to implement manually as well, does BaseAtomFeaturizer support getting a dictionary of a node's categorical features?

Yes, it returns a dictionary of node features, as can be found in the example here: https://github.com/awslabs/dgl-lifesci/blob/master/python/dgllife/utils/featurizers.py#L858

Progressively refining this dictionary by analyzing the nodes of each batch seems like a viable option, but it doesn't seem elegant enough that I can't believe it's the right one. Can anyone tell me how you solved this problem? I'am looking forward to you reply.

It's generally recommended to first extract node features for each molecule and then save them in the corresponding DGLGraph. When you batch multiple graphs corresponding to molecules, their features will be also batched.

mufeili avatar Jul 26 '23 09:07 mufeili

In the context of molecules, most likely they are atom types. It depends on if your graph already has atom types extracted and stored.

For my featurizer BaseAtomFeaturizer({'atom_type':ConcatFeaturizer([atom_type_one_hot, atom_hybridization_one_hot, atom_formal_charge, atom_chiral_tag_one_hot, atom_is_in_ring, atom_is_aromatic])}), the node features is a long vector, look like (for OCCOCC) :

tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0.,
         0.],
        [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0.,
         0.],
        [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0.,
         0.],
        [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0.,
         0.],
        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0.,
         0.],
        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0.,
         0.]])

It looks like I can't get node categorical features directly using the existing API.

Wang-Lin-boop avatar Jul 26 '23 12:07 Wang-Lin-boop

You can use https://github.com/awslabs/dgl-lifesci/blob/master/python/dgllife/utils/featurizers.py#L179

mufeili avatar Jul 26 '23 13:07 mufeili