Skip to content

Commit 806be7f

Browse files
committed
Add diff (value + percentge) in cmp report table if exactly two results are compared
1 parent bebc6ef commit 806be7f

File tree

2 files changed

+106
-7
lines changed

2 files changed

+106
-7
lines changed

src/cloudai/report_generator/comparison_report.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ def generate(self):
118118

119119
logging.info(f"Comparison report created: {html_file}")
120120

121+
@staticmethod
122+
def _extract_cmp_values(data: list) -> tuple[float | None, float | None]:
123+
val1, val2 = None, None
124+
try:
125+
val1 = float(data[-2])
126+
val2 = float(data[-1])
127+
except Exception as e:
128+
logging.debug(f"Could not extract comparison values from data {data}: {e}")
129+
return val1, val2
130+
131+
@staticmethod
132+
def _format_diff_cell(val1: float | None, val2: float | None) -> str:
133+
if val1 is None or val2 is None or val2 == 0:
134+
return "n/a"
135+
diff = val1 - val2
136+
diff_percent = (diff / val2) * 100
137+
return f"{diff:+.2f} ({diff_percent:+.2f}%)"
138+
121139
def create_table(
122140
self,
123141
group: GroupedTestRuns,
@@ -131,9 +149,12 @@ def create_table(
131149
table = Table(title=f"{title}: {group.name}", title_justify="left")
132150
for col in info_columns:
133151
table.add_column(col)
134-
for item in group.items:
135-
style = next(style_cycle)
136-
for col in data_columns:
152+
153+
enable_diff_column = len(group.items) == 2
154+
155+
for col in data_columns:
156+
for item in group.items:
157+
style = next(style_cycle)
137158
name_str = "\n".join(item.name.split())
138159
table.add_column(
139160
f"{name_str}\n[white on {style}]{col}",
@@ -143,11 +164,23 @@ def create_table(
143164
no_wrap=False,
144165
)
145166

167+
if enable_diff_column:
168+
diff_style = next(style_cycle)
169+
table.add_column(f"diff\n{col}", justify="right", style=diff_style, header_style=diff_style)
170+
146171
df_with_max_rows = max(dfs, key=len)
147172
for row_idx in range(len(df_with_max_rows)):
148173
data = []
149-
for df in dfs:
150-
data.extend([str(df[col].get(row_idx, "n/a")) for col in data_columns])
174+
for col in data_columns:
175+
data_points = []
176+
for df in dfs:
177+
data_points.append(str(df[col].get(row_idx, "n/a")))
178+
179+
if enable_diff_column:
180+
val1, val2 = self._extract_cmp_values(data_points)
181+
data_points.append(self._format_diff_cell(val1, val2))
182+
183+
data.extend(data_points)
151184

152185
table.add_row(*[str(df_with_max_rows[col][row_idx]) for col in info_columns], *data)
153186

tests/report_generation_strategy/test_comparison_report.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,16 @@ def test_two_data_points(self, cmp_report: MyComparisonReport, nccl_tr: TestRun)
9090
["value"],
9191
)
9292

93-
assert len(table.columns) == 3
93+
assert len(table.columns) == 4
9494
assert len(table.rows) == 3
9595
assert list(table.columns[0].cells) == ["1", "2", "4"]
9696
assert list(table.columns[1].cells) == ["10", "20", "40"]
9797
assert list(table.columns[2].cells) == ["100", "200", "400"]
98+
assert list(table.columns[3].cells) == [
99+
ComparisonReport._format_diff_cell(10, 100),
100+
ComparisonReport._format_diff_cell(20, 200),
101+
ComparisonReport._format_diff_cell(40, 400),
102+
]
98103

99104
def test_one_data_point_is_empty(self, cmp_report: MyComparisonReport, nccl_tr: TestRun) -> None:
100105
table = cmp_report.create_table(
@@ -114,11 +119,72 @@ def test_one_data_point_is_empty(self, cmp_report: MyComparisonReport, nccl_tr:
114119
["value"],
115120
)
116121

117-
assert len(table.columns) == 3
122+
assert len(table.columns) == 4
123+
assert table.columns[-1].header == "diff\nvalue"
118124
assert len(table.rows) == 3
119125
assert list(table.columns[0].cells) == ["1", "2", "4"]
120126
assert list(table.columns[1].cells) == ["n/a", "n/a", "n/a"]
121127
assert list(table.columns[2].cells) == ["10", "20", "40"]
128+
assert list(table.columns[3].cells) == ["n/a", "n/a", "n/a"]
129+
130+
def test_two_data_points_with_two_data_columns(self, cmp_report: MyComparisonReport, nccl_tr: TestRun) -> None:
131+
table = cmp_report.create_table(
132+
GroupedTestRuns(
133+
name="grp_name",
134+
items=[
135+
TRGroupItem(name="item_name", tr=nccl_tr),
136+
TRGroupItem(name="item_name2", tr=nccl_tr),
137+
],
138+
),
139+
[
140+
pd.DataFrame({"size": [1], "value1": [10], "value2": [5]}),
141+
pd.DataFrame({"size": [1], "value1": [100], "value2": [50]}),
142+
],
143+
"title",
144+
["size"],
145+
["value1", "value2"],
146+
)
147+
148+
assert len(table.columns) == 7 # 1 info + 1 size + 2*2 data + 2 diff
149+
assert len(table.rows) == 1
150+
assert list(table.columns[0].cells) == ["1"]
151+
assert list(table.columns[1].cells) == ["10"]
152+
assert list(table.columns[2].cells) == ["100"]
153+
assert list(table.columns[3].cells) == [
154+
ComparisonReport._format_diff_cell(10, 100),
155+
]
156+
assert list(table.columns[4].cells) == ["5"]
157+
assert list(table.columns[5].cells) == ["50"]
158+
assert list(table.columns[6].cells) == [
159+
ComparisonReport._format_diff_cell(5, 50),
160+
]
161+
162+
def test_three_data_points(self, cmp_report: MyComparisonReport, nccl_tr: TestRun) -> None:
163+
table = cmp_report.create_table(
164+
GroupedTestRuns(
165+
name="grp_name",
166+
items=[
167+
TRGroupItem(name="item_name", tr=nccl_tr),
168+
TRGroupItem(name="item_name2", tr=nccl_tr),
169+
TRGroupItem(name="item_name3", tr=nccl_tr),
170+
],
171+
),
172+
[
173+
pd.DataFrame({"size": [1], "value": [10]}),
174+
pd.DataFrame({"size": [1], "value": [100]}),
175+
pd.DataFrame({"size": [1], "value": [1000]}),
176+
],
177+
"title",
178+
["size"],
179+
["value"],
180+
)
181+
182+
assert len(table.columns) == 4 # 1 info + 3*1 data (NO diff)
183+
assert len(table.rows) == 1
184+
assert list(table.columns[0].cells) == ["1"]
185+
assert list(table.columns[1].cells) == ["10"]
186+
assert list(table.columns[2].cells) == ["100"]
187+
assert list(table.columns[3].cells) == ["1000"]
122188

123189

124190
def test_create_charts(cmp_report: MyComparisonReport, nccl_tr: TestRun) -> None:

0 commit comments

Comments
 (0)