code: def setup_model_train(
      model_name: str,
      dataset_path: str,
      project_path: str,
      nncf: bool,
      category: str,
      score_type: Optional[str] = None,
      fast_run: bool = False,
      dataset_task: Optional[TaskType] = None,
      visualizer_mode: Optional[str] = None,
      device: Union[List[int], int] = [0],
  ) -> Tuple[
      Union[DictConfig, ListConfig],
      LightningDataModule,
      AnomalyModule,
      Trainer,
  ]:
      """Train the model based on the parameters passed.
  
      Args:
          model_name (str): Name of the model to train.
          dataset_path (str): Location of the dataset.
          project_path (str): Path to temporary project folder.
          nncf (bool): Add nncf callback.
          category (str): Category to train on.
          score_type (str, optional): Only used for DFM. Defaults to None.
          fast_run (bool, optional): If set to true, the model trains for only 1 epoch. We train for one epoch as
              this ensures that both anomalous and non-anomalous images are present in the validation step.
          dataset_task (str, optional): Specify the type of task. Must be in ["classification", "segmentation"].
              Used for integration testing of model / task / visualizer_mode.
          visualizer_mode (str, optional): Specify the type of visualization. Must be in ["full", "simple"].
              Used for integration testing of model / task / visualizer_mode.
          device (List[int], int, optional): Select which device you want to train the model on. Defaults to first GPU.
  
      Returns:
          Tuple[DictConfig, LightningDataModule, AnomalyModule, Trainer]: config, datamodule, trained model, trainer
      """
      config = get_configurable_parameters(model_name=model_name)
      if score_type is not None:
          config.model.score_type = score_type
      config.project.seed = 42
      config.dataset.format = "mvtec"
      config.dataset.category = category
      config.dataset.path = dataset_path
      config.project.log_images_to = []
      config.trainer.devices = device
      config.trainer.accelerator = "gpu" if device != 0 else "cpu"
      if dataset_task is not None:
          config.dataset.task = dataset_task
      if visualizer_mode is not None:
          config.visualization.mode = visualizer_mode
          config.visualization.save_images = (
              True  # Enforce processing by Visualizer
          )
          if (
              "pixel" in config.metrics
              and dataset_task == TaskType.CLASSIFICATION
          ):
              del config.metrics.pixel
  
      # Remove legacy flags
      for legacy_device in [
          "num_processes",
          "gpus",
          "ipus",
          "tpu_cores",
      ]:
          if legacy_device in config.trainer:
              config.trainer[legacy_device] = None
  
      if nncf:
          config.optimization["nncf"] = {
              "apply": True,
              "input_info": {"sample_size": None},
          }
          config = update_nncf_config(config)
          config.init_weights = None
  
      # reassign project path as config is updated in `update_config_for_nncf`
      config.project.path = project_path
  
      datamodule = get_datamodule(config)
      model = get_model(config)
  
      callbacks = get_callbacks(config)
  
      # Force model checkpoint to create checkpoint after first epoch
      if fast_run is True:
          for index, callback in enumerate(callbacks):
              if isinstance(callback, ModelCheckpoint):
                  callbacks.pop(index)
                  break
          model_checkpoint = ModelCheckpoint(
              dirpath=os.path.join(config.project.path, "weights"),
              filename="last",
              monitor=None,
              mode="max",
              save_last=True,
              auto_insert_metric_name=False,
          )
          callbacks.append(model_checkpoint)
  
      for index, callback in enumerate(callbacks):
          if isinstance(callback, BaseVisualizerCallback):
              callbacks.pop(index)
              break
  
      # Train the model.
      if fast_run:
          config.trainer.max_epochs = 1
          config.trainer.check_val_every_n_epoch = 1
  
      trainer = Trainer(callbacks=callbacks, **config.trainer)
      trainer.fit(model=model, datamodule=datamodule)
      return config, datamodule, model, trainer
  
file_path: anomalib/tests/helpers/model.py
imports: import os
  from typing import Dict, List, Optional, Tuple, Union
  import numpy
  from omegaconf import DictConfig, ListConfig
  from pytorch_lightning import LightningDataModule, Trainer
  from pytorch_lightning.callbacks import ModelCheckpoint
  from anomalib.config import get_configurable_parameters, update_nncf_config
  from anomalib.data import TaskType, get_datamodule
  from anomalib.models import get_model
  from anomalib.models.components import AnomalyModule
  from anomalib.utils.callbacks import get_callbacks
  from anomalib.utils.callbacks.visualizer import BaseVisualizerCallback
label_desc: Use image node_id 1 for calling a global function with example usage: setup_model_train(model_name, dataset_path, project_path, nncf, category, score_type, fast_run, dataset_task, visualizer_mode, device) and returns: config,  datamodule,  model,  trainer