Skip to content

Commit 4a42b1a

Browse files
committed
Autocorrect output_type to 'file' if outfile parameter is set
1 parent 05a3a08 commit 4a42b1a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pygmt/src/triangulate.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
triangulate - Delaunay triangulation or Voronoi partitioning and gridding of
33
Cartesian data.
44
"""
5+
import warnings
6+
57
import pandas as pd
68
from pygmt.clib import Session
79
from pygmt.exceptions import GMTInvalidInput
@@ -165,7 +167,7 @@ def regular_grid( # pylint: disable=too-many-arguments,too-many-locals
165167
registration=None,
166168
skiprows=None,
167169
wrap=None,
168-
**kwargs
170+
**kwargs,
169171
):
170172
"""
171173
Delaunay triangle based gridding of Cartesian data.
@@ -257,7 +259,7 @@ def regular_grid( # pylint: disable=too-many-arguments,too-many-locals
257259
registration=registration,
258260
skiprows=skiprows,
259261
wrap=wrap,
260-
**kwargs
262+
**kwargs,
261263
)
262264

263265
@staticmethod
@@ -281,7 +283,7 @@ def delaunay_triples( # pylint: disable=too-many-arguments,too-many-locals
281283
incols=None,
282284
skiprows=None,
283285
wrap=None,
284-
**kwargs
286+
**kwargs,
285287
):
286288
"""
287289
Delaunay triangle based gridding of Cartesian data.
@@ -344,6 +346,15 @@ def delaunay_triples( # pylint: disable=too-many-arguments,too-many-locals
344346
"Must specify 'output_type' either as 'numpy', 'pandas' or 'file'."
345347
)
346348

349+
if isinstance(outfile, str) and output_type != "file":
350+
msg = (
351+
f"Changing 'output_type' from '{output_type}' to 'file' "
352+
"since 'outfile' parameter is set. Please use output_type='file' "
353+
"to silence this warning."
354+
)
355+
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
356+
output_type = "file"
357+
347358
# Return a pandas.DataFrame if ``outfile`` is not set
348359
with GMTTempFile(suffix=".txt") as tmpfile:
349360
if output_type != "file":
@@ -366,5 +377,5 @@ def delaunay_triples( # pylint: disable=too-many-arguments,too-many-locals
366377
incols=incols,
367378
skiprows=skiprows,
368379
wrap=wrap,
369-
**kwargs
380+
**kwargs,
370381
)

pygmt/tests/test_triangulate.py

+14
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ def test_delaunay_triples_ndarray_output(dataframe, expected_dataframe):
107107
np.testing.assert_allclose(actual=output, desired=expected_dataframe.to_numpy())
108108

109109

110+
def test_delaunay_triples_outfile(dataframe, expected_dataframe):
111+
"""
112+
Test triangulate.delaunay_triples with ``outfile``.
113+
"""
114+
with GMTTempFile(suffix=".txt") as tmpfile:
115+
with pytest.warns(RuntimeWarning) as record:
116+
result = triangulate.delaunay_triples(data=dataframe, outfile=tmpfile.name)
117+
assert len(record) == 1 # check that only one warning was raised
118+
assert result is None # return value is None
119+
assert os.path.exists(path=tmpfile.name)
120+
temp_df = pd.read_csv(filepath_or_buffer=tmpfile.name, sep="\t", header=None)
121+
pd.testing.assert_frame_equal(left=temp_df, right=expected_dataframe)
122+
123+
110124
def test_delaunay_triples_invalid_format(dataframe):
111125
"""
112126
Test that triangulate.delaunay_triples fails with incorrect format.

0 commit comments

Comments
 (0)