code: def measure_image_overlap_statistics(
      ground_truth_image,
      test_image,
      mask=None,
  ):
      # Check that the inputs are binary
      if not np.array_equal(
          ground_truth_image, ground_truth_image.astype(bool)
      ):
          raise ValueError("ground_truth_image is not a binary image")
  
      if not np.array_equal(test_image, test_image.astype(bool)):
          raise ValueError("test_image is not a binary image")
  
      if mask is None:
          mask = np.ones_like(ground_truth_image, bool)
  
      # Covert 3D image to 2D long
      if ground_truth_image.ndim > 2:
          ground_truth_image = ground_truth_image.reshape(
              -1, ground_truth_image.shape[-1]
          )
          test_image = test_image.reshape(-1, test_image.shape[-1])
  
          mask = mask.reshape(-1, mask.shape[-1])
  
      false_positives = test_image & ~ground_truth_image
  
      false_positives[~mask] = False
  
      false_negatives = (~test_image) & ground_truth_image
  
      false_negatives[~mask] = False
  
      true_positives = test_image & ground_truth_image
  
      true_positives[~mask] = False
  
      true_negatives = (~test_image) & (~ground_truth_image)
  
      true_negatives[~mask] = False
  
      false_positive_count = np.sum(false_positives)
  
      true_positive_count = np.sum(true_positives)
  
      false_negative_count = np.sum(false_negatives)
  
      true_negative_count = np.sum(true_negatives)
  
      labeled_pixel_count = true_positive_count + false_positive_count
  
      true_count = true_positive_count + false_negative_count
  
      if labeled_pixel_count == 0:
          precision = 1.0
      else:
          precision = float(true_positive_count) / float(
              labeled_pixel_count
          )
  
      if true_count == 0:
          recall = 1.0
      else:
          recall = float(true_positive_count) / float(true_count)
  
      if (precision + recall) == 0:
          f_factor = 0.0  # From http://en.wikipedia.org/wiki/F1_score
      else:
          f_factor = 2.0 * precision * recall / (precision + recall)
  
      negative_count = false_positive_count + true_negative_count
  
      if negative_count == 0:
          false_positive_rate = 0.0
  
          true_negative_rate = 1.0
      else:
          false_positive_rate = float(false_positive_count) / float(
              negative_count
          )
  
          true_negative_rate = float(true_negative_count) / float(
              negative_count
          )
      if true_count == 0:
          false_negative_rate = 0.0
  
          true_positive_rate = 1.0
      else:
          false_negative_rate = float(false_negative_count) / float(
              true_count
          )
  
          true_positive_rate = float(true_positive_count) / float(
              true_count
          )
  
      ground_truth_labels, ground_truth_count = scipy.ndimage.label(
          ground_truth_image & mask, np.ones((3, 3), bool)
      )
  
      test_labels, test_count = scipy.ndimage.label(
          test_image & mask, np.ones((3, 3), bool)
      )
  
      rand_index, adjusted_rand_index = compute_rand_index(
          test_labels, ground_truth_labels, mask
      )
  
      data = {
          "true_positives": true_positives,
          "true_negatives": true_negatives,
          "false_positives": false_positives,
          "false_negatives": false_negatives,
          "Ffactor": f_factor,
          "Precision": precision,
          "Recall": recall,
          "TruePosRate": true_positive_rate,
          "FalsePosRate": false_positive_rate,
          "FalseNegRate": false_negative_rate,
          "TrueNegRate": true_negative_rate,
          "RandIndex": rand_index,
          "AdjustedRandIndex": adjusted_rand_index,
      }
  
      return data
  
imports: import numpy
  import scipy
  import centrosome
  import centrosome.cpmorphology
  import centrosome.filter
  import centrosome.propagate
  import centrosome.fastemd
  from sklearn.cluster import KMeans
  from cellprofiler_library.opts import measureimageoverlap
  from cellprofiler_library.functions.segmentation import convert_labels_to_ijv
  from cellprofiler_library.functions.segmentation import indices_from_ijv
  from cellprofiler_library.functions.segmentation import count_from_ijv
  from cellprofiler_library.functions.segmentation import areas_from_ijv
  from cellprofiler_library.functions.segmentation import cast_labels_to_label_set
label_desc: Use image node_id 1 for calling a global function with example usage: measure_image_overlap_statistics(ground_truth_image, test_image, mask) and returns: data