test_cubic.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for the CUBIC analysis.
  4. :copyright: Copyright 2016 by the Elephant team, see AUTHORS.txt.
  5. :license: Modified BSD, see LICENSE.txt for details.
  6. """
  7. import unittest
  8. import elephant.cubic as cubic
  9. import quantities as pq
  10. import neo
  11. import numpy
  12. class CubicTestCase(unittest.TestCase):
  13. '''
  14. This test is constructed to check the implementation of the CuBIC
  15. method [1].
  16. In the setup function is constructed an neo.AnalogSignal, that
  17. represents the Population Histogram of a population of neurons with order
  18. of correlation equal to ten. Since the population contu is either equal to
  19. 0 or 10 means that the embedded order of correlation is exactly 10.
  20. In test_cubic() the format of all the output and the order of correlation
  21. of the function jelephant.cubic.cubic() are tested.
  22. References
  23. ----------
  24. [1]Staude, Rotter, Gruen, (2009) J. Comp. Neurosci
  25. '''
  26. def setUp(self):
  27. n2 = 300
  28. n0 = 100000-n2
  29. self.xi = 10
  30. self.data_signal = neo.AnalogSignal(
  31. numpy.array([self.xi] * n2 + [0] * n0).reshape(n0 + n2, 1) *
  32. pq.dimensionless, sampling_period=1*pq.s)
  33. self.data_array = numpy.array([self.xi] * n2 + [0] * n0)
  34. self.alpha = 0.05
  35. self.ximax = 10
  36. def test_cubic(self):
  37. # Computing the output of CuBIC for the test data AnalogSignal
  38. xi, p_vals, k, test_aborted = cubic.cubic(
  39. self.data_signal, alpha=self.alpha)
  40. # Check the types of the outputs
  41. self.assertIsInstance(xi, int)
  42. self.assertIsInstance(p_vals, list)
  43. self.assertIsInstance(k, list)
  44. # Check that the number of tests is the output order of correlation
  45. self.assertEqual(xi, len(p_vals))
  46. # Check that all the first xi-1 tests have not passed the
  47. # significance level alpha
  48. for p in p_vals[:-1]:
  49. self.assertGreater(self.alpha, p)
  50. # Check that the last p-value has passed the significance level
  51. self.assertGreater(p_vals[-1], self.alpha)
  52. # Check that the number of cumulant of the output is 3
  53. self.assertEqual(3, len(k))
  54. # Check the analytical constrain of the cumulants for which K_1<K_2
  55. self.assertGreater(k[1], k[0])
  56. # Check the computed order of correlation is the expected
  57. # from the test data
  58. self.assertEqual(xi, self.xi)
  59. # Computing the output of CuBIC for the test data Array
  60. xi, p_vals, k, test_aborted = cubic.cubic(
  61. self.data_array, alpha=self.alpha)
  62. # Check the types of the outputs
  63. self.assertIsInstance(xi, int)
  64. self.assertIsInstance(p_vals, list)
  65. self.assertIsInstance(k, list)
  66. # Check that the number of tests is the output order of correlation
  67. self.assertEqual(xi, len(p_vals))
  68. # Check that all the first xi-1 tests have not passed the
  69. # significance level alpha
  70. for p in p_vals[:-1]:
  71. self.assertGreater(self.alpha, p)
  72. # Check that the last p-value has passed the significance level
  73. self.assertGreater(p_vals[-1], self.alpha)
  74. # Check that the number of cumulant of the output is 3
  75. self.assertEqual(3, len(k))
  76. # Check the analytical constrain of the cumulants for which K_1<K_2
  77. self.assertGreater(k[1], k[0])
  78. # Check the computed order of correlation is the expected
  79. # from the test data
  80. self.assertEqual(xi, self.xi)
  81. # Check the output for test_aborted
  82. self.assertEqual(test_aborted, False)
  83. def test_cubic_ximax(self):
  84. # Test exceeding ximax
  85. xi_ximax, p_vals_ximax, k_ximax, test_aborted = cubic.cubic(
  86. self.data_signal, alpha=1, ximax=self.ximax)
  87. self.assertEqual(test_aborted, True)
  88. self.assertEqual(xi_ximax - 1, self.ximax)
  89. def test_cubic_errors(self):
  90. # Check error ouputs for mis-settings of the parameters
  91. # Empty signal
  92. self.assertRaises(
  93. ValueError, cubic.cubic, neo.AnalogSignal(
  94. []*pq.dimensionless, sampling_period=10*pq.ms))
  95. # Multidimensional array
  96. self.assertRaises(ValueError, cubic.cubic, neo.AnalogSignal(
  97. [[1, 2, 3], [1, 2, 3]] * pq.dimensionless,
  98. sampling_period=10 * pq.ms))
  99. self.assertRaises(ValueError, cubic.cubic, numpy.array(
  100. [[1, 2, 3], [1, 2, 3]]))
  101. # Negative alpha
  102. self.assertRaises(ValueError, cubic.cubic, self.data_array, alpha=-0.1)
  103. # Negative number of iterations ximax
  104. self.assertRaises(ValueError, cubic.cubic, self.data_array, ximax=-100)
  105. # Checking case in which the second cumulant of the signal is smaller
  106. # than the first cumulant (analitycal constrain of the method)
  107. self.assertRaises(ValueError, cubic.cubic, neo.AnalogSignal(
  108. numpy.array([1]*1000).reshape(1000, 1), units=pq.dimensionless,
  109. sampling_period=10*pq.ms), alpha=self.alpha)
  110. def suite():
  111. suite = unittest.makeSuite(CubicTestCase, 'test')
  112. return suite
  113. if __name__ == "__main__":
  114. runner = unittest.TextTestRunner(verbosity=2)
  115. runner.run(suite())