Skip to content

Commit ff9432f

Browse files
yuvalifericniebler
authored andcommitted
Allow accumulator persistency (#18)
* Add, and update, documentation build targets. * adding serialization to stats * serialize rolling stats
1 parent 1b1995f commit ff9432f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1523
-37
lines changed

example/Jamfile.v2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ exe example
88
:
99
<include>../../..
1010
<include>$(BOOST_ROOT)
11+
<library>/boost/serialization
12+
<cxxflags>"-Wno-deprecated-declarations"
1113
;

example/main.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77

88
#include <iostream>
9+
#include <fstream> // needed for de/serialization example (4)
910
#include <algorithm>
1011
#include <boost/ref.hpp>
1112
#include <boost/bind.hpp>
1213
#include <boost/array.hpp>
1314
#include <boost/foreach.hpp>
1415
#include <boost/accumulators/accumulators.hpp>
1516
#include <boost/accumulators/statistics.hpp>
17+
// needed for de/serialization example (4)
18+
#include <boost/archive/text_oarchive.hpp>
19+
#include <boost/archive/text_iarchive.hpp>
1620

1721
using namespace boost;
1822
using namespace boost::accumulators;
@@ -159,6 +163,85 @@ void example3()
159163
}
160164
}
161165

166+
///////////////////////////////////////////////////////////////////////////////
167+
// example4
168+
//
169+
// Show how accumulators could be persisted into a file, and then continued
170+
// from where they were left of
171+
//
172+
void example4()
173+
{
174+
accumulator_set<
175+
double
176+
, stats<tag::min, tag::mean(immediate), tag::sum, tag::moment<2>, tag::p_square_quantile, tag::kurtosis >
177+
> acc(quantile_probability = 0.9);
178+
179+
{
180+
// accumulate values from array
181+
boost::array<double, 10> data = {-10., -8., -7., -6., -5., -4., -3., -2., -1., 0.};
182+
183+
acc = std::for_each(data.begin(), data.end(), acc);
184+
}
185+
186+
std::cout << " min = " << (min)(acc) << std::endl;
187+
std::cout << " mean = " << mean(acc) << std::endl;
188+
std::cout << " count = " << count(acc) << std::endl;
189+
std::cout << " sum = " << sum(acc) << std::endl;
190+
std::cout << " moment<2> = " << accumulators::moment<2>(acc) << std::endl;
191+
std::cout << " p_square_quantile = " << accumulators::p_square_quantile(acc) << std::endl;
192+
std::cout << " kurtosis = " << accumulators::kurtosis(acc) << std::endl;
193+
194+
// save accumulator list into a file called "saved-stats"
195+
const unsigned ACC_VER = 0;
196+
const char* file_name = "saved-stats";
197+
{
198+
std::ofstream ofs(file_name);
199+
boost::archive::text_oarchive oa(ofs);
200+
acc.serialize(oa, ACC_VER);
201+
}
202+
203+
// create a new accumulator set and initialize from data saved into the file
204+
accumulator_set<
205+
double
206+
, stats<tag::min, tag::mean(immediate), tag::sum, tag::moment<2>, tag::p_square_quantile, tag::kurtosis >
207+
> restored_acc(quantile_probability = 0.9);
208+
209+
{
210+
std::ifstream ifs(file_name);
211+
boost::archive::text_iarchive ia(ifs);
212+
restored_acc.serialize(ia, ACC_VER);
213+
}
214+
215+
// continue accumulating into both sets
216+
{
217+
// accumulate values from array
218+
boost::array<double, 10> data = {10., 8., 7., 6., 5., 4., 3., 2., 1., 0.};
219+
220+
acc = std::for_each(data.begin(), data.end(), acc);
221+
restored_acc = std::for_each(data.begin(), data.end(), restored_acc);
222+
}
223+
224+
// validate that both return th same values
225+
std::cout << std::endl << "Values in original set:" << std::endl;
226+
std::cout << " min""(acc) = " << (min)(acc) << std::endl;
227+
std::cout << " mean(acc) = " << mean(acc) << std::endl;
228+
std::cout << " count(acc) = " << count(acc) << std::endl;
229+
std::cout << " sum(acc) = " << sum(acc) << std::endl;
230+
std::cout << " moment<2>(acc) = " << accumulators::moment<2>(acc) << std::endl;
231+
std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile(acc) << std::endl;
232+
std::cout << " kurtosis(acc) = " << accumulators::kurtosis(acc) << std::endl;
233+
234+
std::cout << std::endl << "Values in restored set:" << std::endl;
235+
std::cout << " min""(acc) = " << (min)(restored_acc) << std::endl;
236+
std::cout << " mean(acc) = " << mean(restored_acc) << std::endl;
237+
std::cout << " count(acc) = " << count(restored_acc) << std::endl;
238+
std::cout << " sum(acc) = " << sum(restored_acc) << std::endl;
239+
std::cout << " moment<2>(acc) = " << accumulators::moment<2>(restored_acc) << std::endl;
240+
std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile(restored_acc) << std::endl;
241+
std::cout << " kurtosis(acc) = " << accumulators::kurtosis(restored_acc) << std::endl;
242+
243+
}
244+
162245
///////////////////////////////////////////////////////////////////////////////
163246
// main
164247
int main()
@@ -172,5 +255,8 @@ int main()
172255
std::cout << "\nExample 3:\n";
173256
example3();
174257

258+
std::cout << "\nExample 4:\n";
259+
example4();
260+
175261
return 0;
176262
}

include/boost/accumulators/framework/accumulator_set.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ namespace detail
9494
{
9595
};
9696

97+
// function object that serialize an accumulator
98+
template<typename Archive>
99+
struct serialize_accumulator
100+
{
101+
serialize_accumulator(Archive & _ar, const unsigned int _file_version) :
102+
ar(_ar), file_version(_file_version)
103+
{}
104+
105+
template<typename Accumulator>
106+
void operator ()(Accumulator &accumulator)
107+
{
108+
accumulator.serialize(ar, file_version);
109+
}
110+
111+
private:
112+
Archive& ar;
113+
const unsigned int file_version;
114+
};
115+
97116
} // namespace detail
98117

99118
#ifdef _MSC_VER
@@ -420,6 +439,14 @@ struct accumulator_set
420439
);
421440
}
422441

442+
// make the accumulator set serializeable
443+
template<class Archive>
444+
void serialize(Archive & ar, const unsigned int file_version)
445+
{
446+
detail::serialize_accumulator<Archive> serializer(ar, file_version);
447+
fusion::for_each(this->accumulators, serializer);
448+
}
449+
423450
private:
424451

425452
accumulators_type accumulators;

include/boost/accumulators/statistics/count.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ namespace impl
4343
return this->cnt;
4444
}
4545

46+
// make this accumulator serializeable
47+
template<class Archive>
48+
void serialize(Archive & ar, const unsigned int file_version)
49+
{
50+
ar & cnt;
51+
}
52+
4653
private:
4754
std::size_t cnt;
4855
};

include/boost/accumulators/statistics/covariance.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ namespace impl
152152
return this->cov_;
153153
}
154154

155+
// make this accumulator serializeable
156+
template<class Archive>
157+
void serialize(Archive & ar, const unsigned int file_version)
158+
{
159+
ar & cov_;
160+
}
161+
155162
private:
156163
result_type cov_;
157164
};

include/boost/accumulators/statistics/density.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <boost/accumulators/statistics/count.hpp>
2626
#include <boost/accumulators/statistics/max.hpp>
2727
#include <boost/accumulators/statistics/min.hpp>
28+
#include <boost/serialization/vector.hpp>
29+
#include <boost/serialization/utility.hpp>
2830

2931
namespace boost { namespace accumulators
3032
{
@@ -184,6 +186,20 @@ namespace impl
184186
return make_iterator_range(this->histogram);
185187
}
186188

189+
// make this accumulator serializeable
190+
// TODO split to save/load and check on parameters provided in ctor
191+
template<class Archive>
192+
void serialize(Archive & ar, const unsigned int file_version)
193+
{
194+
ar & cache_size;
195+
ar & cache;
196+
ar & num_bins;
197+
ar & samples_in_bin;
198+
ar & bin_positions;
199+
ar & histogram;
200+
ar & is_dirty;
201+
}
202+
187203
private:
188204
std::size_t cache_size; // number of cached samples
189205
array_type cache; // cache to store the first cache_size samples

include/boost/accumulators/statistics/extended_p_square.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <boost/accumulators/statistics_fwd.hpp>
2727
#include <boost/accumulators/statistics/count.hpp>
2828
#include <boost/accumulators/statistics/times2_iterator.hpp>
29+
#include <boost/serialization/vector.hpp>
2930

3031
namespace boost { namespace accumulators
3132
{
@@ -236,6 +237,19 @@ namespace impl
236237
);
237238
}
238239

240+
public:
241+
// make this accumulator serializeable
242+
// TODO: do we need to split to load/save and verify that the parameters did not change?
243+
template<class Archive>
244+
void serialize(Archive & ar, const unsigned int file_version)
245+
{
246+
ar & probabilities;
247+
ar & heights;
248+
ar & actual_positions;
249+
ar & desired_positions;
250+
ar & positions_increments;
251+
}
252+
239253
private:
240254
array_type probabilities; // the quantile probabilities
241255
array_type heights; // q_i

include/boost/accumulators/statistics/extended_p_square_quantile.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ namespace impl
185185
}
186186

187187
}
188+
189+
public:
190+
// make this accumulator serializeable
191+
// TODO: do we need to split to load/save and verify that the parameters did not change?
192+
template<class Archive>
193+
void serialize(Archive & ar, const unsigned int file_version)
194+
{
195+
ar & probabilities;
196+
ar & probability;
197+
}
198+
188199
private:
189200

190201
array_type probabilities;

include/boost/accumulators/statistics/kurtosis.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ namespace impl
6363
* ( accumulators::moment<2>(args) - mean(args) * mean(args) )
6464
) - 3.;
6565
}
66+
67+
// serialization is done by accumulators it depends on
68+
template<class Archive>
69+
void serialize(Archive & ar, const unsigned int file_version) {}
6670
};
6771

6872
} // namespace impl

include/boost/accumulators/statistics/max.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ namespace impl
4848
return this->max_;
4949
}
5050

51+
// make this accumulator serializeable
52+
template<class Archive>
53+
void serialize(Archive & ar, const unsigned int file_version)
54+
{
55+
ar & max_;
56+
}
57+
5158
private:
5259
Sample max_;
5360
};

0 commit comments

Comments
 (0)