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

Display sparse matrices by showing their structure with braille patterns #33821

Merged
merged 31 commits into from
May 22, 2020

Conversation

goggle
Copy link
Contributor

@goggle goggle commented Nov 12, 2019

This PR closes #30587.

I have created some screenshots of this braille pattern printing. You can have a look at them here.

@ViralBShah ViralBShah added the sparse Sparse arrays label Nov 12, 2019
@dkarrasch dkarrasch added the display and printing Aesthetics and correctness of printed representations of objects. label Nov 12, 2019
@goggle goggle force-pushed the 30587-spy-printing-sparse-matrices branch from ecafd6f to 4d0c386 Compare November 12, 2019 15:32
Copy link
Member

@dkarrasch dkarrasch left a comment

Choose a reason for hiding this comment

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

This is so beautiful! I have just a minor comment below. I found the scaling steps hard to digest, so you may wish to consider leaving some more comments on the rationale, in case someone wants to debug this or change behavior slightly later.

stdlib/SparseArrays/src/sparsematrix.jl Outdated Show resolved Hide resolved
@dkarrasch
Copy link
Member

Ok, so in case the matrix is really really large, the "downscaling" may lump nonzeros together, which is somewhat clear and fine, but I didn't get that from the code alone.

@dkarrasch
Copy link
Member

@goggle Could you please rebase your branch? One failing test has been resolved in the meantime. I'd say we merge when all (except the windows "hard repl kill" thing) pass.

@goggle goggle force-pushed the 30587-spy-printing-sparse-matrices branch from 57409e1 to 1774cbf Compare November 15, 2019 08:30
@goggle goggle force-pushed the 30587-spy-printing-sparse-matrices branch 7 times, most recently from e671817 to c8aa9de Compare November 23, 2019 13:02
print(io, "\n \u22ee")
_format_line.(s2:e2, cols2, padr, padc)
function Base.show(io::IOContext, S::AbstractSparseMatrixCSC)
if size(S, 1) <= 20 && size(S, 2) <= 20
Copy link
Member

@mbauman mbauman Nov 26, 2019

Choose a reason for hiding this comment

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

Use displaysize to determine this cutoff? It'll need a very rough estimate of how long each column will be once printed, but we can at least take a shot.

Copy link
Contributor Author

@goggle goggle Nov 26, 2019

Choose a reason for hiding this comment

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

This threshold (currently set to 20) was introduced because displaying small sparse matrices with braille patterns isn't really helpful. If the matrix is still too big to be printed on the screen, the print_matrix function already wraps it. That's in my opinion a better solution than displaying such small matrices with braille patterns. Also note that making this dependent on displaysize would imply that two users would see a sparse matrix sprandn(10, 10, 0.4) differently depending on their screen size. I'm not sure if this is a good idea.

Maybe some other opinions on this would be helpful?

Copy link
Member

Choose a reason for hiding this comment

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

Right, the cutoff isn't determined by the size of the display, it's determined by the minimum size for braille output to be comprehensible.

I picked 20 out of a hat, however, so one might want to experiment a bit to see how things look. Maybe show the braille output in a comment for a bunch of small matrix sizes and we can judge how they look?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here are some examples of a structured n x n matrix in different sizes printed with braille patterns:
image

I think 20 is a reasonable choice for that cutoff value.

Copy link
Member

Choose a reason for hiding this comment

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

16x16 would probably be fine too.

Copy link
Member

Choose a reason for hiding this comment

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

We might change the condition for non-braille printing to

if max(size(S)...) < 16 && !get(io, :compact, false)

so that a compact IOContext ensures braille display.

@stev47
Copy link
Contributor

stev47 commented Nov 27, 2019

Monospaced font support seems to be very poor for braille characters:

$ fc-list :mono | wc -l
144
$ fc-list :mono:charset=2800 | wc -l
3

I hope it's not too late, but maybe we should think twice before using braille as default.

@stevengj
Copy link
Member

stevengj commented Nov 27, 2019

Monospaced font support seems to be very poor for braille characters

As long as the text rendering supports fallback fonts (i.e. everything except for the Windows console), it should be fine.

It would be good to check if the Windows console displays braille, though.

@goggle
Copy link
Contributor Author

goggle commented Nov 28, 2019

If anybody with access to a Windows console could paste this code

println([join(Char.(x))*"\n" for x in [x:x+15 for i=0:11 for x=10304+16*i]]...)

into a Julia REPL session and post a screenshot of the output, we should be able to see if Windows displays braille patterns correctly.

@stevengj
Copy link
Member

I tried in Windows 8 (via a VMware emulator), and the results were not good:
image
The Windows console strikes again. If only we had #7267

@davidanthoff
Copy link
Contributor

This is what I get for the new Windows Terminal:
image

@stevengj
Copy link
Member

stevengj commented Nov 29, 2019

Sure, the new Terminal finally supports fallback fonts. But it’s not the default console, and it will be years before it is the default for most of the installed base, so it’s not relevant at the moment.

@goggle
Copy link
Contributor Author

goggle commented Nov 29, 2019

@stevengj What about the code point '\u22c5'? Can this be displayed correctly in the traditional Windows console? I'm asking because this character is used by print_matrix to display the empty values in all the other structured matrices:
image

Edit: And I guess the empty braille pattern '\u2800' also gets displayed as a question mark on the traditional Windows console?

@stevengj
Copy link
Member

In the Windows 8 console, U+22C5 is fine but U+2800 is not supported:
image

@ViralBShah
Copy link
Member

Bump. What remains to get this merged?

@goggle
Copy link
Contributor Author

goggle commented Jan 4, 2020

@ViralBShah We need to make sure that older Windows systems can display braille Unicode points correctly, so there is probably no other way than to have #7267 solved first.

@StefanKarpinski
Copy link
Member

Would it be possible to just use individual dots on Windows? Or just keep the old code on Windows.

@ViralBShah
Copy link
Member

Either of the things @StefanKarpinski mentions would be fine for Windows.

Is there a way to know if the terminal can display the unicode correctly and automatically use the new braille scheme, falling back to single dot or current behaviour if it is not a nice terminal?

@goggle
Copy link
Contributor Author

goggle commented Jan 13, 2020

I'm still not convinced that it is really not possible to display these braille characters on older Windows systems (Windows 7 and 8). I have found this: Here they suggest to use the command

chcp 65001

and to use Lucida console fonts.
Maybe someone with access to Windows 7/8 could try this?

Is there a way to know if the terminal can display the unicode correctly and automatically use the new braille scheme, falling back to single dot or current behaviour if it is not a nice terminal?

I don't know, I've asked myself the same. I assume it is not trivial... (but I could be wrong).

Would it be possible to just use individual dots on Windows? Or just keep the old code on Windows.

That would be possible if we are able to check if these braille characters are displayed correctly. Windows systems that provide PowerShell do not have any problems, so we shouldn't include these.
But in general, I don't think it is a good idea to display data structures differently depending on the underlying system. It adds an unnecessary layer of complexity, makes debugging more difficult, etc. If my first suggestion does not solve the issue, I would really prefer to have #7267 solved first. As @mintty pointed out, it should now be possible to include mintty without patching. So it would be great if someone with knowledge on the Julia packaging system could try to include this! But I have to say that I have absolutely no idea how much work that would be...

@ViralBShah
Copy link
Member

ViralBShah commented Jan 16, 2020

I posted a solution to try out mintty in #7267. Would be nice if anyone with Windows can try it out.

@stevengj
Copy link
Member

Fixed merge conflicts.

Can we merge this when green, since it's been decided that Windows console users are no longer a concern?

@goggle
Copy link
Contributor Author

goggle commented May 22, 2020

Something is wrong with the tests, they are pending for over 14 hours now...
Is there a way to restart them all?

@ViralBShah ViralBShah closed this May 22, 2020
@ViralBShah ViralBShah reopened this May 22, 2020
@stevengj stevengj merged commit a5e032e into JuliaLang:master May 22, 2020
simeonschaub pushed a commit to simeonschaub/julia that referenced this pull request Aug 11, 2020
…rns (JuliaLang#33821)

* Display sparse matrices by showing their structure with braille patterns

* Adopt test which involves showing of sparse matrix

* Adopt docstrings

* Call `rowvals(S)` only once

* Extend code documentation

* Simplify calculation of `scaleHeight` and `scaleWidth`

* Make `brailleBlocks` a `const`

* Initialize `brailleGrid` with `UInt16(10240)`

* Let the compiler do the conversion of `\n`

* Improve printing to `io`

* Unwrap the helper functions directly into the loop

* Show small sparse matrix in traditional manner

* Add tests for `isstored`

* Adapt tests for `show`

* Adapt doctests

* Use `m` and `n` instead of `size` calls

* Initialize `scaleHeight` and `scaleWidth` in `else` clause

* Reenable commented test

* Declare type of `maxHeight` and `maxWidth`

* Do not print leading newline in `_show_with_braille_patterns`

* Reenable test for issue 30589

* Shorten type declarations of `maxHeight` and `maxWidth`

* Add `isstored` to Base

* Add `isstored` for sparse vectors

* Make use of Base function `isstored`

* Add `boundscheck` macro

* Clarify news

* Call `issorted` by `Base.issored`

* Use `Base.isstored` in tests

* Set cutoff value to print sparse matrices with braille to 16

Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
heliosdrm added a commit to JuliaDynamics/RecurrenceAnalysis.jl that referenced this pull request Dec 29, 2021
This is not convenient anymore, as Julia represents big sparse matrices with Braille patterns since v1.6 (JuliaLang/julia#33821). Actually, perhaps we could even use that representation for `AbstractRecurrenceMatrix`.
Datseris pushed a commit to JuliaDynamics/RecurrenceAnalysis.jl that referenced this pull request Dec 29, 2021
* Do not pirate `show` for sparse matrices

This is not convenient anymore, as Julia represents big sparse matrices with Braille patterns since v1.6 (JuliaLang/julia#33821). Actually, perhaps we could even use that representation for `AbstractRecurrenceMatrix`.

* update Project.toml
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. sparse Sparse arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

better printing of sparse matrices?
9 participants