Skip to content

Commit

Permalink
Remove deprecated plotting options (#266)
Browse files Browse the repository at this point in the history
* Mock pyplot.show during tests

* Deprecate keyword arguments

* Test with windows-2019

* Use micromamba 0.20
  • Loading branch information
dnerini committed Feb 14, 2022
1 parent 72f3bf5 commit e734c17
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 78 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test_pysteps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- name: Install mamba and create environment
uses: mamba-org/provision-with-micromamba@main
with:
micromamba-version: 0.20.0
environment-file: ci/ci_test_env.yml
environment-name: test_environment
extra-specs: python=${{ matrix.python-version }}
Expand Down
93 changes: 62 additions & 31 deletions pysteps/tests/test_plt_animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,79 @@

import numpy as np
import pytest
from unittest.mock import patch

from pysteps.tests.helpers import get_precipitation_fields
from pysteps.visualization.animations import animate


def test_animate(tmp_path):
PRECIP, METADATA = get_precipitation_fields(
num_prev_files=2,
num_next_files=0,
return_raw=True,
metadata=True,
upscale=2000,
)

# test data
precip, metadata = get_precipitation_fields(
num_prev_files=2,
num_next_files=0,
return_raw=True,
metadata=True,
upscale=2000,
)
VALID_ARGS = (
([PRECIP], {}),
([PRECIP], {"title": "title"}),
([PRECIP], {"timestamps_obs": METADATA["timestamps"]}),
([PRECIP], {"geodata": METADATA, "map_kwargs": {"plot_map": None}}),
([PRECIP], {"motion_field": np.ones((2, *PRECIP.shape[1:]))}),
(
[PRECIP],
{"precip_kwargs": {"units": "mm/h", "colorbar": True, "colorscale": "pysteps"}},
),
([PRECIP, PRECIP], {}),
([PRECIP, PRECIP], {"title": "title"}),
([PRECIP, PRECIP], {"timestamps_obs": METADATA["timestamps"]}),
([PRECIP, PRECIP], {"timestamps_obs": METADATA["timestamps"], "timestep_min": 5}),
([PRECIP, PRECIP], {"ptype": "prob", "prob_thr": 1}),
([PRECIP, PRECIP], {"ptype": "mean"}),
([PRECIP, np.stack((PRECIP, PRECIP))], {"ptype": "ensemble"}),
)

# obs only
animate(precip)
animate(precip, title="title")
animate(precip, timestamps_obs=metadata["timestamps"])
animate(precip, geodata=metadata, map_kwargs={"plot_map": None})
with pytest.raises(ValueError):
animate(precip, timestamps_obs=metadata["timestamps"][:2])
animate(precip, motion_field=np.ones((2, *precip.shape[1:])))
with pytest.raises(ValueError):
animate(precip, motion_plot="test")

# with forecast
animate(precip, precip)
animate(precip, precip, title="title")
animate(precip, precip, timestamps_obs=metadata["timestamps"])
animate(precip, precip, timestamps_obs=metadata["timestamps"], timestep_min=5)
@pytest.mark.parametrize(["anim_args", "anim_kwargs"], VALID_ARGS)
def test_animate(anim_args, anim_kwargs):
with patch("matplotlib.pyplot.show"):
animate(*anim_args, **anim_kwargs)


VALUEERROR_ARGS = (
([PRECIP], {"timestamps_obs": METADATA["timestamps"][:2]}),
([PRECIP], {"motion_plot": "test"}),
([PRECIP, PRECIP], {"ptype": "prob"}),
)


@pytest.mark.parametrize(["anim_args", "anim_kwargs"], VALUEERROR_ARGS)
def test_animate_valueerrors(anim_args, anim_kwargs):
with pytest.raises(ValueError):
animate(precip, precip, ptype="prob")
animate(precip, precip, ptype="prob", prob_thr=1)
animate(precip, precip, ptype="mean")
animate(precip, np.stack((precip, precip)), ptype="ensemble")
animate(*anim_args, **anim_kwargs)


TYPEERROR_ARGS = (
([PRECIP], {"timestamps": METADATA["timestamps"]}),
([PRECIP], {"plotanimation": True}),
([PRECIP], {"units": "mm/h"}),
([PRECIP], {"colorbar": True}),
([PRECIP], {"colorscale": "pysteps"}),
([PRECIP, PRECIP], {"type": "ensemble"}),
)


@pytest.mark.parametrize(["anim_args", "anim_kwargs"], TYPEERROR_ARGS)
def test_animate_typeerrors(anim_args, anim_kwargs):
with pytest.raises(TypeError):
animate(*anim_args, **anim_kwargs)


# save frames
def test_animate_save(tmp_path):
animate(
precip,
np.stack((precip, precip)),
PRECIP,
np.stack((PRECIP, PRECIP)),
display_animation=False,
savefig=True,
path_outputs=tmp_path,
Expand Down
39 changes: 0 additions & 39 deletions pysteps/visualization/animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
import pysteps as st

PRECIP_VALID_TYPES = ("ensemble", "mean", "prob")
PRECIP_DEPRECATED_ARGUMENTS = (
"units",
"colorbar",
"colorscale",
) # TODO: remove in version >= 1.6
MOTION_VALID_METHODS = ("quiver", "streamplot")


Expand All @@ -47,7 +42,6 @@ def animate(
precip_kwargs=None,
motion_kwargs=None,
map_kwargs=None,
**kwargs,
):
"""
Function to animate observations and forecasts in pysteps.
Expand Down Expand Up @@ -181,39 +175,6 @@ def animate(
f"Supported: {str(PRECIP_VALID_TYPES)}"
)

# TODO: remove in version >= 1.6
if "type" in kwargs:
warnings.warn(
"The 'type' keyword will be deprecated in version 1.6. "
"Use 'ptype' instead."
)
ptype = kwargs.get("type")

# TODO: remove in version >= 1.6
if "timestamps" in kwargs:
warnings.warn(
"The 'timestamps' keyword will be deprecated in version 1.6. "
"Use 'timestamps_obs' instead."
)
timestamps_obs = kwargs.get("timestamps")

# TODO: remove in version >= 1.6
if "plotanimation" in kwargs:
warnings.warn(
"The 'plotanimation' keyword will be deprecated in version 1.6. "
"Use 'display_animation' instead."
)
display_animation = kwargs.get("timestamps")

# TODO: remove in version >= 1.6
for depr_key in PRECIP_DEPRECATED_ARGUMENTS:
if depr_key in kwargs:
warnings.warn(
f"The {depr_key} argument will be deprecated in version 1.6. "
"Add it to 'precip_kwargs' instead."
)
precip_kwargs[depr_key] = kwargs.get(depr_key)

if timestamps_obs is not None:
if len(timestamps_obs) != precip_obs.shape[0]:
raise ValueError(
Expand Down
8 changes: 0 additions & 8 deletions pysteps/visualization/precipfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def plot_precip_field(
axis="on",
cax=None,
map_kwargs=None,
**kwargs,
):
"""
Function to plot a precipitation intensity or probability field with a
Expand Down Expand Up @@ -131,13 +130,6 @@ def plot_precip_field(
if map_kwargs is None:
map_kwargs = {}

if "type" in kwargs:
warnings.warn(
"The 'type' keyword use to indicate the type of plot will be "
"deprecated in version 1.6. Use 'ptype' instead."
)
ptype = kwargs.get("type")

if ptype not in PRECIP_VALID_TYPES:
raise ValueError(
f"Invalid precipitation type '{ptype}'."
Expand Down

0 comments on commit e734c17

Please sign in to comment.