docstr: Prepare cross fold training
  
  This class creates folds out of the given source files.
  The individual splits are the optionally written to the `output_dir` in a json format.
  
  The file with index i will be assigned to fold i % n_folds (not randomly!)
docstr_tok: ["Prepare","cross","fold","training","This","class","creates","folds","out","of","the","given","source","files",".","The","individual","splits","are","the","optionally","written","to","the","`","output_dir","`","in","a","json","format",".","The","file","with","index","i","will","be","assigned","to","fold","i","%","n_folds","(","not","randomly","!",")"]
code: def __init__(
      self,
      n_folds: int,
      data_generator_params: CalamariDataGeneratorParams,
      output_dir: str,
      folds: Optional[List[str]] = None,
  ):
      """Prepare cross fold training
  
      This class creates folds out of the given source files.
      The individual splits are the optionally written to the `output_dir` in a json format.
  
      The file with index i will be assigned to fold i % n_folds (not randomly!)
      """
      self.n_folds = n_folds
      self.data_generator_params = data_generator_params
      self.output_dir = os.path.abspath(output_dir)
  
      if not os.path.exists(output_dir):
          os.makedirs(output_dir)
  
      if len(self.data_generator_params) == 0:
          raise Exception("Empty dataset")
  
      if self.n_folds <= 1:
          raise Exception("At least two folds are required")
  
      self.is_h5_dataset = not isinstance(
          self.data_generator_params, FileDataParams
      )
      self.folds = folds
  
file_path: calamari/calamari_ocr/ocr/training/cross_fold.py
imports: import os
  import json
  from contextlib import ExitStack
  from typing import List, Optional
  from tfaip.data.pipeline.definitions import Sample, PipelineMode
  from tfaip.util.multiprocessing.parallelmap import tqdm_wrapper
  from calamari_ocr.ocr.dataset.datareader.base import CalamariDataGeneratorParams
  from calamari_ocr.ocr.dataset.datareader.file import FileDataGenerator, FileDataParams
  from calamari_ocr.ocr.dataset.datareader.hdf5 import Hdf5DatasetWriter
label_desc: Use image node_id 3 to create a new CrossFold object  with example: obj = CrossFold(n_folds, data_generator_params, output_dir, folds)