Skip to content

Commit

Permalink
propagate nentries rebin and restrict (#70)
Browse files Browse the repository at this point in the history
* propagate nentries rebin and restrict

* bump version
  • Loading branch information
Moelf authored Aug 9, 2022
1 parent 15cad47 commit e1d4139
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FHist"
uuid = "68837c9b-b678-4cd5-9925-8a54edc8f695"
authors = ["Moelf <proton[at]jling.dev>", "Nick Amin <amin.nj[at]gmail.com>"]
version = "0.8.5"
version = "0.8.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
4 changes: 2 additions & 2 deletions src/hist1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/hist2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

"""
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,15 @@ 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]

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]

Expand Down Expand Up @@ -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)
Expand Down

2 comments on commit e1d4139

@Moelf
Copy link
Owner Author

@Moelf Moelf commented on e1d4139 Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/65943

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.6 -m "<description of version>" e1d413944e70be2a6138ad89556c0bf16d79a0cb
git push origin v0.8.6

Please sign in to comment.