From 8ee9cef0ec6689b3dd25942877fc742b502289a5 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 19 Mar 2024 23:07:33 +0800 Subject: [PATCH 1/2] clib: Change the parameter order and set output_type to pands in virtualfile_to_dataset --- pygmt/clib/session.py | 18 ++++++++++-------- pygmt/src/blockm.py | 2 +- pygmt/src/filter1d.py | 5 ++++- pygmt/src/grd2xyz.py | 2 +- pygmt/src/grdhisteq.py | 2 +- pygmt/src/grdtrack.py | 2 +- pygmt/src/grdvolume.py | 2 +- pygmt/src/project.py | 2 +- pygmt/src/select.py | 2 +- pygmt/src/triangulate.py | 2 +- 10 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index e2feb9cf857..2d70ca82286 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -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: """ @@ -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. @@ -1795,7 +1795,8 @@ 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 @@ -1805,7 +1806,8 @@ 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) ... @@ -1814,7 +1816,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) ... @@ -1823,8 +1825,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) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index 3030234d707..1798da62e9e 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -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 ) diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index 3bb08cf6ff2..9d10f488d44 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -123,4 +123,7 @@ 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, + ) diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index ea1c52cb224..4c6d3eb224f 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -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 ) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 880368f1a7c..b0285e4e3d5 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -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": diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index d71300fa3ba..930cf00656f 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -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, ) diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index f53a57e9fd7..c651163076c 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -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) diff --git a/pygmt/src/project.py b/pygmt/src/project.py index 833c58ce299..ab0e6ba7f8d 100644 --- a/pygmt/src/project.py +++ b/pygmt/src/project.py @@ -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, ) diff --git a/pygmt/src/select.py b/pygmt/src/select.py index 2e9b97299a5..fde4f88b47f 100644 --- a/pygmt/src/select.py +++ b/pygmt/src/select.py @@ -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, ) diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index 62c0a24d80e..7a64178c99a 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -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) From 3aaeb931e6bcef85dc5cbea33672b084414c2afe Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 20 Mar 2024 08:56:33 +0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/clib/session.py | 6 ++---- pygmt/src/filter1d.py | 5 +---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 2d70ca82286..6e6e495e244 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1795,8 +1795,7 @@ def virtualfile_to_dataset( ... ) as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") ... result = lib.virtualfile_to_dataset( - ... vfname=vouttbl, - ... output_type="file", + ... vfname=vouttbl, output_type="file" ... ) ... assert result is None ... assert Path(outtmp.name).stat().st_size > 0 @@ -1806,8 +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( - ... vfname=vouttbl, - ... output_type="numpy", + ... vfname=vouttbl, output_type="numpy" ... ) ... assert isinstance(outnp, np.ndarray) ... diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index 9d10f488d44..32e046e2e59 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -123,7 +123,4 @@ def filter1d( module="filter1d", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.virtualfile_to_dataset( - vfname=vouttbl, - output_type=output_type, - ) + return lib.virtualfile_to_dataset(vfname=vouttbl, output_type=output_type)