From afcd8a20687c39b9639db2208403d7cc2aa7482b Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 19 Mar 2014 09:37:13 -0500 Subject: [PATCH 1/2] BUG: Seed RNG in test_partially_invalid_plot_data --- pandas/tests/test_graphics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index f5ee7fc301384..5ef432d009f4f 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -1106,6 +1106,7 @@ def test_all_invalid_plot_data(self): @slow def test_partially_invalid_plot_data(self): + np.random.seed(42) kinds = 'line', 'bar', 'barh', 'kde', 'density' df = DataFrame(randn(10, 2), dtype=object) df[np.random.rand(df.shape[0]) > 0.5] = 'a' From 96d4968fe300b5051478ccc6deb0f30b37ae3c5f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 19 Mar 2014 11:02:33 -0500 Subject: [PATCH 2/2] Use a context manager for RNG --- pandas/tests/test_graphics.py | 14 +++++++------- pandas/tests/test_testing.py | 14 +++++++++++++- pandas/util/testing.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index 5ef432d009f4f..fd0463ccd7ba0 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -1106,13 +1106,13 @@ def test_all_invalid_plot_data(self): @slow def test_partially_invalid_plot_data(self): - np.random.seed(42) - kinds = 'line', 'bar', 'barh', 'kde', 'density' - df = DataFrame(randn(10, 2), dtype=object) - df[np.random.rand(df.shape[0]) > 0.5] = 'a' - for kind in kinds: - with tm.assertRaises(TypeError): - df.plot(kind=kind) + with tm.RNGContext(42): + kinds = 'line', 'bar', 'barh', 'kde', 'density' + df = DataFrame(randn(10, 2), dtype=object) + df[np.random.rand(df.shape[0]) > 0.5] = 'a' + for kind in kinds: + with tm.assertRaises(TypeError): + df.plot(kind=kind) def test_invalid_kind(self): df = DataFrame(randn(10, 2)) diff --git a/pandas/tests/test_testing.py b/pandas/tests/test_testing.py index c3c1c4f5977e6..986e44ced83a2 100644 --- a/pandas/tests/test_testing.py +++ b/pandas/tests/test_testing.py @@ -8,7 +8,8 @@ import sys from pandas import Series from pandas.util.testing import ( - assert_almost_equal, assertRaisesRegexp, raise_with_traceback, assert_series_equal + assert_almost_equal, assertRaisesRegexp, raise_with_traceback, assert_series_equal, + RNGContext ) # let's get meta. @@ -153,3 +154,14 @@ def test_not_equal(self): # ATM meta data is not checked in assert_series_equal # self._assert_not_equal(Series(range(3)),Series(range(3),name='foo'),check_names=True) + +class TestRNGContext(unittest.TestCase): + + def test_RNGContext(self): + expected0 = 1.764052345967664 + expected1 = 1.6243453636632417 + + with RNGContext(0): + with RNGContext(1): + self.assertEqual(np.random.randn(), expected1) + self.assertEqual(np.random.randn(), expected0) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 2860cdf3b200d..8abbb37646b49 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1528,3 +1528,33 @@ def skip_if_no_ne(engine='numexpr'): def disabled(t): t.disabled = True return t + + +class RNGContext(object): + """ + Context manager to set the numpy random number generator speed. Returns + to the original value upon exiting the context manager. + + Parameters + ---------- + seed : int + Seed for numpy.random.seed + + Examples + -------- + + with RNGContext(42): + np.random.randn() + """ + + def __init__(self, seed): + self.seed = seed + + def __enter__(self): + + self.start_state = np.random.get_state() + np.random.seed(self.seed) + + def __exit__(self, exc_type, exc_value, traceback): + + np.random.set_state(self.start_state)