Skip to content

Commit

Permalink
Added logger to warning messages in surface functions. (#83)
Browse files Browse the repository at this point in the history
* Added logger to warning messages with tests.
  • Loading branch information
Estefania Barreto-Ojeda committed Oct 2, 2021
1 parent 5f265df commit 0701138
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
19 changes: 17 additions & 2 deletions membrane_curvature/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

import numpy as np
import warnings
import MDAnalysis
import logging

MDAnalysis.start_logging()
logger = logging.getLogger("MDAnalysis.MDAKit.membrane_curvature")


def derive_surface(atoms, n_cells_x, n_cells_y, max_width_x, max_width_y):
Expand Down Expand Up @@ -88,17 +93,27 @@ def get_z_surface(coordinates, n_x_bins=10, n_y_bins=10, x_range=(0, 100), y_ran
for l, m, z in zip(cell_x_floor, cell_y_floor, z_coords):

try:
# negative coordinates
if l < 0 or m < 0:
msg = ("Atom outside grid boundaries. Skipping atom.")
msg = ("Atom with negative coordinates falls "
"outside grid boundaries. Element "
"({},{}) in grid can't be assigned."
" Skipping atom.").format(l, m)
warnings.warn(msg)
logger.warning(msg)
continue

grid_z_coordinates[l, m] += z
grid_norm_unit[l, m] += 1

# too large positive coordinates
except IndexError:
msg = ("Atom outside grid boundaries. Skipping atom.")
msg = ("Atom coordinates exceed size of grid "
"and element ({},{}) can't be assigned. "
"Maximum (x,y) coordinates must be < ({}, {}). "
"Skipping atom.").format(l, m, x_range[1], y_range[1])
warnings.warn(msg)
logger.warning(msg)

z_surface = normalized_grid(grid_z_coordinates, grid_norm_unit)

Expand Down
44 changes: 44 additions & 0 deletions membrane_curvature/tests/test_membrane_curvature.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,47 @@ def test_test_analysis_no_wrapping(self, universe):
regex = (r"`wrap == False` may result in inaccurate calculation")
with pytest.warns(UserWarning, match=regex):
MembraneCurvature(universe, wrap=False)

@pytest.mark.parametrize('x_bin, y_bin, box_dim, dummy_array', [
# test too large x coordinates 2 bins
(2, 2, 200, np.array([[0., 0., 150.], [200., 0., 150.],
[0., 100., 150.], [100., 100., 120.]])),
# test too large y coordinates 2 bins
(2, 2, 200, np.array([[0., 0., 150.], [100., 0., 150.],
[0., 200., 150.], [100., 100., 150.]])),
# test too large y coordinates with 3 bins
(3, 3, 300, np.array([[0., 0., 150.], [100., 0., 150.], [200., 0., 150.],
[0., 300., 150.], [100., 100., 120.], [200., 100., 120.],
[0., 350., 120.], [100., 200., 120.], [200., 200., 120.]]))
])
def test_positive_coordinates_exceed_grid(self, x_bin, y_bin, box_dim, dummy_array):
u = mda.Universe(dummy_array, n_atoms=len(dummy_array))
u.dimensions = [box_dim, box_dim, 300, 90., 90., 90.]
regex = (r"Atom coordinates exceed size of grid")
with pytest.warns(UserWarning, match=regex):
MembraneCurvature(u, select='all',
n_x_bins=x_bin,
n_y_bins=y_bin,
wrap=False).run()

@pytest.mark.parametrize('x_bin, y_bin, box_dim, dummy_array', [
# test negative x coordinates 2 bins
(2, 2, 200, np.array([[-150., 0., 150.], [100., 0., 150.],
[0., 100., 150.], [100., 100., 150.]])),
# test negative y coordinates 2 bins
(2, 2, 200, np.array([[0., 0., 150.], [200., 0., 150.],
[0., -150., 150.], [100., 100., 120.]])),
# test negative x coordinates with 3 bins
(3, 3, 300, np.array([[0., 0., 150.], [-100., 0., 150.], [200., 0., 150.],
[0., 100., 150.], [100., 100., 120.], [200., 100., 120.],
[0., 200., 120.], [100., 200., 120.], [300., 200., 120.]]))
])
def test_negative_coordinates_exceed_grid(self, x_bin, y_bin, box_dim, dummy_array):
u = mda.Universe(dummy_array, n_atoms=len(dummy_array))
u.dimensions = [box_dim, box_dim, 300, 90., 90., 90.]
regex = (r"Atom with negative coordinates falls")
with pytest.warns(UserWarning, match=regex):
MembraneCurvature(u, select='all',
n_x_bins=x_bin,
n_y_bins=y_bin,
wrap=False).run()

0 comments on commit 0701138

Please sign in to comment.