class_docstr: ``nnUNetV2Runner`` provides an interface in MONAI to use `nnU-Net` V2 library to analyze, train, and evaluate
  neural networks for medical image segmentation tasks.
  
  ``nnUNetV2Runner`` can be used in two ways:
  
  #. with one line of code to execute the complete pipeline.
  #. with a series of commands to run each modules in the pipeline.
  
  The output of the interface is a directory that contains:
  
  #. converted dataset met the requirement of nnU-Net V2
  #. data analysis results
  #. checkpoints from the trained U-Net models
  #. validation accuracy in each fold of cross-validation
  #. the predictions on the testing datasets from the final algorithm ensemble and potential post-processing
  
  Args:
      input_config: the configuration dictionary or the file path to the configuration in the form of YAML.
          The keys required in the configuration are:
          - ``"datalist"``: File path to the datalist for the train/testing splits
          - ``"dataroot"``: File path to the dataset
          - ``"modality"``: Imaging modality, e.g. "CT", ["T2", "ADC"]
          Currently, the configuration supports these optional keys:
          - ``"nnunet_raw"``: File path that will be written to env variable for nnU-Net
          - ``"nnunet_preprocessed"``: File path that will be written to env variable for nnU-Net
          - ``"nnunet_results"``: File path that will be written to env variable for nnU-Net
          - ``"nnUNet_trained_models"``
          - ``"dataset_name_or_id"``: Name or Integer ID of the dataset
          If an optional key is not specified, then the pipeline will use the default values.
      trainer_class_name: the trainer class names offered by nnUNetV2 exhibit variations in training duration.
          Default: "nnUNetTrainer". Other options: "nnUNetTrainer_Xepoch". X could be one of 1,5,10,20,50,100,
          250,2000,4000,8000.
      export_validation_probabilities: True to save softmax predictions from final validation as npz
          files (in addition to predicted segmentations). Needed for finding the best ensemble.
          Default: True.
      work_dir: working directory to save the intermediate and final results.
  
  Examples:
      - Use the one-liner to start the nnU-Net workflow
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner run --input_config ./input.yaml
  
      - Use `convert_dataset` to prepare the data to meet nnU-Net requirements, generate dataset JSON file,
          and copy the dataset to a location specified by ``nnunet_raw`` in the input config file
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner convert_dataset --input_config="./input.yaml"
  
      - `convert_msd_dataset` is an alternative option to prepare the data if the dataset is MSD.
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner convert_msd_dataset \
              --input_config "./input.yaml" --data_dir "/path/to/Task09_Spleen"
  
      - experiment planning and data pre-processing
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner plan_and_process --input_config "./input.yaml"
  
      - training all 20 models using all GPUs available.
          "CUDA_VISIBLE_DEVICES" environment variable is not supported.
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner train --input_config "./input.yaml"
  
      - training a single model on a single GPU for 5 epochs. Here ``config`` is used to specify the configuration.
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner train_single_model --input_config "./input.yaml" \
              --config "3d_fullres" \
              --fold 0 \
              --gpu_id 0 \
              --trainer_class_name "nnUNetTrainer_5epochs" \
              --export_validation_probabilities True
  
      - training for all 20 models (4 configurations by 5 folds) on 2 GPUs
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner train --input_config "./input.yaml" --gpu_id_for_all "0,1"
  
      - 5-fold training for a single model on 2 GPUs. Here ``configs`` is used to specify the configurations.
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner train --input_config "./input.yaml" \
              --configs "3d_fullres" \
              --trainer_class_name "nnUNetTrainer_5epochs" \
              --export_validation_probabilities True \
              --gpu_id_for_all "0,1"
  
      - find the best configuration
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner find_best_configuration --input_config "./input.yaml"
  
      - predict, ensemble, and post-process
  
      .. code-block:: bash
  
          python -m monai.apps.nnunet nnUNetV2Runner predict_ensemble_postprocessing --input_config "./input.yaml"
class_docstr_tok: ["``","nnUNetV2Runner","``","provides","an","interface","in","MONAI","to","use","`","nnU-Net","`","V2","library","to","analyze",",","train",",","and","evaluate","neural","networks","for","medical","image","segmentation","tasks",".","``","nnUNetV2Runner","``","can","be","used","in","two","ways",":","#",".","with","one","line","of","code","to","execute","the","complete","pipeline",".","#",".","with","a","series","of","commands","to","run","each","modules","in","the","pipeline",".","The","output","of","the","interface","is","a","directory","that","contains",":","#",".","converted","dataset","met","the","requirement","of","nnU-Net","V2","#",".","data","analysis","results","#",".","checkpoints","from","the","trained","U-Net","models","#",".","validation","accuracy","in","each","fold","of","cross-validation","#",".","the","predictions","on","the","testing","datasets","from","the","final","algorithm","ensemble","and","potential","post-processing","Args",":","input_config",":","the","configuration","dictionary","or","the","file","path","to","the","configuration","in","the","form","of","YAML",".","The","keys","required","in","the","configuration","are",":","-","``","``","datalist","''","``",":","File","path","to","the","datalist","for","the","train\/testing","splits","-","``","``","dataroot","''","``",":","File","path","to","the","dataset","-","``","``","modality","''","``",":","Imaging","modality",",","e.g",".","``","CT","''",",","[","``","T2","''",",","``","ADC","''","]","Currently",",","the","configuration","supports","these","optional","keys",":","-","``","``","nnunet_raw","''","``",":","File","path","that","will","be","written","to","env","variable","for","nnU-Net","-","``","``","nnunet_preprocessed","''","``",":","File","path","that","will","be","written","to","env","variable","for","nnU-Net","-","``","``","nnunet_results","''","``",":","File","path","that","will","be","written","to","env","variable","for","nnU-Net","-","``","``","nnUNet_trained_models","''","``","-","``","``","dataset_name_or_id","''","``",":","Name","or","Integer","ID","of","the","dataset","If","an","optional","key","is","not","specified",",","then","the","pipeline","will","use","the","default","values",".","trainer_class_name",":","the","trainer","class","names","offered","by","nnUNetV2","exhibit","variations","in","training","duration",".","Default",":","``","nnUNetTrainer","''",".","Other","options",":","``","nnUNetTrainer_Xepoch","''",".","X","could","be","one","of","1,5,10,20,50,100",",","250,2000,4000,8000.","export_validation_probabilities",":","True","to","save","softmax","predictions","from","final","validation","as","npz","files","(","in","addition","to","predicted","segmentations",")",".","Needed","for","finding","the","best","ensemble",".","Default",":","True",".","work_dir",":","working","directory","to","save","the","intermediate","and","final","results",".","Examples",":","-","Use","the","one-liner","to","start","the","nnU-Net","workflow","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","run","--","input_config",".\/input.yaml","-","Use","`","convert_dataset","`","to","prepare","the","data","to","meet","nnU-Net","requirements",",","generate","dataset","JSON","file",",","and","copy","the","dataset","to","a","location","specified","by","``","nnunet_raw","``","in","the","input","config","file","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","convert_dataset","--","input_config=","''",".\/input.yaml","''","-","`","convert_msd_dataset","`","is","an","alternative","option","to","prepare","the","data","if","the","dataset","is","MSD",".","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","convert_msd_dataset","\\","--","input_config","``",".\/input.yaml","''","--","data_dir","``","\/path\/to\/Task09_Spleen","''","-","experiment","planning","and","data","pre-processing","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","plan_and_process","--","input_config","``",".\/input.yaml","''","-","training","all","20","models","using","all","GPUs","available",".","``","CUDA_VISIBLE_DEVICES","''","environment","variable","is","not","supported",".","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","train","--","input_config","``",".\/input.yaml","''","-","training","a","single","model","on","a","single","GPU","for","5","epochs",".","Here","``","config","``","is","used","to","specify","the","configuration",".","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","train_single_model","--","input_config","``",".\/input.yaml","''","\\","--","config","``","3d_fullres","''","\\","--","fold","0","\\","--","gpu_id","0","\\","--","trainer_class_name","``","nnUNetTrainer_5epochs","''","\\","--","export_validation_probabilities","True","-","training","for","all","20","models","(","4","configurations","by","5","folds",")","on","2","GPUs","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","train","--","input_config","``",".\/input.yaml","''","--","gpu_id_for_all","``","0,1","''","-","5-fold","training","for","a","single","model","on","2","GPUs",".","Here","``","configs","``","is","used","to","specify","the","configurations",".","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","train","--","input_config","``",".\/input.yaml","''","\\","--","configs","``","3d_fullres","''","\\","--","trainer_class_name","``","nnUNetTrainer_5epochs","''","\\","--","export_validation_probabilities","True","\\","--","gpu_id_for_all","``","0,1","''","-","find","the","best","configuration","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","find_best_configuration","--","input_config","``",".\/input.yaml","''","-","predict",",","ensemble",",","and","post-process","..","code-block",":",":","bash","python","-m","monai.apps.nnunet","nnUNetV2Runner","predict_ensemble_postprocessing","--","input_config","``",".\/input.yaml","''"]
docstr: Run prediction, ensemble, and/or postprocessing optionally.
  
  Args:
      folds: which folds to use
      run_ensemble: whether to run ensembling.
      run_predict: whether to predict using trained checkpoints
      run_postprocessing: whether to conduct post-processing
      kwargs: this optional parameter allows you to specify additional arguments defined in the
          ``predict`` method.
docstr_tok: ["Run","prediction",",","ensemble",",","and\/or","postprocessing","optionally",".","Args",":","folds",":","which","folds","to","use","run_ensemble",":","whether","to","run","ensembling",".","run_predict",":","whether","to","predict","using","trained","checkpoints","run_postprocessing",":","whether","to","conduct","post-processing","kwargs",":","this","optional","parameter","allows","you","to","specify","additional","arguments","defined","in","the","``","predict","``","method","."]
code: def predict_ensemble_postprocessing(
      self,
      folds: tuple = (0, 1, 2, 3, 4),
      run_ensemble: bool = True,
      run_predict: bool = True,
      run_postprocessing: bool = True,
      **kwargs: Any,
  ) -> None:
      """
      Run prediction, ensemble, and/or postprocessing optionally.
  
      Args:
          folds: which folds to use
          run_ensemble: whether to run ensembling.
          run_predict: whether to predict using trained checkpoints
          run_postprocessing: whether to conduct post-processing
          kwargs: this optional parameter allows you to specify additional arguments defined in the
              ``predict`` method.
      """
      from nnunetv2.ensembling.ensemble import ensemble_folders
      from nnunetv2.postprocessing.remove_connected_components import (
          apply_postprocessing_to_folder,
      )
      from nnunetv2.utilities.file_path_utilities import (
          get_output_folder,
      )
  
      source_dir = join(self.nnunet_raw, self.dataset_name, "imagesTs")
      target_dir_base = join(self.nnunet_results, self.dataset_name)
  
      self.best_configuration = ConfigParser.load_config_file(
          os.path.join(
              self.nnunet_results,
              self.dataset_name,
              "inference_information.json",
          )
      )
  
      run_ensemble = (
          run_ensemble
          and len(
              self.best_configuration["best_model_or_ensemble"][
                  "selected_model_or_models"
              ]
          )
          > 1
      )
  
      used_folds = folds
      output_folders = []
      for im in self.best_configuration["best_model_or_ensemble"][
          "selected_model_or_models"
      ]:
          output_dir = join(
              target_dir_base, f"pred_{im['configuration']}"
          )
          output_folders.append(output_dir)
  
          if run_predict:
              model_folder = get_output_folder(
                  int(self.dataset_name_or_id),
                  im["trainer"],
                  im["plans_identifier"],
                  im["configuration"],
              )
              self.predict(
                  list_of_lists_or_source_folder=source_dir,
                  output_folder=output_dir,
                  model_training_output_dir=model_folder,
                  use_folds=used_folds,
                  save_probabilities=run_ensemble,
                  verbose=False,
                  overwrite=True,
                  **kwargs,
              )
  
      # if we have an ensemble, we need to ensemble the results
      if run_ensemble:
          ensemble_folders(
              output_folders,
              join(target_dir_base, "ensemble_predictions"),
              save_merged_probabilities=False,
          )
          if run_postprocessing:
              folder_for_pp = join(
                  target_dir_base, "ensemble_predictions"
              )
      elif run_postprocessing:
          folder_for_pp = output_folders[0]
  
      # apply postprocessing
      if run_postprocessing:
          pp_fns, pp_fn_kwargs = load_pickle(
              self.best_configuration["best_model_or_ensemble"][
                  "postprocessing_file"
              ]
          )
          apply_postprocessing_to_folder(
              folder_for_pp,
              join(
                  target_dir_base, "ensemble_predictions_postprocessed"
              ),
              pp_fns,
              pp_fn_kwargs,
              plans_file_or_dict=self.best_configuration[
                  "best_model_or_ensemble"
              ]["some_plans_file"],
          )
  
imports: from __future__ import annotations
  import glob
  import os
  import subprocess
  from typing import Any
  import monai
  from monai.apps.nnunet.utils import NNUNETMode
  from monai.apps.nnunet.utils import analyze_data, create_new_data_copy, create_new_dataset_json
  from monai.bundle import ConfigParser
  from monai.utils import ensure_tuple, optional_import
label_desc: Use image node_id 16 for calling the nnUNetV2Runner obj's underlying member method code with example usage: obj.predict_ensemble_postprocessing(folds, run_ensemble, run_predict, run_postprocessing) without return types