Skip to content

Commit d0e0c1e

Browse files
committed
Added upload flow function
1 parent 652b2c4 commit d0e0c1e

2 files changed

Lines changed: 34 additions & 36 deletions

File tree

openml/apiconnector.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ def _create_task_cache_dir(self, task_id):
816816
pass
817817
return task_cache_dir
818818

819+
def _perform_api_call(self, call, data=None, file_path=None, add_authentication=True):
819820
############################################################################
820821
# Runs
821822
def get_runs_list(self, task_id=None, flow_id=None, setup_id=None):
@@ -996,25 +997,29 @@ def _read_url(self, url, data=None, file_path=None):
996997
data['api_key'] = self.config.get('FAKE_SECTION', 'apikey')
997998

998999
if file_path is not None:
999-
if os.path.isabs(file_path):
1000-
try:
1001-
decoder = arff.ArffDecoder()
1002-
except:
1003-
raise "The file you provided is not a valid arff file"
1004-
1005-
fileElement={'dataset': open(file_path, 'rb')}
1006-
data['description']= data.get('description')
1007-
data.pop('dataset', None)
1008-
1009-
try:
1010-
response = requests.post(url, data=data, files=fileElement)
1000+
file_elements = {}
1001+
for key, path in file_path.items():
1002+
if os.path.isabs(path) and os.path.exists(path):
1003+
try:
1004+
if key is 'dataset':
1005+
decoder = arff.ArffDecoder()
1006+
with open(path) as fh:
1007+
decoder.decode(fh, encode_nominal=True)
1008+
except:
1009+
raise ValueError("The file you have provided is not a valid arff file")
1010+
1011+
file_elements[key] = open(path, 'rb')
10111012
except URLError as error:
10121013
print(error)
10131014

1015+
else:
1016+
raise ValueError("File doesn't exist")
1017+
1018+
response = requests.post(url, data=data, files=file_elements)
10141019
return response.status_code, response
1015-
else:
1016-
raise "File doesn't exists"
10171020

1021+
except URLError as error:
1022+
print(error)
10181023
else:
10191024
data = urlencode(data)
10201025
data = data.encode('utf-8')
@@ -1059,20 +1064,19 @@ def _read_url(self, url, data=None, file_path=None):
10591064
def upload_dataset(self, description, file_path=None):
10601065
try:
10611066
data = {'description': description}
1062-
return_code, dataset_xml = self._perform_api_call(
1063-
"/data/", data=data, file_path=file_path)
1067+
if file_path is not None:
1068+
return_code, dataset_xml = self._perform_api_call("/data/",data=data, file_path={'dataset':file_path})
10641069

10651070
except URLError as e:
10661071
# TODO logger.debug
10671072
print(e)
10681073
raise e
10691074
return return_code, dataset_xml
10701075

1071-
def upload_flow(self, description, binary, source):
1076+
def upload_flow(self, description, file_path=None):
10721077
try:
1073-
data = {'description': description, 'binary': binary, 'source': source}
1074-
return_code, dataset_xml = self._perform_api_call(
1075-
"openml.implementation.upload", data=data)
1078+
data = {'description': description}
1079+
return_code, dataset_xml = self._perform_api_call("/flow/", data=data, file_path={'source':file_path})
10761080

10771081
except URLError as e:
10781082
# TODO logger.debug

tests/test_apiconnector.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ def setUp(self):
3939
os.chdir(self.workdir)
4040

4141
self.cached = True
42-
42+
self.connector = APIConnector(cache_directory=self.workdir)
4343
try:
4444
apikey = os.environ['OPENMLAPIKEY']
4545
except:
4646
apikey = None
47-
self.connector = APIConnector(cache_directory=self.workdir,
48-
apikey=apikey)
4947

5048
def tearDown(self):
5149
os.chdir(self.cwd)
@@ -257,7 +255,7 @@ def test_download_run(self):
257255
def test_upload_dataset(self):
258256

259257
dataset = self.connector.download_dataset(3)
260-
filePath = os.path.join(self.connector.dataset_cache_dir, "3", "dataset.arff")
258+
file_path = os.path.join(self.connector.dataset_cache_dir, "3", "dataset.arff")
261259

262260
description = """ <oml:data_set_description xmlns:oml="http://openml.org/openml">
263261
<oml:name>anneal</oml:name>
@@ -269,7 +267,7 @@ def test_upload_dataset(self):
269267
<oml:md5_checksum></oml:md5_checksum>
270268
</oml:data_set_description>
271269
"""
272-
return_code, dataset_xml = self.connector.upload_dataset(description, filePath)
270+
return_code, dataset_xml = self.connector.upload_dataset(description, file_path)
273271
self.assertEqual(return_code, 200)
274272

275273
def test_upload_dataset_with_url(self):
@@ -282,20 +280,16 @@ def test_upload_dataset_with_url(self):
282280
<oml:url>http://expdb.cs.kuleuven.be/expdb/data/uci/nominal/iris.arff</oml:url>
283281
</oml:data_set_description>
284282
"""
285-
return_code, dataset_xml = self.connector.upload_dataset (description)
283+
return_code, dataset_xml = self.connector.upload_dataset(description)
286284
self.assertEqual(return_code, 200)
287285

288286
def test_upload_flow(self):
289-
290-
description = """ <oml:data_set_description xmlns:oml="http://openml.org/openml">
291-
<oml:name>UploadTestWithURL</oml:name>
292-
<oml:version>1</oml:version>
293-
<oml:description>test</oml:description>
294-
<oml:format>ARFF</oml:format>
295-
<oml:url>http://expdb.cs.kuleuven.be/expdb/data/uci/nominal/iris.arff</oml:url>
296-
</oml:data_set_description>
297-
"""
298-
return_code, dataset_xml = self.connector.upload_dataset (description)
287+
file_path = os.path.join(self.connector.dataset_cache_dir,"uploadflow.txt")
288+
file = open(file_path, "w")
289+
file.write("Testing upload flow")
290+
file.close()
291+
description = '''<oml:flow xmlns:oml="http://openml.org/openml"><oml:name>Test</oml:name><oml:description>description</oml:description> </oml:flow>'''
292+
return_code, dataset_xml = self.connector.upload_flow(description, file_path)
299293
self.assertEqual(return_code, 200)
300294

301295

0 commit comments

Comments
 (0)