Skip to content

Commit 4c5908f

Browse files
committed
Add nodal factor util tests
1 parent 80d3385 commit 4c5908f

1 file changed

Lines changed: 349 additions & 0 deletions

File tree

tests/test_ha_utils.py

Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
1+
import numpy as np
2+
import pandas as pd
3+
import pytest
4+
from vtools.functions.ha_utils import nodal_factors
5+
6+
7+
# ============================================================================
8+
# Basic functionality tests
9+
# ============================================================================
10+
11+
def test_single_constituent():
12+
"""Test with single constituent"""
13+
t_dates = pd.date_range("2020-01-01", periods=10, freq="D")
14+
tref_date = pd.Timestamp("2020-01-01")
15+
constituents = ["M2"]
16+
lat_deg = 38.0
17+
18+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
19+
20+
assert F.shape == (10, 1)
21+
assert U.shape == (10, 1)
22+
assert V.shape == (10, 1)
23+
assert np.all(F > 0), "Amplitude factors should be positive"
24+
assert np.all(np.isfinite(F)), "F should contain no NaN or inf"
25+
assert np.all(np.isfinite(U)), "U should contain no NaN or inf"
26+
assert np.all(np.isfinite(V)), "V should contain no NaN or inf"
27+
28+
29+
def test_multiple_constituents():
30+
"""Test with multiple constituents"""
31+
t_dates = pd.date_range("2020-01-01", periods=10, freq="D")
32+
tref_date = pd.Timestamp("2020-01-01")
33+
constituents = ["M2", "K1"]
34+
lat_deg = 38.0
35+
36+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
37+
38+
assert F.shape == (10, 2)
39+
assert U.shape == (10, 2)
40+
assert V.shape == (10, 2)
41+
42+
43+
@pytest.mark.parametrize("constituents", [
44+
["M2"],
45+
["K1"],
46+
["S2"],
47+
["M2", "K1"],
48+
["M2", "K1", "S2", "N2"],
49+
])
50+
def test_various_constituents(constituents):
51+
"""Test with various combinations of constituents"""
52+
t_dates = pd.date_range("2020-01-01", periods=10, freq="D")
53+
tref_date = pd.Timestamp("2020-01-01")
54+
lat_deg = 38.0
55+
56+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
57+
58+
assert F.shape == (10, len(constituents))
59+
assert U.shape == (10, len(constituents))
60+
assert V.shape == (10, len(constituents))
61+
62+
63+
def test_invalid_constituent():
64+
"""Test that invalid constituent raises error"""
65+
t_dates = pd.date_range("2020-01-01", periods=10, freq="D")
66+
tref_date = pd.Timestamp("2020-01-01")
67+
lat_deg = 38.0
68+
69+
with pytest.raises(KeyError):
70+
nodal_factors(t_dates, tref_date, ["INVALID"], lat_deg)
71+
72+
73+
def test_single_timestamp():
74+
"""Test with single timestamp"""
75+
t_dates = pd.Timestamp("2020-01-01")
76+
tref_date = pd.Timestamp("2020-01-01")
77+
constituents = ["M2"]
78+
lat_deg = 38.0
79+
80+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
81+
82+
# When single timestamp is passed, it should be treated as array-like
83+
assert F.ndim == 2
84+
assert U.ndim == 2
85+
assert V.ndim == 2
86+
87+
88+
89+
# ============================================================================
90+
# Phase option tests
91+
# ============================================================================
92+
93+
@pytest.mark.parametrize("phase", ["Greenwich", "linear_time", "raw"])
94+
def test_all_phase_options(phase):
95+
"""Test all valid phase options"""
96+
t_dates = pd.date_range("2020-01-01", periods=20, freq="D")
97+
tref_date = pd.Timestamp("2020-01-01")
98+
constituents = ["M2", "K1"]
99+
lat_deg = 38.0
100+
101+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg, phase=phase)
102+
103+
assert F.shape == (20, 2)
104+
assert U.shape == (20, 2)
105+
assert V.shape == (20, 2)
106+
107+
108+
def test_phase_options_produce_different_results():
109+
"""Test that different phase options produce different V (astronomical arguments)"""
110+
t_dates = pd.date_range("2020-01-01", periods=20, freq="D")
111+
tref_date = pd.Timestamp("2020-01-01")
112+
constituents = ["M2", "K1"]
113+
lat_deg = 38.0
114+
115+
F_green, U_green, V_green = nodal_factors(t_dates, tref_date, constituents, lat_deg, phase="Greenwich")
116+
F_linear, U_linear, V_linear = nodal_factors(t_dates, tref_date, constituents, lat_deg, phase="linear_time")
117+
F_raw, U_raw, V_raw = nodal_factors(t_dates, tref_date, constituents, lat_deg, phase="raw")
118+
119+
# V should be different for different phase options
120+
assert not np.allclose(V_green, V_linear)
121+
assert not np.allclose(V_green, V_raw)
122+
assert not np.allclose(V_linear, V_raw)
123+
124+
125+
def test_invalid_phase_option():
126+
"""Test that invalid phase option raises error"""
127+
t_dates = pd.date_range("2020-01-01", periods=20, freq="D")
128+
tref_date = pd.Timestamp("2020-01-01")
129+
constituents = ["M2", "K1"]
130+
lat_deg = 38.0
131+
132+
with pytest.raises(ValueError):
133+
nodal_factors(t_dates, tref_date, constituents, lat_deg, phase="invalid")
134+
135+
136+
# ============================================================================
137+
# Nodal linear time option tests
138+
# ============================================================================
139+
140+
def test_nodal_linear_time_false():
141+
"""Test with nodal_linear_time=False (default)"""
142+
t_dates = pd.date_range("2020-01-01", periods=50, freq="D")
143+
tref_date = pd.Timestamp("2020-01-01")
144+
constituents = ["M2", "K1"]
145+
lat_deg = 38.0
146+
147+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg, nodal_linear_time=False)
148+
149+
# F and U should have same length as t_dates
150+
assert F.shape == (50, 2)
151+
assert U.shape == (50, 2)
152+
# V always has same length as t_dates
153+
assert V.shape == (50, 2)
154+
155+
156+
def test_nodal_linear_time_true():
157+
"""Test with nodal_linear_time=True"""
158+
t_dates = pd.date_range("2020-01-01", periods=50, freq="D")
159+
tref_date = pd.Timestamp("2020-01-01")
160+
constituents = ["M2", "K1"]
161+
lat_deg = 38.0
162+
163+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg, nodal_linear_time=True)
164+
165+
# F and U should have single row when nodal_linear_time=True
166+
assert F.shape == (1, 2)
167+
assert U.shape == (1, 2)
168+
# V still has same length as t_dates
169+
assert V.shape == (50, 2)
170+
171+
172+
def test_nodal_linear_time_comparison():
173+
"""Compare results between nodal_linear_time=True and False"""
174+
t_dates = pd.date_range("2020-01-01", periods=50, freq="D")
175+
tref_date = pd.Timestamp("2020-01-01")
176+
constituents = ["M2", "K1"]
177+
lat_deg = 38.0
178+
179+
F_full, U_full, V_full = nodal_factors(t_dates, tref_date, constituents, lat_deg, nodal_linear_time=False)
180+
F_linear, U_linear, V_linear = nodal_factors(t_dates, tref_date, constituents, lat_deg, nodal_linear_time=True)
181+
182+
# V should be identical
183+
np.testing.assert_allclose(V_full, V_linear, rtol=1e-10)
184+
185+
# F_linear should be single values, F_full should be time series
186+
assert F_linear.shape == (1, 2)
187+
assert F_full.shape == (50, 2)
188+
189+
190+
# ============================================================================
191+
# Time variability tests
192+
# ============================================================================
193+
194+
def test_nodal_factors_vary_with_time():
195+
"""Test that nodal factors vary over time"""
196+
# Use a longer time period to see variation
197+
t_dates = pd.date_range("2020-01-01", periods=365, freq="D")
198+
tref_date = pd.Timestamp("2020-01-01")
199+
constituents = ["M2", "K1"]
200+
lat_deg = 38.0
201+
202+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
203+
204+
# Check that F varies with time (at least for some constituents)
205+
# M2 is a semidiurnal tide with nodal period of ~18.6 years, so variation over 1 year is small
206+
# but should still be detectable
207+
F_std = np.std(F, axis=0)
208+
assert np.all(F_std > 0), "F should vary with time"
209+
210+
# V should vary significantly with time
211+
V_std = np.std(V, axis=0)
212+
assert np.all(V_std > 0), "V should vary with time"
213+
214+
215+
def test_v_increases_monotonically():
216+
"""Test that astronomical argument V increases with time"""
217+
t_dates = pd.date_range("2020-01-01", periods=100, freq="D")
218+
tref_date = pd.Timestamp("2020-01-01")
219+
constituents = ["M2"]
220+
lat_deg = 38.0
221+
222+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
223+
224+
# V should generally increase with time (may wrap but generally increasing)
225+
# Check first and last values
226+
assert V[-1, 0] > V[0, 0] or (V[-1, 0] < 0.5 and V[0, 0] > 0.5) # wrapping allowed
227+
228+
229+
# ============================================================================
230+
# Edge cases and boundary conditions
231+
# ============================================================================
232+
233+
def test_very_short_time_series():
234+
"""Test with minimal time series length"""
235+
t_dates = pd.date_range("2020-01-01", periods=1, freq="D")
236+
tref_date = pd.Timestamp("2020-01-01")
237+
constituents = ["M2"]
238+
lat_deg = 38.0
239+
240+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
241+
242+
assert F.shape == (1, 1)
243+
assert U.shape == (1, 1)
244+
assert V.shape == (1, 1)
245+
246+
247+
def test_different_tref_date():
248+
"""Test that different reference dates produce different results"""
249+
t_dates = pd.date_range("2022-06-01", periods=30, freq="D")
250+
constituents = ["M2", "K1"]
251+
lat_deg = 38.0
252+
253+
tref1 = pd.Timestamp("2022-01-01")
254+
tref2 = pd.Timestamp("2022-07-01")
255+
nodal_linear_time = True
256+
257+
F1, U1, V1 = nodal_factors(t_dates, tref1, constituents, lat_deg, nodal_linear_time=nodal_linear_time)
258+
F2, U2, V2 = nodal_factors(t_dates, tref2, constituents, lat_deg, nodal_linear_time=nodal_linear_time)
259+
260+
# U should differ for different reference dates
261+
assert not np.allclose(U1, U2), "Phase corrections should differ for different tref_date"
262+
263+
# F should differ for different reference dates
264+
assert not np.allclose(F1, F2), "Amplitude corrections should differ for different tref_date"
265+
266+
267+
@pytest.mark.parametrize("lat_deg", [-90.0, -45.0, 0.0, 45.0, 90.0])
268+
def test_extreme_latitudes(lat_deg):
269+
"""Test with extreme latitude values"""
270+
t_dates = pd.date_range("2020-01-01", periods=10, freq="D")
271+
tref_date = pd.Timestamp("2020-01-01")
272+
constituents = ["M2"]
273+
274+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
275+
276+
assert F.shape == (10, 1)
277+
assert np.all(np.isfinite(F)), "Results should be finite at extreme latitudes"
278+
assert np.all(np.isfinite(U)), "Results should be finite at extreme latitudes"
279+
assert np.all(np.isfinite(V)), "Results should be finite at extreme latitudes"
280+
281+
282+
def test_datetimeindex_input():
283+
"""Test with DatetimeIndex input"""
284+
t_dates = pd.DatetimeIndex(["2020-01-01", "2020-01-02", "2020-01-03"])
285+
tref_date = pd.Timestamp("2020-01-01")
286+
constituents = ["M2"]
287+
lat_deg = 38.0
288+
289+
F, U, V = nodal_factors(t_dates, tref_date, constituents, lat_deg)
290+
291+
assert F.shape == (3, 1)
292+
293+
294+
def test_series_input():
295+
"""Test with Series input (with datetime index)"""
296+
t_dates = pd.Series(
297+
[1, 2, 3],
298+
index=pd.DatetimeIndex(["2020-01-01", "2020-01-02", "2020-01-03"])
299+
)
300+
tref_date = pd.Timestamp("2020-01-01")
301+
constituents = ["M2"]
302+
lat_deg = 38.0
303+
304+
F, U, V = nodal_factors(t_dates.index, tref_date, constituents, lat_deg)
305+
306+
assert F.shape == (3, 1)
307+
308+
309+
def test_invalid_tref_type():
310+
"""Test that invalid tref_date type raises error"""
311+
t_dates = pd.date_range("2020-01-01", periods=10, freq="D")
312+
constituents = ["M2"]
313+
lat_deg = 38.0
314+
315+
with pytest.raises(ValueError):
316+
nodal_factors(t_dates, "2020-01-01", constituents, lat_deg)
317+
318+
319+
def test_invalid_tdates_type():
320+
"""Test that invalid t_dates type raises error"""
321+
tref_date = pd.Timestamp("2020-01-01")
322+
constituents = ["M2"]
323+
lat_deg = 38.0
324+
325+
with pytest.raises(ValueError):
326+
nodal_factors([1, 2, 3], tref_date, constituents, lat_deg)
327+
328+
329+
# ============================================================================
330+
# Consistency tests
331+
# ============================================================================
332+
333+
def test_same_inputs_same_outputs():
334+
"""Test that identical inputs produce identical outputs"""
335+
t_dates = pd.date_range("2020-01-01", periods=20, freq="D")
336+
tref_date = pd.Timestamp("2020-01-01")
337+
constituents = ["M2", "K1"]
338+
lat_deg = 38.0
339+
340+
F1, U1, V1 = nodal_factors(t_dates, tref_date, constituents, lat_deg)
341+
F2, U2, V2 = nodal_factors(t_dates, tref_date, constituents, lat_deg)
342+
343+
np.testing.assert_array_equal(F1, F2)
344+
np.testing.assert_array_equal(U1, U2)
345+
np.testing.assert_array_equal(V1, V2)
346+
347+
348+
349+

0 commit comments

Comments
 (0)