Skip to content

Commit 92afbd1

Browse files
authored
Merge pull request #51 from JuliaAI/UnicodePlots-as-weakdep
Make UnicodePlots a weak dependency (for julia > 1.9)
2 parents 5c62c4e + d75996e commit 92afbd1

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
version:
2020
- '1.6'
2121
- '1' # automatically expands to the latest stable 1.x release of Julia.
22-
- 'nightly'
22+
- '~1.9.0-0'
2323
os:
2424
- ubuntu-latest
2525
arch:

Project.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1212
ScientificTypes = "321657f4-b219-11e9-178b-2701a2544e81"
1313
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
1414

15+
[weakdeps]
16+
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
17+
18+
[extensions]
19+
UnivariateFiniteDisplayExt = "UnicodePlots"
20+
1521
[compat]
1622
CategoricalArrays = "0.9, 0.10"
1723
Distributions = "0.25"
@@ -26,6 +32,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2632
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
2733
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2834
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
35+
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
2936

3037
[targets]
31-
test = ["Random", "StableRNGs", "Test", "FillArrays"]
38+
test = ["FillArrays", "Random", "StableRNGs", "Test", "UnicodePlots"]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ this package is the class pool of a `CategoricalArray`:
3333
using CategoricalDistributions
3434
using CategoricalArrays
3535
import Distributions
36+
import UnicodePlots # for optional pretty display
3637
data = ["no", "yes", "no", "maybe", "maybe", "no",
3738
"maybe", "no", "maybe"] |> categorical
3839
julia> d = Distributions.fit(UnivariateFinite, data)

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

src/CategoricalDistributions.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ using OrderedCollections
1313
using CategoricalArrays
1414
import Missings
1515
using Random
16-
using UnicodePlots
1716

1817
const Dist = Distributions
19-
const MAX_NUM_LEVELS_TO_SHOW_BARS = 12
2018

2119
import Distributions: pdf, logpdf, support, mode
2220

@@ -35,4 +33,9 @@ export pdf, logpdf, support, mode
3533
# re-export from ScientificTypesBase:
3634
export Multiclass, OrderedFactor
3735

36+
# for julia < 1.9
37+
if !isdefined(Base, :get_extension)
38+
include("../ext/UnivariateFiniteDisplayExt.jl")
39+
end
40+
3841
end

src/methods.jl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,6 @@ end
8282
Base.show(io::IO, mime::MIME"text/plain",
8383
d::UnivariateFinite) = show(io, d)
8484

85-
# in common case of `Real` probabilities we can do a pretty bar plot:
86-
function Base.show(io::IO, mime::MIME"text/plain",
87-
d::UnivariateFinite{<:Finite{K},V,R,P}) where {K,V,R,P<:Real}
88-
show_bars = false
89-
if K <= MAX_NUM_LEVELS_TO_SHOW_BARS &&
90-
all(>=(0), values(d.prob_given_ref))
91-
show_bars = true
92-
end
93-
show_bars || return show(io, d)
94-
s = support(d)
95-
x = string.(CategoricalArrays.DataAPI.unwrap.(s))
96-
y = pdf.(d, s)
97-
S = d.scitype
98-
plt = barplot(x, y, title="UnivariateFinite{$S}")
99-
show(io, mime, plt)
100-
end
101-
10285
show_prefix(u::UnivariateFiniteArray{S,V,R,P,1}) where {S,V,R,P} =
10386
"$(length(u))-element"
10487
show_prefix(u::UnivariateFiniteArray) = join(size(u),'x')

test/methods.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ end
268268
# @test v ≈ v_close
269269
end
270270

271+
function displays_okay(v) # `v` a "probability" vector
272+
d = UnivariateFinite(v, pool=missing);
273+
str = sprint(show, "text/plain", d)
274+
contains(str, "UnivariateFinite{Multiclass{$(length(v))}}")
275+
end
276+
277+
@testset "display" begin
278+
@test displays_okay([0.3, 0.7])
279+
@test displays_okay([5 + 3im, 4 - 7im])
280+
using UnicodePlots
281+
@test displays_okay([0.3, 0.7])
282+
@test displays_okay([5 + 3im, 4 - 7im])
283+
end
284+
271285
end # module
272286

273287
true

0 commit comments

Comments
 (0)