-
Notifications
You must be signed in to change notification settings - Fork 224
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a290994
Rename the test test_meca_spec_dictionary to test_meca_spec_dict
seisman ab56278
Improve the test_meca_spec_dict test with a better baseline image
seisman 88ee9d8
Rewrite test_meca_spec_1d_array test to use the existing baseline image
seisman ee791db
Rewrite the test_meca_spec_file test to reuse the existing baseline i…
seisman 3e21af4
Rename the test test_meca_spec_dataframe to test_meca_spec_two_dataframe
seisman 7b1b17e
Add a new test test_meca_spec_dataframe to test passing a dataframe t…
seisman 61c4f2a
Merge branch 'main' into improve-meca-tests
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...baseline/test_meca_spec_dataframe.png.dvc → ...line/test_meca_spec_two_dataframe.png.dvc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||||
|
@@ -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 | ||||||||||
|
||||||||||
|
@@ -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(): | ||||||||||
""" | ||||||||||
Test supplying a pandas.DataFrame containing focal mechanisms and locations | ||||||||||
to the spec parameter. | ||||||||||
Comment on lines
123
to
124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this makes it more clear why "two" is in the function name. |
||||||||||
|
@@ -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(): | ||||||||||
""" | ||||||||||
|
@@ -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(): | ||||||||||
""" | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
test_meca_spec_*
, a single focal mechanism in different data types (dict, DataFrame, 1darray and plaintext file) is passed to thespec
parametertest_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 thespec
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?There was a problem hiding this comment.
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.