Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Display metric=0 in eval table (#8817)

TomuHirataf85e49122478
diff --git a/dspy/evaluate/evaluate.py b/dspy/evaluate/evaluate.py
index 2da5b6ccf..50513ce8e 100644
--- a/dspy/evaluate/evaluate.py
+++ b/dspy/evaluate/evaluate.py
@@ -336,9 +336,14 @@ def stylize_metric_name(df: "pd.DataFrame", metric_name: str) -> "pd.DataFrame":
:param df: The pandas DataFrame for which to stylize cell contents.
:param metric_name: The name of the metric for which to stylize DataFrame cell contents.
"""
- df[metric_name] = df[metric_name].apply(
- lambda x: f"✔️ [{x:.3f}]" if x and isinstance(x, float) else f"✔️ [{x}]" if x else ""
- )
+ def format_metric(x):
+ if isinstance(x, float):
+ return f"✔️ [{x:.3f}]"
+ elif x is not None:
+ return f"✔️ [{x}]"
+ else:
+ return ""
+ df[metric_name] = df[metric_name].apply(format_metric)
return df
diff --git a/tests/evaluate/test_evaluate.py b/tests/evaluate/test_evaluate.py
index 106396c15..211cf2596 100644
--- a/tests/evaluate/test_evaluate.py
+++ b/tests/evaluate/test_evaluate.py
@@ -57,7 +57,11 @@ def test_evaluate_call():
@pytest.mark.extra
def test_construct_result_df():
import pandas as pd
- devset = [new_example("What is 1+1?", "2"), new_example("What is 2+2?", "4")]
+ devset = [
+ new_example("What is 1+1?", "2"),
+ new_example("What is 2+2?", "4"),
+ new_example("What is 3+3?", "-1"),
+ ]
ev = Evaluate(
devset=devset,
metric=answer_exact_match,
@@ -65,16 +69,17 @@ def test_construct_result_df():
results = [
(devset[0], {"answer": "2"}, 100.0),
(devset[1], {"answer": "4"}, 100.0),
+ (devset[2], {"answer": "-1"}, 0.0),
]
result_df = ev._construct_result_table(results, answer_exact_match.__name__)
pd.testing.assert_frame_equal(
result_df,
pd.DataFrame(
{
- "question": ["What is 1+1?", "What is 2+2?"],
- "example_answer": ["2", "4"],
- "pred_answer": ["2", "4"],
- "answer_exact_match": [100.0, 100.0],
+ "question": ["What is 1+1?", "What is 2+2?", "What is 3+3?"],
+ "example_answer": ["2", "4", "-1"],
+ "pred_answer": ["2", "4", "-1"],
+ "answer_exact_match": [100.0, 100.0, 0.0],
}
),
)