-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
path randomness is repetitive (sketch RNG seeded to 0 for each path anew) #13479
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
Comments
So, originally, when this feature was implemented, we didn't seed the PRNG.
But, some users complained, and we were having trouble unit-testing it. So,
we started seeding the PRNG. Now, I could have sworn we left in a way to
turn off the seeding, but I can't find anything about it at the moment.
…On Thu, Feb 21, 2019 at 7:05 AM eudoxos ***@***.***> wrote:
Bug report
*Sketch transformation has identical pseudo-random output for identical
inputs*
The sketch transformation (such as used in xkcd) produces exactly the same
randomized result if the same path is plotted repeatedly. This is visible
especially in the legend, where the rectangle patch is repeated for each
item. I exaggerated randomness and scale to make the effect better visible.
As far as I can see, the culprit is in the built-in RNG which gets
re-seeded to 0 for each Sketch instantionation:
https://github.com/matplotlib/matplotlib/blob/e02f2c5dca0bfd6a83cd0d41db31e48dea9fd14f/src/path_converters.h#L941
.
import matplotlib.pyplot as pltwith plt.xkcd(randomness=40,scale=1):
import matplotlib
from matplotlib import gridspec
pat,txt=plt.pie([10,20,30,40],wedgeprops={'edgecolor':'black'})
plt.legend(pat,['first','second','third','fourth'],loc='best')
*Actual outcome*
[image: image]
<https://user-images.githubusercontent.com/1029876/53167574-90cbe380-35d8-11e9-93f2-596f6771adbf.png>
zoom on the legend:
[image: image]
<https://user-images.githubusercontent.com/1029876/53167608-ac36ee80-35d8-11e9-86c7-d2745ecfb932.png>
*Expected outcome*
Shapes are distored randomly, e.g. each rectangle in the legend is
different.
*Matplotlib version*
- Operating system: Ubuntu 18.04
- Matplotlib version: 2.2.2
- Matplotlib backend (print(matplotlib.get_backend())): TkAgg
- Python version: 3.6.7
- Jupyter version (if applicable): [none]
- Other libraries:
installation: default packaged matplotlib installation
(python3-matplotlib).
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#13479>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AARy-KCz6BKILcCjw7V4c7bW9ZWmFnbFks5vPouKgaJpZM4bHSql>
.
|
Ideally, we would seed once per figure. That would make figures reproducible, but repeated elements within the figure random. Didn‘t check the code if that‘s easily possible. |
Note that if you seed once per figure the shape of a patch would depend on how many patches you have drawn beforehands. |
Yes, but if I create the same figure with the same code twice, it's the same. |
seeding once would be good, and grabbing the python seed would be even better if that is at all possible. |
Would it be acceptable to have the seed as a static variable inside the Sketch class, increment it every time its ctor is called (so that it is seeded with 0 for the first instance, 1 for the next one etc), plus somehow expose a python function to reset the seed to 0, which would be called from Figure ctor? |
As pointed out above, I would not consider this acceptable. There needs to be a way to ensure that a shape is the same, independent of how many other shapes there are drawn before in that figure. |
If you look at the legend which I posted above, it is exactly this shape is the same which makes it so ugly and computerish, that's the subject of the issue. If there is no general agreement, there must be a setting (in rcParams perhaps?) for that. The options are (not all necessarily to implemented, I am just writing out what is theoretically possible):
What I reported (as an issue) is 1. To me at least, 2. is a compromise between per-figure reproducibility (e.g. for testing) and random appearance. |
The only option I see is to let matplotlib/lib/matplotlib/artist.py Line 609 in 1d7494f
that will need to be (converted to) a valid seed before passing onto the C level. The C class needs to take this further argument. |
I think @ImportanceOfBeingErnest has the correct solution. Then unti tests can set the seed in python, as they already do many times in the tests. |
Based on @ImportanceOfBeingErnest suggestion, I added passing the seed to related functions in this branch: https://github.com/matplotlib/matplotlib/compare/master...eudoxos:sketch-seed?expand=1 (WIP). For the next thing, how to make the seed change for each next instance of the PRNG? My idea is to use a special value It would look like following:
Any comments on that? |
This is what I am getting now, using the special seed value of -1, which means: use the last one and increment. There were a few gotchas but it seems to be working now. A question now is whether we should treat all seed numbers as the seed to use (which is all numbers right now, besides -1), or on the contrary, have all numbers as a beginning of evolving (increasing) seed series, and only one ( Resetting to the beginning for each figure not yet done. |
@eudoxos Are you interested to open a PR for adding the For the automatic change of the seed, I would need to think more about it. |
@timhoffm just put |
@eudoxos sorry you never got a response, any chance you're still interested in adding a seed parameter? |
Hi, check out the branch https://github.com/eudoxos/matplotlib/tree/sketch-seed . I don't have resources to re-apply to the current matplotlib (though I think it could be quite straightforward), test, document etc to push it all way to merging. Please feel welcome to build upon what I did. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Bug report
Sketch transformation has identical pseudo-random output for identical inputs
The sketch transformation (such as used in xkcd) produces exactly the same randomized result if the same path is plotted repeatedly. This is visible especially in the legend, where the rectangle patch is repeated for each item. I exaggerated randomness and scale to make the effect better visible.
As far as I can see, the culprit is in the built-in RNG which gets re-seeded to 0 for each Sketch instantionation:
matplotlib/src/path_converters.h
Line 941 in e02f2c5
Actual outcome

zoom on the legend:
Expected outcome
Shapes are distored randomly, e.g. each rectangle in the legend is different.
Matplotlib version
print(matplotlib.get_backend())
): TkAgginstallation: default packaged matplotlib installation (python3-matplotlib).
The text was updated successfully, but these errors were encountered: