11import json
22import xmltodict
3+ import pandas as pd
4+ from typing import Union , List , Optional , Dict
35
46import openml .utils
57import openml ._api_calls
68from ..evaluations import OpenMLEvaluation
79
810
9- def list_evaluations (function , offset = None , size = None , id = None , task = None ,
10- setup = None , flow = None , uploader = None , tag = None ,
11- per_fold = None ):
11+ def list_evaluations (
12+ function : str ,
13+ offset : Optional [int ] = None ,
14+ size : Optional [int ] = None ,
15+ id : Optional [List ] = None ,
16+ task : Optional [List ] = None ,
17+ setup : Optional [List ] = None ,
18+ flow : Optional [List ] = None ,
19+ uploader : Optional [List ] = None ,
20+ tag : Optional [str ] = None ,
21+ per_fold : Optional [bool ] = None ,
22+ output_format : str = 'object'
23+ ) -> Union [Dict , pd .DataFrame ]:
1224 """
1325 List all run-evaluation pairs matching all of the given filters.
1426 (Supports large amount of results)
@@ -36,21 +48,48 @@ def list_evaluations(function, offset=None, size=None, id=None, task=None,
3648
3749 per_fold : bool, optional
3850
51+ output_format: str, optional (default='object')
52+ The parameter decides the format of the output.
53+ - If 'object' the output is a dict of OpenMLEvaluation objects
54+ - If 'dict' the output is a dict of dict
55+ - If 'dataframe' the output is a pandas DataFrame
56+
3957 Returns
4058 -------
41- dict
59+ dict or dataframe
4260 """
43- if per_fold is not None :
44- per_fold = str (per_fold ).lower ()
45-
46- return openml .utils ._list_all (_list_evaluations , function , offset = offset ,
47- size = size , id = id , task = task , setup = setup ,
48- flow = flow , uploader = uploader , tag = tag ,
49- per_fold = per_fold )
61+ if output_format not in ['dataframe' , 'dict' , 'object' ]:
62+ raise ValueError ("Invalid output format selected. "
63+ "Only 'object', 'dataframe', or 'dict' applicable." )
5064
51-
52- def _list_evaluations (function , id = None , task = None ,
53- setup = None , flow = None , uploader = None , ** kwargs ):
65+ per_fold_str = None
66+ if per_fold is not None :
67+ per_fold_str = str (per_fold ).lower ()
68+
69+ return openml .utils ._list_all (output_format = output_format ,
70+ listing_call = _list_evaluations ,
71+ function = function ,
72+ offset = offset ,
73+ size = size ,
74+ id = id ,
75+ task = task ,
76+ setup = setup ,
77+ flow = flow ,
78+ uploader = uploader ,
79+ tag = tag ,
80+ per_fold = per_fold_str )
81+
82+
83+ def _list_evaluations (
84+ function : str ,
85+ id : Optional [List ] = None ,
86+ task : Optional [List ] = None ,
87+ setup : Optional [List ] = None ,
88+ flow : Optional [List ] = None ,
89+ uploader : Optional [List ] = None ,
90+ output_format : str = 'object' ,
91+ ** kwargs
92+ ) -> Union [Dict , pd .DataFrame ]:
5493 """
5594 Perform API call ``/evaluation/function{function}/{filters}``
5695
@@ -75,9 +114,17 @@ def _list_evaluations(function, id=None, task=None,
75114 kwargs: dict, optional
76115 Legal filter operators: tag, limit, offset.
77116
117+ output_format: str, optional (default='dict')
118+ The parameter decides the format of the output.
119+ - If 'dict' the output is a dict of dict
120+ The parameter decides the format of the output.
121+ - If 'dict' the output is a dict of dict
122+ - If 'dataframe' the output is a pandas DataFrame
123+ - If 'dataframe' the output is a pandas DataFrame
124+
78125 Returns
79126 -------
80- dict
127+ dict of objects, or dataframe
81128 """
82129
83130 api_call = "evaluation/list/function/%s" % function
@@ -95,10 +142,10 @@ def _list_evaluations(function, id=None, task=None,
95142 if uploader is not None :
96143 api_call += "/uploader/%s" % ',' .join ([str (int (i )) for i in uploader ])
97144
98- return __list_evaluations (api_call )
145+ return __list_evaluations (api_call , output_format = output_format )
99146
100147
101- def __list_evaluations (api_call ):
148+ def __list_evaluations (api_call , output_format = 'object' ):
102149 """Helper function to parse API calls which are lists of runs"""
103150 xml_string = openml ._api_calls ._perform_api_call (api_call , 'get' )
104151 evals_dict = xmltodict .parse (xml_string , force_list = ('oml:evaluation' ,))
@@ -123,15 +170,33 @@ def __list_evaluations(api_call):
123170 if 'oml:array_data' in eval_ :
124171 array_data = eval_ ['oml:array_data' ]
125172
126- evals [run_id ] = OpenMLEvaluation (int (eval_ ['oml:run_id' ]),
127- int (eval_ ['oml:task_id' ]),
128- int (eval_ ['oml:setup_id' ]),
129- int (eval_ ['oml:flow_id' ]),
130- eval_ ['oml:flow_name' ],
131- eval_ ['oml:data_id' ],
132- eval_ ['oml:data_name' ],
133- eval_ ['oml:function' ],
134- eval_ ['oml:upload_time' ],
135- value , values , array_data )
173+ if output_format == 'object' :
174+ evals [run_id ] = OpenMLEvaluation (int (eval_ ['oml:run_id' ]),
175+ int (eval_ ['oml:task_id' ]),
176+ int (eval_ ['oml:setup_id' ]),
177+ int (eval_ ['oml:flow_id' ]),
178+ eval_ ['oml:flow_name' ],
179+ eval_ ['oml:data_id' ],
180+ eval_ ['oml:data_name' ],
181+ eval_ ['oml:function' ],
182+ eval_ ['oml:upload_time' ],
183+ value , values , array_data )
184+ else :
185+ # for output_format in ['dict', 'dataframe']
186+ evals [run_id ] = {'run_id' : int (eval_ ['oml:run_id' ]),
187+ 'task_id' : int (eval_ ['oml:task_id' ]),
188+ 'setup_id' : int (eval_ ['oml:setup_id' ]),
189+ 'flow_id' : int (eval_ ['oml:flow_id' ]),
190+ 'flow_name' : eval_ ['oml:flow_name' ],
191+ 'data_id' : eval_ ['oml:data_id' ],
192+ 'data_name' : eval_ ['oml:data_name' ],
193+ 'function' : eval_ ['oml:function' ],
194+ 'upload_time' : eval_ ['oml:upload_time' ],
195+ 'value' : value ,
196+ 'values' : values ,
197+ 'array_data' : array_data }
198+
199+ if output_format == 'dataframe' :
200+ evals = pd .DataFrame .from_dict (evals , orient = 'index' )
136201
137202 return evals
0 commit comments