Skip to content

Misaligned unicode from tree.draw() in jupyter notebook #189

Closed

Description

@brianzhang01

Repro: https://github.com/brianzhang01/msprime_unicode/blob/master/msprime_unicode.ipynb

I have also observed this issue on Google Colab. The ASCII output is aligned fine (shown in notebook), and the SVG works too (not shown).

Activity

  1. brianzhang01 commented on May 27, 2019

    @brianzhang01
    MemberAuthor

    So apparently Unicode character width can either be 1 or 2 Latin characters of width. It seems like Jupyter notebook sets all Unicode characters to be rendered as width 2 by default. There may or may not be a way to alter this.
    https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html#unicode-formatting

  2. brianzhang01 commented on May 27, 2019

    @brianzhang01
    MemberAuthor

    One approach would be to add an option into the Unicode drawer that converts regular ASCII characters into full-width versions: https://stackoverflow.com/questions/4622357/how-to-control-padding-of-unicode-string-containing-east-asia-characters

  3. hyanwong commented on May 28, 2019

    @hyanwong
    Member

    I have had problems in the past with unicode font substitution. But I agree that having format="unicode" working in a Jupyter notebook would be nice, and a reason to keep the ascii format until this is working (although it's pretty easy to display SVG in a juptyer notebook, so I feel it isn't that strong an argument)

  4. jeromekelleher commented on May 28, 2019

    @jeromekelleher
    Member

    I think this is a browser/font issue - when I view notebooks you've linked to they all look fine. My guess is that on Macs the browser doesn't use a proper fixed-width font when it sees unicode chars, but does for some reason with ascii. I'm guessing that you're using a Mac @brianzhang01?

  5. jeromekelleher commented on May 28, 2019

    @jeromekelleher
    Member

    Here's what the trees look like for me on Vivaldi Linux:
    Screenshot from 2019-05-28 09-17-25

  6. jeromekelleher commented on May 28, 2019

    @jeromekelleher
    Member

    Firefox linux:
    firefox

  7. jeromekelleher commented on May 28, 2019

    @jeromekelleher
    Member

    Would you mind trying this on a few browsers on Macs @brianzhang01, so we can get a handle on how much of a problem this is? I know that these trees don't render properly on Android either.

    If it is a browser font issue, I don't think there's much we can do about it, and all we can do is recommend that people use SVG trees in browser environments.

  8. hyanwong commented on May 28, 2019

    @hyanwong
    Member

    If it works on Jerome's browser I strongly agree that it is likely to be a font substitution issue on Macs, which also occurs e.g. in the html docs and sometimes in the terminal too.

  9. brianzhang01 commented on May 28, 2019

    @brianzhang01
    MemberAuthor

    Yes, I am using Mac OS X Sierra. Agreed that this is likely a Mac font issue.

    The misalignment with the linked GitHub notebook is an issue for me on both Chrome and Safari. Interestingly, on Chrome and Safari, the trees here show up fine: https://msprime.readthedocs.io/en/stable/tutorial.html.

    Screen Shot 2019-05-28 at 2 10 11 PM

    If I copy either the GitHub or msprime documentation Unicode tree into Sublime Text, it's also misaligned there.

    Screen Shot 2019-05-28 at 2 07 49 PM

    Having a Markdown code environment makes things a bit better, but there are still some slight alignment issues.

    Screen Shot 2019-05-28 at 2 10 35 PM

    I will try to do a few more tests like iOS and Firefox on Mac OS X.

  10. jeromekelleher commented on May 28, 2019

    @jeromekelleher
    Member

    It is almost certainly a font spacing issue - sadly, proper monospaced fonts seem to be a rare thing outside of Linux. It'd be good if you could collect together a few more examples @brianzhang01, thanks.

  11. brianzhang01 commented on May 28, 2019

    @brianzhang01
    MemberAuthor

    I was at a public library with Windows (10 I think, but could be 8), so I tested there and also got issues that seemed identical to the ones on Mac. The GitHub notebook preview was off on Google Chrome, and the Markdown was off in Google Chrome and Internet Explorer. (IE wasn't able to show the GitHub notebook preview, since I think GitHub no longer supports IE.) Below are two photos of Chrome.

    IMG_1256

    IMG_1255

    I also get misalignment on iOS Safari; here is a screenshot.

    IMG_1257

  12. jeromekelleher commented on May 29, 2019

    @jeromekelleher
    Member

    Thanks very much @brianzhang01, this is helpful. I think these are all clearly monospace font issues. Keeping the ASCII tree drawer will make it a lot harder to add new features to the text rendering engine (and therefore much less likely to happen). I think the only thing we can do here really is change all the trees shown in the docs to SVG, and put in a note about this know browser alignment issue in the docs.

    Does this sounds reasonable to you?

  13. brianzhang01 commented on May 29, 2019

    @brianzhang01
    MemberAuthor

    A few notes:

    • The new SVG features sound exciting, and I already noticed the labelling of mutations.
    • I'll see if I can invest a bit of time into a fix for the Unicode alignment issue. I posted this link earlier, which I think is one reasonable approach. The issue is not really the existence of monospace fonts. It's that some East Asian Unicode characters require double-width to display. On Linux, perhaps only this subset of characters is shown in double-width, but on Mac and Windows, they show what are known as ambiguous width characters in double-width as well. The proposed solution is to add an option that changes all characters, such as digits 0-9 and the space character, to double-width.
    • I'm having trouble intuiting why it would be easier to add certain features to the Unicode version rather than the ASCII version. Is it more just the burden of maintaining two text formats?
    • I mentioned this earlier, but strangely, the trees in the docs (msprime, tskit) show up aligned for me in Chrome and Safari on Mac. So I'm not sure if we need to change the docs to SVG, but we could if we think it's a more compelling demo.
  14. jeromekelleher commented on May 29, 2019

    @jeromekelleher
    Member

    I'll see if I can invest a bit of time into a fix for the Unicode alignment issue.

    This would be great, thanks @brianzhang01. If the issue is to do with the ambiguous width characters then we should definitely try to fix it. I had always assumed that the issue was that spaces took up slightly less space than the other chars, leading to this misalignments.

    I'm having trouble intuiting why it would be easier to add certain features to the Unicode version rather than the ASCII version. Is it more just the burden of maintaining two text formats?

    Basically, yes. I'm in the middle of rewriting the Text drawing code at the moment, and it is greatly simplified by not having to worry about ascii (bytes) and unicode (str) issues. Using plain ASCII characters in unicode output, rather than the box drawing characters wouldn't be very hard though.

    I mentioned this earlier, but strangely, the trees in the docs (msprime, tskit) show up aligned for me in Chrome and Safari on Mac. So I'm not sure if we need to change the docs to SVG, but we could if we think it's a more compelling demo.

    This is weird. It would be good to get to the bottom of this problem!

  15. brianzhang01 commented on May 31, 2019

    @brianzhang01
    MemberAuthor

    I have found a way to produce Unicode strings that is aligned for me. The catch is that it may no longer align for users on Linux.

    https://github.com/brianzhang01/msprime_unicode/blob/master/tskit_unicode_fix1.ipynb
    https://github.com/brianzhang01/msprime_unicode/blob/master/tskit_unicode_fix1.pdf

    I have included a PDF of what it looks like for me, as it seems like we see some inconsistency across different operating systems. The idea is taken from this StackOverflow post, which is to convert the non-Unicode characters into Unicode characters of double width: https://stackoverflow.com/questions/4622357/how-to-control-padding-of-unicode-string-containing-east-asia-characters.

    There may also be a way to convert them into Unicode characters of ambiguous width, which I think may solve the problem for both Linux and non-Linux. On Linux, all the Unicode characters will then be displayed as single width, while on non-Linux, all the characters will get displayed as double width. Confusingly / strangely, Unicode double-width on my system lies somewhere between having 1.0x and 2.0x the same number of ASCII characters, as my GitHub notebook shows.

    If you read the rest of the linked StackOverflow post, the sections on unicodedata and the link to the kitchen library may also prove useful.

  16. brianzhang01 commented on May 31, 2019

    @brianzhang01
    MemberAuthor

    I already observe that my solution, when run in IPython within the terminal (on Mac OS X), yields misaligned results, due to the overcorrection. I would expect those on Linux to see the same when they view my Jupyter notebook in GitHub.

    Screen Shot 2019-05-31 at 7 14 16 PM

  17. jeromekelleher commented on Jun 3, 2019

    @jeromekelleher
    Member

    We definitely need to keep things working well on the terminal and in standard text files, since this is the primary application for the text trees.

    As SVG trees work well in notebooks and browsers, I wonder if it's worth spending time on this? What's your use-case for using the text trees in the notebook rather than SVGs?

  18. brianzhang01 commented on Jun 5, 2019

    @brianzhang01
    MemberAuthor

    You're right, I think SVG trees would be the way to go in notebooks, especially with the features on the way. However, if we were to choose between ASCII or Unicode for a text format, I think the slightly nicer visual afforded by Unicode may not be worth all these alignment nightmares or the general hassle of dealing with Unicode strings. (Although maybe from your vantage point it's not that bad now that Python 2 has been dropped.)

    I think we can close this issue by adding some documentation that the problem exists for Unicode on some platforms. We can also recommend the fix that I have included in the GitHub Jupyter notebook, adding a function like the following to translate output from tree.draw(format="unicode") in the case that the platform displays the box characters in full width.

    # Thanks to https://stackoverflow.com/a/4632373/
    def unicode_reformat(input):
        SPACE = '\N{IDEOGRAPHIC SPACE}'
        EXCLA = '\N{FULLWIDTH EXCLAMATION MARK}'
        TILDE = '\N{FULLWIDTH TILDE}'
        west = ''.join(chr(i) for i in range(ord(' '),ord('~')))
        east = SPACE + ''.join(chr(i) for i in range(ord(EXCLA),ord(TILDE)))
        full = str.maketrans(west,east)
        return input.translate(full)
    

    Here are a few more links:

    https://www.unicode.org/Public/11.0.0/ucd/EastAsianWidth.txt (codes for all Unicode characters and how they show up in East Asian width)
    https://www.unicode.org/reports/tr11/
    tqdm/tqdm#259 (an example of these issues showing up in a Terminal)

  19. added 3 commits that reference this issue on Jun 12, 2019
    5f75a4a
    9ae4b01
    19e2dc8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions