code: def calc_group_assignment(preds, labels, count_fn):
      """
      Calculate optimal prediction to ground truth group assignments
  
      :param preds, labels: A list of regular or joint groups
      :return assign_idxs: A list of tuples representing the mapping of predicted
      groups to ground truth groups, where (i, j) means preds[i] maps to labels[j]
      :return counts: A list of shape [len(preds), len(labels)], where
      counts[i][j] contains the TP, FP, FN counts if predicted group i was
      assigned to ground truth group j
      """
      assert len(preds) and len(
          labels
      ), "Preds and labels must have non-0 length!"
      counts = [
          [(0, 0, 0) for _ in range(len(labels))]
          for _ in range(len(preds))
      ]
      costs = [
          [0 for _ in range(len(labels))] for _ in range(len(preds))
      ]
  
      for i, pred in enumerate(preds):
          for j, label in enumerate(labels):
              TP, FP, FN = count_fn(pred, label)
              counts[i][j] = (TP, FP, FN)
              costs[i][j] = -(TP - (FP + FN) / 2)
  
      pred_idxs, label_idxs = linear_sum_assignment(costs)
      pred_idxs, label_idxs = list(pred_idxs), list(label_idxs)
      assign_idxs = list(zip(pred_idxs, label_idxs))
      return assign_idxs, counts
  
imports: from copy import deepcopy
  from collections import namedtuple
  import numpy
  from scipy.optimize import linear_sum_assignment
  from finetune.util.metrics import _convert_to_token_list, _get_unique_classes, calc_precision, calc_recall, calc_f1
label_desc: Use image node_id 10 for calling a global function with example usage: calc_group_assignment(preds, labels, count_fn) and returns: assign_idxs,  counts