code: def get_z_crops(x, ix, min_pix=1500, n_comps=2, rad_crit=20000):
      final_slices = []
  
      for six in range(x.shape[0]):
          tx = np.copy(x[six]) < -600
          img_center = np.array(tx.shape) / 2
          tx = clear_border(tx)
  
          clusters, n_cands = lb(tx)
          count = np.unique(clusters, return_counts=True)
          keep_comps = np.array(
              [ii for ii in np.argwhere((count[1] > min_pix)) if ii > 0]
          ).astype(int)
  
          if len(keep_comps) > n_comps - 1:
              coms = com(tx, clusters, index=keep_comps)
              keep_com = [
                  ix
                  for ix, ii in enumerate(coms[0])
                  if (
                      (ii[0] - img_center[0]) ** 2
                      + (ii[1] - img_center[1]) ** 2
                      < rad_crit
                  )
              ]
              keep_comps = keep_comps[keep_com]
  
              if len(keep_comps) > n_comps - 1:
                  final_slices.append(six)
                  #      print('appending', six)
  
      z_min = np.min(final_slices) - 7
      z_max = np.max(final_slices) + 7
      dist = z_max - z_min
      if dist >= 151:
          print(
              "trying again with min pix",
              min_pix + 500,
              rad_crit - 500,
              ix,
              dist,
          )
          z_min, z_max = get_z_crops(
              x, ix, min_pix=min_pix + 500, rad_crit=rad_crit - 500
          )
      if dist <= 43:
          print(
              "trying again with one component",
              min_pix - 100,
              rad_crit + 100,
              ix,
              dist,
          )
          z_min, z_max = get_z_crops(
              x,
              ix,
              n_comps=1,
              min_pix=min_pix - 100,
              rad_crit=rad_crit + 100,
          )
  
      print(z_min, z_max, z_max - z_min, ix)
      return z_min, z_max
  
file_path: medicaldetectiontoolkit/experiments/pet_ct_tnm_classification/preprocessing.py
imports: import os
  import SimpleITK
  import numpy
  from multiprocessing import Pool
  import pandas
  import numpy.testing
  from skimage.transform import resize
  import subprocess
  from scipy.ndimage.measurements import label
  from scipy.ndimage.measurements import center_of_mass
  import nrrd
  from copy import deepcopy
  from skimage.segmentation import clear_border
  import configs
label_desc: Use image node_id 1 for calling a global function with example usage: get_z_crops(x, ix, min_pix, n_comps, rad_crit) and returns: z_min,  z_max