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

Add the validate_output_table_type function to check the output_type parameter #2772

Merged
merged 5 commits into from
Oct 30, 2023
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
1 change: 1 addition & 0 deletions pygmt/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
launch_external_viewer,
non_ascii_to_octal,
)
from pygmt.helpers.validators import validate_output_table_type
41 changes: 41 additions & 0 deletions pygmt/helpers/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Functions to check if given arguments are valid.
"""
import warnings

from pygmt.exceptions import GMTInvalidInput


def validate_output_table_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
23 changes: 9 additions & 14 deletions pygmt/src/filter1d.py
Original file line number Diff line number Diff line change
@@ -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_table_type,
)


@fmt_docstring
Expand Down Expand Up @@ -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_table_type(output_type, outfile=outfile)

with GMTTempFile() as tmpfile:
with Session() as lib:
Expand Down
18 changes: 2 additions & 16 deletions pygmt/src/grd2xyz.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
grd2xyz - Convert grid to data table
"""
import warnings

import pandas as pd
import xarray as xr
Expand All @@ -13,6 +12,7 @@
fmt_docstring,
kwargs_to_strings,
use_alias,
validate_output_table_type,
)

__doctest_skip__ = ["grd2xyz"]
Expand Down Expand Up @@ -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_table_type(output_type, outfile=outfile)

if kwargs.get("o") is not None and output_type == "pandas":
raise GMTInvalidInput(
Expand Down
16 changes: 2 additions & 14 deletions pygmt/src/grdhisteq.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
grdhisteq - Perform histogram equalization for a grid.
"""
import warnings

import numpy as np
import pandas as pd
Expand All @@ -13,6 +12,7 @@
fmt_docstring,
kwargs_to_strings,
use_alias,
validate_output_table_type,
)
from pygmt.io import load_dataarray

Expand Down Expand Up @@ -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_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'.")

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
Expand Down
9 changes: 2 additions & 7 deletions pygmt/src/grdvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_table_type,
)

__doctest_skip__ = ["grdvolume"]
Expand Down Expand Up @@ -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_table_type(output_type, outfile=outfile)

with GMTTempFile() as tmpfile:
with Session() as lib:
Expand Down
17 changes: 2 additions & 15 deletions pygmt/src/triangulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +12,7 @@
fmt_docstring,
kwargs_to_strings,
use_alias,
validate_output_table_type,
)
from pygmt.io import load_dataarray

Expand Down Expand Up @@ -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_table_type(output_type, outfile)

# Return a pandas.DataFrame if ``outfile`` is not set
with GMTTempFile(suffix=".txt") as tmpfile:
Expand Down