def infer_type_dict()

in runtool/runtool/runtool.py [0:0]


def infer_type_dict(node: dict) -> Union[Algorithm, Dataset, Experiment, dict]:
    """
    infer_type_dict converts a dict into an instance
    of one of the following classes.

    - Algorithm
    - Dataset
    - Experiment

    Example:
    >>> algorithm = {
    ...     "image": "image_name",
    ...     "instance": "ml.m5.2xlarge",
    ...     "hyperparameters": {},
    ... }
    >>> isinstance(infer_type(algorithm), Algorithm)
    True

    >>> dataset = {
    ...     "path": {
    ...         "train": "some path"
    ...     },
    ...     "meta": {},
    ... }
    >>> isinstance(infer_type(dataset), Dataset)
    True

    >>> experiment = {
    ...     "dataset": dataset,
    ...     "algorithm": algorithm,
    ... }
    >>> isinstance(infer_type(experiment), Experiment)
    True

    If the node matches multiple classes, the first match will be returned.
    An example of this is when a dictionary contains both a valid algorithm
    and a valid dataset. See below example.

    >>> isinstance(infer_type(dict(**algorithm, **dataset)), Algorithm)
    True

    If the node does not match the structure required for either
    Algorithm, Dataset or Experiment, the node is returned unaltered.
    >>> infer_type({}) == {}
    True
    """
    for class_ in (Algorithm, Dataset, Experiment):
        if class_.verify(node):
            return class_(node)
    return node