45
45
]
46
46
47
47
48
- def _validate_data_input (data : Any , kind : Kind , required_z : bool = False ) -> None :
48
+ def _validate_data_input (data : Any , kind : Kind , ncols = 2 ) -> None :
49
49
"""
50
50
Check if the data to be passed to the virtualfile_from_ functions is valid.
51
51
@@ -67,7 +67,8 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
67
67
Traceback (most recent call last):
68
68
...
69
69
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
70
- >>> _validate_data_input(data=[[1, 2, 3], [4, 5, 6]], kind="empty", required_z=True)
70
+ >>> _validate_data_input(data=[[1, 2, 3], [4, 5, 6]], kind="empty", ncols=3)
71
+ >>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], ncols=3)
71
72
Traceback (most recent call last):
72
73
...
73
74
pygmt.exceptions.GMTInvalidInput: Must provide x, y, and z.
@@ -78,7 +79,7 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
78
79
>>> import pandas as pd
79
80
>>> import xarray as xr
80
81
>>> data = np.arange(8).reshape((4, 2))
81
- >>> _validate_data_input(data=data, kind="matrix", required_z=True )
82
+ >>> _validate_data_input(data=data, ncols=3, kind="matrix")
82
83
Traceback (most recent call last):
83
84
...
84
85
pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
@@ -88,16 +89,16 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
88
89
89
90
>>> _validate_data_input(
90
91
... data=pd.DataFrame(data, columns=["x", "y"]),
92
+ ... ncols=3,
91
93
... kind="vectors",
92
- ... required_z=True,
93
94
... )
94
95
Traceback (most recent call last):
95
96
...
96
97
pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
97
98
>>> _validate_data_input(
98
99
... data=xr.Dataset(pd.DataFrame(data, columns=["x", "y"])),
100
+ ... ncols=3,
99
101
... kind="vectors",
100
- ... required_z=True,
101
102
... )
102
103
Traceback (most recent call last):
103
104
...
@@ -108,28 +109,25 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
108
109
GMTInvalidInput
109
110
If the data input is not valid.
110
111
"""
111
- # Determine the required number of columns based on the required_z flag.
112
- required_cols = 3 if required_z else 1
113
-
114
112
match kind :
115
113
case "empty" : # data = [x, y], [x, y, z], [x, y, z, ...]
116
114
if len (data ) < 2 or any (v is None for v in data [:2 ]):
117
115
msg = "Must provide both x and y."
118
116
raise GMTInvalidInput (msg )
119
- if required_z and (len (data ) < 3 or data [:3 ] is None ):
117
+ if ncols >= 3 and (len (data ) < 3 or data [:3 ] is None ):
120
118
msg = "Must provide x, y, and z."
121
119
raise GMTInvalidInput (msg )
122
120
case "matrix" : # 2-D numpy.ndarray
123
- if (actual_cols := data .shape [1 ]) < required_cols :
124
- msg = f"Need at least { required_cols } columns but { actual_cols } column(s) are given."
121
+ if (actual_cols := data .shape [1 ]) < ncols :
122
+ msg = f"Need at least { ncols } columns but { actual_cols } column(s) are given."
125
123
raise GMTInvalidInput (msg )
126
124
case "vectors" :
127
125
# "vectors" means the original data is either dictionary, list, tuple,
128
126
# pandas.DataFrame, pandas.Series, xarray.Dataset, or xarray.DataArray.
129
127
# The original data is converted to a list of vectors or a 2-D numpy.ndarray
130
128
# in the virtualfile_in function.
131
- if (actual_cols := len (data )) < required_cols :
132
- msg = f"Need at least { required_cols } columns but { actual_cols } column(s) are given."
129
+ if (actual_cols := len (data )) < ncols :
130
+ msg = f"Need at least { ncols } columns but { actual_cols } column(s) are given."
133
131
raise GMTInvalidInput (msg )
134
132
135
133
0 commit comments