44import xmltodict
55
66from .setup import OpenMLSetup , OpenMLParameter
7- from openml .flows import sklearn_to_flow , flow_exists
7+ from openml .flows import flow_exists
88
99
1010def setup_exists (flow , model = None ):
@@ -74,6 +74,80 @@ def get_setup(setup_id):
7474 return _create_setup_from_xml (result_dict )
7575
7676
77+ def list_setups (flow = None , tag = None , setup = None , offset = None , size = None ):
78+ """List all setups matching all of the given filters.
79+
80+ Perform API call `/setup/list/{filters}
81+
82+ Parameters
83+ ----------
84+ flow : int, optional
85+
86+ tag : str, optional
87+
88+ setup : list(int), optional
89+
90+ offset : int, optional
91+
92+ size : int, optional
93+
94+ Returns
95+ -------
96+ list
97+ List of found setups.
98+ """
99+
100+ api_call = "setup/list"
101+ if offset is not None :
102+ api_call += "/offset/%d" % int (offset )
103+ if size is not None :
104+ api_call += "/limit/%d" % int (size )
105+ if setup is not None :
106+ api_call += "/setup/%s" % ',' .join ([str (int (i )) for i in setup ])
107+ if flow is not None :
108+ api_call += "/flow/%s" % flow
109+ if tag is not None :
110+ api_call += "/tag/%s" % tag
111+
112+ return _list_setups (api_call )
113+
114+
115+ def _list_setups (api_call ):
116+ """Helper function to parse API calls which are lists of setups"""
117+
118+ xml_string = openml ._api_calls ._perform_api_call (api_call )
119+
120+ setups_dict = xmltodict .parse (xml_string )
121+ # Minimalistic check if the XML is useful
122+ if 'oml:setups' not in setups_dict :
123+ raise ValueError ('Error in return XML, does not contain "oml:setups": %s'
124+ % str (setups_dict ))
125+ elif '@xmlns:oml' not in setups_dict ['oml:setups' ]:
126+ raise ValueError ('Error in return XML, does not contain '
127+ '"oml:setups"/@xmlns:oml: %s'
128+ % str (setups_dict ))
129+ elif setups_dict ['oml:setups' ]['@xmlns:oml' ] != 'http://openml.org/openml' :
130+ raise ValueError ('Error in return XML, value of '
131+ '"oml:seyups"/@xmlns:oml is not '
132+ '"http://openml.org/openml": %s'
133+ % str (setups_dict ))
134+
135+ if isinstance (setups_dict ['oml:setups' ]['oml:setup' ], list ):
136+ setups_list = setups_dict ['oml:setups' ]['oml:setup' ]
137+ elif isinstance (setups_dict ['oml:setups' ]['oml:setup' ], dict ):
138+ setups_list = [setups_dict ['oml:setups' ]['oml:setup' ]]
139+ else :
140+ raise TypeError ()
141+
142+ setups = dict ()
143+ for setup_ in setups_list :
144+ # making it a dict to give it the right format
145+ current = _create_setup_from_xml ({'oml:setup_parameters' : setup_ })
146+ setups [current .setup_id ] = current
147+
148+ return setups
149+
150+
77151def initialize_model (setup_id ):
78152 '''
79153 Initialized a model based on a setup_id (i.e., using the exact
@@ -147,6 +221,7 @@ def _create_setup_from_xml(result_dict):
147221 '''
148222 Turns an API xml result into a OpenMLSetup object
149223 '''
224+ setup_id = int (result_dict ['oml:setup_parameters' ]['oml:setup_id' ])
150225 flow_id = int (result_dict ['oml:setup_parameters' ]['oml:flow_id' ])
151226 parameters = {}
152227 if 'oml:parameter' not in result_dict ['oml:setup_parameters' ]:
@@ -164,7 +239,7 @@ def _create_setup_from_xml(result_dict):
164239 else :
165240 raise ValueError ('Expected None, list or dict, received someting else: %s' % str (type (xml_parameters )))
166241
167- return OpenMLSetup (flow_id , parameters )
242+ return OpenMLSetup (setup_id , flow_id , parameters )
168243
169244def _create_setup_parameter_from_xml (result_dict ):
170245 return OpenMLParameter (int (result_dict ['oml:id' ]),
@@ -173,4 +248,4 @@ def _create_setup_parameter_from_xml(result_dict):
173248 result_dict ['oml:parameter_name' ],
174249 result_dict ['oml:data_type' ],
175250 result_dict ['oml:default_value' ],
176- result_dict ['oml:value' ])
251+ result_dict ['oml:value' ])
0 commit comments