Skip to content

Commit c02e09b

Browse files
committed
updated the tests
1 parent 61816f4 commit c02e09b

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

openml/runs/run.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# License: BSD 3-Clause
22
from __future__ import annotations
33

4+
import csv
45
import pickle
56
import time
67
from collections import OrderedDict
@@ -183,9 +184,56 @@ def _load_predictions_arff(arff_text: str) -> dict[str, Any]:
183184
if idx >= 0
184185
]
185186
if relation_indexes:
186-
return cast("dict[str, Any]", arff.loads(normalized[min(relation_indexes) :]))
187+
arff_candidate = normalized[min(relation_indexes) :]
188+
try:
189+
return cast("dict[str, Any]", arff.loads(arff_candidate))
190+
except arff.ArffException:
191+
sanitized = OpenMLRun._sanitize_arff_text(arff_candidate)
192+
return cast("dict[str, Any]", arff.loads(sanitized))
187193
raise
188194

195+
@staticmethod
196+
def _sanitize_arff_text(arff_text: str) -> str:
197+
lines = arff_text.splitlines()
198+
199+
in_data = False
200+
attribute_count = 0
201+
cleaned_lines: list[str] = []
202+
203+
for line in lines:
204+
stripped = line.strip()
205+
lowered = stripped.lower()
206+
207+
if not in_data:
208+
if lowered.startswith("@attribute"):
209+
attribute_count += 1
210+
if lowered.startswith("@data"):
211+
in_data = True
212+
cleaned_lines.append(line)
213+
continue
214+
215+
if stripped == "" or stripped.startswith("%"):
216+
cleaned_lines.append(line)
217+
continue
218+
219+
if stripped.startswith("{"):
220+
cleaned_lines.append(line)
221+
continue
222+
223+
parsed_fields = next(
224+
csv.reader(
225+
[line],
226+
delimiter=",",
227+
quotechar="'",
228+
skipinitialspace=True,
229+
)
230+
)
231+
232+
if len(parsed_fields) == attribute_count:
233+
cleaned_lines.append(line)
234+
235+
return "\n".join(cleaned_lines) + "\n"
236+
189237
@property
190238
def id(self) -> int | None:
191239
"""The ID of the run, None if not uploaded to the server yet."""

0 commit comments

Comments
 (0)