Skip to content

Commit 713d0a2

Browse files
committed
Refactor using match-case statements
1 parent af3ea38 commit 713d0a2

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

pygmt/clib/session.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import contextlib
99
import ctypes as ctp
10-
import pathlib
1110
import sys
1211
import warnings
1312
from collections.abc import Generator, Sequence
@@ -1720,44 +1719,45 @@ def virtualfile_in( # noqa: PLR0912
17201719
}[kind]
17211720

17221721
# Ensure the data is an iterable (Python list or tuple)
1723-
if kind in {"geojson", "grid", "image", "file", "arg"}:
1724-
if kind == "image" and data.dtype != "uint8":
1725-
msg = (
1726-
f"Input image has dtype: {data.dtype} which is unsupported, "
1727-
"and may result in an incorrect output. Please recast image "
1728-
"to a uint8 dtype and/or scale to 0-255 range, e.g. "
1729-
"using a histogram equalization function like "
1730-
"skimage.exposure.equalize_hist."
1731-
)
1732-
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
1733-
_data = (data,) if not isinstance(data, pathlib.PurePath) else (str(data),)
1734-
elif kind == "none": # data is given via a series of vectors
1735-
_data = [np.atleast_1d(x), np.atleast_1d(y)]
1736-
if z is not None:
1737-
_data.append(np.atleast_1d(z))
1738-
if extra_arrays:
1739-
_data.extend(extra_arrays)
1740-
elif kind == "vectors":
1741-
if hasattr(data, "items") and not hasattr(data, "to_frame"):
1742-
# pandas.DataFrame or xarray.Dataset types.
1743-
# pandas.Series will be handled below like a 1-D numpy.ndarray.
1744-
_data = [array for _, array in data.items()]
1745-
else:
1746-
# Python list, tuple, and pandas.Series types
1747-
_data = np.atleast_2d(np.asanyarray(data).T)
1748-
elif kind == "matrix": # 2-D numpy.ndarray
1749-
if data.dtype.kind in "iuf":
1722+
match kind:
1723+
case "arg" | "file" | "geojson" | "grid":
1724+
_data = (data,)
1725+
case "image":
1726+
if data.dtype != "uint8":
1727+
msg = (
1728+
f"Input image has dtype: {data.dtype} which is unsupported, "
1729+
"and may result in an incorrect output. Please recast image "
1730+
"to a uint8 dtype and/or scale to 0-255 range, e.g. "
1731+
"using a histogram equalization function like "
1732+
"skimage.exposure.equalize_hist."
1733+
)
1734+
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
1735+
_data = (data,)
1736+
case "matrix": # 2-D numpy.ndarray
17501737
# virtualfile_from_matrix only support 2-D numpy.ndarray which are
17511738
# signed integer (i), unsigned integer (u) or floating point (f) types
1752-
_data = (data,)
1753-
else: # turn 2-D numpy.ndarray into list of arrays
1754-
_virtualfile_from = self.virtualfile_from_vectors
1755-
_data = list(data.T)
1739+
if data.dtype.kind in "iuf":
1740+
_data = (data,)
1741+
else: # turn 2-D numpy.ndarray into list of arrays
1742+
_virtualfile_from = self.virtualfile_from_vectors
1743+
_data = list(data.T)
1744+
case "none": # data is given via a series of vectors
1745+
_data = [np.atleast_1d(x), np.atleast_1d(y)]
1746+
if z is not None:
1747+
_data.append(np.atleast_1d(z))
1748+
if extra_arrays:
1749+
_data.extend(extra_arrays)
1750+
case "vectors":
1751+
if hasattr(data, "items") and not hasattr(data, "to_frame"):
1752+
# pandas.DataFrame or xarray.Dataset types.
1753+
# pandas.Series will be handled below like a 1-D numpy.ndarray.
1754+
_data = [array for _, array in data.items()]
1755+
else:
1756+
# Python list, tuple, and pandas.Series types
1757+
_data = np.atleast_2d(np.asanyarray(data).T)
17561758

17571759
# Finally create the virtualfile from the data, to be passed into GMT
1758-
file_context = _virtualfile_from(*_data)
1759-
1760-
return file_context
1760+
return _virtualfile_from(*_data)
17611761

17621762
def virtualfile_from_data(
17631763
self,

0 commit comments

Comments
 (0)