Skip to content

Commit 88a6466

Browse files
committed
first version check with dict
1 parent ee7f56f commit 88a6466

File tree

2 files changed

+129
-78
lines changed

2 files changed

+129
-78
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:

util/dataframe_ops.py

Lines changed: 126 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
reference datasets with specified tolerances.
66
"""
77

8+
import ast
89
import sys
910
import warnings
1011

@@ -297,7 +298,7 @@ def parse_check(tolerance_file_name, input_file_ref, input_file_cur, factor):
297298

298299

299300
def check_file_with_tolerances(
300-
tolerance_file_name, input_file_ref, input_file_cur, factor
301+
tolerance_file_name, input_file_ref, input_file_cur, factor, rules
301302
):
302303
file_type = get_file_type(input_file_ref)
303304

@@ -315,20 +316,27 @@ def check_file_with_tolerances(
315316
df_ref = parse_probtest_fof(input_file_ref)
316317

317318
df_cur = parse_probtest_fof(input_file_cur)
318-
cols = ["check", "state", "r_state", "r_check"]
319-
existing_cols = [c for c in cols if c in df_ref and c in df_cur]
320319

321-
if existing_cols:
322-
ds1_multiple = df_ref[existing_cols]
323-
ds2_multiple = df_cur[existing_cols]
320+
errors = multiple_solutions_from_dict(df_ref, df_cur, rules)
324321

325-
out, diff = check_multiple_solutions(
326-
ds1_multiple, ds2_multiple, existing_cols
327-
)
322+
if errors:
323+
logger.error("RESULT: check FAILED")
324+
sys.exit(1)
325+
326+
# cols = ["check", "state", "r_state", "r_check"]
327+
# existing_cols = [c for c in cols if c in df_ref and c in df_cur]
328+
329+
# if existing_cols:
330+
# ds1_multiple = df_ref[existing_cols]
331+
# ds2_multiple = df_cur[existing_cols]
332+
333+
# out, diff = check_multiple_solutions(
334+
# ds1_multiple, ds2_multiple, existing_cols
335+
# )
328336

329-
if out == 1:
330-
logger.error("RESULT: check FAILED. Errors at the lines %s", diff)
331-
sys.exit(1)
337+
# if out == 1:
338+
# logger.error("RESULT: check FAILED. Errors at the lines %s", diff)
339+
# sys.exit(1)
332340

333341
else:
334342
df_tol, df_ref, df_cur = parse_check(
@@ -383,61 +391,109 @@ def has_enough_data(dfs):
383391
}
384392

385393

386-
def check_multiple_solutions(ds1, ds2, existing_cols):
387-
388-
allowed_checks = [13, 18, 32]
389-
allowed_states = [1, 5, 7, 9]
390-
391-
df1 = ds1[existing_cols]
392-
df2 = ds2[existing_cols]
393-
394-
diff = []
395-
396-
check_cols = ["check", "r_check"]
397-
state_cols = ["state", "r_state"]
398-
out = 0
399-
400-
for idx in df1.index:
401-
check_col = next((c for c in check_cols if c in df1.columns), None)
402-
state_col = next((c for c in state_cols if c in df1.columns), None)
403-
404-
if check_col is None or state_col is None:
405-
raise KeyError("'Check' or “state” columns not found in datasets.")
406-
407-
check_ref = df1.at[idx, check_col]
408-
check_cur = df2.at[idx, check_col]
409-
state_ref = df1.at[idx, state_col]
410-
state_cur = df2.at[idx, state_col]
411-
412-
# CASE 1: check does not change
413-
if check_ref == check_cur:
414-
# If check is not an accepted, state should not change
415-
if check_ref not in allowed_checks:
416-
if state_ref != state_cur:
417-
out = 1
418-
diff.append(idx)
419-
420-
# If is an admitted change, state can change, but only in the admitted cases
421-
else:
422-
if state_ref != state_cur:
423-
if (state_ref not in allowed_states) or (
424-
state_cur not in allowed_states
425-
):
426-
out = 1
427-
diff.append(idx)
428-
429-
# CASE 2: check changes
430-
else:
431-
if (check_ref not in allowed_checks) and (check_cur not in allowed_checks):
432-
out = 1
433-
diff.append(idx)
434-
435-
# If check values are both admitted, also state should
436-
# be in the admitted values
437-
elif (state_ref not in allowed_states) or (state_cur not in allowed_states):
438-
out = 1
439-
diff.append(idx)
440-
441-
diff = np.array(diff)
442-
443-
return out, diff
394+
def multiple_solutions_from_dict(df_ref, df_cur, rules):
395+
396+
rules_dict = ast.literal_eval(rules)
397+
398+
cols_present = [
399+
col
400+
for col in rules_dict.keys()
401+
if col in df_ref.columns and col in df_cur.columns
402+
]
403+
errors = []
404+
405+
if cols_present:
406+
for i in range(len(df_ref)):
407+
row1 = df_ref.iloc[i]
408+
row2 = df_cur.iloc[i]
409+
410+
for col in cols_present:
411+
val1 = row1[col]
412+
val2 = row2[col]
413+
414+
if val1 != val2:
415+
if val1 not in rules_dict[col] or val2 not in rules_dict[col]:
416+
errors.append(
417+
{
418+
"row": i,
419+
"column": col,
420+
"file1": val1,
421+
"file2": val2,
422+
"error": "values different and not admitted",
423+
}
424+
)
425+
426+
if errors:
427+
logger.error("Errors found while comparing the files:")
428+
for e in errors:
429+
logger.error(
430+
"Row %s - Column '%s': file1=%s, file2=%s → %s",
431+
e["row"],
432+
e["column"],
433+
e["file1"],
434+
e["file2"],
435+
e["error"],
436+
)
437+
return errors
438+
439+
return []
440+
441+
442+
# def check_multiple_solutions(ds1, ds2, existing_cols):
443+
444+
# allowed_checks = [13, 18, 32]
445+
# allowed_states = [1, 5, 7, 9]
446+
447+
# df1 = ds1[existing_cols]
448+
# df2 = ds2[existing_cols]
449+
450+
# diff = []
451+
452+
# check_cols = ["check", "r_check"]
453+
# state_cols = ["state", "r_state"]
454+
# out = 0
455+
456+
# for idx in df1.index:
457+
# check_col = next((c for c in check_cols if c in df1.columns), None)
458+
# state_col = next((c for c in state_cols if c in df1.columns), None)
459+
460+
# if check_col is None or state_col is None:
461+
# raise KeyError("'Check' or “state” columns not found in datasets.")
462+
463+
# check_ref = df1.at[idx, check_col]
464+
# check_cur = df2.at[idx, check_col]
465+
# state_ref = df1.at[idx, state_col]
466+
# state_cur = df2.at[idx, state_col]
467+
468+
# # CASE 1: check does not change
469+
# if check_ref == check_cur:
470+
# # If check is not an accepted, state should not change
471+
# if check_ref not in allowed_checks:
472+
# if state_ref != state_cur:
473+
# out = 1
474+
# diff.append(idx)
475+
476+
# # If is an admitted change, state can change, but only in the admitted cases
477+
# else:
478+
# if state_ref != state_cur:
479+
# if (state_ref not in allowed_states) or (
480+
# state_cur not in allowed_states
481+
# ):
482+
# out = 1
483+
# diff.append(idx)
484+
485+
# # CASE 2: check changes
486+
# else:
487+
# if (check_ref not in allowed_checks) and (check_cur not in allowed_checks):
488+
# out = 1
489+
# diff.append(idx)
490+
491+
# # If check values are both admitted, also state should
492+
# # be in the admitted values
493+
# elif (state_ref not in allowed_states) or (state_cur not in allowed_states):
494+
# out = 1
495+
# diff.append(idx)
496+
497+
# diff = np.array(diff)
498+
499+
# return out, diff

0 commit comments

Comments
 (0)