test_base.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests of the neo.core.baseneo.BaseNeo class and related functions
  4. """
  5. from datetime import datetime, date, time, timedelta
  6. from decimal import Decimal
  7. from fractions import Fraction
  8. import sys
  9. import unittest
  10. import numpy as np
  11. import quantities as pq
  12. try:
  13. from IPython.lib.pretty import pretty
  14. except ImportError as err:
  15. HAVE_IPYTHON = False
  16. else:
  17. HAVE_IPYTHON = True
  18. from neo.core.baseneo import (BaseNeo, _check_annotations,
  19. merge_annotations, merge_annotation)
  20. from neo.test.tools import assert_arrays_equal
  21. if sys.version_info[0] >= 3:
  22. _bytes = bytes
  23. long = int
  24. def bytes(s):
  25. return _bytes(s, encoding='ascii')
  26. class Test_check_annotations(unittest.TestCase):
  27. '''
  28. TestCase to make sure _check_annotations works
  29. '''
  30. def setUp(self):
  31. self.values = [1, 2.2, 3 + 2j,
  32. 'test', r'test', b'test',
  33. None,
  34. datetime(year=2008, month=12, day=3, hour=10, minute=4),
  35. timedelta(weeks=2, days=7, hours=18, minutes=28,
  36. seconds=18, milliseconds=28, microseconds=45),
  37. time(hour=10, minute=4),
  38. Decimal("3.14"), Fraction(13, 21),
  39. np.array([1.1, 1.2, 1.3]),
  40. np.array([1, 2, 3]),
  41. np.array('test', dtype='S'),
  42. np.array([True, False])]
  43. def test__check_annotations__invalid_ValueError(self):
  44. value = set([])
  45. self.assertRaises(ValueError, _check_annotations, value)
  46. def test__check_annotations__invalid_dtype_ValueError(self):
  47. value = np.array([], dtype='O')
  48. self.assertRaises(ValueError, _check_annotations, value)
  49. def test__check_annotations__valid_dtypes(self):
  50. for value in self.values:
  51. _check_annotations(value)
  52. def test__check_annotations__list(self):
  53. _check_annotations(self.values)
  54. def test__check_annotations__tuple(self):
  55. _check_annotations(tuple(self.values))
  56. _check_annotations((self.values, self.values))
  57. def test__check_annotations__dict(self):
  58. names = ['value%s' % i for i in range(len(self.values))]
  59. values = dict(zip(names, self.values))
  60. _check_annotations(values)
  61. class TestBaseNeo(unittest.TestCase):
  62. '''
  63. TestCase to make sure basic initialization and methods work
  64. '''
  65. def test_init(self):
  66. '''test to make sure initialization works properly'''
  67. base = BaseNeo(name='a base', description='this is a test')
  68. self.assertEqual(base.name, 'a base')
  69. self.assertEqual(base.description, 'this is a test')
  70. self.assertEqual(base.file_origin, None)
  71. def test_annotate(self):
  72. '''test to make sure annotation works properly'''
  73. base = BaseNeo()
  74. base.annotate(test1=1, test2=1)
  75. result1 = {'test1': 1, 'test2': 1}
  76. self.assertDictEqual(result1, base.annotations)
  77. base.annotate(test3=2, test4=3)
  78. result2 = {'test3': 2, 'test4': 3}
  79. result2a = dict(list(result1.items()) + list(result2.items()))
  80. self.assertDictContainsSubset(result1, base.annotations)
  81. self.assertDictContainsSubset(result2, base.annotations)
  82. self.assertDictEqual(result2a, base.annotations)
  83. base.annotate(test1=5, test2=8)
  84. result3 = {'test1': 5, 'test2': 8}
  85. result3a = dict(list(result3.items()) + list(result2.items()))
  86. self.assertDictContainsSubset(result2, base.annotations)
  87. self.assertDictContainsSubset(result3, base.annotations)
  88. self.assertDictEqual(result3a, base.annotations)
  89. self.assertNotEqual(base.annotations['test1'], result1['test1'])
  90. self.assertNotEqual(base.annotations['test2'], result1['test2'])
  91. def test__children(self):
  92. base = BaseNeo()
  93. self.assertEqual(base._single_parent_objects, ())
  94. self.assertEqual(base._multi_parent_objects, ())
  95. self.assertEqual(base._single_parent_containers, ())
  96. self.assertEqual(base._multi_parent_containers, ())
  97. self.assertEqual(base._parent_objects, ())
  98. self.assertEqual(base._parent_containers, ())
  99. self.assertEqual(base.parents, ())
  100. class Test_BaseNeo_merge_annotations_merge(unittest.TestCase):
  101. '''
  102. TestCase to make sure merge_annotations and merge methods work
  103. '''
  104. def setUp(self):
  105. self.name1 = 'a base 1'
  106. self.name2 = 'a base 2'
  107. self.description1 = 'this is a test 1'
  108. self.description2 = 'this is a test 2'
  109. self.base1 = BaseNeo(name=self.name1, description=self.description1)
  110. self.base2 = BaseNeo(name=self.name2, description=self.description2)
  111. def test_merge_annotations__dict(self):
  112. self.base1.annotations = {'val0': 'val0', 'val1': 1,
  113. 'val2': 2.2, 'val3': 'test1',
  114. 'val4': [.4], 'val5': {0: 0, 1: {0: 0}},
  115. 'val6': np.array([0, 1, 2])}
  116. self.base2.annotations = {'val2': 2.2, 'val3': 'test2',
  117. 'val4': [4, 4.4], 'val5': {1: {1: 1}, 2: 2},
  118. 'val6': np.array([4, 5, 6]), 'val7': True}
  119. ann1 = self.base1.annotations
  120. ann2 = self.base2.annotations
  121. ann1c = self.base1.annotations.copy()
  122. ann2c = self.base2.annotations.copy()
  123. targ = {'val0': 'val0', 'val1': 1, 'val2': 2.2, 'val3': 'test1;test2',
  124. 'val4': [.4, 4, 4.4], 'val5': {0: 0, 1: {0: 0, 1: 1}, 2: 2},
  125. 'val7': True}
  126. self.base1.merge_annotations(self.base2)
  127. val6t = np.array([0, 1, 2, 4, 5, 6])
  128. val61 = ann1.pop('val6')
  129. val61c = ann1c.pop('val6')
  130. val62 = ann2.pop('val6')
  131. val62c = ann2c.pop('val6')
  132. self.assertEqual(ann1, self.base1.annotations)
  133. self.assertNotEqual(ann1c, self.base1.annotations)
  134. self.assertEqual(ann2c, self.base2.annotations)
  135. self.assertEqual(targ, self.base1.annotations)
  136. assert_arrays_equal(val61, val6t)
  137. self.assertRaises(AssertionError, assert_arrays_equal, val61c, val6t)
  138. assert_arrays_equal(val62, val62c)
  139. self.assertEqual(self.name1, self.base1.name)
  140. self.assertEqual(self.name2, self.base2.name)
  141. self.assertEqual(self.description1, self.base1.description)
  142. self.assertEqual(self.description2, self.base2.description)
  143. def test_merge_annotations__func__dict(self):
  144. ann1 = {'val0': 'val0', 'val1': 1, 'val2': 2.2, 'val3': 'test1',
  145. 'val4': [.4], 'val5': {0: 0, 1: {0: 0}},
  146. 'val6': np.array([0, 1, 2])}
  147. ann2 = {'val2': 2.2, 'val3': 'test2',
  148. 'val4': [4, 4.4], 'val5': {1: {1: 1}, 2: 2},
  149. 'val6': np.array([4, 5, 6]), 'val7': True}
  150. ann1c = ann1.copy()
  151. ann2c = ann2.copy()
  152. targ = {'val0': 'val0', 'val1': 1, 'val2': 2.2, 'val3': 'test1;test2',
  153. 'val4': [.4, 4, 4.4], 'val5': {0: 0, 1: {0: 0, 1: 1}, 2: 2},
  154. 'val7': True}
  155. res = merge_annotations(ann1, ann2)
  156. val6t = np.array([0, 1, 2, 4, 5, 6])
  157. val6r = res.pop('val6')
  158. val61 = ann1.pop('val6')
  159. val61c = ann1c.pop('val6')
  160. val62 = ann2.pop('val6')
  161. val62c = ann2c.pop('val6')
  162. self.assertEqual(ann1, ann1c)
  163. self.assertEqual(ann2, ann2c)
  164. self.assertEqual(res, targ)
  165. assert_arrays_equal(val6r, val6t)
  166. self.assertRaises(AssertionError, assert_arrays_equal, val61, val6t)
  167. assert_arrays_equal(val61, val61c)
  168. assert_arrays_equal(val62, val62c)
  169. def test_merge_annotation__func__str(self):
  170. ann1 = 'test1'
  171. ann2 = 'test2'
  172. targ = 'test1;test2'
  173. res = merge_annotation(ann1, ann2)
  174. self.assertEqual(res, targ)
  175. def test_merge_annotation__func__ndarray(self):
  176. ann1 = np.array([0, 1, 2])
  177. ann2 = np.array([4, 5, 6])
  178. ann1c = ann1.copy()
  179. ann2c = ann2.copy()
  180. targ = np.array([0, 1, 2, 4, 5, 6])
  181. res = merge_annotation(ann1, ann2)
  182. assert_arrays_equal(res, targ)
  183. assert_arrays_equal(ann1, ann1c)
  184. assert_arrays_equal(ann2, ann2c)
  185. def test_merge_annotation__func__list(self):
  186. ann1 = [0, 1, 2]
  187. ann2 = [4, 5, 6]
  188. ann1c = ann1[:]
  189. ann2c = ann2[:]
  190. targ = [0, 1, 2, 4, 5, 6]
  191. res = merge_annotation(ann1, ann2)
  192. self.assertEqual(res, targ)
  193. self.assertEqual(ann1, ann1c)
  194. self.assertEqual(ann2, ann2c)
  195. def test_merge_annotation__func__dict(self):
  196. ann1 = {0: 0, 1: {0: 0}}
  197. ann2 = {1: {1: 1}, 2: 2}
  198. ann1c = ann1.copy()
  199. ann2c = ann2.copy()
  200. targ = {0: 0, 1: {0: 0, 1: 1}, 2: 2}
  201. res = merge_annotation(ann1, ann2)
  202. self.assertEqual(res, targ)
  203. self.assertEqual(ann1, ann1c)
  204. self.assertEqual(ann2, ann2c)
  205. def test_merge_annotation__func__int(self):
  206. ann1 = 1
  207. ann2 = 1
  208. ann3 = 3
  209. targ = 1
  210. res = merge_annotation(ann1, ann2)
  211. self.assertEqual(res, targ)
  212. self.assertRaises(AssertionError, merge_annotation, ann1, ann3)
  213. def test_merge_annotation__func__float(self):
  214. ann1 = 1.1
  215. ann2 = 1.1
  216. ann3 = 1.3
  217. targ = 1.1
  218. res = merge_annotation(ann1, ann2)
  219. self.assertEqual(res, targ)
  220. self.assertRaises(AssertionError, merge_annotation, ann1, ann3)
  221. def test_merge_annotation__func__bool(self):
  222. ann1 = False
  223. ann2 = False
  224. ann3 = True
  225. ann4 = True
  226. targ1 = False
  227. targ2 = True
  228. res1 = merge_annotation(ann1, ann2)
  229. res2 = merge_annotation(ann3, ann4)
  230. self.assertEqual(res1, targ1)
  231. self.assertEqual(res2, targ2)
  232. self.assertRaises(AssertionError, merge_annotation, ann1, ann3)
  233. self.assertRaises(AssertionError, merge_annotation, ann2, ann4)
  234. def test_merge__dict(self):
  235. self.base1.annotations = {'val0': 'val0', 'val1': 1,
  236. 'val2': 2.2, 'val3': 'test1'}
  237. self.base2.annotations = {'val2': 2.2, 'val3': 'test2',
  238. 'val4': [4, 4.4], 'val5': True}
  239. ann1 = self.base1.annotations
  240. ann1c = self.base1.annotations.copy()
  241. ann2c = self.base2.annotations.copy()
  242. targ = {'val0': 'val0', 'val1': 1, 'val2': 2.2, 'val3': 'test1;test2',
  243. 'val4': [4, 4.4], 'val5': True}
  244. self.base1.merge(self.base2)
  245. self.assertEqual(ann1, self.base1.annotations)
  246. self.assertNotEqual(ann1c, self.base1.annotations)
  247. self.assertEqual(ann2c, self.base2.annotations)
  248. self.assertEqual(targ, self.base1.annotations)
  249. self.assertEqual(self.name1, self.base1.name)
  250. self.assertEqual(self.name2, self.base2.name)
  251. self.assertEqual(self.description1, self.base1.description)
  252. self.assertEqual(self.description2, self.base2.description)
  253. def test_merge_annotations__different_type_AssertionError(self):
  254. self.base1.annotations = {'val1': 1, 'val2': 2.2, 'val3': 'tester'}
  255. self.base2.annotations = {'val3': False, 'val4': [4, 4.4],
  256. 'val5': True}
  257. self.base1.merge_annotations(self.base2)
  258. self.assertEqual(self.base1.annotations,
  259. {'val1': 1,
  260. 'val2': 2.2,
  261. 'val3': 'MERGE CONFLICT',
  262. 'val4': [4, 4.4],
  263. 'val5': True})
  264. def test_merge__different_type_AssertionError(self):
  265. self.base1.annotations = {'val1': 1, 'val2': 2.2, 'val3': 'tester'}
  266. self.base2.annotations = {'val3': False, 'val4': [4, 4.4],
  267. 'val5': True}
  268. self.base1.merge(self.base2)
  269. self.assertEqual(self.base1.annotations,
  270. {'val1': 1,
  271. 'val2': 2.2,
  272. 'val3': 'MERGE CONFLICT',
  273. 'val4': [4, 4.4],
  274. 'val5': True})
  275. def test_merge_annotations__unmergable_unequal_AssertionError(self):
  276. self.base1.annotations = {'val1': 1, 'val2': 2.2, 'val3': True}
  277. self.base2.annotations = {'val3': False, 'val4': [4, 4.4],
  278. 'val5': True}
  279. self.base1.merge_annotations(self.base2)
  280. self.assertEqual(self.base1.annotations,
  281. {'val1': 1,
  282. 'val2': 2.2,
  283. 'val3': 'MERGE CONFLICT',
  284. 'val4': [4, 4.4],
  285. 'val5': True})
  286. def test_merge__unmergable_unequal_AssertionError(self):
  287. self.base1.annotations = {'val1': 1, 'val2': 2.2, 'val3': True}
  288. self.base2.annotations = {'val3': False, 'val4': [4, 4.4],
  289. 'val5': True}
  290. self.base1.merge(self.base2)
  291. self.assertEqual(self.base1.annotations,
  292. {'val1': 1,
  293. 'val2': 2.2,
  294. 'val3': 'MERGE CONFLICT',
  295. 'val4': [4, 4.4],
  296. 'val5': True})
  297. class TestBaseNeoCoreTypes(unittest.TestCase):
  298. '''
  299. TestCase to make sure annotations are properly checked for core built-in
  300. python data types
  301. '''
  302. def setUp(self):
  303. '''create the instance to be tested, called before every test'''
  304. self.base = BaseNeo()
  305. def test_python_nonetype(self):
  306. '''test to make sure None type data is accepted'''
  307. value = None
  308. self.base.annotate(data=value)
  309. result = {'data': value}
  310. self.assertEqual(value, self.base.annotations['data'])
  311. self.assertDictEqual(result, self.base.annotations)
  312. def test_python_int(self):
  313. '''test to make sure int type data is accepted'''
  314. value = 10
  315. self.base.annotate(data=value)
  316. result = {'data': value}
  317. self.assertEqual(value, self.base.annotations['data'])
  318. self.assertDictEqual(result, self.base.annotations)
  319. def test_python_long(self):
  320. '''test to make sure long type data is accepted'''
  321. value = long(7)
  322. self.base.annotate(data=value)
  323. result = {'data': value}
  324. self.assertEqual(value, self.base.annotations['data'])
  325. self.assertDictEqual(result, self.base.annotations)
  326. def test_python_float(self):
  327. '''test to make sure float type data is accepted'''
  328. value = 9.2
  329. self.base.annotate(data=value)
  330. result = {'data': value}
  331. self.assertEqual(value, self.base.annotations['data'])
  332. self.assertDictEqual(result, self.base.annotations)
  333. def test_python_complex(self):
  334. '''test to make sure complex type data is accepted'''
  335. value = complex(23.17, 11.29)
  336. self.base.annotate(data=value)
  337. result = {'data': value}
  338. self.assertEqual(value, self.base.annotations['data'])
  339. self.assertDictEqual(result, self.base.annotations)
  340. def test_python_string(self):
  341. '''test to make sure string type data is accepted'''
  342. value = 'this is a test'
  343. self.base.annotate(data=value)
  344. result = {'data': value}
  345. self.assertEqual(value, self.base.annotations['data'])
  346. self.assertDictEqual(result, self.base.annotations)
  347. def test_python_unicode(self):
  348. '''test to make sure unicode type data is accepted'''
  349. value = u'this is also a test'
  350. self.base.annotate(data=value)
  351. result = {'data': value}
  352. self.assertEqual(value, self.base.annotations['data'])
  353. self.assertDictEqual(result, self.base.annotations)
  354. def test_python_bytes(self):
  355. '''test to make sure bytes type data is accepted'''
  356. value = bytes('1,2,3,4,5')
  357. self.base.annotate(data=value)
  358. result = {'data': value}
  359. self.assertEqual(value, self.base.annotations['data'])
  360. self.assertDictEqual(result, self.base.annotations)
  361. class TestBaseNeoStandardLibraryTypes(unittest.TestCase):
  362. '''
  363. TestCase to make sure annotations are properly checked for data types from
  364. the python standard library that are not core built-in data types
  365. '''
  366. def setUp(self):
  367. '''create the instance to be tested, called before every test'''
  368. self.base = BaseNeo()
  369. def test_python_fraction(self):
  370. '''test to make sure Fraction type data is accepted'''
  371. value = Fraction(13, 21)
  372. self.base.annotate(data=value)
  373. result = {'data': value}
  374. self.assertEqual(value, self.base.annotations['data'])
  375. self.assertDictEqual(result, self.base.annotations)
  376. def test_python_decimal(self):
  377. '''test to make sure Decimal type data is accepted'''
  378. value = Decimal("3.14")
  379. self.base.annotate(data=value)
  380. result = {'data': value}
  381. self.assertEqual(value, self.base.annotations['data'])
  382. self.assertDictEqual(result, self.base.annotations)
  383. def test_python_datetime(self):
  384. '''test to make sure datetime type data is accepted'''
  385. value = datetime(year=2008, month=12, day=3, hour=10, minute=4)
  386. self.base.annotate(data=value)
  387. result = {'data': value}
  388. self.assertEqual(value, self.base.annotations['data'])
  389. self.assertDictEqual(result, self.base.annotations)
  390. def test_python_date(self):
  391. '''test to make sure date type data is accepted'''
  392. value = date(year=2008, month=12, day=3)
  393. self.base.annotate(data=value)
  394. result = {'data': value}
  395. self.assertEqual(value, self.base.annotations['data'])
  396. self.assertDictEqual(result, self.base.annotations)
  397. def test_python_time(self):
  398. '''test to make sure time type data is accepted'''
  399. value = time(hour=10, minute=4)
  400. self.base.annotate(data=value)
  401. result = {'data': value}
  402. self.assertEqual(value, self.base.annotations['data'])
  403. self.assertDictEqual(result, self.base.annotations)
  404. def test_python_timedelta(self):
  405. '''test to make sure timedelta type data is accepted'''
  406. value = timedelta(weeks=2, days=7, hours=18, minutes=28,
  407. seconds=18, milliseconds=28,
  408. microseconds=45)
  409. self.base.annotate(data=value)
  410. result = {'data': value}
  411. self.assertEqual(value, self.base.annotations['data'])
  412. self.assertDictEqual(result, self.base.annotations)
  413. class TestBaseNeoContainerTypes(unittest.TestCase):
  414. '''
  415. TestCase to make sure annotations are properly checked for data type
  416. inside python built-in container types
  417. '''
  418. def setUp(self):
  419. '''create the instance to be tested, called before every test'''
  420. self.base = BaseNeo()
  421. def test_python_list(self):
  422. '''test to make sure list type data is accepted'''
  423. value = [None, 10, 9.2, complex(23, 11),
  424. ['this is a test', bytes('1,2,3,4,5')],
  425. [Fraction(13, 21), Decimal("3.14")]]
  426. self.base.annotate(data=value)
  427. result = {'data': value}
  428. self.assertListEqual(value, self.base.annotations['data'])
  429. self.assertDictEqual(result, self.base.annotations)
  430. def test_python_tuple(self):
  431. '''test to make sure tuple type data is accepted'''
  432. value = (None, 10, 9.2, complex(23, 11),
  433. ('this is a test', bytes('1,2,3,4,5')),
  434. (Fraction(13, 21), Decimal("3.14")))
  435. self.base.annotate(data=value)
  436. result = {'data': value}
  437. self.assertTupleEqual(value, self.base.annotations['data'])
  438. self.assertDictEqual(result, self.base.annotations)
  439. def test_python_dict(self):
  440. '''test to make sure dict type data is accepted'''
  441. value = {'NoneType': None, 'int': 10, 'float': 9.2,
  442. 'complex': complex(23, 11),
  443. 'dict1': {'string': 'this is a test',
  444. 'bytes': bytes('1,2,3,4,5')},
  445. 'dict2': {'Fraction': Fraction(13, 21),
  446. 'Decimal': Decimal("3.14")}}
  447. self.base.annotate(data=value)
  448. result = {'data': value}
  449. self.assertDictEqual(result, self.base.annotations)
  450. def test_python_set(self):
  451. '''test to make sure set type data is rejected'''
  452. value = set([None, 10, 9.2, complex(23, 11)])
  453. self.assertRaises(ValueError, self.base.annotate, data=value)
  454. def test_python_frozenset(self):
  455. '''test to make sure frozenset type data is rejected'''
  456. value = frozenset([None, 10, 9.2, complex(23, 11)])
  457. self.assertRaises(ValueError, self.base.annotate, data=value)
  458. def test_python_iter(self):
  459. '''test to make sure iter type data is rejected'''
  460. value = iter([None, 10, 9.2, complex(23, 11)])
  461. self.assertRaises(ValueError, self.base.annotate, data=value)
  462. class TestBaseNeoNumpyArrayTypes(unittest.TestCase):
  463. '''
  464. TestCase to make sure annotations are properly checked for numpy arrays
  465. '''
  466. def setUp(self):
  467. '''create the instance to be tested, called before every test'''
  468. self.base = BaseNeo()
  469. def test_numpy_array_int(self):
  470. '''test to make sure int type numpy arrays are accepted'''
  471. value = np.array([1, 2, 3, 4, 5], dtype=np.int)
  472. self.base.annotate(data=value)
  473. result = {'data': value}
  474. self.assertDictEqual(result, self.base.annotations)
  475. def test_numpy_array_uint(self):
  476. '''test to make sure uint type numpy arrays are accepted'''
  477. value = np.array([1, 2, 3, 4, 5], dtype=np.uint)
  478. self.base.annotate(data=value)
  479. result = {'data': value}
  480. self.assertDictEqual(result, self.base.annotations)
  481. def test_numpy_array_int0(self):
  482. '''test to make sure int0 type numpy arrays are accepted'''
  483. value = np.array([1, 2, 3, 4, 5], dtype=np.int0)
  484. self.base.annotate(data=value)
  485. result = {'data': value}
  486. self.assertDictEqual(result, self.base.annotations)
  487. def test_numpy_array_uint0(self):
  488. '''test to make sure uint0 type numpy arrays are accepted'''
  489. value = np.array([1, 2, 3, 4, 5], dtype=np.uint0)
  490. self.base.annotate(data=value)
  491. result = {'data': value}
  492. self.assertDictEqual(result, self.base.annotations)
  493. def test_numpy_array_int8(self):
  494. '''test to make sure int8 type numpy arrays are accepted'''
  495. value = np.array([1, 2, 3, 4, 5], dtype=np.int8)
  496. self.base.annotate(data=value)
  497. result = {'data': value}
  498. self.assertDictEqual(result, self.base.annotations)
  499. def test_numpy_array_uint8(self):
  500. '''test to make sure uint8 type numpy arrays are accepted'''
  501. value = np.array([1, 2, 3, 4, 5], dtype=np.uint8)
  502. self.base.annotate(data=value)
  503. result = {'data': value}
  504. self.assertDictEqual(result, self.base.annotations)
  505. def test_numpy_array_int16(self):
  506. '''test to make sure int16 type numpy arrays are accepted'''
  507. value = np.array([1, 2, 3, 4, 5], dtype=np.int16)
  508. self.base.annotate(data=value)
  509. result = {'data': value}
  510. self.assertDictEqual(result, self.base.annotations)
  511. def test_numpy_array_uint16(self):
  512. '''test to make sure uint16 type numpy arrays are accepted'''
  513. value = np.array([1, 2, 3, 4, 5], dtype=np.uint16)
  514. self.base.annotate(data=value)
  515. result = {'data': value}
  516. self.assertDictEqual(result, self.base.annotations)
  517. def test_numpy_array_int32(self):
  518. '''test to make sure int32 type numpy arrays are accepted'''
  519. value = np.array([1, 2, 3, 4, 5], dtype=np.int32)
  520. self.base.annotate(data=value)
  521. result = {'data': value}
  522. self.assertDictEqual(result, self.base.annotations)
  523. def test_numpy_array_uint32(self):
  524. '''test to make sure uint32 type numpy arrays are accepted'''
  525. value = np.array([1, 2, 3, 4, 5], dtype=np.uint32)
  526. self.base.annotate(data=value)
  527. result = {'data': value}
  528. self.assertDictEqual(result, self.base.annotations)
  529. def test_numpy_array_int64(self):
  530. '''test to make sure int64 type numpy arrays are accepted'''
  531. value = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  532. self.base.annotate(data=value)
  533. result = {'data': value}
  534. self.assertDictEqual(result, self.base.annotations)
  535. def test_numpy_array_uint64(self):
  536. '''test to make sure uint64 type numpy arrays are accepted'''
  537. value = np.array([1, 2, 3, 4, 5], dtype=np.uint64)
  538. self.base.annotate(data=value)
  539. result = {'data': value}
  540. self.assertDictEqual(result, self.base.annotations)
  541. def test_numpy_array_float(self):
  542. '''test to make sure float type numpy arrays are accepted'''
  543. value = np.array([1, 2, 3, 4, 5], dtype=np.float)
  544. self.base.annotate(data=value)
  545. result = {'data': value}
  546. self.assertDictEqual(result, self.base.annotations)
  547. def test_numpy_array_floating(self):
  548. '''test to make sure floating type numpy arrays are accepted'''
  549. value = np.array([1, 2, 3, 4, 5], dtype=np.floating)
  550. self.base.annotate(data=value)
  551. result = {'data': value}
  552. self.assertDictEqual(result, self.base.annotations)
  553. def test_numpy_array_double(self):
  554. '''test to make sure double type numpy arrays are accepted'''
  555. value = np.array([1, 2, 3, 4, 5], dtype=np.double)
  556. self.base.annotate(data=value)
  557. result = {'data': value}
  558. self.assertDictEqual(result, self.base.annotations)
  559. def test_numpy_array_float16(self):
  560. '''test to make sure float16 type numpy arrays are accepted'''
  561. value = np.array([1, 2, 3, 4, 5], dtype=np.float16)
  562. self.base.annotate(data=value)
  563. result = {'data': value}
  564. self.assertDictEqual(result, self.base.annotations)
  565. def test_numpy_array_float32(self):
  566. '''test to make sure float32 type numpy arrays are accepted'''
  567. value = np.array([1, 2, 3, 4, 5], dtype=np.float32)
  568. self.base.annotate(data=value)
  569. result = {'data': value}
  570. self.assertDictEqual(result, self.base.annotations)
  571. def test_numpy_array_float64(self):
  572. '''test to make sure float64 type numpy arrays are accepted'''
  573. value = np.array([1, 2, 3, 4, 5], dtype=np.float64)
  574. self.base.annotate(data=value)
  575. result = {'data': value}
  576. self.assertDictEqual(result, self.base.annotations)
  577. @unittest.skipUnless(hasattr(np, "float128"), "float128 not available")
  578. def test_numpy_array_float128(self):
  579. '''test to make sure float128 type numpy arrays are accepted'''
  580. value = np.array([1, 2, 3, 4, 5], dtype=np.float128)
  581. self.base.annotate(data=value)
  582. result = {'data': value}
  583. self.assertDictEqual(result, self.base.annotations)
  584. def test_numpy_array_complex(self):
  585. '''test to make sure complex type numpy arrays are accepted'''
  586. value = np.array([1, 2, 3, 4, 5], dtype=np.complex)
  587. self.base.annotate(data=value)
  588. result = {'data': value}
  589. self.assertDictEqual(result, self.base.annotations)
  590. def test_numpy_scalar_complex64(self):
  591. '''test to make sure complex64 type numpy arrays are accepted'''
  592. value = np.array([1, 2, 3, 4, 5], dtype=np.complex64)
  593. self.base.annotate(data=value)
  594. result = {'data': value}
  595. self.assertDictEqual(result, self.base.annotations)
  596. def test_numpy_scalar_complex128(self):
  597. '''test to make sure complex128 type numpy arrays are accepted'''
  598. value = np.array([1, 2, 3, 4, 5], dtype=np.complex128)
  599. self.base.annotate(data=value)
  600. result = {'data': value}
  601. self.assertDictEqual(result, self.base.annotations)
  602. @unittest.skipUnless(hasattr(np, "complex256"),
  603. "complex256 not available")
  604. def test_numpy_scalar_complex256(self):
  605. '''test to make sure complex256 type numpy arrays are accepted'''
  606. value = np.array([1, 2, 3, 4, 5], dtype=np.complex256)
  607. self.base.annotate(data=value)
  608. result = {'data': value}
  609. self.assertDictEqual(result, self.base.annotations)
  610. def test_numpy_array_bool(self):
  611. '''test to make sure bool type numpy arrays are accepted'''
  612. value = np.array([1, 2, 3, 4, 5], dtype=np.bool)
  613. self.base.annotate(data=value)
  614. result = {'data': value}
  615. self.assertDictEqual(result, self.base.annotations)
  616. def test_numpy_array_str(self):
  617. '''test to make sure str type numpy arrays are accepted'''
  618. value = np.array([1, 2, 3, 4, 5], dtype=np.str)
  619. self.base.annotate(data=value)
  620. result = {'data': value}
  621. self.assertDictEqual(result, self.base.annotations)
  622. def test_numpy_array_string0(self):
  623. '''test to make sure string0 type numpy arrays are accepted'''
  624. if sys.version_info[0] >= 3:
  625. dtype = np.str0
  626. else:
  627. dtype = np.string0
  628. value = np.array([1, 2, 3, 4, 5], dtype=dtype)
  629. self.base.annotate(data=value)
  630. result = {'data': value}
  631. self.assertDictEqual(result, self.base.annotations)
  632. class TestBaseNeoNumpyScalarTypes(unittest.TestCase):
  633. '''
  634. TestCase to make sure annotations are properly checked for numpy scalars
  635. '''
  636. def setUp(self):
  637. '''create the instance to be tested, called before every test'''
  638. self.base = BaseNeo()
  639. def test_numpy_scalar_int(self):
  640. '''test to make sure int type numpy scalars are accepted'''
  641. value = np.array(99, dtype=np.int)
  642. self.base.annotate(data=value)
  643. result = {'data': value}
  644. self.assertDictEqual(result, self.base.annotations)
  645. def test_numpy_scalar_uint(self):
  646. '''test to make sure uint type numpy scalars are accepted'''
  647. value = np.array(99, dtype=np.uint)
  648. self.base.annotate(data=value)
  649. result = {'data': value}
  650. self.assertDictEqual(result, self.base.annotations)
  651. def test_numpy_scalar_int0(self):
  652. '''test to make sure int0 type numpy scalars are accepted'''
  653. value = np.array(99, dtype=np.int0)
  654. self.base.annotate(data=value)
  655. result = {'data': value}
  656. self.assertDictEqual(result, self.base.annotations)
  657. def test_numpy_scalar_uint0(self):
  658. '''test to make sure uint0 type numpy scalars are accepted'''
  659. value = np.array(99, dtype=np.uint0)
  660. self.base.annotate(data=value)
  661. result = {'data': value}
  662. self.assertDictEqual(result, self.base.annotations)
  663. def test_numpy_scalar_int8(self):
  664. '''test to make sure int8 type numpy scalars are accepted'''
  665. value = np.array(99, dtype=np.int8)
  666. self.base.annotate(data=value)
  667. result = {'data': value}
  668. self.assertDictEqual(result, self.base.annotations)
  669. def test_numpy_scalar_uint8(self):
  670. '''test to make sure uint8 type numpy scalars are accepted'''
  671. value = np.array(99, dtype=np.uint8)
  672. self.base.annotate(data=value)
  673. result = {'data': value}
  674. self.assertDictEqual(result, self.base.annotations)
  675. def test_numpy_scalar_int16(self):
  676. '''test to make sure int16 type numpy scalars are accepted'''
  677. value = np.array(99, dtype=np.int16)
  678. self.base.annotate(data=value)
  679. result = {'data': value}
  680. self.assertDictEqual(result, self.base.annotations)
  681. def test_numpy_scalar_uint16(self):
  682. '''test to make sure uint16 type numpy scalars are accepted'''
  683. value = np.array(99, dtype=np.uint16)
  684. self.base.annotate(data=value)
  685. result = {'data': value}
  686. self.assertDictEqual(result, self.base.annotations)
  687. def test_numpy_scalar_int32(self):
  688. '''test to make sure int32 type numpy scalars are accepted'''
  689. value = np.array(99, dtype=np.int32)
  690. self.base.annotate(data=value)
  691. result = {'data': value}
  692. self.assertDictEqual(result, self.base.annotations)
  693. def test_numpy_scalar_uint32(self):
  694. '''test to make sure uint32 type numpy scalars are accepted'''
  695. value = np.array(99, dtype=np.uint32)
  696. self.base.annotate(data=value)
  697. result = {'data': value}
  698. self.assertDictEqual(result, self.base.annotations)
  699. def test_numpy_scalar_int64(self):
  700. '''test to make sure int64 type numpy scalars are accepted'''
  701. value = np.array(99, dtype=np.int64)
  702. self.base.annotate(data=value)
  703. result = {'data': value}
  704. self.assertDictEqual(result, self.base.annotations)
  705. def test_numpy_scalar_uint64(self):
  706. '''test to make sure uint64 type numpy scalars are accepted'''
  707. value = np.array(99, dtype=np.uint64)
  708. self.base.annotate(data=value)
  709. result = {'data': value}
  710. self.assertDictEqual(result, self.base.annotations)
  711. def test_numpy_scalar_float(self):
  712. '''test to make sure float type numpy scalars are accepted'''
  713. value = np.array(99, dtype=np.float)
  714. self.base.annotate(data=value)
  715. result = {'data': value}
  716. self.assertDictEqual(result, self.base.annotations)
  717. def test_numpy_scalar_floating(self):
  718. '''test to make sure floating type numpy scalars are accepted'''
  719. value = np.array(99, dtype=np.floating)
  720. self.base.annotate(data=value)
  721. result = {'data': value}
  722. self.assertDictEqual(result, self.base.annotations)
  723. def test_numpy_scalar_double(self):
  724. '''test to make sure double type numpy scalars are accepted'''
  725. value = np.array(99, dtype=np.double)
  726. self.base.annotate(data=value)
  727. result = {'data': value}
  728. self.assertDictEqual(result, self.base.annotations)
  729. def test_numpy_scalar_float16(self):
  730. '''test to make sure float16 type numpy scalars are accepted'''
  731. value = np.array(99, dtype=np.float16)
  732. self.base.annotate(data=value)
  733. result = {'data': value}
  734. self.assertDictEqual(result, self.base.annotations)
  735. def test_numpy_scalar_float32(self):
  736. '''test to make sure float32 type numpy scalars are accepted'''
  737. value = np.array(99, dtype=np.float32)
  738. self.base.annotate(data=value)
  739. result = {'data': value}
  740. self.assertDictEqual(result, self.base.annotations)
  741. def test_numpy_scalar_float64(self):
  742. '''test to make sure float64 type numpy scalars are accepted'''
  743. value = np.array(99, dtype=np.float64)
  744. self.base.annotate(data=value)
  745. result = {'data': value}
  746. self.assertDictEqual(result, self.base.annotations)
  747. @unittest.skipUnless(hasattr(np, "float128"), "float128 not available")
  748. def test_numpy_scalar_float128(self):
  749. '''test to make sure float128 type numpy scalars are accepted'''
  750. value = np.array(99, dtype=np.float128)
  751. self.base.annotate(data=value)
  752. result = {'data': value}
  753. self.assertDictEqual(result, self.base.annotations)
  754. def test_numpy_scalar_complex(self):
  755. '''test to make sure complex type numpy scalars are accepted'''
  756. value = np.array(99, dtype=np.complex)
  757. self.base.annotate(data=value)
  758. result = {'data': value}
  759. self.assertDictEqual(result, self.base.annotations)
  760. def test_numpy_scalar_complex64(self):
  761. '''test to make sure complex64 type numpy scalars are accepted'''
  762. value = np.array(99, dtype=np.complex64)
  763. self.base.annotate(data=value)
  764. result = {'data': value}
  765. self.assertDictEqual(result, self.base.annotations)
  766. def test_numpy_scalar_complex128(self):
  767. '''test to make sure complex128 type numpy scalars are accepted'''
  768. value = np.array(99, dtype=np.complex128)
  769. self.base.annotate(data=value)
  770. result = {'data': value}
  771. self.assertDictEqual(result, self.base.annotations)
  772. @unittest.skipUnless(hasattr(np, "complex256"), "complex256 not available")
  773. def test_numpy_scalar_complex256(self):
  774. '''test to make sure complex256 type numpy scalars are accepted'''
  775. value = np.array(99, dtype=np.complex256)
  776. self.base.annotate(data=value)
  777. result = {'data': value}
  778. self.assertDictEqual(result, self.base.annotations)
  779. def test_numpy_scalar_bool(self):
  780. '''test to make sure bool type numpy scalars are rejected'''
  781. value = np.array(99, dtype=np.bool)
  782. self.base.annotate(data=value)
  783. result = {'data': value}
  784. self.assertDictEqual(result, self.base.annotations)
  785. def test_numpy_array_str(self):
  786. '''test to make sure str type numpy scalars are accepted'''
  787. value = np.array(99, dtype=np.str)
  788. self.base.annotate(data=value)
  789. result = {'data': value}
  790. self.assertDictEqual(result, self.base.annotations)
  791. def test_numpy_scalar_string0(self):
  792. '''test to make sure string0 type numpy scalars are rejected'''
  793. if sys.version_info[0] >= 3:
  794. dtype = np.str0
  795. else:
  796. dtype = np.string0
  797. value = np.array(99, dtype=dtype)
  798. self.base.annotate(data=value)
  799. result = {'data': value}
  800. self.assertDictEqual(result, self.base.annotations)
  801. class TestBaseNeoQuantitiesArrayTypes(unittest.TestCase):
  802. '''
  803. TestCase to make sure annotations are properly checked for quantities
  804. arrays
  805. '''
  806. def setUp(self):
  807. '''create the instance to be tested, called before every test'''
  808. self.base = BaseNeo()
  809. def test_quantities_array_int(self):
  810. '''test to make sure int type quantites arrays are accepted'''
  811. value = pq.Quantity([1, 2, 3, 4, 5], dtype=np.int, units=pq.s)
  812. self.base.annotate(data=value)
  813. result = {'data': value}
  814. self.assertDictEqual(result, self.base.annotations)
  815. def test_quantities_array_uint(self):
  816. '''test to make sure uint type quantites arrays are accepted'''
  817. value = pq.Quantity([1, 2, 3, 4, 5], dtype=np.uint, units=pq.meter)
  818. self.base.annotate(data=value)
  819. result = {'data': value}
  820. self.assertDictEqual(result, self.base.annotations)
  821. def test_quantities_array_float(self):
  822. '''test to make sure float type quantites arrays are accepted'''
  823. value = [1, 2, 3, 4, 5] * pq.kg
  824. self.base.annotate(data=value)
  825. result = {'data': value}
  826. self.assertDictEqual(result, self.base.annotations)
  827. def test_quantities_array_str(self):
  828. '''test to make sure str type quantites arrays are accepted'''
  829. value = pq.Quantity([1, 2, 3, 4, 5], dtype=np.str, units=pq.meter)
  830. self.base.annotate(data=value)
  831. result = {'data': value}
  832. self.assertDictEqual(result, self.base.annotations)
  833. class TestBaseNeoQuantitiesScalarTypes(unittest.TestCase):
  834. '''
  835. TestCase to make sure annotations are properly checked for quantities
  836. scalars
  837. '''
  838. def setUp(self):
  839. '''create the instance to be tested, called before every test'''
  840. self.base = BaseNeo()
  841. def test_quantities_scalar_int(self):
  842. '''test to make sure int type quantites scalars are accepted'''
  843. value = pq.Quantity(99, dtype=np.int, units=pq.s)
  844. self.base.annotate(data=value)
  845. result = {'data': value}
  846. self.assertDictEqual(result, self.base.annotations)
  847. def test_quantities_scalar_uint(self):
  848. '''test to make sure uint type quantites scalars are accepted'''
  849. value = pq.Quantity(99, dtype=np.uint, units=pq.meter)
  850. self.base.annotate(data=value)
  851. result = {'data': value}
  852. self.assertDictEqual(result, self.base.annotations)
  853. def test_quantities_scalar_float(self):
  854. '''test to make sure float type quantites scalars are accepted'''
  855. value = 99 * pq.kg
  856. self.base.annotate(data=value)
  857. result = {'data': value}
  858. self.assertDictEqual(result, self.base.annotations)
  859. def test_quantities_scalar_str(self):
  860. '''test to make sure str type quantites scalars are accepted'''
  861. value = pq.Quantity(99, dtype=np.str, units=pq.meter)
  862. self.base.annotate(data=value)
  863. result = {'data': value}
  864. self.assertDictEqual(result, self.base.annotations)
  865. class TestBaseNeoUserDefinedTypes(unittest.TestCase):
  866. '''
  867. TestCase to make sure annotations are properly checked for arbitrary
  868. objects
  869. '''
  870. def setUp(self):
  871. '''create the instance to be tested, called before every test'''
  872. self.base = BaseNeo()
  873. def test_my_class(self):
  874. '''test to make sure user defined class type data is rejected'''
  875. class Foo(object):
  876. pass
  877. value = Foo()
  878. self.assertRaises(ValueError, self.base.annotate, data=value)
  879. def test_my_class_list(self):
  880. '''test to make sure user defined class type data is rejected'''
  881. class Foo(object):
  882. pass
  883. value = [Foo(), Foo(), Foo()]
  884. self.assertRaises(ValueError, self.base.annotate, data=value)
  885. @unittest.skipUnless(HAVE_IPYTHON, "requires IPython")
  886. class Test_pprint(unittest.TestCase):
  887. def test__pretty(self):
  888. name = 'an object'
  889. description = 'this is a test'
  890. obj = BaseNeo(name=name, description=description)
  891. res = pretty(obj)
  892. targ = "BaseNeo name: '%s' description: '%s'" % (name, description)
  893. self.assertEqual(res, targ)
  894. if __name__ == "__main__":
  895. unittest.main()