-
Notifications
You must be signed in to change notification settings - Fork 227
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
Refactor the data_kind and validate_data_input functions #3335
Conversation
48e1378
to
e79a7d0
Compare
1316a94
to
008a56f
Compare
9211a90
to
46fb307
Compare
@@ -193,15 +188,6 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data | |||
kind = "matrix" | |||
else: | |||
kind = "vectors" | |||
_validate_data_input( |
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.
Removed from here and moved to Session.virtualfile_in
.
@@ -111,7 +115,9 @@ def _validate_data_input( | |||
raise GMTInvalidInput("data must provide x, y, and z columns.") | |||
|
|||
|
|||
def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data=True): | |||
def data_kind( |
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.
Now this function only checks data
, and x
/y
/z
is no longer used.
@@ -32,25 +25,6 @@ def test_load_static_earth_relief(): | |||
assert isinstance(data, xr.DataArray) | |||
|
|||
|
|||
@pytest.mark.parametrize( |
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.
This test is removed because:
data_kind
no longer checks if the input is valid- All these tests are already covered by the
validate_data_input
doctests.
pygmt/helpers/utils.py
Outdated
@@ -124,64 +130,53 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data | |||
* 1-D arrays x and y (and z, optionally) |
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.
Delete this line about x/y/z?
* 1-D arrays x and y (and z, optionally) |
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.
Updated the docstring at b575591. Currently, any unrecognized data type is taken as a matrix
type. It's not ideal, so I plan to refactor the data_kind function later, and will also polish the docstring when refactoring.
'arg' | ||
>>> data_kind(data=xr.DataArray(np.random.rand(4, 3))) | ||
'grid' | ||
>>> data_kind(data=xr.DataArray(np.random.rand(3, 4, 5))) | ||
'image' | ||
""" | ||
# determine the data kind | ||
kind: Literal["arg", "file", "geojson", "grid", "image", "matrix", "vectors"] |
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.
What is this line doing here?
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.
To suppress the mypy error:
pygmt/helpers/utils.py:191: error: Incompatible return value type (got "str", expected "Literal['arg', 'file', 'geojson', 'grid', 'image', 'matrix', 'vectors']") [return-value]
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.
Ah interesting. Probably should turn this into an enum, but leave that for another day.
pygmt/helpers/utils.py
Outdated
Check what kind of data is provided to a module. | ||
|
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.
Since x/y/z is no longer used, at L124-129, change the docstring to read:
Possible types (provided to `data`):
* a file name
* a pathlib.PurePath object
* an xarray.DataArray object
* a 2-D matrix
pygmt/helpers/utils.py
Outdated
@@ -124,64 +130,53 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data | |||
* 1-D arrays x and y (and z, optionally) | |||
* an optional argument (None, bool, int or float) provided as 'data' |
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.
* an optional argument (None, bool, int or float) provided as 'data' | |
* an optional argument (None, bool, int or float) |
Do you know why the pygmt/pygmt/tests/test_velo.py Lines 30 to 42 in d3101f3
|
It passes in the latest rerun. |
Description of proposed changes
This PR is a subset of PR #2744. PR #2744 takes more time than I initially expected (Still not satisfied with the new
Session.virtualfile_in
API function), so better to refactor thedata_kind
/validate_data_input
function in a separate PR (this PR).As mentioned in PR #2744:
This PR moves the
validate_data_input
function outside of thedata_kind
, and calls it inSession.virtualfile_in
explicitly. In this way, each function only focuses on one single data. which makes the functions easier to maintain.I also don't like the current function design of the
data_kind
/validate_data_input
functions, but I prefer to refactor the function details in a separate PR, to make this PR smaller for reviewing.Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash command is:
/format
: automatically format and lint the code