Skip to content

Commit de999ad

Browse files
committed
added setup caching
1 parent 7fdb403 commit de999ad

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

openml/runs/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ def _create_run_from_xml(xml):
620620
if 'oml:output_data' not in run:
621621
raise ValueError('Run does not contain output_data (OpenML server error?)')
622622

623-
if 'oml:file' in 'oml:output_data':
623+
if 'oml:file' in run['oml:output_data']:
624624
if isinstance(run['oml:output_data']['oml:file'], dict):
625625
# only one result.. probably due to an upload error
626626
file_dict = run['oml:output_data']['oml:file']

openml/setups/functions.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from collections import OrderedDict
22

3+
import io
34
import openml
5+
import os
46
import xmltodict
57

8+
from .. import config
69
from .setup import OpenMLSetup, OpenMLParameter
710
from openml.flows import flow_exists
811

@@ -54,8 +57,23 @@ def setup_exists(flow, model=None):
5457
return False
5558

5659

60+
def _get_cached_setup(setup_id):
61+
"""Load a run from the cache."""
62+
cache_dir = config.get_cache_directory()
63+
setup_cache_dir = os.path.join(cache_dir, "setups")
64+
try:
65+
setup_file = os.path.join(setup_cache_dir, "setup_%d.xml" % int(setup_id))
66+
with io.open(setup_file, encoding='utf8') as fh:
67+
setup_xml = xmltodict.parse(fh.read())
68+
setup = _create_setup_from_xml(setup_xml)
69+
return setup
70+
71+
except (OSError, IOError):
72+
raise openml.exceptions.OpenMLCacheException("Setup file for setup id %d not cached" % setup_id)
73+
74+
5775
def get_setup(setup_id):
58-
'''
76+
"""
5977
Downloads the setup (configuration) description from OpenML
6078
and returns a structured object
6179
@@ -68,9 +86,18 @@ def get_setup(setup_id):
6886
-------
6987
OpenMLSetup
7088
an initialized openml setup object
71-
'''
72-
result = openml._api_calls._perform_api_call('/setup/%d' %setup_id)
73-
result_dict = xmltodict.parse(result)
89+
"""
90+
run_file = os.path.join(config.get_cache_directory(), "setups", "setup_%d.xml" % setup_id)
91+
92+
try:
93+
return _get_cached_setup(setup_id)
94+
95+
except (openml.exceptions.OpenMLCacheException):
96+
setup_xml = openml._api_calls._perform_api_call('/setup/%d' % setup_id)
97+
with io.open(run_file, "w", encoding='utf8') as fh:
98+
fh.write(setup_xml)
99+
100+
result_dict = xmltodict.parse(setup_xml)
74101
return _create_setup_from_xml(result_dict)
75102

76103

@@ -217,6 +244,7 @@ def _to_dict(flow_id, openml_parameter_settings):
217244

218245
return xml
219246

247+
220248
def _create_setup_from_xml(result_dict):
221249
'''
222250
Turns an API xml result into a OpenMLSetup object

tests/test_setups/test_setup_functions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,14 @@ def test_setuplist_offset(self):
140140

141141
all = set(setups.keys()).union(setups2.keys())
142142

143-
self.assertEqual(len(all), size * 2)
143+
self.assertEqual(len(all), size * 2)
144+
145+
def test_get_cached_setup(self):
146+
openml.config.set_cache_directory(self.static_cache_dir)
147+
openml.setups.functions._get_cached_setup(1)
148+
149+
150+
def test_get_uncached_setup(self):
151+
openml.config.set_cache_directory(self.static_cache_dir)
152+
with self.assertRaises(openml.exceptions.OpenMLCacheException):
153+
openml.setups.functions._get_cached_setup(10)

0 commit comments

Comments
 (0)