From ce08fd26ae5e910090b87603ccf997003f832a5b Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 22 Feb 2022 17:35:30 +0000 Subject: [PATCH 01/12] change import --- pygmt/tests/test_grdtrack.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 242ae2a9ee4..43b9f9b3bf2 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -8,7 +8,8 @@ import pytest from packaging.version import Version from pygmt import clib, grdtrack, which -from pygmt.datasets import load_earth_relief, load_ocean_ridge_points +from pygmt.datasets import load_sample_data +from pygmt.helpers.testing import load_static_earth_relief from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import data_kind @@ -24,9 +25,7 @@ def fixture_dataarray(): """ Load the grid data from the sample earth_relief file. """ - return load_earth_relief(registration="gridline").sel( - lat=slice(-49, -42), lon=slice(-118, -107) - ) + return load_static_earth_relief() @pytest.fixture(scope="module", name="dataframe") @@ -34,7 +33,7 @@ def fixture_dataframe(): """ Load the ocean ridge file. """ - return load_ocean_ridge_points() + return load_sample_data("ocean_ridge_points") @pytest.fixture(scope="module", name="csvfile") @@ -50,7 +49,7 @@ def fixture_ncfile(): """ Load the ncfile. """ - return which("@earth_relief_01d", download="a") + return which("@static_earth_relief", download="a") def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe): From 550324252de2bc213617874a7c1a4a3a415ea3b8 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 22 Feb 2022 18:10:13 +0000 Subject: [PATCH 02/12] add static data within the range of static_earth_relief --- pygmt/tests/test_grdtrack.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 43b9f9b3bf2..380a960eef1 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -9,9 +9,9 @@ from packaging.version import Version from pygmt import clib, grdtrack, which from pygmt.datasets import load_sample_data -from pygmt.helpers.testing import load_static_earth_relief from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import data_kind +from pygmt.helpers.testing import load_static_earth_relief TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") TEMP_TRACK = os.path.join(TEST_DATA_DIR, "tmp_track.txt") @@ -31,9 +31,26 @@ def fixture_dataarray(): @pytest.fixture(scope="module", name="dataframe") def fixture_dataframe(): """ - Load the ocean ridge file. - """ - return load_sample_data("ocean_ridge_points") + Load a pandas DataFrame with points. + """ + points = [ + [-51.613, -17.93], + [-48.917, -22.434], + [-50.444, -16.358], + [-50.721, -16.628], + [-51.394, -12.196], + [-50.207, -18.404], + [-52.56, -16.977], + [-51.866, -19.794], + [-48.001, -14.144], + [-54.438, -19.193], + [-52.315, -17.755], + [-49.37, -16.645], + [-49.945, -17.345], + [-47.583, -13.467], + [-53.756, -17.869], + ] + return pd.DataFrame(data=points, columns=["x", "y"]) @pytest.fixture(scope="module", name="csvfile") From 31f39f5a6da5902da87843684edbda88f7ff5eaa Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 22 Feb 2022 18:12:43 +0000 Subject: [PATCH 03/12] remove csvfile import --- pygmt/tests/test_grdtrack.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 380a960eef1..252f9defa1b 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -52,15 +52,6 @@ def fixture_dataframe(): ] return pd.DataFrame(data=points, columns=["x", "y"]) - -@pytest.fixture(scope="module", name="csvfile") -def fixture_csvfile(): - """ - Load the csvfile. - """ - return which("@ridge.txt", download="c") - - @pytest.fixture(scope="module", name="ncfile") def fixture_ncfile(): """ From 04ec26b774f6976304c66c4e3642f6c4b34f6d33 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 22 Feb 2022 18:46:30 +0000 Subject: [PATCH 04/12] remove direct imports of ncfile; add expected_array output --- pygmt/tests/test_grdtrack.py | 96 +++++++++++------------------------- 1 file changed, 28 insertions(+), 68 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 252f9defa1b..974aea1fcb3 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -3,6 +3,7 @@ """ import os +import numpy as np import numpy.testing as npt import pandas as pd import pytest @@ -28,8 +29,28 @@ def fixture_dataarray(): return load_static_earth_relief() +@pytest.fixture(scope="module", name="expected_array") +def fixture_numpy_array(): + """ + Load a numpy array with x, y, and bathymetry data. + """ + array = [ + [-51.613, -17.93, 796.59434514], + [-48.917, -22.434, 566.49184359], + [-50.444, -16.358, 571.1492788], + [-50.721, -16.628, 578.76116859], + [-51.394, -12.196, 274.43205501], + [-50.207, -18.404, 532.11444935], + [-52.56, -16.977, 670.16934401], + [-51.866, -19.794, 426.77300768], + [-48.001, -14.144, 741.35824074], + [-54.438, -19.193, 490.02716679], + ] + return array + + @pytest.fixture(scope="module", name="dataframe") -def fixture_dataframe(): +def fixture_dataframe(expected_array): """ Load a pandas DataFrame with points. """ @@ -44,23 +65,11 @@ def fixture_dataframe(): [-51.866, -19.794], [-48.001, -14.144], [-54.438, -19.193], - [-52.315, -17.755], - [-49.37, -16.645], - [-49.945, -17.345], - [-47.583, -13.467], - [-53.756, -17.869], ] - return pd.DataFrame(data=points, columns=["x", "y"]) + return pd.DataFrame(data=points, columns=["longitude", "latitude"]) -@pytest.fixture(scope="module", name="ncfile") -def fixture_ncfile(): - """ - Load the ncfile. - """ - return which("@static_earth_relief", download="a") - -def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe): +def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_array): """ Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as inputs. @@ -68,54 +77,7 @@ def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe): output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry") assert isinstance(output, pd.DataFrame) assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] - npt.assert_allclose(output.iloc[0], [-110.9536, -42.2489, -2797.394987]) - - return output - - -def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile): - """ - Run grdtrack by passing in a csvfile and xarray.DataArray as inputs. - """ - try: - output = grdtrack(points=csvfile, grid=dataarray, outfile=TEMP_TRACK) - assert output is None # check that output is None since outfile is set - assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path - - track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">") - npt.assert_allclose(track.iloc[0], [-110.9536, -42.2489, -2797.394987]) - finally: - os.remove(path=TEMP_TRACK) - - return output - - -def test_grdtrack_input_dataframe_and_ncfile(dataframe, ncfile): - """ - Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs. - """ - - output = grdtrack(points=dataframe, grid=ncfile, newcolname="bathymetry") - assert isinstance(output, pd.DataFrame) - assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] - npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245]) - - return output - - -def test_grdtrack_input_csvfile_and_ncfile(csvfile, ncfile): - """ - Run grdtrack by passing in a csvfile and netcdf file as inputs. - """ - try: - output = grdtrack(points=csvfile, grid=ncfile, outfile=TEMP_TRACK) - assert output is None # check that output is None since outfile is set - assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path - - track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">") - npt.assert_allclose(track.iloc[0], [-32.2971, 37.4118, -1939.748245]) - finally: - os.remove(path=TEMP_TRACK) + npt.assert_allclose(np.array(output), expected_array) return output @@ -152,11 +114,9 @@ def test_grdtrack_without_newcolname_setting(dataarray, dataframe): grdtrack(points=dataframe, grid=dataarray) -def test_grdtrack_without_outfile_setting(csvfile, ncfile): +def test_grdtrack_without_outfile_setting(dataarray, dataframe): """ Run grdtrack by not passing in outfile parameter setting. """ - output = grdtrack(points=csvfile, grid=ncfile) - npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245]) - - return output + with pytest.raises(GMTInvalidInput): + grdtrack(points=dataframe, grid=dataarray) From 59f510a954239067d60cded40916f7a2498d4f43 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Tue, 1 Mar 2022 10:22:41 -0500 Subject: [PATCH 05/12] Apply suggestions from code review --- pygmt/tests/test_grdtrack.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 974aea1fcb3..a0a7aaa3ee7 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -8,8 +8,7 @@ import pandas as pd import pytest from packaging.version import Version -from pygmt import clib, grdtrack, which -from pygmt.datasets import load_sample_data +from pygmt import clib, grdtrack from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import data_kind from pygmt.helpers.testing import load_static_earth_relief @@ -50,7 +49,7 @@ def fixture_numpy_array(): @pytest.fixture(scope="module", name="dataframe") -def fixture_dataframe(expected_array): +def fixture_dataframe(): """ Load a pandas DataFrame with points. """ From 44f789ab9be93770240a73baa551cacbad1789ec Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Tue, 1 Mar 2022 10:42:33 -0500 Subject: [PATCH 06/12] Remove unused objects --- pygmt/tests/test_grdtrack.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index d5a70f29c0b..221326ca5a6 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -1,7 +1,6 @@ """ Tests for grdtrack. """ -import os import numpy as np import numpy.testing as npt @@ -12,9 +11,6 @@ from pygmt.helpers import data_kind from pygmt.helpers.testing import load_static_earth_relief -TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") -TEMP_TRACK = os.path.join(TEST_DATA_DIR, "tmp_track.txt") - @pytest.fixture(scope="module", name="dataarray") def fixture_dataarray(): From ad51f9a3acae76778fb67ef176d88430d759abf8 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Wed, 2 Mar 2022 15:42:36 -0500 Subject: [PATCH 07/12] Add file with data for grdtrack tests --- pygmt/tests/data/track.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 pygmt/tests/data/track.txt diff --git a/pygmt/tests/data/track.txt b/pygmt/tests/data/track.txt new file mode 100644 index 00000000000..27fe93c40a5 --- /dev/null +++ b/pygmt/tests/data/track.txt @@ -0,0 +1,10 @@ +-51.613 -17.93 +-48.917 -22.434 +-50.444 -16.358 +-50.721 -16.628 +-51.394 -12.196 +-50.207 -18.404 +-52.56 -16.977 +-51.866 -19.794 +-48.001 -14.144 +-54.438 -19.193 From 963a56c124b0837e824d438c18ab81b820e14eb3 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Wed, 2 Mar 2022 15:43:26 -0500 Subject: [PATCH 08/12] Modify grdtrack tests to use data file --- pygmt/tests/test_grdtrack.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 221326ca5a6..653c76b115e 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -1,6 +1,7 @@ """ Tests for grdtrack. """ +import os import numpy as np import numpy.testing as npt @@ -11,6 +12,9 @@ from pygmt.helpers import data_kind from pygmt.helpers.testing import load_static_earth_relief +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") +POINTS_DATA = os.path.join(TEST_DATA_DIR, "track.txt") + @pytest.fixture(scope="module", name="dataarray") def fixture_dataarray(): @@ -45,19 +49,9 @@ def fixture_dataframe(): """ Load a pandas DataFrame with points. """ - points = [ - [-51.613, -17.93], - [-48.917, -22.434], - [-50.444, -16.358], - [-50.721, -16.628], - [-51.394, -12.196], - [-50.207, -18.404], - [-52.56, -16.977], - [-51.866, -19.794], - [-48.001, -14.144], - [-54.438, -19.193], - ] - return pd.DataFrame(data=points, columns=["longitude", "latitude"]) + return pd.read_csv( + POINTS_DATA, sep=r"\s+", header=None, names=["longitude", "latitude"] + ) def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_array): From 2851a3fd55a6026881c30c599c0fdc0eff0225dd Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Wed, 2 Mar 2022 15:45:46 -0500 Subject: [PATCH 09/12] Remove unnecessary return --- pygmt/tests/test_grdtrack.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 653c76b115e..a1066101ee3 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -64,8 +64,6 @@ def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_a assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] npt.assert_allclose(np.array(output), expected_array) - return output - def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): """ From e1848030bd76c54562587d1c73890cb7efb39ca0 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Wed, 2 Mar 2022 15:52:05 -0500 Subject: [PATCH 10/12] Add test_grdtrack_input_csvfile_and_dataarray --- pygmt/tests/test_grdtrack.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index a1066101ee3..7996765fbb3 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -9,7 +9,7 @@ import pytest from pygmt import grdtrack from pygmt.exceptions import GMTInvalidInput -from pygmt.helpers import data_kind +from pygmt.helpers import GMTTempFile, data_kind from pygmt.helpers.testing import load_static_earth_relief TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") @@ -65,6 +65,18 @@ def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_a npt.assert_allclose(np.array(output), expected_array) +def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array): + """ + Run grdtrack by passing in a csvfile and xarray.DataArray as inputs. + """ + with GMTTempFile() as tmpfile: + output = grdtrack(points=POINTS_DATA, grid=dataarray, 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 = np.loadtxt(tmpfile.name) + npt.assert_allclose(np.array(output), expected_array) + + def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): """ Run grdtrack using points input that is not a pandas.DataFrame (matrix) or From 396f21a24c230e9ad465bebe6e09a35412666076 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Wed, 2 Mar 2022 15:54:55 -0500 Subject: [PATCH 11/12] Add test_grdtrack_input_dataframe_and_ncfile --- pygmt/tests/test_grdtrack.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 7996765fbb3..359da3d785b 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -77,6 +77,18 @@ def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array): npt.assert_allclose(np.array(output), expected_array) +def test_grdtrack_input_dataframe_and_ncfile(dataframe, expected_array): + """ + Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs. + """ + output = grdtrack( + points=dataframe, grid="@static_earth_relief.nc", newcolname="bathymetry" + ) + assert isinstance(output, pd.DataFrame) + assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] + npt.assert_allclose(np.array(output), expected_array) + + def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): """ Run grdtrack using points input that is not a pandas.DataFrame (matrix) or From bb6e72bb467c2acd1b4f19d486aeb532352a2cf8 Mon Sep 17 00:00:00 2001 From: Meghan Jones Date: Wed, 2 Mar 2022 15:58:00 -0500 Subject: [PATCH 12/12] Add test_grdtrack_input_csvfile_and_ncfile --- pygmt/tests/test_grdtrack.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 359da3d785b..70c76d1beff 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -89,6 +89,20 @@ def test_grdtrack_input_dataframe_and_ncfile(dataframe, expected_array): npt.assert_allclose(np.array(output), expected_array) +def test_grdtrack_input_csvfile_and_ncfile(expected_array): + """ + Run grdtrack by passing in a csvfile and netcdf file as inputs. + """ + with GMTTempFile() as tmpfile: + output = grdtrack( + points=POINTS_DATA, grid="@static_earth_relief.nc", 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 = np.loadtxt(tmpfile.name) + npt.assert_allclose(np.array(output), expected_array) + + def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): """ Run grdtrack using points input that is not a pandas.DataFrame (matrix) or