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: Refactor the tests for pass a single focal mechanism to spec #2219

Closed
wants to merge 7 commits into from
4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_spec_1d_array.png.dvc

This file was deleted.

4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_meca_spec_dict.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: a5abdacee354bac190b3b07777603478
size: 23952
path: test_meca_spec_dict.png
4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_spec_dictionary.png.dvc

This file was deleted.

4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_spec_file.png.dvc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
outs:
- md5: b40481dd506db95c617978fe41ca9f9c
size: 4583
path: test_meca_spec_dataframe.png
path: test_meca_spec_two_dataframe.png
132 changes: 67 additions & 65 deletions pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@pytest.mark.mpl_image_compare
def test_meca_spec_dictionary():
def test_meca_spec_dict():
"""
Test supplying a dictionary containing a single focal mechanism to the spec
parameter.
Expand All @@ -24,7 +24,71 @@ def test_meca_spec_dictionary():
scale="2.5c",
region=[-1, 1, 4, 6],
projection="M14c",
frame=2,
frame=True,
)
return fig


@pytest.mark.mpl_image_compare(filename="test_meca_spec_dict.png")
def test_meca_spec_1darray():
"""
Test supplying a 1D numpy array containing a single focal mechanism to the
spec parameter.
"""
fig = Figure()
fig.meca(
# The columns are:
# longitude, latitude, depth, strike, dip, rake, magnitude
spec=np.array([0, 5, 0, 0, 90, 0, 5]),
convention="aki",
region=[-1, 1, 4, 6],
scale="2.5c",
projection="M14c",
frame=True,
)
return fig


@pytest.mark.mpl_image_compare(filename="test_meca_spec_dict.png")
def test_meca_spec_file():
"""
Test supplying a file containing a single focal mechanism to the spec
parameter.
"""
fig = Figure()
focal_mechanism = [0, 5, 0, 0, 90, 0, 5]
# writes temp file to pass to gmt
with GMTTempFile() as temp:
with open(temp.name, mode="w", encoding="utf8") as temp_file:
temp_file.write(" ".join([str(x) for x in focal_mechanism]))
# supply focal mechanisms to meca as a file
fig.meca(
spec=temp.name,
convention="aki",
region=[-1, 1, 4, 6],
scale="2.5c",
projection="M14c",
frame=True,
)
return fig


@pytest.mark.mpl_image_compare(filename="test_meca_spec_dict.png")
def test_meca_spec_dataframe():
"""
Test supplying a dictionary containing a single focal mechanism to the spec
parameter.
"""
fig = Figure()
fig.meca(
spec=pd.DataFrame(dict(strike=0, dip=90, rake=0, magnitude=5), index=[0]),
longitude=0,
latitude=5,
depth=0,
scale="2.5c",
region=[-1, 1, 4, 6],
projection="M14c",
frame=True,
)
return fig

Expand Down Expand Up @@ -54,7 +118,7 @@ def test_meca_spec_dict_list():


@pytest.mark.mpl_image_compare
def test_meca_spec_dataframe():
def test_meca_spec_two_dataframe():
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this being renamed? As far as I can tell there is just a single DataFrame in this function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was planning to have two groups of tests:

  1. In tests test_meca_spec_*, a single focal mechanism in different data types (dict, DataFrame, 1darray and plaintext file) is passed to the spec parameter
  2. In tests test_meca_spec_two_*, two focal mechanisms in different data types (dict with list as keys, DataFrame, 2D list, 2D array and plaintext file) are passed to the spec parameter.

That's why I added the _two suffix to the tests, but I agree that's not a good name for these tests. Do you have better ideas?

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense. I think it will be hard to come up with a descriptive test name without making it too wordy. I think it might just be easier to update the docstring (I made my suggestion below) to make it clear that it's passing two focal mechanisms.

"""
Test supplying a pandas.DataFrame containing focal mechanisms and locations
to the spec parameter.
Comment on lines 123 to 124
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Test supplying a pandas.DataFrame containing focal mechanisms and locations
to the spec parameter.
Test supplying two focal mechanisms and their locations by passing a pandas.DataFrame
to the spec parameter.

I think this makes it more clear why "two" is in the function name.

Expand All @@ -81,44 +145,6 @@ def test_meca_spec_dataframe():
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_1d_array():
"""
Test supplying a 1D numpy array containing focal mechanisms and locations
to the spec parameter.
"""
fig = Figure()
# supply focal mechanisms to meca as a 1D numpy array, here we are using
# the Harvard CMT zero trace convention but the focal mechanism
# parameters may be specified any of the available conventions. Since we
# are not using a dict or dataframe the convention and component should
# be specified.
focal_mechanism = [
-127.40, # longitude
40.87, # latitude
12, # depth
-3.19, # mrr
0.16, # mtt
3.03, # mff
-1.02, # mrt
-3.93, # mrf
-0.02, # mtf
23, # exponent
0, # plot_lon, 0 to plot at event location
0, # plot_lat, 0 to plot at event location
]
focal_mech_array = np.asarray(focal_mechanism)
fig.meca(
spec=focal_mech_array,
convention="mt",
component="full",
region=[-128, -127, 40, 41],
scale="2c",
projection="M14c",
)
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_2d_array():
"""
Expand Down Expand Up @@ -149,30 +175,6 @@ def test_meca_spec_2d_array():
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_file():
"""
Test supplying a file containing focal mechanisms and locations to the spec
parameter.
"""
fig = Figure()
focal_mechanism = [-127.43, 40.81, 12, -3.19, 1.16, 3.93, -1.02, -3.93, -1.02, 23]
# writes temp file to pass to gmt
with GMTTempFile() as temp:
with open(temp.name, mode="w", encoding="utf8") as temp_file:
temp_file.write(" ".join([str(x) for x in focal_mechanism]))
# supply focal mechanisms to meca as a file
fig.meca(
spec=temp.name,
convention="mt",
component="full",
region=[-128, -127, 40, 41],
scale="2c",
projection="M14c",
)
return fig


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