code: def configure_trainer(
      config_train: Train,
      config: dict,
      metrics_logger,
      early_stopping: bool = False,
      early_stopping_target: str = "Loss",
      accelerator: Optional[str] = None,
      progress_bar_enabled: bool = True,
      metrics_enabled: bool = False,
      checkpointing_enabled: bool = False,
      num_batches_per_epoch: int = 100,
  ):
      """
      Configures the PyTorch Lightning trainer.
  
      Parameters
      ----------
          config_train : Dict
              dictionary containing the overall training configuration.
          config : dict
              dictionary containing the custom PyTorch Lightning trainer configuration.
          metrics_logger : MetricsLogger
              MetricsLogger object to log metrics to.
          early_stopping: bool
              If True, early stopping is enabled.
          early_stopping_target : str
              Target metric to use for early stopping.
          accelerator : str
              Accelerator to use for training.
          progress_bar_enabled : bool
              If False, no progress bar is shown.
          metrics_enabled : bool
              If False, no metrics are logged. Calculating metrics is computationally expensive and reduces the training
              speed.
          checkpointing_enabled : bool
              If False, no checkpointing is performed. Checkpointing reduces the training speed.
          num_batches_per_epoch : int
              Number of batches per epoch.
  
      Returns
      -------
          pl.Trainer
              PyTorch Lightning trainer
          checkpoint_callback
              PyTorch Lightning checkpoint callback to load the best model
      """
      config = config.copy()
  
      # Enable Learning rate finder if not learning rate provided
      if config_train.learning_rate is None:
          config["auto_lr_find"] = True
  
      # Set max number of epochs
      if hasattr(config_train, "epochs"):
          if config_train.epochs is not None:
              config["max_epochs"] = config_train.epochs
  
      # Configure the Ligthing-logs directory
      if "default_root_dir" not in config.keys():
          config["default_root_dir"] = os.getcwd()
  
      # Accelerator
      if isinstance(accelerator, str):
          if (
              accelerator == "auto" and torch.cuda.is_available()
          ) or accelerator == "gpu":
              config["accelerator"] = "gpu"
              config["devices"] = -1
          elif (
              accelerator == "auto" and hasattr(torch.backends, "mps")
          ) or accelerator == "mps":
              if torch.backends.mps.is_available():
                  config["accelerator"] = "mps"
                  config["devices"] = 1
          elif accelerator != "auto":
              config["accelerator"] = accelerator
              config["devices"] = 1
  
          if "accelerator" in config:
              log.info(
                  f"Using accelerator {config['accelerator']} with {config['devices']} device(s)."
              )
          else:
              log.info(
                  "No accelerator available. Using CPU for training."
              )
  
      # Configure metrics
      if metrics_enabled:
          config["logger"] = metrics_logger
      else:
          config["logger"] = False
  
      # Configure callbacks
      callbacks = []
      has_custom_callbacks = True if "callbacks" in config else False
  
      # Configure checkpointing
      has_modelcheckpoint_callback = (
          True
          if has_custom_callbacks
          and any(
              isinstance(callback, pl.callbacks.ModelCheckpoint)
              for callback in config["callbacks"]
          )
          else False
      )
      if has_modelcheckpoint_callback and not checkpointing_enabled:
          raise ValueError(
              "Checkpointing is disabled but a ModelCheckpoint callback is provided. Please enable checkpointing or "
              "remove the callback."
          )
      if checkpointing_enabled:
          if not has_modelcheckpoint_callback:
              # Callback to access both the last and best model
              checkpoint_callback = pl.callbacks.ModelCheckpoint(
                  monitor=early_stopping_target,
                  mode="min",
                  save_top_k=1,
                  save_last=True,
              )
              callbacks.append(checkpoint_callback)
          else:
              checkpoint_callback = next(
                  callback
                  for callback in config["callbacks"]
                  if isinstance(callback, pl.callbacks.ModelCheckpoint)
              )
      else:
          config["enable_checkpointing"] = False
          checkpoint_callback = None
  
      # Configure the progress bar, refresh every epoch
      has_progressbar_callback = (
          True
          if has_custom_callbacks
          and any(
              isinstance(callback, pl.callbacks.ProgressBarBase)
              for callback in config["callbacks"]
          )
          else False
      )
      if has_progressbar_callback and not progress_bar_enabled:
          raise ValueError(
              "Progress bar is disabled but a ProgressBar callback is provided. Please enable the progress bar or remove"
              " the callback."
          )
      if progress_bar_enabled:
          if not has_progressbar_callback:
              prog_bar_callback = ProgressBar(
                  refresh_rate=num_batches_per_epoch,
                  epochs=config_train.epochs,
              )
              callbacks.append(prog_bar_callback)
      else:
          config["enable_progress_bar"] = False
  
      # Early stopping monitor
      has_earlystopping_callback = (
          True
          if has_custom_callbacks
          and any(
              isinstance(callback, pl.callbacks.EarlyStopping)
              for callback in config["callbacks"]
          )
          else False
      )
      if has_earlystopping_callback and not early_stopping:
          raise ValueError(
              "Early stopping is disabled but an EarlyStopping callback is provided. Please enable early stopping or "
              "remove the callback."
          )
      if early_stopping:
          if not metrics_enabled:
              raise ValueError(
                  "Early stopping requires metrics to be enabled."
              )
          if not has_earlystopping_callback:
              early_stop_callback = pl.callbacks.EarlyStopping(
                  monitor=early_stopping_target,
                  mode="min",
                  patience=20,
                  divergence_threshold=5.0,
              )
              callbacks.append(early_stop_callback)
  
      if has_custom_callbacks:
          config["callbacks"].extend(callbacks)
      else:
          config["callbacks"] = callbacks
      config["num_sanity_val_steps"] = 0
      config["enable_model_summary"] = False
      # TODO: Disabling sampler_ddp brings a good speedup in performance, however, check whether this is a good idea
      # https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#replace-sampler-ddp
      # config["replace_sampler_ddp"] = False
  
      return pl.Trainer(**config), checkpoint_callback
  
imports: from __future__ import annotations
  import logging
  import math
  import os
  import sys
  from collections import OrderedDict
  from typing import TYPE_CHECKING, Iterable, Optional, Union
  import numpy
  import pandas
  import pytorch_lightning
  import torch
  from neuralprophet import utils_torch
  from neuralprophet.hdays_utils import get_country_holidays
  from neuralprophet.logger import ProgressBar
label_desc: Use image node_id 25 for calling a global function with example usage: configure_trainer(config_train, config, metrics_logger, early_stopping, early_stopping_target, accelerator, progress_bar_enabled, metrics_enabled, checkpointing_enabled, num_batches_per_epoch) and returns: pl,  checkpoint_callback