test_base.py 41 KB

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