Skip to content

Commit 9c47b38

Browse files
authored
clib.Session.virtualfile_in: Refactor if-else statements into match-case statements (#3521)
1 parent d41922c commit 9c47b38

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

Diff for: pygmt/clib/session.py

+37-38
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import contextlib
99
import ctypes as ctp
1010
import io
11-
import pathlib
1211
import sys
1312
import warnings
1413
from collections.abc import Callable, Generator, Sequence
@@ -1792,43 +1791,43 @@ def virtualfile_in( # noqa: PLR0912
17921791
"vectors": self.virtualfile_from_vectors,
17931792
}[kind]
17941793

1795-
# Ensure the data is an iterable (Python list or tuple)
1796-
if kind in {"geojson", "grid", "image", "file", "arg", "stringio"}:
1797-
if kind == "image" and data.dtype != "uint8":
1798-
msg = (
1799-
f"Input image has dtype: {data.dtype} which is unsupported, "
1800-
"and may result in an incorrect output. Please recast image "
1801-
"to a uint8 dtype and/or scale to 0-255 range, e.g. "
1802-
"using a histogram equalization function like "
1803-
"skimage.exposure.equalize_hist."
1804-
)
1805-
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
1806-
_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)
1814-
elif kind == "vectors":
1815-
if hasattr(data, "items") and not hasattr(data, "to_frame"):
1816-
# pandas.DataFrame or xarray.Dataset types.
1817-
# pandas.Series will be handled below like a 1-D numpy.ndarray.
1818-
_data = [array for _, array in data.items()]
1819-
else:
1820-
# Python list, tuple, numpy.ndarray, and pandas.Series types
1821-
_data = np.atleast_2d(np.asanyarray(data).T)
1822-
elif kind == "matrix":
1823-
# GMT can only accept a 2-D matrix which are signed integer (i), unsigned
1824-
# integer (u) or floating point (f) types. For other data types, we need to
1825-
# use virtualfile_from_vectors instead, which turns the matrix into a list
1826-
# of vectors and allows for better handling of non-integer/float type inputs
1827-
# (e.g. for string or datetime data types).
1828-
_data = (data,)
1829-
if data.dtype.kind not in "iuf":
1830-
_virtualfile_from = self.virtualfile_from_vectors
1831-
_data = data.T
1794+
# Ensure the data is an iterable (Python list or tuple).
1795+
match kind:
1796+
case "arg" | "file" | "geojson" | "grid" | "image" | "stringio":
1797+
_data = (data,)
1798+
if kind == "image" and data.dtype != "uint8":
1799+
msg = (
1800+
f"Input image has dtype: {data.dtype} which is unsupported, "
1801+
"and may result in an incorrect output. Please recast image "
1802+
"to a uint8 dtype and/or scale to 0-255 range, e.g. "
1803+
"using a histogram equalization function like "
1804+
"skimage.exposure.equalize_hist."
1805+
)
1806+
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
1807+
case "empty": # data is None, so data must be given via x/y/z.
1808+
_data = [x, y]
1809+
if z is not None:
1810+
_data.append(z)
1811+
if extra_arrays:
1812+
_data.extend(extra_arrays)
1813+
case "vectors":
1814+
if hasattr(data, "items") and not hasattr(data, "to_frame"):
1815+
# pandas.DataFrame or xarray.Dataset types.
1816+
# pandas.Series will be handled below like a 1-D numpy.ndarray.
1817+
_data = [array for _, array in data.items()]
1818+
else:
1819+
# Python list, tuple, numpy.ndarray, and pandas.Series types
1820+
_data = np.atleast_2d(np.asanyarray(data).T)
1821+
case "matrix":
1822+
# GMT can only accept a 2-D matrix which are signed integer (i),
1823+
# unsigned integer (u) or floating point (f) types. For other data
1824+
# types, we need to use virtualfile_from_vectors instead, which turns
1825+
# the matrix into a list of vectors and allows for better handling of
1826+
# non-integer/float type inputs (e.g. for string or datetime data types)
1827+
_data = (data,)
1828+
if data.dtype.kind not in "iuf":
1829+
_virtualfile_from = self.virtualfile_from_vectors
1830+
_data = data.T
18321831

18331832
# Finally create the virtualfile from the data, to be passed into GMT
18341833
file_context = _virtualfile_from(*_data)

0 commit comments

Comments
 (0)