From 9c47b388f1e1926f5045904450b887a4726b11e9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 16 Oct 2024 11:01:19 +0800 Subject: [PATCH] clib.Session.virtualfile_in: Refactor if-else statements into match-case statements (#3521) --- pygmt/clib/session.py | 75 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index ba6f87763d9..f9a720eb735 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -8,7 +8,6 @@ import contextlib import ctypes as ctp import io -import pathlib import sys import warnings from collections.abc import Callable, Generator, Sequence @@ -1792,43 +1791,43 @@ def virtualfile_in( # noqa: PLR0912 "vectors": self.virtualfile_from_vectors, }[kind] - # Ensure the data is an iterable (Python list or tuple) - if kind in {"geojson", "grid", "image", "file", "arg", "stringio"}: - if kind == "image" and data.dtype != "uint8": - msg = ( - f"Input image has dtype: {data.dtype} which is unsupported, " - "and may result in an incorrect output. Please recast image " - "to a uint8 dtype and/or scale to 0-255 range, e.g. " - "using a histogram equalization function like " - "skimage.exposure.equalize_hist." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - _data = (data,) if not isinstance(data, pathlib.PurePath) else (str(data),) - elif kind == "empty": - # data is None, so data must be given via x/y/z. - _data = [x, y] - if z is not None: - _data.append(z) - if extra_arrays: - _data.extend(extra_arrays) - elif kind == "vectors": - if hasattr(data, "items") and not hasattr(data, "to_frame"): - # pandas.DataFrame or xarray.Dataset types. - # pandas.Series will be handled below like a 1-D numpy.ndarray. - _data = [array for _, array in data.items()] - else: - # Python list, tuple, numpy.ndarray, and pandas.Series types - _data = np.atleast_2d(np.asanyarray(data).T) - elif kind == "matrix": - # GMT can only accept a 2-D matrix which are signed integer (i), unsigned - # integer (u) or floating point (f) types. For other data types, we need to - # use virtualfile_from_vectors instead, which turns the matrix into a list - # of vectors and allows for better handling of non-integer/float type inputs - # (e.g. for string or datetime data types). - _data = (data,) - if data.dtype.kind not in "iuf": - _virtualfile_from = self.virtualfile_from_vectors - _data = data.T + # Ensure the data is an iterable (Python list or tuple). + match kind: + case "arg" | "file" | "geojson" | "grid" | "image" | "stringio": + _data = (data,) + if kind == "image" and data.dtype != "uint8": + msg = ( + f"Input image has dtype: {data.dtype} which is unsupported, " + "and may result in an incorrect output. Please recast image " + "to a uint8 dtype and/or scale to 0-255 range, e.g. " + "using a histogram equalization function like " + "skimage.exposure.equalize_hist." + ) + warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) + case "empty": # data is None, so data must be given via x/y/z. + _data = [x, y] + if z is not None: + _data.append(z) + if extra_arrays: + _data.extend(extra_arrays) + case "vectors": + if hasattr(data, "items") and not hasattr(data, "to_frame"): + # pandas.DataFrame or xarray.Dataset types. + # pandas.Series will be handled below like a 1-D numpy.ndarray. + _data = [array for _, array in data.items()] + else: + # Python list, tuple, numpy.ndarray, and pandas.Series types + _data = np.atleast_2d(np.asanyarray(data).T) + case "matrix": + # GMT can only accept a 2-D matrix which are signed integer (i), + # unsigned integer (u) or floating point (f) types. For other data + # types, we need to use virtualfile_from_vectors instead, which turns + # the matrix into a list of vectors and allows for better handling of + # non-integer/float type inputs (e.g. for string or datetime data types) + _data = (data,) + if data.dtype.kind not in "iuf": + _virtualfile_from = self.virtualfile_from_vectors + _data = data.T # Finally create the virtualfile from the data, to be passed into GMT file_context = _virtualfile_from(*_data)