Skip to content

Commit 65de22b

Browse files
tests: modernize deprecated numpy assertions in test_helix_analysis.py (ref #3743)
1 parent d25f074 commit 65de22b

2 files changed

Lines changed: 59 additions & 58 deletions

File tree

package/AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Chronological list of authors
281281
- Shubham Mittal
282282
- Charity Grey
283283
- Sai Udayagiri
284+
- Animesh Priyanshu
284285

285286
External code
286287
-------------
@@ -328,4 +329,4 @@ Logo
328329

329330
The MDAnalysis 'Atom' logo was designed by Christian Beckstein; it is
330331
Copyright (c) 2011 Christian Beckstein and made available under a
331-
Creative Commons Attribution-NoDerivs 3.0 Unported License.
332+
Creative Commons Attribution-NoDerivs 3.0 Unported License.

testsuite/MDAnalysisTests/analysis/test_helix_analysis.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import numpy as np
2626
import pytest
27-
from numpy.testing import assert_equal, assert_almost_equal, assert_allclose
27+
from numpy.testing import assert_equal, assert_allclose
2828

2929
import MDAnalysis as mda
3030
from MDAnalysis.analysis import helix_analysis as hel
@@ -167,7 +167,7 @@ def test_local_screw_angles_plane_circle():
167167
screw = hel.local_screw_angles([1, 0, 0], [0, 1, 0], xyz)
168168
correct = np.zeros_like(angdeg)
169169
correct[(angdeg > 180)] = 180
170-
assert_almost_equal(screw, correct)
170+
assert_allclose(screw, correct)
171171

172172

173173
def test_local_screw_angles_ortho_circle():
@@ -187,7 +187,7 @@ def test_local_screw_angles_ortho_circle():
187187
correct[(angdeg < 180)] = 90
188188
correct[(angdeg > 180)] = -90
189189
correct[0] = correct[15] = 0
190-
assert_almost_equal(screw, correct)
190+
assert_allclose(screw, correct)
191191

192192

193193
def test_local_screw_angles_around_circle():
@@ -203,7 +203,7 @@ def test_local_screw_angles_around_circle():
203203
screw = hel.local_screw_angles([0, 1, 0], [1, 0, 0], xyz)
204204
angdeg[-14:] = -angdeg[1:15][::-1]
205205
angdeg[-15] = 180
206-
assert_almost_equal(screw, angdeg)
206+
assert_allclose(screw, angdeg)
207207

208208

209209
def test_local_screw_angles_around_circle_rising():
@@ -221,7 +221,7 @@ def test_local_screw_angles_around_circle_rising():
221221
screw = hel.local_screw_angles([0, 1, 0], [1, 0, 0], xyz)
222222
angdeg[-14:] = -angdeg[1:15][::-1]
223223
angdeg[-15] = 180
224-
assert_almost_equal(screw, angdeg)
224+
assert_allclose(screw, angdeg)
225225

226226

227227
def test_local_screw_angles_parallel_axes():
@@ -233,7 +233,7 @@ def test_local_screw_angles_parallel_axes():
233233
"""
234234
xyz = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
235235
angles = hel.local_screw_angles([1, 0, 0], [-1, 0, 0], xyz)
236-
assert_almost_equal(angles, [0, 90, 0])
236+
assert_allclose(angles, [0, 90, 0])
237237

238238

239239
@pytest.fixture()
@@ -288,27 +288,27 @@ def zigzag():
288288
)
289289
def test_helix_analysis_zigzag(zigzag, ref_axis, screw_angles):
290290
properties = hel.helix_analysis(zigzag.atoms.positions, ref_axis=ref_axis)
291-
assert_almost_equal(properties["local_twists"], 180, decimal=4)
292-
assert_almost_equal(properties["local_nres_per_turn"], 2, decimal=4)
293-
assert_almost_equal(properties["global_axis"], [0, 0, -1], decimal=4)
291+
assert_allclose(properties["local_twists"], 180, atol=1e-4)
292+
assert_allclose(properties["local_nres_per_turn"], 2, atol=1e-4)
293+
assert_allclose(properties["global_axis"], [0, 0, -1], atol=1e-4)
294294
# all 0 vectors
295-
assert_almost_equal(properties["local_axes"], 0, decimal=4)
296-
assert_almost_equal(properties["local_bends"], 0, decimal=4)
297-
assert_almost_equal(properties["all_bends"], 0, decimal=4)
298-
assert_almost_equal(properties["local_heights"], 0, decimal=4)
299-
assert_almost_equal(
295+
assert_allclose(properties["local_axes"], 0, atol=1e-4)
296+
assert_allclose(properties["local_bends"], 0, atol=1e-4)
297+
assert_allclose(properties["all_bends"], 0, atol=1e-4)
298+
assert_allclose(properties["local_heights"], 0, atol=1e-4)
299+
assert_allclose(
300300
properties["local_helix_directions"][0::2],
301301
[[-1, 0, 0]] * 49,
302-
decimal=4,
302+
atol=1e-4,
303303
)
304-
assert_almost_equal(
305-
properties["local_helix_directions"][1::2], [[1, 0, 0]] * 49, decimal=4
304+
assert_allclose(
305+
properties["local_helix_directions"][1::2], [[1, 0, 0]] * 49, atol=1e-4
306306
)
307307
origins = zigzag.atoms.positions[1:-1].copy()
308308
origins[:, 0] = 0
309-
assert_almost_equal(properties["local_origins"], origins, decimal=4)
310-
assert_almost_equal(
311-
properties["local_screw_angles"], screw_angles * 49, decimal=4
309+
assert_allclose(properties["local_origins"], origins, atol=1e-4)
310+
assert_allclose(
311+
properties["local_screw_angles"], screw_angles * 49, atol=1e-4
312312
)
313313

314314

@@ -348,19 +348,19 @@ def test_helix_analysis_square_oct():
348348
properties = hel.helix_analysis(u.atoms.positions, ref_axis=[0, 0, 1])
349349
twist_trans = [102.76438, 32.235607]
350350
twists = ([90] * 2 + twist_trans + [45] * 6 + twist_trans[::-1]) * n_rep
351-
assert_almost_equal(
352-
properties["local_twists"], twists[: n_atoms - 3], decimal=4
351+
assert_allclose(
352+
properties["local_twists"], twists[: n_atoms - 3], atol=1e-4
353353
)
354354
res_trans = [3.503159, 11.167775]
355355
res = ([4] * 2 + res_trans + [8] * 6 + res_trans[::-1]) * n_rep
356-
assert_almost_equal(
357-
properties["local_nres_per_turn"], res[: n_atoms - 3], decimal=4
356+
assert_allclose(
357+
properties["local_nres_per_turn"], res[: n_atoms - 3], atol=1e-4
358358
)
359-
assert_almost_equal(properties["global_axis"], [-1, 0, 0], decimal=3)
360-
assert_almost_equal(properties["local_axes"] - [1, 0, 0], 0, decimal=4)
361-
assert_almost_equal(properties["local_bends"], 0, decimal=4)
362-
assert_almost_equal(properties["all_bends"], 0, decimal=4)
363-
assert_almost_equal(properties["local_heights"], 1, decimal=4)
359+
assert_allclose(properties["global_axis"], [-1, 0, 0], atol=1e-3)
360+
assert_allclose(properties["local_axes"] - [1, 0, 0], 0, atol=1e-4)
361+
assert_allclose(properties["local_bends"], 0, atol=1e-4)
362+
assert_allclose(properties["all_bends"], 0, atol=1e-4)
363+
assert_allclose(properties["local_heights"], 1, atol=1e-4)
364364

365365
loc_rot = [
366366
[0.0, 0.0, 1.0],
@@ -376,8 +376,8 @@ def test_helix_analysis_square_oct():
376376
[0.0, 0.70710677, -0.70710677],
377377
[0.0, 0.97528684, -0.2209424],
378378
] * n_rep
379-
assert_almost_equal(
380-
properties["local_helix_directions"], loc_rot[: n_atoms - 2], decimal=4
379+
assert_allclose(
380+
properties["local_helix_directions"], loc_rot[: n_atoms - 2], atol=1e-4
381381
)
382382

383383
origins = u.atoms.positions.copy()[1:-1]
@@ -398,7 +398,7 @@ def test_helix_analysis_square_oct():
398398
]
399399
* n_rep
400400
)[: len(origins)]
401-
assert_almost_equal(properties["local_origins"], origins, decimal=4)
401+
assert_allclose(properties["local_origins"], origins, atol=1e-4)
402402

403403
# calculated to the x-y plane
404404
# all input vectors (loc_rot) are in y-z plane
@@ -418,8 +418,8 @@ def test_helix_analysis_square_oct():
418418
] * n_rep
419419

420420
# not quite 0, comes out as 1.32e-06
421-
assert_almost_equal(
422-
properties["local_screw_angles"], screw[:-2], decimal=3
421+
assert_allclose(
422+
properties["local_screw_angles"], screw[:-2], atol=1e-3
423423
)
424424

425425

@@ -441,14 +441,14 @@ def helanal(self, psf_ca):
441441
def test_regression_summary(self, helanal):
442442
bends = helanal.results.summary["all_bends"]
443443
old_helanal = read_bending_matrix(HELANAL_BENDING_MATRIX_SUBSET)
444-
assert_almost_equal(
445-
np.triu(bends["mean"], 1), old_helanal["Mean"], decimal=1
444+
assert_allclose(
445+
np.triu(bends["mean"], 1), old_helanal["Mean"], atol=0.1
446446
)
447-
assert_almost_equal(
448-
np.triu(bends["sample_sd"], 1), old_helanal["SD"], decimal=1
447+
assert_allclose(
448+
np.triu(bends["sample_sd"], 1), old_helanal["SD"], atol=0.1
449449
)
450-
assert_almost_equal(
451-
np.triu(bends["abs_dev"], 1), old_helanal["ABDEV"], decimal=1
450+
assert_allclose(
451+
np.triu(bends["abs_dev"], 1), old_helanal["ABDEV"], atol=0.1
452452
)
453453

454454
def test_regression_values(self):
@@ -470,8 +470,8 @@ def test_regression_values(self):
470470
calculated = ha.results[key][0]
471471

472472
msg = "Mismatch between calculated and reference {}"
473-
assert_almost_equal(
474-
calculated, value, decimal=4, err_msg=msg.format(key)
473+
assert_allclose(
474+
calculated, value, atol=1e-4, err_msg=msg.format(key)
475475
)
476476

477477
def test_multiple_selections(self, psf_ca):
@@ -562,29 +562,29 @@ def test_helanal_zigzag(self, zigzag, ref_axis, screw_angles):
562562
ha = hel.HELANAL(
563563
zigzag, select="all", ref_axis=ref_axis, flatten_single_helix=True
564564
).run()
565-
assert_almost_equal(ha.results.local_twists, 180, decimal=4)
566-
assert_almost_equal(ha.results.local_nres_per_turn, 2, decimal=4)
567-
assert_almost_equal(ha.results.global_axis, [[0, 0, -1]], decimal=4)
565+
assert_allclose(ha.results.local_twists, 180, atol=1e-4)
566+
assert_allclose(ha.results.local_nres_per_turn, 2, atol=1e-4)
567+
assert_allclose(ha.results.global_axis, [[0, 0, -1]], atol=1e-4)
568568
# all 0 vectors
569-
assert_almost_equal(ha.results.local_axes, 0, decimal=4)
570-
assert_almost_equal(ha.results.local_bends, 0, decimal=4)
571-
assert_almost_equal(ha.results.all_bends, 0, decimal=4)
572-
assert_almost_equal(ha.results.local_heights, 0, decimal=4)
573-
assert_almost_equal(
569+
assert_allclose(ha.results.local_axes, 0, atol=1e-4)
570+
assert_allclose(ha.results.local_bends, 0, atol=1e-4)
571+
assert_allclose(ha.results.all_bends, 0, atol=1e-4)
572+
assert_allclose(ha.results.local_heights, 0, atol=1e-4)
573+
assert_allclose(
574574
ha.results.local_helix_directions[0][0::2],
575575
[[-1, 0, 0]] * 49,
576-
decimal=4,
576+
atol=1e-4,
577577
)
578-
assert_almost_equal(
578+
assert_allclose(
579579
ha.results.local_helix_directions[0][1::2],
580580
[[1, 0, 0]] * 49,
581-
decimal=4,
581+
atol=1e-4,
582582
)
583583
origins = zigzag.atoms.positions[1:-1].copy()
584584
origins[:, 0] = 0
585-
assert_almost_equal(ha.results.local_origins[0], origins, decimal=4)
586-
assert_almost_equal(
587-
ha.results.local_screw_angles[0], screw_angles * 49, decimal=4
585+
assert_allclose(ha.results.local_origins[0], origins, atol=1e-4)
586+
assert_allclose(
587+
ha.results.local_screw_angles[0], screw_angles * 49, atol=1e-4
588588
)
589589

590590

@@ -597,4 +597,4 @@ def test_vector_of_best_fit():
597597

598598
vector = hel.vector_of_best_fit(data)
599599
cos = np.dot(vector, unit)
600-
assert_almost_equal(abs(cos), 1.0, decimal=5)
600+
assert abs(cos) == pytest.approx(1.0, abs=1e-5)

0 commit comments

Comments
 (0)