Classes | Macros
PointIsolationAlgRandom_test.cc File Reference

Unit test with random data for PointIsolationAlg. More...

#include "larexamples/Algorithms/RemoveIsolatedSpacePoints/PointIsolationAlg.h"
#include "larcorealg/TestUtils/StopWatch.h"
#include "cetlib/pow.h"
#include <cetlib/quiet_unit_test.hpp>
#include <boost/test/test_tools.hpp>
#include <array>
#include <random>
#include <chrono>
#include <ratio>
#include <iostream>
#include <algorithm>

Go to the source code of this file.

Classes

struct  ArgsFixture
 

Macros

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )
 

Functions

template<typename Engine , typename Coord = float>
void PointIsolationTest (Engine &generator, unsigned int nPoints, std::vector< Coord > const &radii)
 
 BOOST_FIXTURE_TEST_CASE (PointIsolationTestCase, ArgsFixture)
 

Detailed Description

Unit test with random data for PointIsolationAlg.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
June 30, 2016
See also
PointIsolationAlg.h

This test populate datasets with random data and tests the isolation algorithm with them.

The test accepts one optional argument:

PointIsolationAlg_test  [seed]

to set the random seed to a particular value.

Definition in file PointIsolationAlgRandom_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )

Definition at line 28 of file PointIsolationAlgRandom_test.cc.

Function Documentation

BOOST_FIXTURE_TEST_CASE ( PointIsolationTestCase  ,
ArgsFixture   
)

Definition at line 156 of file PointIsolationAlgRandom_test.cc.

156  {
157 
158  // we explicitly set the seed, even if with a default value
159  auto seed = std::default_random_engine::default_seed;
160 
161  if (argc > 1) {
162  std::istringstream sstr;
163  sstr.str(argv[1]);
164  sstr >> seed;
165  if (!sstr) {
166  throw std::runtime_error
167  ("Invalid seed specified: " + std::string(argv[1]));
168  }
169  } // if seed specified
170 
171  // this engine can be arbitrarily crappy; don't use it for real physics!
172  std::default_random_engine generator(seed);
173  std::cout << "Random seed: " << seed << std::endl;
174 
175  // try all these isolation radii
176  std::vector<float> const Radii { 0.05, 0.1, 0.5, 2.0 };
177  std::vector<unsigned int> const DataSizes { 100, 10000 };
178 
179  for (unsigned int nPoints: DataSizes)
180  PointIsolationTest(generator, nPoints, Radii);
181 
182 } // PointIsolationTestCase()
std::string string
Definition: nybbler.cc:12
generator
Definition: train.py:468
void PointIsolationTest(Engine &generator, unsigned int nPoints, std::vector< Coord > const &radii)
QTextStream & endl(QTextStream &s)
template<typename Engine , typename Coord = float>
void PointIsolationTest ( Engine &  generator,
unsigned int  nPoints,
std::vector< Coord > const &  radii 
)

Definition at line 57 of file PointIsolationAlgRandom_test.cc.

59  {
60 
61  using Coord_t = Coord;
62 
63  //
64  // create the input sample
65  //
66  std::uniform_real_distribution<Coord_t> uniform(-1., +1.);
67  auto randomCoord = std::bind(uniform, generator);
68 
69  using Point_t = std::array<Coord_t, 3U>;
70  std::vector<Point_t> points;
71 
72  points.reserve(nPoints);
73  for (unsigned int i = 0; i < nPoints; ++i)
74  points.push_back({{ randomCoord(), randomCoord(), randomCoord() }});
75  std::cout
76  << "\n" << std::string(75, '=')
77  << "\nTest with " << nPoints << " points"
78  << "\n" << std::string(72, '-')
79  << std::endl;
80 
81  //
82  // create the algorithm
83  //
84  using PointIsolationAlg_t = lar::example::PointIsolationAlg<Coord_t>;
85 
86  typename PointIsolationAlg_t::Configuration_t config;
87  config.rangeX = { -2., +2. };
88  config.rangeY = { -2., +2. };
89  config.rangeZ = { -2., +2. };
90  config.radius2 = 1.;
91 // config.maxMemory = 100 * 1048576; // we keep the default memory setting
92  PointIsolationAlg_t algo(config);
93 
94  //
95  // for each isolation radius:
96  //
97 
98  // measurement in milliseconds, double precision:
100  for (Coord_t radius: radii) {
101 
102  //
103  // set up the algorithm
104  //
105  config.radius2 = cet::square(radius);
106  algo.reconfigure(config);
107 
108  std::cout << "Isolation radius: " << radius << std::endl;
109 
110  //
111  // run the algorithm with the brute force approach
112  //
113  timer.restart();
114  auto expected
115  = algo.bruteRemoveIsolatedPoints(points.begin(), points.end());
116  auto elapsed = timer.elapsed();
117  std::sort(expected.begin(), expected.end());
118  std::cout << " brute force: " << elapsed << " ms"
119  << std::endl;
120 
121  //
122  // run the algorithm with the default approach
123  //
124  timer.restart();
125  auto actual = algo.removeIsolatedPoints(points);
126  elapsed = timer.elapsed();
127  std::sort(actual.begin(), actual.end());
128  std::cout << " regular: " << elapsed << " ms"
129  << std::endl;
130 
131  //
132  // sort and compare the results
133  //
134  BOOST_TEST(actual == expected, boost::test_tools::per_element());
135 
136  } // for isolation radius
137 
138  std::cout << std::string(72, '-') << std::endl;
139 
140 } // PointIsolationTest()
Algorithm to detect isolated space points.
const char expected[]
Definition: Exception_t.cc:22
std::string string
Definition: nybbler.cc:12
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >> Point_t
ElapsedTime_t elapsed() const
Returns the total time spent running since the last restart.
Definition: StopWatch.h:254
enum geo::coordinates Coord_t
constexpr T square(T x)
Definition: pow.h:21
void restart()
Restarts the watch; previous time is forgotten.
Definition: StopWatch.h:218
CoordStruct Coord
Definition: restypedef.cpp:17
Provides time interval measurements.
Definition: StopWatch.h:79
static Config * config
Definition: config.cpp:1054
generator
Definition: train.py:468
QTextStream & endl(QTextStream &s)