Skip to content

Commit d41922c

Browse files
seismanweiji14
andauthored
**Breaking**: data_kind: data is None and required now returns the 'empty' kind (#3482)
Co-authored-by: Wei Ji <[email protected]>
1 parent a5c0aa2 commit d41922c

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

pygmt/clib/session.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1773,16 +1773,17 @@ def virtualfile_in( # noqa: PLR0912
17731773
if check_kind == "raster":
17741774
valid_kinds += ("grid", "image")
17751775
elif check_kind == "vector":
1776-
valid_kinds += ("matrix", "vectors", "geojson")
1776+
valid_kinds += ("empty", "matrix", "vectors", "geojson")
17771777
if kind not in valid_kinds:
17781778
raise GMTInvalidInput(
17791779
f"Unrecognized data type for {check_kind}: {type(data)}"
17801780
)
17811781

17821782
# Decide which virtualfile_from_ function to use
17831783
_virtualfile_from = {
1784-
"file": contextlib.nullcontext,
17851784
"arg": contextlib.nullcontext,
1785+
"empty": self.virtualfile_from_vectors,
1786+
"file": contextlib.nullcontext,
17861787
"geojson": tempfile_from_geojson,
17871788
"grid": self.virtualfile_from_grid,
17881789
"image": tempfile_from_image,
@@ -1803,15 +1804,15 @@ def virtualfile_in( # noqa: PLR0912
18031804
)
18041805
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
18051806
_data = (data,) if not isinstance(data, pathlib.PurePath) else (str(data),)
1807+
elif kind == "empty":
1808+
# data is None, so data must be given via x/y/z.
1809+
_data = [x, y]
1810+
if z is not None:
1811+
_data.append(z)
1812+
if extra_arrays:
1813+
_data.extend(extra_arrays)
18061814
elif kind == "vectors":
1807-
if data is None:
1808-
# data is None, so data must be given via x/y/z.
1809-
_data = [x, y]
1810-
if z is not None:
1811-
_data.append(z)
1812-
if extra_arrays:
1813-
_data.extend(extra_arrays)
1814-
elif hasattr(data, "items") and not hasattr(data, "to_frame"):
1815+
if hasattr(data, "items") and not hasattr(data, "to_frame"):
18151816
# pandas.DataFrame or xarray.Dataset types.
18161817
# pandas.Series will be handled below like a 1-D numpy.ndarray.
18171818
_data = [array for _, array in data.items()]

pygmt/helpers/utils.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _check_encoding(
190190
def data_kind(
191191
data: Any, required: bool = True
192192
) -> Literal[
193-
"arg", "file", "geojson", "grid", "image", "matrix", "stringio", "vectors"
193+
"arg", "empty", "file", "geojson", "grid", "image", "matrix", "stringio", "vectors"
194194
]:
195195
r"""
196196
Check the kind of data that is provided to a module.
@@ -200,6 +200,8 @@ def data_kind(
200200
201201
- ``"arg"``: ``data`` is ``None`` and ``required=False``, or bool, int, float,
202202
representing an optional argument, used for dealing with optional virtual files
203+
- ``"empty"`: ``data`` is ``None`` and ``required=True``. It means the data is given
204+
via a series of vectors like x/y/z
203205
- ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
204206
representing one or more file names
205207
- ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
@@ -209,10 +211,9 @@ def data_kind(
209211
- ``"stringio"``: a :class:`io.StringIO` object
210212
- ``"matrix"``: a 2-D array-like object that implements ``__array_interface__``
211213
(e.g., :class:`numpy.ndarray`)
212-
- ``"vectors"``: ``data`` is ``None`` and ``required=True``, or any unrecognized
213-
data. Common data types include, a :class:`pandas.DataFrame` object, a dictionary
214-
with array-like values, a 1-D/3-D :class:`numpy.ndarray` object, or array-like
215-
objects.
214+
- ``"vectors"``: any unrecognized data. Common data types include, a
215+
:class:`pandas.DataFrame` object, a dictionary with array-like values, a 1-D/3-D
216+
:class:`numpy.ndarray` object, or array-like objects.
216217
217218
Parameters
218219
----------
@@ -242,6 +243,11 @@ def data_kind(
242243
>>> data_kind(data=None, required=False)
243244
'arg'
244245
246+
The "empty" kind:
247+
248+
>>> data_kind(data=None, required=True)
249+
'empty'
250+
245251
The "file" kind:
246252
247253
>>> [data_kind(data=data) for data in ("file.txt", ("file1.txt", "file2.txt"))]
@@ -293,10 +299,10 @@ def data_kind(
293299
'vectors'
294300
>>> data_kind(data=pd.Series([1, 2, 3], name="x")) # pd.Series
295301
'vectors'
296-
>>> data_kind(data=None)
297-
'vectors'
298302
"""
299303
match data:
304+
case None if required: # No data provided and required=True.
305+
kind = "empty"
300306
case str() | pathlib.PurePath(): # One file.
301307
kind = "file"
302308
case list() | tuple() if all(

pygmt/src/legend.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def legend(
9191
kwargs["F"] = box
9292

9393
kind = data_kind(spec)
94-
if spec is not None and kind not in {"file", "stringio"}:
94+
if kind not in {"empty", "file", "stringio"}:
9595
raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}")
9696
if kind == "file" and is_nonstr_iter(spec):
9797
raise GMTInvalidInput("Only one legend specification file is allowed.")

pygmt/src/plot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs):
206206

207207
kind = data_kind(data)
208208
extra_arrays = []
209-
if kind == "vectors": # Add more columns for vectors input
209+
if kind == "empty": # Add more columns for vectors input
210210
# Parameters for vector styles
211211
if (
212212
kwargs.get("S") is not None

pygmt/src/plot3d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def plot3d(
184184
kind = data_kind(data)
185185
extra_arrays = []
186186

187-
if kind == "vectors": # Add more columns for vectors input
187+
if kind == "empty": # Add more columns for vectors input
188188
# Parameters for vector styles
189189
if (
190190
kwargs.get("S") is not None

pygmt/src/text.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def text_( # noqa: PLR0912
196196
raise GMTInvalidInput("'text' can't be None or array when 'position' is given.")
197197
if textfiles is not None and text is not None:
198198
raise GMTInvalidInput("'text' can't be specified when 'textfiles' is given.")
199-
if kind == "vectors" and text is None:
199+
if kind == "empty" and text is None:
200200
raise GMTInvalidInput("Must provide text with x/y pairs.")
201201

202202
# Arguments that can accept arrays.
@@ -220,7 +220,7 @@ def text_( # noqa: PLR0912
220220

221221
extra_arrays = []
222222
confdict = {}
223-
if kind == "vectors":
223+
if kind == "empty":
224224
for arg, flag, name in array_args:
225225
if is_nonstr_iter(arg):
226226
kwargs["F"] += flag

0 commit comments

Comments
 (0)