Skip to content

clib: Change the parameter order and set output_type to pandas in virtualfile_to_dataset #3124

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 3 commits into from
Mar 20, 2024
Merged
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
16 changes: 8 additions & 8 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1740,8 +1740,8 @@ def read_virtualfile(

def virtualfile_to_dataset(
self,
output_type: Literal["pandas", "numpy", "file"],
vfname: str,
output_type: Literal["pandas", "numpy", "file"] = "pandas",
column_names: list[str] | None = None,
) -> pd.DataFrame | np.ndarray | None:
"""
Expand All @@ -1751,15 +1751,15 @@ def virtualfile_to_dataset(

Parameters
----------
vfname
The virtual file name that stores the result data. Required for ``"pandas"``
and ``"numpy"`` output type.
output_type
Desired output type of the result data.

- ``"pandas"`` will return a :class:`pandas.DataFrame` object.
- ``"numpy"`` will return a :class:`numpy.ndarray` object.
- ``"file"`` means the result was saved to a file and will return ``None``.
vfname
The virtual file name that stores the result data. Required for ``"pandas"``
and ``"numpy"`` output type.
column_names
The column names for the :class:`pandas.DataFrame` output.
Comment on lines 1763 to 1764
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the proof of concept at #3117 works, these lines will need to be updated (after we add some logic to use the header column names in this virtualfile_to_dataset method)


Expand Down Expand Up @@ -1795,7 +1795,7 @@ def virtualfile_to_dataset(
... ) as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... result = lib.virtualfile_to_dataset(
... output_type="file", vfname=vouttbl
... vfname=vouttbl, output_type="file"
... )
... assert result is None
... assert Path(outtmp.name).stat().st_size > 0
Expand All @@ -1805,7 +1805,7 @@ def virtualfile_to_dataset(
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... outnp = lib.virtualfile_to_dataset(
... output_type="numpy", vfname=vouttbl
... vfname=vouttbl, output_type="numpy"
... )
... assert isinstance(outnp, np.ndarray)
...
Expand All @@ -1814,7 +1814,7 @@ def virtualfile_to_dataset(
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... outpd = lib.virtualfile_to_dataset(
... output_type="pandas", vfname=vouttbl
... vfname=vouttbl, output_type="pandas"
... )
... assert isinstance(outpd, pd.DataFrame)
...
Expand All @@ -1823,8 +1823,8 @@ def virtualfile_to_dataset(
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... outpd2 = lib.virtualfile_to_dataset(
... output_type="pandas",
... vfname=vouttbl,
... output_type="pandas",
... column_names=["col1", "col2", "col3", "coltext"],
... )
... assert isinstance(outpd2, pd.DataFrame)
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _blockm(
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
output_type=output_type, vfname=vouttbl, column_names=column_names
vfname=vouttbl, output_type=output_type, column_names=column_names
)


Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/filter1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ def filter1d(
module="filter1d",
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(output_type=output_type, vfname=vouttbl)
return lib.virtualfile_to_dataset(vfname=vouttbl, output_type=output_type)
2 changes: 1 addition & 1 deletion pygmt/src/grd2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,5 @@ def grd2xyz(
args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
output_type=output_type, vfname=vouttbl, column_names=column_names
vfname=vouttbl, output_type=output_type, column_names=column_names
)
2 changes: 1 addition & 1 deletion pygmt/src/grdhisteq.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ def compute_bins(
)

result = lib.virtualfile_to_dataset(
output_type=output_type,
vfname=vouttbl,
output_type=output_type,
column_names=["start", "stop", "bin_id"],
)
if output_type == "pandas":
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def grdtrack(
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
output_type=output_type,
vfname=vouttbl,
output_type=output_type,
column_names=column_names,
)
2 changes: 1 addition & 1 deletion pygmt/src/grdvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ def grdvolume(
module="grdvolume",
args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(output_type=output_type, vfname=vouttbl)
return lib.virtualfile_to_dataset(vfname=vouttbl, output_type=output_type)
2 changes: 1 addition & 1 deletion pygmt/src/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def project(
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
output_type=output_type,
vfname=vouttbl,
output_type=output_type,
column_names=column_names,
)
2 changes: 1 addition & 1 deletion pygmt/src/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def select(
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
output_type=output_type,
vfname=vouttbl,
output_type=output_type,
column_names=column_names,
)
2 changes: 1 addition & 1 deletion pygmt/src/triangulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,4 @@ def delaunay_triples(
module="triangulate",
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(output_type=output_type, vfname=vouttbl)
return lib.virtualfile_to_dataset(vfname=vouttbl, output_type=output_type)