Belle II Software  release-05-01-25
scores.py
1 """This module contains score functions to quantify the quality of a classification
2 
3 All score function have the signature
4 def score(truths, predictions):
5 
6 comparing the truth information against a model and return a numerical value.
7 """
8 
9 import numpy as np
10 
11 
12 def data_amount(truths, predictions):
13  """Score function: amount of data after a selection"""
14  n_predictions = len(predictions)
15  n_truths = len(truths)
16 
17  if n_predictions != n_truths:
18  raise ValueError("Prediction and truth do not represent the same amount of data.")
19 
20  return n_predictions
21 
22 
23 def signal_amount(truths, predictions):
24  """Score function: amount of signal of a classification"""
25  return np.count_nonzero(truths)
26 
27 
28 def accepted_amount(truths, predictions):
29  """Score function: amount accepted of a classification"""
30  return np.count_nonzero(predictions)
31 
32 
33 def accepted_signal_amount(truths, predictions):
34  """Score function: amount of accepted signal of a classification"""
35  return np.count_nonzero(predictions * truths)
36 
37 
38 # Functions independent of the data model
39 
40 # Amounts #
41 # ####### #
42 
43 def background_amount(truths, predictions):
44  """Score function: amount of background of a classification"""
45  return data_amount(truths, predictions) - signal_amount(truths, predictions)
46 
47 
48 def rejected_amount(truths, predictions):
49  """Score function: amount rejected of a classification"""
50  return data_amount(truths, predictions) - accepted_amount(truths, predictions)
51 
52 
53 def rejected_signal_amount(truths, predictions):
54  """Score function: amount of rejected signal of a classification"""
55  return signal_amount(truths, predictions) - accepted_signal_amount(truths, predictions)
56 
57 
58 def accepted_background_amount(truths, predictions):
59  """Score function: amount of accepted background of a classification"""
60  return accepted_amount(truths, predictions) - accepted_signal_amount(truths, predictions)
61 
62 
63 def rejected_background_amount(truths, predictions):
64  """Score function: amount of rejected background of a classification"""
65  return background_amount(truths, predictions) - accepted_background_amount(truths, predictions)
66 
67 
68 # Ratios #
69 # ###### #
70 
71 def purity(truths, predictions):
72  """Score function: purity = accepted signal / accepted"""
73  return np.divide(1.0 * accepted_signal_amount(truths, predictions), accepted_amount(truths, predictions))
74 
75 
76 def efficiency(truths, predictions):
77  """Score function: efficiency = accepted signal / signal"""
78  return np.divide(1.0 * accepted_signal_amount(truths, predictions), signal_amount(truths, predictions))
79 
80 
81 def accuracy(truths, predictions):
82  """Score function: accuracy = (accepted signal + rejected background) / total"""
83  n_correct = accepted_signal_amount(truths, predictions) + rejected_background(truths, predictions)
84  n_data = data_amount(truths, predictions)
85  return np.divide(1.0 * n_correct, n_data)
86 
87 
88 def background_rejection(truths, predictions):
89  """Score function: background rejection = rejected background / background"""
90  n_background = background_amount(truths, predictions)
91  n_rejected_background = rejected_background_amount(truths, predictions)
92  return np.divide(1.0 * n_rejected_background, n_background)
93 
94 
95 def signal_background_ratio(truths, predictions):
96  """Score function: background / signal"""
97  n_data = data_amount(truths, predictions)
98  n_signal = signal_amount(truths, predictions)
99  n_background = n_data - n_signal
100  return np.divide(1.0 * n_signal, n_background)
tracking.validation.scores.background_amount
def background_amount(truths, predictions)
Definition: scores.py:43
tracking.validation.scores.accepted_background_amount
def accepted_background_amount(truths, predictions)
Definition: scores.py:58
tracking.validation.scores.accepted_signal_amount
def accepted_signal_amount(truths, predictions)
Definition: scores.py:33
tracking.validation.scores.efficiency
def efficiency(truths, predictions)
Definition: scores.py:76
tracking.validation.scores.rejected_background_amount
def rejected_background_amount(truths, predictions)
Definition: scores.py:63
tracking.validation.scores.rejected_amount
def rejected_amount(truths, predictions)
Definition: scores.py:48
tracking.validation.scores.signal_background_ratio
def signal_background_ratio(truths, predictions)
Definition: scores.py:95
tracking.validation.scores.data_amount
def data_amount(truths, predictions)
Definition: scores.py:12
tracking.validation.scores.purity
def purity(truths, predictions)
Definition: scores.py:71
tracking.validation.scores.signal_amount
def signal_amount(truths, predictions)
Definition: scores.py:23
tracking.validation.scores.accepted_amount
def accepted_amount(truths, predictions)
Definition: scores.py:28
tracking.validation.scores.accuracy
def accuracy(truths, predictions)
Definition: scores.py:81
tracking.validation.scores.background_rejection
def background_rejection(truths, predictions)
Definition: scores.py:88
tracking.validation.scores.rejected_signal_amount
def rejected_signal_amount(truths, predictions)
Definition: scores.py:53