Skip to content

Commit 675031a

Browse files
committed
fixed the issue on the api_call method
1 parent 1dabd97 commit 675031a

1 file changed

Lines changed: 5 additions & 190 deletions

File tree

openml/apiconnector.py

Lines changed: 5 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -818,191 +818,8 @@ def _create_task_cache_dir(self, task_id):
818818
return task_cache_dir
819819

820820
def _perform_api_call(self, call, data=None, file_dictionary=None, add_authentication=True):
821-
############################################################################
822-
# Runs
823-
def get_runs_list(self, task_id=None, flow_id=None, setup_id=None):
824-
"""Return a list of all runs for either a task, flow or setup.
825-
826-
Exactly one of the optional parameters must be given.
827-
828-
Parameters
829-
----------
830-
task_id : int, optional
831-
flow_id : int, optional
832-
setup_id : int, optional
833-
834-
Returns
835-
-------
836-
list
837-
A list of all runs for a given ID.
838-
"""
839-
test = [task_id is None, flow_id is None, setup_id is None]
840-
if np.nansum(test) != 2:
841-
raise ValueError
842-
843-
call = "run/list"
844-
845-
if task_id is not None:
846-
call += "?task_id=%d" % task_id
847-
elif flow_id is not None:
848-
call += "?flow_id=%d" % flow_id
849-
elif setup_id is not None:
850-
call += "?setup_id=%d" % setup_id
851-
852-
return_code, xml_string = self._perform_api_call(call)
853-
datasets_dict = xmltodict.parse(xml_string)
854-
855-
856-
if isinstance(datasets_dict['oml:runs']['oml:run'], dict):
857-
runs = [datasets_dict['oml:runs']['oml:run']]
858-
else:
859-
# Minimalistic check if the XML is useful
860-
assert type(datasets_dict['oml:runs']['oml:run']) == list, \
861-
type(datasets_dict['oml:runs']['oml:run'])
862-
assert datasets_dict['oml:runs']['@xmlns:oml'] == \
863-
'http://openml.org/openml'
864-
865-
runs = []
866-
for runs_ in datasets_dict['oml:runs']['oml:run']:
867-
run = {'run_id': int(runs_['oml:run_id']),
868-
'task_id': int(runs_['oml:task_id']),
869-
'setup_id': int(runs_['oml:setup_id']),
870-
'flow_id': int(runs_['oml:flow_id']),
871-
'uploader': int(runs_['oml:uploader']),
872-
'error_message': runs_['oml:error_message']}
873-
874-
runs.append(run)
875-
runs.sort(key=lambda t: t['run_id'])
876-
877-
return runs
878-
879-
def download_run(self, run_id):
880-
"""Download the OpenML run for a given run ID.
881-
882-
Parameters
883-
----------
884-
run_id : int
885-
The OpenML run id.
886-
"""
887-
try:
888-
run_id = int(run_id)
889-
except:
890-
raise ValueError("Task ID is neither an Integer nor can be "
891-
"cast to an Integer.")
892-
893-
xml_file = os.path.join(self._create_run_cache_dir(run_id),
894-
"run.xml")
895-
896-
try:
897-
with open(xml_file) as fh:
898-
run = self._create_run_from_xml(fh.read())
899-
except (OSError, IOError):
900-
901-
try:
902-
return_code, run_xml = self._perform_api_call(
903-
"run/%d" % run_id)
904-
except (URLError, UnicodeEncodeError) as e:
905-
print(e)
906-
raise e
907-
908-
# Cache the xml task file
909-
if os.path.exists(xml_file):
910-
with open(xml_file) as fh:
911-
local_xml = fh.read()
912-
913-
if run_xml != local_xml:
914-
raise ValueError("Run description of run %d cached at %s "
915-
"has changed." % (run_id, xml_file))
916-
917-
else:
918-
with open(xml_file, "w") as fh:
919-
fh.write(run_xml)
920-
921-
run = self._create_run_from_xml(run_xml)
922-
923-
return run
924-
925-
def _create_run_cache_dir(self, run_id):
926-
run_cache_dir = os.path.join(self.task_cache_dir, str(run_id))
927-
928-
try:
929-
os.makedirs(run_cache_dir)
930-
except (IOError, OSError):
931-
# TODO add debug information!
932-
pass
933-
return run_cache_dir
934-
935-
def _create_run_from_xml(self, xml):
936-
dic = xmltodict.parse(xml)[u"oml:run"]
937-
datasets = []
938-
for key in dic[u'oml:input_data']:
939-
dataset = dic[u'oml:input_data'][key]
940-
did = dataset[u'oml:did']
941-
datasets.append(did)
942-
943-
tags = []
944-
for tag in dic[u"oml:tag"]:
945-
tags.append(tag)
946-
947-
files = dict()
948-
for file_ in dic[u"oml:output_data"][u"oml:file"]:
949-
name = file_[u"oml:name"]
950-
url = file_[u"oml:url"]
951-
files[name] = url
952-
953-
evaluations = dict()
954-
for evaluation in dic[u"oml:output_data"][u"oml:evaluation"]:
955-
name = evaluation[u"oml:name"]
956-
value = evaluation.get(u"oml:value")
957-
value_array = evaluation.get(u"oml:array_data")
958-
evaluations[name] = (value, value_array)
959-
960-
return OpenMLRun(
961-
dic[u"oml:run_id"], dic[u"oml:uploader"],
962-
dic[u"oml:task_id"], dic[u"oml:flow_id"],
963-
dic[u"oml:setup_string"], dic[u'oml:setup_id'],
964-
tags, datasets, files, evaluations)
965-
966-
############################################################################
967-
# Flows
968-
def get_flow_list(self):
969-
"""Return a list of all flows on OpenML.
970-
971-
Returns
972-
-------
973-
list
974-
A list of all flows.
975821
"""
976-
return_code, xml_string = self._perform_api_call("/flow/list")
977-
datasets_dict = xmltodict.parse(xml_string)
978-
979-
if isinstance(datasets_dict['oml:flows']['oml:flow'], dict):
980-
flows = [datasets_dict['oml:implementations']['oml:implementation']]
981-
else:
982-
# Minimalistic check if the XML is useful
983-
assert type(datasets_dict['oml:flows']['oml:flow']) == list, \
984-
type(datasets_dict['oml:flows']['oml:flow'])
985-
assert datasets_dict['oml:flows']['@xmlns:oml'] == \
986-
'http://openml.org/openml'
987-
988-
flows = []
989-
for flow_ in datasets_dict['oml:flows']['oml:flow']:
990-
flow = {'id': int(flow_['oml:id']),
991-
'full_name': flow_['oml:full_name'],
992-
'name': flow_['oml:name'],
993-
'version': flow_['oml:version'],
994-
'external_version': flow_['oml:external_version'],
995-
'uploader': int(flow_['oml:uploader'])}
996-
997-
flows.append(flow)
998-
flows.sort(key=lambda t: t['id'])
999-
1000-
return flows
1001-
1002-
############################################################################
1003-
# Internal stuff
1004-
def _perform_api_call(self, call, data=None, file_path=None):
1005-
"""Perform an API call at the OpenML server.
822+
Perform an API call at the OpenML server.
1006823
return self._read_url(url, data=data, filePath=filePath,
1007824
def _read_url(self, url, add_authentication=False, data=None, filePath=None):
1008825
@@ -1047,12 +864,10 @@ def _read_url(self, url, data=None, file_dictionary=None):
1047864
raise ValueError("The file you have provided is not a valid arff file")
1048865

1049866
file_elements[key] = open(path, 'rb')
1050-
except URLError as error:
1051-
print(error)
1052867

1053868
else:
1054869
raise ValueError("File doesn't exist")
1055-
870+
try:
1056871
response = requests.post(url, data=data, files=file_elements)
1057872
return response.status_code, response
1058873

@@ -1104,6 +919,8 @@ def upload_dataset(self, description, file_path=None):
1104919
data = {'description': description}
1105920
if file_path is not None:
1106921
return_code, dataset_xml = self._perform_api_call("/data/",data=data, file_dictionary={'dataset': file_path})
922+
else:
923+
return_code, dataset_xml = self._perform_api_call("/data/",data=data)
1107924

1108925
except URLError as e:
1109926
# TODO logger.debug
@@ -1137,6 +954,4 @@ def upload_run(self, files):
1137954
raise e
1138955
return return_code, dataset_xml
1139956
else:
1140-
raise ValueError("prediction files doesn't exist")
1141-
1142-
957+
raise ValueError("prediction files doesn't exist")

0 commit comments

Comments
 (0)