Skip to content

Commit 2da6715

Browse files
Remove YAML integer keys from class_label metadata (huggingface#5277)
* Remove YAML integer keys from class_label metadata * Fix * Fix error message * Sort integer strings numerically
1 parent edf1902 commit 2da6715

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/datasets/features/features.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1645,11 +1645,14 @@ def simplify(feature: dict) -> dict:
16451645
#
16461646
# class_label: -> class_label:
16471647
# names: -> names:
1648-
# - negative -> 0: negative
1649-
# - positive -> 1: positive
1648+
# - negative -> '0': negative
1649+
# - positive -> '1': positive
16501650
#
16511651
if isinstance(feature.get("class_label"), dict) and isinstance(feature["class_label"].get("names"), list):
1652-
feature["class_label"]["names"] = dict(enumerate(feature["class_label"]["names"]))
1652+
# server-side requirement: keys must be strings
1653+
feature["class_label"]["names"] = {
1654+
str(label_id): label_name for label_id, label_name in enumerate(feature["class_label"]["names"])
1655+
}
16531656
return feature
16541657

16551658
def to_yaml_inner(obj: Union[dict, list]) -> dict:
@@ -1698,14 +1701,14 @@ def unsimplify(feature: dict) -> dict:
16981701
#
16991702
# class_label: -> class_label:
17001703
# names: -> names:
1701-
# 0: negative -> - negative
1702-
# 1: positive -> - positive
1704+
# '0': negative -> - negative
1705+
# '1': positive -> - positive
17031706
#
17041707
if isinstance(feature.get("class_label"), dict) and isinstance(feature["class_label"].get("names"), dict):
1705-
label_ids = sorted(feature["class_label"]["names"])
1706-
if label_ids and label_ids != list(range(label_ids[-1] + 1)):
1708+
label_ids = sorted(feature["class_label"]["names"], key=int)
1709+
if label_ids and [int(label_id) for label_id in label_ids] != list(range(int(label_ids[-1]) + 1)):
17071710
raise ValueError(
1708-
f"ClassLabel expected a value for all label ids [0:{label_ids[-1] + 1}] but some ids are missing."
1711+
f"ClassLabel expected a value for all label ids [0:{int(label_ids[-1]) + 1}] but some ids are missing."
17091712
)
17101713
feature["class_label"]["names"] = [feature["class_label"]["names"][label_id] for label_id in label_ids]
17111714
return feature

0 commit comments

Comments
 (0)