Skip to content

Commit 169623d

Browse files
committed
Switch to min cdd for comparison
1 parent b4e10cf commit 169623d

1 file changed

Lines changed: 44 additions & 6 deletions

File tree

swmm-toolkit/tests/test_series.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,45 @@
1010

1111
REPORT_STEP_SECONDS = 3600 # Example1
1212

13+
14+
def cdd(t, r):
15+
import math
16+
17+
if t == r:
18+
return 10.0
19+
20+
tmp = abs(t - r)
21+
if tmp < 1.0e-7:
22+
tmp = 1.0e-7
23+
elif tmp > 2.0:
24+
tmp = 1.0
25+
26+
tmp = -math.log10(tmp)
27+
if tmp < 0.0:
28+
tmp = 0.0
29+
30+
return tmp
31+
32+
def check_cdd_float(test: list[float], ref: list[float], cdd_tol: int) -> bool:
33+
"""
34+
Checks minimum correct decimal digits between two float sequences. Fails if lengths differ.
35+
"""
36+
import math
37+
38+
if len(test) != len(ref):
39+
return False
40+
41+
min_cdd = 10.0
42+
43+
for t, r in zip(test, ref):
44+
tmp = cdd(t, r)
45+
46+
if tmp < min_cdd:
47+
min_cdd = tmp
48+
49+
return math.floor(min_cdd) >= cdd_tol
50+
51+
1352
def _curr_dt():
1453
y, m, d, hh, mm, ss = solver.simulation_get_current_datetime()
1554
return datetime(y, m, d, hh, mm, ss)
@@ -81,16 +120,15 @@ def test_compare_aligned_series():
81120
)
82121

83122
# values should match within tolerance
84-
import numpy as np
85-
86-
solver_vals = np.array([v for _, v in s])
87-
output_vals = np.array([v for _, v in o])
123+
solver_vals = [v for _, v in s]
124+
output_vals = [v for _, v in o]
88125

89-
assert np.allclose(solver_vals, output_vals, rtol=1e-6, atol=1e-9), (
126+
assert check_cdd_float(solver_vals, output_vals, 1), (
90127
"Solver and output values differ. "
91128
"See zipped output for details:\n" +
92129
"\n".join(
93-
f"{t1.strftime('%Y-%m-%d %H:%M:%S')} | {v1:.6f} || {t2.strftime('%Y-%m-%d %H:%M:%S')} | {v2:.6f} | diff={v1-v2:.2e}"
130+
f"{t1.strftime('%Y-%m-%d %H:%M:%S')} | {v1:.6f} || {t2.strftime('%Y-%m-%d %H:%M:%S')} | {v2:.6f} | cdd={cdd(v1, v2):.2f}"
94131
for (t1, v1), (t2, v2) in list(zip(s, o))[:10]
95132
)
96133
)
134+

0 commit comments

Comments
 (0)