Skip to content

Commit

Permalink
fix ExtremeValues albeit with a breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Feb 21, 2024
1 parent a5a3edf commit aabf395
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OnlineStatsBase"
uuid = "925886fa-5bf2-5e8e-b522-a9147a512338"
version = "1.6.2"
version = "1.6.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
45 changes: 17 additions & 28 deletions src/stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,46 +287,35 @@ Track the `b` smallest and largest values of a data stream (as well as how many
value(o).hi # [98 => 1, 99 => 1, 100 => 1]
"""
mutable struct ExtremeValues{T,S} <: OnlineStat{S}
lo::Vector{Pair{T,Int}}
hi::Vector{Pair{T,Int}}
mutable struct ExtremeValues{T, S} <: OnlineStat{S}
lo::OrderedDict{T, Int}
hi::OrderedDict{T, Int}
b::Int
n::Int
end
value(o::ExtremeValues) = (; lo=o.lo, hi=o.hi)
value(o::ExtremeValues) = (; lo=sort!(o.lo), hi=sort!(o.hi))
function ExtremeValues(T::Type = Float64, b=25)
_, _, S = extrema_init(T)
ExtremeValues{T,S}(Pair{T,Int}[], Pair{T,Int}[], b, 0)
ExtremeValues{T,S}(OrderedDict(), OrderedDict(), b, 0)
end

_fit!(o::ExtremeValues{T,S}, y::S) where {T,S} = _fit!(o, y => 1)

function _fit!(o::ExtremeValues{T,S}, pr::Pair{<:S, Int}) where {T,S}
y, yn = pr
o.n += yn
y, n = pr
o.n += n
lo, hi, b = o.lo, o.hi, o.b
if length(lo) < b
push!(lo, pr); push!(hi, pr)
sort!(lo); sort!(hi)
elseif y first(lo[end])
if y in (first(el) for el in lo)
i = findfirst(x -> x[1] == y, lo)
lo[i] = lo[i][1] => lo[i][2] + yn
else
push!(lo, pr)
sort!(lo)
length(lo) > b && deleteat!(lo, b + 1)
end
elseif first(hi[1]) y
if y in (first(el) for el in hi)
i = findfirst(x -> x[1] == y, hi)
hi[i] = hi[i][1] => hi[i][2] + yn
else
push!(hi, pr)
sort!(hi)
length(hi) > b && deleteat!(hi, 1)
end
isempty(lo) && return (lo[y] = hi[y] = n) # Case 1: First observation
if y in keys(lo) || y in keys(hi) # Case 2: y already in lo and/or hi
y in keys(lo) && (lo[y] += n)
y in keys(hi) && (hi[y] += n)
return
end
length(lo) < b && return (lo[y] = n; hi[y] = n)
y < maximum(keys(lo)) && (lo[y] = n)
y > minimum(keys(hi)) && (hi[y] = n)
length(lo) > b && delete!(lo, maximum(keys(lo)))
length(hi) > b && delete!(hi, minimum(keys(hi)))
end

function _merge!(a::ExtremeValues, b::ExtremeValues)
Expand Down
4 changes: 2 additions & 2 deletions test/test_stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ println(" > ExtremeValues")
@testset "ExtremeValues" begin
o = fit!(ExtremeValues(Float64, 5), y)
ysorted = sort(y)
@test first.(value(o).lo) == ysorted[1:5]
@test first.(value(o).hi) == ysorted[end-4:end]
@test collect(keys(value(o).lo)) == ysorted[1:5]
@test collect(keys(value(o).hi)) == ysorted[end-4:end]

@test ==(mergevals(ExtremeValues(), y, y2)...)
end
Expand Down

4 comments on commit aabf395

@joshday
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 register

Release Notes

@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/101354

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v1.6.3 -m "<description of version>" aabf395ec03a647d08069a744b6b941485e5c893
git push origin v1.6.3

@joshday
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 register

Release Notes:

Breaking Change

@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 updated: JuliaRegistries/General/101354

Tagging

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 v1.6.3 -m "<description of version>" aabf395ec03a647d08069a744b6b941485e5c893
git push origin v1.6.3

Please sign in to comment.