Public Types | Static Public Member Functions | Static Private Member Functions | List of all members
lar_content::LArMvaHelper Class Reference

LArMvaHelper class. More...

#include <LArMvaHelper.h>

Public Types

typedef MvaTypes::MvaFeature MvaFeature
 
typedef MvaTypes::MvaFeatureVector MvaFeatureVector
 

Static Public Member Functions

template<typename... TLISTS>
static pandora::StatusCode ProduceTrainingExample (const std::string &trainingOutputFile, const bool result, TLISTS &&...featureLists)
 Produce a training example with the given features and result. More...
 
template<typename... TLISTS>
static bool Classify (const MvaInterface &classifier, TLISTS &&...featureLists)
 Use the trained classifier to predict the boolean class of an example. More...
 
template<typename... TLISTS>
static double CalculateClassificationScore (const MvaInterface &classifier, TLISTS &&...featureLists)
 Use the trained classifer to calculate the classification score of an example (>0 means boolean class true) More...
 
template<typename... TLISTS>
static double CalculateProbability (const MvaInterface &classifier, TLISTS &&...featureLists)
 Use the trained mva to calculate a classification probability for an example. More...
 
template<typename... Ts, typename... TARGS>
static MvaFeatureVector CalculateFeatures (const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
 Calculate the features in a given feature tool vector. More...
 
template<typename T , typename... Ts, typename... TARGS>
static MvaFeatureVector CalculateFeaturesOfType (const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
 Calculate the features of a given derived feature tool type in a feature tool vector. More...
 
template<typename... Ts>
static pandora::StatusCode AddFeatureToolToVector (pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts... > &featureToolVector)
 Add a feature tool to a vector of feature tools. More...
 

Static Private Member Functions

static std::string GetTimestampString ()
 Get a timestamp string for this point in time. More...
 
template<typename TLIST , typename... TLISTS>
static pandora::StatusCode WriteFeaturesToFile (std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList, TLISTS &&...featureLists)
 Recursively write the features of the given lists to file. More...
 
static pandora::StatusCode WriteFeaturesToFile (std::ofstream &, const std::string &)
 Recursively write the features of the given lists to file (terminating method) More...
 
template<typename TLIST >
static pandora::StatusCode WriteFeaturesToFileImpl (std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList)
 Write the features of the given list to file (implementation method) More...
 
template<typename TLIST , typename... TLISTS>
static MvaFeatureVector ConcatenateFeatureLists (TLIST &&featureList, TLISTS &&...featureLists)
 Recursively concatenate vectors of features. More...
 
static MvaFeatureVector ConcatenateFeatureLists ()
 Recursively concatenate vectors of features (terminating method) More...
 

Detailed Description

LArMvaHelper class.

Definition at line 54 of file LArMvaHelper.h.

Member Typedef Documentation

Definition at line 57 of file LArMvaHelper.h.

Definition at line 58 of file LArMvaHelper.h.

Member Function Documentation

template<typename... Ts>
pandora::StatusCode lar_content::LArMvaHelper::AddFeatureToolToVector ( pandora::AlgorithmTool *const  pFeatureTool,
MvaFeatureToolVector< Ts... > &  featureToolVector 
)
static

Add a feature tool to a vector of feature tools.

Parameters
pFeatureToolthe feature tool
featureToolVectorthe vector to append
Returns
success

Definition at line 274 of file LArMvaHelper.h.

275 {
276  if (MvaFeatureTool<Ts...> *const pCastFeatureTool = dynamic_cast<MvaFeatureTool<Ts...> *const>(pFeatureTool))
277  {
278  featureToolVector.push_back(pCastFeatureTool);
279  return pandora::STATUS_CODE_SUCCESS;
280  }
281 
282  return pandora::STATUS_CODE_FAILURE;
283 }
template<typename... TLISTS>
double lar_content::LArMvaHelper::CalculateClassificationScore ( const MvaInterface classifier,
TLISTS &&...  featureLists 
)
static

Use the trained classifer to calculate the classification score of an example (>0 means boolean class true)

Parameters
classifierthe classifier
featureListsthe lists of features
Returns
the classification score

Definition at line 228 of file LArMvaHelper.h.

229 {
230  return classifier.CalculateClassificationScore(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
231 }
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:357
template<typename... Ts, typename... TARGS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::CalculateFeatures ( const MvaFeatureToolVector< Ts... > &  featureToolVector,
TARGS &&...  args 
)
static

Calculate the features in a given feature tool vector.

Parameters
featureToolVectorthe feature tool vector
argsarguments to pass to the tool
Returns
the vector of features

Definition at line 244 of file LArMvaHelper.h.

245 {
246  LArMvaHelper::MvaFeatureVector featureVector;
247 
248  for (MvaFeatureTool<Ts...> *const pFeatureTool : featureToolVector)
249  pFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
250 
251  return featureVector;
252 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
static QCString args
Definition: declinfo.cpp:674
template<typename T , typename... Ts, typename... TARGS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::CalculateFeaturesOfType ( const MvaFeatureToolVector< Ts... > &  featureToolVector,
TARGS &&...  args 
)
static

Calculate the features of a given derived feature tool type in a feature tool vector.

Parameters
featureToolVectorthe feature tool vector
argsarguments to pass to the tool
Returns
the vector of features

Definition at line 257 of file LArMvaHelper.h.

258 {
259  using TD = typename std::decay<T>::type;
260  LArMvaHelper::MvaFeatureVector featureVector;
261 
262  for (MvaFeatureTool<Ts...> *const pFeatureTool : featureToolVector)
263  {
264  if (TD *const pCastFeatureTool = dynamic_cast<TD *const>(pFeatureTool))
265  pCastFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
266  }
267 
268  return featureVector;
269 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
static QCString args
Definition: declinfo.cpp:674
static QCString type
Definition: declinfo.cpp:672
template<typename... TLISTS>
double lar_content::LArMvaHelper::CalculateProbability ( const MvaInterface classifier,
TLISTS &&...  featureLists 
)
static

Use the trained mva to calculate a classification probability for an example.

Parameters
classifierthe classifier
featureListsthe lists of features
Returns
the classification probability

Definition at line 236 of file LArMvaHelper.h.

237 {
238  return classifier.CalculateProbability(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
239 }
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:357
template<typename... TLISTS>
bool lar_content::LArMvaHelper::Classify ( const MvaInterface classifier,
TLISTS &&...  featureLists 
)
static

Use the trained classifier to predict the boolean class of an example.

Parameters
classifierthe classifier
featureListsthe lists of features
Returns
the predicted boolean class of the example

Definition at line 220 of file LArMvaHelper.h.

221 {
222  return classifier.Classify(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
223 }
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:357
template<typename TLIST , typename... TLISTS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::ConcatenateFeatureLists ( TLIST &&  featureList,
TLISTS &&...  featureLists 
)
staticprivate

Recursively concatenate vectors of features.

Parameters
featureLista list of features
featureListsoptional further lists of features
Returns
the concatenated vector of features

Definition at line 339 of file LArMvaHelper.h.

340 {
341  static_assert(std::is_same<typename std::decay<TLIST>::type, LArMvaHelper::MvaFeatureVector>::value,
342  "LArMvaHelper: Could not concatenate feature lists because one or more lists was not a vector of MvaFeatures");
343 
344  LArMvaHelper::MvaFeatureVector featureVector;
345 
346  for (const MvaFeature &feature : featureList)
347  featureVector.push_back(feature);
348 
349  LArMvaHelper::MvaFeatureVector newFeatureVector = ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...);
350  featureVector.insert(featureVector.end(), newFeatureVector.begin(), newFeatureVector.end());
351 
352  return featureVector;
353 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
MvaTypes::MvaFeature MvaFeature
Definition: LArMvaHelper.h:57
static QCString type
Definition: declinfo.cpp:672
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:357
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::ConcatenateFeatureLists ( )
inlinestaticprivate

Recursively concatenate vectors of features (terminating method)

Definition at line 357 of file LArMvaHelper.h.

358 {
360 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
std::string lar_content::LArMvaHelper::GetTimestampString ( )
inlinestaticprivate

Get a timestamp string for this point in time.

Returns
a timestamp string

Definition at line 287 of file LArMvaHelper.h.

288 {
289  std::time_t timestampNow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
290 
291  struct tm *pTimeInfo(NULL);
292  char buffer[80];
293 
294  pTimeInfo = localtime(&timestampNow);
295  strftime(buffer, 80, "%x_%X", pTimeInfo);
296 
297  std::string timeString(buffer);
298 
299  if (!timeString.empty() && timeString.back() == '\n') // last char is always a newline
300  timeString.pop_back();
301 
302  return timeString;
303 }
std::string string
Definition: nybbler.cc:12
tm
Definition: demo.py:21
template<typename... TLISTS>
pandora::StatusCode lar_content::LArMvaHelper::ProduceTrainingExample ( const std::string trainingOutputFile,
const bool  result,
TLISTS &&...  featureLists 
)
static

Produce a training example with the given features and result.

Parameters
trainingOutputFilethe file to which to append the example
featureListsthe lists of features
Returns
success

Definition at line 197 of file LArMvaHelper.h.

198 {
199  std::ofstream outfile;
200  outfile.open(trainingOutputFile, std::ios_base::app); // always append to the output file
201 
202  if (!outfile.is_open())
203  {
204  std::cout << "LArMvaHelper: could not open file for training examples at " << trainingOutputFile << std::endl;
205  return pandora::STATUS_CODE_FAILURE;
206  }
207 
208  std::string delimiter(",");
209  outfile << GetTimestampString() << delimiter;
210 
211  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFile(outfile, delimiter, featureLists...));
212  outfile << static_cast<int>(result) << '\n';
213 
214  return pandora::STATUS_CODE_SUCCESS;
215 }
static QCString result
std::string string
Definition: nybbler.cc:12
static std::string GetTimestampString()
Get a timestamp string for this point in time.
Definition: LArMvaHelper.h:287
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList, TLISTS &&...featureLists)
Recursively write the features of the given lists to file.
Definition: LArMvaHelper.h:308
QTextStream & endl(QTextStream &s)
template<typename TLIST , typename... TLISTS>
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFile ( std::ofstream &  outfile,
const std::string delimiter,
TLIST &&  featureList,
TLISTS &&...  featureLists 
)
inlinestaticprivate

Recursively write the features of the given lists to file.

Parameters
outfilethe std::ofstream object to use
delimiterthe delimiter string
featureLista list of features to write
featureListsoptional further lists of features to write
Returns
success

Definition at line 308 of file LArMvaHelper.h.

310 {
311  static_assert(std::is_same<typename std::decay<TLIST>::type, LArMvaHelper::MvaFeatureVector>::value,
312  "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
313 
314  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFileImpl(outfile, delimiter, featureList));
315  return WriteFeaturesToFile(outfile, delimiter, featureLists...);
316 }
static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList)
Write the features of the given list to file (implementation method)
Definition: LArMvaHelper.h:328
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList, TLISTS &&...featureLists)
Recursively write the features of the given lists to file.
Definition: LArMvaHelper.h:308
static QCString type
Definition: declinfo.cpp:672
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFile ( std::ofstream &  ,
const std::string  
)
inlinestaticprivate

Recursively write the features of the given lists to file (terminating method)

Returns
success

Definition at line 320 of file LArMvaHelper.h.

321 {
322  return pandora::STATUS_CODE_SUCCESS;
323 }
template<typename TLIST >
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFileImpl ( std::ofstream &  outfile,
const std::string delimiter,
TLIST &&  featureList 
)
staticprivate

Write the features of the given list to file (implementation method)

Parameters
outfilethe std::ofstream object to use
delimiterthe delimiter string
featureLista list of features to write
Returns
success

Definition at line 328 of file LArMvaHelper.h.

329 {
330  for (const MvaFeature &feature : featureList)
331  outfile << feature.Get() << delimiter;
332 
333  return pandora::STATUS_CODE_SUCCESS;
334 }
MvaTypes::MvaFeature MvaFeature
Definition: LArMvaHelper.h:57

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