code: def check_targets_scores(explanation, atol=1e-8):
      # type: (Explanation, float) -> None
      """Check that feature weights sum to target score or proba,
      if both proba and score are present they match,
      and that there are no "remaining" features.
      """
      targets = explanation.targets
      for target in targets:
          weights = target.feature_weights
          # else the check is invalid
          assert weights.neg_remaining == weights.pos_remaining == 0
          weights_sum = sum(fw.weight for fw in weights.pos) + sum(
              fw.weight for fw in weights.neg
          )
          expected = (
              target.score if target.score is not None else target.proba
          )
          assert np.isclose(
              abs(expected), abs(weights_sum), atol=atol
          ), (expected, weights_sum)
      if any(t.score is not None for t in targets):
          if len(targets) == 1 and targets[0].proba is not None:
              target = targets[0]
              # one target with proba => assume sigmoid
              proba = 1.0 / (1 + np.exp(-target.score))
              assert np.isclose(
                  target.proba, proba, atol=atol
              ) or np.isclose(target.proba, 1 - proba, atol=atol)
          elif any(t.proba is not None for t in targets):
              # many targets with proba => assume softmax
              norm = np.sum(np.exp([t.score for t in targets]))
              for target in targets:
                  assert np.isclose(
                      np.exp(target.score) / norm,
                      target.proba,
                      atol=atol,
                  )
  
file_path: eli5/tests/utils.py
imports: from __future__ import print_function
  import os
  import inspect
  import json
  from pprint import pprint
  from hypothesis.strategies import integers
  from hypothesis.extra.numpy import arrays
  import numpy
  from eli5.base import Explanation
  from eli5.formatters import format_as_text, format_as_html, format_as_dict
  from eli5.formatters.html import html_escape
  from eli5.formatters.text import format_signed
  from eli5.sklearn.utils import sklearn_version
label_desc: Use image node_id 7 for calling a global function with example usage: check_targets_scores(explanation, atol) without return types