PageRenderTime 69ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/notify_user/pymodules/python2.7/lib/python/pandas-0.17.1-py2.7-linux-x86_64.egg/pandas/tests/test_rplot.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 299 lines | 245 code | 38 blank | 16 comment | 16 complexity | 45f6db4ec1ea29a5a6538de1203b2e07 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. from pandas.compat import range
  3. import pandas.util.testing as tm
  4. from pandas import read_csv
  5. import os
  6. import nose
  7. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  8. import pandas.tools.rplot as rplot
  9. def curpath():
  10. pth, _ = os.path.split(os.path.abspath(__file__))
  11. return pth
  12. def between(a, b, x):
  13. """Check if x is in the somewhere between a and b.
  14. Parameters:
  15. -----------
  16. a: float, interval start
  17. b: float, interval end
  18. x: float, value to test for
  19. Returns:
  20. --------
  21. True if x is between a and b, False otherwise
  22. """
  23. if a < b:
  24. return x >= a and x <= b
  25. else:
  26. return x <= a and x >= b
  27. @tm.mplskip
  28. class TestUtilityFunctions(tm.TestCase):
  29. """
  30. Tests for RPlot utility functions.
  31. """
  32. def setUp(self):
  33. path = os.path.join(curpath(), 'data/iris.csv')
  34. self.data = read_csv(path, sep=',')
  35. def test_make_aes1(self):
  36. aes = rplot.make_aes()
  37. self.assertTrue(aes['x'] is None)
  38. self.assertTrue(aes['y'] is None)
  39. self.assertTrue(aes['size'] is None)
  40. self.assertTrue(aes['colour'] is None)
  41. self.assertTrue(aes['shape'] is None)
  42. self.assertTrue(aes['alpha'] is None)
  43. self.assertTrue(isinstance(aes, dict))
  44. def test_make_aes2(self):
  45. self.assertRaises(ValueError, rplot.make_aes,
  46. size=rplot.ScaleShape('test'))
  47. self.assertRaises(ValueError, rplot.make_aes,
  48. colour=rplot.ScaleShape('test'))
  49. self.assertRaises(ValueError, rplot.make_aes,
  50. shape=rplot.ScaleSize('test'))
  51. self.assertRaises(ValueError, rplot.make_aes,
  52. alpha=rplot.ScaleShape('test'))
  53. def test_dictionary_union(self):
  54. dict1 = {1 : 1, 2 : 2, 3 : 3}
  55. dict2 = {1 : 1, 2 : 2, 4 : 4}
  56. union = rplot.dictionary_union(dict1, dict2)
  57. self.assertEqual(len(union), 4)
  58. keys = list(union.keys())
  59. self.assertTrue(1 in keys)
  60. self.assertTrue(2 in keys)
  61. self.assertTrue(3 in keys)
  62. self.assertTrue(4 in keys)
  63. self.assertEqual(rplot.dictionary_union(dict1, {}), dict1)
  64. self.assertEqual(rplot.dictionary_union({}, dict1), dict1)
  65. self.assertEqual(rplot.dictionary_union({}, {}), {})
  66. def test_merge_aes(self):
  67. layer1 = rplot.Layer(size=rplot.ScaleSize('test'))
  68. layer2 = rplot.Layer(shape=rplot.ScaleShape('test'))
  69. rplot.merge_aes(layer1, layer2)
  70. self.assertTrue(isinstance(layer2.aes['size'], rplot.ScaleSize))
  71. self.assertTrue(isinstance(layer2.aes['shape'], rplot.ScaleShape))
  72. self.assertEqual(layer2.aes['size'], layer1.aes['size'])
  73. for key in layer2.aes.keys():
  74. if key != 'size' and key != 'shape':
  75. self.assertTrue(layer2.aes[key] is None)
  76. def test_sequence_layers(self):
  77. layer1 = rplot.Layer(self.data)
  78. layer2 = rplot.GeomPoint(x='SepalLength', y='SepalWidth',
  79. size=rplot.ScaleSize('PetalLength'))
  80. layer3 = rplot.GeomPolyFit(2)
  81. result = rplot.sequence_layers([layer1, layer2, layer3])
  82. self.assertEqual(len(result), 3)
  83. last = result[-1]
  84. self.assertEqual(last.aes['x'], 'SepalLength')
  85. self.assertEqual(last.aes['y'], 'SepalWidth')
  86. self.assertTrue(isinstance(last.aes['size'], rplot.ScaleSize))
  87. self.assertTrue(self.data is last.data)
  88. self.assertTrue(rplot.sequence_layers([layer1])[0] is layer1)
  89. @tm.mplskip
  90. class TestTrellis(tm.TestCase):
  91. def setUp(self):
  92. path = os.path.join(curpath(), 'data/tips.csv')
  93. self.data = read_csv(path, sep=',')
  94. layer1 = rplot.Layer(self.data)
  95. layer2 = rplot.GeomPoint(x='total_bill', y='tip')
  96. layer3 = rplot.GeomPolyFit(2)
  97. self.layers = rplot.sequence_layers([layer1, layer2, layer3])
  98. self.trellis1 = rplot.TrellisGrid(['sex', 'smoker'])
  99. self.trellis2 = rplot.TrellisGrid(['sex', '.'])
  100. self.trellis3 = rplot.TrellisGrid(['.', 'smoker'])
  101. self.trellised1 = self.trellis1.trellis(self.layers)
  102. self.trellised2 = self.trellis2.trellis(self.layers)
  103. self.trellised3 = self.trellis3.trellis(self.layers)
  104. def test_grid_sizes(self):
  105. self.assertEqual(len(self.trellised1), 3)
  106. self.assertEqual(len(self.trellised2), 3)
  107. self.assertEqual(len(self.trellised3), 3)
  108. self.assertEqual(len(self.trellised1[0]), 2)
  109. self.assertEqual(len(self.trellised1[0][0]), 2)
  110. self.assertEqual(len(self.trellised2[0]), 2)
  111. self.assertEqual(len(self.trellised2[0][0]), 1)
  112. self.assertEqual(len(self.trellised3[0]), 1)
  113. self.assertEqual(len(self.trellised3[0][0]), 2)
  114. self.assertEqual(len(self.trellised1[1]), 2)
  115. self.assertEqual(len(self.trellised1[1][0]), 2)
  116. self.assertEqual(len(self.trellised2[1]), 2)
  117. self.assertEqual(len(self.trellised2[1][0]), 1)
  118. self.assertEqual(len(self.trellised3[1]), 1)
  119. self.assertEqual(len(self.trellised3[1][0]), 2)
  120. self.assertEqual(len(self.trellised1[2]), 2)
  121. self.assertEqual(len(self.trellised1[2][0]), 2)
  122. self.assertEqual(len(self.trellised2[2]), 2)
  123. self.assertEqual(len(self.trellised2[2][0]), 1)
  124. self.assertEqual(len(self.trellised3[2]), 1)
  125. self.assertEqual(len(self.trellised3[2][0]), 2)
  126. def test_trellis_cols_rows(self):
  127. self.assertEqual(self.trellis1.cols, 2)
  128. self.assertEqual(self.trellis1.rows, 2)
  129. self.assertEqual(self.trellis2.cols, 1)
  130. self.assertEqual(self.trellis2.rows, 2)
  131. self.assertEqual(self.trellis3.cols, 2)
  132. self.assertEqual(self.trellis3.rows, 1)
  133. @tm.mplskip
  134. class TestScaleGradient(tm.TestCase):
  135. def setUp(self):
  136. path = os.path.join(curpath(), 'data/iris.csv')
  137. self.data = read_csv(path, sep=',')
  138. self.gradient = rplot.ScaleGradient("SepalLength", colour1=(0.2, 0.3,
  139. 0.4),
  140. colour2=(0.8, 0.7, 0.6))
  141. def test_gradient(self):
  142. for index in range(len(self.data)):
  143. row = self.data.iloc[index]
  144. r, g, b = self.gradient(self.data, index)
  145. r1, g1, b1 = self.gradient.colour1
  146. r2, g2, b2 = self.gradient.colour2
  147. self.assertTrue(between(r1, r2, r))
  148. self.assertTrue(between(g1, g2, g))
  149. self.assertTrue(between(b1, b2, b))
  150. @tm.mplskip
  151. class TestScaleGradient2(tm.TestCase):
  152. def setUp(self):
  153. path = os.path.join(curpath(), 'data/iris.csv')
  154. self.data = read_csv(path, sep=',')
  155. self.gradient = rplot.ScaleGradient2("SepalLength", colour1=(0.2, 0.3, 0.4), colour2=(0.8, 0.7, 0.6), colour3=(0.5, 0.5, 0.5))
  156. def test_gradient2(self):
  157. for index in range(len(self.data)):
  158. row = self.data.iloc[index]
  159. r, g, b = self.gradient(self.data, index)
  160. r1, g1, b1 = self.gradient.colour1
  161. r2, g2, b2 = self.gradient.colour2
  162. r3, g3, b3 = self.gradient.colour3
  163. value = row[self.gradient.column]
  164. a_ = min(self.data[self.gradient.column])
  165. b_ = max(self.data[self.gradient.column])
  166. scaled = (value - a_) / (b_ - a_)
  167. if scaled < 0.5:
  168. self.assertTrue(between(r1, r2, r))
  169. self.assertTrue(between(g1, g2, g))
  170. self.assertTrue(between(b1, b2, b))
  171. else:
  172. self.assertTrue(between(r2, r3, r))
  173. self.assertTrue(between(g2, g3, g))
  174. self.assertTrue(between(b2, b3, b))
  175. @tm.mplskip
  176. class TestScaleRandomColour(tm.TestCase):
  177. def setUp(self):
  178. path = os.path.join(curpath(), 'data/iris.csv')
  179. self.data = read_csv(path, sep=',')
  180. self.colour = rplot.ScaleRandomColour('SepalLength')
  181. def test_random_colour(self):
  182. for index in range(len(self.data)):
  183. colour = self.colour(self.data, index)
  184. self.assertEqual(len(colour), 3)
  185. r, g, b = colour
  186. self.assertTrue(r >= 0.0)
  187. self.assertTrue(g >= 0.0)
  188. self.assertTrue(b >= 0.0)
  189. self.assertTrue(r <= 1.0)
  190. self.assertTrue(g <= 1.0)
  191. self.assertTrue(b <= 1.0)
  192. @tm.mplskip
  193. class TestScaleConstant(tm.TestCase):
  194. def test_scale_constant(self):
  195. scale = rplot.ScaleConstant(1.0)
  196. self.assertEqual(scale(None, None), 1.0)
  197. scale = rplot.ScaleConstant("test")
  198. self.assertEqual(scale(None, None), "test")
  199. class TestScaleSize(tm.TestCase):
  200. def setUp(self):
  201. path = os.path.join(curpath(), 'data/iris.csv')
  202. self.data = read_csv(path, sep=',')
  203. self.scale1 = rplot.ScaleShape('Name')
  204. self.scale2 = rplot.ScaleShape('PetalLength')
  205. def test_scale_size(self):
  206. for index in range(len(self.data)):
  207. marker = self.scale1(self.data, index)
  208. self.assertTrue(marker in ['o', '+', 's', '*', '^', '<', '>', 'v', '|', 'x'])
  209. def test_scale_overflow(self):
  210. def f():
  211. for index in range(len(self.data)):
  212. self.scale2(self.data, index)
  213. self.assertRaises(ValueError, f)
  214. @tm.mplskip
  215. class TestRPlot(tm.TestCase):
  216. def test_rplot1(self):
  217. import matplotlib.pyplot as plt
  218. path = os.path.join(curpath(), 'data/tips.csv')
  219. plt.figure()
  220. self.data = read_csv(path, sep=',')
  221. self.plot = rplot.RPlot(self.data, x='tip', y='total_bill')
  222. self.plot.add(rplot.TrellisGrid(['sex', 'smoker']))
  223. self.plot.add(rplot.GeomPoint(colour=rplot.ScaleRandomColour('day'), shape=rplot.ScaleShape('size')))
  224. self.fig = plt.gcf()
  225. self.plot.render(self.fig)
  226. def test_rplot2(self):
  227. import matplotlib.pyplot as plt
  228. path = os.path.join(curpath(), 'data/tips.csv')
  229. plt.figure()
  230. self.data = read_csv(path, sep=',')
  231. self.plot = rplot.RPlot(self.data, x='tip', y='total_bill')
  232. self.plot.add(rplot.TrellisGrid(['.', 'smoker']))
  233. self.plot.add(rplot.GeomPoint(colour=rplot.ScaleRandomColour('day'), shape=rplot.ScaleShape('size')))
  234. self.fig = plt.gcf()
  235. self.plot.render(self.fig)
  236. def test_rplot3(self):
  237. import matplotlib.pyplot as plt
  238. path = os.path.join(curpath(), 'data/tips.csv')
  239. plt.figure()
  240. self.data = read_csv(path, sep=',')
  241. self.plot = rplot.RPlot(self.data, x='tip', y='total_bill')
  242. self.plot.add(rplot.TrellisGrid(['sex', '.']))
  243. self.plot.add(rplot.GeomPoint(colour=rplot.ScaleRandomColour('day'), shape=rplot.ScaleShape('size')))
  244. self.fig = plt.gcf()
  245. self.plot.render(self.fig)
  246. def test_rplot_iris(self):
  247. import matplotlib.pyplot as plt
  248. path = os.path.join(curpath(), 'data/iris.csv')
  249. plt.figure()
  250. self.data = read_csv(path, sep=',')
  251. plot = rplot.RPlot(self.data, x='SepalLength', y='SepalWidth')
  252. plot.add(rplot.GeomPoint(colour=rplot.ScaleGradient('PetalLength', colour1=(0.0, 1.0, 0.5), colour2=(1.0, 0.0, 0.5)),
  253. size=rplot.ScaleSize('PetalWidth', min_size=10.0, max_size=200.0),
  254. shape=rplot.ScaleShape('Name')))
  255. self.fig = plt.gcf()
  256. plot.render(self.fig)
  257. if __name__ == '__main__':
  258. import unittest
  259. unittest.main()