Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

test_nestio.py 34 KB

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