Skip to content

Commit

Permalink
Merge pull request #86 from nlesc-nano/devel
Browse files Browse the repository at this point in the history
 Fixed an issue where an incorrect offset was within _evaluate_distance()
  • Loading branch information
BvB93 authored Feb 24, 2020
2 parents e4377bd + 37f5575 commit 3b298dd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
11 changes: 7 additions & 4 deletions CAT/attachment/ligand_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down
39 changes: 36 additions & 3 deletions CAT/data_handling/warn_map.py
Original file line number Diff line number Diff line change
@@ -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<scm.plams.mol.molecule.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:
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion CAT/workflows/workflow_dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b298dd

Please sign in to comment.