code: def test_translate_equivalence(
      data: Dict[str, Any],
      translate_params_equiv: str,
      compare_output: bool,
  ):
      """
      Tests whether the output and scores generated by sockeye.translate with translate_params_equiv are equal to
      the previously generated outputs, referenced in the data dictionary.
      """
      out_path = os.path.join(data["work_dir"], "test.out.equiv")
      out_with_target_prefix_path = os.path.join(
          data["work_dir"], "test_with_target_prefix.out.equiv"
      )
  
      # First set of params (with target prefix in JSON format)
      params = "{} {} {}".format(
          sockeye.translate.__file__,
          TRANSLATE_PARAMS_COMMON.format(
              model=data["model"],
              input=data["test_source_with_target_prefix"],
              output=out_with_target_prefix_path,
          ),
          translate_params_equiv,
      )
      params += TRANSLATE_WITH_JSON_FORMAT
      with patch.object(sys, "argv", params.split()):
          sockeye.translate.main()
  
      # Collect translate outputs and scores
      translate_outputs_with_target_prefix_equiv = (
          collect_translate_output_and_scores(
              out_with_target_prefix_path
          )
      )
  
      # Second set of params (without using target prefix)
      params = "{} {} {}".format(
          sockeye.translate.__file__,
          TRANSLATE_PARAMS_COMMON.format(
              model=data["model"],
              input=data["test_source"],
              output=out_path,
          ),
          translate_params_equiv,
      )
      if "test_source_factors" in data:
          params += TRANSLATE_WITH_FACTORS_COMMON.format(
              input_factors=" ".join(data["test_source_factors"])
          )
      with patch.object(sys, "argv", params.split()):
          sockeye.translate.main()
      # Collect translate outputs and scores
      translate_outputs_equiv = collect_translate_output_and_scores(
          out_path
      )
  
      assert "test_outputs" in data
      assert "test_with_target_prefix_outputs" in data
      assert (
          len(data["test_outputs"])
          == len(data["test_with_target_prefix_outputs"])
          == len(translate_outputs_with_target_prefix_equiv)
          == len(translate_outputs_equiv)
      )
      if compare_output:
          for (
              json_output,
              json_output_with_target_prefix,
              json_output_equiv,
              json_output_with_target_prefix_equiv,
          ) in zip(
              data["test_outputs"],
              data["test_with_target_prefix_outputs"],
              translate_outputs_equiv,
              translate_outputs_with_target_prefix_equiv,
          ):
              assert (
                  json_output["translation"]
                  == json_output_equiv["translation"]
              ), f"'{json_output['translation']}' vs. '{json_output_equiv['translation']}'"
              assert (
                  json_output_with_target_prefix["translation"]
                  == json_output_with_target_prefix_equiv["translation"]
              ), f"'{json_output_with_target_prefix['translation']}' vs. '{json_output_with_target_prefix_equiv['translation']}'"
              assert abs(
                  json_output["score"] - json_output_equiv["score"]
              ) < 0.01 or np.isnan(
                  json_output["score"] - json_output_equiv["score"]
              ), f"'{json_output['score']}' vs. '{ json_output_equiv['score']}'"
              assert abs(
                  json_output_with_target_prefix["score"]
                  - json_output_with_target_prefix_equiv["score"]
              ) < 0.01 or np.isnan(
                  json_output_with_target_prefix["score"]
                  - json_output_with_target_prefix_equiv["score"]
              ), f"'{json_output_with_target_prefix['score']}' vs. '{ json_output_with_target_prefix_equiv['score']}'"
  
              # Check translation output always includes target prefix tokens
              prefix = json_output_with_target_prefix[
                  "target_prefix"
              ].split()
              translation = json_output_with_target_prefix[
                  "translation"
              ].split()
              ending = min(len(prefix), len(translation))
              assert (
                  prefix[:ending] == translation[:ending]
              ), f"'{prefix[:ending]}' vs. '{translation[:ending]}'"
  
              # Check translation output factors always include target prefix factors
              if (
                  "target_prefix_factors"
                  in json_output_with_target_prefix
              ):
                  prefix = json_output_with_target_prefix[
                      "target_prefix_factors"
                  ]
                  if len(prefix) > 0:
                      for j in range(1, len(prefix) + 1):
                          factors_from_translation = (
                              json_output_with_target_prefix[
                                  f"factor{j}"
                              ]
                          )
                          ending = min(
                              len(prefix[j - 1]),
                              len(factors_from_translation),
                          )
                          assert (
                              prefix[j - 1][:ending]
                              == factors_from_translation[:ending]
                          ), f"'{prefix[j - 1][:ending]}' vs. '{factors_from_translation[:ending]}' from . '{json_output_with_target_prefix}'"
  
file_path: sockeye/test/common.py
imports: import logging
  import os
  import sys
  from contextlib import ExitStack
  from typing import Any, Dict, List
  from unittest.mock import patch
  import numpy
  import sockeye.score
  import sockeye.translate
  from sockeye import constants
  from sockeye.test_utils import run_train_translate, run_translate_restrict, TRANSLATE_PARAMS_COMMON, TRANSLATE_WITH_FACTORS_COMMON, collect_translate_output_and_scores, SCORE_PARAMS_COMMON, SCORE_WITH_SOURCE_FACTORS_COMMON, SCORE_WITH_TARGET_FACTORS_COMMON, TRANSLATE_WITH_JSON_FORMAT
label_desc: Use image node_id 2 for calling a global function with example usage: test_translate_equivalence(data, translate_params_equiv, compare_output) without return types