code: def merge_label_to_id_mapping(
      *pairs: Tuple[str, Mapping[str, int]],
      offsets: torch.LongTensor = None,
      mappings: Optional[Sequence[Mapping[int, int]]] = None,
      extra: Optional[Mapping[str, int]] = None,
  ) -> Dict[str, int]:
      """
      Merge label-to-id mappings.
  
      :param pairs:
          pairs of `(prefix, label_to_id)`, where `label_to_id` is the label-to-id mapping, and `prefix` is a string
          prefix to prepend to each key of the label-to-id mapping.
      :param offsets: shape: `(len(pairs),)`
          id offsets for each pair
      :param mappings:
          explicit id remappings for each pair
      :param extra:
          extra entries to add after merging
  
      :return:
          a merged label-to-id mapping
  
      :raises ValueError:
          if not exactly one of `offsets` or `mappings` is provided
      """
      if (offsets is None and mappings is None) or (
          offsets is not None and mappings is not None
      ):
          raise ValueError(
              "Exactly one of `offsets` or `mappings` has to be provided"
          )
      # merge labels with same ID
      value_to_keys: DefaultDict[int, Set[str]] = defaultdict(set)
      for i, (prefix, mapping) in enumerate(pairs):
          for key, value in mapping.items():
              key = f"{prefix}:{key}"
              if offsets is None:
                  # for mypy
                  assert mappings is not None
                  value = mappings[i][value]
              else:
                  value = value + offsets[i].item()
              value_to_keys[value].add(key)
      if extra:
          for k, v in extra.items():
              value_to_keys[v].add(k)
      # reconstruct label-to-id
      result: Dict[str, int] = {}
      for value, keys in value_to_keys.items():
          if len(keys) == 1:
              key = list(keys)[0]
          else:
              key = str(set(keys))
          result[key] = value
      return result
  
imports: import logging
  from abc import ABC, abstractmethod
  from collections import defaultdict
  from typing import Any, ClassVar, DefaultDict, Dict, Iterable, Mapping, NamedTuple, Optional, Sequence, Set, Tuple, Union
  import numpy
  import pandas
  import torch
  from class_resolver import ClassResolver
  from pandas.api.types import is_numeric_dtype, is_string_dtype
  from ...triples import CoreTriplesFactory, TriplesFactory
  from ...typing import COLUMN_HEAD, COLUMN_TAIL, EA_SIDE_LEFT, EA_SIDE_RIGHT, EA_SIDES, MappedTriples, TargetColumn
  from ...utils import format_relative_comparison, get_connected_components
label_desc: Use image node_id 2 for calling a global function with example usage: merge_label_to_id_mapping() and returns: result