PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ramp/tests/test_utils.py

https://github.com/psattige/ramp
Python | 54 lines | 41 code | 11 blank | 2 comment | 1 complexity | deed7f148ae2c29ddd89739e990a7137 MD5 | raw file
  1. import sys
  2. sys.path.append('../..')
  3. import unittest
  4. import numpy as np
  5. import pandas as pd
  6. from pandas import DataFrame, Series, Index
  7. from pandas.util.testing import assert_frame_equal, assert_index_equal
  8. from ramp.features.base import F, Map
  9. from ramp.utils import *
  10. class TestUtils(unittest.TestCase):
  11. def test_shuffle_df(self):
  12. n = 100
  13. df = pd.DataFrame({'a':np.random.rand(n),
  14. 'b':np.random.rand(n)})
  15. df_shuffled = shuffle_df(df)
  16. self.assertEqual(df.shape, df_shuffled.shape)
  17. self.assertNotEqual(tuple(df['a']), tuple(df_shuffled['a']))
  18. self.assertNotEqual(tuple(df['b']), tuple(df_shuffled['b']))
  19. self.assertNotEqual(tuple(df.index), tuple(df_shuffled.index))
  20. assert_frame_equal(df.sort(), df_shuffled.sort())
  21. def test_np_hashes(self):
  22. a = np.random.randn(20)
  23. h = get_np_hash(a)
  24. a[0] = 200000
  25. h2 = get_np_hash(a)
  26. self.assertNotEqual(h, h2)
  27. b = a[0]
  28. a[0] = b
  29. self.assertEqual(h2, get_np_hash(a))
  30. def test_stable_repr(self):
  31. f = F('test')
  32. f2 = F('test')
  33. # equalivalent objects should have same repr
  34. self.assertEqual(stable_repr(f), stable_repr(f2))
  35. # repr is independent of object ids
  36. class Test: pass
  37. f.f = Test()
  38. r1 = stable_repr(f)
  39. f.f = Test()
  40. r2 = stable_repr(f)
  41. self.assertEqual(r1, r2)
  42. if __name__ == '__main__':
  43. unittest.main(verbosity=2)