This repository was archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Tweak plotting to collect outliers into a single bin; drop UnicodePlots dependency #13
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a7af86
Add `simpleunicodehistogram`
ericphanson 03ccac4
tweak `simpleunicodehistogram` for outliers
ericphanson 9d70bbf
rename things, add configuration, fix tests, update readme
ericphanson e1cca72
use Julia 1.0 compatible syntax
ericphanson 83f0367
fix omission of minimum
ericphanson e05bf53
bump version
ericphanson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
name = "BenchmarkHistograms" | ||
uuid = "a80a1652-aad8-438d-b80b-ecb1a674e33b" | ||
authors = ["Eric Hanson <[email protected]> and contributors"] | ||
version = "0.1.1" | ||
version = "0.2.0" | ||
|
||
[deps] | ||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" | ||
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228" | ||
|
||
[compat] | ||
BenchmarkTools = "0.7, 1.0" | ||
UnicodePlots = "1.3" | ||
julia = "1" | ||
|
||
[extras] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Modified from https://github.com/JuliaCI/BenchmarkTools.jl/pull/180#issuecomment-711128281 by @brenhinkeller | ||
|
||
const BLOCKS = [" ","▏","▎","▍","▌","▋","▊","▉","█","█"] | ||
|
||
function simple_unicode_histogram(io::IO, x::AbstractArray; | ||
nbins::Integer=ceil(Int, log2(length(x))+1), | ||
plot_width::Integer=30, show_counts::Bool=true, | ||
outlier_quantile = 0.999, | ||
xlabel="", ylabel="") | ||
# Find bounds. Our naive attempt is to use equal width | ||
# bins from the minimum to the maximum. | ||
l, M = extrema(x) | ||
# our lower bounds are exclusive, so we want to be sure to get the min | ||
l = prevfloat(l) | ||
|
||
# Now, we check: if we don't have some big outliers, we'd expect | ||
# the 99.9 percentile, `Q`, to be within a few bins of the maximum. | ||
# Here, we choose 2. If it is not, then we decide that indeed | ||
# there are outliers. We will instead divide the range from | ||
# the minimum to `Q` equally with `nbins-1` bins, and then reserve | ||
# the last bin to hold everything greater than `Q`. | ||
Q = quantile(x, outlier_quantile) | ||
initial_dx = (M - l) / nbins | ||
truncate = M - Q > 2*initial_dx | ||
|
||
# our "upper bound" | ||
u = truncate ? Q : M | ||
|
||
# Fill histogram | ||
hist_counts = fill(0, nbins) | ||
dx = truncate ? (u - l) / (nbins - 1) : initial_dx | ||
for xi in x | ||
index = ceil(Int, (xi - l) / dx) | ||
if 1 <= index <= nbins | ||
hist_counts[index] += 1 | ||
else | ||
hist_counts[end] += 1 | ||
end | ||
end | ||
|
||
if truncate | ||
bin_edges = [range(l;stop=u,length=nbins); M] | ||
else | ||
bin_edges = range(l;stop=u,length=nbins+1) | ||
end | ||
|
||
# Print the histogram | ||
d = ceil(Int, -log10(u-l))+1 | ||
scale = plot_width/maximum(hist_counts) | ||
lower_labels = string.(round.(bin_edges[1:end-1], digits=d+ceil(Int,log10(nbins)-1))) | ||
upper_labels = string.(round.(bin_edges[2:end], digits=d+ceil(Int,log10(nbins)-1))) | ||
longest_lower = maximum(length.(lower_labels)) | ||
longest_upper = maximum(length.(upper_labels)) | ||
!isempty(ylabel) && println(io, ylabel, "\n") | ||
for i=1:nbins | ||
nblocks = hist_counts[i] * scale | ||
block_string = repeat("█", floor(Int, nblocks)) * BLOCKS[ceil(Int,(nblocks - floor(nblocks))*8)+1] | ||
print(io, " (", lower_labels[i], " "^(longest_lower - length(lower_labels[i]))) | ||
print(io, " - ", upper_labels[i], " "^(longest_upper - length(upper_labels[i])), "] ") | ||
printstyled(io, block_string; color=:green) | ||
if show_counts | ||
print(io, hist_counts[i]) | ||
end | ||
println(io) | ||
end | ||
isempty(xlabel) || println(io, "\n", " "^max(plot_width ÷2 + 6 - length(xlabel)÷2, 0), xlabel) | ||
return nothing | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the outlier change is especially nice here, where on my m1 macbook, I had such severe outliers that @MasonProtter had to generate the plot in order for it to look Gaussian (#6). With the new outlier bin, I can plot it myself 😄