From 6827c8282b376a6cb83179543d8d827322e89629 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 10:59:15 +0800 Subject: [PATCH 01/13] clib: Add the return_table method for output table data --- pygmt/clib/session.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index d52f66501af..f08c43fd65f 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1738,6 +1738,42 @@ def read_virtualfile( dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind] return ctp.cast(pointer, ctp.POINTER(dtype)) + def return_table( + self, + output_type: Literal["pandas", "numpy", "file"], + vfile: str, + column_names: list[str] | None = None, + ) -> pd.DataFrame | np.ndarray | None: + """ + Return an output table from a virtual file based on the output type. + + Parameters + ---------- + output_type + The output type. Valid values are ``"pandas"``, ``"numpy"``, or ``"file"``. + vfile + The virtual file name. + column_names + The column names for the :class:`pandas.DataFrame` output. + + Returns + ------- + :class:`pandas.DataFrame` or :class:`numpy.ndarray` or None + The output table. If ``output_type`` is ``"file"``, returns ``None``. + """ + if output_type == "file": # Already written to file, so return None + return None + # Read the virtual file as a GMT dataset and convert to pandas.DataFrame + result = self.read_virtualfile(vfile, kind="dataset").contents.to_dataframe() + # Assign column names + if column_names is not None: + result.columns = column_names + # Pandas.DataFrame output + if output_type == "pandas": + return result + # NumPy.ndarray output + return result.to_numpy() + def extract_region(self): """ Extract the WESN bounding box of the currently active figure. From bd166fef842828bf8972e7afcb9835d2df75cf56 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 11:56:46 +0800 Subject: [PATCH 02/13] Add the function to doc --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 8758ef10423..ca63a3d4472 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -294,6 +294,7 @@ conversion of Python variables to GMT virtual files: clib.Session.virtualfile_from_grid clib.Session.virtualfile_in clib.Session.virtualfile_out + clib.Session.return_table Low level access (these are mostly used by the :mod:`pygmt.clib` package): From d376a746196af9aabfa23df8ef80874c39fe30ff Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 13:22:23 +0800 Subject: [PATCH 03/13] Add doctest --- pygmt/clib/session.py | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index f08c43fd65f..cecffc16ee6 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1760,6 +1760,78 @@ def return_table( ------- :class:`pandas.DataFrame` or :class:`numpy.ndarray` or None The output table. If ``output_type`` is ``"file"``, returns ``None``. + + Examples + -------- + >>> from pathlib import Path + >>> import numpy as np + >>> import pandas as pd + >>> + >>> from pygmt.helpers import GMTTempFile + >>> from pygmt.clib import Session + >>> + >>> with GMTTempFile(suffix=".txt") as tmpfile: + ... # prepare the sample data file + ... with open(tmpfile.name, mode="w") as fp: + ... print(">", file=fp) + ... print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp) + ... print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp) + ... print(">", file=fp) + ... print("7.0 8.0 9.0 TEXT8 TEXT90", file=fp) + ... print("10.0 11.0 12.0 TEXT123 TEXT456789", file=fp) + ... + ... # file output + ... with Session() as lib: + ... with GMTTempFile(suffix=".txt") as outtmp: + ... with lib.virtualfile_out( + ... kind="dataset", fname=outtmp.name + ... ) as vouttbl: + ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") + ... result = lib.return_table(output_type="file", vfile=vouttbl) + ... assert result is None + ... assert Path(outtmp.name).stat().st_size > 0 + ... + ... # numpy output + ... with Session() as lib: + ... with lib.virtualfile_out(kind="dataset") as vouttbl: + ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") + ... outnp = lib.return_table(output_type="numpy", vfile=vouttbl) + ... assert isinstance(outnp, np.ndarray) + ... + ... # pandas output + ... with Session() as lib: + ... with lib.virtualfile_out(kind="dataset") as vouttbl: + ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") + ... outpd = lib.return_table(output_type="pandas", vfile=vouttbl) + ... assert isinstance(outpd, pd.DataFrame) + ... + ... # pandas output with specified column names + ... with Session() as lib: + ... with lib.virtualfile_out(kind="dataset") as vouttbl: + ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") + ... outpd2 = lib.return_table( + ... output_type="pandas", + ... vfile=vouttbl, + ... column_names=["col1", "col2", "col3", "coltext"], + ... ) + ... assert isinstance(outpd2, pd.DataFrame) + >>> outnp + array([[1.0, 2.0, 3.0, 'TEXT1 TEXT23'], + [4.0, 5.0, 6.0, 'TEXT4 TEXT567'], + [7.0, 8.0, 9.0, 'TEXT8 TEXT90'], + [10.0, 11.0, 12.0, 'TEXT123 TEXT456789']], dtype=object) + >>> outpd + 0 1 2 3 + 0 1.0 2.0 3.0 TEXT1 TEXT23 + 1 4.0 5.0 6.0 TEXT4 TEXT567 + 2 7.0 8.0 9.0 TEXT8 TEXT90 + 3 10.0 11.0 12.0 TEXT123 TEXT456789 + >>> outpd2 + col1 col2 col3 coltext + 0 1.0 2.0 3.0 TEXT1 TEXT23 + 1 4.0 5.0 6.0 TEXT4 TEXT567 + 2 7.0 8.0 9.0 TEXT8 TEXT90 + 3 10.0 11.0 12.0 TEXT123 TEXT456789 """ if output_type == "file": # Already written to file, so return None return None From 193bd05683cf3aee956eac7f7edc753b7ddd09fb Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 13:31:58 +0800 Subject: [PATCH 04/13] Improve docstrings --- pygmt/clib/session.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index cecffc16ee6..bcca02df945 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1741,7 +1741,7 @@ def read_virtualfile( def return_table( self, output_type: Literal["pandas", "numpy", "file"], - vfile: str, + vfile: str | None = None, column_names: list[str] | None = None, ) -> pd.DataFrame | np.ndarray | None: """ @@ -1750,16 +1750,21 @@ def return_table( Parameters ---------- output_type - The output type. Valid values are ``"pandas"``, ``"numpy"``, or ``"file"``. + 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``. vfile - The virtual file name. + 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. Returns ------- - :class:`pandas.DataFrame` or :class:`numpy.ndarray` or None - The output table. If ``output_type`` is ``"file"``, returns ``None``. + table + The output table. If ``output_type="file"`` returns ``None``. Examples -------- @@ -1835,16 +1840,16 @@ def return_table( """ if output_type == "file": # Already written to file, so return None return None + # Read the virtual file as a GMT dataset and convert to pandas.DataFrame result = self.read_virtualfile(vfile, kind="dataset").contents.to_dataframe() + if output_type == "numpy": # numpy.ndarray output + return result.to_numpy() + # Assign column names if column_names is not None: result.columns = column_names - # Pandas.DataFrame output - if output_type == "pandas": - return result - # NumPy.ndarray output - return result.to_numpy() + return result # pandas.DataFrame output def extract_region(self): """ From c9e482ae1dc15877cc2cc0759de286d220774776 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 13:45:52 +0800 Subject: [PATCH 05/13] fix --- pygmt/clib/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index bcca02df945..61daa68a8cf 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1741,7 +1741,7 @@ def read_virtualfile( def return_table( self, output_type: Literal["pandas", "numpy", "file"], - vfile: str | None = None, + vfile: str, column_names: list[str] | None = None, ) -> pd.DataFrame | np.ndarray | None: """ From 9baad27101135f880995448096fc955868f33281 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 11:02:41 +0800 Subject: [PATCH 06/13] Make all functions/methods have consistent behavior for table output --- pygmt/src/blockm.py | 87 +++++++++++++++++++++------------ pygmt/src/filter1d.py | 31 ++++-------- pygmt/src/grd2xyz.py | 41 ++++++---------- pygmt/src/grdhisteq.py | 46 ++++++++--------- pygmt/src/grdtrack.py | 53 ++++++++++---------- pygmt/src/grdvolume.py | 31 ++++-------- pygmt/src/project.py | 61 ++++++++++++----------- pygmt/src/select.py | 46 +++++++++-------- pygmt/src/triangulate.py | 33 +++++-------- pygmt/tests/test_triangulate.py | 7 ++- 10 files changed, 208 insertions(+), 228 deletions(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index c863f32f3b0..a8c1af9124d 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -6,17 +6,17 @@ import pandas as pd from pygmt.clib import Session from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, use_alias, + validate_output_table_type, ) __doctest_skip__ = ["blockmean", "blockmedian", "blockmode"] -def _blockm(block_method, data, x, y, z, outfile, **kwargs): +def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs): r""" Block average (x, y, z) data tables by mean, median, or mode estimation. @@ -42,30 +42,28 @@ def _blockm(block_method, data, x, y, z, outfile, **kwargs): - None if ``outfile`` is set (filtered output will be stored in file set by ``outfile``) """ - with GMTTempFile(suffix=".csv") as tmpfile: - with Session() as lib: - with lib.virtualfile_in( + output_type = validate_output_table_type(output_type, outfile=outfile) + + column_names = None + if isinstance(data, pd.DataFrame) and output_type == "pandas": + column_names = data.columns.to_list() + + with Session() as lib: + with ( + lib.virtualfile_in( check_kind="vector", data=data, x=x, y=y, z=z, required_z=True - ) as vintbl: - # Run blockm* on data table - if outfile is None: - outfile = tmpfile.name - lib.call_module( - module=block_method, - args=build_arg_string(kwargs, infile=vintbl, outfile=outfile), - ) - - # Read temporary csv output to a pandas table - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame - try: - column_names = data.columns.to_list() - result = pd.read_csv(tmpfile.name, sep="\t", names=column_names) - except AttributeError: # 'str' object has no attribute 'columns' - result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") - elif outfile != tmpfile.name: # return None if outfile set, output in outfile - result = None - - return result + ) as vintbl, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module=block_method, + args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), + ) + return lib.return_table( + output_type=output_type, + vfile=vouttbl, + column_names=column_names, + ) @fmt_docstring @@ -86,7 +84,9 @@ def _blockm(block_method, data, x, y, z, outfile, **kwargs): w="wrap", ) @kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma", o="sequence_comma") -def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs): +def blockmean( + data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs +): r""" Block average (x, y, z) data tables by mean estimation. @@ -159,7 +159,14 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs): >>> data_bmean = pygmt.blockmean(data=data, region=[245, 255, 20, 30], spacing="5m") """ return _blockm( - block_method="blockmean", data=data, x=x, y=y, z=z, outfile=outfile, **kwargs + block_method="blockmean", + data=data, + x=x, + y=y, + z=z, + output_type=output_type, + outfile=outfile, + **kwargs, ) @@ -180,7 +187,9 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs): w="wrap", ) @kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma", o="sequence_comma") -def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs): +def blockmedian( + data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs +): r""" Block average (x, y, z) data tables by median estimation. @@ -246,7 +255,14 @@ def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs): ... ) """ return _blockm( - block_method="blockmedian", data=data, x=x, y=y, z=z, outfile=outfile, **kwargs + block_method="blockmedian", + data=data, + x=x, + y=y, + z=z, + output_type=output_type, + outfile=outfile, + **kwargs, ) @@ -267,7 +283,9 @@ def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs): w="wrap", ) @kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma", o="sequence_comma") -def blockmode(data=None, x=None, y=None, z=None, outfile=None, **kwargs): +def blockmode( + data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs +): r""" Block average (x, y, z) data tables by mode estimation. @@ -331,5 +349,12 @@ def blockmode(data=None, x=None, y=None, z=None, outfile=None, **kwargs): >>> data_bmode = pygmt.blockmode(data=data, region=[245, 255, 20, 30], spacing="5m") """ return _blockm( - block_method="blockmode", data=data, x=x, y=y, z=z, outfile=outfile, **kwargs + block_method="blockmode", + data=data, + x=x, + y=y, + z=z, + output_type=output_type, + outfile=outfile, + **kwargs, ) diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index 79163e2b0dd..a8a123b1f6d 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -2,11 +2,9 @@ filter1d - Time domain filtering of 1-D data tables """ -import pandas as pd from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, use_alias, @@ -117,22 +115,13 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs): output_type = validate_output_table_type(output_type, outfile=outfile) - with GMTTempFile() as tmpfile: - with Session() as lib: - with lib.virtualfile_in(check_kind="vector", data=data) as vintbl: - if outfile is None: - outfile = tmpfile.name - lib.call_module( - module="filter1d", - args=build_arg_string(kwargs, infile=vintbl, outfile=outfile), - ) - - # Read temporary csv output to a pandas table - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame - result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") - elif outfile != tmpfile.name: # return None if outfile set, output in outfile - result = None - - if output_type == "numpy": - result = result.to_numpy() - return result + with Session() as lib: + with ( + lib.virtualfile_in(check_kind="vector", data=data) as vintbl, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module="filter1d", + args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), + ) + return lib.return_table(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index eade93473c2..f5152496bfb 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -2,12 +2,10 @@ grd2xyz - Convert grid to data table """ -import pandas as pd import xarray as xr from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, @@ -150,30 +148,23 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): ) # Set the default column names for the pandas dataframe header - dataframe_header = ["x", "y", "z"] + column_names = ["x", "y", "z"] # Let output pandas column names match input DataArray dimension names if isinstance(grid, xr.DataArray) and output_type == "pandas": # Reverse the dims because it is rows, columns ordered. - dataframe_header = [grid.dims[1], grid.dims[0], grid.name] - - with GMTTempFile() as tmpfile: - with Session() as lib: - with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd: - if outfile is None: - outfile = tmpfile.name - lib.call_module( - module="grd2xyz", - args=build_arg_string(kwargs, infile=vingrd, outfile=outfile), - ) - - # Read temporary csv output to a pandas table - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame - result = pd.read_csv( - tmpfile.name, sep="\t", names=dataframe_header, comment=">" + column_names = [grid.dims[1], grid.dims[0], grid.name] + + with Session() as lib: + with ( + lib.virtualfile_in(check_kind="raster", data=grid) as vingrd, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module="grd2xyz", + args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl), ) - elif outfile != tmpfile.name: # return None if outfile set, output in outfile - result = None - - if output_type == "numpy": - result = result.to_numpy() - return result + return lib.return_table( + output_type=output_type, + vfile=vouttbl, + column_names=column_names, + ) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 0e2c8c9ea60..5490d70cd50 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -3,7 +3,6 @@ """ import numpy as np -import pandas as pd from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( @@ -231,33 +230,28 @@ def compute_bins(grid, output_type="pandas", **kwargs): if kwargs.get("h") is not None and output_type != "file": raise GMTInvalidInput("'header' is only allowed with output_type='file'.") - with GMTTempFile(suffix=".txt") as tmpfile: - with Session() as lib: - with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd: - if outfile is None: - kwargs["D"] = outfile = tmpfile.name # output to tmpfile - lib.call_module( - module="grdhisteq", args=build_arg_string(kwargs, infile=vingrd) - ) + with Session() as lib: + with ( + lib.virtualfile_in(check_kind="raster", data=grid) as vingrd, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + kwargs["D"] = vouttbl # -D for output file name + lib.call_module( + module="grdhisteq", args=build_arg_string(kwargs, infile=vingrd) + ) - if outfile == tmpfile.name: - # if user did not set outfile, return pd.DataFrame - result = pd.read_csv( - filepath_or_buffer=outfile, - sep="\t", - header=None, - names=["start", "stop", "bin_id"], - dtype={ + result = lib.return_table( + output_type=output_type, + vfile=vouttbl, + column_names=["start", "stop", "bin_id"], + ) + if output_type == "pandas": + result = result.astype( + { "start": np.float32, "stop": np.float32, "bin_id": np.uint32, - }, + } ) - elif outfile != tmpfile.name: - # return None if outfile set, output in outfile - return None - - if output_type == "numpy": - return result.to_numpy() - - return result.set_index("bin_id") + return result.set_index("bin_id") + return result diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index 1e5df5ffbda..731bbe254f1 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -6,11 +6,11 @@ from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, use_alias, + validate_output_table_type, ) __doctest_skip__ = ["grdtrack"] @@ -44,7 +44,9 @@ w="wrap", ) @kwargs_to_strings(R="sequence", S="sequence", i="sequence_comma", o="sequence_comma") -def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs): +def grdtrack( + grid, points=None, output_type="pandas", outfile=None, newcolname=None, **kwargs +): r""" Sample grids at specified (x,y) locations. @@ -291,30 +293,27 @@ def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs): if hasattr(points, "columns") and newcolname is None: raise GMTInvalidInput("Please pass in a str to 'newcolname'") - with GMTTempFile(suffix=".csv") as tmpfile: - with Session() as lib: - with ( - lib.virtualfile_in(check_kind="raster", data=grid) as vingrd, - lib.virtualfile_in( - check_kind="vector", data=points, required_data=False - ) as vintbl, - ): - kwargs["G"] = vingrd - if outfile is None: # Output to tmpfile if outfile is not set - outfile = tmpfile.name - lib.call_module( - module="grdtrack", - args=build_arg_string(kwargs, infile=vintbl, outfile=outfile), - ) + output_type = validate_output_table_type(output_type, outfile=outfile) - # Read temporary csv output to a pandas table - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame - try: - column_names = [*points.columns.to_list(), newcolname] - result = pd.read_csv(tmpfile.name, sep="\t", names=column_names) - except AttributeError: # 'str' object has no attribute 'columns' - result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") - elif outfile != tmpfile.name: # return None if outfile set, output in outfile - result = None + column_names = None + if isinstance(points, pd.DataFrame) and output_type == "pandas": + column_names = [*points.columns.to_list(), newcolname] - return result + with Session() as lib: + with ( + lib.virtualfile_in(check_kind="raster", data=grid) as vingrd, + lib.virtualfile_in( + check_kind="vector", data=points, required_data=False + ) as vintbl, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + kwargs["G"] = vingrd + lib.call_module( + module="grdtrack", + args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), + ) + return lib.return_table( + output_type=output_type, + vfile=vouttbl, + column_names=column_names, + ) diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index 1bb696e9e04..b8712347168 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -2,10 +2,8 @@ grdvolume - Calculate grid volume and area constrained by a contour. """ -import pandas as pd from pygmt.clib import Session from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, @@ -103,22 +101,13 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): """ output_type = validate_output_table_type(output_type, outfile=outfile) - with GMTTempFile() as tmpfile: - with Session() as lib: - with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd: - if outfile is None: - outfile = tmpfile.name - lib.call_module( - module="grdvolume", - args=build_arg_string(kwargs, infile=vingrd, outfile=outfile), - ) - - # Read temporary csv output to a pandas table - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame - result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") - elif outfile != tmpfile.name: # return None if outfile set, output in outfile - result = None - - if output_type == "numpy": - result = result.to_numpy() - return result + with Session() as lib: + with ( + lib.virtualfile_in(check_kind="raster", data=grid) as vingrid, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module="grdvolume", + args=build_arg_string(kwargs, infile=vingrid, outfile=vouttbl), + ) + return lib.return_table(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/src/project.py b/pygmt/src/project.py index 99738bfd9c8..4abf2de1659 100644 --- a/pygmt/src/project.py +++ b/pygmt/src/project.py @@ -2,15 +2,14 @@ project - Project data onto lines or great circles, or generate tracks. """ -import pandas as pd from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, use_alias, + validate_output_table_type, ) @@ -32,7 +31,9 @@ f="coltypes", ) @kwargs_to_strings(E="sequence", L="sequence", T="sequence", W="sequence", C="sequence") -def project(data=None, x=None, y=None, z=None, outfile=None, **kwargs): +def project( + data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs +): r""" Project data onto lines or great circles, or generate tracks. @@ -223,29 +224,31 @@ def project(data=None, x=None, y=None, z=None, outfile=None, **kwargs): "The `convention` parameter is not allowed with `generate`." ) - with GMTTempFile(suffix=".csv") as tmpfile: - if outfile is None: # Output to tmpfile if outfile is not set - outfile = tmpfile.name - with Session() as lib: - if kwargs.get("G") is None: - with lib.virtualfile_in( - check_kind="vector", data=data, x=x, y=y, z=z, required_z=False - ) as vintbl: - # Run project on the temporary (csv) data table - arg_str = build_arg_string(kwargs, infile=vintbl, outfile=outfile) - else: - arg_str = build_arg_string(kwargs, outfile=outfile) - lib.call_module(module="project", args=arg_str) - - # if user did not set outfile, return pd.DataFrame - if outfile == tmpfile.name: - if kwargs.get("G") is not None: - column_names = list("rsp") - result = pd.read_csv(tmpfile.name, sep="\t", names=column_names) - else: - result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") - # return None if outfile set, output in outfile - elif outfile != tmpfile.name: - result = None - - return result + output_type = validate_output_table_type(output_type, outfile=outfile) + + column_names = None + if kwargs.get("G") is not None and output_type == "pandas": + column_names = list("rsp") + + with Session() as lib: + with ( + lib.virtualfile_in( + check_kind="vector", + data=data, + x=x, + y=y, + z=z, + required_z=False, + required_data=False, + ) as vintbl, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module="project", + args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), + ) + return lib.return_table( + output_type=output_type, + vfile=vouttbl, + column_names=column_names, + ) diff --git a/pygmt/src/select.py b/pygmt/src/select.py index fe132b356f9..4d0dc53d0b2 100644 --- a/pygmt/src/select.py +++ b/pygmt/src/select.py @@ -5,11 +5,11 @@ import pandas as pd from pygmt.clib import Session from pygmt.helpers import ( - GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, use_alias, + validate_output_table_type, ) __doctest_skip__ = ["select"] @@ -41,7 +41,7 @@ w="wrap", ) @kwargs_to_strings(M="sequence", R="sequence", i="sequence_comma", o="sequence_comma") -def select(data=None, outfile=None, **kwargs): +def select(data=None, output_type="pandas", outfile=None, **kwargs): r""" Select data table subsets based on multiple spatial criteria. @@ -196,25 +196,23 @@ def select(data=None, outfile=None, **kwargs): >>> # longitudes 246 and 247 and latitudes 20 and 21 >>> out = pygmt.select(data=ship_data, region=[246, 247, 20, 21]) """ - - with GMTTempFile(suffix=".csv") as tmpfile: - with Session() as lib: - with lib.virtualfile_in(check_kind="vector", data=data) as vintbl: - if outfile is None: - outfile = tmpfile.name - lib.call_module( - module="select", - args=build_arg_string(kwargs, infile=vintbl, outfile=outfile), - ) - - # Read temporary csv output to a pandas table - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame - try: - column_names = data.columns.to_list() - result = pd.read_csv(tmpfile.name, sep="\t", names=column_names) - except AttributeError: # 'str' object has no attribute 'columns' - result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") - elif outfile != tmpfile.name: # return None if outfile set, output in outfile - result = None - - return result + output_type = validate_output_table_type(output_type, outfile=outfile) + + column_names = None + if isinstance(data, pd.DataFrame) and output_type == "pandas": + column_names = data.columns.to_list() + + with Session() as lib: + with ( + lib.virtualfile_in(check_kind="vector", data=data) as vintbl, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module="select", + args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), + ) + return lib.return_table( + output_type=output_type, + vfile=vouttbl, + column_names=column_names, + ) diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index e73ab92fe5e..e8505bca5aa 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -3,7 +3,6 @@ Cartesian data. """ -import pandas as pd from pygmt.clib import Session from pygmt.helpers import ( GMTTempFile, @@ -243,25 +242,15 @@ def delaunay_triples( """ output_type = validate_output_table_type(output_type, outfile) - with GMTTempFile(suffix=".txt") as tmpfile: - with Session() as lib: - with lib.virtualfile_in( + with Session() as lib: + with ( + lib.virtualfile_in( check_kind="vector", data=data, x=x, y=y, z=z, required_z=False - ) as vintbl: - if outfile is None: - outfile = tmpfile.name - lib.call_module( - module="triangulate", - args=build_arg_string(kwargs, infile=vintbl, outfile=outfile), - ) - - if outfile == tmpfile.name: - # if user did not set outfile, return pd.DataFrame - result = pd.read_csv(outfile, sep="\t", header=None) - elif outfile != tmpfile.name: - # return None if outfile set, output in outfile - result = None - - if output_type == "numpy": - result = result.to_numpy() - return result + ) as vintbl, + lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl, + ): + lib.call_module( + module="triangulate", + args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), + ) + return lib.return_table(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/tests/test_triangulate.py b/pygmt/tests/test_triangulate.py index 154bc82b09f..75cccbf17ab 100644 --- a/pygmt/tests/test_triangulate.py +++ b/pygmt/tests/test_triangulate.py @@ -44,7 +44,8 @@ def fixture_expected_dataframe(): [4, 6, 1], [3, 4, 2], [9, 3, 8], - ] + ], + dtype=float, ) @@ -116,7 +117,9 @@ def test_delaunay_triples_outfile(dataframe, expected_dataframe): assert len(record) == 1 # check that only one warning was raised assert result is None # return value is None assert Path(tmpfile.name).stat().st_size > 0 - temp_df = pd.read_csv(filepath_or_buffer=tmpfile.name, sep="\t", header=None) + temp_df = pd.read_csv( + filepath_or_buffer=tmpfile.name, sep="\t", header=None, dtype=float + ) pd.testing.assert_frame_equal(left=temp_df, right=expected_dataframe) From 223ae5cd74546ecffb7f334851d9633c39b0eb26 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Mar 2024 14:09:06 +0800 Subject: [PATCH 07/13] Check output_type first because it's faster --- pygmt/src/blockm.py | 2 +- pygmt/src/grd2xyz.py | 2 +- pygmt/src/grdtrack.py | 2 +- pygmt/src/project.py | 2 +- pygmt/src/select.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index a8c1af9124d..2e4bbd11a7e 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -45,7 +45,7 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs): output_type = validate_output_table_type(output_type, outfile=outfile) column_names = None - if isinstance(data, pd.DataFrame) and output_type == "pandas": + if output_type == "pandas" and isinstance(data, pd.DataFrame): column_names = data.columns.to_list() with Session() as lib: diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index f5152496bfb..8589b2ef57e 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -150,7 +150,7 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): # Set the default column names for the pandas dataframe header column_names = ["x", "y", "z"] # Let output pandas column names match input DataArray dimension names - if isinstance(grid, xr.DataArray) and output_type == "pandas": + if output_type == "pandas" and isinstance(grid, xr.DataArray): # Reverse the dims because it is rows, columns ordered. column_names = [grid.dims[1], grid.dims[0], grid.name] diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index 731bbe254f1..bf72385f2ee 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -296,7 +296,7 @@ def grdtrack( output_type = validate_output_table_type(output_type, outfile=outfile) column_names = None - if isinstance(points, pd.DataFrame) and output_type == "pandas": + if output_type == "pandas" and isinstance(points, pd.DataFrame): column_names = [*points.columns.to_list(), newcolname] with Session() as lib: diff --git a/pygmt/src/project.py b/pygmt/src/project.py index 4abf2de1659..293a29ead44 100644 --- a/pygmt/src/project.py +++ b/pygmt/src/project.py @@ -227,7 +227,7 @@ def project( output_type = validate_output_table_type(output_type, outfile=outfile) column_names = None - if kwargs.get("G") is not None and output_type == "pandas": + if output_type == "pandas" and kwargs.get("G") is not None: column_names = list("rsp") with Session() as lib: diff --git a/pygmt/src/select.py b/pygmt/src/select.py index 4d0dc53d0b2..1b406052c96 100644 --- a/pygmt/src/select.py +++ b/pygmt/src/select.py @@ -199,7 +199,7 @@ def select(data=None, output_type="pandas", outfile=None, **kwargs): output_type = validate_output_table_type(output_type, outfile=outfile) column_names = None - if isinstance(data, pd.DataFrame) and output_type == "pandas": + if output_type == "pandas" and isinstance(data, pd.DataFrame): column_names = data.columns.to_list() with Session() as lib: From ce029b252a8b29dd36dcad2bfeedef36a88c8ee8 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 8 Mar 2024 07:49:53 +0800 Subject: [PATCH 08/13] Rename return_table to return_dataset --- pygmt/clib/session.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 61daa68a8cf..f77fd832198 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1738,14 +1738,16 @@ def read_virtualfile( dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind] return ctp.cast(pointer, ctp.POINTER(dtype)) - def return_table( + def return_dataset( self, output_type: Literal["pandas", "numpy", "file"], vfile: str, column_names: list[str] | None = None, ) -> pd.DataFrame | np.ndarray | None: """ - Return an output table from a virtual file based on the output type. + Output a dataset stored in a virtual file in different formats. + + The format of the dataset is determined by the ``output_type`` parameter. Parameters ---------- @@ -1763,8 +1765,8 @@ def return_table( Returns ------- - table - The output table. If ``output_type="file"`` returns ``None``. + result + The result dataset. If ``output_type="file"`` returns ``None``. Examples -------- @@ -1792,7 +1794,9 @@ def return_table( ... kind="dataset", fname=outtmp.name ... ) as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... result = lib.return_table(output_type="file", vfile=vouttbl) + ... result = lib.return_dataset( + ... output_type="file", vfile=vouttbl + ... ) ... assert result is None ... assert Path(outtmp.name).stat().st_size > 0 ... @@ -1800,21 +1804,21 @@ def return_table( ... with Session() as lib: ... with lib.virtualfile_out(kind="dataset") as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... outnp = lib.return_table(output_type="numpy", vfile=vouttbl) + ... outnp = lib.return_dataset(output_type="numpy", vfile=vouttbl) ... assert isinstance(outnp, np.ndarray) ... ... # pandas output ... with Session() as lib: ... with lib.virtualfile_out(kind="dataset") as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... outpd = lib.return_table(output_type="pandas", vfile=vouttbl) + ... outpd = lib.return_dataset(output_type="pandas", vfile=vouttbl) ... assert isinstance(outpd, pd.DataFrame) ... ... # pandas output with specified column names ... with Session() as lib: ... with lib.virtualfile_out(kind="dataset") as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... outpd2 = lib.return_table( + ... outpd2 = lib.return_dataset( ... output_type="pandas", ... vfile=vouttbl, ... column_names=["col1", "col2", "col3", "coltext"], From 9640c26788a62d346a8b114ef8b838430af36c5d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 8 Mar 2024 08:07:25 +0800 Subject: [PATCH 09/13] Update doc index page --- doc/api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/index.rst b/doc/api/index.rst index ca63a3d4472..e7d7f0decd0 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -294,7 +294,7 @@ conversion of Python variables to GMT virtual files: clib.Session.virtualfile_from_grid clib.Session.virtualfile_in clib.Session.virtualfile_out - clib.Session.return_table + clib.Session.return_dataset Low level access (these are mostly used by the :mod:`pygmt.clib` package): From d59e5a66090725a3ae94fc8e21c64dca21f34dc2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 8 Mar 2024 15:07:50 +0800 Subject: [PATCH 10/13] Change return_table to return_dataset --- pygmt/src/blockm.py | 2 +- pygmt/src/filter1d.py | 2 +- 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 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index 2e4bbd11a7e..c16b2a87081 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -59,7 +59,7 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs): module=block_method, args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_table( + return lib.return_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index a8a123b1f6d..fc0ef059131 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -124,4 +124,4 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs): module="filter1d", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_table(output_type=output_type, vfile=vouttbl) + return lib.return_dataset(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index 8589b2ef57e..ad3577dea8b 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -163,7 +163,7 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): module="grd2xyz", args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl), ) - return lib.return_table( + return lib.return_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 5490d70cd50..e4a479629cd 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -240,7 +240,7 @@ def compute_bins(grid, output_type="pandas", **kwargs): module="grdhisteq", args=build_arg_string(kwargs, infile=vingrd) ) - result = lib.return_table( + result = lib.return_dataset( output_type=output_type, vfile=vouttbl, column_names=["start", "stop", "bin_id"], diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index bf72385f2ee..e8d09720720 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -312,7 +312,7 @@ def grdtrack( module="grdtrack", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_table( + return lib.return_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index b8712347168..774f97d7531 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -110,4 +110,4 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): module="grdvolume", args=build_arg_string(kwargs, infile=vingrid, outfile=vouttbl), ) - return lib.return_table(output_type=output_type, vfile=vouttbl) + return lib.return_dataset(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/src/project.py b/pygmt/src/project.py index 293a29ead44..5e228ddf9dd 100644 --- a/pygmt/src/project.py +++ b/pygmt/src/project.py @@ -247,7 +247,7 @@ def project( module="project", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_table( + return lib.return_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/select.py b/pygmt/src/select.py index 1b406052c96..34c5fad84ef 100644 --- a/pygmt/src/select.py +++ b/pygmt/src/select.py @@ -211,7 +211,7 @@ def select(data=None, output_type="pandas", outfile=None, **kwargs): module="select", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_table( + return lib.return_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index e8505bca5aa..bc37f404eb9 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -253,4 +253,4 @@ def delaunay_triples( module="triangulate", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_table(output_type=output_type, vfile=vouttbl) + return lib.return_dataset(output_type=output_type, vfile=vouttbl) From 796f1cc5c66051632d848c34cfab8c58ee5a77e1 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 11 Mar 2024 07:58:54 +0800 Subject: [PATCH 11/13] Update pygmt/clib/session.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/clib/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index f77fd832198..2ab37d08d6b 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1745,7 +1745,7 @@ def return_dataset( column_names: list[str] | None = None, ) -> pd.DataFrame | np.ndarray | None: """ - Output a dataset stored in a virtual file in different formats. + Output a tabular dataset stored in a virtual file to a different format. The format of the dataset is determined by the ``output_type`` parameter. From 711142c8816c91f7de51ebfb548990ce3a51731e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 11 Mar 2024 11:14:36 +0800 Subject: [PATCH 12/13] Rename return_dataset to virtualfile_to_dataset --- doc/api/index.rst | 2 +- pygmt/clib/session.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/doc/api/index.rst b/doc/api/index.rst index e7d7f0decd0..ca62bfd02be 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -294,7 +294,7 @@ conversion of Python variables to GMT virtual files: clib.Session.virtualfile_from_grid clib.Session.virtualfile_in clib.Session.virtualfile_out - clib.Session.return_dataset + clib.Session.virtualfile_to_dataset Low level access (these are mostly used by the :mod:`pygmt.clib` package): diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 2ab37d08d6b..524dad95036 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1738,7 +1738,7 @@ def read_virtualfile( dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind] return ctp.cast(pointer, ctp.POINTER(dtype)) - def return_dataset( + def virtualfile_to_dataset( self, output_type: Literal["pandas", "numpy", "file"], vfile: str, @@ -1794,7 +1794,7 @@ def return_dataset( ... kind="dataset", fname=outtmp.name ... ) as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... result = lib.return_dataset( + ... result = lib.virtualfile_to_dataset( ... output_type="file", vfile=vouttbl ... ) ... assert result is None @@ -1804,21 +1804,25 @@ def return_dataset( ... with Session() as lib: ... with lib.virtualfile_out(kind="dataset") as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... outnp = lib.return_dataset(output_type="numpy", vfile=vouttbl) + ... outnp = lib.virtualfile_to_dataset( + ... output_type="numpy", vfile=vouttbl + ... ) ... assert isinstance(outnp, np.ndarray) ... ... # pandas output ... with Session() as lib: ... with lib.virtualfile_out(kind="dataset") as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... outpd = lib.return_dataset(output_type="pandas", vfile=vouttbl) + ... outpd = lib.virtualfile_to_dataset( + ... output_type="pandas", vfile=vouttbl + ... ) ... assert isinstance(outpd, pd.DataFrame) ... ... # pandas output with specified column names ... with Session() as lib: ... with lib.virtualfile_out(kind="dataset") as vouttbl: ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... outpd2 = lib.return_dataset( + ... outpd2 = lib.virtualfile_to_dataset( ... output_type="pandas", ... vfile=vouttbl, ... column_names=["col1", "col2", "col3", "coltext"], From 3293c8ff1ccf06a40277d478c15811491aca9252 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 11 Mar 2024 11:23:17 +0800 Subject: [PATCH 13/13] Rename return_dataset to virtualfile_to_dataset --- pygmt/src/blockm.py | 2 +- pygmt/src/filter1d.py | 2 +- 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 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index c16b2a87081..970acf8bcd1 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -59,7 +59,7 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs): module=block_method, args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_dataset( + return lib.virtualfile_to_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index fc0ef059131..73d6eaa37db 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -124,4 +124,4 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs): module="filter1d", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_dataset(output_type=output_type, vfile=vouttbl) + return lib.virtualfile_to_dataset(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index ad3577dea8b..c9e611038d8 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -163,7 +163,7 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): module="grd2xyz", args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl), ) - return lib.return_dataset( + return lib.virtualfile_to_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index e4a479629cd..9232f0ede8c 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -240,7 +240,7 @@ def compute_bins(grid, output_type="pandas", **kwargs): module="grdhisteq", args=build_arg_string(kwargs, infile=vingrd) ) - result = lib.return_dataset( + result = lib.virtualfile_to_dataset( output_type=output_type, vfile=vouttbl, column_names=["start", "stop", "bin_id"], diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index e8d09720720..5b9b2865b3d 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -312,7 +312,7 @@ def grdtrack( module="grdtrack", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_dataset( + return lib.virtualfile_to_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index 774f97d7531..7b48e63c046 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -110,4 +110,4 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): module="grdvolume", args=build_arg_string(kwargs, infile=vingrid, outfile=vouttbl), ) - return lib.return_dataset(output_type=output_type, vfile=vouttbl) + return lib.virtualfile_to_dataset(output_type=output_type, vfile=vouttbl) diff --git a/pygmt/src/project.py b/pygmt/src/project.py index 5e228ddf9dd..79b3e6a2695 100644 --- a/pygmt/src/project.py +++ b/pygmt/src/project.py @@ -247,7 +247,7 @@ def project( module="project", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_dataset( + return lib.virtualfile_to_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/select.py b/pygmt/src/select.py index 34c5fad84ef..633f967e85c 100644 --- a/pygmt/src/select.py +++ b/pygmt/src/select.py @@ -211,7 +211,7 @@ def select(data=None, output_type="pandas", outfile=None, **kwargs): module="select", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_dataset( + return lib.virtualfile_to_dataset( output_type=output_type, vfile=vouttbl, column_names=column_names, diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index bc37f404eb9..e0f517b20c4 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -253,4 +253,4 @@ def delaunay_triples( module="triangulate", args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl), ) - return lib.return_dataset(output_type=output_type, vfile=vouttbl) + return lib.virtualfile_to_dataset(output_type=output_type, vfile=vouttbl)