From e1d413944e70be2a6138ad89556c0bf16d79a0cb Mon Sep 17 00:00:00 2001 From: Jerry Ling Date: Tue, 9 Aug 2022 09:40:43 -0400 Subject: [PATCH] propagate nentries rebin and restrict (#70) * propagate nentries rebin and restrict * bump version --- Project.toml | 2 +- src/hist1d.jl | 4 ++-- src/hist2d.jl | 4 ++-- test/runtests.jl | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index d3ed9c0..aba8992 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "FHist" uuid = "68837c9b-b678-4cd5-9925-8a54edc8f695" authors = ["Moelf ", "Nick Amin "] -version = "0.8.5" +version = "0.8.6" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/hist1d.jl b/src/hist1d.jl index 3e91e1f..636a546 100644 --- a/src/hist1d.jl +++ b/src/hist1d.jl @@ -283,7 +283,7 @@ function rebin(h::Hist1D, n::Int=1) if _is_uniform_bins(edges) edges = range(first(edges), last(edges), length=length(edges)) end - return Hist1D(Histogram(edges, counts), sumw2; overflow=h.overflow) + return Hist1D(Histogram(edges, counts), sumw2, nentries(h); overflow=h.overflow) end rebin(n::Int) = h::Hist1D -> rebin(h, n) @@ -312,6 +312,6 @@ function restrict(h::Hist1D, low=-Inf, high=Inf) if _is_uniform_bins(edges) edges = range(first(edges), last(edges), length=length(edges)) end - Hist1D(Histogram(edges, c), sumw2; overflow=h.overflow) + Hist1D(Histogram(edges, c), sumw2, nentries(h); overflow=h.overflow) end restrict(low=-Inf, high=Inf) = h::Hist1D->restrict(h, low, high) diff --git a/src/hist2d.jl b/src/hist2d.jl index 00a6020..1ca43fc 100644 --- a/src/hist2d.jl +++ b/src/hist2d.jl @@ -247,7 +247,7 @@ function rebin(h::Hist2D, nx::Int=1, ny::Int=nx) ey = first.(p1d(binedges(h)[2], ny)) _is_uniform_bins(ex) && (ex = range(first(ex), last(ex), length=length(ex))) _is_uniform_bins(ey) && (ey = range(first(ey), last(ey), length=length(ey))) - return Hist2D(Histogram((ex,ey), counts), sumw2; overflow=h.overflow) + return Hist2D(Histogram((ex,ey), counts), sumw2, nentries(h); overflow=h.overflow) end rebin(nx::Int, ny::Int) = h::Hist2D -> rebin(h, nx, ny) @@ -265,7 +265,7 @@ function project(h::Hist2D, axis::Symbol=:x) counts = [sum(bincounts(h), dims=dim)...] sumw2 = [sum(h.sumw2, dims=dim)...] edges = axis == :x ? ex : ey - return Hist1D(Histogram(edges, counts), sumw2; overflow=h.overflow) + return Hist1D(Histogram(edges, counts), sumw2, nentries(h); overflow=h.overflow) end """ diff --git a/test/runtests.jl b/test/runtests.jl index 1247e31..f7c924b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -394,6 +394,7 @@ end @testset "Rebinning" begin h1 = Hist1D(rand(10^2), 0:0.1:1) @test h1 == rebin(h1, 1) + @test nentries(h1) == nentries(rebin(h1, 1)) @test integral(h1) == integral(rebin(h1, 5)) @test sum(h1.sumw2) == sum(rebin(h1, 5).sumw2) @test binedges(rebin(h1, 5)) == [0, 0.5, 1.0] @@ -401,6 +402,7 @@ end h2 = Hist1D(rand(10^2), [0.0, 0.1, 0.7, 0.9, 1.0]) @test h2 == rebin(h2, 1) @test integral(h2) == integral(rebin(h2, 2)) + @test nentries(h2) == nentries(rebin(h2, 1)) @test sum(h2.sumw2) == sum(rebin(h2, 2).sumw2) @test binedges(rebin(h2, 2)) == [0, 0.7, 1.0] @@ -485,6 +487,7 @@ end hright = restrict(h, 0.0, Inf) @test h == restrict(h) + @test nentries(h) == nentries(restrict(h)) @test restrict(h, -1, 1) == (h |> restrict(-1,1)) @test integral(hleft) + integral(hright) == integral(h) @test nbins(hleft) + nbins(hright) == nbins(h)