Skip to content

Commit

Permalink
Fix percentage (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma authored Dec 4, 2022
1 parent 7fa258e commit 3151401
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 48 deletions.
53 changes: 39 additions & 14 deletions src/Tally.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,56 @@ function _maximal_length_of_items(keys, vals)
end

function _get_nice_percentages(per::Vector{Float64})
find_ndigits = 2
for p in per
iszero(p) && continue
while all(x -> isequal(x, '0') || isequal(x, '.'), sprint_formatted("%.$(find_ndigits)f", p))
find_ndigits += 1
find_ndigits = 0
nice = String[]
if length(per) == 0
return 0, String[]
end
while true
for p in per
iszero(p) && continue
while all(x -> x in [' ', '0', '.'], sprint_formatted("%3.$(find_ndigits)f", p))
find_ndigits += 1
end
end
nice = [lpad(sprint_formatted("%3.$(find_ndigits)f", p), find_ndigits + 4)* "%" for p in per]

# now check if different values are distinguished
good = true
for i in 1:length(per)
if any(j -> per[i] != per[j] && nice[i] == nice[j], 1:length(per))
find_ndigits += 1
good = false
break
end
end
if good
break
end
end
return find_ndigits, [sprint_formatted("%.$(find_ndigits)f", p)* "%" for p in per]

while all(x -> startswith(x, ' '), nice)
nice = map(x -> x[2:end], nice)
end

return find_ndigits, nice
end

# Print it as a table
function Base.show(io::IO, ::MIME"text/plain", T::TallyT)
first = true
n = mapreduce(x -> x[2], +, T.data, init = 0)
k, v = _prepare_for_plot(T)
print(io, "Tally with $n items in $(length(T.data)) groups:\n")
print(io, "Tally with $n items in $(length(T.data)) groups")
if length(T) == 0
return
end
print(io, ":")
l_names, l_digits = _maximal_length_of_items(k, v)
percentages = [x/n for x in v]
percentages = [100 * x/n for x in v]
find_ndigits, percentage_strings = _get_nice_percentages(percentages)
for (i, x) in enumerate(zip(k, v))
if first
first = false
else
println(io)
end
println(io)
print(io, rpad(x[1], l_names))
print(io, " | ")
print(io, lpad(sprint(show, x[2]), l_digits))
Expand Down Expand Up @@ -301,7 +326,7 @@ function _prepare_for_plot(T::TallyT; sortby = :value, reverse = false, title =
for x in T
push!(keys, x[1])
push!(vals, x[2])
push!(percentage, x[2]/n)
push!(percentage, 100 * x[2]/n)
end
if isempty(T)
return keys, vals, percentage
Expand Down
72 changes: 38 additions & 34 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,48 @@ Base.hash(::A) = rand(UInt)
@test sprint(show, "text/plain", T) isa String
@test sprint(show, T) isa String

T = tally(push!([1 for i in 1:100000], 2))
s = sprint(show, "text/plain", T)
@test occursin("1", s)

# unicode plotting

# decimals are different on julia 1.0
if VERSION >= v"1.8"
T = tally([2, 1, 1, 1, 1, 2, 2, 3])
plot1 =
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 0.50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 0.38%\n" *
" 3 ┤■■■■■■■■■ 1 0.12%\n" *
" └ ┘ "
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 38%\n" *
" 3 ┤■■■■■■■■■ 1 12%\n" *
" └ ┘ "

plot2 =
" ┌ ┐ \n" *
" 3 ┤■■■■■■■■■ 1 0.12%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 0.38%\n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 0.50%\n" *
" └ ┘ "
" ┌ ┐ \n" *
" 3 ┤■■■■■■■■■ 1 12%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 38%\n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 50%\n" *
" └ ┘ "

plot3 =
" ┌ ┐ \n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 0.38%\n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 0.50%\n" *
" 3 ┤■■■■■■■■■ 1 0.12%\n" *
" └ ┘ "
" ┌ ┐ \n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 38%\n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 50%\n" *
" 3 ┤■■■■■■■■■ 1 12%\n" *
" └ ┘ "

plot4 =
" ┌ ┐ \n" *
" 3 ┤■■■■■■■■■ 1 0.12%\n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 0.50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 0.38%\n" *
" └ ┘ "
" ┌ ┐ \n" *
" 3 ┤■■■■■■■■■ 1 12%\n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 38%\n" *
" └ ┘ "

plot5 =
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 0.50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 0.38%\n" *
" 3 ┤■■■■■■■■■ 1 0.12%\n" *
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 38%\n" *
" 3 ┤■■■■■■■■■ 1 12%\n" *
" └ ┘ "

plot6 =
Expand All @@ -115,18 +119,18 @@ Base.hash(::A) = rand(UInt)
" └ ┘ "

plot7 =
" Tally \n" *
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 0.50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 0.38%\n" *
" 3 ┤■■■■■■■■■ 1 0.12%\n" *
" └ ┘ "
" Tally \n" *
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4 50%\n" *
" 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 3 38%\n" *
" 3 ┤■■■■■■■■■ 1 12%\n" *
" └ ┘ "

plot8 =
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 10 000 0.9999%\n" *
" 2 ┤ 1 0.0001%\n" *
" └ ┘ "
" ┌ ┐ \n" *
" 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 10 000 99.99%\n" *
" 2 ┤ 1 0.01%\n" *
" └ ┘ "

@test string(Tally.plot(T)) == plot1
@test string(Tally.plot(T, sortby = :value)) == plot1
Expand Down

2 comments on commit 3151401

@thofma
Copy link
Owner Author

@thofma thofma commented on 3151401 Dec 4, 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/73451

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.3.0 -m "<description of version>" 3151401cf3601133624c15e3352d76a095772edf
git push origin v0.3.0

Please sign in to comment.