Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ exe example
:
<include>../../..
<include>$(BOOST_ROOT)
<library>/boost/serialization
<cxxflags>"-Wno-deprecated-declarations"
;
86 changes: 86 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>
#include <fstream> // needed for de/serialization example (4)
#include <algorithm>
#include <boost/ref.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <boost/foreach.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
// needed for de/serialization example (4)
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

using namespace boost;
using namespace boost::accumulators;
Expand Down Expand Up @@ -159,6 +163,85 @@ void example3()
}
}

///////////////////////////////////////////////////////////////////////////////
// example4
//
// Show how accumulators could be persisted into a file, and then continued
// from where they were left of
//
void example4()
{
accumulator_set<
double
, stats<tag::min, tag::mean(immediate), tag::sum, tag::moment<2>, tag::p_square_quantile, tag::kurtosis >
> acc(quantile_probability = 0.9);

{
// accumulate values from array
boost::array<double, 10> data = {-10., -8., -7., -6., -5., -4., -3., -2., -1., 0.};

acc = std::for_each(data.begin(), data.end(), acc);
}

std::cout << " min = " << (min)(acc) << std::endl;
std::cout << " mean = " << mean(acc) << std::endl;
std::cout << " count = " << count(acc) << std::endl;
std::cout << " sum = " << sum(acc) << std::endl;
std::cout << " moment<2> = " << accumulators::moment<2>(acc) << std::endl;
std::cout << " p_square_quantile = " << accumulators::p_square_quantile(acc) << std::endl;
std::cout << " kurtosis = " << accumulators::kurtosis(acc) << std::endl;

// save accumulator list into a file called "saved-stats"
const unsigned ACC_VER = 0;
const char* file_name = "saved-stats";
{
std::ofstream ofs(file_name);
boost::archive::text_oarchive oa(ofs);
acc.serialize(oa, ACC_VER);
}

// create a new accumulator set and initialize from data saved into the file
accumulator_set<
double
, stats<tag::min, tag::mean(immediate), tag::sum, tag::moment<2>, tag::p_square_quantile, tag::kurtosis >
> restored_acc(quantile_probability = 0.9);

{
std::ifstream ifs(file_name);
boost::archive::text_iarchive ia(ifs);
restored_acc.serialize(ia, ACC_VER);
}

// continue accumulating into both sets
{
// accumulate values from array
boost::array<double, 10> data = {10., 8., 7., 6., 5., 4., 3., 2., 1., 0.};

acc = std::for_each(data.begin(), data.end(), acc);
restored_acc = std::for_each(data.begin(), data.end(), restored_acc);
}

// validate that both return th same values
std::cout << std::endl << "Values in original set:" << std::endl;
std::cout << " min""(acc) = " << (min)(acc) << std::endl;
std::cout << " mean(acc) = " << mean(acc) << std::endl;
std::cout << " count(acc) = " << count(acc) << std::endl;
std::cout << " sum(acc) = " << sum(acc) << std::endl;
std::cout << " moment<2>(acc) = " << accumulators::moment<2>(acc) << std::endl;
std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile(acc) << std::endl;
std::cout << " kurtosis(acc) = " << accumulators::kurtosis(acc) << std::endl;

std::cout << std::endl << "Values in restored set:" << std::endl;
std::cout << " min""(acc) = " << (min)(restored_acc) << std::endl;
std::cout << " mean(acc) = " << mean(restored_acc) << std::endl;
std::cout << " count(acc) = " << count(restored_acc) << std::endl;
std::cout << " sum(acc) = " << sum(restored_acc) << std::endl;
std::cout << " moment<2>(acc) = " << accumulators::moment<2>(restored_acc) << std::endl;
std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile(restored_acc) << std::endl;
std::cout << " kurtosis(acc) = " << accumulators::kurtosis(restored_acc) << std::endl;

}

///////////////////////////////////////////////////////////////////////////////
// main
int main()
Expand All @@ -172,5 +255,8 @@ int main()
std::cout << "\nExample 3:\n";
example3();

std::cout << "\nExample 4:\n";
example4();

return 0;
}
27 changes: 27 additions & 0 deletions include/boost/accumulators/framework/accumulator_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ namespace detail
{
};

// function object that serialize an accumulator
template<typename Archive>
struct serialize_accumulator
{
serialize_accumulator(Archive & _ar, const unsigned int _file_version) :
ar(_ar), file_version(_file_version)
{}

template<typename Accumulator>
void operator ()(Accumulator &accumulator)
{
accumulator.serialize(ar, file_version);
}

private:
Archive& ar;
const unsigned int file_version;
};

} // namespace detail

#ifdef _MSC_VER
Expand Down Expand Up @@ -338,6 +357,14 @@ struct accumulator_set
);
}

// make the accumulator set serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
detail::serialize_accumulator<Archive> serializer(ar, file_version);
fusion::for_each(this->accumulators, serializer);
}

private:

accumulators_type accumulators;
Expand Down
7 changes: 7 additions & 0 deletions include/boost/accumulators/statistics/count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ namespace impl
return this->cnt;
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & cnt;
}

private:
std::size_t cnt;
};
Expand Down
7 changes: 7 additions & 0 deletions include/boost/accumulators/statistics/covariance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ namespace impl
return this->cov_;
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & cov_;
}

private:
result_type cov_;
};
Expand Down
16 changes: 16 additions & 0 deletions include/boost/accumulators/statistics/density.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <boost/accumulators/statistics/count.hpp>
#include <boost/accumulators/statistics/max.hpp>
#include <boost/accumulators/statistics/min.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/utility.hpp>

namespace boost { namespace accumulators
{
Expand Down Expand Up @@ -184,6 +186,20 @@ namespace impl
return make_iterator_range(this->histogram);
}

// make this accumulator serializeable
// TODO split to save/load and check on parameters provided in ctor
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & cache_size;
ar & cache;
ar & num_bins;
ar & samples_in_bin;
ar & bin_positions;
ar & histogram;
ar & is_dirty;
}

private:
std::size_t cache_size; // number of cached samples
array_type cache; // cache to store the first cache_size samples
Expand Down
14 changes: 14 additions & 0 deletions include/boost/accumulators/statistics/extended_p_square.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <boost/accumulators/statistics_fwd.hpp>
#include <boost/accumulators/statistics/count.hpp>
#include <boost/accumulators/statistics/times2_iterator.hpp>
#include <boost/serialization/vector.hpp>

namespace boost { namespace accumulators
{
Expand Down Expand Up @@ -236,6 +237,19 @@ namespace impl
);
}

public:
// make this accumulator serializeable
// TODO: do we need to split to load/save and verify that the parameters did not change?
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & probabilities;
ar & heights;
ar & actual_positions;
ar & desired_positions;
ar & positions_increments;
}

private:
array_type probabilities; // the quantile probabilities
array_type heights; // q_i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ namespace impl
}

}

public:
// make this accumulator serializeable
// TODO: do we need to split to load/save and verify that the parameters did not change?
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & probabilities;
ar & probability;
}

private:

array_type probabilities;
Expand Down
4 changes: 4 additions & 0 deletions include/boost/accumulators/statistics/kurtosis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ namespace impl
* ( accumulators::moment<2>(args) - mean(args) * mean(args) )
) - 3.;
}

// serialization is done by accumulators it depends on
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version) {}
};

} // namespace impl
Expand Down
7 changes: 7 additions & 0 deletions include/boost/accumulators/statistics/max.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ namespace impl
return this->max_;
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & max_;
}

private:
Sample max_;
};
Expand Down
10 changes: 10 additions & 0 deletions include/boost/accumulators/statistics/mean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace impl
extractor<SumFeature> sum;
return numeric::fdiv(sum(args), count(args));
}

// serialization is done by accumulators it depends on
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version) {}
};

template<typename Sample, typename Tag>
Expand Down Expand Up @@ -71,6 +75,12 @@ namespace impl
return this->mean;
}

template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & mean;
}

private:
result_type mean;
};
Expand Down
23 changes: 23 additions & 0 deletions include/boost/accumulators/statistics/median.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ namespace impl
{
return p_square_quantile_for_median(args);
}

// serialization is done by accumulators it depends on
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version) {}
};
///////////////////////////////////////////////////////////////////////////////
// with_density_median_impl
Expand Down Expand Up @@ -105,6 +109,16 @@ namespace impl
return this->median;
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & sum;
ar & is_dirty;
ar & median;
}


private:
mutable float_type sum;
mutable bool is_dirty;
Expand Down Expand Up @@ -160,6 +174,15 @@ namespace impl

return this->median;
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & is_dirty;
ar & median;
}

private:

mutable bool is_dirty;
Expand Down
7 changes: 7 additions & 0 deletions include/boost/accumulators/statistics/min.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ namespace impl
return this->min_;
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & min_;
}

private:
Sample min_;
};
Expand Down
7 changes: 7 additions & 0 deletions include/boost/accumulators/statistics/moment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ namespace impl
return numeric::fdiv(this->sum, count(args));
}

// make this accumulator serializeable
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & sum;
}

private:
Sample sum;
};
Expand Down
Loading