From 481b19398325c3606632d2a7efbc1e82134f4455 Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Thu, 19 Aug 2021 16:32:12 -0600 Subject: [PATCH 01/12] Wrap blockmode --- pygmt/__init__.py | 1 + pygmt/src/__init__.py | 2 +- pygmt/src/blockm.py | 87 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 25ad96a2127..f13c8dfe0a4 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -31,6 +31,7 @@ from pygmt.src import ( blockmean, blockmedian, + blockmode, config, grd2cpt, grdclip, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 2e1f8b56186..d882a3e6df1 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -4,7 +4,7 @@ # pylint: disable=import-outside-toplevel from pygmt.src.basemap import basemap -from pygmt.src.blockm import blockmean, blockmedian +from pygmt.src.blockm import blockmean, blockmedian, blockmode from pygmt.src.coast import coast from pygmt.src.colorbar import colorbar from pygmt.src.config import config diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index 22f6cbd43c5..0d2c604866b 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -1,5 +1,5 @@ """ -blockm - Block average (x,y,z) data tables by mean or median estimation. +blockm - Block average (x,y,z) data tables by mean, median, or mode estimation. """ import pandas as pd from pygmt.clib import Session @@ -14,18 +14,18 @@ def _blockm(block_method, table, outfile, x, y, z, **kwargs): r""" - Block average (x,y,z) data tables by mean or median estimation. + Block average (x,y,z) data tables by mean, median, or mode estimation. Reads arbitrarily located (x,y,z) triples [or optionally weighted - quadruples (x,y,z,w)] from a table and writes to the output a mean or - median (depending on ``block_method``) position and value for every + quadruples (x,y,z,w)] from a table and writes to the output a mean, + median, or mode (depending on ``block_method``) position and value for every non-empty block in a grid region defined by the ``region`` and ``spacing`` parameters. Parameters ---------- block_method : str - Name of the GMT module to call. Must be "blockmean" or "blockmedian". + Name of the GMT module to call. Must be "blockmean" "blockmedian" or "blockmode". Returns ------- @@ -210,3 +210,80 @@ def blockmedian(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): z=z, **kwargs ) + + +@fmt_docstring +@use_alias( + I="spacing", + R="region", + V="verbose", + a="aspatial", + f="coltypes", + i="incols", + o="outcols", + r="registration", + s="skiprows", + w="wrap", +) +@kwargs_to_strings(R="sequence") +def blockmode(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): + r""" + Block average (x,y,z) data tables by mode estimation. + + Reads arbitrarily located (x,y,z) triples [or optionally weighted + quadruples (x,y,z,w)] and writes to the output a mode position and value + for every non-empty block in a grid region defined by the ``region`` and + ``spacing`` parameters. + + Takes a matrix, xyz triplets, or a file name as input. + + Must provide either ``table`` or ``x``, ``y``, and ``z``. + + Full option list at :gmt-docs:`blockmode.html` + + {aliases} + + Parameters + ---------- + table : str or {table-like} + Pass in (x, y, z) or (longitude, latitude, elevation) values by + providing a file name to an ASCII data table, a 2D + {table-classes}. + x/y/z : 1d arrays + Arrays of x and y coordinates and values z of the data points. + + {I} + + {R} + + outfile : str + The file name for the output ASCII file. + + {V} + {a} + {f} + {i} + {o} + {r} + {s} + {w} + + Returns + ------- + output : pandas.DataFrame or None + Return type depends on whether the ``outfile`` parameter is set: + + - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile`` + is not set. + - None if ``outfile`` is set (filtered output will be stored in file + set by ``outfile``). + """ + return _blockm( + block_method="blockmode", + table=table, + outfile=outfile, + x=x, + y=y, + z=z, + **kwargs + ) From ec4c81da7e98f4f25697e72b6cb54359b524167f Mon Sep 17 00:00:00 2001 From: actions-bot <58130806+actions-bot@users.noreply.github.com> Date: Thu, 19 Aug 2021 23:19:01 +0000 Subject: [PATCH 02/12] [format-command] fixes --- pygmt/src/blockm.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index 0d2c604866b..a2405d9b835 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -279,11 +279,5 @@ def blockmode(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): set by ``outfile``). """ return _blockm( - block_method="blockmode", - table=table, - outfile=outfile, - x=x, - y=y, - z=z, - **kwargs + block_method="blockmode", table=table, outfile=outfile, x=x, y=y, z=z, **kwargs ) From 8a33395fe0635c361e2b1b76692907590fbe1402 Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Wed, 1 Sep 2021 09:48:49 -0600 Subject: [PATCH 03/12] Update pygmt/src/blockm.py Co-authored-by: Meghan Jones --- pygmt/src/blockm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index a2405d9b835..b326f714143 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -25,7 +25,8 @@ def _blockm(block_method, table, outfile, x, y, z, **kwargs): Parameters ---------- block_method : str - Name of the GMT module to call. Must be "blockmean" "blockmedian" or "blockmode". + Name of the GMT module to call. Must be "blockmean" "blockmedian" or + "blockmode". Returns ------- From 1b55f258240143b18d83cde12effdae2d2f88dac Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Wed, 1 Sep 2021 09:49:12 -0600 Subject: [PATCH 04/12] Update pygmt/src/blockm.py Co-authored-by: Meghan Jones --- pygmt/src/blockm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index b326f714143..b1e23ce04b7 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -18,9 +18,9 @@ def _blockm(block_method, table, outfile, x, y, z, **kwargs): Reads arbitrarily located (x,y,z) triples [or optionally weighted quadruples (x,y,z,w)] from a table and writes to the output a mean, - median, or mode (depending on ``block_method``) position and value for every - non-empty block in a grid region defined by the ``region`` and ``spacing`` - parameters. + median, or mode (depending on ``block_method``) position and value for + every non-empty block in a grid region defined by the ``region`` and + ``spacing`` parameters. Parameters ---------- From 0c1fa86ba3d65ff01651407b42f61152c510988a Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Wed, 1 Sep 2021 09:52:55 -0600 Subject: [PATCH 05/12] Update index.rst --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 8e888419b57..b49ebddf65b 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -81,6 +81,7 @@ Operations on tabular data: blockmean blockmedian + blockmode surface Operations on grids: From e33bf3ae8f3eb8aff8f409770568b0bfdd08abe5 Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Wed, 1 Sep 2021 16:09:34 -0600 Subject: [PATCH 06/12] Update pygmt/src/blockm.py Co-authored-by: Dongdong Tian --- pygmt/src/blockm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index cc75789af14..b1866d9dd7d 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -25,7 +25,7 @@ def _blockm(block_method, table, outfile, x, y, z, **kwargs): Parameters ---------- block_method : str - Name of the GMT module to call. Must be "blockmean" "blockmedian" or + Name of the GMT module to call. Must be "blockmean", "blockmedian" or "blockmode". Returns From 1c7df5d937ab932ad73a16916975f7f56f67287e Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Wed, 1 Sep 2021 16:09:51 -0600 Subject: [PATCH 07/12] Update pygmt/src/blockm.py Co-authored-by: Dongdong Tian --- pygmt/src/blockm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index b1866d9dd7d..6a49eef3b2c 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -226,7 +226,7 @@ def blockmedian(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): s="skiprows", w="wrap", ) -@kwargs_to_strings(R="sequence") +@kwargs_to_strings(R="sequence", i="sequence_comma") def blockmode(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): r""" Block average (x,y,z) data tables by mode estimation. From 025e4bf9acd29967b60fe8d917a09f3af2b78df8 Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Mon, 13 Sep 2021 12:32:46 -0600 Subject: [PATCH 08/12] Rename test_blockmean.py to test_blockm.py --- pygmt/tests/{test_blockmean.py => test_blockm.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pygmt/tests/{test_blockmean.py => test_blockm.py} (100%) diff --git a/pygmt/tests/test_blockmean.py b/pygmt/tests/test_blockm.py similarity index 100% rename from pygmt/tests/test_blockmean.py rename to pygmt/tests/test_blockm.py From ef6f3b8e9d6546bce3e6bf984d0bee0c95c6b77c Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Mon, 13 Sep 2021 12:52:46 -0600 Subject: [PATCH 09/12] Add blockmode test --- pygmt/tests/test_blockm.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_blockm.py b/pygmt/tests/test_blockm.py index 67f232a6c6e..e54a8638a56 100644 --- a/pygmt/tests/test_blockm.py +++ b/pygmt/tests/test_blockm.py @@ -1,12 +1,12 @@ """ -Tests for blockmean. +Tests for blockmean and blockmode. """ import os import numpy.testing as npt import pandas as pd import pytest -from pygmt import blockmean +from pygmt import blockmean, blockmode from pygmt.datasets import load_sample_bathymetry from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile, data_kind @@ -96,3 +96,14 @@ def test_blockmean_without_outfile_setting(): assert isinstance(output, pd.DataFrame) assert output.shape == (5849, 3) npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -384.0]) + + +def test_blockmode_input_dataframe(dataframe): + """ + Run blockmode by passing in a pandas.DataFrame as input. + """ + output = blockmode(table=dataframe, spacing="5m", region=[245, 255, 20, 30]) + assert isinstance(output, pd.DataFrame) + assert all(dataframe.columns == output.columns) + assert output.shape == (5849, 3) + npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -385.0]) From 3cd5fb35bd03907dfa725f56ad039f050671159f Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Mon, 13 Sep 2021 14:53:18 -0600 Subject: [PATCH 10/12] Update pygmt/tests/test_blockm.py Co-authored-by: Meghan Jones --- pygmt/tests/test_blockm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_blockm.py b/pygmt/tests/test_blockm.py index e54a8638a56..39a0e315f7f 100644 --- a/pygmt/tests/test_blockm.py +++ b/pygmt/tests/test_blockm.py @@ -106,4 +106,4 @@ def test_blockmode_input_dataframe(dataframe): assert isinstance(output, pd.DataFrame) assert all(dataframe.columns == output.columns) assert output.shape == (5849, 3) - npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -385.0]) + npt.assert_allclose(output.iloc[0], [245.88819, 29.97895, -385.0]) From fcb37ae53440a997a7a154819400067d09244a30 Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Mon, 13 Sep 2021 14:54:01 -0600 Subject: [PATCH 11/12] Update pygmt/src/blockm.py Co-authored-by: Meghan Jones --- pygmt/src/blockm.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index 6a49eef3b2c..edbd0a1797e 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -219,7 +219,11 @@ def blockmedian(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): R="region", V="verbose", a="aspatial", + b="binary", + d="data", + e="find", f="coltypes", + h="header", i="incols", o="outcols", r="registration", From c9b94d1ea2e6dd9b0703a5a9c4325da7681d6405 Mon Sep 17 00:00:00 2001 From: Amanda Leaman <77039253+arleaman@users.noreply.github.com> Date: Mon, 13 Sep 2021 14:54:16 -0600 Subject: [PATCH 12/12] Update pygmt/src/blockm.py Co-authored-by: Meghan Jones --- pygmt/src/blockm.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pygmt/src/blockm.py b/pygmt/src/blockm.py index edbd0a1797e..47060a92364 100644 --- a/pygmt/src/blockm.py +++ b/pygmt/src/blockm.py @@ -266,7 +266,11 @@ def blockmode(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs): {V} {a} + {b} + {d} + {e} {f} + {h} {i} {o} {r}