code: def test_scoring(
      data: Dict[str, Any],
      train_params: str,
      translate_params: str,
      test_similar_scores: bool,
  ):
      """
      Tests the scoring CLI and checks for score equivalence with previously generated translate scores.
      """
      # Train and translate params that affect the score need to be used for scoring as well.
      relevant_params = {
          "--end-of-prepending-tag",
          "--brevity-penalty-type",
          "--brevity-penalty-weight",
          "--brevity-penalty-constant-length-ratio",
          "--length-penalty-alpha",
          "--length-penalty-beta",
      }
      score_params = ""
      params = train_params.split() + translate_params.split()
      for i, param in enumerate(params):
          if param in relevant_params:
              score_params = "{} {}".format(param, params[i + 1])
      out_path = os.path.join(data["work_dir"], "score.out")
      out_with_target_prefix_path = os.path.join(
          data["work_dir"], "score_with_target_prefix.out"
      )
  
      # write translate outputs as target file for scoring and collect tokens
      # also optionally collect factor outputs
      target_path = os.path.join(data["work_dir"], "score.target")
      target_factor_paths = [
          os.path.join(data["work_dir"], "score.target.factor%d" % i)
          for i, _ in enumerate(data.get("test_target_factors", []), 1)
      ]
      with open(
          target_path, "w"
      ) as target_out, ExitStack() as exit_stack:
          target_factor_outs = [
              exit_stack.enter_context(open(p, "w"))
              for p in target_factor_paths
          ]
          for json_output in data["test_outputs"]:
              print(json_output["translation"], file=target_out)
              for i, factor_out in enumerate(target_factor_outs, 1):
                  factor = json_output[f"factor{i}"]
                  print(factor, file=factor_out)
  
      target_with_target_prefix_path = os.path.join(
          data["work_dir"], "score_with_target_prefix.target"
      )
      target_with_target_prefix_factor_paths = [
          os.path.join(
              data["work_dir"],
              f"score_with_target_prefix.target.factor{i}",
          )
          for i, _ in enumerate(data.get("test_target_factors", []), 1)
      ]
      with open(
          target_with_target_prefix_path, "w"
      ) as target_out, ExitStack() as exit_stack:
          target_factor_outs = [
              exit_stack.enter_context(open(p, "w"))
              for p in target_with_target_prefix_factor_paths
          ]
          for json_output in data["test_with_target_prefix_outputs"]:
              print(json_output["translation"], file=target_out)
              for i, factor_out in enumerate(target_factor_outs, 1):
                  factor = json_output[f"factor{i}"]
                  print(factor, file=factor_out)
  
      # First set of params (with target prefix in JSON format)
      params = "{} {} {}".format(
          sockeye.score.__file__,
          SCORE_PARAMS_COMMON.format(
              model=data["model"],
              source=data["test_source"],
              target=target_with_target_prefix_path,
              output=out_with_target_prefix_path,
          ),
          score_params,
      )
      if "test_source_factors" in data:
          params += SCORE_WITH_SOURCE_FACTORS_COMMON.format(
              source_factors=" ".join(data["test_source_factors"])
          )
      if target_with_target_prefix_factor_paths:
          params += SCORE_WITH_TARGET_FACTORS_COMMON.format(
              target_factors=" ".join(
                  target_with_target_prefix_factor_paths
              )
          )
  
      logger.info("Scoring with params %s", params)
      with patch.object(sys, "argv", params.split()):
          sockeye.score.main()
  
      # Collect scores from output file
      with open(out_with_target_prefix_path) as score_out:
          data_scoring_with_target_prefix = [
              [float(x) for x in line.strip().split("\t")]
              for line in score_out
          ]
  
      # Second set of params (without target prefix)
      params = "{} {} {}".format(
          sockeye.score.__file__,
          SCORE_PARAMS_COMMON.format(
              model=data["model"],
              source=data["test_source"],
              target=target_path,
              output=out_path,
          ),
          score_params,
      )
      if "test_source_factors" in data:
          params += SCORE_WITH_SOURCE_FACTORS_COMMON.format(
              source_factors=" ".join(data["test_source_factors"])
          )
      if target_factor_paths:
          params += SCORE_WITH_TARGET_FACTORS_COMMON.format(
              target_factors=" ".join(target_factor_paths)
          )
  
      logger.info("Scoring with params %s", params)
      with patch.object(sys, "argv", params.split()):
          sockeye.score.main()
  
      # Collect scores from output file
      with open(out_path) as score_out:
          data_scoring = [
              [float(x) for x in line.strip().split("\t")]
              for line in score_out
          ]
  
      if test_similar_scores:
          for (
              inp,
              translate_json,
              translate_with_target_prefix_json,
              score_scores,
              score_with_target_prefix_scores,
          ) in zip(
              data["test_inputs"],
              data["test_outputs"],
              data["test_with_target_prefix_outputs"],
              data_scoring,
              data_scoring_with_target_prefix,
          ):
              score_score, *factor_scores = score_scores
              translate_tokens = translate_json["translation"].split()
              translate_score = translate_json["score"]
              logger.info(
                  "tokens: %s || translate score: %.4f || score score: %.4f",
                  translate_tokens,
                  translate_score,
                  score_score,
              )
              assert (
                  translate_score == -np.inf and score_score == -np.inf
              ) or np.isclose(
                  translate_score, score_score, atol=1e-06
              ), (
                  "input: %s || tokens: %s || translate score: %.6f || score score: %.6f"
                  % (
                      inp,
                      translate_tokens,
                      translate_score,
                      score_score,
                  )
              )
              (
                  score_score,
                  *factor_scores,
              ) = score_with_target_prefix_scores
              translate_tokens = translate_with_target_prefix_json[
                  "translation"
              ].split()
              translate_score = translate_with_target_prefix_json[
                  "score"
              ]
              logger.info(
                  "tokens: %s || translate score: %.4f || score score: %.4f",
                  translate_tokens,
                  translate_score,
                  score_score,
              )
              assert (
                  translate_score == -np.inf and score_score == -np.inf
              ) or np.isclose(
                  translate_score, score_score, atol=1e-06
              ), (
                  "input: %s || tokens: %s || translate score: %.6f || score score: %.6f"
                  % (
                      inp,
                      translate_tokens,
                      translate_score,
                      score_score,
                  )
              )
  
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 3 for calling a global function with example usage: test_scoring(data, train_params, translate_params, test_similar_scores) without return types