Skip to content

Commit a73a4bc

Browse files
committed
**Breaking**: clib.Session.virtualfile_from_vectors now takes only one argument
1 parent 9c47b38 commit a73a4bc

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

pygmt/clib/session.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ def open_virtual_file(self, family, geometry, direction, data):
13301330
return self.open_virtualfile(family, geometry, direction, data)
13311331

13321332
@contextlib.contextmanager
1333-
def virtualfile_from_vectors(self, *vectors):
1333+
def virtualfile_from_vectors(self, vectors):
13341334
"""
13351335
Store 1-D arrays as columns of a table inside a virtual file.
13361336
@@ -1372,7 +1372,7 @@ def virtualfile_from_vectors(self, *vectors):
13721372
>>> y = np.array([4, 5, 6])
13731373
>>> z = pd.Series([7, 8, 9])
13741374
>>> with Session() as ses:
1375-
... with ses.virtualfile_from_vectors(x, y, z) as fin:
1375+
... with ses.virtualfile_from_vectors((x, y, z)) as fin:
13761376
... # Send the output to a file so that we can read it
13771377
... with GMTTempFile() as fout:
13781378
... ses.call_module("info", [fin, f"->{fout.name}"])
@@ -1791,19 +1791,19 @@ def virtualfile_in( # noqa: PLR0912
17911791
"vectors": self.virtualfile_from_vectors,
17921792
}[kind]
17931793

1794-
# Ensure the data is an iterable (Python list or tuple).
1794+
# "_data" is the data that will be passed to the _virtualfile_from function.
1795+
# "_data" defaults to "data" but should be adjusted for some cases.
1796+
_data = data
17951797
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)
1798+
case "image" if 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)
18071807
case "empty": # data is None, so data must be given via x/y/z.
18081808
_data = [x, y]
18091809
if z is not None:
@@ -1818,19 +1818,19 @@ def virtualfile_in( # noqa: PLR0912
18181818
else:
18191819
# Python list, tuple, numpy.ndarray, and pandas.Series types
18201820
_data = np.atleast_2d(np.asanyarray(data).T)
1821-
case "matrix":
1821+
case "matrix" if data.dtype.kind not in "iuf":
18221822
# GMT can only accept a 2-D matrix which are signed integer (i),
18231823
# unsigned integer (u) or floating point (f) types. For other data
18241824
# types, we need to use virtualfile_from_vectors instead, which turns
18251825
# the matrix into a list of vectors and allows for better handling of
18261826
# 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
1827+
_virtualfile_from = self.virtualfile_from_vectors
1828+
_data = data.T
1829+
case _:
1830+
pass
18311831

18321832
# Finally create the virtualfile from the data, to be passed into GMT
1833-
file_context = _virtualfile_from(*_data)
1833+
file_context = _virtualfile_from(_data)
18341834
return file_context
18351835

18361836
def virtualfile_from_data(

pygmt/tests/test_clib_virtualfile_from_vectors.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_virtualfile_from_vectors(dtypes):
4444
y = np.arange(size, size * 2, 1, dtype=dtype)
4545
z = np.arange(size * 2, size * 3, 1, dtype=dtype)
4646
with clib.Session() as lib:
47-
with lib.virtualfile_from_vectors(x, y, z) as vfile:
47+
with lib.virtualfile_from_vectors((x, y, z)) as vfile:
4848
with GMTTempFile() as outfile:
4949
lib.call_module("info", [vfile, f"->{outfile.name}"])
5050
output = outfile.read(keep_tabs=True)
@@ -64,7 +64,7 @@ def test_virtualfile_from_vectors_one_string_or_object_column(dtype):
6464
y = np.arange(size, size * 2, 1, dtype=np.int32)
6565
strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=dtype)
6666
with clib.Session() as lib:
67-
with lib.virtualfile_from_vectors(x, y, strings) as vfile:
67+
with lib.virtualfile_from_vectors((x, y, strings)) as vfile:
6868
with GMTTempFile() as outfile:
6969
lib.call_module("convert", [vfile, f"->{outfile.name}"])
7070
output = outfile.read(keep_tabs=True)
@@ -86,7 +86,7 @@ def test_virtualfile_from_vectors_two_string_or_object_columns(dtype):
8686
strings1 = np.array(["a", "bc", "def", "ghij", "klmnolooong"], dtype=dtype)
8787
strings2 = np.array(["pqrst", "uvwx", "yz!", "@#", "$"], dtype=dtype)
8888
with clib.Session() as lib:
89-
with lib.virtualfile_from_vectors(x, y, strings1, strings2) as vfile:
89+
with lib.virtualfile_from_vectors((x, y, strings1, strings2)) as vfile:
9090
with GMTTempFile() as outfile:
9191
lib.call_module("convert", [vfile, f"->{outfile.name}"])
9292
output = outfile.read(keep_tabs=True)
@@ -105,7 +105,7 @@ def test_virtualfile_from_vectors_transpose(dtypes):
105105
for dtype in dtypes:
106106
data = np.arange(shape[0] * shape[1], dtype=dtype).reshape(shape)
107107
with clib.Session() as lib:
108-
with lib.virtualfile_from_vectors(*data.T) as vfile:
108+
with lib.virtualfile_from_vectors(data.T) as vfile:
109109
with GMTTempFile() as outfile:
110110
lib.call_module("info", [vfile, "-C", f"->{outfile.name}"])
111111
output = outfile.read(keep_tabs=True)
@@ -122,7 +122,7 @@ def test_virtualfile_from_vectors_diff_size():
122122
y = np.arange(6)
123123
with clib.Session() as lib:
124124
with pytest.raises(GMTInvalidInput):
125-
with lib.virtualfile_from_vectors(x, y):
125+
with lib.virtualfile_from_vectors((x, y)):
126126
pass
127127

128128

@@ -143,7 +143,7 @@ def test_virtualfile_from_vectors_pandas(dtypes_pandas):
143143
dtype=dtype,
144144
)
145145
with clib.Session() as lib:
146-
with lib.virtualfile_from_vectors(data.x, data.y, data.z) as vfile:
146+
with lib.virtualfile_from_vectors((data.x, data.y, data.z)) as vfile:
147147
with GMTTempFile() as outfile:
148148
lib.call_module("info", [vfile, f"->{outfile.name}"])
149149
output = outfile.read(keep_tabs=True)
@@ -163,7 +163,7 @@ def test_virtualfile_from_vectors_arraylike():
163163
y = tuple(range(size, size * 2, 1))
164164
z = range(size * 2, size * 3, 1)
165165
with clib.Session() as lib:
166-
with lib.virtualfile_from_vectors(x, y, z) as vfile:
166+
with lib.virtualfile_from_vectors((x, y, z)) as vfile:
167167
with GMTTempFile() as outfile:
168168
lib.call_module("info", [vfile, f"->{outfile.name}"])
169169
output = outfile.read(keep_tabs=True)

0 commit comments

Comments
 (0)