Skip to content

Commit

Permalink
Add copy, ==, append! methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed Feb 20, 2020
1 parent 0dc0482 commit 987c4c5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Project.toml
@@ -1,6 +1,6 @@
name = "EmpiricalCDFs"
uuid = "8c59bae6-5410-11ea-0770-21e7a53f514f"
version = "0.2.1"
uuid = "0dcf7749-4f9f-5e13-9c57-506664207bbc"
version = "0.2.2"
license= "MIT"

[deps]
Expand Down
29 changes: 23 additions & 6 deletions src/cdfs.jl
Expand Up @@ -9,6 +9,9 @@ struct EmpiricalCDF{T <: Real} <: AbstractEmpiricalCDF
xdata::Vector{T}
end

Base.:(==)(cdf1::EmpiricalCDF, cdf2::EmpiricalCDF) = cdf1.xdata == cdf2.xdata
Base.copy(cdf::EmpiricalCDF) = EmpiricalCDF(copy(cdf.xdata))

"""
data(cdf::AbstractEmpiricalCDF)
Expand Down Expand Up @@ -103,6 +106,11 @@ end

(cdf::EmpiricalCDFHi)(x::Real) = _val_at_index(cdf, getcdfindex(cdf, x))

Base.:(==)(cdf1::EmpiricalCDFHi, cdf2::EmpiricalCDFHi) = cdf1.lowreject == cdf2.lowreject &&
cdf1.rejectcounts == cdf2.rejectcounts && cdf1.xdata == cdf2.xdata

Base.copy(cdf::EmpiricalCDFHi) = EmpiricalCDFHi(cdf.lowreject, copy(cdf.rejectcounts), copy(cdf.xdata))

"""
EmpiricalCDFHi(lowreject::Real)
Expand All @@ -124,7 +132,7 @@ end
"""
EmpiricalCDF(lowreject::Real)
If `lowereject` is finite return `EmpiricalCDFHi(lowreject)`. Otherwise
If `lowreject` is finite return `EmpiricalCDFHi(lowreject)`. Otherwise
return `EmpiricalCDF()`.
"""
function EmpiricalCDF(lowreject::Real)
Expand Down Expand Up @@ -153,6 +161,8 @@ _val_at_index(cdf::EmpiricalCDFHi, i) = (i+ _rejectcounts(cdf) )/_total_counts(

_increment_rejectcounts(cdf::EmpiricalCDFHi) = cdf.rejectcounts[1] += 1

_addto_rejectcounts(cdf::EmpiricalCDFHi, n) = cdf.rejectcounts[1] += n

_rejectcounts(cdf::EmpiricalCDFHi) = cdf.rejectcounts[1]

"""
Expand All @@ -179,16 +189,25 @@ function Base.push!(cdf::EmpiricalCDFHi, x::Real)
cdf
end

Base.append!(cdf_to::EmpiricalCDF, cdf_from::AbstractEmpiricalCDF) = append!(cdf_to, cdf_from.xdata)

function Base.append!(cdf_to::EmpiricalCDFHi, cdf_from::AbstractEmpiricalCDF)
append!(cdf_to, cdf_from.xdata)
_addto_rejectcounts(cdf_to, _rejectcounts(cdf_from))
return cdf_to
end

"""
append!(cdf::EmpiricalCDF, a::AbstractArray)
append!(cdf_to::AbstractEmpiricalCDF, cdf_from::AbstractEmpiricalCDF)
add samples in `a` to `cdf`.
Add samples in `a` to `cdf`. Add samples in `cdf_from` to `cdf_to`.
"""
Base.append!(cdf::EmpiricalCDF, a::AbstractArray) = (append!(cdf.xdata,a); cdf)
Base.append!(cdf::EmpiricalCDF, a::AbstractArray) = (append!(cdf.xdata, a); cdf)

function Base.append!(cdf::EmpiricalCDFHi, times)
if isinf(cdf.lowreject)
append!(cdf.xdata,times)
append!(cdf.xdata, times)
else
for x in times
_push!(cdf,x)
Expand Down Expand Up @@ -315,10 +334,8 @@ end
function _printfull(io, cdf::AbstractEmpiricalCDF, prpts; lastpt=false)
n = length(cdf)
println(io, "# log10(t) log10(P(t)) log10(1-P(t)) t 1-P(t) P(t)")
# state = start(prpts)
(p, state) = iterate(prpts)
ulim = lastpt ? n : n - 1 # usually don't print ordinate value of 1
# println("ulim is ", ulim)
for i in 1:ulim
@inbounds xp = cdf.xdata[i]
next = iterate(prpts, state)
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Expand Up @@ -13,6 +13,19 @@ using Statistics
@test (cdf = EmpiricalCDF(.5); append!(cdf,rand(10^3)) ; sort!(cdf); typeof(cdf(1)) == Float64)
end

@testset "copy, equality" begin
cdf = EmpiricalCDF()
append!(cdf, rand(10^3))
cdf2 = copy(cdf)
@test cdf == cdf2

cdfh = EmpiricalCDFHi(0.0)
append!(cdfh, randn(10^4))
cdfh2 = EmpiricalCDFHi(0.0)
append!(cdfh2, cdfh)
@test cdfh2 == cdfh
end

@testset "cutoff" begin
a = rand(10^5); # points in [0,1)
cdf0 = EmpiricalCDF(); # accept all points
Expand Down

2 comments on commit 987c4c5

@jlapeyre
Copy link
Owner Author

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/9824

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 987c4c5d34cde876237792c2a5b2ea67e70065ec
git push origin v0.2.2

Please sign in to comment.