From 31bb38ecce5580c9253476a2e91b51ae6b55acf0 Mon Sep 17 00:00:00 2001 From: SimonBoothroyd Date: Wed, 22 Dec 2021 11:07:19 +0000 Subject: [PATCH] Handle uncaught exceptions when filtering (#32) --- .lgtm.yml | 2 ++ nagl/cli/prepare/filter.py | 67 +++++++++++++++++++++++--------------- nagl/labelling.py | 2 +- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/.lgtm.yml b/.lgtm.yml index 0574bb7..87b1364 100644 --- a/.lgtm.yml +++ b/.lgtm.yml @@ -10,3 +10,5 @@ path_classifiers: - devtools/* generated: - nagl/_version.py +queries: + - exclude: py/catch-base-exception diff --git a/nagl/cli/prepare/filter.py b/nagl/cli/prepare/filter.py index 6bfb01a..50521c5 100644 --- a/nagl/cli/prepare/filter.py +++ b/nagl/cli/prepare/filter.py @@ -1,4 +1,5 @@ import functools +import logging from multiprocessing import Pool from typing import TYPE_CHECKING, Tuple @@ -16,38 +17,50 @@ if TYPE_CHECKING: from openff.toolkit.topology import Molecule +_logger = logging.getLogger(__name__) + def apply_filter(molecule: "Molecule", retain_largest: bool) -> Tuple["Molecule", bool]: with capture_toolkit_warnings(): - from openff.toolkit.topology import Molecule - from simtk import unit as simtk_unit - - split_smiles = molecule.to_smiles().split(".") - n_sub_molecules = len(split_smiles) - - if retain_largest and n_sub_molecules > 1: - - largest_smiles = max(split_smiles, key=len) - molecule = Molecule.from_smiles(largest_smiles, allow_undefined_stereo=True) - - # Retain H, C, N, O, F, P, S, Cl, Br, I - allowed_elements = [1, 6, 7, 8, 9, 15, 16, 17, 35, 53] - - mass = sum( - atom.mass.value_in_unit(simtk_unit.gram / simtk_unit.mole) - for atom in molecule.atoms - ) - - return ( - molecule, - ( - all(atom.atomic_number in allowed_elements for atom in molecule.atoms) - and (250.0 < mass < 350.0) - and (len(molecule.find_rotatable_bonds()) <= 7) - ), - ) + try: + from openff.toolkit.topology import Molecule + from simtk import unit as simtk_unit + + split_smiles = molecule.to_smiles().split(".") + n_sub_molecules = len(split_smiles) + + if retain_largest and n_sub_molecules > 1: + + largest_smiles = max(split_smiles, key=len) + molecule = Molecule.from_smiles( + largest_smiles, allow_undefined_stereo=True + ) + + # Retain H, C, N, O, F, P, S, Cl, Br, I + allowed_elements = [1, 6, 7, 8, 9, 15, 16, 17, 35, 53] + + mass = sum( + atom.mass.value_in_unit(simtk_unit.gram / simtk_unit.mole) + for atom in molecule.atoms + ) + + return ( + molecule, + ( + all( + atom.atomic_number in allowed_elements + for atom in molecule.atoms + ) + and (250.0 < mass < 350.0) + and (len(molecule.find_rotatable_bonds()) <= 7) + ), + ) + + except BaseException: + _logger.exception("failed to apply filter") + return molecule, False @click.command( diff --git a/nagl/labelling.py b/nagl/labelling.py index 22de36e..9dceca0 100644 --- a/nagl/labelling.py +++ b/nagl/labelling.py @@ -183,7 +183,7 @@ def label_molecules( n_conformers, rms_cutoff, ) - except (BaseException, Exception) as e: # lgtm [py/catch-base-exception] + except (BaseException, Exception) as e: formatted_traceback = traceback.format_exception( etype=type(e), value=e, tb=e.__traceback__