def generate_sagemaker_overrides()

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


    def generate_sagemaker_overrides(self) -> Dict[str, Any]:
        """
        This function extracts items from the algorithm and the dataset where the
        key has the structure:

        `$sagemaker.<path>`.

        The extracted `<path>` is then used to populate a new `dict` where the
        value at `<path>` is set to that of the `$sagemaker.<path>` in the source.

        i.e.

        >>> job = Job( # doctest: +SKIP
        ...     experiment=dict(
        ...         algorithm={"$sagemaker.hello.world": 10},
        ...         dataset={"smth": 1}
        ...     ),
        ...     ...
        ... )
        >>> job.generate_sagemaker_overrides() == { # doctest: +SKIP
        ...     "hello": {"world": 10},
        ... }
        True

        """
        # generate tuple of format: Tuple[List, Any]
        # this tuple will only contain items having
        # a key starting with "$sagemaker."
        # i.e.
        # {"$sagemaker.hello.world": 10, "smth": 2}
        # ->
        # (["hello", "world"], 10)

        sagemaker_overrides = (
            (key.split(".")[1:], value)
            for key, value in chain(
                self.experiment["algorithm"].items(),
                self.experiment["algorithm"].items(),
            )
            if key.startswith("$sagemaker.")
        )

        # generate a dictionary from the tuple
        # (["hello", "world"], 10)
        # ->
        # {"hello": {"world": 10}}
        overrides = {}
        for path, value in sagemaker_overrides:
            overrides = update_in(overrides, path, lambda _: value)

        return overrides