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

Raise GMTInvalidInput exception when required z is missing #1478

Merged
merged 27 commits into from
Sep 15, 2021

Conversation

yohaimagen
Copy link
Contributor

@yohaimagen yohaimagen commented Sep 1, 2021

adding an additional if statement to the data_kind utils function that is based on a new parameter requierd_z and checks if z vector is provided alongside x and y.

Fixes #1447

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@yohaimagen yohaimagen added the enhancement Improving an existing feature label Sep 1, 2021
@yohaimagen yohaimagen self-assigned this Sep 1, 2021
@yohaimagen
Copy link
Contributor Author

Where should I add tests for not providing the z vector?

@maxrjones
Copy link
Member

Where should I add tests for not providing the z vector?

I think the test should go in pygmt/tests/test_clib.py.

@yohaimagen
Copy link
Contributor Author

yohaimagen commented Sep 2, 2021

I added the test.

If we decide to go the route of requierd_z(see discussion at #1447) we should add requierd_z = True in all the functions that do require z column. Should we do it over here or in a separate PR for each function?

Copy link
Member

@maxrjones maxrjones left a comment

Choose a reason for hiding this comment

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

I recommend adding required_z to the appropriate functions in this PR.

It would be good to raise the same exception if the data kind is matrix (L78) and data.shape[1] < 3 when required_z is set. Would you be able to add this to the PR and add another pytest.raises(GMTInvalidInput) check to the new test?

pygmt/helpers/utils.py Outdated Show resolved Hide resolved
pygmt/helpers/utils.py Outdated Show resolved Hide resolved
pygmt/helpers/utils.py Outdated Show resolved Hide resolved
Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
yohaimagen and others added 2 commits September 3, 2021 13:21
Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
@yohaimagen
Copy link
Contributor Author

/format

@yohaimagen
Copy link
Contributor Author

yohaimagen commented Sep 3, 2021

I am making a list of functions that should pass required_z=True please add functions that I've missed. There are quite a lot of new functions that have been wrapped recently and will be released in 0.5 please let me know which one of them needs to be set required_z=True.

  • plot3d
  • countour
  • blockmean
  • blockmedian
  • surface
  • wiggle

@yohaimagen
Copy link
Contributor Author

Should we remove the tests of the relevant functions that test the passing of z column?

for variable in product([None, data[:, 0]], repeat=3):
# Filter one valid configuration:
if not any(item is None for item in variable):
continue
with pytest.raises(GMTInvalidInput):
fig.contour(
x=variable[0],
y=variable[1],
z=variable[2],
region=region,
projection="X4i",
color="red",
frame="afg",
pen="",
)

if so we shuld extend the test to be similar to the above.

Copy link
Member

@maxrjones maxrjones left a comment

Choose a reason for hiding this comment

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

Nice work, thank you! 🎉

As you have seen, we leave approved pull requests up for at least a day so other maintainers have a final opportunity to check it out. In the meantime, please apply the two minor grammatical suggestions.

pygmt/tests/test_clib.py Outdated Show resolved Hide resolved
pygmt/tests/test_clib.py Outdated Show resolved Hide resolved
Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
@yohaimagen
Copy link
Contributor Author

Actually, when writing the test for the case of a 2d array data with less than three columns when z is required, I noticed that we do not check that 2d array data have at least 2 columns in the general case, should we raise an error if a 2d matrix of a shape nx1 is passed? and if so should I add it here or open another PR?

pygmt/tests/test_clib.py Outdated Show resolved Hide resolved
pygmt/tests/test_clib.py Outdated Show resolved Hide resolved
@seisman
Copy link
Member

seisman commented Sep 14, 2021

Actually, when writing the test for the case of a 2d array data with less than three columns when z is required, I noticed that we do not check that 2d array data have at least 2 columns in the general case, should we raise an error if a 2d matrix of a shape nx1 is passed? and if so should I add it here or open another PR?

It's more complicated, because for some modules (e.g., histogram), only one column is required.

@seisman seisman added this to the 0.5.0 milestone Sep 14, 2021
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@seisman seisman changed the title Raise gmt invalid input exception when z missing Raise GMTInvalidInput exception when z missing Sep 14, 2021
@seisman seisman changed the title Raise GMTInvalidInput exception when z missing Raise GMTInvalidInput exception when required z is missing Sep 14, 2021
@seisman
Copy link
Member

seisman commented Sep 14, 2021

Actually, when writing the test for the case of a 2d array data with less than three columns when z is required, I noticed that we do not check that 2d array data have at least 2 columns in the general case, should we raise an error if a 2d matrix of a shape nx1 is passed? and if so should I add it here or open another PR?

It's more complicated, because for some modules (e.g., histogram), only one column is required.

I think this PR is good for merge and we can deal with the 2-column data check in another issue/PR.

@seisman seisman added the final review call This PR requires final review and approval from a second reviewer label Sep 14, 2021
Copy link
Member

@maxrjones maxrjones left a comment

Choose a reason for hiding this comment

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

I agree with @seisman that more changes aren't needed. Great work, thanks!

@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Sep 15, 2021
@seisman seisman merged commit 03d6797 into main Sep 15, 2021
@seisman seisman deleted the raise_GMTInvalidInput_exception_when_z_missing branch September 15, 2021 23:58
weiji14 added a commit to JamieJQuinn/pygmt that referenced this pull request Sep 16, 2021
@seisman seisman mentioned this pull request Sep 18, 2021
5 tasks
@@ -78,6 +80,8 @@ def data_kind(data, x=None, y=None, z=None):
elif hasattr(data, "__geo_interface__"):
kind = "geojson"
elif data is not None:
if required_z and data.shape[1] < 3:
Copy link
Member

@weiji14 weiji14 Sep 18, 2021

Choose a reason for hiding this comment

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

Checking for data.shape[1] fails for xarray.Dataset inputs with AttributeError: 'Dataset' object has no attribute 'shape'. The shape attribute is valid only for numpy.ndarray, pandas.DataFrame, and maybe a few other PyData objects. Will need to create a bugfix and add some extra tests for xarray.Dataset inputs.

Copy link
Member

@maxrjones maxrjones Sep 18, 2021

Choose a reason for hiding this comment

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

I can submit a PR fix since I think that I suggested this. But I feel that we should not label it as a bug since this code has not yet been included in a release, so it might be confusing to list on the changelog as a bugfix relative to v0.4.1.

Edit: Just noticed the comment on the other PR that @weiji14 would fix this. That's also fine.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I've submitted a bugfix PR at #1523. Have put on the skip-changelog label so it won't show up in the changelog :)

sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 2022
…ppingTools#1478)

Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Raise a GMTInvalidInput exception for only x,y input to blockmedian and blockmean
5 participants