LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 29 of file LArSupportVectorMachine.h.

Member Typedef Documentation

Definition at line 161 of file LArSupportVectorMachine.h.

Definition at line 32 of file LArSupportVectorMachine.h.

Definition at line 160 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 290 of file LArSupportVectorMachine.h.

References CalculateClassificationScoreImpl().

291 {
292  return this->CalculateClassificationScoreImpl(features);
293 }
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 257 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().

258 {
259  if (!m_isInitialized)
260  {
261  std::cout << "SupportVectorMachine: could not perform classification because the svm was uninitialized" << std::endl;
262  throw StatusCodeException(STATUS_CODE_NOT_INITIALIZED);
263  }
264 
265  if (m_svInfoList.empty())
266  {
267  std::cout << "SupportVectorMachine: could not perform classification because the initialized svm had no support vectors in the model" << std::endl;
268  throw StatusCodeException(STATUS_CODE_NOT_INITIALIZED);
269  }
270 
271  LArMvaHelper::MvaFeatureVector standardizedFeatures;
272  standardizedFeatures.reserve(m_nFeatures);
273 
275  {
276  for (std::size_t i = 0; i < m_nFeatures; ++i)
277  standardizedFeatures.push_back(m_featureInfoList.at(i).StandardizeParameter(features.at(i).Get()));
278  }
279 
280  double classScore(0.);
281  for (const SupportVectorInfo &supportVectorInfo : m_svInfoList)
282  {
283  classScore += supportVectorInfo.m_yAlpha *
284  m_kernelFunction(supportVectorInfo.m_supportVector, (m_standardizeFeatures ? standardizedFeatures : features), m_scaleFactor);
285  }
286 
287  return classScore + m_bias;
288 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
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 297 of file LArSupportVectorMachine.h.

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

298 {
299  if (!m_enableProbability)
300  {
301  std::cout << "LArSupportVectorMachine: cannot calculate probabilities for this SVM" << std::endl;
302  throw pandora::STATUS_CODE_NOT_INITIALIZED;
303  }
304 
305  // Use the logistic function to map the linearly-transformed score on the interval (-inf,inf) to a probability on [0,1] - the two free
306  // parameters in the linear transformation are trained such that the logistic map produces an accurate probability
307  const double scaledScore = m_probAParameter * this->CalculateClassificationScoreImpl(features) + m_probBParameter;
308 
309  return 1. / (1. + std::exp(scaledScore));
310 }
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 283 of file LArSupportVectorMachine.h.

References CalculateClassificationScoreImpl().

284 {
285  return (this->CalculateClassificationScoreImpl(features) > 0.);
286 }
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 366 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

367 {
368  const double denominator(scaleFactor * scaleFactor);
369  if (denominator < std::numeric_limits<double>::epsilon())
370  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
371 
372  double total(0.);
373  for (unsigned int i = 0; i < features.size(); ++i)
374  total += supportVector.at(i).Get() * features.at(i).Get();
375 
376  total = total / denominator + 1.;
377  return total * total * total;
378 }
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 382 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

383 {
384  double total(0.);
385  for (unsigned int i = 0; i < features.size(); ++i)
386  total += (supportVector.at(i).Get() - features.at(i).Get()) * (supportVector.at(i).Get() - features.at(i).Get());
387 
388  return std::exp(-scaleFactor * total);
389 }
unsigned int lar_content::SupportVectorMachine::GetNFeatures ( ) const
inline

Get the number of features.

Returns
the number of features

Definition at line 321 of file LArSupportVectorMachine.h.

References m_nFeatures.

322 {
323  return m_nFeatures;
324 }
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().

Referenced by lar_content::SvmPfoCharacterisationAlgorithm::ReadSettings(), lar_content::SvmVertexSelectionAlgorithm::ReadSettings(), and lar_content::NeutrinoIdTool::ReadSettings().

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 314 of file LArSupportVectorMachine.h.

References m_isInitialized.

315 {
316  return m_isInitialized;
317 }
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 335 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

336 {
337  const double denominator(scaleFactor * scaleFactor);
338  if (denominator < std::numeric_limits<double>::epsilon())
339  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
340 
341  double total(0.);
342  for (unsigned int i = 0; i < features.size(); ++i)
343  total += supportVector.at(i).Get() * features.at(i).Get();
344 
345  return total / denominator;
346 }
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 350 of file LArSupportVectorMachine.h.

Referenced by SupportVectorMachine().

351 {
352  const double denominator(scaleFactor * scaleFactor);
353  if (denominator < std::numeric_limits<double>::epsilon())
354  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
355 
356  double total(0.);
357  for (unsigned int i = 0; i < features.size(); ++i)
358  total += supportVector.at(i).Get() * features.at(i).Get();
359 
360  total = total / denominator + 1.;
361  return total * total;
362 }
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 210 of file LArSupportVectorMachine.cc.

References m_featureInfoList.

Referenced by ReadComponent().

211 {
212  std::vector<double> muValues;
213  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(currentHandle,
214  "MuValues", muValues));
215 
216  std::vector<double> sigmaValues;
217  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(currentHandle,
218  "SigmaValues", sigmaValues));
219 
220  if (muValues.size() != sigmaValues.size())
221  {
222  std::cout << "SupportVectorMachine: could not add feature info because the size of mu (" << muValues.size() << ") did not match "
223  "the size of sigma (" << sigmaValues.size() << ")" << std::endl;
224  return STATUS_CODE_INVALID_PARAMETER;
225  }
226 
227  m_featureInfoList.reserve(muValues.size());
228 
229  for (std::size_t i = 0; i < muValues.size(); ++i)
230  m_featureInfoList.emplace_back(muValues.at(i), sigmaValues.at(i));
231 
232  return STATUS_CODE_SUCCESS;
233 }
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,
169  "KernelType", kernelType));
170 
171  double bias(0.);
172  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
173  "Bias", bias));
174 
175  double scaleFactor(0.);
176  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
177  "ScaleFactor", scaleFactor));
178 
179  bool standardize(true);
180  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
181  "Standardize", standardize));
182 
183  bool enableProbability(false);
184  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
185  "EnableProbability", enableProbability));
186 
187  double probAParameter(0.);
188  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
189  "ProbAParameter", probAParameter));
190 
191  double probBParameter(0.);
192  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
193  "ProbBParameter", probBParameter));
194 
195  m_kernelType = static_cast<KernelType>(kernelType);
196  m_bias = bias;
197  m_scaleFactor = scaleFactor;
198  m_enableProbability = enableProbability;
199  m_probAParameter = probAParameter;
200  m_probBParameter = probBParameter;
201 
202  if (kernelType != USER_DEFINED) // if user-defined, leave it so it alone can be set before/after initialization
204 
205  return STATUS_CODE_SUCCESS;
206 }
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 237 of file LArSupportVectorMachine.cc.

References m_svInfoList, and fhicl::detail::atom::value().

Referenced by ReadComponent().

238 {
239  double yAlpha(0.0);
240  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(currentHandle,
241  "AlphaY", yAlpha));
242 
243  std::vector<double> values;
244  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(currentHandle,
245  "Values", values));
246 
247  LArMvaHelper::MvaFeatureVector valuesFeatureVector;
248  for (const double &value : values)
249  valuesFeatureVector.emplace_back(value);
250 
251  m_svInfoList.emplace_back(yAlpha, valuesFeatureVector);
252  return STATUS_CODE_SUCCESS;
253 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
SVInfoList m_svInfoList
The list of SupportVectorInfo objects.
std::string value(boost::any const &)
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 328 of file LArSupportVectorMachine.h.

References m_kernelFunction.

329 {
330  m_kernelFunction = std::move(kernelFunction);
331 }
KernelFunction m_kernelFunction
The kernel function.

Member Data Documentation

double lar_content::SupportVectorMachine::m_bias
private

The bias term.

Definition at line 173 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), and ReadMachine().

bool lar_content::SupportVectorMachine::m_enableProbability
private

Whether to enable probability calculations.

Definition at line 167 of file LArSupportVectorMachine.h.

Referenced by CalculateProbability(), and ReadMachine().

FeatureInfoVector lar_content::SupportVectorMachine::m_featureInfoList
private

The list of FeatureInfo objects.

Definition at line 177 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 165 of file LArSupportVectorMachine.h.

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

KernelFunction lar_content::SupportVectorMachine::m_kernelFunction
private

The kernel function.

Definition at line 180 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 181 of file LArSupportVectorMachine.h.

Referenced by ReadMachine().

KernelType lar_content::SupportVectorMachine::m_kernelType
private

The kernel type.

Definition at line 179 of file LArSupportVectorMachine.h.

Referenced by ReadMachine().

unsigned int lar_content::SupportVectorMachine::m_nFeatures
private

The number of features.

Definition at line 172 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 168 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 169 of file LArSupportVectorMachine.h.

Referenced by CalculateProbability(), and ReadMachine().

double lar_content::SupportVectorMachine::m_scaleFactor
private

The kernel scale factor.

Definition at line 174 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 171 of file LArSupportVectorMachine.h.

Referenced by CalculateClassificationScoreImpl(), and Initialize().

SVInfoList lar_content::SupportVectorMachine::m_svInfoList
private

The list of SupportVectorInfo objects.

Definition at line 176 of file LArSupportVectorMachine.h.

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


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