Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes test on ci #3654

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 26 additions & 8 deletions deepchem/feat/reaction_featurizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from deepchem.feat import Featurizer
from typing import List
import numpy as np
from numpy.typing import ArrayLike

try:
from transformers import RobertaTokenizerFast
Expand Down Expand Up @@ -35,7 +36,10 @@ class RxnFeaturizer(Featurizer):
- False - Mix the reactants and reagents
"""

def __init__(self, tokenizer: RobertaTokenizerFast, sep_reagent: bool):
def __init__(self,
tokenizer: RobertaTokenizerFast,
sep_reagent: bool,
max_length: int = 100):
"""Initialize a ReactionFeaturizer object.

Parameters
Expand All @@ -44,6 +48,8 @@ def __init__(self, tokenizer: RobertaTokenizerFast, sep_reagent: bool):
HuggingFace Tokenizer to be used for featurization.
sep_reagent: bool
Toggle to separate or mix the reactants and reagents.
max_length: int, default 100
Maximum length of padding
"""
if not isinstance(tokenizer, RobertaTokenizerFast):
raise TypeError(
Expand All @@ -52,8 +58,9 @@ def __init__(self, tokenizer: RobertaTokenizerFast, sep_reagent: bool):
else:
self.tokenizer = tokenizer
self.sep_reagent = sep_reagent
self.max_length = max_length

def _featurize(self, datapoint: str, **kwargs) -> List[List[List[int]]]:
def _featurize(self, datapoint: str, **kwargs) -> List[ArrayLike]:
"""Featurizes a datapoint.

Processes each entry in the dataset by first applying the reactant-reagent
Expand Down Expand Up @@ -87,15 +94,26 @@ def _featurize(self, datapoint: str, **kwargs) -> List[List[List[int]]]:
]
target = product

source_encoding = list(
self.tokenizer(source, padding=True, **kwargs).values())
target_encoding = list(
self.tokenizer(target, padding=True, **kwargs).values())

source_encoding = np.asarray(
list(
self.tokenizer(source,
padding='max_length',
truncation=True,
max_length=self.max_length,
**kwargs).values()))
target_encoding = np.asarray(
list(
self.tokenizer(target,
padding='max_length',
truncation=True,
max_length=self.max_length,
**kwargs).values()))
return [source_encoding, target_encoding]

def __call__(self, *args, **kwargs) -> np.ndarray:
return self.featurize(*args, **kwargs)
features = self.featurize(*args, **kwargs)
print(type(features), len(features))
return features

def __str__(self) -> str:
"""Handles file name error.
Expand Down
11 changes: 7 additions & 4 deletions deepchem/feat/tests/test_reaction_featurizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ def test_featurize():
from deepchem.feat.reaction_featurizer import RxnFeaturizer
tokenizer = RobertaTokenizerFast.from_pretrained(
"seyonec/PubChem10M_SMILES_BPE_450k")
featurizer = RxnFeaturizer(tokenizer, sep_reagent=True)
max_length = 20
featurizer = RxnFeaturizer(tokenizer,
sep_reagent=True,
max_length=max_length)
reaction = ['CCS(=O)(=O)Cl.OCCBr>CCN(CC)CC.CCOCC>CCS(=O)(=O)OCCBr']
feats = featurizer.featurize(reaction)
assert (feats.shape == (1, 2, 2, 1))
assert (feats.shape == (1, 2, 2, 1, max_length))


@pytest.mark.torch
Expand All @@ -33,7 +36,7 @@ def test_separation():
feats_sep = featurizer_sep.featurize(reaction)

# decode the source in the mixed and separated cases
mix_decoded = tokenizer.decode(feats_mix[0][0][0][0])
sep_decoded = tokenizer.decode(feats_sep[0][0][0][0])
mix_decoded = tokenizer.decode(feats_mix[0][0][0][0]).replace('<pad>', '')
sep_decoded = tokenizer.decode(feats_sep[0][0][0][0]).replace('<pad>', '')
assert mix_decoded == '<s>CCS(=O)(=O)Cl.OCCBr.CCN(CC)CC.CCOCC></s>'
assert sep_decoded == '<s>CCS(=O)(=O)Cl.OCCBr>CCN(CC)CC.CCOCC</s>'
27 changes: 17 additions & 10 deletions deepchem/feat/tests/test_roberta_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ def test_smiles_featurize():
]
featurizer = RobertaFeaturizer.from_pretrained(
"seyonec/SMILES_tokenized_PubChem_shard00_160k")
feats = featurizer.featurize(smiles,
add_special_tokens=True,
truncation=True)
assert (len(feats) == 2)
assert (all([len(f) == 2 for f in feats]))
long_feat = featurizer.featurize(long_molecule_smiles,
add_special_tokens=True,
truncation=True)
assert (len(long_feat) == 1)
assert (len(long_feat[0] == 2))
max_length = 100
feat_kwargs = {
'add_special_tokens': True,
'truncation': True,
'padding': 'max_length',
'max_length': max_length
}
feats = featurizer.featurize(smiles, **feat_kwargs)
assert len(feats) == 2
assert all([len(f) == 2 for f in feats])
assert all([len(f[0]) == max_length for f in feats])

long_feat = featurizer.featurize(long_molecule_smiles, **feat_kwargs)
assert len(long_feat) == 1
assert len(long_feat[0]) == 2 # the tokens and attention mask
assert len(
long_feat[0][0]) == 100 # number of tokens for each smiles string
2 changes: 1 addition & 1 deletion deepchem/models/gbdt_models/gbdt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def fit(self, dataset: Dataset):

# retrain model to whole data using best n_estimators * 1.25
if self.model.__class__.__name__.startswith('XGB'):
estimated_best_round = np.round(self.model.best_ntree_limit * 1.25)
estimated_best_round = np.round(self.model.best_iteration * 1.25)
else:
estimated_best_round = np.round(self.model.best_iteration_ * 1.25)
self.model.n_estimators = np.int64(estimated_best_round)
Expand Down