Skip to content

pygmt.triangulate.delaunay_triples: Improve performance by storing output in virtual files #3107

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

Merged
merged 4 commits into from
Mar 15, 2024
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
61 changes: 23 additions & 38 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
- ``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, vfname=vouttbl)
7 changes: 5 additions & 2 deletions pygmt/tests/test_triangulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def fixture_expected_dataframe():
[4, 6, 1],
[3, 4, 2],
[9, 3, 8],
]
],
dtype=float,
)


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


Expand Down