code: def confusion_to_f1(confusion_matrix):
      results = {}
  
      keys = set()
      for k in confusion_matrix.keys():
          keys.add(k)
          for k2 in confusion_matrix.get(k).keys():
              keys.add(k2)
  
      sum_f1 = 0
      for k in keys:
          tp = 0
          fn = 0
          fp = 0
          for k2 in keys:
              if k == k2:
                  tp = confusion_matrix.get(k, {}).get(k, 0)
              else:
                  fn = fn + confusion_matrix.get(k, {}).get(k2, 0)
                  fp = fp + confusion_matrix.get(k2, {}).get(k, 0)
          if tp + fp == 0:
              precision = 0.0
          else:
              precision = tp / (tp + fp)
          if tp + fn == 0:
              recall = 0.0
          else:
              recall = tp / (tp + fn)
          if precision + recall == 0.0:
              f1 = 0.0
          else:
              f1 = 2 * (precision * recall) / (precision + recall)
  
          results[k] = F1Result(precision, recall, f1)
  
      return results
  
imports: from collections import defaultdict, namedtuple
label_desc: Use image node_id 4 for calling a global function with example usage: confusion_to_f1(confusion_matrix) and returns: results