Skip to content

Commit

Permalink
pygmt.triangulate.delaunay_triples: Add 'output_type' parameter for o…
Browse files Browse the repository at this point in the history
…utput in pandas/numpy/file formats
  • Loading branch information
seisman committed Mar 13, 2024
1 parent 3a507a8 commit 0d24a35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 59 deletions.
59 changes: 22 additions & 37 deletions pygmt/src/triangulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Cartesian data.
"""

from typing import Literal

import numpy as np
import pandas as pd
from pygmt.clib import Session
from pygmt.helpers import (
Expand Down Expand Up @@ -172,10 +175,10 @@ def delaunay_triples(
y=None,
z=None,
*,
output_type="pandas",
outfile=None,
output_type: Literal["pandas", "numpy", "file"] = "pandas",
outfile: str | None = None,
**kwargs,
):
) -> pd.DataFrame | np.ndarray | None:
"""
Delaunay triangle based gridding of Cartesian data.
Expand Down Expand Up @@ -204,16 +207,8 @@ def delaunay_triples(
{table-classes}.
{projection}
{region}
outfile : str or None
The name of the output ASCII file to store the results of the
histogram equalization in.
output_type : str
Determine the format the xyz data will be returned in [Default is
``pandas``]:
- ``numpy`` - :class:`numpy.ndarray`
- ``pandas``- :class:`pandas.DataFrame`
- ``file`` - ASCII file (requires ``outfile``)
{output_type}
{outfile}
{verbose}
{binary}
{nodata}
Expand All @@ -226,13 +221,13 @@ def delaunay_triples(
Returns
-------
ret : pandas.DataFrame or numpy.ndarray or None
ret
Return type depends on ``outfile`` and ``output_type``:
- None if ``outfile`` is set (output will be stored in file set by
``outfile``)
- :class:`pandas.DataFrame` or :class:`numpy.ndarray` if
``outfile`` is not set (depends on ``output_type``)
- :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not
set (depends on ``output_type``)
Note
----
Expand All @@ -243,25 +238,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.virtualfile_to_dataset(output_type=output_type, vfile=vouttbl)
22 changes: 0 additions & 22 deletions pygmt/tests/test_triangulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,6 @@ def test_delaunay_triples_ndarray_output(dataframe, expected_dataframe):
np.testing.assert_allclose(actual=output, desired=expected_dataframe.to_numpy())


def test_delaunay_triples_outfile(dataframe, expected_dataframe):
"""
Test triangulate.delaunay_triples with ``outfile``.
"""
with GMTTempFile(suffix=".txt") as tmpfile:
with pytest.warns(RuntimeWarning) as record:
result = triangulate.delaunay_triples(data=dataframe, outfile=tmpfile.name)
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)
pd.testing.assert_frame_equal(left=temp_df, right=expected_dataframe)


def test_delaunay_triples_invalid_format(dataframe):
"""
Test that triangulate.delaunay_triples fails with incorrect format.
"""
with pytest.raises(GMTInvalidInput):
triangulate.delaunay_triples(data=dataframe, output_type=1)


@pytest.mark.benchmark
def test_regular_grid_no_outgrid(dataframe, expected_grid):
"""
Expand Down

0 comments on commit 0d24a35

Please sign in to comment.