Skip to content

Commit

Permalink
replace berny.Logger with stdlib's logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrmnn committed Feb 19, 2020
1 parent cd81995 commit 0cba143
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 56 deletions.
36 changes: 0 additions & 36 deletions berny/Logger.py

This file was deleted.

3 changes: 1 addition & 2 deletions berny/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from .berny import Berny
from .coords import angstrom
from .geomlib import Geometry
from .Logger import Logger
from .optimizers import optimize

__all__ = ['optimize', 'Berny', 'Logger', 'geomlib', 'Geometry', 'angstrom']
__all__ = ['optimize', 'Berny', 'geomlib', 'Geometry', 'angstrom']
29 changes: 16 additions & 13 deletions berny/berny.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import logging
import sys
from collections import namedtuple
from itertools import chain
Expand All @@ -11,16 +12,17 @@

from . import Math
from .coords import InternalCoords
from .Logger import Logger

if sys.version_info[:2] >= (3, 5):
from collections.abc import Generator
else:
from ._py2 import Generator # noqa

__version__ = '0.2.2'
__version__ = '0.3.0'
__all__ = ['Berny']

log = logging.getLogger(__name__)

defaults = {
'gradientmax': 0.45e-3,
'gradientrms': 0.15e-3,
Expand All @@ -47,19 +49,21 @@
"""


class BernyAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
return '{} {}'.format(self.extra['step'], msg), kwargs


class Berny(Generator):
"""
Generator that receives energy and gradients and yields the next geometry.
:param Gometry geom: geometry to start with
:param Logger log: used for logging if given
:param bool debug: if True, the generator yields debug info on receiving
the energy and gradients, otherwise it yields None
:param dict restart: start from a state saved from previous run
using ``debug=True``
:param int maxsteps: abort after maximum number of steps
:param int verbosity: if present and log is None, specifies the verbosity of
the default :py:class:`~berny.Logger`
:param params: parameters that override the :py:data:`~berny.berny.defaults`
The Berny object is to be used as follows::
Expand All @@ -78,18 +82,17 @@ class State(object):
def __init__(
self,
geom,
log=None,
debug=False,
restart=None,
maxsteps=100,
verbosity=None,
**params
):
self._log = log or Logger(verbosity=verbosity or 0)
self._debug = debug
self._maxsteps = maxsteps
self._converged = False
self._n = 0
self._log_extra = {'step': self._n}
self._log = BernyAdapter(log, self._log_extra)
s = self._state = Berny.State()
if restart:
vars(s).update(restart)
Expand All @@ -105,7 +108,7 @@ def __init__(
s.future = Berny.Point(s.coords.eval_geom(s.geom), None, None)
s.first = True
for line in str(s.coords).split('\n'):
self._log(line)
self._log.info(line)

def __next__(self):
assert self._n <= self._maxsteps
Expand All @@ -120,12 +123,12 @@ def trust(self):
return self._state.trust

def send(self, energy_gradients): # noqa: D102
log = self._log
log.n = self._n
log = self._log.info
self._log_extra['step'] = self._n
s = self._state
energy, gradients = energy_gradients
gradients = np.array(gradients)
log('Energy: {:.12}'.format(energy), level=1)
log('Energy: {:.12}'.format(energy))
B = s.coords.B_matrix(s.geom)
B_inv = B.T.dot(Math.pinv(np.dot(B, B.T), log=log))
current = Berny.Point(s.future.q, energy, dot(B_inv.T, gradients.reshape(-1)))
Expand Down Expand Up @@ -286,5 +289,5 @@ def is_converged(forces, step, on_sphere, params, log=no_log):
if not result:
all_matched = False
if all_matched:
log('* All criteria matched', level=1)
log('* All criteria matched')
return all_matched
3 changes: 0 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ This covers all supported public API.

.. autofunction:: optimize

.. autoclass:: Logger
:members:

Geometry operations
-------------------

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ skip-string-normalization = true

[tool.poetry]
name = "pyberny"
version = "0.4.3"
version = "0.5.0"
description = "Molecular/crystal structure optimizer"
readme = "README.md"
authors = ["Jan Hermann <[email protected]>"]
Expand Down Expand Up @@ -42,7 +42,7 @@ flake8-comprehensions = "^1.4"
flake8-quotes = "^1.0"
isort = "^4.3"
pydocstyle = "^3.0"
black = { version = "^19-beta.0", allows-prereleases = true, python = "^3.6" }
black = { version = "^19-beta.0", allow-prereleases = true, python = "^3.6" }

[tool.poetry.scripts]
berny = "berny.cli:main"
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import logging

logging.basicConfig(format='%(levelname)s:%(name)s: %(message)s')
logging.getLogger('berny').setLevel('INFO')

0 comments on commit 0cba143

Please sign in to comment.