Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a compact show for MvNormal. #1786

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Vilin97
Copy link

@Vilin97 Vilin97 commented Oct 3, 2023

Create an MvNormal distribution:

julia> using LinearAlgebra, Distributions

julia> dist = MvNormal(I(2)) # plaintext unchanged
MvNormal{Bool, PDMats.PDiagMat{Bool, Vector{Bool}}, FillArrays.Falses{1, Tuple{Base.OneTo{Int64}}}}(
dim: 2
μ: Zeros{Bool}(2)
Σ: Bool[1 0; 0 1]
)

Before:

julia> "$dist"
"MvNormal{Bool, PDMats.PDiagMat{Bool, Vector{Bool}}, FillArrays.Falses{1, Tuple{Base.OneTo{Int64}}}}(\ndim: 2\nμ: Zeros{Bool}(2)\nΣ: Bool[1 0; 0 1]\n)\n"

After:

julia> "$dist" # nice compact representation
"MvNormal(Zeros{Bool}(2), Bool[1 0; 0 1])"

@codecov-commenter
Copy link

codecov-commenter commented Oct 3, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Files Coverage Δ
src/multivariate/mvnormal.jl 73.59% <50.00%> (-4.39%) ⬇️

... and 133 files with indirect coverage changes

📢 Thoughts on this report? Let us know!.

@Vilin97
Copy link
Author

Vilin97 commented Oct 3, 2023

@devmotion , I'm not sure who is the right person to ping, sorry.
Let me know if this is a kind of change that you would be open to. This closes my issue #1785.

Comment on lines +250 to +251
Base.show(io::IO, d::MvNormal) =
print(io, "MvNormal($(d.μ), $(d.Σ))")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a show_oneline implementation in Distributions for compact representation. IMO it should actually be the default for show(io, dist), and only show(io, ::MIME"text/plain", dist) should use the current path which returns a one- or multiline representation depending on the number of parameters.

So I wonder what the implications of changing

show(io::IO, d::Distribution) = show(io, d, fieldnames(typeof(d)))
to

show(io::IO, d::Distribution) = show_oneline(io, d, fieldnames(typeof(d)))
show(io::IO, ::MIME"text/plain", d::Distribution) = show(io, d, fieldnames(typeof(d)))

would be.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show_oneline looks messy. Compare the two:

julia> "$dist" # show_oneline
"MvNormal{Bool, PDMats.PDiagMat{Bool, Vector{Bool}}, FillArrays.Falses{1, Tuple{Base.OneTo{Int64}}}}(dim=2, μ=Zeros{Bool}(2), Σ=Bool[1 0; 0 1])"

julia> "$dist" # the new show method
"MvNormal(Zeros{Bool}(2), Bool[1 0; 0 1])"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe the type parameters should be omitted in show_oneline. Additionally, I don't think you'd want to add the dim output to MvNormal, the vanilla show_oneline with the fields should be sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants