Skip to content

Commit bbcef1b

Browse files
authored
Modernize assertions in test_density.py (Contributes to #3743) (#5155)
* modernize numpy assertions in `analysis/test_density.py` (Contributes to #3743)
1 parent 70aae14 commit bbcef1b

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

package/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ Chronological list of authors
263263
- Amruthesh Thirumalaiswamy
264264
- Ch Zhang
265265
- Raúl Lois-Cuns
266+
- Pranay Pelapkar
266267
- Shreejan Dolai
267268

268269
External code

testsuite/MDAnalysisTests/analysis/test_density.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import warnings
2828
from unittest.mock import Mock, patch
2929

30-
from numpy.testing import assert_equal, assert_almost_equal
30+
from numpy.testing import assert_equal, assert_allclose
3131

3232
import gridData.OpenDX
3333

@@ -77,30 +77,26 @@ def test_shape(self, D):
7777

7878
def test_edges(self, bins, D):
7979
for dim, (edges, fixture) in enumerate(zip(D.edges, bins)):
80-
assert_almost_equal(
81-
edges, fixture, err_msg="edges[{0}] mismatch".format(dim)
82-
)
80+
assert_allclose(edges, fixture, err_msg=f"edges[{dim}] mismatch")
8381

8482
def test_midpoints(self, bins, D):
8583
midpoints = [0.5 * (b[:-1] + b[1:]) for b in bins]
8684
for dim, (mp, fixture) in enumerate(zip(D.midpoints, midpoints)):
87-
assert_almost_equal(
88-
mp, fixture, err_msg="midpoints[{0}] mismatch".format(dim)
89-
)
85+
assert_allclose(mp, fixture, err_msg=f"midpoints[{dim}] mismatch")
9086

9187
def test_delta(self, D):
9288
deltas = np.array([self.Lmax]) / np.array(self.nbins)
93-
assert_almost_equal(D.delta, deltas)
89+
assert_allclose(D.delta, deltas)
9490

9591
def test_grid(self, D):
9692
dV = D.delta.prod() # orthorhombic grids only!
9793
# counts = (rho[0] * dV[0] + rho[1] * dV[1] ...) = sum_i rho[i] * dV
98-
assert_almost_equal(D.grid.sum() * dV, self.counts)
94+
assert D.grid.sum() * dV == pytest.approx(self.counts)
9995

10096
def test_origin(self, bins, D):
10197
midpoints = [0.5 * (b[:-1] + b[1:]) for b in bins]
10298
origin = [m[0] for m in midpoints]
103-
assert_almost_equal(D.origin, origin)
99+
assert_allclose(D.origin, origin)
104100

105101
def test_check_set_unit_keyerror(self, D):
106102
units = {"weight": "A"}
@@ -146,28 +142,28 @@ def test_check_convert_density_units_same_density_units(self, D):
146142
D_orig = copy.deepcopy(D)
147143
D.convert_density(unit)
148144
assert D.units["density"] == D_orig.units["density"] == unit
149-
assert_almost_equal(D.grid, D_orig.grid)
145+
assert_allclose(D.grid, D_orig.grid)
150146

151147
def test_check_convert_density_units_density(self, D):
152148
unit = "nm^{-3}"
153149
D_orig = copy.deepcopy(D)
154150
D.convert_density(unit)
155151
assert D.units["density"] == "nm^{-3}"
156-
assert_almost_equal(D.grid, 10**3 * D_orig.grid)
152+
assert_allclose(D.grid, 10**3 * D_orig.grid)
157153

158154
def test_convert_length_same_length_units(self, D):
159155
unit = "A"
160156
D_orig = copy.deepcopy(D)
161157
D.convert_length(unit)
162158
assert D.units["length"] == D_orig.units["length"] == unit
163-
assert_almost_equal(D.grid, D_orig.grid)
159+
assert_allclose(D.grid, D_orig.grid)
164160

165161
def test_convert_length_other_length_units(self, D):
166162
unit = "nm"
167163
D_orig = copy.deepcopy(D)
168164
D.convert_length(unit)
169165
assert D.units["length"] == unit
170-
assert_almost_equal(D.grid, D_orig.grid)
166+
assert_allclose(D.grid, D_orig.grid)
171167

172168
def test_repr(self, D, D1):
173169
assert str(D) == "<Density density with (3, 4, 5) bins>"
@@ -178,14 +174,14 @@ def test_check_convert_length_edges(self, D):
178174
unit = "nm"
179175
D.convert_length(unit)
180176
for prev_edge, conv_edge in zip(D1.edges, D.edges):
181-
assert_almost_equal(prev_edge, 10 * conv_edge)
177+
assert_allclose(prev_edge, 10 * conv_edge)
182178

183179
def test_check_convert_density_edges(self, D):
184180
unit = "nm^{-3}"
185181
D_orig = copy.deepcopy(D)
186182
D.convert_density(unit)
187183
for new_den, orig_den in zip(D.edges, D_orig.edges):
188-
assert_almost_equal(new_den, orig_den)
184+
assert_allclose(new_den, orig_den)
189185

190186
@pytest.mark.parametrize("dxtype", ("float", "double", "int", "byte"))
191187
def test_export_types(self, D, dxtype, tmpdir, outfile="density.dx"):
@@ -261,18 +257,17 @@ def check_DensityAnalysis(
261257
D = density.DensityAnalysis(ag, delta=self.delta, **kwargs).run(
262258
**runargs, **client_DensityAnalysis
263259
)
264-
assert_almost_equal(
265-
D.results.density.grid.mean(),
266-
ref_meandensity,
267-
err_msg="mean density does not match",
268-
)
260+
assert D.results.density.grid.mean() == pytest.approx(
261+
ref_meandensity
262+
), "mean density does not match"
269263
D.results.density.export(self.outfile)
270264

271265
D2 = density.Density(self.outfile)
272-
assert_almost_equal(
266+
assert_allclose(
273267
D.results.density.grid,
274268
D2.grid,
275-
decimal=self.precision,
269+
rtol=0,
270+
atol=10 ** (-self.precision),
276271
err_msg="DX export failed: different grid sizes",
277272
)
278273

0 commit comments

Comments
 (0)