From 12740038720aa62cd25a12fd6bedc4f38b4eb2f2 Mon Sep 17 00:00:00 2001 From: xd009642 Date: Tue, 26 Feb 2019 21:43:45 +0000 Subject: [PATCH 1/3] Added a cumulative sum function to Histogram This is equivalent to the cumsum for numpys histogram object. --- src/histogram/histograms.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/histogram/histograms.rs b/src/histogram/histograms.rs index 825aadb7..888b573a 100644 --- a/src/histogram/histograms.rs +++ b/src/histogram/histograms.rs @@ -78,6 +78,16 @@ impl Histogram { pub fn grid(&self) -> &Grid { &self.grid } + + /// Returns the cumulative distribution function of a histogram. + /// Equivalent to the numpy histogram function cumsum + pub fn cumulative_sum(&self) -> ArrayD { + let mut total = 0; + self.counts.mapv(|x| { + total += x; + total + }) + } } /// Extension trait for `ArrayBase` providing methods to compute histograms. @@ -163,3 +173,23 @@ impl HistogramExt for ArrayBase histogram } } + +#[cfg(test)] +mod auto_tests { + use super::*; + use histogram::histograms::HistogramExt; + use histogram::bins::{Bins, Edges}; + + #[test] + fn histogram_cdf() { + let data = arr2(&[[1], [2], [3], [4], [1], [1], [4], [5], [8], [8], [8]]); + let grid = Grid::from(vec![ + Bins::new( + Edges::from(vec![0,1,2,3,4,5,6,7,8,9]))]); + + let histogram = data.histogram(grid); + //0, 1 2 3 4 5 6 7 8 + let cdf = arr1(&[0, 3, 4, 5, 7, 8, 8, 8, 11]).into_dyn(); + assert_eq!(histogram.cumulative_sum(), cdf); + } +} From cd17003e1ca8696aa7122765622a820dc6342032 Mon Sep 17 00:00:00 2001 From: xd009642 Date: Wed, 27 Feb 2019 22:23:14 +0000 Subject: [PATCH 2/3] Made CDF n-dimensional --- src/histogram/histograms.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/histogram/histograms.rs b/src/histogram/histograms.rs index 888b573a..38f8f420 100644 --- a/src/histogram/histograms.rs +++ b/src/histogram/histograms.rs @@ -82,11 +82,15 @@ impl Histogram { /// Returns the cumulative distribution function of a histogram. /// Equivalent to the numpy histogram function cumsum pub fn cumulative_sum(&self) -> ArrayD { - let mut total = 0; - self.counts.mapv(|x| { - total += x; - total - }) + let mut cdf = self.counts.clone(); + for i in 0..self.ndim() { + for j in 1..cdf.shape()[i] { + let temp = cdf.index_axis(Axis(i), j - 1).to_owned(); + let mut ax = cdf.index_axis_mut(Axis(i), j); + ax += &temp; + } + } + cdf } } From 6e84659befdc0815c965ac4891343500b1ee521d Mon Sep 17 00:00:00 2001 From: xd009642 Date: Thu, 28 Feb 2019 17:51:26 +0000 Subject: [PATCH 3/3] Added 2D CDF test for histogram --- src/histogram/histograms.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/histogram/histograms.rs b/src/histogram/histograms.rs index 38f8f420..0a0529cd 100644 --- a/src/histogram/histograms.rs +++ b/src/histogram/histograms.rs @@ -185,7 +185,7 @@ mod auto_tests { use histogram::bins::{Bins, Edges}; #[test] - fn histogram_cdf() { + fn histogram_cdf_1d() { let data = arr2(&[[1], [2], [3], [4], [1], [1], [4], [5], [8], [8], [8]]); let grid = Grid::from(vec![ Bins::new( @@ -196,4 +196,20 @@ mod auto_tests { let cdf = arr1(&[0, 3, 4, 5, 7, 8, 8, 8, 11]).into_dyn(); assert_eq!(histogram.cumulative_sum(), cdf); } + + #[test] + fn histogram_cdf_2d() { + let data = arr2(&[[0, 2], [4, 4], [1, 1], [3, 3], [0, 2]]); + let grid = Grid::from(vec![ + Bins::new(Edges::from(vec![0, 1, 2, 3, 4])), + Bins::new(Edges::from(vec![0, 1, 2, 3, 4]))]); + + let histogram = data.histogram(grid); + + let cdf = arr2(&[[0, 0, 2, 2], + [0, 1, 3, 3], + [0, 1, 3, 3], + [0, 1, 3, 4]]).into_dyn(); + assert_eq!(histogram.cumulative_sum(), cdf); + } }