From 3c9fe8e5cfed63ffc30bb32e98cb394aa245b16d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 22 Jun 2023 21:25:32 +0800 Subject: [PATCH 1/4] Figure.meca: Refactor test_meca_eventname to accepct different input types --- ...me.png.dvc => test_meca_eventname.png.dvc} | 2 +- pygmt/tests/test_meca.py | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) rename pygmt/tests/baseline/{test_meca_dict_eventname.png.dvc => test_meca_eventname.png.dvc} (61%) diff --git a/pygmt/tests/baseline/test_meca_dict_eventname.png.dvc b/pygmt/tests/baseline/test_meca_eventname.png.dvc similarity index 61% rename from pygmt/tests/baseline/test_meca_dict_eventname.png.dvc rename to pygmt/tests/baseline/test_meca_eventname.png.dvc index 61e47357f70..a6dedc1effc 100644 --- a/pygmt/tests/baseline/test_meca_dict_eventname.png.dvc +++ b/pygmt/tests/baseline/test_meca_eventname.png.dvc @@ -1,4 +1,4 @@ outs: - md5: 81203f9e3a43ec235cf4b5068f928b56 size: 10689 - path: test_meca_dict_eventname.png + path: test_meca_eventname.png diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index cbff884ea75..ab0f129892f 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -179,22 +179,23 @@ def test_meca_offset(inputtype): return fig -@pytest.mark.mpl_image_compare -def test_meca_dict_eventname(): +@pytest.mark.mpl_image_compare(filename="test_meca_eventname.png") +@pytest.mark.parametrize("inputtype", ["eventname_args"]) +def test_meca_eventname(inputtype): """ - Test offsetting beachballs for a dict input. + Test passing event names. """ + if inputtype == "eventname_args": + args = { + "spec": {"strike": 330, "dip": 30, "rake": 90, "magnitude": 3}, + "longitude": -124, + "latitude": 48, + "depth": 12.0, + "event_name": "Event20220311", + } fig = Figure() - focal_mechanism = {"strike": 330, "dip": 30, "rake": 90, "magnitude": 3} fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True) - fig.meca( - spec=focal_mechanism, - scale="1c", - longitude=-124, - latitude=48, - depth=12.0, - event_name="Event20220311", - ) + fig.meca(scale="1c", **args) return fig @@ -219,7 +220,7 @@ def test_meca_dict_offset_eventname(): return fig -@pytest.mark.mpl_image_compare(filename="test_meca_dict_eventname.png") +@pytest.mark.mpl_image_compare(filename="test_meca_eventname.png") def test_meca_spec_dict_all_scalars(): """ Test supplying a dict with scalar values for all focal parameters. From c1228c0dd58e4bb21778f42727fe07b4ce726019 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 22 Jun 2023 21:43:49 +0800 Subject: [PATCH 2/4] Test passing event names via pandas.DataFrame --- pygmt/tests/test_meca.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index ab0f129892f..abdd3eac008 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -4,7 +4,8 @@ import numpy as np import pandas as pd import pytest -from pygmt import Figure +from packaging.version import Version +from pygmt import Figure, __gmt_version__ from pygmt.helpers import GMTTempFile @@ -179,13 +180,27 @@ def test_meca_offset(inputtype): return fig +# Passing event names via pandas doesn't work for GMT<=6.4, thus marked as +# xfail. See https://github.com/GenericMappingTools/pygmt/issues/2524. @pytest.mark.mpl_image_compare(filename="test_meca_eventname.png") -@pytest.mark.parametrize("inputtype", ["eventname_args"]) +@pytest.mark.parametrize( + "inputtype", + [ + "args", + pytest.param( + "pandas", + marks=pytest.mark.xfail( + condition=Version(__gmt_version__) < Version("6.5.0"), + reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/7557", + ), + ), + ], +) def test_meca_eventname(inputtype): """ Test passing event names. """ - if inputtype == "eventname_args": + if inputtype == "args": args = { "spec": {"strike": 330, "dip": 30, "rake": 90, "magnitude": 3}, "longitude": -124, @@ -193,6 +208,24 @@ def test_meca_eventname(inputtype): "depth": 12.0, "event_name": "Event20220311", } + elif inputtype == "pandas": + # Test pandas input. Requires GMT>=6.5. + # See https://github.com/GenericMappingTools/pygmt/issues/2524. + # The numeric columns must be in float type to trigger the bug. + args = { + "spec": pd.DataFrame( + { + "longitude": [-124.0], + "latitude": [48.0], + "depth": [12.0], + "strike": [330.0], + "dip": [30.0], + "rake": [90.0], + "magnitude": [3.0], + "event_name": ["Event20220311"], + } + ) + } fig = Figure() fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True) fig.meca(scale="1c", **args) From 729b17e412d259ac9a50e5bc63860e2413f68f77 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 22 Jun 2023 22:27:21 +0800 Subject: [PATCH 3/4] Use pytest.mark.skipif, not pytest.mark.xfail because it crashes with GMT 6.4 --- pygmt/tests/test_meca.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index abdd3eac008..66e60349622 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -189,7 +189,7 @@ def test_meca_offset(inputtype): "args", pytest.param( "pandas", - marks=pytest.mark.xfail( + marks=pytest.mark.skipif( condition=Version(__gmt_version__) < Version("6.5.0"), reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/7557", ), From b10f32c884dec1537f0de00b7be8503ea30d89f2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 22 Jun 2023 22:28:03 +0800 Subject: [PATCH 4/4] Rename pandas to dataframe --- pygmt/tests/test_meca.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index 66e60349622..f54f6b7d16a 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -188,7 +188,7 @@ def test_meca_offset(inputtype): [ "args", pytest.param( - "pandas", + "dataframe", marks=pytest.mark.skipif( condition=Version(__gmt_version__) < Version("6.5.0"), reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/7557", @@ -208,8 +208,8 @@ def test_meca_eventname(inputtype): "depth": 12.0, "event_name": "Event20220311", } - elif inputtype == "pandas": - # Test pandas input. Requires GMT>=6.5. + elif inputtype == "dataframe": + # Test pandas.DataFrame input. Requires GMT>=6.5. # See https://github.com/GenericMappingTools/pygmt/issues/2524. # The numeric columns must be in float type to trigger the bug. args = {