Skip to content

Commit 35c66b5

Browse files
authored
Merge pull request #217 from openml/develop
Add develop functions in check_run_exists
2 parents 542834a + b3262b6 commit 35c66b5

15 files changed

Lines changed: 179 additions & 436 deletions

File tree

openml/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
"""
1717
from . import config
1818

19-
from .datasets import OpenMLDataset
19+
from .datasets import OpenMLDataset, OpenMLDataFeature
2020
from . import datasets
2121
from . import runs
2222
from . import flows
2323
from .runs import OpenMLRun
2424
from .tasks import OpenMLTask, OpenMLSplit
2525
from .flows import OpenMLFlow
26-
from .utils import ConditionalImputer
2726

2827

2928
__version__ = "0.2.1"
3029

31-
__all__ = ['OpenMLDataset', 'OpenMLRun', 'OpenMLSplit', 'datasets',
32-
'OpenMLTask', 'OpenMLFlow', 'config', 'runs', 'flows']
30+
__all__ = ['OpenMLDataset', 'OpenMLDataFeature', 'OpenMLRun',
31+
'OpenMLSplit', 'datasets', 'OpenMLTask', 'OpenMLFlow',
32+
'config', 'runs', 'flows']

openml/datasets/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
from .data_feature import OpenMLDataFeature
55

66
__all__ = ['check_datasets_active', 'get_dataset', 'get_datasets',
7-
'OpenMLDataset', 'list_datasets']
7+
'OpenMLDataset', 'OpenMLDataFeature', 'list_datasets']

openml/datasets/data_feature.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class OpenMLDataFeature(object):
66
----------
77
index : int
88
The index of this feature
9-
name : string
9+
name : str
1010
Name of the feature
11-
data_type : string
11+
data_type : str
1212
can be nominal, numeric, string, date (corresponds to arff)
1313
nominal_values : list(str)
1414
list of the possible values, in case of nominal attribute
@@ -17,17 +17,18 @@ class OpenMLDataFeature(object):
1717
LEGAL_DATA_TYPES = ['nominal', 'numeric', 'string', 'date']
1818

1919
def __init__(self, index, name, data_type, nominal_values, number_missing_values):
20-
assert type(index) is int, "Index is of wrong datatype"
21-
assert type(name) is str, "Name is of wrong datatype"
22-
assert type(data_type) is str, "Data_type is of wrong datatype"
23-
assert data_type in self.LEGAL_DATA_TYPES, "data type should be in %s" %str(self.LEGAL_DATA_TYPES)
24-
if nominal_values is not None:
25-
assert type(nominal_values) is list, "Nominal_values is of wrong datatype"
26-
assert type(number_missing_values) is int, "number_missing_values is of wrong datatype"
20+
if type(index) != int:
21+
raise ValueError('Index is of wrong datatype')
22+
if data_type not in self.LEGAL_DATA_TYPES:
23+
raise ValueError('data type should be in %s, found: %s' %(str(self.LEGAL_DATA_TYPES),data_type))
24+
if nominal_values is not None and type(nominal_values) != list:
25+
raise ValueError('Nominal_values is of wrong datatype')
26+
if type(number_missing_values) != int:
27+
raise ValueError('number_missing_values is of wrong datatype')
2728

2829
self.index = index
29-
self.name = name
30-
self.data_type = data_type
30+
self.name = str(name)
31+
self.data_type = str(data_type)
3132
self.nominal_values = nominal_values
3233
self.number_missing_values = number_missing_values
3334

openml/datasets/dataset.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io
33
import logging
44
import os
5+
import six
56
import sys
67

78
import arff
@@ -10,7 +11,7 @@
1011
import scipy.sparse
1112
import xmltodict
1213

13-
from ..datasets.data_feature import OpenMLDataFeature
14+
from .data_feature import OpenMLDataFeature
1415
from ..exceptions import PyOpenMLError
1516

1617
if sys.version_info[0] >= 3:
@@ -65,10 +66,14 @@ def __init__(self, dataset_id=None, name=None, version=None, description=None,
6566
self.default_target_attribute = default_target_attribute
6667
self.row_id_attribute = row_id_attribute
6768
self.ignore_attributes = None
68-
if isinstance(ignore_attribute, str):
69+
if isinstance(ignore_attribute, six.string_types):
6970
self.ignore_attributes = [ignore_attribute]
7071
elif isinstance(ignore_attribute, list):
7172
self.ignore_attributes = ignore_attribute
73+
elif ignore_attribute is None:
74+
pass
75+
else:
76+
raise ValueError('wrong data type for ignore_attribute. Should be list. ')
7277
self.version_label = version_label
7378
self.citation = citation
7479
self.tag = tag
@@ -88,7 +93,8 @@ def __init__(self, dataset_id=None, name=None, version=None, description=None,
8893
xmlfeature['oml:data_type'],
8994
None, #todo add nominal values (currently not in database)
9095
int(xmlfeature['oml:number_of_missing_values']))
91-
assert idx == feature.index, "Data features not provided in right order"
96+
if idx != feature.index:
97+
raise ValueError('Data features not provided in right order')
9298
self.features[feature.index] = feature
9399

94100

@@ -313,15 +319,40 @@ def retrieve_class_labels(self, target_name='class'):
313319
return None
314320

315321

316-
def get_features_by_type(self, data_type, exclude=None, exclude_ignore_attributes=True, exclude_row_id_attribute=True):
322+
def get_features_by_type(self, data_type, exclude=None,
323+
exclude_ignore_attributes=True,
324+
exclude_row_id_attribute=True):
325+
'''
326+
Returns indices of features of a given type, e.g., all nominal features.
327+
Can use additional parameters to exclude various features by index or ontology.
328+
329+
Parameters
330+
----------
331+
data_type : str
332+
The data type to return (e.g., nominal, numeric, date, string)
333+
exclude : list(int)
334+
Indices to exclude (and adapt the return values as if these indices
335+
are not present)
336+
exclude_ignore_attributes : bool
337+
Whether to exclude the defined ignore attributes (and adapt the
338+
return values as if these indices are not present)
339+
exclude_row_id_attribute : bool
340+
Whether to exclude the defined row id attributes (and adapt the
341+
return values as if these indices are not present)
342+
343+
Returns
344+
-------
345+
result : list
346+
a list of indices that have the specified data type
347+
'''
317348
assert data_type in OpenMLDataFeature.LEGAL_DATA_TYPES, "Illegal feature type requested"
318349
if self.ignore_attributes is not None:
319350
assert type(self.ignore_attributes) is list, "ignore_attributes should be a list"
320351
if self.row_id_attribute is not None:
321352
assert type(self.row_id_attribute) is str, "row id attribute should be a str"
322353
if exclude is not None:
323354
assert type(exclude) is list, "Exclude should be a list"
324-
assert all(isinstance(elem, str) for elem in exclude), "Exclude should be a list of strings"
355+
# assert all(isinstance(elem, str) for elem in exclude), "Exclude should be a list of strings"
325356
to_exclude = []
326357
if exclude is not None:
327358
to_exclude.extend(exclude)

openml/flows/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def list_flows(offset=None, size=None, tag=None):
6666
if tag is not None:
6767
api_call += "/tag/%s" % tag
6868

69-
return _list_datasets(api_call)
69+
return _list_flows(api_call)
7070

7171

72-
def _list_datasets(api_call):
72+
def _list_flows(api_call):
7373
# TODO add proper error handling here!
7474
xml_string = _perform_api_call(api_call)
7575
flows_dict = xmltodict.parse(xml_string)

openml/runs/functions.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import openml
88
from sklearn.model_selection._search import BaseSearchCV
99

10-
from build.lib.openml.exceptions import PyOpenMLError
10+
from ..exceptions import PyOpenMLError
1111
from .. import config
1212
from ..flows import sklearn_to_flow, get_flow
1313
from ..setups import setup_exists
@@ -68,12 +68,7 @@ def run_task(task, model):
6868

6969
# execute the run
7070
run = OpenMLRun(task_id=task.task_id, flow_id=None, dataset_id=dataset.dataset_id, model=model)
71-
72-
try:
73-
run.data_content, run.trace_content = _run_task_get_arffcontent(model, task, class_labels)
74-
except PyOpenMLError as message:
75-
run.error_message = str(message)
76-
warnings.warn("Run terminated with error: %s" %run.error_message)
71+
run.data_content, run.trace_content = _run_task_get_arffcontent(model, task, class_labels)
7772

7873
if flow_id < 0:
7974
flow.publish()
@@ -110,8 +105,10 @@ def _run_exists(task_id, setup_id):
110105

111106

112107

113-
def _prediction_to_row(rep_no, fold_no, row_id, correct_label, predicted_label, predicted_probabilities, class_labels, model_classes_mapping):
114-
"""Complicated util function that turns probability estimates of a classifier for a given instance into the right arff format to upload to openml.
108+
def _prediction_to_row(rep_no, fold_no, row_id, correct_label, predicted_label,
109+
predicted_probabilities, class_labels, model_classes_mapping):
110+
"""Util function that turns probability estimates of a classifier for a given
111+
instance into the right arff format to upload to openml.
115112
116113
Parameters
117114
----------
@@ -126,6 +123,9 @@ def _prediction_to_row(rep_no, fold_no, row_id, correct_label, predicted_label,
126123
predicted_probabilities : array (size=num_classes)
127124
probabilities per class
128125
class_labels : array (size=num_classes)
126+
model_classes_mapping : list
127+
A list of classes the model produced.
128+
Obtained by BaseEstimator.classes_
129129
130130
Returns
131131
-------
@@ -162,17 +162,13 @@ def _run_task_get_arffcontent(model, task, class_labels):
162162
testX = X[test_indices]
163163
testY = Y[test_indices]
164164

165-
try:
166-
model.fit(trainX, trainY)
167-
168-
if isinstance(model, BaseSearchCV):
169-
_add_results_to_arfftrace(arff_tracecontent, fold_no, model, rep_no)
170-
model_classes = model.best_estimator_.classes_
171-
else:
172-
model_classes = model.classes_
173-
except AttributeError as e:
174-
# typically happens when training a regressor on classification task
175-
raise PyOpenMLError(str(e))
165+
model.fit(trainX, trainY)
166+
167+
if isinstance(model, BaseSearchCV):
168+
_add_results_to_arfftrace(arff_tracecontent, fold_no, model, rep_no)
169+
model_classes = model.best_estimator_.classes_
170+
else:
171+
model_classes = model.classes_
176172

177173
ProbaY = model.predict_proba(testX)
178174
PredY = model.predict(testX)

openml/utils/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)