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

Table-like printing of a named-tuple of vectors #40121

Open
pdeffebach opened this issue Mar 20, 2021 · 7 comments
Open

Table-like printing of a named-tuple of vectors #40121

pdeffebach opened this issue Mar 20, 2021 · 7 comments
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@pdeffebach
Copy link
Contributor

As a named-tuple of vectors (along with a vector of named tuples) has become the de-factor "minimal" table, I'm wondering if we can change the printing of a named-tuple of vectors to something slightly more table-like.

Consider the output of predict(m, X, interval = :confidence). Which returns a NamedTuple of Vectors

(prediction = [1.214285714285714, 2.8571428571428568, -2.0714285714285725], lower = [-0.925770635957734; -0.014151609177741165; -5.758060544780381], upper = [3.354342064529162; 5.728437323463455; 1.6152034019232366])

Hard to read. in R we get

r$> predict(m1, as.data.frame(x), interval = "confidence")                 
        fit         lwr      upr
1  1.214286 -0.81513839 3.243710
2  2.857143 -0.01289624 5.727182
3 -2.071429 -5.34377662 1.200919

Much easier to read.

I love named tuples of vectors (see my support here).

A package can't fix this without pretty severe type piracy, so I am filing the issue in base.

@JeffBezanson JeffBezanson added the display and printing Aesthetics and correctness of printed representations of objects. label Mar 22, 2021
@JeffBezanson
Copy link
Member

This has come up before --- #24627 --- apparently I was in favor of it :)

@pdeffebach
Copy link
Contributor Author

Ah cool. Maybe we can hook into the Matrix printing in some way to get the alignments right, etc.

@quinnj
Copy link
Member

quinnj commented Mar 25, 2021

At one point, I looked into having NamedTuple display switch over to the Dict display code if the output was larger than one line; I really like the "single line" printing of small NamedTuples, but agree that it's pretty unwieldy when they get too big. The problem w/ the Dict approach is that code is kind of split over a few different places and, IMO, not factored to be very re-usable.

@pdeffebach
Copy link
Contributor Author

Maybe a "general" solution would be to have a named tuple print like a table

a b c
-----
1 2 3

unless the table is too wide then print like a Dict. When printing as a table, arrays go downward.

Then the (a = 1, b = 2, c = 3) printing is reserved for repr.

@quinnj
Copy link
Member

quinnj commented Mar 25, 2021

The thing I like about going from (a = 1, b = 2, c = 3) to:

(
    a = 1,
    b = 2,
    c = 3
)

is you keep the same characters, it's just different whitespace, so it's still copy/pastable by default.

@StefanKarpinski
Copy link
Member

(Yes, except that in the multiline form the last one should also have a trailing comma :)

@pdeffebach
Copy link
Contributor Author

I've been working with named tuples recently and I noticed that all Parameters.jl does for their printing is call dump. I've been using dump instead of print for named tuples and it's been nice.

So i support Quinn's proposal here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

No branches or pull requests

4 participants