code: def get_coords(binary_mask, n_components, dim):
      """
      loops over batch to perform connected component analysis on binary input mask. computes box coordiantes around
      n_components - biggest components (rois).
      :param binary_mask: (b, y, x, (z)). binary mask for one specific foreground class.
      :param n_components: int. number of components to extract per batch element and class.
      :return: coords (b, n, (y1, x1, y2, x2, (z1), (z2))
      :return: batch_components (b, n, (y1, x1, y2, x2, (z1), (z2))
      """
      binary_mask = binary_mask.astype("uint8")
      batch_coords = []
      batch_components = []
      for ix, b in enumerate(binary_mask):
          clusters, n_cands = lb(
              b
          )  # peforms connected component analysis.
          uniques, counts = np.unique(clusters, return_counts=True)
          # only keep n_components largest components.
          keep_uniques = uniques[1:][np.argsort(counts[1:])[::-1]][
              :n_components
          ]
          # separate clusters and concat.
          p_components = np.array(
              [(clusters == ii) * 1 for ii in keep_uniques]
          )
          p_coords = []
          if p_components.shape[0] > 0:
              for roi in p_components:
                  mask_ixs = np.argwhere(roi != 0)
  
                  # get coordinates around component.
                  roi_coords = [
                      np.min(mask_ixs[:, 0]) - 1,
                      np.min(mask_ixs[:, 1]) - 1,
                      np.max(mask_ixs[:, 0]) + 1,
                      np.max(mask_ixs[:, 1]) + 1,
                  ]
                  if dim == 3:
                      roi_coords += [
                          np.min(mask_ixs[:, 2]),
                          np.max(mask_ixs[:, 2]) + 1,
                      ]
                  p_coords.append(roi_coords)
  
              p_coords = np.array(p_coords)
  
              # clip coords.
              p_coords[p_coords < 0] = 0
              p_coords[:, :4][
                  p_coords[:, :4] > binary_mask.shape[-2]
              ] = binary_mask.shape[-2]
              if dim == 3:
                  p_coords[:, 4:][
                      p_coords[:, 4:] > binary_mask.shape[-1]
                  ] = binary_mask.shape[-1]
  
          batch_coords.append(p_coords)
          batch_components.append(p_components)
      return batch_coords, batch_components
  
file_path: medicaldetectiontoolkit/models/detection_unet.py
imports: import torch
  import torch.nn
  import torch.nn.functional
  from scipy.ndimage.measurements import label
  import numpy
  import utils.exp_utils
  import utils.model_utils
label_desc: Use image node_id 1 for calling a global function with example usage: get_coords(binary_mask, n_components, dim) and returns: batch_coords,  batch_components