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

Improve error message for too long reactions #58

Merged
merged 1 commit into from
Sep 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rxnmapper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def convert_batch_to_attns(
return_tensors="pt",
)
parsed_input = {k: v.to(self.device) for k, v in encoded_ids.items()}

max_input_length = parsed_input["input_ids"].shape[1]
max_supported_by_model = self.model.config.max_position_embeddings
if max_input_length > max_supported_by_model:
raise ValueError(
f"Reaction SMILES has {max_input_length} tokens, should be at most {max_supported_by_model}."
)

with torch.no_grad():
output = self.model(**parsed_input)
attentions = output[2]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,15 @@ def test_reaction_with_asterisks(rxn_mapper: RXNMapper):

results = rxn_mapper.get_attention_guided_atom_maps(rxns, canonicalize_rxns=False)
assert_correct_maps(results, expected)


def test_too_long_reaction_smiles_produce_exception_with_understandable_error_message(
rxn_mapper: RXNMapper,
):
# dummy reaction with 1 + 3 + 500 * 2 + 3 + 1 = 1008 tokens
rxn = "C=C" + "[C+][C-]" * 500 + ">>CC"

with pytest.raises(ValueError) as excinfo:
_ = rxn_mapper.get_attention_guided_atom_maps([rxn], canonicalize_rxns=False)

assert "Reaction SMILES has 1008 tokens, should be" in str(excinfo.value)
Loading