From 74bbb5abba607ba3bb1fcd4891b19ddde5b59df9 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Sun, 13 Mar 2022 16:04:06 -0400 Subject: [PATCH 1/5] Standardize output types for grdhisteq --- pygmt/src/grdhisteq.py | 20 ++++++++++++-------- pygmt/tests/test_grdhisteq.py | 15 +++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 61de7503d4e..487958f5020 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -163,7 +163,7 @@ def equalize_grid( ---------- grid : str or xarray.DataArray The file name of the input grid or the grid loaded as a DataArray. - outgrid : str or bool or None + outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. divisions : int @@ -183,7 +183,7 @@ def equalize_grid( ret: xarray.DataArray or None Return type depends on the ``outgrid`` parameter: - - xarray.DataArray if ``outgrid`` is True or None + - xarray.DataArray if ``outgrid`` is None - None if ``outgrid`` is a str (grid output is stored in ``outgrid``) @@ -211,9 +211,13 @@ def equalize_grid( with GMTTempFile(suffix=".nc") as tmpfile: if isinstance(outgrid, str): output_type = "file" - else: + elif outgrid is None: output_type = "xarray" outgrid = tmpfile.name + else: + raise GMTInvalidInput( + "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." + ) return grdhisteq._grdhisteq( grid=grid, output_type=output_type, @@ -281,12 +285,12 @@ def compute_bins( Returns ------- - ret: pandas.DataFrame or None - Return type depends on the ``outfile`` parameter: - - - pandas.DataFrame if ``outfile`` is True or None - - None if ``outfile`` is a str (file output is stored in + ret : pandas.DataFrame or numpy.ndarray or None + 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``) Example ------- diff --git a/pygmt/tests/test_grdhisteq.py b/pygmt/tests/test_grdhisteq.py index 9a4fd004b3d..a812e0d5565 100644 --- a/pygmt/tests/test_grdhisteq.py +++ b/pygmt/tests/test_grdhisteq.py @@ -66,13 +66,12 @@ def test_equalize_grid_outgrid_file(grid, expected_grid, region): xr.testing.assert_allclose(a=temp_grid, b=expected_grid) -@pytest.mark.parametrize("outgrid", [True, None]) -def test_equalize_grid_outgrid(grid, outgrid, expected_grid, region): +def test_equalize_grid_outgrid(grid, expected_grid, region): """ - Test grdhisteq.equalize_grid with ``outgrid=True`` and ``outgrid=None``. + Test grdhisteq.equalize_grid with ``outgrid=None``. """ temp_grid = grdhisteq.equalize_grid( - grid=grid, divisions=2, region=region, outgrid=outgrid + grid=grid, divisions=2, region=region, outgrid=None ) assert temp_grid.gmt.gtype == 1 # Geographic grid assert temp_grid.gmt.registration == 1 # Pixel registration @@ -135,3 +134,11 @@ def test_compute_bins_invalid_format(grid): grdhisteq.compute_bins(grid=grid, output_type=1) with pytest.raises(GMTInvalidInput): grdhisteq.compute_bins(grid=grid, output_type="pandas", header="o+c") + + +def test_equalize_grid_invalid_format(grid): + """ + Test that equalize_grid fails with incorrect format. + """ + with pytest.raises(GMTInvalidInput): + grdhisteq.equalize_grid(grid=grid, outgrid=True) From ffd02b8f4177a7ffb2dda0cbe1e0ee1db84c943b Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Sun, 13 Mar 2022 16:24:03 -0400 Subject: [PATCH 2/5] Update pygmt/src/grdhisteq.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grdhisteq.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 487958f5020..696bb925c4b 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -287,6 +287,7 @@ def compute_bins( ------- ret : pandas.DataFrame or numpy.ndarray or None 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 From fac0aecf1acfa5948b68648ed923edca9263b734 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Sun, 13 Mar 2022 16:27:36 -0400 Subject: [PATCH 3/5] Fix default for outgrid --- pygmt/src/grdhisteq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 696bb925c4b..f92435f66d2 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -141,7 +141,7 @@ def _grdhisteq(grid, output_type, **kwargs): def equalize_grid( grid, *, - outgrid=True, + outgrid=None, divisions=None, region=None, gaussian=None, From 001fc853b857c76c1edafdce2f032545f373db09 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Sun, 13 Mar 2022 21:04:19 -0400 Subject: [PATCH 4/5] Update pygmt/src/grdhisteq.py Co-authored-by: Dongdong Tian --- pygmt/src/grdhisteq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index f92435f66d2..529484fa444 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -216,7 +216,7 @@ def equalize_grid( outgrid = tmpfile.name else: raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." + "Must specify 'outgrid' as a string or None." ) return grdhisteq._grdhisteq( grid=grid, From 2b0a16dad23538ea73f654f783d899f7e1cff9a5 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 13 Mar 2022 22:02:18 -0400 Subject: [PATCH 5/5] Update pygmt/src/grdhisteq.py --- pygmt/src/grdhisteq.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 529484fa444..f53b6d1c900 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -215,9 +215,7 @@ def equalize_grid( output_type = "xarray" outgrid = tmpfile.name else: - raise GMTInvalidInput( - "Must specify 'outgrid' as a string or None." - ) + raise GMTInvalidInput("Must specify 'outgrid' as a string or None.") return grdhisteq._grdhisteq( grid=grid, output_type=output_type,