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_brainwaref32io.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests of neo.io.brainwaref32io
  4. """
  5. # needed for python 3 compatibility
  6. from __future__ import absolute_import, division, print_function
  7. import os.path
  8. import sys
  9. try:
  10. import unittest2 as unittest
  11. except ImportError:
  12. import unittest
  13. import numpy as np
  14. import quantities as pq
  15. from neo.core import Block, ChannelIndex, Segment, SpikeTrain, Unit
  16. from neo.io import BrainwareF32IO
  17. from neo.test.iotest.common_io_test import BaseTestIO
  18. from neo.test.tools import (assert_same_sub_schema,
  19. assert_neo_object_is_compliant)
  20. from neo.test.iotest.tools import create_generic_reader
  21. PY_VER = sys.version_info[0]
  22. def proc_f32(filename):
  23. '''Load an f32 file that has already been processed by the official matlab
  24. file converter. That matlab data is saved to an m-file, which is then
  25. converted to a numpy '.npz' file. This numpy file is the file actually
  26. loaded. This function converts it to a neo block and returns the block.
  27. This block can be compared to the block produced by BrainwareF32IO to
  28. make sure BrainwareF32IO is working properly
  29. block = proc_f32(filename)
  30. filename: The file name of the numpy file to load. It should end with
  31. '*_f32_py?.npz'. This will be converted to a neo 'file_origin' property
  32. with the value '*.f32', so the filename to compare should fit that pattern.
  33. 'py?' should be 'py2' for the python 2 version of the numpy file or 'py3'
  34. for the python 3 version of the numpy file.
  35. example: filename = 'file1_f32_py2.npz'
  36. f32 file name = 'file1.f32'
  37. '''
  38. filenameorig = os.path.basename(filename[:-12]+'.f32')
  39. # create the objects to store other objects
  40. block = Block(file_origin=filenameorig)
  41. chx = ChannelIndex(file_origin=filenameorig,
  42. index=np.array([], dtype=np.int),
  43. channel_names=np.array([], dtype='S'))
  44. unit = Unit(file_origin=filenameorig)
  45. # load objects into their containers
  46. block.channel_indexes.append(chx)
  47. chx.units.append(unit)
  48. try:
  49. with np.load(filename) as f32obj:
  50. f32file = f32obj.items()[0][1].flatten()
  51. except IOError as exc:
  52. if 'as a pickle' in exc.message:
  53. block.create_many_to_one_relationship()
  54. return block
  55. else:
  56. raise
  57. sweeplengths = [res[0, 0].tolist() for res in f32file['sweeplength']]
  58. stims = [res.flatten().tolist() for res in f32file['stim']]
  59. sweeps = [res['spikes'].flatten() for res in f32file['sweep'] if res.size]
  60. fullf32 = zip(sweeplengths, stims, sweeps)
  61. for sweeplength, stim, sweep in fullf32:
  62. for trainpts in sweep:
  63. if trainpts.size:
  64. trainpts = trainpts.flatten().astype('float32')
  65. else:
  66. trainpts = []
  67. paramnames = ['Param%s' % i for i in range(len(stim))]
  68. params = dict(zip(paramnames, stim))
  69. train = SpikeTrain(trainpts, units=pq.ms,
  70. t_start=0, t_stop=sweeplength,
  71. file_origin=filenameorig)
  72. segment = Segment(file_origin=filenameorig, **params)
  73. segment.spiketrains = [train]
  74. unit.spiketrains.append(train)
  75. block.segments.append(segment)
  76. block.create_many_to_one_relationship()
  77. return block
  78. class BrainwareF32IOTestCase(BaseTestIO, unittest.TestCase):
  79. '''
  80. Unit test testcase for neo.io.BrainwareF32IO
  81. '''
  82. ioclass = BrainwareF32IO
  83. read_and_write_is_bijective = False
  84. # These are the files it tries to read and test for compliance
  85. files_to_test = ['block_300ms_4rep_1clust_part_ch1.f32',
  86. 'block_500ms_5rep_empty_fullclust_ch1.f32',
  87. 'block_500ms_5rep_empty_partclust_ch1.f32',
  88. 'interleaved_500ms_5rep_ch2.f32',
  89. 'interleaved_500ms_5rep_nospikes_ch1.f32',
  90. 'multi_500ms_mulitrep_ch1.f32',
  91. 'random_500ms_12rep_noclust_part_ch2.f32',
  92. 'sequence_500ms_5rep_ch2.f32']
  93. # add the appropriate suffix depending on the python version
  94. suffix = '_f32_py%s.npz' % PY_VER
  95. files_to_download = files_to_test[:]
  96. # add the reference files to the list of files to download
  97. files_to_compare = []
  98. for fname in files_to_test:
  99. if fname:
  100. files_to_compare.append(os.path.splitext(fname)[0] + suffix)
  101. # Will fetch from g-node if they don't already exist locally
  102. # How does it know to do this before any of the other tests?
  103. files_to_download = files_to_test + files_to_compare
  104. def test_reading_same(self):
  105. for ioobj, path in self.iter_io_objects(return_path=True):
  106. obj_reader_base = create_generic_reader(ioobj, target=False)
  107. obj_reader_single = create_generic_reader(ioobj)
  108. obj_base = obj_reader_base()
  109. obj_single = obj_reader_single()
  110. try:
  111. assert_same_sub_schema(obj_base, obj_single)
  112. except BaseException as exc:
  113. exc.args += ('from ' + os.path.basename(path),)
  114. raise
  115. def test_against_reference(self):
  116. for obj, path in self.iter_objects(return_path=True):
  117. filename = os.path.basename(path)
  118. refpath = os.path.splitext(path)[0] + self.suffix
  119. refobj = proc_f32(refpath)
  120. try:
  121. assert_neo_object_is_compliant(obj)
  122. assert_neo_object_is_compliant(refobj)
  123. assert_same_sub_schema(obj, refobj)
  124. except BaseException as exc:
  125. exc.args += ('from ' + filename,)
  126. raise
  127. if __name__ == '__main__':
  128. unittest.main()