Skip to content

Make all functions/methods have consistent behavior for table output #3092

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

Closed
wants to merge 19 commits into from
Closed
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
87 changes: 56 additions & 31 deletions pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 output_type == "pandas" and isinstance(data, pd.DataFrame):
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.virtualfile_to_dataset(
output_type=output_type,
vfile=vouttbl,
column_names=column_names,
)


@fmt_docstring
Expand All @@ -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.

Expand Down Expand Up @@ -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,
)


Expand All @@ -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.

Expand Down Expand Up @@ -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,
)


Expand All @@ -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.

Expand Down Expand Up @@ -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,
)
46 changes: 20 additions & 26 deletions pygmt/src/grdhisteq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.virtualfile_to_dataset(
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
53 changes: 26 additions & 27 deletions pygmt/src/grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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 output_type == "pandas" and isinstance(points, pd.DataFrame):
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.virtualfile_to_dataset(
output_type=output_type,
vfile=vouttbl,
column_names=column_names,
)
31 changes: 10 additions & 21 deletions pygmt/src/grdvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.virtualfile_to_dataset(output_type=output_type, vfile=vouttbl)
Loading