code: def ensemble_val(
      val_folder1,
      val_folder2,
      output_folder,
      plan_path,
      validation_folder,
      folds,
      gt_dir,
      threads=4,
      override=True,
  ):
      print("\nEnsembling folders: \n", val_folder1, "\n", val_folder2)
  
      output_folder_base = output_folder
      output_folder = os.path.join(output_folder_base, "ensembled_raw")
  
      with open(plan_path, "rb") as f:
          plans = pickle.load(f)
  
      files = []
      property_files = []
      out_files = []
      gt_segmentations = []
  
      folder_with_gt_segs = gt_dir
  
      for f in range(folds):
          validation_folder_net1 = os.path.join(
              val_folder1, "fold_%d" % f, validation_folder
          )
          validation_folder_net2 = os.path.join(
              val_folder2, "fold_%d" % f, validation_folder
          )
  
          if not os.path.isdir(validation_folder_net1):
              raise AssertionError(
                  "Validation directory missing: %s. Please rerun validation with `python nnunet_tools/nnunet_fold_val.py --config {config_path} --model_path {model_path} --precision fp16 --save_dir {save_dir} --val_save_folder {val predicted save dir}`"
                  % validation_folder_net1
              )
          if not os.path.isdir(validation_folder_net2):
              raise AssertionError(
                  "Validation directory missing: %s. Please rerun validation with `python nnunet_tools/nnunet_fold_val.py --config {config_path} --model_path {model_path} --precision fp16 --save_dir {save_dir} --val_save_folder {val predicted save dir}`"
                  % validation_folder_net2
              )
  
          if not os.path.isfile(
              os.path.join(validation_folder_net1, "summary.json")
          ):
              raise AssertionError(
                  "Validation directory incomplete: %s. Please rerun validation with `python nnunet_tools/nnunet_fold_val.py --config {config_path} --model_path {model_path} --precision fp16 --save_dir {save_dir} --val_save_folder {val predicted save dir}`"
                  % validation_folder_net1
              )
          if not os.path.isfile(
              os.path.join(validation_folder_net2, "summary.json")
          ):
              raise AssertionError(
                  "Validation directory missing: %s. Please rerun validation with `python nnunet_tools/nnunet_fold_val.py --config {config_path} --model_path {model_path} --precision fp16 --save_dir {save_dir} --val_save_folder {val predicted save dir}`"
                  % validation_folder_net2
              )
  
          patient_identifiers1_npz = [
              file[:-4]
              for file in os.listdir(validation_folder_net1)
              if file.endswith("npz")
          ]
          patient_identifiers1_npz.sort()
          patient_identifiers2_npz = [
              file[:-4]
              for file in os.listdir(validation_folder_net2)
              if file.endswith("npz")
          ]
          patient_identifiers2_npz.sort()
  
          patient_identifiers1_nii = [
              file
              for file in os.listdir(validation_folder_net1)
              if file.endswith("nii.gz")
          ]
          patient_identifiers1_nii.sort()
          patient_identifiers1_nii = [
              i[:-7]
              for i in patient_identifiers1_nii
              if not i.endswith("noPostProcess.nii.gz")
              and not i.endswith("_postprocessed.nii.gz")
          ]
  
          patient_identifiers2_nii = [
              file
              for file in os.listdir(validation_folder_net2)
              if file.endswith("nii.gz")
          ]
          patient_identifiers2_nii.sort()
          patient_identifiers2_nii = [
              i[:-7]
              for i in patient_identifiers2_nii
              if not i.endswith("noPostProcess.nii.gz")
              and not i.endswith("_postprocessed.nii.gz")
          ]
  
          if not all(
              [
                  i in patient_identifiers1_npz
                  for i in patient_identifiers1_nii
              ]
          ):
              raise AssertionError(
                  "Missing npz files in folder %s. Please run the validation for all models and folds with the '--save_npz' flag."
                  % (validation_folder_net1)
              )
          if not all(
              [
                  i in patient_identifiers2_npz
                  for i in patient_identifiers2_nii
              ]
          ):
              raise AssertionError(
                  "Missing npz files in folder %s. Please run the validation for all models and folds with the '--save_npz' flag."
                  % (validation_folder_net2)
              )
  
          assert all(
              [
                  i == j
                  for i, j in zip(
                      patient_identifiers1_npz, patient_identifiers2_npz
                  )
              ]
          ), "The number of npz file in {} and {} are not matched.".format(
              validation_folder_net1, validation_folder_net2
          )
  
          os.makedirs(output_folder, exist_ok=True)
  
          for p in patient_identifiers1_npz:
              files.append(
                  [
                      os.path.join(validation_folder_net1, p + ".npz"),
                      os.path.join(validation_folder_net2, p + ".npz"),
                  ]
              )
              property_files.append(
                  [os.path.join(validation_folder_net1, p) + ".pkl"]
              )
              out_files.append(
                  os.path.join(output_folder, p + ".nii.gz")
              )
              gt_segmentations.append(
                  os.path.join(folder_with_gt_segs, p + ".nii.gz")
              )
  
      p = Pool(threads)
      p.starmap(
          merge_files,
          zip(
              files,
              property_files,
              out_files,
              [override] * len(out_files),
              [False] * len(out_files),
          ),
      )
      p.close()
      p.join()
  
      if (
          not os.path.isfile(
              os.path.join(output_folder, "summary.json")
          )
          and len(out_files) > 0
      ):
          aggregate_scores(
              tuple(zip(out_files, gt_segmentations)),
              labels=plans["all_classes"],
              json_output_file=os.path.join(
                  output_folder, "summary.json"
              ),
              num_threads=threads,
          )
      print("runing postprocessing...")
      determine_postprocessing(
          output_folder_base,
          folder_with_gt_segs,
          "ensembled_raw",
          "temp",
          "ensembled_postprocessed",
          threads,
          dice_threshold=0,
      )
      print("postprocessing done.")
  
file_path: PaddleSeg/contrib/MedicalSeg/nnunet/ensemble.py
imports: import argparse
  import os
  import sys
  import ast
  import json
  import numpy
  import pickle
  import shutil
  from copy import deepcopy
  from multiprocessing import Pool
  from itertools import combinations
  from nnunet.utils import save_segmentation_nifti_from_softmax, load_remove_save, aggregate_scores, determine_postprocessing
label_desc: Use image node_id 5 for calling a global function with example usage: ensemble_val(val_folder1, val_folder2, output_folder, plan_path, validation_folder, folds, gt_dir, threads, override) without return types