forked from GenericMappingTools/pygmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap blockmean (GenericMappingTools#1092)
* Rename blockmedian.py to blockm.py * Refactor blockmedian to support mean and mode * Wrap method blockmean * Add tests for blockmean method Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
- Loading branch information
1 parent
1040926
commit af645c2
Showing
5 changed files
with
199 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ Operations on tabular data: | |
.. autosummary:: | ||
:toctree: generated | ||
|
||
blockmean | ||
blockmedian | ||
surface | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Tests for blockmean. | ||
""" | ||
import os | ||
|
||
import numpy.testing as npt | ||
import pandas as pd | ||
import pytest | ||
from pygmt import blockmean | ||
from pygmt.datasets import load_sample_bathymetry | ||
from pygmt.exceptions import GMTInvalidInput | ||
from pygmt.helpers import GMTTempFile, data_kind | ||
|
||
|
||
def test_blockmean_input_dataframe(): | ||
""" | ||
Run blockmean by passing in a pandas.DataFrame as input. | ||
""" | ||
dataframe = load_sample_bathymetry() | ||
output = blockmean(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, -384.0]) | ||
|
||
return output | ||
|
||
|
||
def test_blockmean_wrong_kind_of_input_table_matrix(): | ||
""" | ||
Run blockmean using table input that is not a pandas.DataFrame but still a | ||
matrix. | ||
""" | ||
dataframe = load_sample_bathymetry() | ||
invalid_table = dataframe.values | ||
assert data_kind(invalid_table) == "matrix" | ||
with pytest.raises(GMTInvalidInput): | ||
blockmean(table=invalid_table, spacing="5m", region=[245, 255, 20, 30]) | ||
|
||
|
||
def test_blockmean_wrong_kind_of_input_table_grid(): | ||
""" | ||
Run blockmean using table input that is not a pandas.DataFrame or file but | ||
a grid. | ||
""" | ||
dataframe = load_sample_bathymetry() | ||
invalid_table = dataframe.bathymetry.to_xarray() | ||
assert data_kind(invalid_table) == "grid" | ||
with pytest.raises(GMTInvalidInput): | ||
blockmean(table=invalid_table, spacing="5m", region=[245, 255, 20, 30]) | ||
|
||
|
||
def test_blockmean_input_filename(): | ||
""" | ||
Run blockmean by passing in an ASCII text file as input. | ||
""" | ||
with GMTTempFile() as tmpfile: | ||
output = blockmean( | ||
table="@tut_ship.xyz", | ||
spacing="5m", | ||
region=[245, 255, 20, 30], | ||
outfile=tmpfile.name, | ||
) | ||
assert output is None # check that output is None since outfile is set | ||
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path | ||
output = pd.read_csv(tmpfile.name, sep="\t", header=None) | ||
assert output.shape == (5849, 3) | ||
npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -384.0]) | ||
|
||
return output | ||
|
||
|
||
def test_blockmean_without_outfile_setting(): | ||
""" | ||
Run blockmean by not passing in outfile parameter setting. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
blockmean(table="@tut_ship.xyz", spacing="5m", region=[245, 255, 20, 30]) |