Commit d675d2e2 authored by Davis King's avatar Davis King

Made running_stats serializable.

parent 7a8dcf2f
......@@ -158,6 +158,18 @@ namespace dlib
return (val-mean())/std::sqrt(variance());
}
template <typename U>
friend void serialize (
const running_stats<U>& item,
std::ostream& out
);
template <typename U>
friend void deserialize (
running_stats<U>& item,
std::istream& in
);
private:
T sum;
T sum_sqr;
......@@ -167,6 +179,34 @@ namespace dlib
T max_value;
};
template <typename T>
void serialize (
const running_stats<T>& item,
std::ostream& out
)
{
serialize(item.sum, out);
serialize(item.sum_sqr, out);
serialize(item.n, out);
serialize(item.maximum_n, out);
serialize(item.min_value, out);
serialize(item.max_value, out);
}
template <typename T>
void deserialize (
running_stats<T>& item,
std::istream& in
)
{
deserialize(item.sum, in);
deserialize(item.sum_sqr, in);
deserialize(item.n, in);
deserialize(item.maximum_n, in);
deserialize(item.min_value, in);
deserialize(item.max_value, in);
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -157,6 +157,24 @@ namespace dlib
!*/
};
template <typename T>
void serialize (
const running_stats<T>& item,
std::ostream& out
);
/*!
provides serialization support
!*/
template <typename T>
void deserialize (
running_stats<T>& item,
std::istream& in
);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
template <
......
......@@ -195,7 +195,7 @@ namespace
{
print_spinner();
running_stats<double> rs;
running_stats<double> rs, rs2;
running_scalar_covariance<double> rsc1, rsc2;
......@@ -208,7 +208,6 @@ namespace
rsc2.add(i,-i);
}
// make sure the running_stats and running_scalar_covariance agree
DLIB_TEST_MSG(std::abs(rs.mean() - rsc1.mean_x()) < 1e-10, std::abs(rs.mean() - rsc1.mean_x()));
DLIB_TEST(std::abs(rs.mean() - rsc1.mean_y()) < 1e-10);
......@@ -220,6 +219,23 @@ namespace
DLIB_TEST(std::abs(rsc1.correlation() - 1) < 1e-10);
DLIB_TEST(std::abs(rsc2.correlation() - 0) < 1e-10);
// test serialization of running_stats
ostringstream sout;
serialize(rs, sout);
istringstream sin(sout.str());
deserialize(rs2, sin);
// make sure the running_stats and running_scalar_covariance agree
DLIB_TEST_MSG(std::abs(rs2.mean() - rsc1.mean_x()) < 1e-10, std::abs(rs2.mean() - rsc1.mean_x()));
DLIB_TEST(std::abs(rs2.mean() - rsc1.mean_y()) < 1e-10);
DLIB_TEST(std::abs(rs2.stddev() - rsc1.stddev_x()) < 1e-10);
DLIB_TEST(std::abs(rs2.stddev() - rsc1.stddev_y()) < 1e-10);
DLIB_TEST(std::abs(rs2.variance() - rsc1.variance_x()) < 1e-10);
DLIB_TEST(std::abs(rs2.variance() - rsc1.variance_y()) < 1e-10);
DLIB_TEST(rs2.current_n() == rsc1.current_n());
}
void perform_test (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment