Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simplify definition of mathtext symbols & correctly end tokens in mathtext parsing#22950

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tacaswell merged 3 commits intomatplotlib:mainfromanntzer:mathtextsymbols
Aug 3, 2022

Conversation

anntzer
Copy link
Contributor

PR Summary

First commit: Simplify definition of mathtext symbols.

Use a single regex that handles both single_symbol (a single character)
and symbol_name (\knowntexsymbolname), and also slightly simplify the
"end-of-symbol-name" regex.

This parsing element comes up extremely often, and removing one
indirection layers shaves off ~3-4% off drawing all the current mathtext
tests, i.e.

MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'

Second commit: Correctly end tokens in mathtext parsing.

This avoids parsing\sinx as\sin x (it now raises an error
instead), and removes the need foraccentprefixed (because\doteq
is treated as a single token now, instead of\dot{eq}). This also
means that\doteq (and friends) are now correctly treated as relations
(per_relation_symbols, thus changing the spacing around them); hence
then change in baseline images. Only keep thex \doteq y baseline
(and adjust the test string to undo the spacing), to avoid regen'ing
baselines.

Also shaves ~2% off drawing all the current mathtext tests, i.e.

MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'

(including adjustment for the two removed test cases), probably because
accentprefixed was previously extremely commonly checked, being at the
top of the placeable list; however, performance wasn't really the main
goal here.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (andpytest passes).
  • IsFlake 8 compliant (installflake8-docstrings and runflake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).

Use a single regex that handles both single_symbol (a single character)
and symbol_name (\knowntexsymbolname), and also slightly simplify the
"end-of-symbol-name" regex.

This parsing element comes up extremely often, and removing one
indirection layers shaves off ~3-4% off drawing all the current mathtext
tests, i.e.

MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'

@anntzeranntzer added Performance topic: text/mathtext PR: bugfixPull requests that fix identified bugs labelsMay 1, 2022
@oscargus
Copy link
Member

oscargus commentedMay 14, 2022
edited
Loading

Considering the doc-build failure: maybe one should also add some test in the main test suite for accents of the types\",\~, etc? As far as I can see, only\acute and so on are tested.

@anntzer
Copy link
ContributorAuthor

Ah, good catch, fixed and added test.

@tacaswell
Copy link
Member

I would prefer if we re-gen the test images here.

I think the'\ddots' symbol was one of the glyphs that was flat out wrong but still passing image tests with a tolerance.

@tacaswelltacaswell added this to thev3.6.0 milestoneMay 17, 2022
@anntzer
Copy link
ContributorAuthor

Actually this reveals another bug: I deleted the ddots (etc.) test because they are now recognized as relation operators and extra spaces got added around them, but such spaces should actually not be there because the test string isr'$\dotplus$ $\doteq$ $\doteqdot$ $\ddots$' i.e. the relation operator is at (both) extremities of the dollar-enclosed part, in which case tex does not introduce a space. For a simpler example, considerfigtext(.5, .5, "a$=b$", size=24) with or without usetex. With usetex, there's no space between "a" and "=" (whereas there's one between "=" and "b"), whereas mathtext introduces a space on both sides of the "=".

Fixing this bug (which probably involves reusing something like the "Binary operators at start of string should not be spaced" part of the code indef symbol()) should allow keeping the old ddots test, so I'll look into that...

@anntzeranntzer marked this pull request as draftMay 17, 2022 21:04
@anntzer
Copy link
ContributorAuthor

I went for the easier path of just adding\hspace{-0.2} as needed to fix the images.

@anntzeranntzer marked this pull request as ready for reviewJune 11, 2022 21:49
Use a single regex that handles both single_symbol (a single character)and symbol_name (`\knowntexsymbolname`), and also slightly simplify the"end-of-symbol-name" regex.This parsing element comes up extremely often, and removing oneindirection layers shaves off ~3-4% off drawing all the current mathtexttests, i.e.```MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'```
@jklymak
Copy link
Member

@tacaswell can you re-review to be sure your concerns are met?

@jklymakjklymak removed the request for review fromtacaswellJune 30, 2022 09:28
@jklymak
Copy link
Member

@anntzer it looks like you have still removed a bunch of baseline images. Are we sure those are still tested?

@anntzer
Copy link
ContributorAuthor

Yes I am sure, this is only removing test 77, which checks that "accentprefixed" commands are correctly interpreted (e.g. \doteq is not interpreted as \dot eq), but this is essentially also covered by ther'$\dotplus$ $\doteq$ $\doteqdot$ $\ddots$' just above, and by the\sinx test I added below to check, more generally, that spaces (or braces) are required after operators now (I also added a similar\dota test for good measure).

@tacaswell
Copy link
Member

It it worth an API change note on the spacing?

@anntzer
Copy link
ContributorAuthor

Changelog entry added, also added dotminus to the spaced operators as it was clearly missing before.
Also moved dotplus and dotminus from "relational operators" to "binary operators" (see e.g.https://mirrors.ircam.fr/pub/CTAN/macros/unicodetex/latex/unicode-math/unimath-symbols.pdf), this actually has no effect on our rendering because we use the same amount of space for both even though that is a simplification over tex's algorithm (https://tex.stackexchange.com/a/38986).

This avoids parsing `\sinx` as `\sin x` (it now raises an errorinstead), and removes the need for `accentprefixed` (because `\doteq`is treated as a single token now, instead of `\dot{eq}`).  This alsomeans that `\doteq` (and friends) are now correctly treated as relations(per `_relation_symbols`, thus changing the spacing around them); hencethen change in baseline images.  Adjust test strings accordingly to undothe spacing, to avoid regen'ing baselines.Also shaves ~2% off drawing all the current mathtext tests, i.e.```MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'```(including adjustment for the removed test case), probably becauseaccentprefixed was previously extremely commonly checked, being at thetop of the placeable list; however, performance wasn't really the maingoal here.
@tacaswelltacaswell merged commita33a6fd intomatplotlib:mainAug 3, 2022
@anntzeranntzer deleted the mathtextsymbols branchAugust 3, 2022 21:57
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tacaswelltacaswelltacaswell approved these changes

@oscargusoscargusoscargus approved these changes

Assignees
No one assigned
Labels
PerformancePR: bugfixPull requests that fix identified bugstopic: text/mathtext
Projects
None yet
Milestone
v3.6.0
Development

Successfully merging this pull request may close these issues.

5 participants
@anntzer@oscargus@tacaswell@jklymak@QuLogic

[8]ページ先頭

©2009-2025 Movatter.jp