From 0701138dadac76743455869d2a93f8b7d13ae3de Mon Sep 17 00:00:00 2001 From: Estefania Barreto-Ojeda Date: Sat, 2 Oct 2021 17:39:31 -0600 Subject: [PATCH] Added logger to warning messages in surface functions. (#83) * Added logger to warning messages with tests. --- membrane_curvature/surface.py | 19 +++++++- .../tests/test_membrane_curvature.py | 44 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/membrane_curvature/surface.py b/membrane_curvature/surface.py index a5afc44..c5b7c48 100644 --- a/membrane_curvature/surface.py +++ b/membrane_curvature/surface.py @@ -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): @@ -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) diff --git a/membrane_curvature/tests/test_membrane_curvature.py b/membrane_curvature/tests/test_membrane_curvature.py index 3e03b16..53fa68a 100644 --- a/membrane_curvature/tests/test_membrane_curvature.py +++ b/membrane_curvature/tests/test_membrane_curvature.py @@ -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()