More idiomatic serde support
* Follow the suggestions from the Serde docs. * Rename the feature from `serde` to `serde1`. * Fix a doctest. * Mention the feature in the README.
This commit is contained in:
parent
34d33ef21a
commit
a7dde93df8
10
Cargo.toml
10
Cargo.toml
@ -10,6 +10,9 @@ readme = "README.md"
|
|||||||
repository = "https://github.com/vks/average"
|
repository = "https://github.com/vks/average"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
serde1 = ["serde", "serde_derive"]
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
harness = false
|
harness = false
|
||||||
name = "mean"
|
name = "mean"
|
||||||
@ -26,16 +29,13 @@ name = "kurtosis"
|
|||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
num-integer = "0.1"
|
num-integer = "0.1"
|
||||||
float-ord = "0.2"
|
float-ord = "0.2"
|
||||||
|
serde = { version = "1", optional = true }
|
||||||
|
serde_derive = { version = "1", optional = true }
|
||||||
|
|
||||||
[dependencies.conv]
|
[dependencies.conv]
|
||||||
default-features = false
|
default-features = false
|
||||||
version = "0.3"
|
version = "0.3"
|
||||||
|
|
||||||
[dependencies.serde]
|
|
||||||
features = ["derive"]
|
|
||||||
optional = true
|
|
||||||
version = "1"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bencher = "0.1"
|
bencher = "0.1"
|
||||||
rand = "0.5"
|
rand = "0.5"
|
||||||
|
@ -15,6 +15,7 @@ easily parallelized by using `merge`.
|
|||||||
[Latest Version]: https://img.shields.io/crates/v/average.svg
|
[Latest Version]: https://img.shields.io/crates/v/average.svg
|
||||||
[crates.io]: https://crates.io/crates/average
|
[crates.io]: https://crates.io/crates/average
|
||||||
|
|
||||||
|
|
||||||
## Implemented statistics
|
## Implemented statistics
|
||||||
|
|
||||||
* Mean and its error.
|
* Mean and its error.
|
||||||
@ -24,6 +25,14 @@ easily parallelized by using `merge`.
|
|||||||
* Quantile.
|
* Quantile.
|
||||||
* Histogram.
|
* Histogram.
|
||||||
|
|
||||||
|
|
||||||
|
## Crate features
|
||||||
|
|
||||||
|
The following optional feature is available:
|
||||||
|
|
||||||
|
* `serde1` enables serialization, via Serde version 1.
|
||||||
|
|
||||||
|
|
||||||
## Related Projects
|
## Related Projects
|
||||||
|
|
||||||
* [`quantiles`](https://crates.io/crates/quantiles):
|
* [`quantiles`](https://crates.io/crates/quantiles):
|
||||||
|
@ -89,9 +89,10 @@
|
|||||||
|
|
||||||
extern crate conv;
|
extern crate conv;
|
||||||
extern crate float_ord;
|
extern crate float_ord;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[macro_use]
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
#[cfg(feature = "serde1")]
|
||||||
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate num_traits;
|
extern crate num_traits;
|
||||||
extern crate num_integer;
|
extern crate num_integer;
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ fn max(a: f64, b: f64) -> f64 {
|
|||||||
/// println!("The minimum is {}.", a.min());
|
/// println!("The minimum is {}.", a.min());
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Min {
|
pub struct Min {
|
||||||
x: f64,
|
x: f64,
|
||||||
}
|
}
|
||||||
@ -104,7 +104,7 @@ impl Merge for Min {
|
|||||||
/// assert_eq!(a.max(), 5.);
|
/// assert_eq!(a.max(), 5.);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Max {
|
pub struct Max {
|
||||||
x: f64,
|
x: f64,
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
///
|
///
|
||||||
/// This can be used to estimate the standard error of the mean.
|
/// This can be used to estimate the standard error of the mean.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Kurtosis {
|
pub struct Kurtosis {
|
||||||
/// Estimator of mean, variance and skewness.
|
/// Estimator of mean, variance and skewness.
|
||||||
avg: Skewness,
|
avg: Skewness,
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
/// println!("The mean is {}.", a.mean());
|
/// println!("The mean is {}.", a.mean());
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Mean {
|
pub struct Mean {
|
||||||
/// Mean value.
|
/// Mean value.
|
||||||
avg: f64,
|
avg: f64,
|
||||||
|
@ -32,6 +32,10 @@ pub type MeanWithError = Variance;
|
|||||||
/// # extern crate conv;
|
/// # extern crate conv;
|
||||||
/// # extern crate num_integer;
|
/// # extern crate num_integer;
|
||||||
/// # extern crate num_traits;
|
/// # extern crate num_traits;
|
||||||
|
/// #[cfg(feature = "serde1")]
|
||||||
|
/// extern crate serde;
|
||||||
|
/// #[cfg(feature = "serde1")]
|
||||||
|
/// #[macro_use] extern crate serde_derive;
|
||||||
/// # #[macro_use] extern crate average;
|
/// # #[macro_use] extern crate average;
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// define_moments!(Moments4, 4);
|
/// define_moments!(Moments4, 4);
|
||||||
@ -65,7 +69,7 @@ macro_rules! define_moments {
|
|||||||
/// Estimate the first N moments of a sequence of numbers a sequence of numbers
|
/// Estimate the first N moments of a sequence of numbers a sequence of numbers
|
||||||
/// ("population").
|
/// ("population").
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct $name {
|
pub struct $name {
|
||||||
/// Number of samples.
|
/// Number of samples.
|
||||||
///
|
///
|
||||||
@ -126,13 +130,12 @@ macro_rules! define_moments {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn standardized_moment(&self, p: usize) -> f64 {
|
pub fn standardized_moment(&self, p: usize) -> f64 {
|
||||||
match p {
|
match p {
|
||||||
0 => n,
|
0 => f64::approx_from(self.n).unwrap(),
|
||||||
1 => 0.,
|
1 => 0.,
|
||||||
2 => 1.,
|
2 => 1.,
|
||||||
_ => {
|
_ => {
|
||||||
let variance = self.central_moment(2);
|
let variance = self.central_moment(2);
|
||||||
assert_ne!(variance, 0.);
|
assert_ne!(variance, 0.);
|
||||||
let n = f64::approx_from(self.n).unwrap();
|
|
||||||
self.central_moment(p) / pow(variance.sqrt(), p)
|
self.central_moment(p) / pow(variance.sqrt(), p)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
///
|
///
|
||||||
/// This can be used to estimate the standard error of the mean.
|
/// This can be used to estimate the standard error of the mean.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Skewness {
|
pub struct Skewness {
|
||||||
/// Estimator of mean and variance.
|
/// Estimator of mean and variance.
|
||||||
avg: MeanWithError,
|
avg: MeanWithError,
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
/// println!("The mean is {} ± {}.", a.mean(), a.error());
|
/// println!("The mean is {} ± {}.", a.mean(), a.error());
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Variance {
|
pub struct Variance {
|
||||||
/// Estimator of average.
|
/// Estimator of average.
|
||||||
avg: Mean,
|
avg: Mean,
|
||||||
|
@ -15,7 +15,7 @@ use super::Estimate;
|
|||||||
// This uses the P² algorithm introduced here:
|
// This uses the P² algorithm introduced here:
|
||||||
// http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
|
// http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Quantile {
|
pub struct Quantile {
|
||||||
/// Marker heights.
|
/// Marker heights.
|
||||||
q: [f64; 5],
|
q: [f64; 5],
|
||||||
|
@ -17,7 +17,7 @@ use super::{MeanWithError, Estimate, Merge};
|
|||||||
/// println!("The weighted mean is {}.", a.mean());
|
/// println!("The weighted mean is {}.", a.mean());
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct WeightedMean {
|
pub struct WeightedMean {
|
||||||
/// Sum of the weights.
|
/// Sum of the weights.
|
||||||
weight_sum: f64,
|
weight_sum: f64,
|
||||||
|
@ -59,7 +59,7 @@ fn simple_serde() {
|
|||||||
let a: Moments4 = (1..6).map(f64::from).collect();
|
let a: Moments4 = (1..6).map(f64::from).collect();
|
||||||
let b = serde_json::to_string(&a).unwrap();
|
let b = serde_json::to_string(&a).unwrap();
|
||||||
assert_eq!(&b, "{\"n\":5,\"avg\":3.0,\"m\":[10.0,1.7763568394002506e-15,34.00000000000001]}");
|
assert_eq!(&b, "{\"n\":5,\"avg\":3.0,\"m\":[10.0,1.7763568394002506e-15,34.00000000000001]}");
|
||||||
let mut c: Moments = serde_json::from_str(&b).unwrap();
|
let mut c: Moments4 = serde_json::from_str(&b).unwrap();
|
||||||
assert_eq!(c.len(), 5);
|
assert_eq!(c.len(), 5);
|
||||||
assert_eq!(c.mean(), 3.0);
|
assert_eq!(c.mean(), 3.0);
|
||||||
assert_eq!(c.central_moment(0), 1.0);
|
assert_eq!(c.central_moment(0), 1.0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user