Skip to content

Commit 3a23053

Browse files
committed
adds list studies
1 parent 0980673 commit 3a23053

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

openml/study/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from .study import OpenMLStudy
22
from .functions import get_study, create_study, create_benchmark_suite, \
3-
status_update, attach_to_study, detach_from_study, delete_study
3+
status_update, attach_to_study, detach_from_study, delete_study, \
4+
list_studies
45

56

67
__all__ = [
78
'OpenMLStudy', 'attach_to_study', 'create_benchmark_suite', 'create_study',
8-
'delete_study', 'detach_from_study', 'get_study', 'status_update',
9+
'delete_study', 'detach_from_study', 'get_study', 'list_studies',
10+
'status_update'
911
]

openml/study/functions.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/test_study/test_study_functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,8 @@ def test_study_attach_illegal(self):
159159
openml.study.attach_to_study(study_id, list(run_list_more.keys()))
160160
study_downloaded = openml.study.get_study(study_id)
161161
self.assertListEqual(study_original.runs, study_downloaded.runs)
162+
163+
def test_study_list(self):
164+
study_list = openml.study.list_studies(status='in_preparation')
165+
# might fail if server is recently resetted
166+
self.assertGreater(len(study_list), 2)

0 commit comments

Comments
 (0)