|
| 1 | +import xmltodict |
| 2 | + |
| 3 | +from .._api_calls import _perform_api_call |
| 4 | +from ..evaluations import OpenMLEvaluation |
| 5 | + |
| 6 | +def list_evaluations(function, offset=None, size=None, id=None, task=None, setup=None, |
| 7 | + flow=None, uploader=None, tag=None): |
| 8 | + """List all run-evaluation pairs matching all of the given filters. |
| 9 | +
|
| 10 | + Perform API call `/evaluation/function{function}/{filters} |
| 11 | + |
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + function : str |
| 15 | + the evaluation function. e.g., predictive_accuracy |
| 16 | + offset : int, optional |
| 17 | + the number of runs to skip, starting from the first |
| 18 | + size : int, optional |
| 19 | + the maximum number of runs to show |
| 20 | +
|
| 21 | + id : list, optional |
| 22 | +
|
| 23 | + task : list, optional |
| 24 | +
|
| 25 | + setup: list, optional |
| 26 | +
|
| 27 | + flow : list, optional |
| 28 | +
|
| 29 | + uploader : list, optional |
| 30 | +
|
| 31 | + tag : str, optional |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + list |
| 36 | + List of found evaluations. |
| 37 | + """ |
| 38 | + |
| 39 | + api_call = "evaluation/list/function/%s" %function |
| 40 | + if offset is not None: |
| 41 | + api_call += "/offset/%d" % int(offset) |
| 42 | + if size is not None: |
| 43 | + api_call += "/limit/%d" % int(size) |
| 44 | + if id is not None: |
| 45 | + api_call += "/run/%s" % ','.join([str(int(i)) for i in id]) |
| 46 | + if task is not None: |
| 47 | + api_call += "/task/%s" % ','.join([str(int(i)) for i in task]) |
| 48 | + if setup is not None: |
| 49 | + api_call += "/setup/%s" % ','.join([str(int(i)) for i in setup]) |
| 50 | + if flow is not None: |
| 51 | + api_call += "/flow/%s" % ','.join([str(int(i)) for i in flow]) |
| 52 | + if uploader is not None: |
| 53 | + api_call += "/uploader/%s" % ','.join([str(int(i)) for i in uploader]) |
| 54 | + if tag is not None: |
| 55 | + api_call += "/tag/%s" % tag |
| 56 | + |
| 57 | + return _list_evaluations(api_call) |
| 58 | + |
| 59 | + |
| 60 | +def _list_evaluations(api_call): |
| 61 | + """Helper function to parse API calls which are lists of runs""" |
| 62 | + |
| 63 | + xml_string = _perform_api_call(api_call) |
| 64 | + |
| 65 | + evals_dict = xmltodict.parse(xml_string) |
| 66 | + # Minimalistic check if the XML is useful |
| 67 | + if 'oml:evaluations' not in evals_dict: |
| 68 | + raise ValueError('Error in return XML, does not contain "oml:evaluations": %s' |
| 69 | + % str(evals_dict)) |
| 70 | + |
| 71 | + if isinstance(evals_dict['oml:evaluations']['oml:evaluation'], list): |
| 72 | + evals_list = evals_dict['oml:evaluations']['oml:evaluation'] |
| 73 | + elif isinstance(evals_dict['oml:evaluations']['oml:evaluation'], dict): |
| 74 | + evals_list = [evals_dict['oml:evaluations']['oml:evaluation']] |
| 75 | + else: |
| 76 | + raise TypeError() |
| 77 | + |
| 78 | + evals = dict() |
| 79 | + for eval_ in evals_list: |
| 80 | + run_id = int(eval_['oml:run_id']) |
| 81 | + array_data = None |
| 82 | + if 'oml:array_data' in eval_: |
| 83 | + eval_['oml:array_data'] |
| 84 | + |
| 85 | + evaluation = OpenMLEvaluation(int(eval_['oml:run_id']), int(eval_['oml:task_id']), |
| 86 | + int(eval_['oml:setup_id']), int(eval_['oml:flow_id']), |
| 87 | + eval_['oml:flow_name'], eval_['oml:data_id'], |
| 88 | + eval_['oml:data_name'], eval_['oml:function'], |
| 89 | + eval_['oml:upload_time'], float(eval_['oml:value']), |
| 90 | + array_data) |
| 91 | + evals[run_id] = evaluation |
| 92 | + return evals |
| 93 | + |
0 commit comments