common_rawio_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Common tests for RawIOs:
  4. It is copy/paste from neo/test/iotests/common_io_test.py
  5. The code should be shared for common parts.
  6. The public URL is in url_for_tests.
  7. To deposite new testing files, please create a account at
  8. gin.g-node.org and upload files at NeuralEnsemble/ephy_testing_data
  9. data repo.
  10. '''
  11. # needed for python 3 compatibility
  12. from __future__ import unicode_literals, print_function, division, absolute_import
  13. __test__ = False
  14. # url_for_tests = "https://portal.g-node.org/neo/" #This is the old place
  15. url_for_tests = "https://web.gin.g-node.org/NeuralEnsemble/ephy_testing_data/raw/master/"
  16. import os
  17. import logging
  18. import unittest
  19. from neo.rawio.tests.tools import (can_use_network, make_all_directories,
  20. download_test_file, create_local_temp_dir)
  21. from neo.rawio.tests import rawio_compliance as compliance
  22. class BaseTestRawIO(object):
  23. '''
  24. This class make common tests for all IOs.
  25. Basically download files from G-node portal.
  26. And test the IO is working.
  27. '''
  28. # ~ __test__ = False
  29. # all IO test need to modify this:
  30. rawioclass = None # the IOclass to be tested
  31. files_to_test = [] # list of files to test compliances
  32. files_to_download = [] # when files are at G-Node
  33. # allow environment to tell avoid using network
  34. use_network = can_use_network()
  35. local_test_dir = None
  36. def setUp(self):
  37. '''
  38. Set up the test fixture. This is run for every test
  39. '''
  40. self.shortname = self.rawioclass.__name__.lower().replace('rawio', '')
  41. self.create_local_dir_if_not_exists()
  42. self.download_test_files_if_not_present()
  43. def create_local_dir_if_not_exists(self):
  44. '''
  45. Create a local directory to store testing files and return it.
  46. The directory path is also written to self.local_test_dir
  47. '''
  48. self.local_test_dir = create_local_temp_dir(self.shortname)
  49. return self.local_test_dir
  50. def download_test_files_if_not_present(self):
  51. '''
  52. Download %s file at G-node for testing
  53. url_for_tests is global at beginning of this file.
  54. ''' % self.rawioclass.__name__
  55. if not self.use_network:
  56. raise unittest.SkipTest("Requires download of data from the web")
  57. url = url_for_tests + self.shortname
  58. try:
  59. make_all_directories(self.files_to_download, self.local_test_dir)
  60. download_test_file(self.files_to_download,
  61. self.local_test_dir, url)
  62. except IOError as exc:
  63. raise unittest.SkipTest(exc)
  64. download_test_files_if_not_present.__test__ = False
  65. def cleanup_file(self, path):
  66. '''
  67. Remove test files or directories safely.
  68. '''
  69. cleanup_test_file(self.rawioclass, path, directory=self.local_test_dir)
  70. def get_filename_path(self, filename):
  71. '''
  72. Get the path to a filename in the current temporary file directory
  73. '''
  74. return os.path.join(self.local_test_dir, filename)
  75. def test_read_all(self):
  76. # Read all file in self.entities_to_test
  77. for entity_name in self.entities_to_test:
  78. entity_name = self.get_filename_path(entity_name)
  79. if self.rawioclass.rawmode.endswith('-file'):
  80. reader = self.rawioclass(filename=entity_name)
  81. elif self.rawioclass.rawmode.endswith('-dir'):
  82. reader = self.rawioclass(dirname=entity_name)
  83. txt = reader.__repr__()
  84. assert 'nb_block' not in txt, 'Before parser_header() nb_block should be NOT known'
  85. reader.parse_header()
  86. txt = reader.__repr__()
  87. assert 'nb_block' in txt, 'After parser_header() nb_block should be known'
  88. # ~ print(txt)
  89. #
  90. txt = reader._repr_annotations()
  91. # ~ reader.print_annotations()
  92. # ~ sigs = reader.get_analogsignal_chunk(block_index=0, seg_index=0,
  93. # ~ i_start=None, i_stop=None, channel_indexes=[1])
  94. # ~ import matplotlib.pyplot as plt
  95. # ~ fig, ax = plt.subplots()
  96. # ~ ax.plot(sigs[:, 0])
  97. # ~ plt.show()
  98. # ~ nb_unit = reader.unit_channels_count()
  99. # ~ for unit_index in range(nb_unit):
  100. # ~ wfs = reader.spike_raw_waveforms(block_index=0, seg_index=0,
  101. # ~ unit_index=unit_index)
  102. # ~ if wfs is not None:
  103. # ~ import matplotlib.pyplot as plt
  104. # ~ fig, ax = plt.subplots()
  105. # ~ ax.plot(wfs[:, 0, :50].T)
  106. # ~ plt.show()
  107. # lanch a series of test compliance
  108. compliance.header_is_total(reader)
  109. compliance.count_element(reader)
  110. compliance.read_analogsignals(reader)
  111. compliance.read_spike_times(reader)
  112. compliance.read_spike_waveforms(reader)
  113. compliance.read_events(reader)
  114. compliance.has_annotations(reader)
  115. # basic benchmark
  116. level = logging.getLogger().getEffectiveLevel()
  117. logging.getLogger().setLevel(logging.INFO)
  118. compliance.benchmark_speed_read_signals(reader)
  119. logging.getLogger().setLevel(level)