code: def apply_to_collection(
      data: Any,
      dtype: Union[type, Any, Tuple[Union[type, Any]]],
      function: Callable,
      *args: Any,
      wrong_dtype: Optional[Union[type, Tuple[type]]] = None,
      include_none: bool = True,
      **kwargs: Any,
  ) -> Any:
      """
      递归地对 ``data`` 中的元素执行函数 ``function``，且仅在满足元素为 ``dtype`` 时执行。
  
      该函数参考了 `pytorch-lightning <https://github.com/PyTorchLightning/pytorch-lightning>`_ 的实现
  
      :param data: 需要进行处理的数据集合或数据；
      :param dtype: 数据的类型，函数 ``function`` 只会被应用于 ``data`` 中类型为 ``dtype`` 的数据；
      :param function: 对数据进行处理的函数；
      :param args: ``function`` 所需要的其它参数；
      :param wrong_dtype: ``function`` 一定不会生效的数据类型。
          如果数据既是 ``wrong_dtype`` 类型又是 ``dtype`` 类型那么也不会生效；
      :param include_none: 是否包含执行结果为 ``None`` 的数据，默认为 ``True``；
      :param kwargs: ``function`` 所需要的其它参数；
      :return: 经过 ``function`` 处理后的数据集合；
      """
      # Breaking condition
      if isinstance(data, dtype) and (
          wrong_dtype is None or not isinstance(data, wrong_dtype)
      ):
          return function(data, *args, **kwargs)
  
      elem_type = type(data)
  
      # Recursively apply to collection items
      if isinstance(data, Mapping):
          out = []
          for k, v in data.items():
              v = apply_to_collection(
                  v,
                  dtype,
                  function,
                  *args,
                  wrong_dtype=wrong_dtype,
                  include_none=include_none,
                  **kwargs,
              )
              if include_none or v is not None:
                  out.append((k, v))
          if isinstance(data, defaultdict):
              return elem_type(data.default_factory, OrderedDict(out))
          return elem_type(OrderedDict(out))
  
      is_namedtuple = _is_namedtuple(data)
      is_sequence = isinstance(data, Sequence) and not isinstance(
          data, str
      )
      if is_namedtuple or is_sequence:
          out = []
          for d in data:
              v = apply_to_collection(
                  d,
                  dtype,
                  function,
                  *args,
                  wrong_dtype=wrong_dtype,
                  include_none=include_none,
                  **kwargs,
              )
              if include_none or v is not None:
                  out.append(v)
          return elem_type(*out) if is_namedtuple else elem_type(out)
  
      if _is_dataclass_instance(data):
          # make a deepcopy of the data,
          # but do not deepcopy mapped fields since the computation would
          # be wasted on values that likely get immediately overwritten
          fields = {}
          memo = {}
          for field in dataclasses.fields(data):
              field_value = getattr(data, field.name)
              fields[field.name] = (field_value, field.init)
              memo[id(field_value)] = field_value
          result = deepcopy(data, memo=memo)
          # apply function to each field
          for field_name, (field_value, field_init) in fields.items():
              if field_init:
                  v = apply_to_collection(
                      field_value,
                      dtype,
                      function,
                      *args,
                      wrong_dtype=wrong_dtype,
                      include_none=include_none,
                      **kwargs,
                  )
              if not field_init or (
                  not include_none and v is None
              ):  # retain old value
                  v = getattr(data, field_name)
              setattr(result, field_name, v)
          return result
  
      # data is neither of dtype, nor a collection
      return data
  
imports: import functools
  import inspect
  from inspect import Parameter
  import dataclasses
  from dataclasses import is_dataclass
  from copy import deepcopy
  from collections import defaultdict, OrderedDict
  from typing import Callable, List, Any, Dict, AnyStr, Union, Mapping, Sequence
  from typing import Tuple, Optional
  from time import sleep
  import os
  from contextlib import contextmanager
  from functools import wraps
  from prettytable import PrettyTable
  from pathlib import Path
  from fastNLP.core.log import logger
label_desc: Use image node_id 11 for calling a global function with example usage: apply_to_collection(data, dtype, function) and returns: data, function, elem_type, unknown, result, elem_type