Belle II Software development
Priors Class Reference

Public Member Functions

def __init__ (self, list particlelist, str Model, str prescaling=None)
 
def calculate_priors (self, np.array momentum, np.array cosTheta)
 
np.array get_priors (self, int pdg=None)
 
np.array get_posterior (self, int pid, int pdg=None)
 

Public Attributes

 model
 The torch model for prior calculation.
 
 scale
 Temperature scaling object for calibration.
 
 require_scale
 True if the scaling object exist.
 
 plist
 Sorted particle PDG list.
 
 prior
 Numpy array containing PID prior probability data.
 

Detailed Description

Class to calculate PID prior probabilities and posteriors.

Attributes:
    model(PriorModel): The trained model to be used for evaluation.
    plist(np.array): List of particle PDGs for which the model was trained.
    require_scale(bool): True if a scaling file is provided or else False.
    scale(TemperatureScaling) (optional): Calibration object constructed for temperature scaling.

Definition at line 72 of file evalPriors.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
list  particlelist,
str  Model,
str   prescaling = None 
)
Initialize the Priors class.

Parameters:
    particlelist(list(int)): List of PDG values for which the model was trained.
    Model(str): Path to a previously trained model which will be used to calculate priors.
    prescaling(str) (optional): Path to the scaling file created while training the model.

Definition at line 83 of file evalPriors.py.

83 def __init__(self, particlelist: list, Model: str, prescaling: str = None):
84 """
85 Initialize the Priors class.
86
87 Parameters:
88 particlelist(list(int)): List of PDG values for which the model was trained.
89 Model(str): Path to a previously trained model which will be used to calculate priors.
90 prescaling(str) (optional): Path to the scaling file created while training the model.
91 """
92 model = PriorModel(len(particlelist))
93 model.load_state_dict(torch.load(Model))
94 model.eval()
95 if torch.cuda.is_available():
96 model = model.to("cuda")
97
98 self.model = model
99 if prescaling is not None:
100 scale = scaling(prescaling)
101
102 self.scale = scale
103
104 self.require_scale = True
105 else:
106
107 self.require_scale = False
108
109 self.plist = np.sort(particlelist)
110

Member Function Documentation

◆ calculate_priors()

def calculate_priors (   self,
np.array  momentum,
np.array  cosTheta 
)
Calculates priors for given momentum and cos(theta).

Parameters:
    momentum(np.array): A numpy array containing the momentum of particles.
    cosTheta(np.array): A numpy array containing the cosTheta information of particles.

Returns:
    None.

Definition at line 111 of file evalPriors.py.

111 def calculate_priors(self, momentum: np.array, cosTheta: np.array):
112 """
113 Calculates priors for given momentum and cos(theta).
114
115 Parameters:
116 momentum(np.array): A numpy array containing the momentum of particles.
117 cosTheta(np.array): A numpy array containing the cosTheta information of particles.
118
119 Returns:
120 None.
121 """
122 y = data_load(np.hstack((cosTheta.reshape(-1, 1), momentum.reshape(-1, 1))))
123 if torch.cuda.is_available():
124 y = y.to("cuda")
125 out = self.model(y)
126 if torch.cuda.is_available():
127 out = out.to("cpu")
128 out = out.detach().numpy()
129
130 if self.require_scale:
131 out = self.scale.transform(out)
132
133 self.prior = out
134

◆ get_posterior()

np.array get_posterior (   self,
int  pid,
int   pdg = None 
)
Get PID posterior probabilities.

Parameters:
    pid(np.array): The PID values for the particles used during training process arranged in ascending order of PDG values.
    pdg(int) (optional): PDG value of particle for which posterior is required.

Returns:
    A 1D array of posterior probabilities in case PDG value is provided else returns a 2D array containing
    the posteriors for all particles.

Definition at line 152 of file evalPriors.py.

152 def get_posterior(self, pid: int, pdg: int = None) -> np.array:
153 """
154 Get PID posterior probabilities.
155
156 Parameters:
157 pid(np.array): The PID values for the particles used during training process arranged in ascending order of PDG values.
158 pdg(int) (optional): PDG value of particle for which posterior is required.
159
160 Returns:
161 A 1D array of posterior probabilities in case PDG value is provided else returns a 2D array containing
162 the posteriors for all particles.
163 """
164 priorpid = np.multiply(self.prior, pid)
165 sumprpid = np.sum(priorpid, axis=1)
166 posterior = np.divide(priorpid, sumprpid.reshape(-1, 1))
167 if pdg is None:
168 return posterior
169 else:
170 index = np.where(self.plist == pdg)[0][0]
171 return posterior[:, index]
Definition: pdg.py:1

◆ get_priors()

np.array get_priors (   self,
int   pdg = None 
)
Gives the calculated PID priors.

Parameters:
    pdg(int) (optional): The PDG value of the particles for which prior probabilities are needed.

Returns:
    A 1D array containing prior probabilities for required particle in case PDG value is specified;
    else it will return a 2D array for all particles that were used during training.

Definition at line 135 of file evalPriors.py.

135 def get_priors(self, pdg: int = None) -> np.array:
136 """
137 Gives the calculated PID priors.
138
139 Parameters:
140 pdg(int) (optional): The PDG value of the particles for which prior probabilities are needed.
141
142 Returns:
143 A 1D array containing prior probabilities for required particle in case PDG value is specified;
144 else it will return a 2D array for all particles that were used during training.
145 """
146 if pdg is not None:
147 index = np.where(self.plist == pdg)[0][0]
148 return self.prior[:, index]
149 else:
150 return self.prior
151

Member Data Documentation

◆ model

model

The torch model for prior calculation.

Definition at line 98 of file evalPriors.py.

◆ plist

plist

Sorted particle PDG list.

Definition at line 109 of file evalPriors.py.

◆ prior

prior

Numpy array containing PID prior probability data.

Definition at line 133 of file evalPriors.py.

◆ require_scale

require_scale

True if the scaling object exist.

False if the scaling object doesn't exist.

Definition at line 104 of file evalPriors.py.

◆ scale

scale

Temperature scaling object for calibration.

Definition at line 102 of file evalPriors.py.


The documentation for this class was generated from the following file: