Skip to content
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

Session.virtualfile_in: Remove parameter 'required_z' and add 'ncols' #3369

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,8 +1773,8 @@ def virtualfile_in(
y=None,
z=None,
extra_arrays=None,
required_z=False,
required_data=True,
required_ncols=2,
):
"""
Store any data inside a virtual file.
Expand All @@ -1797,11 +1797,11 @@ def virtualfile_in(
extra_arrays : list of 1-D arrays
Optional. 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.
required_z : bool
State whether the 'z' column is required.
required_data : bool
Set to True when 'data' is required, or False when dealing with
optional virtual files. [Default is True].
required_ncols
Number of minimum required columns.

Returns
-------
Expand Down Expand Up @@ -1835,8 +1835,8 @@ def virtualfile_in(
x=x,
y=y,
z=z,
required_z=required_z,
required_data=required_data,
required_ncols=required_ncols,
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 @@ -42,7 +42,7 @@


def _validate_data_input(
data=None, x=None, y=None, z=None, required_z=False, required_data=True, kind=None
data=None, x=None, y=None, z=None, required_data=True, required_ncols=2, kind=None
) -> None:
"""
Check if the combination of data/x/y/z is valid.
Expand All @@ -65,29 +65,29 @@ def _validate_data_input(
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], required_ncols=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, required_ncols=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,
... required_ncols=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,
... required_ncols=3,
... kind="vectors",
... )
Traceback (most recent call last):
Expand Down Expand Up @@ -115,6 +115,7 @@ def _validate_data_input(
GMTInvalidInput
If the data input is not valid.
"""
required_z = required_ncols >= 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 @@ -55,7 +55,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, required_ncols=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 @@ -145,7 +145,7 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):

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, required_ncols=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 @@ -141,7 +141,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, required_ncols=3
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,6 @@ def plot3d(
y=y,
z=z,
extra_arrays=extra_arrays,
required_z=True,
required_ncols=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 @@ -245,7 +245,7 @@ def project(
x=x,
y=y,
z=z,
required_z=False,
required_ncols=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 @@ -155,7 +155,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, required_ncols=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 @@ -138,7 +138,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, required_ncols=2
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down Expand Up @@ -238,7 +238,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, required_ncols=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 @@ -108,6 +108,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, required_ncols=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 @@ -149,7 +149,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, required_ncols=3
) as vintbl,
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
):
Expand Down
6 changes: 3 additions & 3 deletions pygmt/tests/test_clib_virtualfile_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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"
data=data, required_ncols=3, check_kind="vector"
) as vfile:
with GMTTempFile() as outfile:
lib.call_module("info", [vfile, f"->{outfile.name}"])
Expand All @@ -64,7 +64,7 @@ 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, required_ncols=3, check_kind="vector"):
pass


Expand All @@ -91,7 +91,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], required_ncols=3
)

# Should also fail if given too much data
Expand Down