Skip to content

Commit f5ba137

Browse files
committed
Method to compare summary xlsx reports from CI; better organization of baseline and new results for CI comparisons.
1 parent ecb8f6a commit f5ba137

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

.github/workflows/integration_tests.yml

+12-8
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ jobs:
6666
- name: Compare integration test results
6767
run: |
6868
#FIXME temporarily pull from ci_outputs
69-
git fetch
70-
git show origin/ci_outputs:tests/integration_testing/results/agg_results.json > tests/integration_testing/results/agg_results_master.json
71-
git show origin/ci_outputs:tests/integration_testing/results/ecm_results.json > tests/integration_testing/results/ecm_results_master.json
72-
git show origin/ci_outputs:tests/integration_testing/results/plots/tech_potential/Summary_Data-TP.xlsx > tests/integration_testing/results/plots/tech_potential/Summary_Data-TP_master.xlsx
73-
git show origin/ci_outputs:tests/integration_testing/results/plots/max_adopt_potential/Summary_Data-MAP.xlsx > tests/integration_testing/results/plots/tech_potential/Summary_Data-MAP_master.xlsx
74-
75-
python tests/integration_testing/compare_results.py -d tests/integration_testing/results --baseline_suffix _master
69+
git fetch #FIXME: remove
70+
branch_name="${{ github.ref }}"
71+
git pull origin $branch_name
72+
if [[ $(git diff --quiet ./tests/integration_testing/results/agg_results.json ./tests/integration_testing/results/ecm_results.json) ]]; then
73+
mkdir tests/integration_testing/base_results
74+
git show origin/ci_outputs:tests/integration_testing/results/agg_results.json > tests/integration_testing/base_results/agg_results.json
75+
git show origin/ci_outputs:tests/integration_testing/results/ecm_results.json > tests/integration_testing/base_results/ecm_results.json
76+
git show origin/ci_outputs:tests/integration_testing/results/plots/tech_potential/Summary_Data-TP.xlsx > tests/integration_testing/base_results/Summary_Data-TP.xlsx
77+
git show origin/ci_outputs:tests/integration_testing/results/plots/max_adopt_potential/Summary_Data-MAP.xlsx > tests/integration_testing/base_results/Summary_Data-MAP.xlsx
7678
79+
python tests/integration_testing/compare_results.py --base-dir tests/integration_testing/base_results --new-dir tests/integration_testing/results
80+
fi
7781
- name: Upload artifacts
7882
uses: actions/upload-artifact@v3
7983
with:
@@ -85,7 +89,7 @@ jobs:
8589
git pull origin $branch_name
8690
git add ./tests/integration_testing/results/*.json
8791
if [[ $(git diff --cached --exit-code) ]]; then
88-
git add ./tests/integration_testing/results
92+
git add ./tests/integration_testing/results/plots
8993
git config --system user.email "[email protected]"
9094
git config --system user.name "GitHub Action"
9195
git commit -m "Upload results files from CI build"

tests/integration_testing/compare_results.py

+49-42
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ def load_json(file_path):
1515
with open(file_path, 'r') as file:
1616
return json.load(file)
1717

18+
@staticmethod
19+
def load_summary_report(file_path):
20+
df = pd.read_excel(file_path, index_col=list(range(5)))
21+
return df
22+
1823
def compare_dict_keys(self, dict1, dict2, paths, path='', key_diffs=None):
1924
"""Compares nested keys across two dictionaries by recursively searching each level
2025
2126
Args:
22-
dict1 (dict): dictionary to compare
23-
dict2 (dict): dictionary to compare
27+
dict1 (dict): baseline dictionary to compare
28+
dict2 (dict): new dictionary to compare
2429
paths (list): paths to the original files from which the dictionaries are imported
2530
path (str, optional): current dictionary path at whcih to compare. Defaults to ''.
2631
key_diffs (pd.DataFrame, optional): existing summary of difference. Defaults to None.
@@ -62,8 +67,8 @@ def compare_dict_values(self, dict1, dict2, percent_threshold=10, abs_threshold=
6267
values at common paths. Both thresholds must be met to report results.
6368
6469
Args:
65-
dict1 (dict): dictionary to compare
66-
dict2 (dict): dictionary to compare
70+
dict1 (dict): baseline dictionary to compare
71+
dict2 (dict): new dictionary to compare
6772
percent_threshold (int, optional): the percent difference threshold at which
6873
differences are reported. Defaults to 10.
6974
abs_threshold (int, optional): the abosolute difference threshold at which differences
@@ -104,33 +109,43 @@ def write_dict_value_report(self, diff_report, output_path):
104109
return
105110
df.to_csv(output_path, index=False)
106111

107-
def compare_jsons(self, json1_path, json2_path, write_reports=True):
112+
def compare_jsons(self, json1_path, json2_path, output_dir=True):
108113
"""Compare two jsons and report differences in keys and in values
109114
110115
Args:
111-
json1_path (Path): json file to compare
112-
json2_path (Path): json file to compare
116+
json1_path (Path): baseline json file to compare
117+
json2_path (Path): new json file to compare
113118
write_reports (bool, optional): _description_. Defaults to True.
114119
"""
115120
json1 = self.load_json(json1_path)
116121
json2 = self.load_json(json2_path)
117122

118123
# Compare differences in json keys
119124
key_diffs = self.compare_dict_keys(json1, json2, [json1_path, json2_path])
120-
if write_reports:
121-
out_path = json2_path.parent / f"{json2_path.stem}_key_diffs.csv"
122-
self.write_dict_key_report(key_diffs, out_path)
125+
if output_dir is None:
126+
output_dir = json2_path.parent
127+
self.write_dict_key_report(key_diffs, output_dir / f"{json2_path.stem}_key_diffs.csv")
123128

124129
# Compare differences in json values
125130
val_diffs = self.compare_dict_values(json1, json2)
126-
if write_reports:
127-
out_path = json2_path.parent / f"{json2_path.stem}_value_diffs.csv"
128-
self.write_dict_value_report(val_diffs, out_path)
131+
self.write_dict_value_report(val_diffs, output_dir / f"{json2_path.stem}_value_diffs.csv")
129132

130-
def compare_summary_reports(self, report1_path, report2_path, write_reports=True):
131-
# Compare Summary_Data-TP.xlsx and Summary_Data-MAP.xlsx with baseline files
132-
pass
133+
def compare_summary_reports(self, report1_path, report2_path, output_dir=None):
134+
"""Compare Summary_Data-TP.xlsx and Summary_Data-MAP.xlsx with baseline files
133135
136+
Args:
137+
report1_path (Path): baseline summary report to compare
138+
report2_path (Path): new summary report to compare
139+
output_dir (Path, optional): _description_. Defaults to None.
140+
"""
141+
142+
report1 = self.load_summary_report(report1_path)
143+
report2 = self.load_summary_report(report2_path)
144+
145+
diff = ((report2 - report1)/report1).round(2)
146+
if output_dir is None:
147+
output_dir = report2_path.parent
148+
diff.to_csv(output_dir / f"{report2_path.stem}_percent_diffs.csv")
134149

135150
def main():
136151
parser = argparse.ArgumentParser(description="Compare results files for Scout.")
@@ -140,38 +155,30 @@ def main():
140155
help="Path to the baseline summary report (Excel file)")
141156
parser.add_argument("--summary-new", type=Path,
142157
help="Path to the new summary report (Excel file)")
143-
parser.add_argument("-d", "--directory", type=Path,
144-
help="Directory containing files to compare")
145-
parser.add_argument("--baseline_suffix", type=str, default="_master",
146-
help="If using the --directory argument, specify the suffix for the "
147-
"baseline files (e.g., '_master')")
158+
parser.add_argument("--new-dir", type=Path, help="Directory containing files to compare")
159+
parser.add_argument("--base-dir", type=Path, help="Directory containing files to compare")
148160
parser.add_argument("--threshold", type=float, default=10,
149161
help="Threshold for percent difference")
150162
args = parser.parse_args()
151163

152164
compare = ScoutCompare()
153-
if args.directory:
165+
if args.base_dir and args.new_dir:
154166
# Compare all files
155-
results_dir = args.directory.resolve()
156-
agg_results_json_base = results_dir / f"agg_results{args.baseline_suffix}.json"
157-
agg_results_json = results_dir / "agg_results.json"
158-
compare.compare_jsons(agg_results_json_base, agg_results_json)
159-
160-
ecm_results_json_base = results_dir / f"ecm_results{args.baseline_suffix}.json"
161-
ecm_results_json = results_dir / "ecm_results.json"
162-
compare.compare_jsons(ecm_results_json_base, ecm_results_json)
163-
164-
plots_dir = results_dir / "plots"
165-
summary_tp_base = plots_dir / "tech_potential" / \
166-
f"Summary_Data-TP{args.baseline_suffix}.xlsx"
167-
summary_tp = plots_dir / "tech_potential" / "Summary_Data-TP.xlsx"
168-
compare.compare_summary_reports(summary_tp_base, summary_tp)
169-
170-
summary_map_base = (plots_dir / "max_adopt_potential" /
171-
f"Summary_Data-MAP{args.baseline_suffix}.xlsx")
172-
summary_map = plots_dir / "tech_potential" / "Summary_Data-MAP.xlsx"
173-
compare.compare_summary_reports(summary_map_base, summary_map)
174-
167+
base_dir = args.base_dir.resolve()
168+
new_dir = args.new_dir.resolve()
169+
agg_json_base = base_dir / "agg_results.json"
170+
agg_json_new = new_dir / "agg_results.json"
171+
compare.compare_jsons(agg_json_base, agg_json_new, output_dir=new_dir)
172+
ecm_json_base = base_dir / "ecm_results.json"
173+
ecm_json_new = new_dir / "ecm_results.json"
174+
compare.compare_jsons(ecm_json_base, ecm_json_new, output_dir=new_dir)
175+
176+
summary_tp_base = base_dir / "Summary_Data-TP.xlsx"
177+
summary_tp_new = new_dir / "plots" / "tech_potential" / "Summary_Data-TP.xlsx"
178+
compare.compare_summary_reports(summary_tp_base, summary_tp_new, output_dir=new_dir)
179+
summary_map_base = base_dir / "Summary_Data-MAP.xlsx"
180+
summary_map_new = new_dir / "plots" / "max_adopt_potential" / "Summary_Data-MAP.xlsx"
181+
compare.compare_summary_reports(summary_map_base, summary_map_new, output_dir=new_dir)
175182
else:
176183
# Compare only as specified by the arguments
177184
if args.json_baseline and args.json_new:

0 commit comments

Comments
 (0)