Skip to content

Commit 16b3414

Browse files
committed
Merge branch 'check_fof_update' of https://github.com/MeteoSwiss/probtest into check_fof_update
2 parents e11aedf + 88a6466 commit 16b3414

File tree

5 files changed

+233
-89
lines changed

5 files changed

+233
-89
lines changed

engine/check.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,8 @@
4343
default="",
4444
help=cli_help["fof_types"],
4545
)
46-
def check(
47-
reference_files,
48-
current_files,
49-
tolerance_files,
50-
factor,
51-
fof_types,
52-
):
46+
@click.option("--rules", default="")
47+
def check(reference_files, current_files, tolerance_files, factor, fof_types, rules):
5348

5449
zipped = zip(reference_files, current_files, tolerance_files)
5550

@@ -60,7 +55,7 @@ def check(
6055
for reference_file, current_file, tolerance_file in expanded_zip:
6156

6257
out, err, tol = check_file_with_tolerances(
63-
tolerance_file, reference_file, current_file, factor
58+
tolerance_file, reference_file, current_file, factor, rules
6459
)
6560

6661
if out:
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
This module contains tests for verifying the correctness of the tolerance CLI
3+
command.
4+
The tests compare the output of the tolerance CLI with reference tolerance data
5+
and check for discrepancies.
6+
"""
7+
8+
import os
9+
10+
import pandas as pd
11+
import pytest
12+
13+
from tests.helpers import (
14+
assert_empty_df,
15+
load_pandas,
16+
pandas_error,
17+
run_tolerance_cli,
18+
store_as_potential_new_ref,
19+
)
20+
21+
22+
@pytest.mark.parametrize("use_minimum_tolerance", [True, False])
23+
def test_tolerance_cli_stats(
24+
ref_data, df_ref_tolerance, tmp_dir, new_ref, use_minimum_tolerance
25+
):
26+
27+
stats_file_name = os.path.join(ref_data, "stats_{member_type}{member_id}.csv")
28+
tolerance_file_name = os.path.join(tmp_dir, "tolerance.csv")
29+
30+
if use_minimum_tolerance:
31+
run_tolerance_cli(
32+
stats_file_name,
33+
tolerance_file_name,
34+
member_type="dp_",
35+
minimum_tolerance=1e-14,
36+
)
37+
38+
df_test = load_pandas(tolerance_file_name, index_col=[0, 1])
39+
err = pandas_error(df_ref_tolerance, df_test)
40+
41+
else:
42+
run_tolerance_cli(
43+
stats_file_name,
44+
tolerance_file_name,
45+
member_type="dp_",
46+
)
47+
48+
df_test = load_pandas(tolerance_file_name, index_col=[0, 1])
49+
modified_df_ref_tolerance = df_ref_tolerance
50+
modified_df_ref_tolerance.loc[("NetCDF:*.nc", "T")] = 0.0
51+
52+
err = pandas_error(modified_df_ref_tolerance, df_test)
53+
54+
store_as_potential_new_ref(tolerance_file_name, new_ref)
55+
56+
assert_empty_df(err, "Tolerance datasets are not equal!")
57+
58+
59+
@pytest.mark.parametrize("use_minimum_tolerance", [True, False])
60+
def test_tolerance_cli_fof(
61+
fof_file_set, df_ref_tolerance_fof, new_ref, use_minimum_tolerance
62+
):
63+
64+
fof_pattern = fof_file_set["path"]
65+
tolerance_files = fof_file_set["tol"]
66+
member_ids = "1,2,3,4"
67+
68+
for fof_file in fof_file_set["fof"]:
69+
assert os.path.exists(fof_file), f"File FOF missing: {fof_file}"
70+
71+
if use_minimum_tolerance:
72+
73+
run_tolerance_cli(
74+
fof_pattern, tolerance_files, member_ids=member_ids, minimum_tolerance=1e-14
75+
)
76+
77+
df_test = pd.read_csv(tolerance_files[0], index_col=[0])
78+
df_test.columns = [None]
79+
err = pandas_error(df_ref_tolerance_fof, df_test)
80+
81+
else:
82+
run_tolerance_cli(fof_pattern, tolerance_files, member_ids=member_ids)
83+
84+
df_test = pd.read_csv(tolerance_files[0], index_col=[0])
85+
df_test.columns = [None]
86+
err = pandas_error(df_ref_tolerance_fof, df_test)
87+
88+
store_as_potential_new_ref(tolerance_files[0], new_ref)
89+
90+
assert_empty_df(err, "Tolerance datasets are not equal!")

tests/engine/test_check.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616

1717

18-
@pytest.fixture(name="dataframes", scope="function")
18+
@pytest.fixture(name="stats_dataframes", scope="function")
1919
def fixture_dataframes():
2020
"""
2121
Create stats dataframes and reference tolerances.
@@ -89,8 +89,8 @@ def _check(df1, df2, tol_large, tol_small, file_type):
8989
)
9090

9191

92-
def test_check(dataframes):
93-
df1, df2, tol_large, tol_small = dataframes
92+
def test_check_stats(stats_dataframes):
93+
df1, df2, tol_large, tol_small = stats_dataframes
9494
_check(df1, df2, tol_large, tol_small, file_type="stats")
9595

9696

@@ -109,7 +109,7 @@ def test_check_fof(fof_datasets):
109109
)
110110

111111

112-
def test_check_one_zero(dataframes):
112+
def test_check_one_zero_stats(dataframes):
113113
"""
114114
Test that a null value in ds1 causes failure,
115115
and that a variation within tolerance is accepted.
@@ -122,7 +122,7 @@ def test_check_one_zero(dataframes):
122122
diff_df = diff_df.groupby(["variable"]).max()
123123
out, err, _ = check_variable(diff_df, tol_large)
124124

125-
assert not out, f"Check con 0-value reference ha validato erroneamente:\n{err}"
125+
assert not out, f"Check with 0-value reference validated incorrectly:\n{err}"
126126

127127
df2 = df2.copy()
128128
df2.loc[("NetCDF:*atm_3d*.nc", "var_1", 2), (0, "max")] = CHECK_THRESHOLD / 2
@@ -149,7 +149,7 @@ def test_check_one_zero_fof(fof_datasets):
149149

150150
out, err, _ = check_variable(diff_df, tol_large)
151151

152-
assert not out, f"Check con 0-value reference ha validato erroneamente:\n{err}"
152+
assert not out, f"Check with 0-value reference validated incorrectly:\n{err}"
153153

154154
_, _, ds_veri2_copy = split_feedback_dataset(ds2_copy)
155155
ds_veri2_copy = ds_veri2_copy.copy(deep=True)
@@ -163,7 +163,7 @@ def test_check_one_zero_fof(fof_datasets):
163163
)
164164

165165

166-
def test_check_smalls(dataframes):
166+
def test_check_smalls_stats(dataframes):
167167
"""
168168
Both values are close to 0 and should be accepted even though
169169
their relative difference is large.

tests/engine/test_check2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from engine.check import check
1414

1515

16-
@pytest.fixture(name="dataframes", scope="function")
16+
@pytest.fixture(name="stats_dataframes", scope="function")
1717
def fixture_dataframes(tmp_dir):
1818
"""
1919
Create stats dataframes and reference tolerances.
@@ -94,13 +94,13 @@ def fixture_fof_datasets(tmp_dir, sample_dataset_fof):
9494
yield ds1_file, ds2_file, tol_large_file, tol_small_file
9595

9696

97-
def test_check_cli_stats(dataframes):
97+
def test_check_cli_stats(stats_dataframes):
9898
"""
9999
Test that is not validated in the case of large tolerances but
100100
is validated in the case of small tolerances.
101101
"""
102102

103-
df1_stats, df2_stats, tol_large, tol_small = dataframes
103+
df1_stats, df2_stats, tol_large, tol_small = stats_dataframes
104104

105105
runner = CliRunner()
106106
result = runner.invoke(

0 commit comments

Comments
 (0)