Skip to content

Commit 88bde40

Browse files
committed
added testcase
1 parent 0dbe3ce commit 88bde40

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

openml/runs/functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ def _prediction_to_row(rep_no, fold_no, row_id, correct_label, predicted_label,
276276
arff_line : list
277277
representation of the current prediction in OpenML format
278278
"""
279+
if not isinstance(rep_no, int): raise ValueError('rep_no should be int')
280+
if not isinstance(fold_no, int): raise ValueError('fold_no should be int')
281+
if not isinstance(row_id, int): raise ValueError('row_id should be int')
282+
if not len(predicted_probabilities) == len(class_labels):
283+
raise ValueError('len(predicted_probabilities) != len(class_labels)')
284+
279285
arff_line = [rep_no, fold_no, row_id]
280286
for class_label_idx in range(len(class_labels)):
281287
if class_label_idx in model_classes_mapping:

tests/test_runs/test_run_functions.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from openml.testing import TestBase
1515
from openml.runs.functions import _run_task_get_arffcontent, \
1616
_get_seeded_model, _run_exists, _extract_arfftrace, \
17-
_extract_arfftrace_attributes
17+
_extract_arfftrace_attributes, _prediction_to_row
1818

1919
from sklearn.naive_bayes import GaussianNB
2020
from sklearn.model_selection._search import BaseSearchCV
@@ -283,7 +283,7 @@ def test__run_exists(self):
283283
self.assertGreater(len(run_ids), 0)
284284

285285

286-
def test_get_seeded_model(self):
286+
def test__get_seeded_model(self):
287287
# randomized models that are initialized without seeds, can be seeded
288288
randomized_clfs = [
289289
BaggingClassifier(),
@@ -318,7 +318,7 @@ def test_get_seeded_model(self):
318318
self.assertIsInstance(new_params[param], int)
319319
self.assertIsNotNone(new_params[param])
320320

321-
def test_get_seeded_model_raises(self):
321+
def test__get_seeded_model_raises(self):
322322
# the _get_seeded_model should raise exception if random_state is anything else than an int
323323
randomized_clfs = [
324324
BaggingClassifier(random_state=np.random.RandomState(42)),
@@ -377,6 +377,44 @@ def test__extract_arfftrace(self):
377377

378378
self.assertEqual(param_grid.keys(), optimized_params)
379379

380+
def test__prediction_to_row(self):
381+
repeat_nr = 0
382+
fold_nr = 0
383+
clf = sklearn.pipeline.Pipeline(steps=[('Imputer', Imputer(strategy='mean')),
384+
('VarianceThreshold', VarianceThreshold(threshold=0.05)),
385+
('Estimator', GaussianNB())])
386+
task = openml.tasks.get_task(20)
387+
train, test = task.get_train_test_split_indices(repeat_nr, fold_nr)
388+
X, y = task.get_X_and_y()
389+
clf.fit(X[train], y[train])
390+
391+
test_X = X[test]
392+
test_y = y[test]
393+
394+
probaY = clf.predict_proba(test_X)
395+
predY = clf.predict(test_X)
396+
for idx in range(0, len(test_X)):
397+
arff_line = _prediction_to_row(repeat_nr, fold_nr, idx,
398+
task.class_labels[test_y[idx]],
399+
predY[idx], probaY[idx], task.class_labels, clf.classes_)
400+
401+
self.assertIsInstance(arff_line, list)
402+
self.assertEqual(len(arff_line), 5 + len(task.class_labels))
403+
self.assertEqual(arff_line[0], repeat_nr)
404+
self.assertEqual(arff_line[1], fold_nr)
405+
self.assertEqual(arff_line[2], idx)
406+
sum = 0.0
407+
for att_idx in range(3, 3 + len(task.class_labels)):
408+
self.assertIsInstance(arff_line[att_idx], float)
409+
self.assertGreaterEqual(arff_line[att_idx], 0.0)
410+
self.assertLessEqual(arff_line[att_idx], 1.0)
411+
sum += arff_line[att_idx]
412+
self.assertAlmostEqual(sum, 1.0)
413+
414+
self.assertIn(arff_line[-1], task.class_labels)
415+
self.assertIn(arff_line[-2], task.class_labels)
416+
pass
417+
380418

381419
def test_run_with_classifiers_in_param_grid(self):
382420
task = openml.tasks.get_task(115)

0 commit comments

Comments
 (0)