Skip to content

Commit e79a7d0

Browse files
committed
Make _validate_data_input public and move it to Session.virtualfile_in
1 parent 318a8c4 commit e79a7d0

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

pygmt/clib/session.py

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
data_kind,
3838
tempfile_from_geojson,
3939
tempfile_from_image,
40+
validate_data_input,
4041
)
4142

4243
FAMILIES = [
@@ -1594,6 +1595,15 @@ def virtualfile_in( # noqa: PLR0912
15941595
kind = data_kind(
15951596
data, x, y, z, required_z=required_z, required_data=required_data
15961597
)
1598+
validate_data_input(
1599+
data=data,
1600+
x=x,
1601+
y=y,
1602+
z=z,
1603+
required_z=required_z,
1604+
required_data=required_data,
1605+
kind=kind,
1606+
)
15971607

15981608
if check_kind:
15991609
valid_kinds = ("file", "arg") if required_data is False else ("file",)

pygmt/helpers/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
is_nonstr_iter,
2323
launch_external_viewer,
2424
non_ascii_to_octal,
25+
validate_data_input,
2526
)
2627
from pygmt.helpers.validators import validate_output_table_type

pygmt/helpers/utils.py

+15-24
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,67 @@
1919
from pygmt.exceptions import GMTInvalidInput
2020

2121

22-
def _validate_data_input(
22+
def validate_data_input(
2323
data=None, x=None, y=None, z=None, required_z=False, required_data=True, kind=None
2424
):
2525
"""
2626
Check if the combination of data/x/y/z is valid.
2727
2828
Examples
2929
--------
30-
>>> _validate_data_input(data="infile")
31-
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6])
32-
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], z=[7, 8, 9])
33-
>>> _validate_data_input(data=None, required_data=False)
34-
>>> _validate_data_input()
30+
>>> validate_data_input(data="infile")
31+
>>> validate_data_input(x=[1, 2, 3], y=[4, 5, 6])
32+
>>> validate_data_input(x=[1, 2, 3], y=[4, 5, 6], z=[7, 8, 9])
33+
>>> validate_data_input(data=None, required_data=False)
34+
>>> validate_data_input()
3535
Traceback (most recent call last):
3636
...
3737
pygmt.exceptions.GMTInvalidInput: No input data provided.
38-
>>> _validate_data_input(x=[1, 2, 3])
38+
>>> validate_data_input(x=[1, 2, 3])
3939
Traceback (most recent call last):
4040
...
4141
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
42-
>>> _validate_data_input(y=[4, 5, 6])
42+
>>> validate_data_input(y=[4, 5, 6])
4343
Traceback (most recent call last):
4444
...
4545
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
46-
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], required_z=True)
46+
>>> validate_data_input(x=[1, 2, 3], y=[4, 5, 6], required_z=True)
4747
Traceback (most recent call last):
4848
...
4949
pygmt.exceptions.GMTInvalidInput: Must provide x, y, and z.
5050
>>> import numpy as np
5151
>>> import pandas as pd
5252
>>> import xarray as xr
5353
>>> data = np.arange(8).reshape((4, 2))
54-
>>> _validate_data_input(data=data, required_z=True, kind="matrix")
54+
>>> validate_data_input(data=data, required_z=True, kind="matrix")
5555
Traceback (most recent call last):
5656
...
5757
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
58-
>>> _validate_data_input(
58+
>>> validate_data_input(
5959
... data=pd.DataFrame(data, columns=["x", "y"]),
6060
... required_z=True,
6161
... kind="matrix",
6262
... )
6363
Traceback (most recent call last):
6464
...
6565
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
66-
>>> _validate_data_input(
66+
>>> validate_data_input(
6767
... data=xr.Dataset(pd.DataFrame(data, columns=["x", "y"])),
6868
... required_z=True,
6969
... kind="matrix",
7070
... )
7171
Traceback (most recent call last):
7272
...
7373
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
74-
>>> _validate_data_input(data="infile", x=[1, 2, 3])
74+
>>> validate_data_input(data="infile", x=[1, 2, 3])
7575
Traceback (most recent call last):
7676
...
7777
pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
78-
>>> _validate_data_input(data="infile", y=[4, 5, 6])
78+
>>> validate_data_input(data="infile", y=[4, 5, 6])
7979
Traceback (most recent call last):
8080
...
8181
pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
82-
>>> _validate_data_input(data="infile", z=[7, 8, 9])
82+
>>> validate_data_input(data="infile", z=[7, 8, 9])
8383
Traceback (most recent call last):
8484
...
8585
pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
@@ -193,15 +193,6 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data
193193
kind = "matrix"
194194
else:
195195
kind = "vectors"
196-
_validate_data_input(
197-
data=data,
198-
x=x,
199-
y=y,
200-
z=z,
201-
required_z=required_z,
202-
required_data=required_data,
203-
kind=kind,
204-
)
205196
return kind
206197

207198

0 commit comments

Comments
 (0)