code: def _extract_performance(job: TrainingJob) -> dict[str, Any]:
      num_models = len(job.metrics["training_time"])
  
      # We need to keep the "mean_weighted_quantile_loss" for legacy experiments
      hierarchy = {
          "training": ["training_time", "num_gradient_updates"],
          "evaluation": [
              "train_loss",
              "val_loss",
              "val_mean_weighted_quantile_loss",
              "val_ncrps",
          ],
          "testing": [
              "mase",
              "smape",
              "nrmse",
              "nd",
              "mean_weighted_quantile_loss",
              "ncrps",
          ],
      }
      rename = {
          "training_time": "duration",
          "mean_weighted_quantile_loss": "ncrps",
          "val_mean_weighted_quantile_loss": "val_ncrps",
      }
      integer_metrics = {"num_gradient_updates"}
  
      performances = [
          {
              group: {
                  rename.get(item, item): (
                      int(job.metrics[item][i].item())
                      if item in integer_metrics
                      else job.metrics[item][i].item()
                  )
                  for item in items
                  if item in job.metrics
              }
              for group, items in hierarchy.items()
              if any(len(job.metrics[item]) > 0 for item in items)
          }
          for i in range(num_models)
      ]
  
      result = {
          "meta": {
              "num_model_parameters": int(
                  job.metrics["num_model_parameters"][0].item()
              ),
              "latency": np.mean(job.metrics["latency"]).item(),
          },
          "performances": performances,
      }
  
      assert len(result["performances"]) in (1, 11)
      assert all(
          all(not math.isnan(v) for v in vv.values())
          for p in result["performances"]
          for vv in p.values()  # type: ignore
      )
  
      return result
  
imports: from __future__ import annotations
  import json
  import math
  import os
  import shutil
  from pathlib import Path
  from typing import Any, cast, Dict, List, Optional, Union
  import numpy
  from tsbench.config import MODEL_REGISTRY
  from tsbench.evaluations.aws import Analysis, TrainingJob
  from tsbench.evaluations.metrics import Metric, Performance
  from tsbench.forecasts import QuantileForecasts
label_desc: Use image node_id 4 for calling a global function with example usage: _extract_performance(job) and returns: result