code: def matrix_2d(
      categories1, categories2, matrix_counts, matrix_err_counts, metric
  ):
      """Constructs a 2D matrix.
  
      The matrix is in a dictionary format which can then be saved to JSON.
  
      :param categories1: The categories for the first selected feature.
      :type categories1: list
      :param categories2: The categories for the second selected feature.
      :type categories2: list
      :param matrix_counts: The matrix counts.
      :type matrix_counts: numpy.ndarray
      :param matrix_err_counts: The matrix error counts.
      :type matrix_err_counts: numpy.ndarray
      :param metric: The calculated metric.
      :type metric: str
      :returns: The 2D matrix dictionary.
      :rtype: dict
      """
      matrix = []
      category1 = []
      category1_min_interval = []
      category1_max_interval = []
      is_multi_agg = is_multi_agg_metric(metric)
      for row_index in reversed(range(len(categories1))):
          matrix_row = []
          cat1 = categories1[row_index]
          if isinstance(categories1, pd.IntervalIndex):
              category1_min_interval.append(cat1.left)
              category1_max_interval.append(cat1.right)
              category1.append(str(cat1))
          else:
              category1.append(get_py_value(cat1))
          for col_index in range(len(categories2)):
              cat2 = categories2[col_index]
              index_exists_err = cat1 in matrix_err_counts.index
              col_exists_err = cat2 in matrix_err_counts.columns
              if metric == Metrics.ERROR_RATE:
                  false_count = 0
                  if index_exists_err and col_exists_err:
                      false_count = int(
                          matrix_err_counts.loc[cat1, cat2]
                      )
              elif is_multi_agg:
                  tp_sum = []
                  fp_sum = []
                  fn_sum = []
                  tn_sum = []
                  metric_value = 0
                  if index_exists_err and col_exists_err:
                      metric_value = float(
                          matrix_err_counts.loc[cat1, cat2][0]
                      )
                      tp_sum = matrix_err_counts.loc[cat1, cat2][1]
                      fp_sum = matrix_err_counts.loc[cat1, cat2][2]
                      fn_sum = matrix_err_counts.loc[cat1, cat2][3]
                      tn_sum = matrix_err_counts.loc[cat1, cat2][4]
                      error = float(
                          matrix_err_counts.loc[cat1, cat2][5]
                      )
              else:
                  metric_value = 0
                  if index_exists_err and col_exists_err:
                      metric_value = float(
                          matrix_err_counts.loc[cat1, cat2]
                      )
              index_exists = cat1 in matrix_counts.index
              col_exists = cat2 in matrix_counts.columns
              total_count = 0
              if index_exists and col_exists:
                  total_count = int(matrix_counts.loc[cat1, cat2])
              if metric == Metrics.ERROR_RATE:
                  matrix_row.append(
                      {
                          FALSE_COUNT: false_count,
                          COUNT: total_count,
                          METRIC_NAME: metric_to_display_name[metric],
                      }
                  )
              elif is_multi_agg:
                  matrix_row.append(
                      {
                          METRIC_VALUE: metric_value,
                          TP: tp_sum,
                          FP: fp_sum,
                          FN: fn_sum,
                          TN: tn_sum,
                          ERROR: error,
                          METRIC_NAME: metric_to_display_name[metric],
                          COUNT: total_count,
                      }
                  )
              else:
                  matrix_row.append(
                      {
                          METRIC_VALUE: metric_value,
                          METRIC_NAME: metric_to_display_name[metric],
                          COUNT: total_count,
                      }
                  )
          matrix.append(matrix_row)
  
      category2 = []
      category2_min_interval = []
      category2_max_interval = []
      for cat2 in categories2:
          if isinstance(categories2, pd.IntervalIndex):
              category2_min_interval.append(cat2.left)
              category2_max_interval.append(cat2.right)
              category2.append(str(cat2))
          else:
              category2.append(get_py_value(cat2))
      category1 = {
          VALUES: category1,
          INTERVAL_MIN: category1_min_interval,
          INTERVAL_MAX: category1_max_interval,
      }
      category2 = {
          VALUES: category2,
          INTERVAL_MIN: category2_min_interval,
          INTERVAL_MAX: category2_max_interval,
      }
      return {
          MATRIX: matrix,
          CATEGORY1: category1,
          CATEGORY2: category2,
      }
  
imports: import logging
  import math
  import warnings
  from abc import ABC, abstractmethod
  import numpy
  import pandas
  from sklearn.metrics import multilabel_confusion_matrix
  from erroranalysis._internal.cohort_filter import filter_from_cohort
  from erroranalysis._internal.constants import DIFF, PRED_Y, ROW_INDEX, TRUE_Y, MatrixParams, MetricKeys, Metrics, ModelTask, metric_to_display_name
  from erroranalysis._internal.metrics import get_ordered_classes, is_multi_agg_metric, metric_to_func
  from raiutils.exceptions import UserConfigValidationException
label_desc: Use image node_id 8 for calling a global function with example usage: matrix_2d(categories1, categories2, matrix_counts, matrix_err_counts, metric) and returns: dict