test_brainwaredamio.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. Tests of neo.io.brainwaredamio
  3. """
  4. import os.path
  5. import unittest
  6. import numpy as np
  7. import quantities as pq
  8. from neo.core import (AnalogSignal, Block,
  9. Group, Segment)
  10. from neo.io import BrainwareDamIO
  11. from neo.test.iotest.common_io_test import BaseTestIO
  12. from neo.test.tools import (assert_same_sub_schema,
  13. assert_neo_object_is_compliant)
  14. from neo.test.iotest.tools import create_generic_reader
  15. def proc_dam(filename):
  16. '''Load an dam file that has already been processed by the official matlab
  17. file converter. That matlab data is saved to an m-file, which is then
  18. converted to a numpy '.npz' file. This numpy file is the file actually
  19. loaded. This function converts it to a neo block and returns the block.
  20. This block can be compared to the block produced by BrainwareDamIO to
  21. make sure BrainwareDamIO is working properly
  22. block = proc_dam(filename)
  23. filename: The file name of the numpy file to load. It should end with
  24. '*_dam_py?.npz'. This will be converted to a neo 'file_origin' property
  25. with the value '*.dam', so the filename to compare should fit that pattern.
  26. 'py?' should be 'py2' for the python 2 version of the numpy file or 'py3'
  27. for the python 3 version of the numpy file.
  28. example: filename = 'file1_dam_py2.npz'
  29. dam file name = 'file1.dam'
  30. '''
  31. with np.load(filename, allow_pickle=True) as damobj:
  32. damfile = list(damobj.items())[0][1].flatten()
  33. filename = os.path.basename(filename[:-12] + '.dam')
  34. signals = [res.flatten() for res in damfile['signal']]
  35. stimIndexes = [int(res[0, 0].tolist()) for res in damfile['stimIndex']]
  36. timestamps = [res[0, 0] for res in damfile['timestamp']]
  37. block = Block(file_origin=filename)
  38. gr = Group(file_origin=filename)
  39. block.groups.append(gr)
  40. params = [res['params'][0, 0].flatten() for res in damfile['stim']]
  41. values = [res['values'][0, 0].flatten() for res in damfile['stim']]
  42. params = [[res1[0] for res1 in res] for res in params]
  43. values = [[res1 for res1 in res] for res in values]
  44. stims = [dict(zip(param, value)) for param, value in zip(params, values)]
  45. fulldam = zip(stimIndexes, timestamps, signals, stims)
  46. for stimIndex, timestamp, signal, stim in fulldam:
  47. sig = AnalogSignal(signal=signal * pq.mV,
  48. t_start=timestamp * pq.d,
  49. file_origin=filename,
  50. sampling_period=1. * pq.s)
  51. segment = Segment(file_origin=filename,
  52. index=stimIndex,
  53. **stim)
  54. segment.analogsignals = [sig]
  55. block.segments.append(segment)
  56. gr.analogsignals.append(sig)
  57. sig.group = gr
  58. block.create_many_to_one_relationship()
  59. return block
  60. class BrainwareDamIOTestCase(BaseTestIO, unittest.TestCase):
  61. '''
  62. Unit test testcase for neo.io.BrainwareDamIO
  63. '''
  64. ioclass = BrainwareDamIO
  65. read_and_write_is_bijective = False
  66. # These are the files it tries to read and test for compliance
  67. files_to_test = ['block_300ms_4rep_1clust_part_ch1.dam',
  68. 'interleaved_500ms_5rep_ch2.dam',
  69. 'long_170s_1rep_1clust_ch2.dam',
  70. 'multi_500ms_mulitrep_ch1.dam',
  71. 'random_500ms_12rep_noclust_part_ch2.dam',
  72. 'sequence_500ms_5rep_ch2.dam']
  73. # these are reference files to compare to
  74. files_to_compare = ['block_300ms_4rep_1clust_part_ch1',
  75. 'interleaved_500ms_5rep_ch2',
  76. '',
  77. 'multi_500ms_mulitrep_ch1',
  78. 'random_500ms_12rep_noclust_part_ch2',
  79. 'sequence_500ms_5rep_ch2']
  80. # add the suffix
  81. for i, fname in enumerate(files_to_compare):
  82. if fname:
  83. files_to_compare[i] += '_dam_py3.npz'
  84. # Will fetch from g-node if they don't already exist locally
  85. # How does it know to do this before any of the other tests?
  86. files_to_download = files_to_test + files_to_compare
  87. def test_reading_same(self):
  88. for ioobj, path in self.iter_io_objects(return_path=True):
  89. obj_reader_base = create_generic_reader(ioobj, target=False)
  90. obj_reader_single = create_generic_reader(ioobj)
  91. obj_base = obj_reader_base()
  92. obj_single = obj_reader_single()
  93. try:
  94. assert_same_sub_schema(obj_base, [obj_single])
  95. except BaseException as exc:
  96. exc.args += ('from ' + os.path.basename(path),)
  97. raise
  98. def test_against_reference(self):
  99. for filename, refname in zip(self.files_to_test,
  100. self.files_to_compare):
  101. if not refname:
  102. continue
  103. obj = self.read_file(filename=filename)
  104. refobj = proc_dam(self.get_filename_path(refname))
  105. try:
  106. assert_neo_object_is_compliant(obj)
  107. assert_neo_object_is_compliant(refobj)
  108. assert_same_sub_schema(obj, refobj)
  109. except BaseException as exc:
  110. exc.args += ('from ' + filename,)
  111. raise
  112. if __name__ == '__main__':
  113. unittest.main()