Skip to content

Commit 3ed5c7a

Browse files
committed
incorporated requests of @mfeurer
1 parent 59433d8 commit 3ed5c7a

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

openml/runs/run.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ def _generate_trace_arff_dict(self):
107107

108108
return arff_dict
109109

110-
def get_metric_score(self, sklearn_fn, kwargs={}):
110+
def get_metric_fn(self, sklearn_fn, kwargs={}):
111111
'''Calculates metric scores based on predicted values. Assumes the
112-
run has been executed locally (and contans run_data). Furthermore,
113-
it assumes that the 'correct' field has been set (which is
114-
automatically the case for local runs)
112+
run has been executed locally (and contains run_data). Furthermore,
113+
it assumes that the 'correct' attribute is specified in the arff
114+
(which is an optional field, but always the case for openml-python
115+
runs)
115116
116117
Parameters
117118
-------
@@ -133,8 +134,15 @@ def get_metric_score(self, sklearn_fn, kwargs={}):
133134
else:
134135
raise ValueError('Run should have been locally executed.')
135136

137+
if 'correct' not in predictions_arff['attributes']:
138+
raise ValueError('Attribute "correct" should be set')
139+
if 'predict' not in predictions_arff['attributes']:
140+
raise ValueError('Attribute "predict" should be set')
141+
136142
def _attribute_list_to_dict(attribute_list):
137-
# convenience function
143+
# convenience function: Creates a mapping to map from the name of attributes
144+
# present in the arff prediction file to their index. This is necessary
145+
# because the number of classes can be different for different tasks.
138146
res = dict()
139147
for idx in range(len(attribute_list)):
140148
res[attribute_list[idx][0]] = idx

tests/test_flows/test_flow_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_list_flows(self):
2525
# data from the internet...
2626
flows = openml.flows.list_flows()
2727
# 3000 as the number of flows on openml.org
28-
self.assertGreaterEqual(len(flows), 3000)
28+
self.assertGreaterEqual(len(flows), 1500)
2929
for fid in flows:
3030
self._check_flow(flows[fid])
3131

tests/test_runs/test_run_functions.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,26 +412,16 @@ def test_initialize_cv_from_run(self):
412412

413413
self.assertEquals(modelS.cv.random_state, 62501)
414414
self.assertEqual(modelR.cv.random_state, 62501)
415-
416-
def test_get_run_metric_score(self):
417-
418-
# construct sci-kit learn classifier
419-
clf = Pipeline(steps=[('imputer', Imputer(strategy='median')), ('estimator', RandomForestClassifier())])
420-
421-
422-
# download task
423-
task = openml.tasks.get_task(7)
424-
425-
# invoke OpenML run
426-
run = openml.runs.run_model_on_task(task, clf)
415+
416+
def _test_local_evaluations(self, run):
427417

428418
# compare with the scores in user defined measures
429419
accuracy_scores_provided = []
430420
for rep in run.fold_evaluations['predictive_accuracy'].keys():
431421
for fold in run.fold_evaluations['predictive_accuracy'][rep].keys():
432422
accuracy_scores_provided.append(run.fold_evaluations['predictive_accuracy'][rep][fold])
433423
accuracy_scores = run.get_metric_score(sklearn.metrics.accuracy_score)
434-
self.assertEquals(sum(accuracy_scores_provided), sum(accuracy_scores))
424+
np.testing.assert_array_almost_equal(accuracy_scores_provided, accuracy_scores)
435425

436426
# also check if we can obtain some other scores: # TODO: how to do AUC?
437427
tests = [(sklearn.metrics.cohen_kappa_score, {'weights': None}),
@@ -447,6 +437,25 @@ def test_get_run_metric_score(self):
447437
self.assertGreaterEqual(alt_scores[idx], 0)
448438
self.assertLessEqual(alt_scores[idx], 1)
449439

440+
def test_local_run_metric_score(self):
441+
442+
# construct sci-kit learn classifier
443+
clf = Pipeline(steps=[('imputer', Imputer(strategy='median')), ('estimator', RandomForestClassifier())])
444+
445+
# download task
446+
task = openml.tasks.get_task(7)
447+
448+
# invoke OpenML run
449+
run = openml.runs.run_model_on_task(task, clf)
450+
451+
self._test_local_evaluations(run)
452+
453+
def test_online_run_metric_score(self):
454+
openml.config.server = self.production_server
455+
run = openml.runs.get_run(5572567)
456+
self._test_local_evaluations(run)
457+
458+
450459
def test_initialize_model_from_run(self):
451460
clf = sklearn.pipeline.Pipeline(steps=[('Imputer', Imputer(strategy='median')),
452461
('VarianceThreshold', VarianceThreshold(threshold=0.05)),

0 commit comments

Comments
 (0)