Fix serde support
* Use correct Cargo flags. * Fix tests. * Fix histogram serialization.
This commit is contained in:
parent
15ff7e9ab4
commit
547149bfd2
@ -11,7 +11,7 @@ repository = "https://github.com/vks/average"
|
|||||||
version = "0.9.3"
|
version = "0.9.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
serde1 = ["serde", "serde_derive"]
|
serde1 = ["serde", "serde_derive", "serde-big-array"]
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
harness = false
|
harness = false
|
||||||
@ -31,6 +31,7 @@ num-integer = "0.1"
|
|||||||
float-ord = "0.2"
|
float-ord = "0.2"
|
||||||
serde = { version = "1", optional = true }
|
serde = { version = "1", optional = true }
|
||||||
serde_derive = { version = "1", optional = true }
|
serde_derive = { version = "1", optional = true }
|
||||||
|
serde-big-array = { version = "0.1.4", optional = true }
|
||||||
|
|
||||||
[dependencies.conv]
|
[dependencies.conv]
|
||||||
default-features = false
|
default-features = false
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate core;
|
/// # extern crate core;
|
||||||
/// # #[macro_use] extern crate average;
|
/// # #[macro_use] extern crate average;
|
||||||
|
/// # #[cfg(feature = "serde1")] #[macro_use] extern crate serde_derive;
|
||||||
|
/// # #[cfg(feature = "serde1")] #[macro_use] extern crate serde_big_array;
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// use average::Histogram;
|
/// use average::Histogram;
|
||||||
///
|
///
|
||||||
@ -28,27 +30,33 @@ macro_rules! define_histogram {
|
|||||||
($name:ident, $LEN:expr) => (
|
($name:ident, $LEN:expr) => (
|
||||||
mod $name {
|
mod $name {
|
||||||
use $crate::Histogram as Trait;
|
use $crate::Histogram as Trait;
|
||||||
|
#[cfg(feature = "serde1")] big_array! {
|
||||||
|
BigArray; LEN, (LEN + 1),
|
||||||
|
}
|
||||||
|
|
||||||
/// The number of bins of the histogram.
|
/// The number of bins of the histogram.
|
||||||
const LEN: usize = $LEN;
|
const LEN: usize = $LEN;
|
||||||
|
|
||||||
/// A histogram with a number of bins known at compile time.
|
/// A histogram with a number of bins known at compile time.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
|
||||||
pub struct Histogram {
|
pub struct Histogram {
|
||||||
/// The ranges defining the bins of the histogram.
|
/// The ranges defining the bins of the histogram.
|
||||||
|
#[cfg_attr(feature = "serde1", serde(with = "BigArray"))]
|
||||||
range: [f64; LEN + 1],
|
range: [f64; LEN + 1],
|
||||||
/// The bins of the histogram.
|
/// The bins of the histogram.
|
||||||
|
#[cfg_attr(feature = "serde1", serde(with = "BigArray"))]
|
||||||
bin: [u64; LEN],
|
bin: [u64; LEN],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::core::fmt::Debug for Histogram {
|
impl ::core::fmt::Debug for Histogram {
|
||||||
fn fmt(&self, formatter: &mut ::core::fmt::Formatter)
|
fn fmt(&self, formatter: &mut ::core::fmt::Formatter)
|
||||||
-> ::core::fmt::Result {
|
-> ::core::fmt::Result {
|
||||||
write!(formatter, "Histogram {{ range: ")?;
|
formatter.write_str("Histogram {{ range: ")?;
|
||||||
self.range[..].fmt(formatter)?;
|
self.range[..].fmt(formatter)?;
|
||||||
write!(formatter, ", bins: ")?;
|
formatter.write_str(", bins: ")?;
|
||||||
self.bin[..].fmt(formatter)?;
|
self.bin[..].fmt(formatter)?;
|
||||||
write!(formatter, " }}")
|
formatter.write_str(" }}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +96,8 @@ extern crate float_ord;
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
#[cfg(feature = "serde1")]
|
#[cfg(feature = "serde1")]
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
|
#[cfg(feature = "serde1")]
|
||||||
|
#[macro_use] extern crate serde_big_array;
|
||||||
extern crate num_traits;
|
extern crate num_traits;
|
||||||
extern crate num_integer;
|
extern crate num_integer;
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ impl Merge for WeightedMean {
|
|||||||
/// println!("The weighted mean is {} ± {}.", a.weighted_mean(), a.error());
|
/// println!("The weighted mean is {} ± {}.", a.weighted_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 WeightedMeanWithError {
|
pub struct WeightedMeanWithError {
|
||||||
/// Sum of the squares of the weights.
|
/// Sum of the squares of the weights.
|
||||||
weight_sum_sq: f64,
|
weight_sum_sq: f64,
|
||||||
|
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
#[cfg(feature = "serde1")]
|
||||||
|
#[macro_use] extern crate serde_derive;
|
||||||
|
#[cfg(feature = "serde1")]
|
||||||
|
extern crate serde_json;
|
||||||
|
#[cfg(feature = "serde1")]
|
||||||
|
#[macro_use] extern crate serde_big_array;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
use rand::distributions::Distribution;
|
use rand::distributions::Distribution;
|
||||||
@ -228,3 +234,19 @@ fn merge() {
|
|||||||
println!("{:?}", h1.bins());
|
println!("{:?}", h1.bins());
|
||||||
assert_eq!(h.bins(), h1.bins());
|
assert_eq!(h.bins(), h1.bins());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serde1")]
|
||||||
|
#[test]
|
||||||
|
fn simple_serde() {
|
||||||
|
/*
|
||||||
|
let mut a = Histogram10::from_ranges(
|
||||||
|
[0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.7, 0.8, 0.9, 1.0, 2.0].iter().cloned()).unwrap();
|
||||||
|
for &i in &[0.05, 0.7, 1.0, 1.5] {
|
||||||
|
h.add(i).unwrap();
|
||||||
|
}
|
||||||
|
let b = serde_json::to_string(&a).unwrap();
|
||||||
|
assert_eq!(&b, "{\"avg\":{\"avg\":3.0,\"n\":5},\"sum_2\":10.0}");
|
||||||
|
let c: Histogram10 = serde_json::from_str(&b).unwrap();
|
||||||
|
assert_eq!(h.bins(), &[1, 0, 0, 0, 0, 0, 1, 0, 0, 2]);
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#[macro_use] extern crate average;
|
#[macro_use] extern crate average;
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
@ -43,7 +43,7 @@ fn simple() {
|
|||||||
assert_almost_eq!(a.kurtosis(), -1.365, 1e-15);
|
assert_almost_eq!(a.kurtosis(), -1.365, 1e-15);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_serde() {
|
fn simple_serde() {
|
||||||
let a: Kurtosis = (1..6).map(f64::from).collect();
|
let a: Kurtosis = (1..6).map(f64::from).collect();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
extern crate average;
|
extern crate average;
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
@ -21,7 +21,7 @@ fn trivial() {
|
|||||||
assert_eq!(m.max(), 3.)
|
assert_eq!(m.max(), 3.)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn trivial_serde() {
|
fn trivial_serde() {
|
||||||
let mut m = Max::new();
|
let mut m = Max::new();
|
||||||
|
@ -37,7 +37,7 @@ fn simple() {
|
|||||||
assert_almost_eq!(a.error(), f64::sqrt(0.5), 1e-16);
|
assert_almost_eq!(a.error(), f64::sqrt(0.5), 1e-16);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_serde() {
|
fn simple_serde() {
|
||||||
let a: MeanWithError = (1..6).map(f64::from).collect();
|
let a: MeanWithError = (1..6).map(f64::from).collect();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
extern crate average;
|
extern crate average;
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
@ -21,7 +21,7 @@ fn trivial() {
|
|||||||
assert_eq!(m.min(), -1.)
|
assert_eq!(m.min(), -1.)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn trivial_serde() {
|
fn trivial_serde() {
|
||||||
let mut m = Min::new();
|
let mut m = Min::new();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#[macro_use] extern crate average;
|
#[macro_use] extern crate average;
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
@ -53,12 +53,12 @@ fn simple() {
|
|||||||
assert_almost_eq!(a.standardized_moment(4), -1.365 + 3.0, 1e-14);
|
assert_almost_eq!(a.standardized_moment(4), -1.365 + 3.0, 1e-14);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_serde() {
|
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.7763568394002505e-15,34.00000000000001]}");
|
||||||
let mut c: Moments4 = 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);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
|
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
|
||||||
|
|
||||||
extern crate average;
|
extern crate average;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate quantiles;
|
extern crate quantiles;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ fn few_observations() {
|
|||||||
assert_eq!(q.quantile(), 2.5);
|
assert_eq!(q.quantile(), 2.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn few_observations_serde() {
|
fn few_observations_serde() {
|
||||||
let mut q = Quantile::new(0.5);
|
let mut q = Quantile::new(0.5);
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#[macro_use] extern crate average;
|
#[macro_use] extern crate average;
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
@ -42,7 +42,7 @@ fn simple() {
|
|||||||
assert_almost_eq!(a.skewness(), 0.2795084971874741, 1e-15);
|
assert_almost_eq!(a.skewness(), 0.2795084971874741, 1e-15);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_serde() {
|
fn simple_serde() {
|
||||||
let a: Skewness = (1..6).map(f64::from).collect();
|
let a: Skewness = (1..6).map(f64::from).collect();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#[macro_use] extern crate average;
|
#[macro_use] extern crate average;
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
@ -45,7 +45,7 @@ fn simple() {
|
|||||||
assert_almost_eq!(a.error(), f64::sqrt(0.5), 1e-16);
|
assert_almost_eq!(a.error(), f64::sqrt(0.5), 1e-16);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde1")]
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_serde() {
|
fn simple_serde() {
|
||||||
let a: WeightedMeanWithError = (1..6).map(|x| (f64::from(x), 1.0)).collect();
|
let a: WeightedMeanWithError = (1..6).map(|x| (f64::from(x), 1.0)).collect();
|
||||||
|
Loading…
Reference in New Issue
Block a user