test_cbook.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. import itertools
  2. import pickle
  3. from weakref import ref
  4. import warnings
  5. from unittest.mock import patch, Mock
  6. from datetime import datetime
  7. import numpy as np
  8. from numpy.testing import (assert_array_equal, assert_approx_equal,
  9. assert_array_almost_equal)
  10. import pytest
  11. import matplotlib.cbook as cbook
  12. import matplotlib.colors as mcolors
  13. from matplotlib.cbook import MatplotlibDeprecationWarning, delete_masked_points
  14. def test_is_hashable():
  15. with pytest.warns(MatplotlibDeprecationWarning):
  16. s = 'string'
  17. assert cbook.is_hashable(s)
  18. lst = ['list', 'of', 'stings']
  19. assert not cbook.is_hashable(lst)
  20. class Test_delete_masked_points:
  21. def test_bad_first_arg(self):
  22. with pytest.raises(ValueError):
  23. delete_masked_points('a string', np.arange(1.0, 7.0))
  24. def test_string_seq(self):
  25. a1 = ['a', 'b', 'c', 'd', 'e', 'f']
  26. a2 = [1, 2, 3, np.nan, np.nan, 6]
  27. result1, result2 = delete_masked_points(a1, a2)
  28. ind = [0, 1, 2, 5]
  29. assert_array_equal(result1, np.array(a1)[ind])
  30. assert_array_equal(result2, np.array(a2)[ind])
  31. def test_datetime(self):
  32. dates = [datetime(2008, 1, 1), datetime(2008, 1, 2),
  33. datetime(2008, 1, 3), datetime(2008, 1, 4),
  34. datetime(2008, 1, 5), datetime(2008, 1, 6)]
  35. a_masked = np.ma.array([1, 2, 3, np.nan, np.nan, 6],
  36. mask=[False, False, True, True, False, False])
  37. actual = delete_masked_points(dates, a_masked)
  38. ind = [0, 1, 5]
  39. assert_array_equal(actual[0], np.array(dates)[ind])
  40. assert_array_equal(actual[1], a_masked[ind].compressed())
  41. def test_rgba(self):
  42. a_masked = np.ma.array([1, 2, 3, np.nan, np.nan, 6],
  43. mask=[False, False, True, True, False, False])
  44. a_rgba = mcolors.to_rgba_array(['r', 'g', 'b', 'c', 'm', 'y'])
  45. actual = delete_masked_points(a_masked, a_rgba)
  46. ind = [0, 1, 5]
  47. assert_array_equal(actual[0], a_masked[ind].compressed())
  48. assert_array_equal(actual[1], a_rgba[ind])
  49. class Test_boxplot_stats:
  50. def setup(self):
  51. np.random.seed(937)
  52. self.nrows = 37
  53. self.ncols = 4
  54. self.data = np.random.lognormal(size=(self.nrows, self.ncols),
  55. mean=1.5, sigma=1.75)
  56. self.known_keys = sorted([
  57. 'mean', 'med', 'q1', 'q3', 'iqr',
  58. 'cilo', 'cihi', 'whislo', 'whishi',
  59. 'fliers', 'label'
  60. ])
  61. self.std_results = cbook.boxplot_stats(self.data)
  62. self.known_nonbootstrapped_res = {
  63. 'cihi': 6.8161283264444847,
  64. 'cilo': -0.1489815330368689,
  65. 'iqr': 13.492709959447094,
  66. 'mean': 13.00447442387868,
  67. 'med': 3.3335733967038079,
  68. 'fliers': np.array([
  69. 92.55467075, 87.03819018, 42.23204914, 39.29390996
  70. ]),
  71. 'q1': 1.3597529879465153,
  72. 'q3': 14.85246294739361,
  73. 'whishi': 27.899688243699629,
  74. 'whislo': 0.042143774965502923
  75. }
  76. self.known_bootstrapped_ci = {
  77. 'cihi': 8.939577523357828,
  78. 'cilo': 1.8692703958676578,
  79. }
  80. self.known_whis3_res = {
  81. 'whishi': 42.232049135969874,
  82. 'whislo': 0.042143774965502923,
  83. 'fliers': np.array([92.55467075, 87.03819018]),
  84. }
  85. self.known_res_percentiles = {
  86. 'whislo': 0.1933685896907924,
  87. 'whishi': 42.232049135969874
  88. }
  89. self.known_res_range = {
  90. 'whislo': 0.042143774965502923,
  91. 'whishi': 92.554670752188699
  92. }
  93. def test_form_main_list(self):
  94. assert isinstance(self.std_results, list)
  95. def test_form_each_dict(self):
  96. for res in self.std_results:
  97. assert isinstance(res, dict)
  98. def test_form_dict_keys(self):
  99. for res in self.std_results:
  100. assert set(res) <= set(self.known_keys)
  101. def test_results_baseline(self):
  102. res = self.std_results[0]
  103. for key, value in self.known_nonbootstrapped_res.items():
  104. assert_array_almost_equal(res[key], value)
  105. def test_results_bootstrapped(self):
  106. results = cbook.boxplot_stats(self.data, bootstrap=10000)
  107. res = results[0]
  108. for key, value in self.known_bootstrapped_ci.items():
  109. assert_approx_equal(res[key], value)
  110. def test_results_whiskers_float(self):
  111. results = cbook.boxplot_stats(self.data, whis=3)
  112. res = results[0]
  113. for key, value in self.known_whis3_res.items():
  114. assert_array_almost_equal(res[key], value)
  115. def test_results_whiskers_range(self):
  116. results = cbook.boxplot_stats(self.data, whis=[0, 100])
  117. res = results[0]
  118. for key, value in self.known_res_range.items():
  119. assert_array_almost_equal(res[key], value)
  120. def test_results_whiskers_percentiles(self):
  121. results = cbook.boxplot_stats(self.data, whis=[5, 95])
  122. res = results[0]
  123. for key, value in self.known_res_percentiles.items():
  124. assert_array_almost_equal(res[key], value)
  125. def test_results_withlabels(self):
  126. labels = ['Test1', 2, 'ardvark', 4]
  127. results = cbook.boxplot_stats(self.data, labels=labels)
  128. res = results[0]
  129. for lab, res in zip(labels, results):
  130. assert res['label'] == lab
  131. results = cbook.boxplot_stats(self.data)
  132. for res in results:
  133. assert 'label' not in res
  134. def test_label_error(self):
  135. labels = [1, 2]
  136. with pytest.raises(ValueError):
  137. cbook.boxplot_stats(self.data, labels=labels)
  138. def test_bad_dims(self):
  139. data = np.random.normal(size=(34, 34, 34))
  140. with pytest.raises(ValueError):
  141. cbook.boxplot_stats(data)
  142. def test_boxplot_stats_autorange_false(self):
  143. x = np.zeros(shape=140)
  144. x = np.hstack([-25, x, 25])
  145. bstats_false = cbook.boxplot_stats(x, autorange=False)
  146. bstats_true = cbook.boxplot_stats(x, autorange=True)
  147. assert bstats_false[0]['whislo'] == 0
  148. assert bstats_false[0]['whishi'] == 0
  149. assert_array_almost_equal(bstats_false[0]['fliers'], [-25, 25])
  150. assert bstats_true[0]['whislo'] == -25
  151. assert bstats_true[0]['whishi'] == 25
  152. assert_array_almost_equal(bstats_true[0]['fliers'], [])
  153. class Test_callback_registry:
  154. def setup(self):
  155. self.signal = 'test'
  156. self.callbacks = cbook.CallbackRegistry()
  157. def connect(self, s, func):
  158. return self.callbacks.connect(s, func)
  159. def is_empty(self):
  160. assert self.callbacks._func_cid_map == {}
  161. assert self.callbacks.callbacks == {}
  162. def is_not_empty(self):
  163. assert self.callbacks._func_cid_map != {}
  164. assert self.callbacks.callbacks != {}
  165. def test_callback_complete(self):
  166. # ensure we start with an empty registry
  167. self.is_empty()
  168. # create a class for testing
  169. mini_me = Test_callback_registry()
  170. # test that we can add a callback
  171. cid1 = self.connect(self.signal, mini_me.dummy)
  172. assert type(cid1) == int
  173. self.is_not_empty()
  174. # test that we don't add a second callback
  175. cid2 = self.connect(self.signal, mini_me.dummy)
  176. assert cid1 == cid2
  177. self.is_not_empty()
  178. assert len(self.callbacks._func_cid_map) == 1
  179. assert len(self.callbacks.callbacks) == 1
  180. del mini_me
  181. # check we now have no callbacks registered
  182. self.is_empty()
  183. def dummy(self):
  184. pass
  185. def test_pickling(self):
  186. assert hasattr(pickle.loads(pickle.dumps(cbook.CallbackRegistry())),
  187. "callbacks")
  188. def raising_cb_reg(func):
  189. class TestException(Exception):
  190. pass
  191. def raising_function():
  192. raise RuntimeError
  193. def transformer(excp):
  194. if isinstance(excp, RuntimeError):
  195. raise TestException
  196. raise excp
  197. # default behavior
  198. cb = cbook.CallbackRegistry()
  199. cb.connect('foo', raising_function)
  200. # old default
  201. cb_old = cbook.CallbackRegistry(exception_handler=None)
  202. cb_old.connect('foo', raising_function)
  203. # filter
  204. cb_filt = cbook.CallbackRegistry(exception_handler=transformer)
  205. cb_filt.connect('foo', raising_function)
  206. return pytest.mark.parametrize('cb, excp',
  207. [[cb, None],
  208. [cb_old, RuntimeError],
  209. [cb_filt, TestException]])(func)
  210. @raising_cb_reg
  211. def test_callbackregistry_process_exception(cb, excp):
  212. if excp is not None:
  213. with pytest.raises(excp):
  214. cb.process('foo')
  215. else:
  216. cb.process('foo')
  217. def test_sanitize_sequence():
  218. d = {'a': 1, 'b': 2, 'c': 3}
  219. k = ['a', 'b', 'c']
  220. v = [1, 2, 3]
  221. i = [('a', 1), ('b', 2), ('c', 3)]
  222. assert k == sorted(cbook.sanitize_sequence(d.keys()))
  223. assert v == sorted(cbook.sanitize_sequence(d.values()))
  224. assert i == sorted(cbook.sanitize_sequence(d.items()))
  225. assert i == cbook.sanitize_sequence(i)
  226. assert k == cbook.sanitize_sequence(k)
  227. fail_mapping = (
  228. ({'a': 1}, {'forbidden': ('a')}),
  229. ({'a': 1}, {'required': ('b')}),
  230. ({'a': 1, 'b': 2}, {'required': ('a'), 'allowed': ()})
  231. )
  232. warn_passing_mapping = (
  233. ({'a': 1, 'b': 2}, {'a': 1}, {'alias_mapping': {'a': ['b']}}, 1),
  234. ({'a': 1, 'b': 2}, {'a': 1},
  235. {'alias_mapping': {'a': ['b']}, 'allowed': ('a',)}, 1),
  236. ({'a': 1, 'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}, 1),
  237. ({'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'c': 3},
  238. {'alias_mapping': {'a': ['b']}, 'required': ('a', )}, 1),
  239. )
  240. pass_mapping = (
  241. ({'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}),
  242. ({'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}),
  243. ({'b': 2}, {'a': 2},
  244. {'alias_mapping': {'a': ['b']}, 'forbidden': ('b', )}),
  245. ({'a': 1, 'c': 3}, {'a': 1, 'c': 3},
  246. {'required': ('a', ), 'allowed': ('c', )}),
  247. ({'a': 1, 'c': 3}, {'a': 1, 'c': 3},
  248. {'required': ('a', 'c'), 'allowed': ('c', )}),
  249. ({'a': 1, 'c': 3}, {'a': 1, 'c': 3},
  250. {'required': ('a', 'c'), 'allowed': ('a', 'c')}),
  251. ({'a': 1, 'c': 3}, {'a': 1, 'c': 3},
  252. {'required': ('a', 'c'), 'allowed': ()}),
  253. ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c')}),
  254. ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'allowed': ('a', 'c')}),
  255. )
  256. @pytest.mark.parametrize('inp, kwargs_to_norm', fail_mapping)
  257. def test_normalize_kwargs_fail(inp, kwargs_to_norm):
  258. with pytest.raises(TypeError):
  259. cbook.normalize_kwargs(inp, **kwargs_to_norm)
  260. @pytest.mark.parametrize('inp, expected, kwargs_to_norm, warn_count',
  261. warn_passing_mapping)
  262. def test_normalize_kwargs_warn(inp, expected, kwargs_to_norm, warn_count):
  263. with warnings.catch_warnings(record=True) as w:
  264. warnings.simplefilter("always")
  265. assert expected == cbook.normalize_kwargs(inp, **kwargs_to_norm)
  266. assert len(w) == warn_count
  267. @pytest.mark.parametrize('inp, expected, kwargs_to_norm',
  268. pass_mapping)
  269. def test_normalize_kwargs_pass(inp, expected, kwargs_to_norm):
  270. with warnings.catch_warnings(record=True) as w:
  271. warnings.simplefilter("always")
  272. assert expected == cbook.normalize_kwargs(inp, **kwargs_to_norm)
  273. assert len(w) == 0
  274. def test_warn_external_frame_embedded_python():
  275. with patch.object(cbook, "sys") as mock_sys:
  276. mock_sys._getframe = Mock(return_value=None)
  277. with pytest.warns(UserWarning, match=r"\Adummy\Z"):
  278. cbook._warn_external("dummy")
  279. def test_to_prestep():
  280. x = np.arange(4)
  281. y1 = np.arange(4)
  282. y2 = np.arange(4)[::-1]
  283. xs, y1s, y2s = cbook.pts_to_prestep(x, y1, y2)
  284. x_target = np.asarray([0, 0, 1, 1, 2, 2, 3], dtype='float')
  285. y1_target = np.asarray([0, 1, 1, 2, 2, 3, 3], dtype='float')
  286. y2_target = np.asarray([3, 2, 2, 1, 1, 0, 0], dtype='float')
  287. assert_array_equal(x_target, xs)
  288. assert_array_equal(y1_target, y1s)
  289. assert_array_equal(y2_target, y2s)
  290. xs, y1s = cbook.pts_to_prestep(x, y1)
  291. assert_array_equal(x_target, xs)
  292. assert_array_equal(y1_target, y1s)
  293. def test_to_prestep_empty():
  294. steps = cbook.pts_to_prestep([], [])
  295. assert steps.shape == (2, 0)
  296. def test_to_poststep():
  297. x = np.arange(4)
  298. y1 = np.arange(4)
  299. y2 = np.arange(4)[::-1]
  300. xs, y1s, y2s = cbook.pts_to_poststep(x, y1, y2)
  301. x_target = np.asarray([0, 1, 1, 2, 2, 3, 3], dtype='float')
  302. y1_target = np.asarray([0, 0, 1, 1, 2, 2, 3], dtype='float')
  303. y2_target = np.asarray([3, 3, 2, 2, 1, 1, 0], dtype='float')
  304. assert_array_equal(x_target, xs)
  305. assert_array_equal(y1_target, y1s)
  306. assert_array_equal(y2_target, y2s)
  307. xs, y1s = cbook.pts_to_poststep(x, y1)
  308. assert_array_equal(x_target, xs)
  309. assert_array_equal(y1_target, y1s)
  310. def test_to_poststep_empty():
  311. steps = cbook.pts_to_poststep([], [])
  312. assert steps.shape == (2, 0)
  313. def test_to_midstep():
  314. x = np.arange(4)
  315. y1 = np.arange(4)
  316. y2 = np.arange(4)[::-1]
  317. xs, y1s, y2s = cbook.pts_to_midstep(x, y1, y2)
  318. x_target = np.asarray([0, .5, .5, 1.5, 1.5, 2.5, 2.5, 3], dtype='float')
  319. y1_target = np.asarray([0, 0, 1, 1, 2, 2, 3, 3], dtype='float')
  320. y2_target = np.asarray([3, 3, 2, 2, 1, 1, 0, 0], dtype='float')
  321. assert_array_equal(x_target, xs)
  322. assert_array_equal(y1_target, y1s)
  323. assert_array_equal(y2_target, y2s)
  324. xs, y1s = cbook.pts_to_midstep(x, y1)
  325. assert_array_equal(x_target, xs)
  326. assert_array_equal(y1_target, y1s)
  327. def test_to_midstep_empty():
  328. steps = cbook.pts_to_midstep([], [])
  329. assert steps.shape == (2, 0)
  330. @pytest.mark.parametrize(
  331. "args",
  332. [(np.arange(12).reshape(3, 4), 'a'),
  333. (np.arange(12), 'a'),
  334. (np.arange(12), np.arange(3))])
  335. def test_step_fails(args):
  336. with pytest.raises(ValueError):
  337. cbook.pts_to_prestep(*args)
  338. def test_grouper():
  339. class dummy:
  340. pass
  341. a, b, c, d, e = objs = [dummy() for j in range(5)]
  342. g = cbook.Grouper()
  343. g.join(*objs)
  344. assert set(list(g)[0]) == set(objs)
  345. assert set(g.get_siblings(a)) == set(objs)
  346. for other in objs[1:]:
  347. assert g.joined(a, other)
  348. g.remove(a)
  349. for other in objs[1:]:
  350. assert not g.joined(a, other)
  351. for A, B in itertools.product(objs[1:], objs[1:]):
  352. assert g.joined(A, B)
  353. def test_grouper_private():
  354. class dummy:
  355. pass
  356. objs = [dummy() for j in range(5)]
  357. g = cbook.Grouper()
  358. g.join(*objs)
  359. # reach in and touch the internals !
  360. mapping = g._mapping
  361. for o in objs:
  362. assert ref(o) in mapping
  363. base_set = mapping[ref(objs[0])]
  364. for o in objs[1:]:
  365. assert mapping[ref(o)] is base_set
  366. def test_flatiter():
  367. x = np.arange(5)
  368. it = x.flat
  369. assert 0 == next(it)
  370. assert 1 == next(it)
  371. ret = cbook.safe_first_element(it)
  372. assert ret == 0
  373. assert 0 == next(it)
  374. assert 1 == next(it)
  375. def test_reshape2d():
  376. class dummy:
  377. pass
  378. xnew = cbook._reshape_2D([], 'x')
  379. assert np.shape(xnew) == (1, 0)
  380. x = [dummy() for j in range(5)]
  381. xnew = cbook._reshape_2D(x, 'x')
  382. assert np.shape(xnew) == (1, 5)
  383. x = np.arange(5)
  384. xnew = cbook._reshape_2D(x, 'x')
  385. assert np.shape(xnew) == (1, 5)
  386. x = [[dummy() for j in range(5)] for i in range(3)]
  387. xnew = cbook._reshape_2D(x, 'x')
  388. assert np.shape(xnew) == (3, 5)
  389. # this is strange behaviour, but...
  390. x = np.random.rand(3, 5)
  391. xnew = cbook._reshape_2D(x, 'x')
  392. assert np.shape(xnew) == (5, 3)
  393. # Now test with a list of lists with different lengths, which means the
  394. # array will internally be converted to a 1D object array of lists
  395. x = [[1, 2, 3], [3, 4], [2]]
  396. xnew = cbook._reshape_2D(x, 'x')
  397. assert isinstance(xnew, list)
  398. assert isinstance(xnew[0], np.ndarray) and xnew[0].shape == (3,)
  399. assert isinstance(xnew[1], np.ndarray) and xnew[1].shape == (2,)
  400. assert isinstance(xnew[2], np.ndarray) and xnew[2].shape == (1,)
  401. # We now need to make sure that this works correctly for Numpy subclasses
  402. # where iterating over items can return subclasses too, which may be
  403. # iterable even if they are scalars. To emulate this, we make a Numpy
  404. # array subclass that returns Numpy 'scalars' when iterating or accessing
  405. # values, and these are technically iterable if checking for example
  406. # isinstance(x, collections.abc.Iterable).
  407. class ArraySubclass(np.ndarray):
  408. def __iter__(self):
  409. for value in super().__iter__():
  410. yield np.array(value)
  411. def __getitem__(self, item):
  412. return np.array(super().__getitem__(item))
  413. v = np.arange(10, dtype=float)
  414. x = ArraySubclass((10,), dtype=float, buffer=v.data)
  415. xnew = cbook._reshape_2D(x, 'x')
  416. # We check here that the array wasn't split up into many individual
  417. # ArraySubclass, which is what used to happen due to a bug in _reshape_2D
  418. assert len(xnew) == 1
  419. assert isinstance(xnew[0], ArraySubclass)
  420. def test_contiguous_regions():
  421. a, b, c = 3, 4, 5
  422. # Starts and ends with True
  423. mask = [True]*a + [False]*b + [True]*c
  424. expected = [(0, a), (a+b, a+b+c)]
  425. assert cbook.contiguous_regions(mask) == expected
  426. d, e = 6, 7
  427. # Starts with True ends with False
  428. mask = mask + [False]*e
  429. assert cbook.contiguous_regions(mask) == expected
  430. # Starts with False ends with True
  431. mask = [False]*d + mask[:-e]
  432. expected = [(d, d+a), (d+a+b, d+a+b+c)]
  433. assert cbook.contiguous_regions(mask) == expected
  434. # Starts and ends with False
  435. mask = mask + [False]*e
  436. assert cbook.contiguous_regions(mask) == expected
  437. # No True in mask
  438. assert cbook.contiguous_regions([False]*5) == []
  439. # Empty mask
  440. assert cbook.contiguous_regions([]) == []
  441. def test_safe_first_element_pandas_series(pd):
  442. # deliberately create a pandas series with index not starting from 0
  443. s = pd.Series(range(5), index=range(10, 15))
  444. actual = cbook.safe_first_element(s)
  445. assert actual == 0
  446. def test_make_keyword_only(recwarn):
  447. @cbook._make_keyword_only("3.0", "arg")
  448. def func(pre, arg, post=None):
  449. pass
  450. func(1, arg=2)
  451. assert len(recwarn) == 0
  452. with pytest.warns(MatplotlibDeprecationWarning):
  453. func(1, 2)
  454. with pytest.warns(MatplotlibDeprecationWarning):
  455. func(1, 2, 3)
  456. def test_warn_external(recwarn):
  457. cbook._warn_external("oops")
  458. assert len(recwarn) == 1
  459. assert recwarn[0].filename == __file__