From cd11f985b43316ff8ca89a011a180b4a9022e619 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 27 Oct 2023 11:11:48 +0800 Subject: [PATCH 1/3] Add the validate_output_type function to check the output_type parameter --- pygmt/helpers/__init__.py | 1 + pygmt/helpers/validators.py | 41 +++++++++++++++++++++++++++++++++++++ pygmt/src/filter1d.py | 23 ++++++++------------- pygmt/src/grd2xyz.py | 18 ++-------------- pygmt/src/grdhisteq.py | 16 ++------------- pygmt/src/grdvolume.py | 9 ++------ pygmt/src/triangulate.py | 17 ++------------- 7 files changed, 59 insertions(+), 66 deletions(-) create mode 100644 pygmt/helpers/validators.py diff --git a/pygmt/helpers/__init__.py b/pygmt/helpers/__init__.py index 5eb265e8002..bd3b9680257 100644 --- a/pygmt/helpers/__init__.py +++ b/pygmt/helpers/__init__.py @@ -21,3 +21,4 @@ launch_external_viewer, non_ascii_to_octal, ) +from pygmt.helpers.validators import validate_output_type diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py new file mode 100644 index 00000000000..5d11b39f16e --- /dev/null +++ b/pygmt/helpers/validators.py @@ -0,0 +1,41 @@ +""" +Functions to check if given arguments are valid. +""" +import warnings + +from pygmt.exceptions import GMTInvalidInput + + +def validate_output_type(output_type, outfile=None): + """ + Check if the 'output_type' and 'outfile' parameters are valid. + + Parameters + ---------- + output_type : str + The type for a table output. Valid values are "file", "numpy", and + "pandas". + outfile : str + The file name for the output table file. Required if + ``output_type="file"``. + + Returns + ------- + str + The original or corrected output type. + """ + if output_type not in ["file", "numpy", "pandas"]: + raise GMTInvalidInput( + "Must specify 'output_type' either as 'file', 'numpy', or 'pandas'." + ) + if output_type == "file" and outfile is None: + raise GMTInvalidInput("Must specify 'outfile' for output_type='file'.") + if output_type != "file" and outfile is not None: + msg = ( + f"Changing 'output_type' from '{output_type}' to 'file' " + "since 'outfile' parameter is set. Please use output_type='file' " + "to silence this warning." + ) + warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) + output_type = "file" + return output_type diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index cc189a53053..1a82a88a694 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -1,12 +1,17 @@ """ filter1d - Time domain filtering of 1-D data tables """ -import warnings 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 +from pygmt.helpers import ( + GMTTempFile, + build_arg_string, + fmt_docstring, + use_alias, + validate_output_type, +) @fmt_docstring @@ -109,18 +114,8 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs): """ if kwargs.get("F") is None: raise GMTInvalidInput("Pass a required argument to 'filter_type'.") - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput("Must specify format as either numpy, pandas, or file.") - if outfile is not None and output_type != "file": - msg = ( - f"Changing `output_type` of filter1d from '{output_type}' to 'file' " - "since `outfile` parameter is set. Please use `output_type='file'` " - "to silence this warning." - ) - warnings.warn(msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" - elif output_type == "file" and outfile is None: - raise GMTInvalidInput("Must specify outfile for ASCII output.") + + output_type = validate_output_type(output_type, outfile=outfile) with GMTTempFile() as tmpfile: with Session() as lib: diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index bd3af2962b8..236290c698f 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -1,7 +1,6 @@ """ grd2xyz - Convert grid to data table """ -import warnings import pandas as pd import xarray as xr @@ -13,6 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) __doctest_skip__ = ["grd2xyz"] @@ -143,21 +143,7 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): 0 10.0 25.0 863.0 1 10.5 25.0 985.5 """ - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." - ) - - if outfile is not None and output_type != "file": - msg = ( - f"Changing 'output_type' of grd2xyz from '{output_type}' to 'file' " - "since 'outfile' parameter is set. Please use output_type='file' " - "to silence this warning." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" - elif outfile is None and output_type == "file": - raise GMTInvalidInput("Must specify 'outfile' for ASCII output.") + output_type = validate_output_type(output_type, outfile=outfile) if kwargs.get("o") is not None and output_type == "pandas": raise GMTInvalidInput( diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 0d795a7dcbd..e66ebe79daa 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -1,7 +1,6 @@ """ grdhisteq - Perform histogram equalization for a grid. """ -import warnings import numpy as np import pandas as pd @@ -13,6 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) from pygmt.io import load_dataarray @@ -321,23 +321,11 @@ def compute_bins( This method does a weighted histogram equalization for geographic grids to account for node area varying with latitude. """ - # Return a pandas.DataFrame if ``outfile`` is not set - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." - ) + output_type = validate_output_type(output_type, outfile=outfile) if header is not None and output_type != "file": raise GMTInvalidInput("'header' is only allowed with output_type='file'.") - if isinstance(outfile, str) and output_type != "file": - msg = ( - f"Changing 'output_type' from '{output_type}' to 'file' " - "since 'outfile' parameter is set. Please use output_type='file' " - "to silence this warning." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" with GMTTempFile(suffix=".txt") as tmpfile: if output_type != "file": outfile = tmpfile.name diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index ba4a2d871bc..77f7881b8ff 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -3,13 +3,13 @@ """ 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_type, ) __doctest_skip__ = ["grdvolume"] @@ -101,12 +101,7 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): 3 350 2.018302e+12 5.222640e+14 258.764032 4 400 1.857370e+12 4.252699e+14 228.963499 """ - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - """Must specify format as either numpy, pandas, or file.""" - ) - if output_type == "file" and outfile is None: - raise GMTInvalidInput("""Must specify outfile for ASCII output.""") + output_type = validate_output_type(output_type, outfile=outfile) with GMTTempFile() as tmpfile: with Session() as lib: diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index b3578941dcd..60661b1242a 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -2,7 +2,6 @@ triangulate - Delaunay triangulation or Voronoi partitioning and gridding of Cartesian data. """ -import warnings import pandas as pd from pygmt.clib import Session @@ -13,6 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) from pygmt.io import load_dataarray @@ -357,20 +357,7 @@ def delaunay_triples( # pylint: disable=too-many-arguments,too-many-locals ``triangulate`` is a Cartesian or small-geographic area operator and is unaware of periodic or polar boundary conditions. """ - # Return a pandas.DataFrame if ``outfile`` is not set - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." - ) - - if isinstance(outfile, str) and output_type != "file": - msg = ( - f"Changing 'output_type' from '{output_type}' to 'file' " - "since 'outfile' parameter is set. Please use output_type='file' " - "to silence this warning." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" + output_type = validate_output_type(output_type, outfile) # Return a pandas.DataFrame if ``outfile`` is not set with GMTTempFile(suffix=".txt") as tmpfile: From 1132b8d4b697766091f14ff5ff5c005741745798 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 28 Oct 2023 08:09:38 +0800 Subject: [PATCH 2/3] Update pygmt/helpers/validators.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/helpers/validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py index 5d11b39f16e..9e204af8c59 100644 --- a/pygmt/helpers/validators.py +++ b/pygmt/helpers/validators.py @@ -8,7 +8,7 @@ def validate_output_type(output_type, outfile=None): """ - Check if the 'output_type' and 'outfile' parameters are valid. + Check if the ``output_type`` and ``outfile`` parameters are valid. Parameters ---------- From becbb739b8c78d28684583bf7686e597917dd7b0 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 30 Oct 2023 10:58:00 +0800 Subject: [PATCH 3/3] Rename validate_output_type to validate_output_table_type --- pygmt/helpers/__init__.py | 2 +- pygmt/helpers/validators.py | 2 +- pygmt/src/filter1d.py | 4 ++-- pygmt/src/grd2xyz.py | 4 ++-- pygmt/src/grdhisteq.py | 4 ++-- pygmt/src/grdvolume.py | 4 ++-- pygmt/src/triangulate.py | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pygmt/helpers/__init__.py b/pygmt/helpers/__init__.py index bd3b9680257..8b8891cb63f 100644 --- a/pygmt/helpers/__init__.py +++ b/pygmt/helpers/__init__.py @@ -21,4 +21,4 @@ launch_external_viewer, non_ascii_to_octal, ) -from pygmt.helpers.validators import validate_output_type +from pygmt.helpers.validators import validate_output_table_type diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py index 9e204af8c59..c400163fee8 100644 --- a/pygmt/helpers/validators.py +++ b/pygmt/helpers/validators.py @@ -6,7 +6,7 @@ from pygmt.exceptions import GMTInvalidInput -def validate_output_type(output_type, outfile=None): +def validate_output_table_type(output_type, outfile=None): """ Check if the ``output_type`` and ``outfile`` parameters are valid. diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index 1a82a88a694..f9555c3db86 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -10,7 +10,7 @@ build_arg_string, fmt_docstring, use_alias, - validate_output_type, + validate_output_table_type, ) @@ -115,7 +115,7 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs): if kwargs.get("F") is None: raise GMTInvalidInput("Pass a required argument to 'filter_type'.") - output_type = validate_output_type(output_type, outfile=outfile) + output_type = validate_output_table_type(output_type, outfile=outfile) with GMTTempFile() as tmpfile: with Session() as lib: diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index 236290c698f..e339573b46f 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -12,7 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, - validate_output_type, + validate_output_table_type, ) __doctest_skip__ = ["grd2xyz"] @@ -143,7 +143,7 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): 0 10.0 25.0 863.0 1 10.5 25.0 985.5 """ - output_type = validate_output_type(output_type, outfile=outfile) + output_type = validate_output_table_type(output_type, outfile=outfile) if kwargs.get("o") is not None and output_type == "pandas": raise GMTInvalidInput( diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index e66ebe79daa..cf5a9ada852 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -12,7 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, - validate_output_type, + validate_output_table_type, ) from pygmt.io import load_dataarray @@ -321,7 +321,7 @@ def compute_bins( This method does a weighted histogram equalization for geographic grids to account for node area varying with latitude. """ - output_type = validate_output_type(output_type, outfile=outfile) + output_type = validate_output_table_type(output_type, outfile=outfile) if header is not None and output_type != "file": raise GMTInvalidInput("'header' is only allowed with output_type='file'.") diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index 77f7881b8ff..a38b257b263 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -9,7 +9,7 @@ fmt_docstring, kwargs_to_strings, use_alias, - validate_output_type, + validate_output_table_type, ) __doctest_skip__ = ["grdvolume"] @@ -101,7 +101,7 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): 3 350 2.018302e+12 5.222640e+14 258.764032 4 400 1.857370e+12 4.252699e+14 228.963499 """ - output_type = validate_output_type(output_type, outfile=outfile) + output_type = validate_output_table_type(output_type, outfile=outfile) with GMTTempFile() as tmpfile: with Session() as lib: diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index 60661b1242a..8c29324524a 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -12,7 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, - validate_output_type, + validate_output_table_type, ) from pygmt.io import load_dataarray @@ -357,7 +357,7 @@ def delaunay_triples( # pylint: disable=too-many-arguments,too-many-locals ``triangulate`` is a Cartesian or small-geographic area operator and is unaware of periodic or polar boundary conditions. """ - output_type = validate_output_type(output_type, outfile) + output_type = validate_output_table_type(output_type, outfile) # Return a pandas.DataFrame if ``outfile`` is not set with GMTTempFile(suffix=".txt") as tmpfile: