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