class_docstr: Builds ensembles out of runs that exist in the Backend
  
  This is used by EnsembleBuilderManager and created in a dask-client
  every time a run finishes and there is currently no EnsembleBuilder active.
docstr: Parameters
  ----------
  backend: Backend
      backend to write and read files
  
  dataset_name: str
      name of dataset
  
  task_type: int
      type of ML task
  
  metrics: Sequence[Scorer]
      Metrics to optimize the ensemble for. These must be non-duplicated.
  
  ensemble_class: Type[AbstractEnsemble]
      Implementation of the ensemble algorithm.
  
  ensemble_kwargs: Mapping[str, Any] | None
      Arguments passed to the constructor of the ensemble algorithm.
  
  ensemble_nbest: int | float = 50
  
      * int: consider only the n best prediction (> 0)
  
      * float: consider only this fraction of the best, between (0, 1)
  
      Both with respect to the validation predictions.
  
  max_models_on_disc: int | float | None = 100
     Defines the maximum number of models that are kept in the disc.
     It defines an upper bound on the models that can be used in the ensemble.
  
     * int: and dictates the max number of models to keep. (>= 1)
  
     * float: it will be interpreted as the max megabytes allowed of disc space.
     If the number of ensemble candidates require more disc space than
     this float value, the worst models are deleted to keep within this budget.
     Models and predictions of the worst-performing models will be deleted then.
  
     * None: the feature is disabled.
  
  seed: int = 1
      random seed that is used as part of the filename
  
  precision: int [16 | 32 | 64 | 128] = 32
      precision of floats to read the predictions
  
  memory_limit: int | None = 1024
      memory limit in mb. If ``None``, no memory limit is enforced.
  
  read_at_most: int | None = None
      read at most n new prediction files in each iteration. If `None`, will read
      the predictions and calculate losses for all runs that require it.
  
  
  logger_port: int = DEFAULT_TCP_LOGGING_PORT
      port that receives logging records
  
  random_state: int | RandomState | None = None
      An int or RandomState object used for generating the ensemble.
docstr_tok: ["Parameters","--","--","--","--","--","backend",":","Backend","backend","to","write","and","read","files","dataset_name",":","str","name","of","dataset","task_type",":","int","type","of","ML","task","metrics",":","Sequence","[","Scorer","]","Metrics","to","optimize","the","ensemble","for",".","These","must","be","non-duplicated",".","ensemble_class",":","Type","[","AbstractEnsemble","]","Implementation","of","the","ensemble","algorithm",".","ensemble_kwargs",":","Mapping","[","str",",","Any","]","|","None","Arguments","passed","to","the","constructor","of","the","ensemble","algorithm",".","ensemble_nbest",":","int","|","float","=","50","*","int",":","consider","only","the","n","best","prediction","(",">","0",")","*","float",":","consider","only","this","fraction","of","the","best",",","between","(","0",",","1",")","Both","with","respect","to","the","validation","predictions",".","max_models_on_disc",":","int","|","float","|","None","=","100","Defines","the","maximum","number","of","models","that","are","kept","in","the","disc",".","It","defines","an","upper","bound","on","the","models","that","can","be","used","in","the","ensemble",".","*","int",":","and","dictates","the","max","number","of","models","to","keep",".","(",">","=","1",")","*","float",":","it","will","be","interpreted","as","the","max","megabytes","allowed","of","disc","space",".","If","the","number","of","ensemble","candidates","require","more","disc","space","than","this","float","value",",","the","worst","models","are","deleted","to","keep","within","this","budget",".","Models","and","predictions","of","the","worst-performing","models","will","be","deleted","then",".","*","None",":","the","feature","is","disabled",".","seed",":","int","=","1","random","seed","that","is","used","as","part","of","the","filename","precision",":","int","[","16","|","32","|","64","|","128","]","=","32","precision","of","floats","to","read","the","predictions","memory_limit",":","int","|","None","=","1024","memory","limit","in","mb",".","If","``","None","``",",","no","memory","limit","is","enforced",".","read_at_most",":","int","|","None","=","None","read","at","most","n","new","prediction","files","in","each","iteration",".","If","`","None","`",",","will","read","the","predictions","and","calculate","losses","for","all","runs","that","require","it",".","logger_port",":","int","=","DEFAULT_TCP_LOGGING_PORT","port","that","receives","logging","records","random_state",":","int","|","RandomState","|","None","=","None","An","int","or","RandomState","object","used","for","generating","the","ensemble","."]
code: def __init__(
      self,
      backend: Backend,
      dataset_name: str,
      task_type: int,
      metrics: Sequence[Scorer],
      ensemble_class: Type[AbstractEnsemble] = EnsembleSelection,
      ensemble_kwargs: Mapping[str, Any] | None = None,
      ensemble_nbest: int | float = 50,
      max_models_on_disc: int | float | None = 100,
      seed: int = 1,
      precision: int = 32,
      memory_limit: int | None = 1024,
      read_at_most: int | None = None,
      logger_port: int = logging.handlers.DEFAULT_TCP_LOGGING_PORT,
      random_state: int | np.random.RandomState | None = None,
  ):
      """
      Parameters
      ----------
      backend: Backend
          backend to write and read files
  
      dataset_name: str
          name of dataset
  
      task_type: int
          type of ML task
  
      metrics: Sequence[Scorer]
          Metrics to optimize the ensemble for. These must be non-duplicated.
  
      ensemble_class: Type[AbstractEnsemble]
          Implementation of the ensemble algorithm.
  
      ensemble_kwargs: Mapping[str, Any] | None
          Arguments passed to the constructor of the ensemble algorithm.
  
      ensemble_nbest: int | float = 50
  
          * int: consider only the n best prediction (> 0)
  
          * float: consider only this fraction of the best, between (0, 1)
  
          Both with respect to the validation predictions.
  
      max_models_on_disc: int | float | None = 100
         Defines the maximum number of models that are kept in the disc.
         It defines an upper bound on the models that can be used in the ensemble.
  
         * int: and dictates the max number of models to keep. (>= 1)
  
         * float: it will be interpreted as the max megabytes allowed of disc space.
         If the number of ensemble candidates require more disc space than
         this float value, the worst models are deleted to keep within this budget.
         Models and predictions of the worst-performing models will be deleted then.
  
         * None: the feature is disabled.
  
      seed: int = 1
          random seed that is used as part of the filename
  
      precision: int [16 | 32 | 64 | 128] = 32
          precision of floats to read the predictions
  
      memory_limit: int | None = 1024
          memory limit in mb. If ``None``, no memory limit is enforced.
  
      read_at_most: int | None = None
          read at most n new prediction files in each iteration. If `None`, will read
          the predictions and calculate losses for all runs that require it.
  
  
      logger_port: int = DEFAULT_TCP_LOGGING_PORT
          port that receives logging records
  
      random_state: int | RandomState | None = None
          An int or RandomState object used for generating the ensemble.
      """
      if isinstance(ensemble_nbest, int) and ensemble_nbest < 1:
          raise ValueError(
              f"int ensemble_nbest ({ensemble_nbest}) must be (>1)"
          )
  
      if isinstance(ensemble_nbest, float) and not (
          0 <= ensemble_nbest <= 1
      ):
          raise ValueError(
              f"float ensemble_nbest ({ensemble_nbest}) not in (0,1)"
          )
  
      if max_models_on_disc is not None and max_models_on_disc < 0:
          raise ValueError(
              "max_models_on_disc must be positive or None"
          )
  
      if read_at_most is not None and (
          read_at_most < 1 or read_at_most == np.inf
      ):
          raise ValueError(
              "Read at most must be int greater than 1 or None"
          )
  
      # Setup the logger
      self.logger = get_named_client_logger(
          name="EnsembleBuilder", port=logger_port
      )
      self.logger_port = logger_port
  
      # Log the behaviour
      if ensemble_nbest == 1:
          t = type(ensemble_nbest)
          self.logger.debug(
              f"Using behaviour when {t} for {ensemble_nbest}:{t}"
          )
  
      self.seed = seed
      self.metrics = metrics
      self.backend = backend
      self.precision = precision
      self.task_type = task_type
      self.memory_limit = memory_limit
      self.read_at_most = read_at_most
      self.random_state = check_random_state(random_state)
      self.dataset_name = dataset_name
      self.ensemble_class = ensemble_class
      self.ensemble_kwargs = ensemble_kwargs
      self.ensemble_nbest = ensemble_nbest
  
      # Decide if self.max_models_on_disc is a memory limit or model limit
      self.max_models_on_disc: int | None = None
      self.model_memory_limit: float | None = None
  
      if isinstance(max_models_on_disc, int):
          self.max_models_on_disc = max_models_on_disc
      elif isinstance(max_models_on_disc, float):
          self.model_memory_limit = max_models_on_disc
  
      # The starting time of the procedure
      self.start_time: float = 0.0
  
      # Track the ensemble performance
      self.ensemble_history: list[dict[str, Any]] = []
  
      # Keep running knowledge of its validation performance
      self.validation_performance_ = np.inf
  
      # Data we may need
      # TODO: The test data is needlessly loaded but automl_common has no concept of
      # these and is perhaps too rigid
      datamanager: XYDataManager = self.backend.load_datamanager()
      self._X_test: SUPPORTED_FEAT_TYPES | None = datamanager.data.get(
          "X_test", None
      )
      self._y_test: np.ndarray | None = datamanager.data.get(
          "Y_test", None
      )
      self._X_ensemble: SUPPORTED_FEAT_TYPES | None = None
      self._y_ensemble: np.ndarray | None = None
  
imports: from __future__ import annotations
  from typing import Any, Iterable, Mapping, Sequence, Type, cast
  import logging.handlers
  import multiprocessing
  import numbers
  import os
  import pickle
  import time
  import traceback
  from itertools import accumulate
  from pathlib import Path
  import numpy
  import pandas
  import pynisher
  from sklearn.utils.validation import check_random_state
  from autosklearn.automl_common.common.utils.backend import Backend
  from autosklearn.data.validation import SUPPORTED_FEAT_TYPES
  from autosklearn.data.xy_data_manager import XYDataManager
  from autosklearn.ensemble_building.run import Run, RunID
  from autosklearn.ensembles.abstract_ensemble import AbstractEnsemble
  from autosklearn.ensembles.ensemble_selection import EnsembleSelection
  from autosklearn.metrics import Scorer, calculate_loss, calculate_scores
  from autosklearn.util.disk import rmtree
  from autosklearn.util.functional import cut, findwhere, roundrobin, split
  from autosklearn.util.logging_ import get_named_client_logger
  from autosklearn.util.parallel import preload_modules
label_desc: Use image node_id 1 to create a new EnsembleBuilder object  with example: obj = EnsembleBuilder(backend, dataset_name, task_type, metrics, ensemble_class, ensemble_kwargs, ensemble_nbest, max_models_on_disc, seed, precision, memory_limit, read_at_most, logger_port, random_state)