Skip to content

Commit 7030cfe

Browse files
committed
add forgotten new file
1 parent a4a0abf commit 7030cfe

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ext/UnivariateFiniteDisplayExt.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module UnivariateFiniteDisplayExt
2+
3+
const MAX_NUM_LEVELS_TO_SHOW_BARS = 12
4+
5+
using CategoricalDistributions
6+
import CategoricalArrays
7+
import UnicodePlots
8+
import ScientificTypes.Finite
9+
10+
# The following is a specialization of a `show` method already in /src/ for the common
11+
# case of `Real` probabilities.
12+
function Base.show(io::IO, mime::MIME"text/plain",
13+
d::UnivariateFinite{<:Finite{K},V,R,P}) where {K,V,R,P<:Real}
14+
show_bars = false
15+
if K <= MAX_NUM_LEVELS_TO_SHOW_BARS &&
16+
all(>=(0), values(d.prob_given_ref))
17+
show_bars = true
18+
end
19+
show_bars || return show(io, d)
20+
s = support(d)
21+
x = string.(CategoricalArrays.DataAPI.unwrap.(s))
22+
y = pdf.(d, s)
23+
S = d.scitype
24+
plt = UnicodePlots.barplot(x, y, title="UnivariateFinite{$S}")
25+
show(io, mime, plt)
26+
end
27+
28+
end

0 commit comments

Comments
 (0)