code: def _sample_positive_patches(
      image,
      abnormality_filepath,
      abnormality_mask,
      abnormality_area,
      patch_size,
      number_of_patches=10,
      min_overlap_threshold=0.90,
      max_number_of_trials_per_threshold=100,
  ):
      """Sample random patches from the image overlapping with the given abnormality.
  
      The abnormal area of the patch with respect to either (a) the total area of
      the patch, or (b) the total area of the abnormality, must be at least
      `min_overlap_threshold` (i.e. 90% by default).
  
      After `max_number_of_trials_per_threshold` samples, if not enough patches
      meeting this requirement have been generated, the `min_overlap_threshold` is
      reduced by 5%. This procedure is repeated until min_overlap_threshold < 0.1
      (which should not happen ever, if the dataset is correct).
  
      Args:
        image: Image to patch from.
        abnormality_filepath: Only used for logging.
        abnormality_mask: Binary mask of the abnormality in the image.
        abnormality_area: Precomputed area of the abnormality.
        patch_size: Size of the patch to extract.
        number_of_patches: Number of patches to sample around the abnormality ROI.
        min_overlap_threshold: Minimum relative area of the patch overlapping with
          the abnormality.
        max_number_of_trials_per_threshold: Maximum number of random samples to try
          before reducing the `min_overlap_threshold` by 5%.
  
      Yields:
        The patch cropped from the input image.
      """
      cv2 = tfds.core.lazy_imports.cv2
  
      # The paper trying to be reproduced states that 90% of the are of each
      # positive patch should correspond to abnormal tissue. Thus if the total area
      # of abnormality is smaller than 0.9 * patch_area, we are certain that no
      # patch can meet this requirement. This happens indeed quite often.
      #
      # However, in a piece of code release by the authors of the paper
      # (https://github.com/yuyuyu123456/CBIS-DDSM/blob/bf3abc6ac2890b9b51eb5125e00056e39295fa44/ddsm_train/sample_patches_combined.py#L26)
      # the authors accept a patch if the total area of abnormality in the patch is
      # greater than 75% OR if 75% of the total abnormal area is in the patch.
      # In addition, they reduce the overlapping threholds every 1000 trials to
      # handle some corner casses.
  
      abnormality_roi = _get_roi_from_mask(abnormality_mask)
      abnorm_x, abnorm_y, abnorm_w, abnorm_h = cv2.boundingRect(
          abnormality_roi
      )
  
      number_of_yielded_patches = 0
      while min_overlap_threshold > 0.1:
          # Determine the region where random samples should be sampled from.
          max_h, min_h = max(abnorm_h, patch_size[0]), min(
              abnorm_h, patch_size[0]
          )
          max_w, min_w = max(abnorm_w, patch_size[1]), min(
              abnorm_w, patch_size[1]
          )
          min_y = abnorm_y - max_h + min_overlap_threshold * min_h
          min_x = abnorm_x - max_w + min_overlap_threshold * min_w
          max_y = (
              abnorm_y + abnorm_h - int(min_overlap_threshold * min_h)
          )
          max_x = (
              abnorm_x + abnorm_w - int(min_overlap_threshold * min_w)
          )
          # Ensure that all sampled batches are within the image.
          min_y = max(min_y, 0)
          min_x = max(min_x, 0)
          max_y = max(
              min(max_y, image.shape[0] - patch_size[0] - 1), min_y
          )
          max_x = max(
              min(max_x, image.shape[1] - patch_size[1] - 1), min_x
          )
          # Cap the number of trials if the sampling region is too small.
          effective_range_size = max_number_of_trials_per_threshold
          if (max_y - min_y + 1) * (
              max_x - min_x + 1
          ) < effective_range_size:
              logging.debug(
                  (
                      "The sampling region for patches of size %r with "
                      "min_overlap_threshold=%f contains less possible patches than "
                      "max_number_of_trials_per_threshold=%d, in abnormality %s"
                  ),
                  patch_size,
                  min_overlap_threshold,
                  max_number_of_trials_per_threshold,
                  abnormality_filepath,
              )
              effective_range_size = (max_y - min_y + 1) * (
                  max_x - min_x + 1
              )
  
          for _ in range(effective_range_size):
              patch_y = np.random.randint(min_y, max_y + 1)
              patch_x = np.random.randint(min_x, max_x + 1)
              if _patch_overlaps_any_abnormality_above_threshold(
                  patch_y,
                  patch_x,
                  patch_size,
                  [abnormality_mask],
                  [abnormality_area],
                  min_overlap_threshold,
              ):
                  number_of_yielded_patches += 1
                  yield image[
                      patch_y : (patch_y + patch_size[0]),
                      patch_x : (patch_x + patch_size[1]),
                  ]
              # If we have yielded all requested patches return.
              if number_of_yielded_patches >= number_of_patches:
                  return
          # We failed to produce patches with the minimum overlapping requirements.
          # Reduce those requirements and try again.
          min_overlap_threshold = min_overlap_threshold * 0.95
          logging.debug(
              (
                  "Overlapping constraints relaxed to min_overlap_threshold=%f while "
                  "sampling positive patches for the abnormality %s"
              ),
              min_overlap_threshold,
              abnormality_filepath,
          )
  
      # This should not happen ever.
      raise ValueError(
          "Only %d positive patches of size %r could be sampled satisfying the "
          "current conditions (min. relative overlapping area = %f) for the "
          "abnormality %s"
          % (
              number_of_yielded_patches,
              patch_size,
              min_overlap_threshold,
              abnormality_filepath,
          )
      )
  
imports: import csv
  import os
  import re
  from absl import logging
  import numpy
  from tensorflow_datasets.core.utils.lazy_imports_utils import tensorflow
  import tensorflow_datasets.public_api
label_desc: Use image node_id 10 for calling a global function with example usage: _sample_positive_patches(image, abnormality_filepath, abnormality_mask, abnormality_area, patch_size, number_of_patches, min_overlap_threshold, max_number_of_trials_per_threshold) without return types