Belle II Software development
FittedGroupedDEDXEstimatorTrainer Class Reference
Inheritance diagram for FittedGroupedDEDXEstimatorTrainer:
GroupedDEDXEstimationTrainer DEDXEstimationTrainer FunctionFittedGroupedDEDXEstimatorTrainer MaximumEstimatorTrainer MaximumEstimatorTrainerSQRT MedianEstimatorTrainer MedianEstimatorTrainerSQRT GaussianEstimatorTrainer GaussianEstimatorTrainerSQRT LandauEstimatorTrainer LandauEstimatorTrainerSQRT

Public Member Functions

def __init__ (self, result_function, use_sigma_for_result_fitting)
 
def create_result_dataframe (self)
 
def fit_result_parameters (self)
 
def train (self, data)
 
def plot_fit_result (self, data)
 
def plot_grouped_result (self, data)
 

Public Attributes

 result_function
 cached copy of the result function
 
 result_parameters_for_each_dedx_bin
 cached copy of the dictionary of fitting parameters for each dE/dx bin
 
 use_sigma_for_result_fitting
 cached copy of the flag to add mean+/-sigma values to the output Dataframe
 
 dedx_estimator_function
 cached copies of the fit parameters and estimator function
 

Detailed Description

Train a neural network for dE/dx-based particle identification

Definition at line 92 of file train.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  result_function,
  use_sigma_for_result_fitting 
)
Constructor

Reimplemented from DEDXEstimationTrainer.

Reimplemented in GaussianEstimatorTrainer, LandauEstimatorTrainer, MaximumEstimatorTrainer, MedianEstimatorTrainer, GaussianEstimatorTrainerSQRT, LandauEstimatorTrainerSQRT, MaximumEstimatorTrainerSQRT, MedianEstimatorTrainerSQRT, and FunctionFittedGroupedDEDXEstimatorTrainer.

Definition at line 95 of file train.py.

95 def __init__(self, result_function, use_sigma_for_result_fitting):
96 """Constructor"""
97
98
99 self.result_function = result_function
100
101 self.result_parameters_for_each_dedx_bin = {}
102
103 self.use_sigma_for_result_fitting = use_sigma_for_result_fitting
104
105 GroupedDEDXEstimationTrainer.__init__(self)
106

Member Function Documentation

◆ create_result_dataframe()

def create_result_dataframe (   self)
Fit for the mean dE/dx and standard deviation, return the fit Dataframe

Definition at line 107 of file train.py.

107 def create_result_dataframe(self):
108 """Fit for the mean dE/dx and standard deviation, return the fit Dataframe"""
109 result_df = pd.DataFrame([{"dedx_bin_center": dedx_bin_center,
110 "mu": fit_parameters[1][1],
111 "sigma": fit_parameters[0]} for dedx_bin_center,
112 fit_parameters in self.result_parameters_for_each_dedx_bin.items()
113 if fit_parameters is not None])
114
115 if len(result_df) == 0:
116 raise ValueError("Could not find any fitted parameters!")
117
118 if self.use_sigma_for_result_fitting:
119 result_df["mu_plus_sigma"] = result_df.mu + result_df.sigma
120 result_df["mu_minus_sigma"] = result_df.mu - result_df.sigma
121
122 result_df.sort("dedx_bin_center", inplace=True)
123
124 return result_df
125

◆ fit_result_parameters()

def fit_result_parameters (   self)
Define the parameters for the fit, assign initial guesses

Definition at line 126 of file train.py.

126 def fit_result_parameters(self):
127 """Define the parameters for the fit, assign initial guesses"""
128 result_df = self.create_result_dataframe()
129
130 p0 = (7e+08, -4e+04, 0.1, 0)
131
132 if self.use_sigma_for_result_fitting:
133 popt, pcov = curve_fit(self.result_function, result_df.dedx_bin_center, result_df.mu, p0=p0,
134 sigma=result_df.sigma, absolute_sigma=True)
135 else:
136 popt, pcov = curve_fit(self.result_function, result_df.dedx_bin_center, result_df.mu, p0=p0)
137
138 return popt, lambda dedx: self.result_function(dedx, *popt)
139

◆ plot_fit_result()

def plot_fit_result (   self,
  data 
)
Plot the fitted results

Definition at line 154 of file train.py.

154 def plot_fit_result(self, data):
155 """Plot the fitted results"""
156 plot_dedx_data = np.linspace(data[self.dedx_column].min(), data[self.dedx_column].max(), 100)
157 result_df = self.create_result_dataframe()
158
159 plt.plot(plot_dedx_data, self.dedx_estimator_function(plot_dedx_data), color="black", label="Fitted estimator")
160 if self.use_sigma_for_result_fitting:
161 # color = "black"
162 plt.errorbar(result_df.dedx_bin_center, result_df.mu, marker="o", ls="", label="Data Points", yerr=result_df.sigma)
163
164 plt.ylim(0, 0.14)
165 plt.xlabel("dEdX in ADC count/cm")
166 plt.ylabel("p in GeV/c")
167 plt.legend(frameon=True)
168

◆ plot_grouped_result()

def plot_grouped_result (   self,
  data 
)
Plot the fitted grouped results

Reimplemented in FunctionFittedGroupedDEDXEstimatorTrainer.

Definition at line 169 of file train.py.

169 def plot_grouped_result(self, data):
170 """Plot the fitted grouped results"""
171 dedx_binned_data, dedx_bins = self.create_dedx_bins(data)
172
173 # List to prevent bug in pd.DataFrame.apply
174 already_plotted_list = []
175
176 def plot_fitted_results(dedx_bin):
177 dedx_bin_center = dedx_bin.mean().values[0]
178
179 if dedx_bin_center not in already_plotted_list:
180 already_plotted_list.append(dedx_bin_center)
181
182 fit_data = self.create_fit_data(dedx_bin)
183 plt.plot(fit_data.p_bin_centers, fit_data.number_of_p_values, ls="", marker=".", color="black")
184
185 return True
186
187 plt.xlabel("p in GeV/c")
188 plt.ylabel("Entries")
189
190 dedx_binned_data.apply(plot_fitted_results)
191
192

◆ train()

def train (   self,
  data 
)
Train the neural network using curated data

Reimplemented from DEDXEstimationTrainer.

Definition at line 140 of file train.py.

140 def train(self, data):
141 """Train the neural network using curated data"""
142 dedx_binned_data, dedx_bins = self.create_dedx_bins(data)
143
144 def fit_and_save_results(dedx_bin):
145 fit_result = self.fit_p_to_dedx_bin(dedx_bin)
146 return {dedx_bin.mean()[self.dedx_column]: fit_result}
147
148 for result in dedx_binned_data.apply(fit_and_save_results):
149 self.result_parameters_for_each_dedx_bin.update(result)
150
151
152 self.dedx_estimator_parameters, self.dedx_estimator_function = self.fit_result_parameters()
153
Definition: train.py:1

Member Data Documentation

◆ dedx_estimator_function

dedx_estimator_function

cached copies of the fit parameters and estimator function

Definition at line 152 of file train.py.

◆ result_function

result_function

cached copy of the result function

Definition at line 99 of file train.py.

◆ result_parameters_for_each_dedx_bin

result_parameters_for_each_dedx_bin

cached copy of the dictionary of fitting parameters for each dE/dx bin

Definition at line 101 of file train.py.

◆ use_sigma_for_result_fitting

use_sigma_for_result_fitting

cached copy of the flag to add mean+/-sigma values to the output Dataframe

Definition at line 103 of file train.py.


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