-
Notifications
You must be signed in to change notification settings - Fork 232
Refactor the data_kind and validate_data_input functions #3335
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
Changes from 8 commits
e79a7d0
e2a8ceb
008a56f
46fb307
d3854da
a9670aa
7ff0d9e
70962d0
b575591
c522e36
0bde17d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,7 +12,7 @@ | |||||
import warnings | ||||||
import webbrowser | ||||||
from collections.abc import Iterable, Sequence | ||||||
from typing import Any | ||||||
from typing import Any, Literal | ||||||
|
||||||
import xarray as xr | ||||||
from pygmt.encodings import charset | ||||||
|
@@ -79,6 +79,10 @@ def _validate_data_input( | |||||
Traceback (most recent call last): | ||||||
... | ||||||
pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z. | ||||||
>>> _validate_data_input(data="infile", x=[1, 2, 3], y=[4, 5, 6]) | ||||||
Traceback (most recent call last): | ||||||
... | ||||||
pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z. | ||||||
>>> _validate_data_input(data="infile", z=[7, 8, 9]) | ||||||
Traceback (most recent call last): | ||||||
... | ||||||
|
@@ -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( | ||||||
data: Any = None, required: bool = True | ||||||
) -> Literal["arg", "file", "geojson", "grid", "image", "matrix", "vectors"]: | ||||||
""" | ||||||
Check what kind of data is provided to a module. | ||||||
|
||||||
|
||||||
|
@@ -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) | ||||||
|
* 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.
Outdated
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) |
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.
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
.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
from pathlib import Path | ||
|
||
import numpy as np | ||
import pytest | ||
import xarray as xr | ||
from pygmt import Figure | ||
|
@@ -13,7 +12,6 @@ | |
GMTTempFile, | ||
args_in_kwargs, | ||
build_arg_list, | ||
data_kind, | ||
kwargs_to_strings, | ||
unique_name, | ||
) | ||
|
@@ -33,25 +31,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 commentThe reason will be displayed to describe this comment to others. Learn more. This test is removed because:
|
||
("data", "x", "y"), | ||
[ | ||
(None, None, None), | ||
("data.txt", np.array([1, 2]), np.array([4, 5])), | ||
("data.txt", np.array([1, 2]), None), | ||
("data.txt", None, np.array([4, 5])), | ||
(None, np.array([1, 2]), None), | ||
(None, None, np.array([4, 5])), | ||
], | ||
) | ||
def test_data_kind_fails(data, x, y): | ||
""" | ||
Make sure data_kind raises exceptions when it should. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
data_kind(data=data, x=x, y=y) | ||
|
||
|
||
def test_unique_name(): | ||
""" | ||
Make sure the names are really unique. | ||
|
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
, andx
/y
/z
is no longer used.