|
7 | 7 |
|
8 | 8 | import contextlib |
9 | 9 | import ctypes as ctp |
10 | | -import pathlib |
11 | 10 | import sys |
12 | 11 | import warnings |
13 | 12 | from collections.abc import Generator, Sequence |
@@ -1720,44 +1719,47 @@ def virtualfile_in( # noqa: PLR0912 |
1720 | 1719 | }[kind] |
1721 | 1720 |
|
1722 | 1721 | # 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" | "geojson" | "grid": |
| 1724 | + _data = (data,) |
| 1725 | + case "file": |
| 1726 | + _data = (str(data),) |
| 1727 | + case "image": |
| 1728 | + if data.dtype != "uint8": |
| 1729 | + msg = ( |
| 1730 | + f"Input image has dtype: {data.dtype} which is unsupported, " |
| 1731 | + "and may result in an incorrect output. Please recast image " |
| 1732 | + "to a uint8 dtype and/or scale to 0-255 range, e.g. " |
| 1733 | + "using a histogram equalization function like " |
| 1734 | + "skimage.exposure.equalize_hist." |
| 1735 | + ) |
| 1736 | + warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) |
| 1737 | + _data = (data,) |
| 1738 | + case "matrix": # 2-D numpy.ndarray |
1750 | 1739 | # virtualfile_from_matrix only support 2-D numpy.ndarray which are |
1751 | 1740 | # 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) |
| 1741 | + if data.dtype.kind in "iuf": |
| 1742 | + _data = (data,) |
| 1743 | + else: # turn 2-D numpy.ndarray into list of arrays |
| 1744 | + _virtualfile_from = self.virtualfile_from_vectors |
| 1745 | + _data = list(data.T) |
| 1746 | + case "none": # data is given via a series of vectors |
| 1747 | + _data = [np.atleast_1d(x), np.atleast_1d(y)] |
| 1748 | + if z is not None: |
| 1749 | + _data.append(np.atleast_1d(z)) |
| 1750 | + if extra_arrays: |
| 1751 | + _data.extend(extra_arrays) |
| 1752 | + case "vectors": |
| 1753 | + if hasattr(data, "items") and not hasattr(data, "to_frame"): |
| 1754 | + # pandas.DataFrame or xarray.Dataset types. |
| 1755 | + # pandas.Series will be handled below like a 1-D numpy.ndarray. |
| 1756 | + _data = [array for _, array in data.items()] |
| 1757 | + else: |
| 1758 | + # Python list, tuple, and pandas.Series types |
| 1759 | + _data = np.atleast_2d(np.asanyarray(data).T) |
1756 | 1760 |
|
1757 | 1761 | # Finally create the virtualfile from the data, to be passed into GMT |
1758 | | - file_context = _virtualfile_from(*_data) |
1759 | | - |
1760 | | - return file_context |
| 1762 | + return _virtualfile_from(*_data) |
1761 | 1763 |
|
1762 | 1764 | def virtualfile_from_data( |
1763 | 1765 | self, |
|
0 commit comments