Skip to content

BUG: GH17525 Function _get_standard_colors resets global random seed #17730

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

Merged
merged 14 commits into from
Oct 3, 2017

Conversation

cmazzullo
Copy link
Contributor

@codecov
Copy link

codecov bot commented Sep 30, 2017

Codecov Report

Merging #17730 into master will decrease coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17730      +/-   ##
==========================================
- Coverage   91.27%   91.23%   -0.04%     
==========================================
  Files         163      163              
  Lines       49766    49859      +93     
==========================================
+ Hits        45422    45490      +68     
- Misses       4344     4369      +25
Flag Coverage Δ
#multiple 89.03% <100%> (-0.02%) ⬇️
#single 40.25% <100%> (-0.15%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_style.py 74.28% <100%> (-0.25%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/util/_tester.py 29.41% <0%> (-9.48%) ⬇️
pandas/core/common.py 91.42% <0%> (-0.56%) ⬇️
pandas/core/indexes/multi.py 96.39% <0%> (-0.51%) ⬇️
pandas/core/config.py 87.7% <0%> (-0.39%) ⬇️
pandas/core/indexes/category.py 97.46% <0%> (-0.29%) ⬇️
pandas/core/groupby.py 92.04% <0%> (-0.2%) ⬇️
pandas/core/ops.py 91.76% <0%> (-0.13%) ⬇️
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 030e374...092c2d5. Read the comment docs.

@@ -114,8 +114,11 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
import random

def random_color(column):
rstate = random.getstate() # to avoid resetting the seed
Copy link
Member

Choose a reason for hiding this comment

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

Point to the issue number.

Copy link
Member

@gfyoung gfyoung Oct 1, 2017

Choose a reason for hiding this comment

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

Also, since you're changing the behavior, let's document it. A brief doc-string would be good here for future developers.

@gfyoung
Copy link
Member

gfyoung commented Oct 1, 2017

@cmazzullo : Also, you're definitely going to need tests.

@pep8speaks
Copy link

pep8speaks commented Oct 1, 2017

Hello @cmazzullo! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on October 03, 2017 at 08:06 Hours UTC

@cmazzullo
Copy link
Contributor Author

Okay, just added one.

@@ -114,8 +114,13 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
import random

def random_color(column):
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think random provides a context manger, maybe do this like:

rstate = random.getstate()
try:
    random.seed(...)
    return = [random...... ]
finally:
    random.setstate(rstate)

Copy link
Contributor

Choose a reason for hiding this comment

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

actually just use this

In [20]: rs = pandas.core.common._random_state()

In [21]: rs.rand(3)
Out[21]: array([ 0.6786481 ,  0.94231001,  0.03334007])

@jreback jreback added this to the 0.21.0 milestone Oct 1, 2017
@@ -614,6 +614,7 @@ Plotting
- Line plots no longer assume monotonic x data when calculating xlims, they show the entire lines now even for unsorted x data. (:issue:`11310`, :issue:`11471`)
- With matplotlib 2.0.0 and above, calculation of x limits for line plots is left to matplotlib, so that its new default settings are applied. (:issue:`15495`)
- Bug in ``Series.plot.bar`` or ``DataFramee.plot.bar`` with ``y`` not respecting user-passed ``color`` (:issue:`16822`)
- Bug in ``plotting._style._get_standard_colors`` resetting the random seed when generating random colors (:issue:`17525`)
Copy link
Contributor

Choose a reason for hiding this comment

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

reference the user visibile issue (e.g. parallel coordinates)?

@@ -284,3 +284,20 @@ def test_subplot_titles(self):
title=title[:-1])
title_list = [ax.get_title() for sublist in plot for ax in sublist]
assert title_list == title[:3] + ['']

def test_get_standard_colors_random_seed(self):
""" For #17525 """
Copy link
Member

Choose a reason for hiding this comment

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

Generally, we just add a comment above, not a docstring.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm with a minor comment. have't looked why its failing. ping on green.

@@ -614,6 +614,7 @@ Plotting
- Line plots no longer assume monotonic x data when calculating xlims, they show the entire lines now even for unsorted x data. (:issue:`11310`, :issue:`11471`)
- With matplotlib 2.0.0 and above, calculation of x limits for line plots is left to matplotlib, so that its new default settings are applied. (:issue:`15495`)
- Bug in ``Series.plot.bar`` or ``DataFramee.plot.bar`` with ``y`` not respecting user-passed ``color`` (:issue:`16822`)
- Bug in ``plotting._style._get_standard_colors`` causing ``plotting.parallel_coordinates`` to reset the random seed when using random colors (:issue:`17525`)
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the first part, a user won't know what you are talking about.

""" Returns a random color represented as a list of length 3"""
# GH17525 use common._random_state to avoid resetting the seed
rs = _random_state()
return rs.rand(3)
Copy link
Contributor

Choose a reason for hiding this comment

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

return rs.rand(3).tolist() will make this work.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. ping on green.

@jreback jreback removed this from the 0.21.0 milestone Oct 2, 2017
@jorisvandenbossche jorisvandenbossche merged commit 2e2093e into pandas-dev:master Oct 3, 2017
@jorisvandenbossche
Copy link
Member

@cmazzullo Thanks!

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.21.1, 0.21.0 Oct 3, 2017
@cmazzullo
Copy link
Contributor Author

Sure thing

@cmazzullo cmazzullo deleted the style-random-seed branch October 3, 2017 11:19
ghost pushed a commit to reef-technologies/pandas that referenced this pull request Oct 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pandas.plotting.parallel_coordinates resets random seed!
5 participants