Skip to content

Commit

Permalink
Add patch for long inchis (#165)
Browse files Browse the repository at this point in the history
* add failing tests

* add fix
  • Loading branch information
lilyminium authored Dec 17, 2024
1 parent 92d3302 commit b4ee21b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion openff/nagl/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ def lookup(self, molecule: "Molecule") -> torch.Tensor:
If the property value cannot be found for this molecule
"""
from openff.toolkit.topology import Molecule
from openff.toolkit.utils.exceptions import EmptyInChiError

inchi_key = molecule.to_inchi(fixed_hydrogens=True)
try:
inchi_key = molecule.to_inchi(fixed_hydrogens=True)
except EmptyInChiError as e:
raise KeyError(e.msg)
try:
entry = self.properties[inchi_key]
except KeyError:
Expand Down
5 changes: 5 additions & 0 deletions openff/nagl/tests/nn/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ def test_outside_lookup_table(self, am1bcc_model):
[-0.738375, 0.246125, 0.246125, 0.246125],
atol=1e-5
)

def test_compute_long_molecule(self, am1bcc_model):
mol = Molecule.from_smiles(341 * "C")
charges = am1bcc_model.compute_property(mol, as_numpy=True)
assert charges.shape == (mol.n_atoms,)

class TestChargeGNNModelRC3(BaseTestChargeGNNModel):
model_name = "openff-gnn-am1bcc-0.1.0-rc.3"
Expand Down
5 changes: 5 additions & 0 deletions openff/nagl/tests/test_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@ def test_lookup_with_different_connectivity(self, lookup_table):
mol = Molecule.from_mapped_smiles("[H:5][C:1]([H:6])([H:7])[N+2:2](-[O-:3])[O-:4]")
properties = lookup_table.lookup(mol)
assert_allclose(properties.numpy(), np.array([-0.103, 0.234, -0.209, -0.209, 0.096, 0.096, 0.096]))

def test_lookup_long(self, lookup_table):
mol = Molecule.from_smiles(341 * "C")
with pytest.raises(KeyError, match="failed to generate an InChI"):
lookup_table.lookup(mol)

0 comments on commit b4ee21b

Please sign in to comment.