akensert / molgraph

Graph neural networks for molecular machine learning. Implemented and compatible with TensorFlow and Keras.
https://molgraph.readthedocs.io/en/latest/
MIT License
47 stars 5 forks source link

how to add additional atom_features on top of existing one #19

Closed thegodone closed 1 year ago

thegodone commented 1 year ago

I would like to add one new atom feature but I don't want to change your features default file.

How to add this atom feature from an new python file ?

I try this but it is not working:

from rdkit import Chem
from rdkit.Chem.EState import EStateIndices

@molgraph.chemistry.atom_features.register('estate')
class EState(molgraph.chemistry.Feature):
    def __call__(self, atom: Chem.Atom) -> float:
        mol = atom.GetOwningMol()
        res = EStateIndices(mol)
        val = res[atom.GetIdx()]
        if math.isnan(val) or val is None:
            return 0.0
        return val

atom_encoder = Featurizer([molgraph.chemistry.atom_features.get('estate')]

my error:

    atom_encoder = Featurizer([
  File "/Users/tgg/miniforge3/envs/tf/lib/python3.9/site-packages/molgraph-0.3.12-py3.9.egg/molgraph/chemistry/encoders.py", line 77, in __init__
  File "/Users/tgg/miniforge3/envs/tf/lib/python3.9/site-packages/molgraph-0.3.12-py3.9.egg/molgraph/chemistry/encoders.py", line 426, in _validate_features
ValueError: Invalid `features`.
akensert commented 1 year ago

From here:

def _validate_features(features: List[Feature]):
    dummy_mol = Chem.MolFromSmiles('CC')
    dummy_atom = dummy_mol.GetAtomWithIdx(0)
    dummy_bond = dummy_mol.GetBondWithIdx(0)
    try:
        # Check if features are atom features
        _ = [f(dummy_atom) for f in features]
        feature_type = 'atom'
    except:
        try:
            # Check if features are bond features
            _ = [f(dummy_bond) for f in features]
            feature_type = 'bond'
        except:
            feature_type = None

    if feature_type is None:
        raise ValueError('Invalid `features`.')

    return feature_type

( Not very informative value error :/ )

It checks whether the features of the Featurizer are atom features or bond features by testrunning the Featurizer on atoms or bonds respectively. (Not the prettiest implementation, but it was a quick implementation and it does its job)

So what happened is that it could not run your Featurizer (with its custom feature) on neither atoms nor bonds.

I checked what the error was:

NameError: name 'math' is not defined

So I guess the solution is to import the math library?

akensert commented 1 year ago

I will try to update the code in the near future to give a more informative error. You can keep this issue open.

EDIT: In addition to fixing the error message, should the Feature be implemented too (as a default feature of molgraph)? What is this (atom) feature?

thegodone commented 1 year ago

yes it works with import math thanks I did not check this fantastic

thegodone commented 1 year ago

I was just testing/experimenting potential new features and does not want to modify your code. OCHEM makes pip install molgraph. I don't want to change that