code: def normalize_flattened_metric_results(
      result: Mapping[str, Any],
      metric_result_cls: Type[MetricResults] | None = None,
  ) -> Mapping[str, Any]:
      """
      Flatten metric result dictionary and normalize metric keys.
  
      :param result:
          the result dictionary.
      :param metric_result_cls:
          the metric result class providing metric name normalization
  
      :return:
          the flattened metric results with normalized metric names.
      """
      # avoid cyclic imports
      from .rank_based_evaluator import RankBasedMetricResults
  
      # normalize keys
      if metric_result_cls is None:
          warnings.warn(
              "Please explicitly provide a metric result class.",
              category=DeprecationWarning,
          )
          metric_result_cls = RankBasedMetricResults
      # TODO: find a better way to handle this
      flat_result = flatten_dictionary(result)
      result = {}
      for key, value in flat_result.items():
          try:
              normalized_key = metric_result_cls.key_from_string(key)
          except ValueError as error:
              new_key = (
                  key.replace("nondeterministic", "")
                  .replace("unknown", "")
                  .strip(".")
                  .replace("..", ".")
              )
              logger.warning(
                  f"Trying to fix malformed key={key} to key={new_key} (error: {error})"
              )
              normalized_key = metric_result_cls.key_from_string(
                  new_key
              )
          result[
              metric_result_cls.key_to_string(normalized_key)
          ] = value
      return result
  
imports: from __future__ import annotations
  import logging
  import timeit
  import warnings
  from abc import ABC, abstractmethod
  from collections import ChainMap
  from typing import Any, ClassVar, Collection, Generic, List, Mapping, NamedTuple, Optional, Tuple, Type, TypeVar, Union, cast
  import pandas
  import torch
  from torch_max_mem import maximize_memory_utilization
  from tqdm.autonotebook import tqdm
  from ..constants import COLUMN_LABELS, TARGET_TO_INDEX, TARGET_TO_KEY_LABELS
  from ..metrics.utils import Metric
  from ..models import Model
  from ..triples.triples_factory import restrict_triples
  from ..triples.utils import get_entities, get_relations
  from ..typing import LABEL_HEAD, LABEL_RELATION, LABEL_TAIL, InductiveMode, MappedTriples, Target
  from ..utils import flatten_dictionary, format_relative_comparison, normalize_string, prepare_filter_triples
label_desc: Use image node_id 8 for calling a global function with example usage: normalize_flattened_metric_results(result, metric_result_cls) and returns: result