Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure.meca: Fix the bug when passing a dict of scalar values to the spec parameter #2174

Merged
merged 8 commits into from
Nov 13, 2022
4 changes: 4 additions & 0 deletions pygmt/src/meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ def meca(

# convert dict to pd.DataFrame so columns can be reordered
if isinstance(spec, dict):
# convert values to ndarray so pandas doesn't complain about "all
# scalar values". See issue #2174 for details.
seisman marked this conversation as resolved.
Show resolved Hide resolved
for key, value in spec.items():
spec[key] = np.atleast_1d(value)
seisman marked this conversation as resolved.
Show resolved Hide resolved
spec = pd.DataFrame(spec)

# expected columns are:
Expand Down
19 changes: 19 additions & 0 deletions pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ def test_meca_spec_dictionary():
return fig


@pytest.mark.mpl_image_compare(filename="test_meca_spec_dictionary.png")
def test_meca_spec_dict_all_scalars():
"""
Test supplying a dict with scalar values for all focal parameters.
"""
fig = Figure()
# Right lateral strike slip focal mechanism
fig.meca(
spec=dict(
strike=0, dip=90, rake=0, magnitude=5, longitude=0, latitude=5, depth=0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested passing in the event_name name as a string too? Want to see if np.at_least1d handles it properly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, because np.atleast_1d("abc") returns array(['abc'], dtype='<U3').

I've revised the test to also pass a scalar event_name argument.

),
scale="2.5c",
region=[-1, 1, 4, 6],
projection="M14c",
frame=2,
)
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_dict_list():
"""
Expand Down