diff --git a/openff/nagl/lookups.py b/openff/nagl/lookups.py index 229a35e..9c7412b 100644 --- a/openff/nagl/lookups.py +++ b/openff/nagl/lookups.py @@ -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: diff --git a/openff/nagl/tests/nn/test_model.py b/openff/nagl/tests/nn/test_model.py index 5486b27..f6f4b83 100644 --- a/openff/nagl/tests/nn/test_model.py +++ b/openff/nagl/tests/nn/test_model.py @@ -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" diff --git a/openff/nagl/tests/test_lookups.py b/openff/nagl/tests/test_lookups.py index 676699f..35fe030 100644 --- a/openff/nagl/tests/test_lookups.py +++ b/openff/nagl/tests/test_lookups.py @@ -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)