Commit 05c9001f authored by alat-rights's avatar alat-rights
Browse files

Changed one hot to be subclass of Featurizer

parent 921f06dc
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ import numpy as np

from deepchem.utils.typing import RDKitMol
from deepchem.utils.molecule_feature_utils import one_hot_encode
from deepchem.feat.base_classes import MolecularFeaturizer
from deepchem.feat.base_classes import Featurizer 

logger = logging.getLogger(__name__)

@@ -16,7 +16,7 @@ ZINC_CHARSET = [
]


class OneHotFeaturizer(MolecularFeaturizer):
class OneHotFeaturizer(Featurizer):
  """Encodes SMILES as a one-hot array.

  This featurizer encodes SMILES string as a one-hot array.
@@ -69,7 +69,7 @@ class OneHotFeaturizer(MolecularFeaturizer):
        for val in string 
    ])

  def _featurizeMol(self, mol: RDKitMol) -> np.ndarray:
  def featurizeMol(self, mol: RDKitMol) -> np.ndarray:
    """Compute one-hot featurization of this molecule.

    Parameters
@@ -88,9 +88,8 @@ class OneHotFeaturizer(MolecularFeaturizer):
      from rdkit import Chem
    except ModuleNotFoundError:
      raise ImportError("This class requires RDKit to be installed.")

    smiles = Chem.MolToSmiles(mol)
    return _featurize(smiles)
    return self._featurize(smiles)

  def pad_string(self, string: str) -> str:
    """Pad SMILES string to `self.pad_length`
+4 −8
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ class TestOneHotFeaturizert(unittest.TestCase):
    featurizer = OneHotFeaturizer()
    feature = featurizer(string) # Implicit call to _featurize()
    assert feature.shape == (1, 100, length)

    # untransform
    undo_string = featurizer.untransform(feature[0])
    assert string == undo_string
@@ -34,9 +33,8 @@ class TestOneHotFeaturizert(unittest.TestCase):
    smiles = 'CC(=O)Oc1ccccc1C(=O)O'
    mol = Chem.MolFromSmiles(smiles)
    featurizer = OneHotFeaturizer()
    feature = featurizer([mol]) # Implicit call to _featurizeMol()--why []?
    feature = featurizer.featurizeMol(mol) # Implicit call to _featurizeMol()--why []?
    assert feature.shape == (1, 100, length)

    # untranform
    undo_smiles = featurizer.untransform(feature[0])
    assert smiles == undo_smiles
@@ -50,12 +48,11 @@ class TestOneHotFeaturizert(unittest.TestCase):
    featurizer = OneHotFeaturizer(max_length=120)
    feature = featurizer(string)
    assert feature.shape == (1, 120, length)

    # untranform
    undo_string = featurizer.untransform(feature[0])
    assert string == undo_string

  def test_onehot_featurizer_SMIELS_with_max_length(self):
  def test_onehot_featurizer_SMILES_with_max_length(self):
    """
    Test one hot encoding with max_length.
    """
@@ -64,9 +61,8 @@ class TestOneHotFeaturizert(unittest.TestCase):
    smiles = 'CC(=O)Oc1ccccc1C(=O)O'
    mol = Chem.MolFromSmiles(smiles)
    featurizer = OneHotFeaturizer(max_length=120)
    feature = featurizer([mol])
    feature = featurizer.featurizeMol(mol)
    assert feature.shape == (1, 120, length)

    # untranform
    undo_smiles = featurizer.untransform(feature[0])
    assert smiles == undo_smiles
@@ -80,7 +76,7 @@ class TestOneHotFeaturizert(unittest.TestCase):
    smiles = 'CN=C=O'
    mol = Chem.MolFromSmiles(smiles)
    featurizer = OneHotFeaturizer(charset=charset, max_length=100)
    feature = featurizer([mol])
    feature = featurizer(mol)
    assert np.allclose(feature[0][0], np.array([1, 0, 0, 0, 0, 0, 0]))
    assert np.allclose(feature[0][1], np.array([0, 1, 0, 0, 0, 0, 0]))
    assert np.allclose(feature[0][2], np.array([0, 0, 1, 0, 0, 0, 0]))