Skip to content

Commit 94681bd

Browse files
Meghan Jonesweiji14seisman
authored
Standardize output types for grdhisteq methods (#1812)
Modifies grdhisteq methods, so that an `xarray.DataArray` is returned only if outgrid is None. Also updated the docstring returns section for compute_bins to clarify the dependence on output_type. Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent a88f1ac commit 94681bd

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

pygmt/src/grdhisteq.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _grdhisteq(grid, output_type, **kwargs):
141141
def equalize_grid(
142142
grid,
143143
*,
144-
outgrid=True,
144+
outgrid=None,
145145
divisions=None,
146146
region=None,
147147
gaussian=None,
@@ -163,7 +163,7 @@ def equalize_grid(
163163
----------
164164
grid : str or xarray.DataArray
165165
The file name of the input grid or the grid loaded as a DataArray.
166-
outgrid : str or bool or None
166+
outgrid : str or None
167167
The name of the output netCDF file with extension .nc to store the
168168
grid in.
169169
divisions : int
@@ -183,7 +183,7 @@ def equalize_grid(
183183
ret: xarray.DataArray or None
184184
Return type depends on the ``outgrid`` parameter:
185185
186-
- xarray.DataArray if ``outgrid`` is True or None
186+
- xarray.DataArray if ``outgrid`` is None
187187
- None if ``outgrid`` is a str (grid output is stored in
188188
``outgrid``)
189189
@@ -211,9 +211,11 @@ def equalize_grid(
211211
with GMTTempFile(suffix=".nc") as tmpfile:
212212
if isinstance(outgrid, str):
213213
output_type = "file"
214-
else:
214+
elif outgrid is None:
215215
output_type = "xarray"
216216
outgrid = tmpfile.name
217+
else:
218+
raise GMTInvalidInput("Must specify 'outgrid' as a string or None.")
217219
return grdhisteq._grdhisteq(
218220
grid=grid,
219221
output_type=output_type,
@@ -281,12 +283,13 @@ def compute_bins(
281283
282284
Returns
283285
-------
284-
ret: pandas.DataFrame or None
285-
Return type depends on the ``outfile`` parameter:
286+
ret : pandas.DataFrame or numpy.ndarray or None
287+
Return type depends on ``outfile`` and ``output_type``:
286288
287-
- pandas.DataFrame if ``outfile`` is True or None
288-
- None if ``outfile`` is a str (file output is stored in
289+
- None if ``outfile`` is set (output will be stored in file set by
289290
``outfile``)
291+
- :class:`pandas.DataFrame` or :class:`numpy.ndarray` if
292+
``outfile`` is not set (depends on ``output_type``)
290293
291294
Example
292295
-------

pygmt/tests/test_grdhisteq.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ def test_equalize_grid_outgrid_file(grid, expected_grid, region):
6666
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
6767

6868

69-
@pytest.mark.parametrize("outgrid", [True, None])
70-
def test_equalize_grid_outgrid(grid, outgrid, expected_grid, region):
69+
def test_equalize_grid_outgrid(grid, expected_grid, region):
7170
"""
72-
Test grdhisteq.equalize_grid with ``outgrid=True`` and ``outgrid=None``.
71+
Test grdhisteq.equalize_grid with ``outgrid=None``.
7372
"""
7473
temp_grid = grdhisteq.equalize_grid(
75-
grid=grid, divisions=2, region=region, outgrid=outgrid
74+
grid=grid, divisions=2, region=region, outgrid=None
7675
)
7776
assert temp_grid.gmt.gtype == 1 # Geographic grid
7877
assert temp_grid.gmt.registration == 1 # Pixel registration
@@ -135,3 +134,11 @@ def test_compute_bins_invalid_format(grid):
135134
grdhisteq.compute_bins(grid=grid, output_type=1)
136135
with pytest.raises(GMTInvalidInput):
137136
grdhisteq.compute_bins(grid=grid, output_type="pandas", header="o+c")
137+
138+
139+
def test_equalize_grid_invalid_format(grid):
140+
"""
141+
Test that equalize_grid fails with incorrect format.
142+
"""
143+
with pytest.raises(GMTInvalidInput):
144+
grdhisteq.equalize_grid(grid=grid, outgrid=True)

0 commit comments

Comments
 (0)