Skip to content

Commit 01951fd

Browse files
authored
Improved InstanceScores summary to be readible and in decent width (#1847)
1 parent fa53229 commit 01951fd

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

examples/key_value_extraction_evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def text_to_image(text: str):
7171
print(json.dumps(results.instance_scores[0]["source"], indent=4))
7272

7373
print("Instance Results:")
74-
print(results.instance_scores)
74+
print(results.instance_scores.summary)
7575

7676
print("Global Results:")
7777
print(results.global_scores.summary)

src/unitxt/metric_utils.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,11 @@ def to_df(self, flatten=True, columns=None):
687687
def _to_markdown(self, df, max_col_width=30, **kwargs):
688688
def wrap_column(series, max_width=30):
689689
"""Wraps string values in a Pandas Series to a maximum width."""
690-
return series.apply(lambda x: textwrap.fill(str(x), width=max_width))
690+
return series.apply(
691+
lambda x: "\n".join(
692+
textwrap.fill(line, width=max_width) for line in str(x).splitlines()
693+
)
694+
)
691695

692696
wrapped_df = df.copy()
693697
for col in wrapped_df.columns:
@@ -699,22 +703,24 @@ def to_markdown(self, flatten=True, columns=None, max_col_width=30, **kwargs):
699703

700704
@property
701705
def summary(self):
702-
return self._to_markdown(
703-
self.to_df()
704-
.head()
705-
.drop(
706-
columns=[
707-
"metadata",
708-
"media",
709-
"groups",
710-
"subset",
711-
"demos",
712-
"metrics",
713-
"postprocessors",
714-
],
715-
errors="ignore",
716-
)
706+
df = self.to_df(
707+
flatten=False,
708+
columns=[
709+
"source",
710+
"prediction",
711+
"processed_prediction",
712+
"references",
713+
"processed_references",
714+
"score",
715+
],
716+
).head()
717+
df["score_name"] = df["score"].apply(lambda x: x["instance"]["score_name"])
718+
df["all_scores"] = df["score"].apply(
719+
lambda x: "\n".join(f"{k}: {v}" for k, v in x["instance"].items())
717720
)
721+
df["score"] = df["score"].apply(lambda x: x["instance"]["score"])
722+
723+
return self._to_markdown(df)
718724

719725
def __repr__(self):
720726
return to_pretty_string(self, float_format=".2g")

0 commit comments

Comments
 (0)