code: def pp_patient(inputs):
      ix, path = inputs
      background_categories = [
          "M1b_brain",
          "N_inflammation",
          "T_benign",
          "T_other",
      ]
      # for lix, l in enumerate(patient['class_target']):
      #     if l in background_categories:
      #         seg[seg == lix + 1] = 0
      #     else:
      #         seg[seg == lix + 1] = 1
  
      selection = [106, 273]
  
      if ix in selection:
          pid = ix
          print("processing", pid, path)
          x = sitk.ReadImage(os.path.join(path, "lsa_ct.nii.gz"))
          p = sitk.ReadImage(os.path.join(path, "lsa_pet.nii.gz"))
          readdata, header = nrrd.read(
              os.path.join(path, "lsa.seg.nrrd")
          )
          if len(readdata.shape) == 3:
              readdata = readdata[None]
              spacing = np.diagonal(header["space directions"])
          else:
              spacing = np.diagonal(header["space directions"][1:, :])
  
          origin = header["space origin"] * np.sign(spacing)
          labels = [
              header[k].split("=")[-1]
              for k in header.keys()
              if "_Name" in k
          ]
          seg = np.zeros_like(readdata[0])
          print("READDATA SHAPE", readdata.shape)
          for ix in range(readdata.shape[0]):
              if labels[ix] not in background_categories:
                  seg[readdata[ix] == 1] = ix = 1
  
          seg = seg.astype("uint8")
          s = sitk.GetImageFromArray(np.transpose(seg, axes=(2, 1, 0)))
          s.SetSpacing(abs(spacing))
          s.SetOrigin(origin)
  
          x_spacing = x.GetSpacing()
          if x_spacing[0] < 0.95 or x_spacing[2] < 3:
              new_spacing = (0.976562, 0.976562, 3.27)
              new_size = [
                  int(x.GetSize()[ii] * x_spacing[ii] / new_spacing[ii])
                  for ii in range(3)
              ]
              reference_image = sitk.Image(
                  new_size, x.GetPixelIDValue()
              )
              reference_image.SetOrigin(x.GetOrigin())
              reference_image.SetDirection(x.GetDirection())
              reference_image.SetSpacing(new_spacing)
  
              # Resample without any smoothing.
              x = sitk.Resample(x, reference_image)
  
          resampler = sitk.ResampleImageFilter()
          resampler.SetReferenceImage(x)
          # §resampler.SetInterpolator()  # default linear
          rp = resampler.Execute(p)
          rs = resampler.Execute(s)
          pi = sitk.GetArrayFromImage(rp)
          si = sitk.GetArrayFromImage(rs)
          xi = sitk.GetArrayFromImage(x)
  
          zmin, zmax = get_z_crops(xi, ix)
  
          x = xi[zmin:zmax]
          p = pi[zmin:zmax]
          s = si[zmin:zmax]
  
          x = np.clip(x, -1200, 600)
          x = (1200 + x) / (600 + 1200)  # (x-a) / (b-a) * (c-d)
          x = (x - np.mean(x)) / np.std(x)
  
          # p = np.clip(p, 0, 2)
          #   p = (p) / (2)
          p = (p - np.mean(p)) / np.std(p)
  
          assert np.all(np.array(x.shape) == np.array(s.shape))
  
          img = np.concatenate((x[None], p[None])).astype(np.float32)
  
          remaining_comps = np.unique(s)
          remaining_labels = [
              ii
              for ix, ii in enumerate(labels)
              if ix + 1 in remaining_comps
          ]
          s[s > 0] = 1
  
          fg_slices = [
              ii for ii in np.unique(np.argwhere(s != 0)[:, 0])
          ]
  
          out_df = pd.read_pickle(
              os.path.join(cf.pp_dir, "info_df.pickle")
          )
          out_df.loc[len(out_df)] = {
              "pid": pid,
              "raw_pid": path.split("/")[-1],
              "class_target": remaining_labels,
              "fg_slices": fg_slices,
          }
          out_df.to_pickle(os.path.join(cf.pp_dir, "info_df.pickle"))
  
          np.save(os.path.join(cf.pp_dir, "{}_rois.npy".format(pid)), s)
          np.save(
              os.path.join(cf.pp_dir, "{}_img.npy".format(pid)), img
          )
  
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 2 for calling a global function with example usage: pp_patient(inputs) without return types