Skip to content

Commit f389e05

Browse files
seismanweiji14
andauthored
Session.virtualfile_in: Deprecate parameter 'required_z'. Use 'mincols' instead (will be removed in v0.20.0) (#3369)
Co-authored-by: Wei Ji <[email protected]>
1 parent 4bbc37e commit f389e05

12 files changed

+60
-27
lines changed

pygmt/clib/session.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,16 +1748,18 @@ def virtualfile_from_stringio(
17481748
seg.header = None
17491749
seg.text = None
17501750

1751+
# TODO(PyGMT>=0.20.0): Remove the deprecated parameter 'required_z'.
17511752
# TODO(PyGMT>=0.20.0): Remove the deprecated parameter 'extra_arrays'.
1752-
def virtualfile_in(
1753+
def virtualfile_in( # noqa: PLR0912
17531754
self,
17541755
check_kind=None,
17551756
data=None,
17561757
x=None,
17571758
y=None,
17581759
z=None,
1759-
required_z=False,
1760+
mincols=2,
17601761
required_data=True,
1762+
required_z=False,
17611763
extra_arrays=None,
17621764
):
17631765
"""
@@ -1778,11 +1780,19 @@ def virtualfile_in(
17781780
data input.
17791781
x/y/z : 1-D arrays or None
17801782
x, y, and z columns as numpy arrays.
1781-
required_z : bool
1782-
State whether the 'z' column is required.
1783+
mincols
1784+
Number of minimum required columns. Default is 2 (i.e. require x and y
1785+
columns).
17831786
required_data : bool
17841787
Set to True when 'data' is required, or False when dealing with
17851788
optional virtual files. [Default is True].
1789+
required_z : bool
1790+
State whether the 'z' column is required.
1791+
1792+
.. deprecated:: v0.16.0
1793+
The parameter 'required_z' will be removed in v0.20.0. Use parameter
1794+
'mincols' instead. E.g., ``required_z=True`` is equivalent to
1795+
``mincols=3``.
17861796
extra_arrays : list of 1-D arrays
17871797
A list of numpy arrays in addition to x, y, and z. All of these arrays must
17881798
be of the same size as the x/y/z arrays.
@@ -1818,13 +1828,23 @@ def virtualfile_in(
18181828
... print(fout.read().strip())
18191829
<vector memory>: N = 3 <7/9> <4/6> <1/3>
18201830
"""
1831+
if required_z is True:
1832+
warnings.warn(
1833+
"The parameter 'required_z' is deprecated in v0.16.0 and will be "
1834+
"removed in v0.20.0. Use parameter 'mincols' instead. E.g., "
1835+
"``required_z=True`` is equivalent to ``mincols=3``.",
1836+
category=FutureWarning,
1837+
stacklevel=1,
1838+
)
1839+
mincols = 3
1840+
18211841
kind = data_kind(data, required=required_data)
18221842
_validate_data_input(
18231843
data=data,
18241844
x=x,
18251845
y=y,
18261846
z=z,
1827-
required_z=required_z,
1847+
mincols=mincols,
18281848
required_data=required_data,
18291849
kind=kind,
18301850
)

pygmt/helpers/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444

4545
def _validate_data_input( # noqa: PLR0912
46-
data=None, x=None, y=None, z=None, required_z=False, required_data=True, kind=None
46+
data=None, x=None, y=None, z=None, mincols=2, required_data=True, kind=None
4747
) -> None:
4848
"""
4949
Check if the combination of data/x/y/z is valid.
@@ -66,29 +66,29 @@ def _validate_data_input( # noqa: PLR0912
6666
Traceback (most recent call last):
6767
...
6868
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
69-
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], required_z=True)
69+
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], mincols=3)
7070
Traceback (most recent call last):
7171
...
7272
pygmt.exceptions.GMTInvalidInput: Must provide x, y, and z.
7373
>>> import numpy as np
7474
>>> import pandas as pd
7575
>>> import xarray as xr
7676
>>> data = np.arange(8).reshape((4, 2))
77-
>>> _validate_data_input(data=data, required_z=True, kind="matrix")
77+
>>> _validate_data_input(data=data, mincols=3, kind="matrix")
7878
Traceback (most recent call last):
7979
...
8080
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
8181
>>> _validate_data_input(
8282
... data=pd.DataFrame(data, columns=["x", "y"]),
83-
... required_z=True,
83+
... mincols=3,
8484
... kind="vectors",
8585
... )
8686
Traceback (most recent call last):
8787
...
8888
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
8989
>>> _validate_data_input(
9090
... data=xr.Dataset(pd.DataFrame(data, columns=["x", "y"])),
91-
... required_z=True,
91+
... mincols=3,
9292
... kind="vectors",
9393
... )
9494
Traceback (most recent call last):
@@ -116,6 +116,7 @@ def _validate_data_input( # noqa: PLR0912
116116
GMTInvalidInput
117117
If the data input is not valid.
118118
"""
119+
required_z = mincols >= 3
119120
if data is None: # data is None
120121
if x is None and y is None: # both x and y are None
121122
if required_data: # data is not optional

pygmt/src/blockm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _blockm(
5656
with Session() as lib:
5757
with (
5858
lib.virtualfile_in(
59-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
59+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
6060
) as vintbl,
6161
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,
6262
):

pygmt/src/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def contour(
148148

149149
with Session() as lib:
150150
with lib.virtualfile_in(
151-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
151+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
152152
) as vintbl:
153153
lib.call_module(
154154
module="contour", args=build_arg_list(kwargs, infile=vintbl)

pygmt/src/nearneighbor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def nearneighbor(
147147
with Session() as lib:
148148
with (
149149
lib.virtualfile_in(
150-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
150+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
151151
) as vintbl,
152152
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
153153
):

pygmt/src/plot3d.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,5 @@ def plot3d( # noqa: PLR0912
259259
kwargs["S"] = "u0.2c"
260260

261261
with Session() as lib:
262-
with lib.virtualfile_in(
263-
check_kind="vector", data=data, required_z=True
264-
) as vintbl:
262+
with lib.virtualfile_in(check_kind="vector", data=data, mincols=3) as vintbl:
265263
lib.call_module(module="plot3d", args=build_arg_list(kwargs, infile=vintbl))

pygmt/src/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def project(
246246
x=x,
247247
y=y,
248248
z=z,
249-
required_z=False,
249+
mincols=2,
250250
required_data=False,
251251
) as vintbl,
252252
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,

pygmt/src/surface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def surface(
161161
with Session() as lib:
162162
with (
163163
lib.virtualfile_in(
164-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
164+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
165165
) as vintbl,
166166
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
167167
):

pygmt/src/triangulate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def regular_grid(
144144
with Session() as lib:
145145
with (
146146
lib.virtualfile_in(
147-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=False
147+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=2
148148
) as vintbl,
149149
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
150150
):
@@ -244,7 +244,7 @@ def delaunay_triples(
244244
with Session() as lib:
245245
with (
246246
lib.virtualfile_in(
247-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=False
247+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=2
248248
) as vintbl,
249249
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,
250250
):

pygmt/src/wiggle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ def wiggle(
109109

110110
with Session() as lib:
111111
with lib.virtualfile_in(
112-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
112+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
113113
) as vintbl:
114114
lib.call_module(module="wiggle", args=build_arg_list(kwargs, infile=vintbl))

pygmt/src/xyz2grd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def xyz2grd(
155155
with Session() as lib:
156156
with (
157157
lib.virtualfile_in(
158-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
158+
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
159159
) as vintbl,
160160
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
161161
):

pygmt/tests/test_clib_virtualfile_in.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def test_virtualfile_in_required_z_matrix(array_func, kind):
4141
)
4242
data = array_func(dataframe)
4343
with clib.Session() as lib:
44-
with lib.virtualfile_in(
45-
data=data, required_z=True, check_kind="vector"
46-
) as vfile:
44+
with lib.virtualfile_in(data=data, mincols=3, check_kind="vector") as vfile:
4745
with GMTTempFile() as outfile:
4846
lib.call_module("info", [vfile, f"->{outfile.name}"])
4947
output = outfile.read(keep_tabs=True)
@@ -64,10 +62,26 @@ def test_virtualfile_in_required_z_matrix_missing():
6462
data = np.ones((5, 2))
6563
with clib.Session() as lib:
6664
with pytest.raises(GMTInvalidInput):
67-
with lib.virtualfile_in(data=data, required_z=True, check_kind="vector"):
65+
with lib.virtualfile_in(data=data, mincols=3, check_kind="vector"):
6866
pass
6967

7068

69+
# TODO(PyGMT>=0.20.0): Remove this test for the deprecated 'required_z' parameter.
70+
def test_virtualfile_in_required_z_deprecated():
71+
"""
72+
Same as test_virtualfile_in_required_z_matrix_missing but using the deprecated
73+
'required_z' parameter.
74+
"""
75+
data = np.ones((5, 2))
76+
with clib.Session() as lib:
77+
with pytest.raises(GMTInvalidInput): # noqa: PT012
78+
with pytest.warns(FutureWarning):
79+
with lib.virtualfile_in(
80+
data=data, required_z=True, check_kind="vector"
81+
):
82+
pass
83+
84+
7185
def test_virtualfile_in_fail_non_valid_data(data):
7286
"""
7387
Should raise an exception if too few or too much data is given.
@@ -91,7 +105,7 @@ def test_virtualfile_in_fail_non_valid_data(data):
91105
with clib.Session() as lib:
92106
with pytest.raises(GMTInvalidInput):
93107
lib.virtualfile_in(
94-
x=variable[0], y=variable[1], z=variable[2], required_z=True
108+
x=variable[0], y=variable[1], z=variable[2], mincols=3
95109
)
96110

97111
# Should also fail if given too much data

0 commit comments

Comments
 (0)