code: def test_add_early_stopping_by_val_score():
      acc_scores = [0.1, 0.2, 0.3, 0.4, 0.3, 0.3, 0.2, 0.1, 0.1, 0.0]
  
      def setup_trainer():
          trainer = Engine(lambda e, b: None)
          evaluator = Engine(lambda e, b: None)
  
          @trainer.on(Events.EPOCH_COMPLETED)
          def validate(engine):
              evaluator.run([0, 1])
  
          @evaluator.on(Events.EPOCH_COMPLETED)
          def set_eval_metric(engine):
              acc = acc_scores[trainer.state.epoch - 1]
              engine.state.metrics = {"acc": acc, "loss": 1 - acc}
  
          return trainer, evaluator
  
      trainer, evaluator = setup_trainer()
  
      add_early_stopping_by_val_score(
          patience=3,
          evaluator=evaluator,
          trainer=trainer,
          metric_name="acc",
      )
  
      state = trainer.run([0, 1], max_epochs=len(acc_scores))
  
      assert state.epoch == 7
  
      trainer, evaluator = setup_trainer()
  
      add_early_stopping_by_val_score(
          patience=3,
          evaluator=evaluator,
          trainer=trainer,
          metric_name="loss",
          score_sign=-1.0,
      )
  
      state = trainer.run([0, 1], max_epochs=len(acc_scores))
  
      assert state.epoch == 7
  
imports: import os
  import sys
  from unittest.mock import call, MagicMock
  import pytest
  import torch
  import torch.nn
  from torch.utils.data.distributed import DistributedSampler
  import ignite.contrib.handlers
  import ignite.distributed
  from ignite.contrib.engines.common import _setup_logging, add_early_stopping_by_val_score, gen_save_best_models_by_val_score, save_best_model_by_val_score, setup_any_logging, setup_clearml_logging, setup_common_training_handlers, setup_mlflow_logging, setup_neptune_logging, setup_plx_logging, setup_tb_logging, setup_trains_logging, setup_visdom_logging, setup_wandb_logging
  from ignite.engine import Engine, Events
  from ignite.handlers import DiskSaver, TerminateOnNan
label_desc: Use image node_id 9 for calling a global function with example usage: test_add_early_stopping_by_val_score() and returns: trainer,  evaluator