code: def validate_matrix_metric(
      matrix,
      exp_total_count,
      exp_total_error,
      metric,
      num_cat1,
      num_cat2,
  ):
      is_precision = metric in precision_metrics
      is_recall = metric in recall_metrics
      is_accuracy = metric == Metrics.ACCURACY_SCORE
      is_f1_score = metric in f1_metrics
      is_multi_agg = is_multi_agg_metric(metric)
      if metric == Metrics.ERROR_RATE:
          # take sum of count, false count
          total_count = 0
          total_false_count = 0
          for i in range(num_cat1):
              for j in range(num_cat2):
                  cell_count = matrix[MATRIX][i][j][COUNT]
                  assert cell_count >= 0
                  total_count += cell_count
                  cell_false_count = matrix[MATRIX][i][j][FALSE_COUNT]
                  assert cell_false_count >= 0
                  total_false_count += cell_false_count
          assert exp_total_count == total_count
          assert exp_total_error == total_false_count
      elif (
          metric == Metrics.MEAN_SQUARED_ERROR
          or metric == Metrics.MEAN_ABSOLUTE_ERROR
      ):
          # take sum of count, metric value
          total_count = 0
          total_metric_value = 0
          for i in range(num_cat1):
              for j in range(num_cat2):
                  count = matrix[MATRIX][i][j][COUNT]
                  assert count >= 0
                  total_count += count
                  cell_metric_value = matrix[MATRIX][i][j][METRIC_VALUE]
                  assert cell_metric_value >= 0
                  cell_value = cell_metric_value * count
                  total_metric_value += cell_value
                  metric_name = matrix[MATRIX][i][j][METRIC_NAME]
                  assert metric_name == metric_to_display_name[metric]
          total_metric_value = total_metric_value / total_count
          assert exp_total_count == total_count
          assert abs(exp_total_error - total_metric_value) < TOLERANCE
      elif is_multi_agg:
          # compute the overall metric from the data in each of the cells
          total_count = 0
          total_metric_value = 0
          cell_tp_value = None
          cell_tn_value = None
          cell_fp_value = None
          cell_fn_value = None
          for i in range(num_cat1):
              for j in range(num_cat2):
                  count = matrix[MATRIX][i][j][COUNT]
                  assert count >= 0
                  total_count += count
                  cell_metric_value = matrix[MATRIX][i][j][METRIC_VALUE]
                  assert cell_metric_value >= 0
                  if cell_tp_value is None:
                      cell_tp_value = np.array(
                          matrix[MATRIX][i][j][TP], dtype=FLOAT64
                      )
                  else:
                      cell_tp_value += np.array(
                          matrix[MATRIX][i][j][TP], dtype=FLOAT64
                      )
                  if is_precision or is_accuracy or is_f1_score:
                      if cell_fp_value is None:
                          cell_fp_value = np.array(
                              matrix[MATRIX][i][j][FP], dtype=FLOAT64
                          )
                      else:
                          cell_fp_value += np.array(
                              matrix[MATRIX][i][j][FP], dtype=FLOAT64
                          )
                  if is_recall or is_accuracy or is_f1_score:
                      if cell_fn_value is None:
                          cell_fn_value = np.array(
                              matrix[MATRIX][i][j][FN], dtype=FLOAT64
                          )
                      else:
                          cell_fn_value += np.array(
                              matrix[MATRIX][i][j][FN], dtype=FLOAT64
                          )
                  if is_accuracy:
                      if cell_tn_value is None:
                          cell_tn_value = np.array(
                              matrix[MATRIX][i][j][TN], dtype=FLOAT64
                          )
                      else:
                          cell_tn_value += np.array(
                              matrix[MATRIX][i][j][TN], dtype=FLOAT64
                          )
                  metric_name = matrix[MATRIX][i][j][METRIC_NAME]
                  assert metric_name == metric_to_display_name[metric]
          tp_sum = cell_tp_value.sum()
          is_overall_precision = (
              metric == Metrics.MICRO_PRECISION_SCORE
              or metric == Metrics.PRECISION_SCORE
          )
          is_overall_recall = (
              metric == Metrics.MICRO_RECALL_SCORE
              or metric == Metrics.RECALL_SCORE
          )
          is_overall_f1_score = (
              metric == Metrics.MICRO_F1_SCORE
              or metric == Metrics.F1_SCORE
          )
          if is_overall_precision:
              total_metric_value = tp_sum / (
                  tp_sum + cell_fp_value.sum()
              )
          elif is_overall_recall:
              total_metric_value = tp_sum / (
                  tp_sum + cell_fn_value.sum()
              )
          elif metric == Metrics.MACRO_PRECISION_SCORE:
              per_class_metrics = cell_tp_value / (
                  cell_tp_value + cell_fp_value
              )
              num_classes = len(per_class_metrics)
              total_metric_value = per_class_metrics.sum() / num_classes
          elif metric == Metrics.MACRO_RECALL_SCORE:
              per_class_metrics = cell_tp_value / (
                  cell_tp_value + cell_fn_value
              )
              num_classes = len(per_class_metrics)
              total_metric_value = per_class_metrics.sum() / num_classes
          elif metric == Metrics.PRECISION_SCORE:
              total_metric_value = tp_sum / (
                  tp_sum + cell_fp_value.sum()
              )
          elif metric == Metrics.ACCURACY_SCORE:
              if len(cell_tn_value) < 2:
                  tn_sum = cell_tn_value.sum()
                  fn_sum = cell_fn_value.sum()
                  fp_sum = cell_fp_value.sum()
                  num_correct = tp_sum + tn_sum
                  num_total = num_correct + fn_sum + fp_sum
                  total_metric_value = num_correct / num_total
              else:
                  num_total = (
                      cell_tp_value[0]
                      + cell_tn_value[0]
                      + cell_fn_value[0]
                      + cell_fp_value[0]
                  )
                  total_metric_value = tp_sum / num_total
          elif is_overall_f1_score:
              fn_sum = cell_fn_value.sum()
              fp_sum = cell_fp_value.sum()
              total_metric_value = tp_sum / (
                  tp_sum + (fp_sum + fn_sum) / 2
              )
          elif metric == Metrics.MACRO_F1_SCORE:
              pc_precision = cell_tp_value / (
                  cell_tp_value + cell_fp_value
              )
              pc_recall = cell_tp_value / (
                  cell_tp_value + cell_fn_value
              )
              num_classes = len(pc_precision)
              pc_f1_score = (
                  2
                  * (pc_precision * pc_recall)
                  / (pc_precision + pc_recall)
              ).sum()
              total_metric_value = pc_f1_score / num_classes
          assert exp_total_count == total_count
          assert abs(exp_total_error - total_metric_value) < TOLERANCE
      else:
          raise NotImplementedError(
              "Metric {} validation not supported yet".format(metric)
          )
  
imports: import numpy
  import pandas
  import pytest
  from erroranalysis._internal.cohort_filter import filter_from_cohort
  from erroranalysis._internal.constants import ARG, COLUMN, METHOD, ROW_INDEX, TRUE_Y, MatrixParams, Metrics, ModelTask, f1_metrics, metric_to_display_name, precision_metrics, recall_metrics
  from erroranalysis._internal.error_analyzer import ModelAnalyzer
  from erroranalysis._internal.matrix_filter import CATEGORY1, CATEGORY2, COUNT, FALSE_COUNT, FN, FP, INTERVAL_MAX, INTERVAL_MIN, MATRIX, METRIC_NAME, METRIC_VALUE, TN, TP, VALUES, bin_data
  from erroranalysis._internal.metrics import get_ordered_classes, is_multi_agg_metric, metric_to_func
  from rai_test_utils.datasets.tabular import create_adult_census_data, create_binary_classification_dataset, create_cancer_data, create_diabetes_data, create_housing_data, create_iris_data, create_simple_titanic_data, create_wine_data
  from rai_test_utils.models.model_utils import create_models_classification, create_models_regression
  from rai_test_utils.models.sklearn import create_kneighbors_classifier, create_titanic_pipeline
  from raiutils.exceptions import UserConfigValidationException
label_desc: Use image node_id 6 for calling a global function with example usage: validate_matrix_metric(matrix, exp_total_count, exp_total_error, metric, num_cat1, num_cat2) without return types