Skip to content

Commit

Permalink
TST: test_streamplot_2D faster (#4221)
Browse files Browse the repository at this point in the history
* TST: test_streamplot_2D faster

* speed up `test_streamplot_2D()` because it has been
reported to take ~20 seconds on a regular basis
in CI in gh-4209

* we don't really need to plot the output, which
was taking most of the time, and instead we can
just check the data structures that MDAnalysis
returns (this may be a better test by some definitions
anyway...); I suppose we could also spot check
a few values in the arrays if we wanted as well

* locally, that single test seems to run in 0.39 s
on this branch vs. 4.7 s on `develop`

* TST: PR 4221 revisions

* use `pytest.approx` for single value comparisons

* `u1` and `v1` are now checked more thoroughly for
their actual floating point values
  • Loading branch information
tylerjereddy authored Aug 10, 2023
1 parent bd3c14c commit 1eca655
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions testsuite/MDAnalysisTests/visualization/test_streamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ def membrane_xtc(tmpdir_factory, univ):
z_delta += 0.02
return str(tmp_xtc)

def test_streamplot_2D(membrane_xtc, univ, tmpdir):
# simple roundtrip test to ensure that
# a plot is generated by the 2D streamplot
# code
def test_streamplot_2D(membrane_xtc, univ):
# regression test the data structures
# generated by the 2D streamplot code
u1, v1, avg, std = streamlines.generate_streamlines(topology_file_path=Martini_membrane_gro,
trajectory_file_path=membrane_xtc,
grid_spacing=20,
Expand All @@ -67,22 +66,18 @@ def test_streamplot_2D(membrane_xtc, univ, tmpdir):
ymax=univ.atoms.positions[...,1].max(),
maximum_delta_magnitude=2.0,
num_cores=1)
x = np.linspace(univ.atoms.positions[...,0].min(),
univ.atoms.positions[...,0].max(),
5)
y = np.linspace(univ.atoms.positions[...,1].min(),
univ.atoms.positions[...,1].max(),
5)
speed = np.sqrt(u1*u1 + v1*v1)
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.streamplot(x, y, u1, v1, density=(10,10), color=speed, linewidth=3*speed/speed.max(),
cmap=plt.cm.viridis)
plot_outpath = os.path.join(str(tmpdir), 'testing_streamline.png')
fig.savefig(plot_outpath, dpi=300)

with open(plot_outpath, 'rb'):
pass
assert_allclose(u1, np.array([[0.79999924, 0.79999924, 0.80000687, 0.79999542, 0.79998779],
[0.80000019, 0.79999542, 0.79999924, 0.79999542, 0.80001068],
[0.8000021, 0.79999924, 0.80001068, 0.80000305, 0.79999542],
[0.80000019, 0.79999542, 0.80001068, 0.80000305, 0.80000305],
[0.79999828, 0.80000305, 0.80000305, 0.80000305, 0.79999542]]))
assert_allclose(v1, np.array([[0.53999901, 0.53999996, 0.53999996, 0.53999996, 0.54000092],
[0.5399971, 0.54000092, 0.54000092, 0.54000092, 0.5399971 ],
[0.54000473, 0.54000473, 0.54000092, 0.5399971, 0.54000473],
[0.54000092, 0.53999329, 0.53999329, 0.53999329, 0.54000092],
[0.54000092, 0.53999329, 0.53999329, 0.54000092, 0.53999329]]))
assert avg == pytest.approx(0.965194167)
assert std == pytest.approx(4.444808820e-06)


def test_streamplot_2D_zero_return(membrane_xtc, univ, tmpdir):
Expand Down

0 comments on commit 1eca655

Please sign in to comment.