-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from JuliaStats/dh/show2
Improved implementation of `show` for distributions
- Loading branch information
Showing
3 changed files
with
67 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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,48 +1,71 @@ | ||
function show(io::IO, d::Distribution) | ||
@printf io "%s distribution\n" typeof(d) | ||
for parameter in typeof(d).names | ||
if isa(d.(parameter), AbstractArray) | ||
param = string(ucfirst(string(parameter)), | ||
":\n", | ||
d.(parameter), | ||
"\n") | ||
else | ||
param = string(ucfirst(string(parameter)), | ||
": ", | ||
d.(parameter), | ||
"\n") | ||
|
||
|
||
# the name of a distribution | ||
# | ||
# Generally, this should be just the type name, e.g. Normal. | ||
# Under certain circumstances, one may want to specialize | ||
# this function to provide a name that is easier to read, | ||
# especially when the type is parametric. | ||
# | ||
distrname(d::Distribution) = string(typeof(d)) | ||
|
||
show(io::IO, d::Distribution) = show(io, d, typeof(d).names) | ||
|
||
# For some distributions, the fields may contain internal details, | ||
# which we don't want to show, this function allows one to | ||
# specify which fields to show. | ||
# | ||
function show(io::IO, d::Distribution, pnames::(Symbol...)) | ||
# decide whether to use one-line or multi-line format | ||
# | ||
# Criteria: if total number of values is greater than 8, or | ||
# there are matrix-valued params, we use multi-line format | ||
# | ||
namevals = (Symbol, Any)[] | ||
multline = false | ||
tlen = 0 | ||
for (i, p) in enumerate(pnames) | ||
pv = d.(p) | ||
if !(isa(pv, Number) || isa(pv, (Number...)) || isa(pv, AbstractVector)) | ||
multline = true | ||
end | ||
print(io, param) | ||
tlen += length(pv) | ||
push!(namevals, (p, pv)) | ||
end | ||
if tlen > 8 | ||
multline = true | ||
end | ||
|
||
# call the function that actually does the job | ||
multline ? show_multline(io, d, namevals) : | ||
show_oneline(io, d, namevals) | ||
end | ||
|
||
function compact_show(io::IO, d::Distribution) | ||
print(io, typeof(d)) | ||
print(io, "( ") | ||
for parameter in typeof(d).names | ||
print(io, string(parameter)) | ||
print(io, "=") | ||
pv = d.(parameter) | ||
if isa(pv, AbstractVector) | ||
print(io, '[') | ||
if !isempty(pv) | ||
for i = 1 : length(pv)-1 | ||
print(io, pv[i]) | ||
print(io, ", ") | ||
end | ||
print(io, pv[end]) | ||
end | ||
print(io, ']') | ||
else | ||
print(io, pv) | ||
end | ||
print(io, " ") | ||
end | ||
print(io, ")") | ||
function show_oneline(io::IO, d::Distribution, namevals) | ||
print(io, distrname(d)) | ||
np = length(namevals) | ||
print(io, '(') | ||
for (i, nv) in enumerate(namevals) | ||
(p, pv) = nv | ||
print(io, p) | ||
print(io, '=') | ||
show(io, pv) | ||
if i < np | ||
print(io, ", ") | ||
end | ||
end | ||
print(io, ')') | ||
end | ||
|
||
function show(io::IO, d::UnivariateDistribution) | ||
compact_show(io, d) | ||
function show_multline(io::IO, d::Distribution, namevals) | ||
print(io, distrname(d)) | ||
println(io, "(") | ||
for (p, pv) in namevals | ||
print(io, p) | ||
print(io, ": ") | ||
println(io, pv) | ||
end | ||
println(io, ")") | ||
end | ||
|
||
|