code: def map_nested_structure(
      nested_instance: Any,
      mapping: Callable[
          [Union[torch.Tensor, np.ndarray, int, float]],
          Union[torch.Tensor, np.ndarray, int, float],
      ],
      in_place: bool,
      _depth: int = 0,
  ) -> Any:
      """Apply a custom callable to an nested object where the base elements are either torch.Tensor, np.ndarray,
      int or float.
  
      :param nested_instance: the nested instance that should be mapped.
      :param mapping: the mapping that should be applied to the base instance.
      :param in_place: specifies if the mapping should be done in_place (mutating the given object), if this is set
          to true but not possible (with immutable objects) an exception is thrown.
      :param _depth: a counter for the recursion depth
      """
      # List, iterators, generators
      if (
          isinstance(nested_instance, torch.Tensor)
          or isinstance(nested_instance, np.ndarray)
          or isinstance(nested_instance, int)
          or isinstance(nested_instance, float)
      ):
          # If it is a torch.Tensor or np.ndarray object
          if in_place:
              out = mapping(nested_instance)
              if _depth == 0 and nested_instance is not out:
                  raise Exception(
                      "The given nested structure could not be mapped in-place (as specified) since "
                      "the method had to be applied at depth 0, and the mapped results is not the same"
                  )
              return out
          else:
              if isinstance(nested_instance, torch.Tensor):
                  return mapping(nested_instance.clone().detach())
              elif isinstance(nested_instance, np.ndarray):
                  return mapping(np.copy(nested_instance))
              else:
                  return mapping(nested_instance)
      else:
          if isinstance(nested_instance, dict):
              if in_place:
                  for key, value in nested_instance.items():
                      nested_instance[key] = map_nested_structure(
                          value, mapping, in_place, _depth + 1
                      )
                  return nested_instance
              else:
                  new_nested_instance = dict()
                  for key, value in nested_instance.items():
                      new_nested_instance[key] = map_nested_structure(
                          value, mapping, in_place, _depth + 1
                      )
                  return new_nested_instance
  
          elif isinstance(nested_instance, Sequence):
              if isinstance(
                  nested_instance, collections.MutableSequence
              ):
                  if in_place:
                      for idx, value in enumerate(nested_instance):
                          nested_instance[idx] = map_nested_structure(
                              value, mapping, in_place, _depth + 1
                          )
                      return nested_instance
                  else:
                      new_nested_instance = type(nested_instance)()
                      for idx, value in enumerate(nested_instance):
                          new_nested_instance.insert(
                              idx,
                              map_nested_structure(
                                  value, mapping, in_place, _depth + 1
                              ),
                          )
                      return new_nested_instance
  
              elif isinstance(nested_instance, tuple):
                  if in_place:
                      raise Exception(
                          "The given nested structure could not be mapped in-place (as specified) since "
                          "a tuple was encountered. Please revisit the function call"
                      )
                  else:
                      new_nested_instance = type(nested_instance)()
                      for idx, value in enumerate(nested_instance):
                          new_nested_instance += (
                              map_nested_structure(
                                  value, mapping, in_place, _depth + 1
                              ),
                          )
                      return new_nested_instance
  
              else:
                  raise Exception(
                      "Not supported Sequence substructure type found: {}".format(
                          type(nested_instance)
                      )
                  )
  
          else:
              raise Exception(
                  "Not supported Structure type found: {}".format(
                      type(nested_instance)
                  )
              )
  
file_path: maze/maze/perception/perception_utils.py
imports: import collections
  from collections import defaultdict
  from typing import Union, Dict, Any, Callable, Sequence, Iterable
  import gym
  import numpy
  import torch
  from gym import spaces
  from maze.core.env.structured_env import StepKeyType
  from maze.core.utils.structured_env_utils import flat_structured_space
label_desc: Use image node_id 6 for calling a global function with example usage: map_nested_structure(nested_instance, mapping, in_place, _depth) and returns: out, mapping, nested_instance, new_nested_instance, mapping, mapping, nested_instance, new_nested_instance, new_nested_instance