From d53efb5d1abd07022f98d72fa935a4a5b8a1d0a6 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 17 Aug 2026 11:05:47 +0000 Subject: [PATCH] fix: report the actual median word count in model cards The "Median" row of the Training Set Metrics table was computed as the mean word count. Use statistics.median instead, and relax the model card patterns so they no longer hardcode the mean. Co-authored-by: Tony Coder <407243179@qq.com> --- src/setfit/model_card.py | 3 ++- tests/model_card_pattern.py | 2 +- tests/span/aspect_model_card_pattern.py | 2 +- tests/span/polarity_model_card_pattern.py | 2 +- tests/test_model_card.py | 20 ++++++++++++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/setfit/model_card.py b/src/setfit/model_card.py index 30706bcc..1cdebbf4 100644 --- a/src/setfit/model_card.py +++ b/src/setfit/model_card.py @@ -4,6 +4,7 @@ from dataclasses import dataclass, field, fields from pathlib import Path from platform import python_version +from statistics import median from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import datasets @@ -312,7 +313,7 @@ def add_naive_word_count(sample: Dict[str, Any]) -> Dict[str, Any]: { "Training set": "Word count", "Min": min(dataset["word_count"]), - "Median": sum(dataset["word_count"]) / len(dataset), + "Median": median(dataset["word_count"]), "Max": max(dataset["word_count"]), }, ] diff --git a/tests/model_card_pattern.py b/tests/model_card_pattern.py index 59e290ec..9eaf4b52 100644 --- a/tests/model_card_pattern.py +++ b/tests/model_card_pattern.py @@ -99,7 +99,7 @@ ### Training Set Metrics \| Training set \| Min \| Median \| Max \| \|:-------------\|:----\|:-------\|:----\| -\| Word count \| 3 \| 7.875 \| 18 \| +\| Word count \| 3 \| [\d.]+ +\| 18 \| \| Label \| Training Sample Count \| \|:---------\|:----------------------\| diff --git a/tests/span/aspect_model_card_pattern.py b/tests/span/aspect_model_card_pattern.py index 7cf7393b..24cd46a2 100644 --- a/tests/span/aspect_model_card_pattern.py +++ b/tests/span/aspect_model_card_pattern.py @@ -111,7 +111,7 @@ ### Training Set Metrics \| Training set \| Min \| Median \| Max \| \|:-------------\|:----\|:-------\|:----\| -\| Word count \| 5 \| 14.5 \| 23 \| +\| Word count \| 5 \| [\d.]+ +\| 23 \| \| Label \| Training Sample Count \| \|:----------\|:----------------------\| diff --git a/tests/span/polarity_model_card_pattern.py b/tests/span/polarity_model_card_pattern.py index e9eabf67..5e375170 100644 --- a/tests/span/polarity_model_card_pattern.py +++ b/tests/span/polarity_model_card_pattern.py @@ -111,7 +111,7 @@ ### Training Set Metrics \| Training set \| Min \| Median \| Max \| \|:-------------\|:----\|:-------\|:----\| -\| Word count \| 8 \| 16.8 \| 28 \| +\| Word count \| 8 \| [\d.]+ +\| 28 \| \| Label \| Training Sample Count \| \|:---------\|:----------------------\| diff --git a/tests/test_model_card.py b/tests/test_model_card.py index 9bdb199c..8aab2e8b 100644 --- a/tests/test_model_card.py +++ b/tests/test_model_card.py @@ -62,6 +62,26 @@ def test_model_card_languages() -> None: assert "**Languages:** en, nl, de" in model_card +def test_train_set_metrics_word_count() -> None: + # The word counts are 2, 3, 4 and 11, i.e. a median of 3.5 and a mean of 5.0 + train_dataset = Dataset.from_dict( + { + "text": [ + "one two", + "one two three", + "one two three four", + "one two three four five six seven eight nine ten eleven", + ], + "label": [0, 1, 0, 1], + } + ) + model_card_data = SetFitModelCardData() + model_card_data.set_train_set_metrics(train_dataset) + assert model_card_data.train_set_metrics_list == [ + {"Training set": "Word count", "Min": 2, "Median": 3.5, "Max": 11} + ] + + def test_is_on_huggingface_edge_case() -> None: assert not is_on_huggingface("test_value") assert not is_on_huggingface("a/test/value")