|
4 | 4 |
|
5 | 5 | import numpy as np
|
6 | 6 | import pytest
|
| 7 | +from unittest.mock import patch |
7 | 8 |
|
8 | 9 | from pysteps.tests.helpers import get_precipitation_fields
|
9 | 10 | from pysteps.visualization.animations import animate
|
10 | 11 |
|
11 | 12 |
|
12 |
| -def test_animate(tmp_path): |
| 13 | +PRECIP, METADATA = get_precipitation_fields( |
| 14 | + num_prev_files=2, |
| 15 | + num_next_files=0, |
| 16 | + return_raw=True, |
| 17 | + metadata=True, |
| 18 | + upscale=2000, |
| 19 | +) |
13 | 20 |
|
14 |
| - # test data |
15 |
| - precip, metadata = get_precipitation_fields( |
16 |
| - num_prev_files=2, |
17 |
| - num_next_files=0, |
18 |
| - return_raw=True, |
19 |
| - metadata=True, |
20 |
| - upscale=2000, |
21 |
| - ) |
| 21 | +VALID_ARGS = ( |
| 22 | + ([PRECIP], {}), |
| 23 | + ([PRECIP], {"title": "title"}), |
| 24 | + ([PRECIP], {"timestamps_obs": METADATA["timestamps"]}), |
| 25 | + ([PRECIP], {"geodata": METADATA, "map_kwargs": {"plot_map": None}}), |
| 26 | + ([PRECIP], {"motion_field": np.ones((2, *PRECIP.shape[1:]))}), |
| 27 | + ( |
| 28 | + [PRECIP], |
| 29 | + {"precip_kwargs": {"units": "mm/h", "colorbar": True, "colorscale": "pysteps"}}, |
| 30 | + ), |
| 31 | + ([PRECIP, PRECIP], {}), |
| 32 | + ([PRECIP, PRECIP], {"title": "title"}), |
| 33 | + ([PRECIP, PRECIP], {"timestamps_obs": METADATA["timestamps"]}), |
| 34 | + ([PRECIP, PRECIP], {"timestamps_obs": METADATA["timestamps"], "timestep_min": 5}), |
| 35 | + ([PRECIP, PRECIP], {"ptype": "prob", "prob_thr": 1}), |
| 36 | + ([PRECIP, PRECIP], {"ptype": "mean"}), |
| 37 | + ([PRECIP, np.stack((PRECIP, PRECIP))], {"ptype": "ensemble"}), |
| 38 | +) |
22 | 39 |
|
23 |
| - # obs only |
24 |
| - animate(precip) |
25 |
| - animate(precip, title="title") |
26 |
| - animate(precip, timestamps_obs=metadata["timestamps"]) |
27 |
| - animate(precip, geodata=metadata, map_kwargs={"plot_map": None}) |
28 |
| - with pytest.raises(ValueError): |
29 |
| - animate(precip, timestamps_obs=metadata["timestamps"][:2]) |
30 |
| - animate(precip, motion_field=np.ones((2, *precip.shape[1:]))) |
31 |
| - with pytest.raises(ValueError): |
32 |
| - animate(precip, motion_plot="test") |
33 | 40 |
|
34 |
| - # with forecast |
35 |
| - animate(precip, precip) |
36 |
| - animate(precip, precip, title="title") |
37 |
| - animate(precip, precip, timestamps_obs=metadata["timestamps"]) |
38 |
| - animate(precip, precip, timestamps_obs=metadata["timestamps"], timestep_min=5) |
| 41 | +@pytest.mark.parametrize(["anim_args", "anim_kwargs"], VALID_ARGS) |
| 42 | +def test_animate(anim_args, anim_kwargs): |
| 43 | + with patch("matplotlib.pyplot.show"): |
| 44 | + animate(*anim_args, **anim_kwargs) |
| 45 | + |
| 46 | + |
| 47 | +VALUEERROR_ARGS = ( |
| 48 | + ([PRECIP], {"timestamps_obs": METADATA["timestamps"][:2]}), |
| 49 | + ([PRECIP], {"motion_plot": "test"}), |
| 50 | + ([PRECIP, PRECIP], {"ptype": "prob"}), |
| 51 | +) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize(["anim_args", "anim_kwargs"], VALUEERROR_ARGS) |
| 55 | +def test_animate_valueerrors(anim_args, anim_kwargs): |
39 | 56 | with pytest.raises(ValueError):
|
40 |
| - animate(precip, precip, ptype="prob") |
41 |
| - animate(precip, precip, ptype="prob", prob_thr=1) |
42 |
| - animate(precip, precip, ptype="mean") |
43 |
| - animate(precip, np.stack((precip, precip)), ptype="ensemble") |
| 57 | + animate(*anim_args, **anim_kwargs) |
| 58 | + |
| 59 | + |
| 60 | +TYPEERROR_ARGS = ( |
| 61 | + ([PRECIP], {"timestamps": METADATA["timestamps"]}), |
| 62 | + ([PRECIP], {"plotanimation": True}), |
| 63 | + ([PRECIP], {"units": "mm/h"}), |
| 64 | + ([PRECIP], {"colorbar": True}), |
| 65 | + ([PRECIP], {"colorscale": "pysteps"}), |
| 66 | + ([PRECIP, PRECIP], {"type": "ensemble"}), |
| 67 | +) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize(["anim_args", "anim_kwargs"], TYPEERROR_ARGS) |
| 71 | +def test_animate_typeerrors(anim_args, anim_kwargs): |
| 72 | + with pytest.raises(TypeError): |
| 73 | + animate(*anim_args, **anim_kwargs) |
| 74 | + |
44 | 75 |
|
45 |
| - # save frames |
| 76 | +def test_animate_save(tmp_path): |
46 | 77 | animate(
|
47 |
| - precip, |
48 |
| - np.stack((precip, precip)), |
| 78 | + PRECIP, |
| 79 | + np.stack((PRECIP, PRECIP)), |
49 | 80 | display_animation=False,
|
50 | 81 | savefig=True,
|
51 | 82 | path_outputs=tmp_path,
|
|
0 commit comments