From 40133314771bd4a86b2504ac253038c61db4d2d4 Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Fri, 7 Jun 2024 10:55:15 +0200 Subject: [PATCH] refactor: better error message for unsorted spans Changed from AssertionError to SparvErrorMessage, that also prints the first unsorted index pair and their values. --- sparv/core/io.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sparv/core/io.py b/sparv/core/io.py index 8191ef4e..1c58434c 100644 --- a/sparv/core/io.py +++ b/sparv/core/io.py @@ -91,8 +91,14 @@ def _write_single_annotation( if is_span: if not isinstance(values, list): values = list(values) - # Make sure that spans are sorted - assert all(values[i] <= values[i + 1] for i in range(len(values) - 1)), "Annotation spans must be sorted." + # Validate that spans are sorted + for i in range(len(values) - 1): + if values[i] > values[i + 1]: + raise SparvErrorMessage( + f"Annotation spans must be sorted. values[{i}]={values[i]} > values[{i+1}]={values[i+1]}", + module="core.io", + function="_write_single_annotation", + ) # Always save spans with decimal tuples if values and not isinstance(values[0][0], tuple): values = [((v[0],), (v[1],)) for v in values]