13from torch.nn.functional
import one_hot
18from sklearn.preprocessing
import PolynomialFeatures
21from priorDataLoaderAndModel
import PriorModel
26def data_load(data: np.array) -> torch.FloatTensor:
28 Creates a dataset in the format that
is required by the model
for processing.
31 data(np.array): A 2D numpy array containing cos(theta)
as the first column
and momentum
as the second column.
34 A torch tensor containing second order polynomial feature transformation of the provided data along
with the
35 additional transverse momentum.
37 x = np.sin(np.arccos(data[:, 0])) * data[:, 1]
38 x = np.hstack((data, x.reshape(-1, 1)))
39 pf = PolynomialFeatures(2, include_bias=False)
40 x = pf.fit_transform(x)
41 return torch.FloatTensor(x)
47 Creates the temperature scaling object for calibration.
50 dat(str): The path to the scaling file generated during training.
53 The scaling
class to transform the output predictions from the model.
58 from netcal.scaling
import TemperatureScaling
60 data = ur.open(dat)[
"scale"].pandas.df()
61 pdg_list = list(data.columns)
62 pdg_list.remove(
"truth")
63 sorted_list = np.sort([e[:-4]
for e
in pdg_list])
64 y = data[sorted_list[0] +
"_pdg"].values.reshape(-1, 1)
65 for i
in range(len(sorted_list) - 1):
66 y = np.hstack((y, data[sorted_list[i + 1] +
"_pdg"].values.reshape(-1, 1)))
67 temp = TemperatureScaling()
68 temp.fit(y, one_hot(torch.LongTensor(data[
"truth"].values)).numpy())
74 Class to calculate PID prior probabilities and posteriors.
77 model(PriorModel): The trained model to be used
for evaluation.
78 plist(np.array): List of particle PDGs
for which the model was trained.
79 require_scale(bool):
True if a scaling file
is provided
or else False.
80 scale(TemperatureScaling) (optional): Calibration object constructed
for temperature scaling.
83 def __init__(self, particlelist: list, Model: str, prescaling: str =
None):
85 Initialize the Priors class.
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.
93 model.load_state_dict(torch.load(Model))
95 if torch.cuda.is_available():
96 model = model.to(
"cuda")
99 if prescaling
is not None:
100 scale = scaling(prescaling)
113 Calculates priors for given momentum
and cos(theta).
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.
122 y = data_load(np.hstack((cosTheta.reshape(-1, 1), momentum.reshape(-1, 1))))
123 if torch.cuda.is_available():
126 if torch.cuda.is_available():
128 out = out.detach().numpy()
131 out = self.
scale.transform(out)
137 Gives the calculated PID priors.
140 pdg(int) (optional): The PDG value of the particles for which prior probabilities are needed.
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.
147 index = np.where(self.
plist == pdg)[0][0]
148 return self.
prior[:, index]
154 Get PID posterior probabilities.
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.
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.
164 priorpid = np.multiply(self.prior, pid)
165 sumprpid = np.sum(priorpid, axis=1)
166 posterior = np.divide(priorpid, sumprpid.reshape(-1, 1))
170 index = np.where(self.
plist == pdg)[0][0]
171 return posterior[:, index]
def __init__(self, list particlelist, str Model, str prescaling=None)
def calculate_priors(self, np.array momentum, np.array cosTheta)
require_scale
True if the scaling object exist.
model
The torch model for prior calculation.
prior
Numpy array containing PID prior probability data.
plist
Sorted particle PDG list.
np.array get_posterior(self, int pid, int pdg=None)
scale
Temperature scaling object for calibration.
np.array get_priors(self, int pdg=None)