LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar_content::SupportVectorMachine Class Reference

SupportVectorMachine class. More...

#include "LArSupportVectorMachine.h"

Inheritance diagram for lar_content::SupportVectorMachine:
lar_content::MvaInterface

Classes

class  FeatureInfo
 FeatureInfo class. More...
 
class  SupportVectorInfo
 SupportVectorInfo class. More...
 

Public Types

enum  KernelType {
  USER_DEFINED = 0, LINEAR = 1, QUADRATIC = 2, CUBIC = 3,
  GAUSSIAN_RBF = 4
}
 KernelType enum. More...
 
typedef std::function< double(const LArMvaHelper::MvaFeatureVector &, const LArMvaHelper::MvaFeatureVector &, const double)> KernelFunction
 

Public Member Functions

 SupportVectorMachine ()
 Default constructor. More...
 
pandora::StatusCode Initialize (const std::string &parameterLocation, const std::string &svmName)
 Initialize the svm using a serialized model. More...
 
bool Classify (const LArMvaHelper::MvaFeatureVector &features) const
 Make a classification for a set of input features, based on the trained model. More...
 
double CalculateClassificationScore (const LArMvaHelper::MvaFeatureVector &features) const
 Calculate the classification score for a set of input features, based on the trained model. More...
 
double CalculateProbability (const LArMvaHelper::MvaFeatureVector &features) const
 Calculate the classification probability for a set of input features, based on the trained model. More...
 
bool IsInitialized () const
 Query whether this svm is initialized. More...
 
unsigned int GetNFeatures () const
 Get the number of features. More...
 
void SetKernelFunction (KernelFunction kernelFunction)
 Set the kernel function to use. More...
 

Private Types

typedef std::vector< SupportVectorInfoSVInfoList
 
typedef std::vector< FeatureInfoFeatureInfoVector
 
typedef std::map< KernelType, KernelFunctionKernelMap
 

Private Member Functions

void ReadXmlFile (const std::string &svmFileName, const std::string &svmName)
 Read the svm parameters from an xml file. More...
 
pandora::StatusCode ReadComponent (pandora::TiXmlElement *pCurrentXmlElement)
 Read the component at the current xml element. More...
 
pandora::StatusCode ReadMachine (const pandora::TiXmlHandle &currentHandle)
 Read the machine component at the current xml handle. More...
 
pandora::StatusCode ReadFeatures (const pandora::TiXmlHandle &currentHandle)
 Read the feature component at the current xml handle. More...
 
pandora::StatusCode ReadSupportVector (const pandora::TiXmlHandle &currentHandle)
 Read the support vector component at the current xml handle. More...
 
double CalculateClassificationScoreImpl (const LArMvaHelper::MvaFeatureVector &features) const
 Implementation method for calculating the classification score using the trained model. More...
 

Static Private Member Functions

static double QuadraticKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 An inhomogeneous quadratic kernel. More...
 
static double CubicKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 An inhomogeneous cubic kernel. More...
 
static double LinearKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 A linear kernel. More...
 
static double GaussianRbfKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 A gaussian RBF kernel. More...
 

Private Attributes

bool m_isInitialized
 Whether this svm has been initialized. More...
 
bool m_enableProbability
 Whether to enable probability calculations. More...
 
double m_probAParameter
 The first-order score coefficient for mapping to a probability using the logistic function. More...
 
double m_probBParameter
 The score offset parameter for mapping to a probability using the logistic function. More...
 
bool m_standardizeFeatures
 Whether to standardize the features. More...
 
unsigned int m_nFeatures
 The number of features. More...
 
double m_bias
 The bias term. More...
 
double m_scaleFactor
 The kernel scale factor. More...
 
SVInfoList m_svInfoList
 The list of SupportVectorInfo objects. More...
 
FeatureInfoVector m_featureInfoList
 The list of FeatureInfo objects. More...
 
KernelType m_kernelType
 The kernel type. More...
 
KernelFunction m_kernelFunction
 The kernel function. More...
 
KernelMap m_kernelMap
 Map from the kernel types to the kernel functions. More...
 

Detailed Description

SupportVectorMachine class.

Definition at line 30 of file LArSupportVectorMachine.h.

Member Typedef Documentation

Definition at line 162 of file LArSupportVectorMachine.h.

Definition at line 33 of file LArSupportVectorMachine.h.

Definition at line 161 of file LArSupportVectorMachine.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

lar_content::SupportVectorMachine::SupportVectorMachine ( )

Default constructor.

Definition at line 18 of file LArSupportVectorMachine.cc.

References CUBIC, CubicKernel(), GAUSSIAN_RBF, GaussianRbfKernel(), LINEAR, LinearKernel(), QUADRATIC, and QuadraticKernel().

18  :
19  m_isInitialized(false),
20  m_enableProbability(false),
21  m_probAParameter(0.),
22  m_probBParameter(0.),
24  m_nFeatures(0),
25  m_bias(0.),
26  m_scaleFactor(1.),
30 {
31 }
static double CubicKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
An inhomogeneous cubic kernel.
bool m_enableProbability
Whether to enable probability calculations.
double m_scaleFactor
The kernel scale factor.
static double QuadraticKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
An inhomogeneous quadratic kernel.
static double LinearKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
A linear kernel.
bool m_standardizeFeatures
Whether to standardize the features.
KernelMap m_kernelMap
Map from the kernel types to the kernel functions.
KernelFunction m_kernelFunction
The kernel function.
KernelType m_kernelType
The kernel type.
double m_probAParameter
The first-order score coefficient for mapping to a probability using the logistic function...
unsigned int m_nFeatures
The number of features.
static double GaussianRbfKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
A gaussian RBF kernel.
bool m_isInitialized
Whether this svm has been initialized.
double m_probBParameter
The score offset parameter for mapping to a probability using the logistic function.

Member Function Documentation

double lar_content::SupportVectorMachine::CalculateClassificationScore ( const LArMvaHelper::MvaFeatureVector features) const
inlinevirtual

Calculate the classification score for a set of input features, based on the trained model.

Parameters
featuresthe input features
Returns
the classification score

Implements lar_content::MvaInterface.

Definition at line 295 of file LArSupportVectorMachine.h.

References CalculateClassificationScoreImpl().

296 {
297  return this->CalculateClassificationScoreImpl(features);
298 }
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
double lar_content::SupportVectorMachine::CalculateClassificationScoreImpl ( const LArMvaHelper::MvaFeatureVector features) const
private

Implementation method for calculating the classification score using the trained model.

Parameters
featuresthe vector of features
Returns
the classification score

Definition at line 250 of file LArSupportVectorMachine.cc.

References m_bias, m_featureInfoList, m_isInitialized, m_kernelFunction, m_nFeatures, m_scaleFactor, m_standardizeFeatures, and m_svInfoList.

Referenced by CalculateClassificationScore(), CalculateProbability(), and Classify().

251 {
252  if (!m_isInitialized)
253  {
254  std::cout << "SupportVectorMachine: could not perform classification because the svm was uninitialized" << std::endl;
255  throw StatusCodeException(STATUS_CODE_NOT_INITIALIZED);
256  }
257 
258  if (m_svInfoList.empty())
259  {
260  std::cout << "SupportVectorMachine: could not perform classification because the initialized svm had no support vectors in the model"
261  << std::endl;
262  throw StatusCodeException(STATUS_CODE_NOT_INITIALIZED);
263  }
264 
265  LArMvaHelper::MvaFeatureVector standardizedFeatures;
266  standardizedFeatures.reserve(m_nFeatures);
267 
269  {
270  for (std::size_t i = 0; i < m_nFeatures; ++i)
271  standardizedFeatures.push_back(m_featureInfoList.at(i).StandardizeParameter(features.at(i).Get()));
272  }
273 
274  double classScore(0.);
275  for (const SupportVectorInfo &supportVectorInfo : m_svInfoList)
276  {
277  classScore += supportVectorInfo.m_yAlpha *
278  m_kernelFunction(supportVectorInfo.m_supportVector, (m_standardizeFeatures ? standardizedFeatures : features), m_scaleFactor);
279  }
280 
281  return classScore + m_bias;
282 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:75
double m_scaleFactor
The kernel scale factor.
FeatureInfoVector m_featureInfoList
The list of FeatureInfo objects.
SVInfoList m_svInfoList
The list of SupportVectorInfo objects.
bool m_standardizeFeatures
Whether to standardize the features.
KernelFunction m_kernelFunction
The kernel function.
unsigned int m_nFeatures
The number of features.
bool m_isInitialized
Whether this svm has been initialized.
double lar_content::SupportVectorMachine::CalculateProbability ( const LArMvaHelper::MvaFeatureVector features) const
inlinevirtual

Calculate the classification probability for a set of input features, based on the trained model.

Parameters
featuresthe input features
Returns
the classification probability

Implements lar_content::MvaInterface.

Definition at line 302 of file LArSupportVectorMachine.h.

References CalculateClassificationScoreImpl(), m_enableProbability, m_probAParameter, and m_probBParameter.

303 {
304  if (!m_enableProbability)
305  {
306  std::cout << "LArSupportVectorMachine: cannot calculate probabilities for this SVM" << std::endl;
307  throw pandora::STATUS_CODE_NOT_INITIALIZED;
308  }
309 
310  // Use the logistic function to map the linearly-transformed score on the interval (-inf,inf) to a probability on [0,1] - the two free
311  // parameters in the linear transformation are trained such that the logistic map produces an accurate probability
312  const double scaledScore = m_probAParameter * this->CalculateClassificationScoreImpl(features) + m_probBParameter;
313 
314  return 1. / (1. + std::exp(scaledScore));
315 }
bool m_enableProbability
Whether to enable probability calculations.
double m_probAParameter
The first-order score coefficient for mapping to a probability using the logistic function...
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
double m_probBParameter
The score offset parameter for mapping to a probability using the logistic function.
bool lar_content::SupportVectorMachine::Classify ( const LArMvaHelper::MvaFeatureVector features) const
inlinevirtual

Make a classification for a set of input features, based on the trained model.

Parameters
featuresthe input features
Returns
the predicted boolean class

Implements lar_content::MvaInterface.

Definition at line 288 of file LArSupportVectorMachine.h.

References CalculateClassificationScoreImpl().

289 {
290  return (this->CalculateClassificationScoreImpl(features) > 0.);
291 }
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
double lar_content::SupportVectorMachine::CubicKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

An inhomogeneous cubic kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 373 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

375 {
376  const double denominator(scaleFactor * scaleFactor);
377  if (denominator < std::numeric_limits<double>::epsilon())
378  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
379 
380  double total(0.);
381  for (unsigned int i = 0; i < features.size(); ++i)
382  total += supportVector.at(i).Get() * features.at(i).Get();
383 
384  total = total / denominator + 1.;
385  return total * total * total;
386 }
double lar_content::SupportVectorMachine::GaussianRbfKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

A gaussian RBF kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 390 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

392 {
393  double total(0.);
394  for (unsigned int i = 0; i < features.size(); ++i)
395  total += (supportVector.at(i).Get() - features.at(i).Get()) * (supportVector.at(i).Get() - features.at(i).Get());
396 
397  return std::exp(-scaleFactor * total);
398 }
unsigned int lar_content::SupportVectorMachine::GetNFeatures ( ) const
inline

Get the number of features.

Returns
the number of features

Definition at line 326 of file LArSupportVectorMachine.h.

References m_nFeatures.

327 {
328  return m_nFeatures;
329 }
unsigned int m_nFeatures
The number of features.
StatusCode lar_content::SupportVectorMachine::Initialize ( const std::string &  parameterLocation,
const std::string &  svmName 
)

Initialize the svm using a serialized model.

Parameters
parameterLocationthe location of the model
svmNamethe name of the model
Returns
success

Definition at line 35 of file LArSupportVectorMachine.cc.

References m_featureInfoList, m_isInitialized, m_nFeatures, m_scaleFactor, m_standardizeFeatures, m_svInfoList, and ReadXmlFile().

36 {
37  if (m_isInitialized)
38  {
39  std::cout << "SupportVectorMachine: svm was already initialized" << std::endl;
40  return STATUS_CODE_ALREADY_INITIALIZED;
41  }
42 
43  this->ReadXmlFile(parameterLocation, svmName);
44 
45  // Check the sizes of sigma and scale factor if they are to be used as divisors
47  {
48  for (const FeatureInfo &featureInfo : m_featureInfoList)
49  {
50  if (featureInfo.m_sigmaValue < std::numeric_limits<double>::epsilon())
51  {
52  std::cout << "SupportVectorMachine: could not standardize parameters because sigma value was too small" << std::endl;
53  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
54  }
55  }
56  }
57 
58  // Check the number of features is consistent.
59  m_nFeatures = m_featureInfoList.size();
60 
61  for (const SupportVectorInfo &svInfo : m_svInfoList)
62  {
63  if (svInfo.m_supportVector.size() != m_nFeatures)
64  {
65  std::cout << "SupportVectorMachine: the number of features in the xml file was inconsistent" << std::endl;
66  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
67  }
68  }
69 
70  // There's the possibility of a user-defined kernel that doesn't use this as a divisor but let's be safe
71  if (m_scaleFactor < std::numeric_limits<double>::epsilon())
72  {
73  std::cout << "SupportVectorMachine: could not evaluate kernel because scale factor was too small" << std::endl;
74  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
75  }
76 
77  m_isInitialized = true;
78  return STATUS_CODE_SUCCESS;
79 }
double m_scaleFactor
The kernel scale factor.
FeatureInfoVector m_featureInfoList
The list of FeatureInfo objects.
SVInfoList m_svInfoList
The list of SupportVectorInfo objects.
bool m_standardizeFeatures
Whether to standardize the features.
void ReadXmlFile(const std::string &svmFileName, const std::string &svmName)
Read the svm parameters from an xml file.
unsigned int m_nFeatures
The number of features.
bool m_isInitialized
Whether this svm has been initialized.
bool lar_content::SupportVectorMachine::IsInitialized ( ) const
inline

Query whether this svm is initialized.

Returns
whether the model is initialized

Definition at line 319 of file LArSupportVectorMachine.h.

References m_isInitialized.

320 {
321  return m_isInitialized;
322 }
bool m_isInitialized
Whether this svm has been initialized.
double lar_content::SupportVectorMachine::LinearKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

A linear kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 340 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

342 {
343  const double denominator(scaleFactor * scaleFactor);
344  if (denominator < std::numeric_limits<double>::epsilon())
345  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
346 
347  double total(0.);
348  for (unsigned int i = 0; i < features.size(); ++i)
349  total += supportVector.at(i).Get() * features.at(i).Get();
350 
351  return total / denominator;
352 }
double lar_content::SupportVectorMachine::QuadraticKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

An inhomogeneous quadratic kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 356 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

358 {
359  const double denominator(scaleFactor * scaleFactor);
360  if (denominator < std::numeric_limits<double>::epsilon())
361  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
362 
363  double total(0.);
364  for (unsigned int i = 0; i < features.size(); ++i)
365  total += supportVector.at(i).Get() * features.at(i).Get();
366 
367  total = total / denominator + 1.;
368  return total * total;
369 }
StatusCode lar_content::SupportVectorMachine::ReadComponent ( pandora::TiXmlElement *  pCurrentXmlElement)
private

Read the component at the current xml element.

Parameters
pCurrentXmlElementaddress of the current xml element
Returns
success

Definition at line 143 of file LArSupportVectorMachine.cc.

References ReadFeatures(), ReadMachine(), and ReadSupportVector().

Referenced by ReadXmlFile().

144 {
145  const std::string componentName(pCurrentXmlElement->ValueStr());
146  const TiXmlHandle currentHandle(pCurrentXmlElement);
147 
148  if ((std::string("Name") == componentName) || (std::string("Timestamp") == componentName))
149  return STATUS_CODE_SUCCESS;
150 
151  if (std::string("Machine") == componentName)
152  return this->ReadMachine(currentHandle);
153 
154  if (std::string("Features") == componentName)
155  return this->ReadFeatures(currentHandle);
156 
157  if (std::string("SupportVector") == componentName)
158  return this->ReadSupportVector(currentHandle);
159 
160  return STATUS_CODE_INVALID_PARAMETER;
161 }
pandora::StatusCode ReadSupportVector(const pandora::TiXmlHandle &currentHandle)
Read the support vector component at the current xml handle.
pandora::StatusCode ReadMachine(const pandora::TiXmlHandle &currentHandle)
Read the machine component at the current xml handle.
pandora::StatusCode ReadFeatures(const pandora::TiXmlHandle &currentHandle)
Read the feature component at the current xml handle.
StatusCode lar_content::SupportVectorMachine::ReadFeatures ( const pandora::TiXmlHandle &  currentHandle)
private

Read the feature component at the current xml handle.

Parameters
currentHandlethe current xml handle
Returns
success

Definition at line 204 of file LArSupportVectorMachine.cc.

References m_featureInfoList.

Referenced by ReadComponent().

205 {
206  std::vector<double> muValues;
207  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(currentHandle, "MuValues", muValues));
208 
209  std::vector<double> sigmaValues;
210  PANDORA_RETURN_RESULT_IF_AND_IF(
211  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(currentHandle, "SigmaValues", sigmaValues));
212 
213  if (muValues.size() != sigmaValues.size())
214  {
215  std::cout << "SupportVectorMachine: could not add feature info because the size of mu (" << muValues.size()
216  << ") did not match "
217  "the size of sigma ("
218  << sigmaValues.size() << ")" << std::endl;
219  return STATUS_CODE_INVALID_PARAMETER;
220  }
221 
222  m_featureInfoList.reserve(muValues.size());
223 
224  for (std::size_t i = 0; i < muValues.size(); ++i)
225  m_featureInfoList.emplace_back(muValues.at(i), sigmaValues.at(i));
226 
227  return STATUS_CODE_SUCCESS;
228 }
FeatureInfoVector m_featureInfoList
The list of FeatureInfo objects.
StatusCode lar_content::SupportVectorMachine::ReadMachine ( const pandora::TiXmlHandle &  currentHandle)
private

Read the machine component at the current xml handle.

Parameters
currentHandlethe current xml handle
Returns
success

Definition at line 165 of file LArSupportVectorMachine.cc.

References m_bias, m_enableProbability, m_kernelFunction, m_kernelMap, m_kernelType, m_probAParameter, m_probBParameter, m_scaleFactor, and USER_DEFINED.

Referenced by ReadComponent().

166 {
167  int kernelType(0);
168  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "KernelType", kernelType));
169 
170  double bias(0.);
171  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "Bias", bias));
172 
173  double scaleFactor(0.);
174  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "ScaleFactor", scaleFactor));
175 
176  bool standardize(true);
177  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "Standardize", standardize));
178 
179  bool enableProbability(false);
180  PANDORA_RETURN_RESULT_IF_AND_IF(
181  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "EnableProbability", enableProbability));
182 
183  double probAParameter(0.);
184  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "ProbAParameter", probAParameter));
185 
186  double probBParameter(0.);
187  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "ProbBParameter", probBParameter));
188 
189  m_kernelType = static_cast<KernelType>(kernelType);
190  m_bias = bias;
191  m_scaleFactor = scaleFactor;
192  m_enableProbability = enableProbability;
193  m_probAParameter = probAParameter;
194  m_probBParameter = probBParameter;
195 
196  if (kernelType != USER_DEFINED) // if user-defined, leave it so it alone can be set before/after initialization
198 
199  return STATUS_CODE_SUCCESS;
200 }
bool m_enableProbability
Whether to enable probability calculations.
double m_scaleFactor
The kernel scale factor.
KernelMap m_kernelMap
Map from the kernel types to the kernel functions.
KernelFunction m_kernelFunction
The kernel function.
KernelType m_kernelType
The kernel type.
double m_probAParameter
The first-order score coefficient for mapping to a probability using the logistic function...
double m_probBParameter
The score offset parameter for mapping to a probability using the logistic function.
StatusCode lar_content::SupportVectorMachine::ReadSupportVector ( const pandora::TiXmlHandle &  currentHandle)
private

Read the support vector component at the current xml handle.

Parameters
currentHandlethe current xml handle
Returns
success

Definition at line 232 of file LArSupportVectorMachine.cc.

References m_svInfoList, value, and util::values().

Referenced by ReadComponent().

233 {
234  double yAlpha(0.0);
235  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle, "AlphaY", yAlpha));
236 
237  std::vector<double> values;
238  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(currentHandle, "Values", values));
239 
240  LArMvaHelper::MvaFeatureVector valuesFeatureVector;
241  for (const double &value : values)
242  valuesFeatureVector.emplace_back(value);
243 
244  m_svInfoList.emplace_back(yAlpha, valuesFeatureVector);
245  return STATUS_CODE_SUCCESS;
246 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:75
SVInfoList m_svInfoList
The list of SupportVectorInfo objects.
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
double value
Definition: spectrum.C:18
void lar_content::SupportVectorMachine::ReadXmlFile ( const std::string &  svmFileName,
const std::string &  svmName 
)
private

Read the svm parameters from an xml file.

Parameters
svmFileNamethe sml file name
svmNamethe name of the svm

Definition at line 83 of file LArSupportVectorMachine.cc.

References ReadComponent().

Referenced by Initialize().

84 {
85  TiXmlDocument xmlDocument(svmFileName);
86 
87  if (!xmlDocument.LoadFile())
88  {
89  std::cout << "SupportVectorMachine::Initialize - Invalid xml file." << std::endl;
90  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
91  }
92 
93  const TiXmlHandle xmlDocumentHandle(&xmlDocument);
94  TiXmlNode *pContainerXmlNode(TiXmlHandle(xmlDocumentHandle).FirstChildElement().Element());
95 
96  // Try to find the svm container with the required name
97  while (pContainerXmlNode)
98  {
99  if (pContainerXmlNode->ValueStr() != "SupportVectorMachine")
100  throw StatusCodeException(STATUS_CODE_FAILURE);
101 
102  const TiXmlHandle currentHandle(pContainerXmlNode);
103 
104  std::string currentName;
105  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(currentHandle, "Name", currentName));
106 
107  if (currentName.empty() || (currentName.size() > 1000))
108  {
109  std::cout << "SupportVectorMachine::Initialize - Implausible svm name extracted from xml." << std::endl;
110  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
111  }
112 
113  if (currentName == svmName)
114  break;
115 
116  pContainerXmlNode = pContainerXmlNode->NextSibling();
117  }
118 
119  if (!pContainerXmlNode)
120  {
121  std::cout << "SupportVectorMachine: Could not find an svm by the name " << svmName << std::endl;
122  throw StatusCodeException(STATUS_CODE_NOT_FOUND);
123  }
124 
125  // Read the components of this svm container
126  TiXmlHandle localHandle(pContainerXmlNode);
127  TiXmlElement *pCurrentXmlElement = localHandle.FirstChild().Element();
128 
129  while (pCurrentXmlElement)
130  {
131  if (STATUS_CODE_SUCCESS != this->ReadComponent(pCurrentXmlElement))
132  {
133  std::cout << "SupportVectorMachine: Unknown component in xml file" << std::endl;
134  throw StatusCodeException(STATUS_CODE_FAILURE);
135  }
136 
137  pCurrentXmlElement = pCurrentXmlElement->NextSiblingElement();
138  }
139 }
pandora::StatusCode ReadComponent(pandora::TiXmlElement *pCurrentXmlElement)
Read the component at the current xml element.
void lar_content::SupportVectorMachine::SetKernelFunction ( KernelFunction  kernelFunction)
inline

Set the kernel function to use.

Parameters
kernelFunctionthe kernel function

Definition at line 333 of file LArSupportVectorMachine.h.

References m_kernelFunction.

334 {
335  m_kernelFunction = std::move(kernelFunction);
336 }
KernelFunction m_kernelFunction
The kernel function.

Member Data Documentation

double lar_content::SupportVectorMachine::m_bias
private

The bias term.

Definition at line 174 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), and ReadMachine().

bool lar_content::SupportVectorMachine::m_enableProbability
private

Whether to enable probability calculations.

Definition at line 168 of file LArSupportVectorMachine.h.

Referenced by CalculateProbability(), and ReadMachine().

FeatureInfoVector lar_content::SupportVectorMachine::m_featureInfoList
private

The list of FeatureInfo objects.

Definition at line 178 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), Initialize(), and ReadFeatures().

bool lar_content::SupportVectorMachine::m_isInitialized
private

Whether this svm has been initialized.

Definition at line 166 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), Initialize(), and IsInitialized().

KernelFunction lar_content::SupportVectorMachine::m_kernelFunction
private

The kernel function.

Definition at line 181 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), ReadMachine(), and SetKernelFunction().

KernelMap lar_content::SupportVectorMachine::m_kernelMap
private

Map from the kernel types to the kernel functions.

Definition at line 182 of file LArSupportVectorMachine.h.

Referenced by ReadMachine().

KernelType lar_content::SupportVectorMachine::m_kernelType
private

The kernel type.

Definition at line 180 of file LArSupportVectorMachine.h.

Referenced by ReadMachine().

unsigned int lar_content::SupportVectorMachine::m_nFeatures
private

The number of features.

Definition at line 173 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), GetNFeatures(), and Initialize().

double lar_content::SupportVectorMachine::m_probAParameter
private

The first-order score coefficient for mapping to a probability using the logistic function.

Definition at line 169 of file LArSupportVectorMachine.h.

Referenced by CalculateProbability(), and ReadMachine().

double lar_content::SupportVectorMachine::m_probBParameter
private

The score offset parameter for mapping to a probability using the logistic function.

Definition at line 170 of file LArSupportVectorMachine.h.

Referenced by CalculateProbability(), and ReadMachine().

double lar_content::SupportVectorMachine::m_scaleFactor
private

The kernel scale factor.

Definition at line 175 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), Initialize(), and ReadMachine().

bool lar_content::SupportVectorMachine::m_standardizeFeatures
private

Whether to standardize the features.

Definition at line 172 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), and Initialize().

SVInfoList lar_content::SupportVectorMachine::m_svInfoList
private

The list of SupportVectorInfo objects.

Definition at line 177 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), Initialize(), and ReadSupportVector().


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