47
47
48
48
def _validate_data_input (data : Any , kind : Kind , required_z : bool = False ) -> None :
49
49
"""
50
- Check if the combination of data/x/y/z is valid.
50
+ Check if the data to be passed to the virtualfile_from_ functions is valid.
51
51
52
52
Examples
53
53
--------
54
- >>> _validate_data_input(data="infile")
55
- >>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6])
56
- >>> _validate_data_input(x=[ 1, 2, 3], y= [4, 5, 6], z=[7, 8, 9] )
57
- >>> _validate_data_input(data=None, required_data=False )
58
- >>> _validate_data_input()
54
+ The "empty" kind means the data is given via a series of vectors like x/y/z.
55
+
56
+ >>> _validate_data_input(data=[[ 1, 2, 3], [4, 5, 6]], kind="empty" )
57
+ >>> _validate_data_input(data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], kind="empty" )
58
+ >>> _validate_data_input(data=[None, [4, 5, 6]], kind="empty" )
59
59
Traceback (most recent call last):
60
60
...
61
- pygmt.exceptions.GMTInvalidInput: No input data provided .
62
- >>> _validate_data_input(x=[ 1, 2, 3])
61
+ pygmt.exceptions.GMTInvalidInput: Must provide both x and y .
62
+ >>> _validate_data_input(data=[[ 1, 2, 3], None], kind="empty" )
63
63
Traceback (most recent call last):
64
64
...
65
65
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
66
- >>> _validate_data_input(y=[4, 5, 6] )
66
+ >>> _validate_data_input(data=[None, None], kind="empty" )
67
67
Traceback (most recent call last):
68
68
...
69
69
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
70
- >>> _validate_data_input(x=[ 1, 2, 3], y= [4, 5, 6], required_z=True)
70
+ >>> _validate_data_input(data=[[ 1, 2, 3], [4, 5, 6]], kind="empty" , required_z=True)
71
71
Traceback (most recent call last):
72
72
...
73
73
pygmt.exceptions.GMTInvalidInput: Must provide x, y, and z.
74
+
75
+ The "matrix" kind means the data is given via a 2-D numpy.ndarray.
76
+
74
77
>>> import numpy as np
75
78
>>> import pandas as pd
76
79
>>> import xarray as xr
77
80
>>> data = np.arange(8).reshape((4, 2))
78
- >>> _validate_data_input(data=data, required_z=True, kind="matrix")
81
+ >>> _validate_data_input(data=data, kind="matrix", required_z=True )
79
82
Traceback (most recent call last):
80
83
...
81
84
pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
85
+
86
+ The "vectors" kind means the original data is either dictionary, list, tuple,
87
+ pandas.DataFrame, pandas.Series, xarray.Dataset, or xarray.DataArray.
88
+
82
89
>>> _validate_data_input(
83
90
... data=pd.DataFrame(data, columns=["x", "y"]),
84
- ... required_z=True,
85
91
... kind="vectors",
92
+ ... required_z=True,
86
93
... )
87
94
Traceback (most recent call last):
88
95
...
89
96
pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
90
97
>>> _validate_data_input(
91
98
... data=xr.Dataset(pd.DataFrame(data, columns=["x", "y"])),
92
- ... required_z=True,
93
99
... kind="vectors",
100
+ ... required_z=True,
94
101
... )
95
102
Traceback (most recent call last):
96
103
...
97
104
pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
98
- >>> _validate_data_input(data="infile", x=[1, 2, 3])
99
- Traceback (most recent call last):
100
- ...
101
- pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
102
- >>> _validate_data_input(data="infile", y=[4, 5, 6])
103
- Traceback (most recent call last):
104
- ...
105
- pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
106
- >>> _validate_data_input(data="infile", x=[1, 2, 3], y=[4, 5, 6])
107
- Traceback (most recent call last):
108
- ...
109
- pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
110
- >>> _validate_data_input(data="infile", z=[7, 8, 9])
111
- Traceback (most recent call last):
112
- ...
113
- pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
114
105
115
106
Raises
116
107
------
@@ -120,29 +111,25 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
120
111
# Determine the required number of columns based on the required_z flag.
121
112
required_cols = 3 if required_z else 1
122
113
123
- # Check based on the data kind.
124
114
match kind :
125
- case "empty" : # data = [x, y, z]
126
- if required_z and len (data ) < 3 :
127
- msg = "Must provide x, y, and z."
128
- raise GMTInvalidInput (msg )
129
- if any (v is None for v in data ):
115
+ case "empty" : # data = [x, y], [x, y, z], [x, y, z, ...]
116
+ if len (data ) < 2 or any (v is None for v in data [:2 ]):
130
117
msg = "Must provide both x and y."
131
118
raise GMTInvalidInput (msg )
119
+ if required_z and (len (data ) < 3 or data [:3 ] is None ):
120
+ msg = "Must provide x, y, and z."
121
+ raise GMTInvalidInput (msg )
132
122
case "matrix" : # 2-D numpy.ndarray
133
123
if (actual_cols := data .shape [1 ]) < required_cols :
134
- msg = (
135
- f"Need at least { required_cols } columns but { actual_cols } column(s) "
136
- "are given."
137
- )
124
+ msg = f"Need at least { required_cols } columns but { actual_cols } column(s) are given."
138
125
raise GMTInvalidInput (msg )
139
126
case "vectors" :
140
- # Check if the number of columns is sufficient.
127
+ # "vectors" means the original data is either dictionary, list, tuple,
128
+ # pandas.DataFrame, pandas.Series, xarray.Dataset, or xarray.DataArray.
129
+ # The original data is converted to a list of vectors or a 2-D numpy.ndarray
130
+ # in the virtualfile_in function.
141
131
if (actual_cols := len (data )) < required_cols :
142
- msg = (
143
- f"Need at least { required_cols } columns but { actual_cols } "
144
- "column(s) are given."
145
- )
132
+ msg = f"Need at least { required_cols } columns but { actual_cols } column(s) are given."
146
133
raise GMTInvalidInput (msg )
147
134
148
135
0 commit comments