@@ -278,3 +278,100 @@ def detach_from_study(study_id, entity_ids):
278278 post_variables )
279279 result = xmltodict .parse (result_xml )['oml:study_detach' ]
280280 return int (result ['oml:linked_entities' ])
281+
282+
283+ def list_studies (offset = None , size = None , main_entity_type = None ,status = None ,
284+ uploader = None ):
285+ """
286+ Return a list of all studies which are on OpenML.
287+
288+ Parameters
289+ ----------
290+ offset : int, optional
291+ The number of studies to skip, starting from the first.
292+ size : int, optional
293+ The maximum number of studies to show.
294+ main_entity_type : str, optional
295+ Can be `task` or `run`. In case of `task`, only benchmark suites are
296+ returned. In case of `run`, only studies are returned.
297+ status : str, optional
298+ Should be {active, in_preparation, deactivated, all}. By default active
299+ studies are returned.
300+ uploader : list (int), optional
301+ Result filter. Will only return studies created by these users.
302+
303+ Returns
304+ -------
305+ datasets : dict of dicts
306+ A mapping from dataset ID to dict.
307+
308+ Every dataset is represented by a dictionary containing
309+ the following information:
310+ - id
311+ - name
312+ - main_entity_type
313+ - status
314+ - creator
315+ - creation_date
316+
317+ If qualities are calculated for the dataset, some of
318+ these are also returned.
319+ """
320+ return openml .utils ._list_all (_list_studies ,
321+ offset = offset ,
322+ size = size ,
323+ main_entity_type = main_entity_type ,
324+ status = status ,
325+ uploader = uploader )
326+
327+
328+ def _list_studies (** kwargs ):
329+ """
330+ Perform api call to return a list of studies.
331+
332+ Parameters
333+ ----------
334+ kwargs : dict, optional
335+ Legal filter operators (keys in the dict):
336+ status, limit, offset, main_entity_type, uploader
337+
338+ Returns
339+ -------
340+ studies : dict of dicts
341+ """
342+ api_call = "study/list"
343+ if kwargs is not None :
344+ for operator , value in kwargs .items ():
345+ api_call += "/%s/%s" % (operator , value )
346+ return __list_studies (api_call )
347+
348+
349+ def __list_studies (api_call ):
350+ xml_string = openml ._api_calls ._perform_api_call (api_call , 'get' )
351+ study_dict = xmltodict .parse (xml_string , force_list = ('oml:study' ,))
352+
353+ # Minimalistic check if the XML is useful
354+ assert type (study_dict ['oml:study_list' ]['oml:study' ]) == list , \
355+ type (study_dict ['oml:study_list' ])
356+ assert study_dict ['oml:study_list' ]['@xmlns:oml' ] == \
357+ 'http://openml.org/openml' , study_dict ['oml:study_list' ]['@xmlns:oml' ]
358+
359+ studies = dict ()
360+ for study_ in study_dict ['oml:study_list' ]['oml:study' ]:
361+ expected_fields = {
362+ 'oml:id' : 'id' ,
363+ 'oml:alias' : 'alias' ,
364+ 'oml:main_entity_type' : 'main_entity_type' ,
365+ 'oml:name' : 'name' ,
366+ 'oml:status' : 'status' ,
367+ 'oml:creation_date' : 'creation_date' ,
368+ 'oml:creator' : 'creator'
369+ }
370+ study_id = int (study_ ['oml:id' ])
371+ current_study = dict ()
372+ for oml_field_name , real_field_name in expected_fields .items ():
373+ if oml_field_name in study_ :
374+ current_study [real_field_name ] = study_ [oml_field_name ]
375+ current_study ['id' ] = int (current_study ['id' ])
376+ studies [study_id ] = current_study
377+ return studies
0 commit comments