11from collections import OrderedDict
22
3+ import io
34import openml
5+ import os
46import xmltodict
57
8+ from .. import config
69from .setup import OpenMLSetup , OpenMLParameter
710from 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+
5775def 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+
220248def _create_setup_from_xml (result_dict ):
221249 '''
222250 Turns an API xml result into a OpenMLSetup object
0 commit comments