From 45d9b6071f45519993c87ee7657b232db60563b6 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 24 Feb 2020 11:26:54 +0100 Subject: [PATCH 1/3] Removed redundant import --- CAT/workflows/workflow_dicts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CAT/workflows/workflow_dicts.py b/CAT/workflows/workflow_dicts.py index 8aa5828d..2d9b3bf0 100644 --- a/CAT/workflows/workflow_dicts.py +++ b/CAT/workflows/workflow_dicts.py @@ -8,7 +8,7 @@ import os from types import MappingProxyType -from typing import Mapping, MutableMapping, Tuple +from typing import Mapping, MutableMapping import yaml import numpy as np From 50821e0c19359988470db2f516f3720dad33eee7 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 24 Feb 2020 11:27:21 +0100 Subject: [PATCH 2/3] Improved warning handling --- CAT/data_handling/warn_map.py | 39 ++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/CAT/data_handling/warn_map.py b/CAT/data_handling/warn_map.py index 8a0cd632..91b782e5 100755 --- a/CAT/data_handling/warn_map.py +++ b/CAT/data_handling/warn_map.py @@ -1,13 +1,44 @@ +""" +CAT.data_handling.warn_map +========================== + +A module for managing exceptions and warnings in CAT. + +""" import warnings from types import MappingProxyType -from typing import Mapping, Callable, NoReturn, Union +from typing import Mapping, Callable, NoReturn, Union, Type + +from scm.plams import MoleculeError __all__ = ['WARN_MAP'] +class MoleculeWarning(Warning, MoleculeError): + """A :exc:`Warning` subclass for :class:`Molecule` related errors.""" # noqa + + +class ValueWarning(Warning, ValueError): + """A :exc:`Warning` subclass for :exc:`ValueError` related errors.""" # noqa + + +#: Map an :exc:`Exception` type to a :exc:`Warning` type. +CATEGORY_MAP: Mapping[Type[Exception], Type[Warning]] = MappingProxyType({ + MoleculeError: MoleculeWarning, + ValueError: ValueWarning +}) + + def _warn(exc: Exception) -> None: - """Perform a warning using **exc**.""" - warnings.warn(str(exc), category=RuntimeWarning, stacklevel=2) + """Perform a warning using **exc**. + + When possible, the warning category will be derived from the passed Exception type + (see :data:`CATEGORY_MAP`). + Will default to :exc:`RuntimeWarning` otherwise. + + """ # noqa + warnings.warn(str(exc), stacklevel=2, + category=CATEGORY_MAP.get(type(exc), RuntimeWarning)) def _raise(exc: Exception) -> NoReturn: @@ -20,6 +51,8 @@ def _ignore(exc: Exception) -> None: return None +#: Map a string to callable for either raising an :exc:`Exception`, +#: displaying a :exc:`Warning` or doing nothing. WARN_MAP: Mapping[str, Callable[[Exception], Union[NoReturn, None]]] = MappingProxyType({ 'raise': _raise, 'warn': _warn, From 37f5575964ef73f2a57dad75f53244ad11d4f759 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 24 Feb 2020 11:54:53 +0100 Subject: [PATCH 3/3] Fixed an issue where an incorrect offset was within ``_evaluate_distance()`` --- CAT/attachment/ligand_attach.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/CAT/attachment/ligand_attach.py b/CAT/attachment/ligand_attach.py index 6a3b5150..a9d562ca 100644 --- a/CAT/attachment/ligand_attach.py +++ b/CAT/attachment/ligand_attach.py @@ -243,6 +243,8 @@ def get_name() -> str: # Attach the rotated ligands to the core, returning the resulting strucutre (PLAMS Molecule). lig_array = rot_mol(ligand, vec1, vec2, atoms_other=core.properties.dummies, idx=idx) + _evaluate_distance(lig_array, len(core)) + qd = core.copy() array_to_qd(ligand, lig_array, mol_out=qd) qd.round_coords() @@ -466,12 +468,13 @@ def rotation_check_kdtree(xyz: np.ndarray, at_other: np.ndarray, k: int = 10): at_other_ = np.concatenate((at_other_, ar[idx_min])) ret[i] = ar[idx_min] - _evaluate_distance(ret, len(at_other)) return ret -def _evaluate_distance(xyz3D: np.ndarray, core_atom_count: int, - threshold: float = 1.0, action: str = 'warn') -> Union[None, NoReturn]: +def _evaluate_distance(xyz3D: np.ndarray, + offset: int = 0, + threshold: float = 1.0, + action: str = 'warn') -> Union[None, NoReturn]: """Eavluate all the distance matrix of **xyz3D** and perform **action** when distances are below **threshold**.""" # noqa try: action_func = WARN_MAP[action] @@ -492,7 +495,7 @@ def _evaluate_distance(xyz3D: np.ndarray, core_atom_count: int, bool_ar = dist < threshold if bool_ar.any(): _idx2 = np.stack([np.arange(len(idx)), idx]).T - _idx2 += 1 + core_atom_count + _idx2 += 1 + offset _idx2.sort(axis=1) idx2 = np.unique(_idx2[bool_ar], axis=0)