test_nestio.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. """
  2. Tests of neo.io.exampleio
  3. """
  4. import warnings
  5. import unittest
  6. import quantities as pq
  7. import numpy as np
  8. from neo.io.nestio import ColumnIO
  9. from neo.io.nestio import NestIO
  10. from neo.test.iotest.common_io_test import BaseTestIO
  11. from neo.test.iotest.tools import get_test_file_full_path
  12. class TestNestIO_Analogsignals(BaseTestIO, unittest.TestCase):
  13. ioclass = NestIO
  14. files_to_test = []
  15. files_to_download = ['0gid-1time-2gex-3Vm-1261-0.dat',
  16. '0gid-1time-2gex-1262-0.dat',
  17. '0gid-1time-2Vm-3gex-4gin-1260-0.dat',
  18. '0gid-1time-2Vm-3Iex-4Iin-1264-0.dat',
  19. '0gid-1time-2Vm-1259-0.dat',
  20. '0gid-1time-1256-0.gdf',
  21. '0gid-1time_in_steps-2Vm-1263-0.dat',
  22. '0gid-1time_in_steps-1258-0.gdf',
  23. '0time-1255-0.gdf',
  24. '0time_in_steps-1257-0.gdf',
  25. 'brunel-delta-nest_mod.py',
  26. 'N1-0gid-1time-2Vm-1265-0.dat',
  27. 'N1-0time-1Vm-1266-0.dat',
  28. 'N1-0Vm-1267-0.dat']
  29. def test_read_analogsignal(self):
  30. """
  31. Tests reading files in the 2 different formats:
  32. - with GIDs, with times as floats
  33. - with GIDs, with time as integer
  34. """
  35. filename = get_test_file_full_path(
  36. ioclass=NestIO,
  37. filename='0gid-1time-2gex-3Vm-1261-0.dat',
  38. directory=self.local_test_dir, clean=False)
  39. r = NestIO(filenames=filename)
  40. r.read_analogsignal(gid=1, t_stop=1000. * pq.ms,
  41. sampling_period=pq.ms, lazy=False,
  42. id_column=0, time_column=1,
  43. value_column=2, value_type='V_m')
  44. r.read_segment(gid_list=[1], t_stop=1000. * pq.ms,
  45. sampling_period=pq.ms, lazy=False, id_column_dat=0,
  46. time_column_dat=1, value_columns_dat=2,
  47. value_types='V_m')
  48. filename = get_test_file_full_path(
  49. ioclass=NestIO,
  50. filename='0gid-1time_in_steps-2Vm-1263-0.dat',
  51. directory=self.local_test_dir, clean=False)
  52. r = NestIO(filenames=filename)
  53. r.read_analogsignal(gid=1, t_stop=1000. * pq.ms,
  54. time_unit=pq.CompoundUnit('0.1*ms'),
  55. sampling_period=pq.ms, lazy=False,
  56. id_column=0, time_column=1,
  57. value_column=2, value_type='V_m')
  58. r.read_segment(gid_list=[1], t_stop=1000. * pq.ms,
  59. time_unit=pq.CompoundUnit('0.1*ms'),
  60. sampling_period=pq.ms, lazy=False, id_column_dat=0,
  61. time_column_dat=1, value_columns_dat=2,
  62. value_types='V_m')
  63. filename = get_test_file_full_path(
  64. ioclass=NestIO,
  65. filename='0gid-1time-2Vm-1259-0.dat',
  66. directory=self.local_test_dir, clean=False)
  67. r = NestIO(filenames=filename)
  68. r.read_analogsignal(gid=1, t_stop=1000. * pq.ms,
  69. time_unit=pq.CompoundUnit('0.1*ms'),
  70. sampling_period=pq.ms, lazy=False,
  71. id_column=0, time_column=1,
  72. value_column=2, value_type='V_m')
  73. r.read_segment(gid_list=[1], t_stop=1000. * pq.ms,
  74. time_unit=pq.CompoundUnit('0.1*ms'),
  75. sampling_period=pq.ms, lazy=False, id_column_dat=0,
  76. time_column_dat=1, value_columns_dat=2,
  77. value_types='V_m')
  78. def test_id_column_none_multiple_neurons(self):
  79. """
  80. Tests if function correctly raises an error if the user tries to read
  81. from a file which does not contain unit IDs, but data for multiple
  82. units.
  83. """
  84. filename = get_test_file_full_path(
  85. ioclass=NestIO,
  86. filename='0time-1255-0.gdf',
  87. directory=self.local_test_dir, clean=False)
  88. r = NestIO(filenames=filename)
  89. with self.assertRaises(ValueError):
  90. r.read_analogsignal(t_stop=1000. * pq.ms, lazy=False,
  91. sampling_period=pq.ms,
  92. id_column=None, time_column=0,
  93. value_column=1)
  94. r.read_segment(t_stop=1000. * pq.ms, lazy=False,
  95. sampling_period=pq.ms, id_column_gdf=None,
  96. time_column_gdf=0)
  97. def test_values(self):
  98. """
  99. Tests if the function returns the correct values.
  100. """
  101. filename = get_test_file_full_path(
  102. ioclass=NestIO,
  103. filename='0gid-1time-2gex-3Vm-1261-0.dat',
  104. directory=self.local_test_dir, clean=False)
  105. id_to_test = 1
  106. r = NestIO(filenames=filename)
  107. seg = r.read_segment(gid_list=[id_to_test],
  108. t_stop=1000. * pq.ms,
  109. sampling_period=pq.ms, lazy=False,
  110. id_column_dat=0, time_column_dat=1,
  111. value_columns_dat=2, value_types='V_m')
  112. dat = np.loadtxt(filename)
  113. target_data = dat[:, 2][np.where(dat[:, 0] == id_to_test)]
  114. target_data = target_data[:, None]
  115. st = seg.analogsignals[0]
  116. np.testing.assert_array_equal(st.magnitude, target_data)
  117. def test_read_segment(self):
  118. """
  119. Tests if signals are correctly stored in a segment.
  120. """
  121. filename = get_test_file_full_path(
  122. ioclass=NestIO,
  123. filename='0gid-1time-2gex-1262-0.dat',
  124. directory=self.local_test_dir, clean=False)
  125. r = NestIO(filenames=filename)
  126. id_list_to_test = range(1, 10)
  127. seg = r.read_segment(gid_list=id_list_to_test,
  128. t_stop=1000. * pq.ms,
  129. sampling_period=pq.ms, lazy=False,
  130. id_column_dat=0, time_column_dat=1,
  131. value_columns_dat=2, value_types='V_m')
  132. self.assertTrue(len(seg.analogsignals) == len(id_list_to_test))
  133. id_list_to_test = []
  134. seg = r.read_segment(gid_list=id_list_to_test,
  135. t_stop=1000. * pq.ms,
  136. sampling_period=pq.ms, lazy=False,
  137. id_column_dat=0, time_column_dat=1,
  138. value_columns_dat=2, value_types='V_m')
  139. self.assertEqual(len(seg.analogsignals), 50)
  140. def test_read_block(self):
  141. """
  142. Tests if signals are correctly stored in a block.
  143. """
  144. filename = get_test_file_full_path(
  145. ioclass=NestIO,
  146. filename='0gid-1time-2gex-1262-0.dat',
  147. directory=self.local_test_dir, clean=False)
  148. r = NestIO(filenames=filename)
  149. id_list_to_test = range(1, 10)
  150. blk = r.read_block(gid_list=id_list_to_test,
  151. t_stop=1000. * pq.ms,
  152. sampling_period=pq.ms, lazy=False,
  153. id_column_dat=0, time_column_dat=1,
  154. value_columns_dat=2, value_types='V_m')
  155. self.assertTrue(len(blk.segments[0].analogsignals) == len(id_list_to_test))
  156. def test_wrong_input(self):
  157. """
  158. Tests two cases of wrong user input, namely
  159. - User does not specify a value column
  160. - User does not make any specifications
  161. - User does not define sampling_period as a unit
  162. - User specifies a non-default value type without
  163. specifying a value_unit
  164. - User specifies t_start < 1.*sampling_period
  165. """
  166. filename = get_test_file_full_path(
  167. ioclass=NestIO,
  168. filename='0gid-1time-2gex-1262-0.dat',
  169. directory=self.local_test_dir, clean=False)
  170. r = NestIO(filenames=filename)
  171. with self.assertRaises(ValueError):
  172. r.read_segment(t_stop=1000. * pq.ms, lazy=False,
  173. id_column_dat=0, time_column_dat=1)
  174. with self.assertRaises(ValueError):
  175. r.read_segment()
  176. with self.assertRaises(ValueError):
  177. r.read_segment(gid_list=[1], t_stop=1000. * pq.ms,
  178. sampling_period=1. * pq.ms, lazy=False,
  179. id_column_dat=0, time_column_dat=1,
  180. value_columns_dat=2, value_types='V_m')
  181. with self.assertRaises(ValueError):
  182. r.read_segment(gid_list=[1], t_stop=1000. * pq.ms,
  183. sampling_period=pq.ms, lazy=False,
  184. id_column_dat=0, time_column_dat=1,
  185. value_columns_dat=2, value_types='U_mem')
  186. def test_t_start_t_stop(self):
  187. """
  188. Test for correct t_start and t_stop values of AnalogSignalArrays.
  189. """
  190. filename = get_test_file_full_path(
  191. ioclass=NestIO,
  192. filename='0gid-1time-2gex-1262-0.dat',
  193. directory=self.local_test_dir, clean=False)
  194. r = NestIO(filenames=filename)
  195. t_start_targ = 450. * pq.ms
  196. t_stop_targ = 480. * pq.ms
  197. seg = r.read_segment(gid_list=[], t_start=t_start_targ,
  198. t_stop=t_stop_targ, lazy=False,
  199. id_column_dat=0, time_column_dat=1,
  200. value_columns_dat=2, value_types='V_m')
  201. anasigs = seg.analogsignals
  202. for anasig in anasigs:
  203. self.assertTrue(anasig.t_start == t_start_targ)
  204. self.assertTrue(anasig.t_stop == t_stop_targ)
  205. def test_notimeid(self):
  206. """
  207. Test for warning, when no time column id was provided.
  208. """
  209. filename = get_test_file_full_path(
  210. ioclass=NestIO,
  211. filename='0gid-1time-2gex-1262-0.dat',
  212. directory=self.local_test_dir, clean=False)
  213. r = NestIO(filenames=filename)
  214. t_start_targ = 450. * pq.ms
  215. t_stop_targ = 460. * pq.ms
  216. sampling_period = pq.CompoundUnit('5*ms')
  217. with warnings.catch_warnings(record=True) as w:
  218. # Cause all warnings to always be triggered.
  219. warnings.simplefilter("always")
  220. seg = r.read_segment(gid_list=[], t_start=t_start_targ,
  221. sampling_period=sampling_period,
  222. t_stop=t_stop_targ, lazy=False,
  223. id_column_dat=0, time_column_dat=None,
  224. value_columns_dat=2, value_types='V_m')
  225. # Verify number and content of warning
  226. self.assertEqual(len(w), 1)
  227. self.assertIn("no time column id", str(w[0].message))
  228. sts = seg.analogsignals
  229. for st in sts:
  230. self.assertTrue(st.t_start == 1 * 5 * pq.ms)
  231. self.assertTrue(
  232. st.t_stop == len(st) * sampling_period + 1 * 5 * pq.ms)
  233. def test_multiple_value_columns(self):
  234. """
  235. Test for simultaneous loading of multiple columns from dat file.
  236. """
  237. filename = get_test_file_full_path(
  238. ioclass=NestIO,
  239. filename='0gid-1time-2Vm-3Iex-4Iin-1264-0.dat',
  240. directory=self.local_test_dir, clean=False)
  241. r = NestIO(filenames=filename)
  242. sampling_period = pq.CompoundUnit('5*ms')
  243. seg = r.read_segment(gid_list=[1001],
  244. value_columns_dat=[2, 3],
  245. sampling_period=sampling_period)
  246. anasigs = seg.analogsignals
  247. self.assertEqual(len(anasigs), 2)
  248. def test_single_gid(self):
  249. filename = get_test_file_full_path(
  250. ioclass=NestIO,
  251. filename='N1-0gid-1time-2Vm-1265-0.dat',
  252. directory=self.local_test_dir, clean=False)
  253. r = NestIO(filenames=filename)
  254. anasig = r.read_analogsignal(gid=1, t_stop=1000. * pq.ms,
  255. time_unit=pq.CompoundUnit('0.1*ms'),
  256. sampling_period=pq.ms, lazy=False,
  257. id_column=0, time_column=1,
  258. value_column=2, value_type='V_m')
  259. assert anasig.annotations['id'] == 1
  260. def test_no_gid(self):
  261. filename = get_test_file_full_path(
  262. ioclass=NestIO,
  263. filename='N1-0time-1Vm-1266-0.dat',
  264. directory=self.local_test_dir, clean=False)
  265. r = NestIO(filenames=filename)
  266. anasig = r.read_analogsignal(gid=None, t_stop=1000. * pq.ms,
  267. time_unit=pq.CompoundUnit('0.1*ms'),
  268. sampling_period=pq.ms, lazy=False,
  269. id_column=None, time_column=0,
  270. value_column=1, value_type='V_m')
  271. self.assertEqual(anasig.annotations['id'], None)
  272. self.assertEqual(len(anasig), 19)
  273. def test_no_gid_no_time(self):
  274. filename = get_test_file_full_path(
  275. ioclass=NestIO,
  276. filename='N1-0Vm-1267-0.dat',
  277. directory=self.local_test_dir, clean=False)
  278. r = NestIO(filenames=filename)
  279. anasig = r.read_analogsignal(gid=None,
  280. sampling_period=pq.ms, lazy=False,
  281. id_column=None, time_column=None,
  282. value_column=0, value_type='V_m')
  283. self.assertEqual(anasig.annotations['id'], None)
  284. self.assertEqual(len(anasig), 19)
  285. class TestNestIO_Spiketrains(BaseTestIO, unittest.TestCase):
  286. ioclass = NestIO
  287. files_to_test = []
  288. files_to_download = []
  289. def test_read_spiketrain(self):
  290. """
  291. Tests reading files in the 4 different formats:
  292. - without GIDs, with times as floats
  293. - without GIDs, with times as integers in time steps
  294. - with GIDs, with times as floats
  295. - with GIDs, with times as integers in time steps
  296. """
  297. filename = get_test_file_full_path(
  298. ioclass=NestIO,
  299. filename='0time-1255-0.gdf',
  300. directory=self.local_test_dir, clean=False)
  301. r = NestIO(filenames=filename)
  302. r.read_spiketrain(t_start=400. * pq.ms, t_stop=500. * pq.ms, lazy=False,
  303. id_column=None, time_column=0)
  304. r.read_segment(t_start=400. * pq.ms, t_stop=500. * pq.ms, lazy=False,
  305. id_column_gdf=None, time_column_gdf=0)
  306. filename = get_test_file_full_path(
  307. ioclass=NestIO,
  308. filename='0time_in_steps-1257-0.gdf',
  309. directory=self.local_test_dir, clean=False)
  310. r = NestIO(filenames=filename)
  311. r.read_spiketrain(t_start=400. * pq.ms, t_stop=500. * pq.ms,
  312. time_unit=pq.CompoundUnit('0.1*ms'), lazy=False,
  313. id_column=None, time_column=0)
  314. r.read_segment(t_start=400. * pq.ms, t_stop=500. * pq.ms,
  315. time_unit=pq.CompoundUnit('0.1*ms'), lazy=False,
  316. id_column_gdf=None, time_column_gdf=0)
  317. filename = get_test_file_full_path(
  318. ioclass=NestIO,
  319. filename='0gid-1time-1256-0.gdf',
  320. directory=self.local_test_dir, clean=False)
  321. r = NestIO(filenames=filename)
  322. r.read_spiketrain(gdf_id=1, t_start=400. * pq.ms, t_stop=500. * pq.ms,
  323. lazy=False, id_column_gdf=0, time_column_gdf=1)
  324. r.read_segment(gid_list=[1], t_start=400. * pq.ms, t_stop=500. * pq.ms,
  325. lazy=False, id_column_gdf=0, time_column_gdf=1)
  326. filename = get_test_file_full_path(
  327. ioclass=NestIO,
  328. filename='0gid-1time_in_steps-1258-0.gdf',
  329. directory=self.local_test_dir, clean=False)
  330. r = NestIO(filenames=filename)
  331. r.read_spiketrain(gdf_id=1, t_start=400. * pq.ms, t_stop=500. * pq.ms,
  332. time_unit=pq.CompoundUnit('0.1*ms'), lazy=False,
  333. id_column=0, time_column=1)
  334. r.read_segment(gid_list=[1], t_start=400. * pq.ms, t_stop=500. * pq.ms,
  335. time_unit=pq.CompoundUnit('0.1*ms'), lazy=False,
  336. id_column_gdf=0, time_column_gdf=1)
  337. def test_read_integer(self):
  338. """
  339. Tests if spike times are actually stored as integers if they are stored
  340. in time steps in the file.
  341. """
  342. filename = get_test_file_full_path(
  343. ioclass=NestIO,
  344. filename='0time_in_steps-1257-0.gdf',
  345. directory=self.local_test_dir, clean=False)
  346. r = NestIO(filenames=filename)
  347. st = r.read_spiketrain(gdf_id=None, t_start=400. * pq.ms,
  348. t_stop=500. * pq.ms,
  349. time_unit=pq.CompoundUnit('0.1*ms'),
  350. lazy=False, id_column=None, time_column=0)
  351. self.assertTrue(st.magnitude.dtype == np.int32)
  352. seg = r.read_segment(gid_list=[None], t_start=400. * pq.ms,
  353. t_stop=500. * pq.ms,
  354. time_unit=pq.CompoundUnit('0.1*ms'),
  355. lazy=False, id_column_gdf=None, time_column_gdf=0)
  356. sts = seg.spiketrains
  357. self.assertTrue(all([st.magnitude.dtype == np.int32 for st in sts]))
  358. filename = get_test_file_full_path(
  359. ioclass=NestIO,
  360. filename='0gid-1time_in_steps-1258-0.gdf',
  361. directory=self.local_test_dir, clean=False)
  362. r = NestIO(
  363. filenames=filename)
  364. st = r.read_spiketrain(gdf_id=1, t_start=400. * pq.ms,
  365. t_stop=500. * pq.ms,
  366. time_unit=pq.CompoundUnit('0.1*ms'),
  367. lazy=False, id_column=0, time_column=1)
  368. self.assertTrue(st.magnitude.dtype == np.int32)
  369. seg = r.read_segment(gid_list=[1], t_start=400. * pq.ms,
  370. t_stop=500. * pq.ms,
  371. time_unit=pq.CompoundUnit('0.1*ms'),
  372. lazy=False, id_column_gdf=0, time_column_gdf=1)
  373. sts = seg.spiketrains
  374. self.assertTrue(all([st.magnitude.dtype == np.int32 for st in sts]))
  375. def test_read_float(self):
  376. """
  377. Tests if spike times are stored as floats if they
  378. are stored as floats in the file.
  379. """
  380. filename = get_test_file_full_path(
  381. ioclass=NestIO,
  382. filename='0gid-1time-1256-0.gdf',
  383. directory=self.local_test_dir, clean=False)
  384. r = NestIO(filenames=filename)
  385. st = r.read_spiketrain(gdf_id=1, t_start=400. * pq.ms,
  386. t_stop=500. * pq.ms,
  387. lazy=False, id_column=0, time_column=1)
  388. self.assertTrue(st.magnitude.dtype == np.float)
  389. seg = r.read_segment(gid_list=[1], t_start=400. * pq.ms,
  390. t_stop=500. * pq.ms,
  391. lazy=False, id_column_gdf=0, time_column_gdf=1)
  392. sts = seg.spiketrains
  393. self.assertTrue(all([s.magnitude.dtype == np.float for s in sts]))
  394. def test_values(self):
  395. """
  396. Tests if the routine loads the correct numbers from the file.
  397. """
  398. id_to_test = 1
  399. filename = get_test_file_full_path(
  400. ioclass=NestIO,
  401. filename='0gid-1time-1256-0.gdf',
  402. directory=self.local_test_dir, clean=False)
  403. r = NestIO(filenames=filename)
  404. seg = r.read_segment(gid_list=[id_to_test],
  405. t_start=400. * pq.ms,
  406. t_stop=500. * pq.ms, lazy=False,
  407. id_column_gdf=0, time_column_gdf=1)
  408. dat = np.loadtxt(filename)
  409. target_data = dat[:, 1][np.where(dat[:, 0] == id_to_test)]
  410. st = seg.spiketrains[0]
  411. np.testing.assert_array_equal(st.magnitude, target_data)
  412. def test_read_segment(self):
  413. """
  414. Tests if spiketrains are correctly stored in a segment.
  415. """
  416. filename = get_test_file_full_path(
  417. ioclass=NestIO,
  418. filename='0gid-1time-1256-0.gdf',
  419. directory=self.local_test_dir, clean=False)
  420. r = NestIO(filenames=filename)
  421. id_list_to_test = range(1, 10)
  422. seg = r.read_segment(gid_list=id_list_to_test, t_start=400. * pq.ms,
  423. t_stop=500. * pq.ms, lazy=False,
  424. id_column_gdf=0, time_column_gdf=1)
  425. self.assertTrue(len(seg.spiketrains) == len(id_list_to_test))
  426. id_list_to_test = []
  427. seg = r.read_segment(gid_list=id_list_to_test, t_start=400. * pq.ms,
  428. t_stop=500. * pq.ms, lazy=False,
  429. id_column_gdf=0, time_column_gdf=1)
  430. self.assertTrue(len(seg.spiketrains) == 50)
  431. def test_read_segment_accepts_range(self):
  432. """
  433. Tests if spiketrains can be retrieved by specifying a range of GDF IDs.
  434. """
  435. filename = get_test_file_full_path(
  436. ioclass=NestIO,
  437. filename='0gid-1time-1256-0.gdf',
  438. directory=self.local_test_dir, clean=False)
  439. r = NestIO(filenames=filename)
  440. seg = r.read_segment(gid_list=(10, 39), t_start=400. * pq.ms,
  441. t_stop=500. * pq.ms, lazy=False,
  442. id_column_gdf=0, time_column_gdf=1)
  443. self.assertEqual(len(seg.spiketrains), 30)
  444. def test_read_segment_range_is_reasonable(self):
  445. """
  446. Tests if error is thrown correctly, when second entry is smaller than
  447. the first one of the range.
  448. """
  449. filename = get_test_file_full_path(
  450. ioclass=NestIO,
  451. filename='0gid-1time-1256-0.gdf',
  452. directory=self.local_test_dir, clean=False)
  453. r = NestIO(filenames=filename)
  454. seg = r.read_segment(gid_list=(10, 10), t_start=400. * pq.ms,
  455. t_stop=500. * pq.ms, lazy=False,
  456. id_column_gdf=0, time_column_gdf=1)
  457. self.assertEqual(len(seg.spiketrains), 1)
  458. with self.assertRaises(ValueError):
  459. r.read_segment(gid_list=(10, 9), t_start=400. * pq.ms,
  460. t_stop=500. * pq.ms, lazy=False,
  461. id_column_gdf=0, time_column_gdf=1)
  462. def test_read_spiketrain_annotates(self):
  463. """
  464. Tests if correct annotation is added when reading a spike train.
  465. """
  466. filename = get_test_file_full_path(
  467. ioclass=NestIO,
  468. filename='0gid-1time-1256-0.gdf',
  469. directory=self.local_test_dir, clean=False)
  470. r = NestIO(filenames=filename)
  471. ID = 7
  472. st = r.read_spiketrain(gdf_id=ID, t_start=400. * pq.ms,
  473. t_stop=500. * pq.ms)
  474. self.assertEqual(ID, st.annotations['id'])
  475. def test_read_segment_annotates(self):
  476. """
  477. Tests if correct annotation is added when reading a segment.
  478. """
  479. filename = get_test_file_full_path(
  480. ioclass=NestIO,
  481. filename='0gid-1time-1256-0.gdf',
  482. directory=self.local_test_dir, clean=False)
  483. r = NestIO(filenames=filename)
  484. IDs = (5, 11)
  485. sts = r.read_segment(gid_list=(5, 11), t_start=400. * pq.ms,
  486. t_stop=500. * pq.ms)
  487. for ID in np.arange(5, 12):
  488. self.assertEqual(ID, sts.spiketrains[ID - 5].annotations['id'])
  489. def test_adding_custom_annotation(self):
  490. """
  491. Tests if custom annotation is correctly added.
  492. """
  493. filename = get_test_file_full_path(
  494. ioclass=NestIO,
  495. filename='0gid-1time-1256-0.gdf',
  496. directory=self.local_test_dir, clean=False)
  497. r = NestIO(filenames=filename)
  498. st = r.read_spiketrain(gdf_id=0, t_start=400. * pq.ms,
  499. t_stop=500. * pq.ms,
  500. layer='L23', population='I')
  501. self.assertEqual(0, st.annotations.pop('id'))
  502. self.assertEqual('L23', st.annotations.pop('layer'))
  503. self.assertEqual('I', st.annotations.pop('population'))
  504. self.assertEqual({}, st.annotations)
  505. def test_wrong_input(self):
  506. """
  507. Tests two cases of wrong user input, namely
  508. - User does not specify neuron IDs although the file contains IDs.
  509. - User does not make any specifications.
  510. """
  511. filename = get_test_file_full_path(
  512. ioclass=NestIO,
  513. filename='0gid-1time-1256-0.gdf',
  514. directory=self.local_test_dir, clean=False)
  515. r = NestIO(filenames=filename)
  516. with self.assertRaises(ValueError):
  517. r.read_segment(t_start=400. * pq.ms, t_stop=500. * pq.ms,
  518. lazy=False,
  519. id_column_gdf=0, time_column_gdf=1)
  520. with self.assertRaises(ValueError):
  521. r.read_segment()
  522. def test_t_start_t_stop(self):
  523. """
  524. Tests if the t_start and t_stop arguments are correctly processed.
  525. """
  526. filename = get_test_file_full_path(
  527. ioclass=NestIO,
  528. filename='0gid-1time-1256-0.gdf',
  529. directory=self.local_test_dir, clean=False)
  530. r = NestIO(filenames=filename)
  531. t_stop_targ = 490. * pq.ms
  532. t_start_targ = 410. * pq.ms
  533. seg = r.read_segment(gid_list=[], t_start=t_start_targ,
  534. t_stop=t_stop_targ, lazy=False,
  535. id_column_gdf=0, time_column_gdf=1)
  536. sts = seg.spiketrains
  537. self.assertTrue(np.max([np.max(st.magnitude) for st in sts
  538. if len(st) > 0])
  539. < t_stop_targ.rescale(sts[0].times.units).magnitude)
  540. self.assertTrue(np.min([np.min(st.magnitude) for st in sts
  541. if len(st) > 0])
  542. >= t_start_targ.rescale(sts[0].times.units).magnitude)
  543. def test_t_start_undefined_raises_error(self):
  544. """
  545. Tests if undefined t_start, i.e., t_start=None raises error.
  546. """
  547. filename = get_test_file_full_path(
  548. ioclass=NestIO,
  549. filename='0gid-1time-1256-0.gdf',
  550. directory=self.local_test_dir, clean=False)
  551. r = NestIO(filenames=filename)
  552. with self.assertRaises(ValueError):
  553. r.read_spiketrain(gdf_id=1, t_stop=500. * pq.ms, lazy=False,
  554. id_column=0, time_column=1)
  555. with self.assertRaises(ValueError):
  556. r.read_segment(gid_list=[1, 2, 3], t_stop=500. * pq.ms, lazy=False,
  557. id_column_gdf=0, time_column_gdf=1)
  558. def test_t_stop_undefined_raises_error(self):
  559. """
  560. Tests if undefined t_stop, i.e., t_stop=None raises error.
  561. """
  562. filename = get_test_file_full_path(
  563. ioclass=NestIO,
  564. filename='0gid-1time-1256-0.gdf',
  565. directory=self.local_test_dir, clean=False)
  566. r = NestIO(filenames=filename)
  567. with self.assertRaises(ValueError):
  568. r.read_spiketrain(gdf_id=1, t_start=400. * pq.ms, lazy=False,
  569. id_column=0, time_column=1)
  570. with self.assertRaises(ValueError):
  571. r.read_segment(gid_list=[1, 2, 3], t_start=400. * pq.ms, lazy=False,
  572. id_column_gdf=0, time_column_gdf=1)
  573. def test_gdf_id_illdefined_raises_error(self):
  574. """
  575. Tests if ill-defined gdf_id in read_spiketrain (i.e., None, list, or
  576. empty list) raises error.
  577. """
  578. filename = get_test_file_full_path(
  579. ioclass=NestIO,
  580. filename='0gid-1time-1256-0.gdf',
  581. directory=self.local_test_dir, clean=False)
  582. r = NestIO(filenames=filename)
  583. with self.assertRaises(ValueError):
  584. r.read_spiketrain(gdf_id=[], t_start=400. * pq.ms,
  585. t_stop=500. * pq.ms)
  586. with self.assertRaises(ValueError):
  587. r.read_spiketrain(gdf_id=[1], t_start=400. * pq.ms,
  588. t_stop=500. * pq.ms)
  589. with self.assertRaises(ValueError):
  590. r.read_spiketrain(t_start=400. * pq.ms, t_stop=500. * pq.ms)
  591. def test_read_segment_can_return_empty_spiketrains(self):
  592. """
  593. Tests if read_segment makes sure that only non-zero spike trains are
  594. returned.
  595. """
  596. filename = get_test_file_full_path(
  597. ioclass=NestIO,
  598. filename='0gid-1time-1256-0.gdf',
  599. directory=self.local_test_dir, clean=False)
  600. r = NestIO(filenames=filename)
  601. seg = r.read_segment(gid_list=[], t_start=400.4 * pq.ms,
  602. t_stop=400.5 * pq.ms)
  603. for st in seg.spiketrains:
  604. self.assertEqual(st.size, 0)
  605. def test_read_spiketrain_can_return_empty_spiketrain(self):
  606. """
  607. Tests if read_spiketrain returns an empty SpikeTrain if no spikes are in
  608. time range.
  609. """
  610. filename = get_test_file_full_path(
  611. ioclass=NestIO,
  612. filename='0gid-1time-1256-0.gdf',
  613. directory=self.local_test_dir, clean=False)
  614. r = NestIO(filenames=filename)
  615. st = r.read_spiketrain(gdf_id=0, t_start=400. * pq.ms,
  616. t_stop=410. * pq.ms)
  617. self.assertEqual(st.size, 0)
  618. class TestNestIO_multiple_signal_types(BaseTestIO, unittest.TestCase):
  619. ioclass = NestIO
  620. files_to_test = []
  621. files_to_download = ['0gid-1time-2gex-3Vm-1261-0.dat',
  622. '0gid-1time_in_steps-1258-0.gdf']
  623. def test_read_analogsignal_and_spiketrain(self):
  624. """
  625. Test if spiketrains and analogsignals can be read simultaneously
  626. using read_segment
  627. """
  628. files = ['0gid-1time-2gex-3Vm-1261-0.dat',
  629. '0gid-1time_in_steps-1258-0.gdf']
  630. filenames = [get_test_file_full_path(ioclass=NestIO, filename=file,
  631. directory=self.local_test_dir,
  632. clean=False)
  633. for file in files]
  634. r = NestIO(filenames=filenames)
  635. seg = r.read_segment(gid_list=[], t_start=400 * pq.ms,
  636. t_stop=600 * pq.ms,
  637. id_column_gdf=0, time_column_gdf=1,
  638. id_column_dat=0, time_column_dat=1,
  639. value_columns_dat=2)
  640. self.assertEqual(len(seg.spiketrains), 50)
  641. self.assertEqual(len(seg.analogsignals), 50)
  642. class TestColumnIO(BaseTestIO, unittest.TestCase):
  643. ioclass = NestIO
  644. files_to_download = ['0gid-1time-2Vm-3gex-4gin-1260-0.dat']
  645. def setUp(self):
  646. BaseTestIO.setUp(self)
  647. filename = get_test_file_full_path(
  648. ioclass=NestIO,
  649. filename='0gid-1time-2Vm-3gex-4gin-1260-0.dat',
  650. directory=self.local_test_dir, clean=False)
  651. self.testIO = ColumnIO(filename=filename)
  652. def test_no_arguments(self):
  653. """
  654. Test if data can be read using the default keyword arguments.
  655. """
  656. columns = self.testIO.get_columns()
  657. expected = self.testIO.data
  658. np.testing.assert_array_equal(columns, expected)
  659. def test_single_column_id(self):
  660. """
  661. Test if the column_ids keywords works properly.
  662. """
  663. column = self.testIO.get_columns(column_ids=1)
  664. expected = self.testIO.data[:, [1]]
  665. np.testing.assert_array_equal(column, expected)
  666. def test_multiple_column_ids(self):
  667. """
  668. Test if multiple columns can be read at the same time.
  669. """
  670. columns = self.testIO.get_columns(column_ids=range(2))
  671. expected = self.testIO.data[:, [0, 1]]
  672. np.testing.assert_array_equal(columns, expected)
  673. def test_no_condition(self):
  674. """
  675. Test if a missing condition function leads to a warning
  676. """
  677. with warnings.catch_warnings(record=True) as w:
  678. # Cause all warnings to always be triggered.
  679. warnings.simplefilter("always")
  680. self.testIO.get_columns(condition_column=0)
  681. # Verify number and content of warning
  682. assert len(w) == 1
  683. assert "no condition" in str(w[-1].message)
  684. def test_no_condition_column(self):
  685. """
  686. Test if a missing condition column leads to an error
  687. """
  688. with self.assertRaises(ValueError) as context:
  689. self.testIO.get_columns(condition=lambda x: True)
  690. self.assertTrue('no condition_column ID provided' in
  691. str(context.exception))
  692. def test_correct_condition_selection(self):
  693. """
  694. Test if combination of condition function and condition_column works
  695. properly.
  696. """
  697. condition_column = 0
  698. def condition_function(x): return x > 10
  699. result = self.testIO.get_columns(condition=condition_function,
  700. condition_column=0)
  701. selected_ids = np.where(condition_function(self.testIO.data[:,
  702. condition_column]))[0]
  703. expected = self.testIO.data[selected_ids, :]
  704. np.testing.assert_array_equal(result, expected)
  705. assert all(condition_function(result[:, condition_column]))
  706. def test_sorting(self):
  707. """
  708. Test if presorting of columns work properly.
  709. """
  710. result = self.testIO.get_columns(sorting_columns=0)
  711. assert len(result) > 0
  712. assert all(np.diff(result[:, 0]) >= 0)
  713. if __name__ == "__main__":
  714. unittest.main()