|
| 1 | +# pylint: disable=unused-argument |
| 2 | +""" |
| 3 | +Tests for x2sys_cross |
| 4 | +""" |
| 5 | +import os |
| 6 | +from tempfile import TemporaryDirectory |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import numpy.testing as npt |
| 10 | +import pandas as pd |
| 11 | +import pytest |
| 12 | + |
| 13 | +from .. import x2sys_cross, x2sys_init |
| 14 | +from ..datasets import load_sample_bathymetry |
| 15 | +from ..exceptions import GMTInvalidInput |
| 16 | +from ..helpers import data_kind |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(name="mock_x2sys_home") |
| 20 | +def fixture_mock_x2sys_home(monkeypatch): |
| 21 | + """ |
| 22 | + Set the X2SYS_HOME environment variable to the current working directory |
| 23 | + for the test session |
| 24 | + """ |
| 25 | + monkeypatch.setenv("X2SYS_HOME", os.getcwd()) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="module", name="tracks") |
| 29 | +def fixture_tracks(): |
| 30 | + """ |
| 31 | + Load track data from the sample bathymetry file |
| 32 | + """ |
| 33 | + dataframe = load_sample_bathymetry() |
| 34 | + return [dataframe.query(expr="bathymetry > -20")] # reduce size of dataset |
| 35 | + |
| 36 | + |
| 37 | +def test_x2sys_cross_input_file_output_file(mock_x2sys_home): |
| 38 | + """ |
| 39 | + Run x2sys_cross by passing in a filename, and output internal crossovers to |
| 40 | + an ASCII txt file |
| 41 | + """ |
| 42 | + with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir: |
| 43 | + tag = os.path.basename(tmpdir) |
| 44 | + x2sys_init(tag=tag, fmtfile="xyz", force=True) |
| 45 | + outfile = os.path.join(tmpdir, "tmp_coe.txt") |
| 46 | + output = x2sys_cross( |
| 47 | + tracks=["@tut_ship.xyz"], tag=tag, coe="i", outfile=outfile, verbose="i" |
| 48 | + ) |
| 49 | + |
| 50 | + assert output is None # check that output is None since outfile is set |
| 51 | + assert os.path.exists(path=outfile) # check that outfile exists at path |
| 52 | + _ = pd.read_csv(outfile, sep="\t", header=2) # ensure ASCII text file loads ok |
| 53 | + |
| 54 | + return output |
| 55 | + |
| 56 | + |
| 57 | +def test_x2sys_cross_input_file_output_dataframe(mock_x2sys_home): |
| 58 | + """ |
| 59 | + Run x2sys_cross by passing in a filename, and output internal crossovers to |
| 60 | + a pandas.DataFrame |
| 61 | + """ |
| 62 | + with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir: |
| 63 | + tag = os.path.basename(tmpdir) |
| 64 | + x2sys_init(tag=tag, fmtfile="xyz", force=True) |
| 65 | + output = x2sys_cross(tracks=["@tut_ship.xyz"], tag=tag, coe="i", verbose="i") |
| 66 | + |
| 67 | + assert isinstance(output, pd.DataFrame) |
| 68 | + assert output.shape == (14294, 12) |
| 69 | + columns = list(output.columns) |
| 70 | + assert columns[:6] == ["x", "y", "i_1", "i_2", "dist_1", "dist_2"] |
| 71 | + assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"] |
| 72 | + |
| 73 | + return output |
| 74 | + |
| 75 | + |
| 76 | +def test_x2sys_cross_input_dataframe_output_dataframe(mock_x2sys_home, tracks): |
| 77 | + """ |
| 78 | + Run x2sys_cross by passing in one dataframe, and output external crossovers |
| 79 | + to a pandas.DataFrame. Not actually implemented yet, wait for |
| 80 | + https://github.com/GenericMappingTools/gmt/issues/3717 |
| 81 | + """ |
| 82 | + with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir: |
| 83 | + tag = os.path.basename(tmpdir) |
| 84 | + x2sys_init(tag=tag, fmtfile="xyz", force=True) |
| 85 | + |
| 86 | + with pytest.raises(NotImplementedError): |
| 87 | + _ = x2sys_cross(tracks=tracks, tag=tag, coe="i", verbose="i") |
| 88 | + |
| 89 | + # assert isinstance(output, pd.DataFrame) |
| 90 | + # assert output.shape == (4, 12) |
| 91 | + # columns = list(output.columns) |
| 92 | + # assert columns[:6] == ["x", "y", "t_1", "t_2", "dist_1", "dist_2"] |
| 93 | + # assert columns[6:] == ["head_1","head_2","vel_1","vel_2","z_X","z_M"] |
| 94 | + # assert output.dtypes["t_1"].type == np.datetime64 |
| 95 | + # assert output.dtypes["t_2"].type == np.datetime64 |
| 96 | + |
| 97 | + # return output |
| 98 | + |
| 99 | + |
| 100 | +def test_x2sys_cross_input_two_filenames(mock_x2sys_home): |
| 101 | + """ |
| 102 | + Run x2sys_cross by passing in two filenames, and output external crossovers |
| 103 | + to a pandas.DataFrame |
| 104 | + """ |
| 105 | + with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir: |
| 106 | + tag = os.path.basename(tmpdir) |
| 107 | + x2sys_init(tag=tag, fmtfile="xyz", force=True) |
| 108 | + |
| 109 | + # Create temporary xyz files |
| 110 | + for i in range(2): |
| 111 | + np.random.seed(seed=i) |
| 112 | + with open(os.path.join(os.getcwd(), f"track_{i}.xyz"), mode="w") as fname: |
| 113 | + np.savetxt(fname=fname, X=np.random.rand(10, 3)) |
| 114 | + |
| 115 | + output = x2sys_cross( |
| 116 | + tracks=["track_0.xyz", "track_1.xyz"], tag=tag, coe="e", verbose="i" |
| 117 | + ) |
| 118 | + |
| 119 | + assert isinstance(output, pd.DataFrame) |
| 120 | + assert output.shape == (24, 12) |
| 121 | + columns = list(output.columns) |
| 122 | + assert columns[:6] == ["x", "y", "i_1", "i_2", "dist_1", "dist_2"] |
| 123 | + assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"] |
| 124 | + |
| 125 | + return output |
| 126 | + |
| 127 | + |
| 128 | +def test_x2sys_cross_invalid_tracks_input_type(tracks): |
| 129 | + """ |
| 130 | + Run x2sys_cross using tracks input that is not a pandas.DataFrame (matrix) |
| 131 | + or str (file) type, which would raise a GMTInvalidInput error. |
| 132 | + """ |
| 133 | + invalid_tracks = tracks[0].to_xarray().bathymetry |
| 134 | + assert data_kind(invalid_tracks) == "grid" |
| 135 | + with pytest.raises(GMTInvalidInput): |
| 136 | + x2sys_cross(tracks=[invalid_tracks]) |
| 137 | + |
| 138 | + |
| 139 | +def test_x2sys_cross_region_interpolation_numpoints(mock_x2sys_home): |
| 140 | + """ |
| 141 | + Test that x2sys_cross's region (R), interpolation (l) and numpoints (W) |
| 142 | + arguments work. |
| 143 | + """ |
| 144 | + with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir: |
| 145 | + tag = os.path.basename(tmpdir) |
| 146 | + x2sys_init(tag=tag, fmtfile="xyz", force=True) |
| 147 | + output = x2sys_cross( |
| 148 | + tracks=["@tut_ship.xyz"], |
| 149 | + tag=tag, |
| 150 | + coe="i", |
| 151 | + region=[245, 250, 20, 25], |
| 152 | + interpolation="a", # Akima spline interpolation |
| 153 | + numpoints=5, # Use up to 5 data points in interpolation |
| 154 | + ) |
| 155 | + |
| 156 | + assert isinstance(output, pd.DataFrame) |
| 157 | + assert output.shape == (3867, 12) |
| 158 | + # Check crossover errors (z_X) and mean value of observables (z_M) |
| 159 | + npt.assert_allclose(output.z_X.mean(), -139.2, rtol=1e-4) |
| 160 | + npt.assert_allclose(output.z_M.mean(), -2890.465813) |
| 161 | + |
| 162 | + |
| 163 | +def test_x2sys_cross_trackvalues(mock_x2sys_home): |
| 164 | + """ |
| 165 | + Test that x2sys_cross's trackvalues (Z) argument work. |
| 166 | + """ |
| 167 | + with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir: |
| 168 | + tag = os.path.basename(tmpdir) |
| 169 | + x2sys_init(tag=tag, fmtfile="xyz", force=True) |
| 170 | + output = x2sys_cross(tracks=["@tut_ship.xyz"], tag=tag, trackvalues=True) |
| 171 | + |
| 172 | + assert isinstance(output, pd.DataFrame) |
| 173 | + assert output.shape == (14294, 12) |
| 174 | + # Check mean of track 1 values (z_1) and track 2 values (z_2) |
| 175 | + npt.assert_allclose(output.z_1.mean(), -2420.569767) |
| 176 | + npt.assert_allclose(output.z_2.mean(), -2400.357549) |
0 commit comments