test_kernels.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for the kernels module.
  4. :copyright: Copyright 2014-2016 by the Elephant team, see AUTHORS.txt.
  5. :license: Modified BSD, see LICENSE.txt for details.
  6. """
  7. import unittest
  8. import numpy as np
  9. import quantities as pq
  10. import scipy.integrate as spint
  11. import elephant.kernels as kernels
  12. class kernel_TestCase(unittest.TestCase):
  13. def setUp(self):
  14. self.kernel_types = [obj for obj in kernels.__dict__.values()
  15. if isinstance(obj, type) and
  16. issubclass(obj, kernels.Kernel) and
  17. hasattr(obj, "_evaluate") and
  18. obj is not kernels.Kernel and
  19. obj is not kernels.SymmetricKernel]
  20. self.fraction = 0.9999
  21. def test_error_kernels(self):
  22. """
  23. Test of various error cases in the kernels module.
  24. """
  25. self.assertRaises(
  26. TypeError, kernels.RectangularKernel, sigma=2.0)
  27. self.assertRaises(
  28. ValueError, kernels.RectangularKernel, sigma=-0.03*pq.s)
  29. self.assertRaises(
  30. ValueError, kernels.RectangularKernel, sigma=2.0*pq.ms,
  31. invert=2)
  32. rec_kernel = kernels.RectangularKernel(sigma=0.3*pq.ms)
  33. self.assertRaises(
  34. TypeError, rec_kernel, [1, 2, 3])
  35. self.assertRaises(
  36. TypeError, rec_kernel, [1, 2, 3]*pq.V)
  37. kernel = kernels.Kernel(sigma=0.3*pq.ms)
  38. self.assertRaises(
  39. NotImplementedError, kernel._evaluate, [1, 2, 3]*pq.V)
  40. self.assertRaises(
  41. NotImplementedError, kernel.boundary_enclosing_area_fraction,
  42. fraction=0.9)
  43. self.assertRaises(TypeError,
  44. rec_kernel.boundary_enclosing_area_fraction, [1, 2])
  45. self.assertRaises(ValueError,
  46. rec_kernel.boundary_enclosing_area_fraction, -10)
  47. self.assertEquals(kernel.is_symmetric(), False)
  48. self.assertEquals(rec_kernel.is_symmetric(), True)
  49. @unittest.skip('very time-consuming test')
  50. def test_error_alpha_kernel(self):
  51. alp_kernel = kernels.AlphaKernel(sigma=0.3*pq.ms)
  52. self.assertRaises(ValueError,
  53. alp_kernel.boundary_enclosing_area_fraction, 0.9999999)
  54. def test_kernels_normalization(self):
  55. """
  56. Test that each kernel normalizes to area one.
  57. """
  58. sigma = 0.1 * pq.mV
  59. kernel_resolution = sigma / 100.0
  60. kernel_list = [kernel_type(sigma, invert=False) for
  61. kernel_type in self.kernel_types]
  62. for kernel in kernel_list:
  63. b = kernel.boundary_enclosing_area_fraction(self.fraction).magnitude
  64. restric_defdomain = \
  65. np.linspace(-b, b, 2*b/kernel_resolution.magnitude) * sigma.units
  66. kern = kernel(restric_defdomain)
  67. norm = spint.cumtrapz(y=kern.magnitude,
  68. x=restric_defdomain.magnitude)[-1]
  69. self.assertAlmostEqual(norm, 1, delta=0.003)
  70. def test_kernels_stddev(self):
  71. """
  72. Test that the standard deviation calculated from the kernel (almost)
  73. equals the parameter sigma with which the kernel was constructed.
  74. """
  75. sigma = 0.5 * pq.s
  76. kernel_resolution = sigma / 50.0
  77. for invert in (False, True):
  78. kernel_list = [kernel_type(sigma, invert) for
  79. kernel_type in self.kernel_types]
  80. for kernel in kernel_list:
  81. b = kernel.boundary_enclosing_area_fraction(self.fraction).magnitude
  82. restric_defdomain = \
  83. np.linspace(-b, b, 2*b/kernel_resolution.magnitude) * \
  84. sigma.units
  85. kern = kernel(restric_defdomain)
  86. av_integr = kern * restric_defdomain
  87. average = spint.cumtrapz(y=av_integr.magnitude,
  88. x=restric_defdomain.magnitude)[-1] * \
  89. sigma.units
  90. var_integr = (restric_defdomain-average)**2 * kern
  91. variance = spint.cumtrapz(y=var_integr.magnitude,
  92. x=restric_defdomain.magnitude)[-1] * \
  93. sigma.units**2
  94. stddev = np.sqrt(variance)
  95. self.assertAlmostEqual(stddev, sigma, delta=0.01*sigma)
  96. def test_kernel_boundary_enclosing(self):
  97. """
  98. Test whether the integral of the kernel with boundary taken from
  99. the return value of the method boundary_enclosing_area_fraction
  100. is (almost) equal to the input variable `fraction` of
  101. boundary_enclosing_area_fraction.
  102. """
  103. sigma = 0.5 * pq.s
  104. kernel_resolution = sigma / 500.0
  105. kernel_list = [kernel_type(sigma, invert=False) for
  106. kernel_type in self.kernel_types]
  107. for fraction in np.arange(0.15, 1.0, 0.4):
  108. for kernel in kernel_list:
  109. b = kernel.boundary_enclosing_area_fraction(fraction).magnitude
  110. restric_defdomain = \
  111. np.linspace(-b, b, 2*b/kernel_resolution.magnitude) * \
  112. sigma.units
  113. kern = kernel(restric_defdomain)
  114. frac = spint.cumtrapz(y=kern.magnitude,
  115. x=restric_defdomain.magnitude)[-1]
  116. self.assertAlmostEqual(frac, fraction, delta=0.002)
  117. if __name__ == '__main__':
  118. unittest.main()