Skip to content

Session.virtualfile_in: Deprecate parameter 'required_z'. Use 'mincols' instead (will be removed in v0.20.0) #3369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5d3d308
Rename 'required_z' to 'required_ncols' in _validate_data_input
seisman Mar 5, 2025
406cb39
Rename 'required_z' to 'required_ncols' in Session.virtualfile_in
seisman Mar 5, 2025
af189e6
Rename 'required_z' to 'required_ncols' in Session.virtualfile_in tests
seisman Mar 5, 2025
2d55e5a
Rename 'required_z' to 'required_ncols' in module wrappers
seisman Mar 5, 2025
84e8d8a
Merge branch 'main' into refactor/virtualfile_in
seisman Mar 10, 2025
5eeb37b
Rename required_ncols to the shorter ncols
seisman Mar 10, 2025
2851af8
Merge branch 'main' into refactor/virtualfile_in
seisman Mar 26, 2025
0e0c1d8
Deprecate required_z in a backward-compatible way
seisman Mar 26, 2025
f8293cd
Add one test with the deprecated 'required_z' parameter to increase c…
seisman Mar 26, 2025
312aa39
Merge branch 'main' into refactor/virtualfile_in
seisman Mar 28, 2025
b7ca239
Move TODO comment to the top
seisman Mar 28, 2025
a01afbb
Merge branch 'main' into refactor/virtualfile_in
seisman Apr 15, 2025
da874d1
Put ncols before required_data
seisman Apr 15, 2025
0612cdf
Merge branch 'main' into refactor/virtualfile_in
seisman Apr 15, 2025
4fba462
Merge branch 'main' into refactor/virtualfile_in
seisman Apr 17, 2025
ef1f82a
Merge branch 'main' into refactor/virtualfile_in
seisman Apr 22, 2025
4fae564
Merge branch 'main' into refactor/virtualfile_in
seisman Apr 26, 2025
970b824
Update pygmt/clib/session.py
seisman Apr 28, 2025
c5eb0e9
Merge branch 'main' into refactor/virtualfile_in
seisman Apr 28, 2025
41ff984
Rename ncols to mincols
seisman Apr 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,16 +1748,18 @@ def virtualfile_from_stringio(
seg.header = None
seg.text = None

# TODO(PyGMT>=0.20.0): Remove the deprecated parameter 'required_z'.
# TODO(PyGMT>=0.20.0): Remove the deprecated parameter 'extra_arrays'.
def virtualfile_in(
def virtualfile_in( # noqa: PLR0912
self,
check_kind=None,
data=None,
x=None,
y=None,
z=None,
required_z=False,
mincols=2,
required_data=True,
required_z=False,
extra_arrays=None,
):
"""
Expand All @@ -1778,11 +1780,19 @@ def virtualfile_in(
data input.
x/y/z : 1-D arrays or None
x, y, and z columns as numpy arrays.
required_z : bool
State whether the 'z' column is required.
mincols
Number of minimum required columns. Default is 2 (i.e. require x and y
columns).
required_data : bool
Set to True when 'data' is required, or False when dealing with
optional virtual files. [Default is True].
required_z : bool
State whether the 'z' column is required.

.. deprecated:: v0.16.0
The parameter 'required_z' will be removed in v0.20.0. Use parameter
'mincols' instead. E.g., ``required_z=True`` is equivalent to
``mincols=3``.
extra_arrays : list of 1-D arrays
A list of numpy arrays in addition to x, y, and z. All of these arrays must
be of the same size as the x/y/z arrays.
Expand Down Expand Up @@ -1818,13 +1828,23 @@ def virtualfile_in(
... print(fout.read().strip())
<vector memory>: N = 3 <7/9> <4/6> <1/3>
"""
if required_z is True:
warnings.warn(
"The parameter 'required_z' is deprecated in v0.16.0 and will be "
"removed in v0.20.0. Use parameter 'mincols' instead. E.g., "
"``required_z=True`` is equivalent to ``mincols=3``.",
category=FutureWarning,
stacklevel=1,
)
mincols = 3

kind = data_kind(data, required=required_data)
_validate_data_input(
data=data,
x=x,
y=y,
z=z,
required_z=required_z,
mincols=mincols,
required_data=required_data,
kind=kind,
)
Expand Down
11 changes: 6 additions & 5 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


def _validate_data_input( # noqa: PLR0912
data=None, x=None, y=None, z=None, required_z=False, required_data=True, kind=None
data=None, x=None, y=None, z=None, mincols=2, required_data=True, kind=None
) -> None:
"""
Check if the combination of data/x/y/z is valid.
Expand All @@ -66,29 +66,29 @@ def _validate_data_input( # noqa: PLR0912
Traceback (most recent call last):
...
pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], required_z=True)
>>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], mincols=3)
Traceback (most recent call last):
...
pygmt.exceptions.GMTInvalidInput: Must provide x, y, and z.
>>> import numpy as np
>>> import pandas as pd
>>> import xarray as xr
>>> data = np.arange(8).reshape((4, 2))
>>> _validate_data_input(data=data, required_z=True, kind="matrix")
>>> _validate_data_input(data=data, mincols=3, kind="matrix")
Traceback (most recent call last):
...
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
>>> _validate_data_input(
... data=pd.DataFrame(data, columns=["x", "y"]),
... required_z=True,
... mincols=3,
... kind="vectors",
... )
Traceback (most recent call last):
...
pygmt.exceptions.GMTInvalidInput: data must provide x, y, and z columns.
>>> _validate_data_input(
... data=xr.Dataset(pd.DataFrame(data, columns=["x", "y"])),
... required_z=True,
... mincols=3,
... kind="vectors",
... )
Traceback (most recent call last):
Expand Down Expand Up @@ -116,6 +116,7 @@ def _validate_data_input( # noqa: PLR0912
GMTInvalidInput
If the data input is not valid.
"""
required_z = mincols >= 3
if data is None: # data is None
if x is None and y is None: # both x and y are None
if required_data: # data is not optional
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _blockm(
with Session() as lib:
with (
lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
) as vintbl,
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,
):
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def contour(

with Session() as lib:
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
) as vintbl:
lib.call_module(
module="contour", args=build_arg_list(kwargs, infile=vintbl)
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/nearneighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def nearneighbor(
with Session() as lib:
with (
lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down
4 changes: 1 addition & 3 deletions pygmt/src/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,5 @@ def plot3d( # noqa: PLR0912
kwargs["S"] = "u0.2c"

with Session() as lib:
with lib.virtualfile_in(
check_kind="vector", data=data, required_z=True
) as vintbl:
with lib.virtualfile_in(check_kind="vector", data=data, mincols=3) as vintbl:
lib.call_module(module="plot3d", args=build_arg_list(kwargs, infile=vintbl))
2 changes: 1 addition & 1 deletion pygmt/src/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def project(
x=x,
y=y,
z=z,
required_z=False,
mincols=2,
required_data=False,
) as vintbl,
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def surface(
with Session() as lib:
with (
lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down
4 changes: 2 additions & 2 deletions pygmt/src/triangulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def regular_grid(
with Session() as lib:
with (
lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=False
check_kind="vector", data=data, x=x, y=y, z=z, mincols=2
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down Expand Up @@ -244,7 +244,7 @@ def delaunay_triples(
with Session() as lib:
with (
lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=False
check_kind="vector", data=data, x=x, y=y, z=z, mincols=2
) as vintbl,
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,
):
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/wiggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ def wiggle(

with Session() as lib:
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
) as vintbl:
lib.call_module(module="wiggle", args=build_arg_list(kwargs, infile=vintbl))
2 changes: 1 addition & 1 deletion pygmt/src/xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def xyz2grd(
with Session() as lib:
with (
lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
check_kind="vector", data=data, x=x, y=y, z=z, mincols=3
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down
24 changes: 19 additions & 5 deletions pygmt/tests/test_clib_virtualfile_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def test_virtualfile_in_required_z_matrix(array_func, kind):
)
data = array_func(dataframe)
with clib.Session() as lib:
with lib.virtualfile_in(
data=data, required_z=True, check_kind="vector"
) as vfile:
with lib.virtualfile_in(data=data, mincols=3, check_kind="vector") as vfile:
with GMTTempFile() as outfile:
lib.call_module("info", [vfile, f"->{outfile.name}"])
output = outfile.read(keep_tabs=True)
Expand All @@ -64,10 +62,26 @@ def test_virtualfile_in_required_z_matrix_missing():
data = np.ones((5, 2))
with clib.Session() as lib:
with pytest.raises(GMTInvalidInput):
with lib.virtualfile_in(data=data, required_z=True, check_kind="vector"):
with lib.virtualfile_in(data=data, mincols=3, check_kind="vector"):
pass


# TODO(PyGMT>=0.20.0): Remove this test for the deprecated 'required_z' parameter.
def test_virtualfile_in_required_z_deprecated():
"""
Same as test_virtualfile_in_required_z_matrix_missing but using the deprecated
'required_z' parameter.
"""
data = np.ones((5, 2))
with clib.Session() as lib:
with pytest.raises(GMTInvalidInput): # noqa: PT012
with pytest.warns(FutureWarning):
with lib.virtualfile_in(
data=data, required_z=True, check_kind="vector"
):
pass


def test_virtualfile_in_fail_non_valid_data(data):
"""
Should raise an exception if too few or too much data is given.
Expand All @@ -91,7 +105,7 @@ def test_virtualfile_in_fail_non_valid_data(data):
with clib.Session() as lib:
with pytest.raises(GMTInvalidInput):
lib.virtualfile_in(
x=variable[0], y=variable[1], z=variable[2], required_z=True
x=variable[0], y=variable[1], z=variable[2], mincols=3
)

# Should also fail if given too much data
Expand Down
Loading