diff --git a/AUTHORS b/AUTHORS index b0fdac0..c511583 100644 --- a/AUTHORS +++ b/AUTHORS @@ -19,3 +19,4 @@ Contributors: * Giacomo Fiorin * Eloy Félix * René Hafner (Hamburger) +* Anthony Cruz-Balberdy diff --git a/CHANGELOG b/CHANGELOG index 42eb9b4..64a24cd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,13 +13,15 @@ The rules for this file: * accompany each entry with github issue/PR number (Issue #xyz) ------------------------------------------------------------------------------ -??/??/2019 eloyfelix, renehamburger1993 +??/??/2020 eloyfelix, renehamburger1993, acruzpr * 0.6.0 Enhancements * Allow parsing/writing gzipped DX files + * Allow the generation of coordinates for the cell centers using the origin as defined by APBS DX file format + or as defined by the Original DX file format (#78) Fixes diff --git a/gridData/core.py b/gridData/core.py index bbc9a99..b6b07c3 100644 --- a/gridData/core.py +++ b/gridData/core.py @@ -112,7 +112,7 @@ def __init__(self, grid=None, edges=None, origin=None, delta=None, .. versionchanged:: 0.5.0 - New *file_format* keyword argument. + New *file_format* keyword argument """ # file formats are guess from extension == lower case key @@ -573,11 +573,25 @@ def save(self, filename): """ self.export(filename, file_format="pickle") - def centers(self): + def centers(self, origin_centered=False): """Returns the coordinates of the centers of all grid cells as an - iterator.""" + iterator. + + Parameters + ---------- + origin_centered : bool + When is set to True the origin will be set to the lower-left corner of the grid following + the APBS DX format specifications. + + When is set to False the origin will be set to the center of the first grid cell following + the official DX format specifications. + + .. versionchanged:: 0.6.0 + New *origin_centered* keyword argument + """ for idx in numpy.ndindex(self.grid.shape): - yield self.delta * numpy.array(idx) + self.origin + offset = 0.5 * self.delta if not origin_centered else 0 + yield self.delta * numpy.array(idx) + self.origin + offset def check_compatible(self, other): """Check if *other* can be used in an arithmetic operation. diff --git a/gridData/tests/test_grid.py b/gridData/tests/test_grid.py index 667d0ae..99ec161 100644 --- a/gridData/tests/test_grid.py +++ b/gridData/tests/test_grid.py @@ -151,12 +151,23 @@ def test_non_orthonormal_boxes(self, data): with pytest.raises(NotImplementedError): Grid(data['griddata'], origin=data['origin'], delta=delta) - def test_centers(self, data): + def test_centers_false(self, data): # this only checks the edges. If you know an alternative # algorithm that isn't an exact duplicate of the one in # g.centers to test this please implement it. g = Grid(data['griddata'], origin=np.ones(3), delta=data['delta']) - centers = np.array(list(g.centers())) + centers = np.array(list(g.centers(False))) + offset = g.delta * 0.5 + assert_array_equal(centers[0], g.origin + offset) + assert_array_equal(centers[-1] - g.origin - offset, + (np.array(g.grid.shape) - 1) * data['delta']) + + def test_centers_true(self, data): + # this only checks the edges. If you know an alternative + # algorithm that isn't an exact duplicate of the one in + # g.centers to test this please implement it. + g = Grid(data['griddata'], origin=np.ones(3), delta=data['delta']) + centers = np.array(list(g.centers(True))) assert_array_equal(centers[0], g.origin) assert_array_equal(centers[-1] - g.origin, (np.array(g.grid.shape) - 1) * data['delta'])