test_spike_train_correlation.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for the spike_train_correlation module.
  4. :copyright: Copyright 2015-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. from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
  10. import quantities as pq
  11. import neo
  12. import elephant.conversion as conv
  13. import elephant.spike_train_correlation as sc
  14. class covariance_TestCase(unittest.TestCase):
  15. def setUp(self):
  16. # These two arrays must be such that they do not have coincidences
  17. # spanning across two neighbor bins assuming ms bins [0,1),[1,2),...
  18. self.test_array_1d_0 = [
  19. 1.3, 7.56, 15.87, 28.23, 30.9, 34.2, 38.2, 43.2]
  20. self.test_array_1d_1 = [
  21. 1.02, 2.71, 18.82, 28.46, 28.79, 43.6]
  22. # Build spike trains
  23. self.st_0 = neo.SpikeTrain(
  24. self.test_array_1d_0, units='ms', t_stop=50.)
  25. self.st_1 = neo.SpikeTrain(
  26. self.test_array_1d_1, units='ms', t_stop=50.)
  27. # And binned counterparts
  28. self.binned_st = conv.BinnedSpikeTrain(
  29. [self.st_0, self.st_1], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  30. binsize=1 * pq.ms)
  31. def test_covariance_binned(self):
  32. '''
  33. Test covariance between two binned spike trains.
  34. '''
  35. # Calculate clipped and unclipped
  36. res_clipped = sc.covariance(
  37. self.binned_st, binary=True)
  38. res_unclipped = sc.covariance(
  39. self.binned_st, binary=False)
  40. # Check dimensions
  41. self.assertEqual(len(res_clipped), 2)
  42. self.assertEqual(len(res_unclipped), 2)
  43. # Check result unclipped against result calculated from scratch for
  44. # the off-diagonal element
  45. mat = self.binned_st.to_array()
  46. mean_0 = np.mean(mat[0])
  47. mean_1 = np.mean(mat[1])
  48. target_from_scratch = \
  49. np.dot(mat[0] - mean_0, mat[1] - mean_1) / (len(mat[0]) - 1)
  50. # Check result unclipped against result calculated by numpy.corrcoef
  51. target_numpy = np.cov(mat)
  52. self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
  53. self.assertAlmostEqual(res_unclipped[0][1], target_from_scratch)
  54. self.assertAlmostEqual(res_unclipped[1][0], target_from_scratch)
  55. # Check result clipped against result calculated from scratch for
  56. # the off-diagonal elemant
  57. mat = self.binned_st.to_bool_array()
  58. mean_0 = np.mean(mat[0])
  59. mean_1 = np.mean(mat[1])
  60. target_from_scratch = \
  61. np.dot(mat[0] - mean_0, mat[1] - mean_1) / (len(mat[0]) - 1)
  62. # Check result unclipped against result calculated by numpy.corrcoef
  63. target_numpy = np.cov(mat)
  64. self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
  65. self.assertAlmostEqual(res_clipped[0][1], target_from_scratch)
  66. self.assertAlmostEqual(res_clipped[1][0], target_from_scratch)
  67. def test_covariance_binned_same_spiketrains(self):
  68. '''
  69. Test if the covariation between two identical binned spike
  70. trains evaluates to the expected 2x2 matrix.
  71. '''
  72. # Calculate correlation
  73. binned_st = conv.BinnedSpikeTrain(
  74. [self.st_0, self.st_0], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  75. binsize=1 * pq.ms)
  76. target = sc.covariance(binned_st)
  77. # Check dimensions
  78. self.assertEqual(len(target), 2)
  79. # Check result
  80. assert_array_equal(target[0][0], target[1][1])
  81. def test_covariance_binned_short_input(self):
  82. '''
  83. Test if input list of only one binned spike train yields correct result
  84. that matches numpy.cov (covariance with itself)
  85. '''
  86. # Calculate correlation
  87. binned_st = conv.BinnedSpikeTrain(
  88. self.st_0, t_start=0 * pq.ms, t_stop=50. * pq.ms,
  89. binsize=1 * pq.ms)
  90. target = sc.covariance(binned_st)
  91. # Check result unclipped against result calculated by numpy.corrcoef
  92. mat = binned_st.to_bool_array()
  93. target_numpy = np.cov(mat)
  94. # Check result and dimensionality of result
  95. self.assertEqual(target.ndim, target_numpy.ndim)
  96. self.assertAlmostEqual(target, target_numpy)
  97. class corrcoeff_TestCase(unittest.TestCase):
  98. def setUp(self):
  99. # These two arrays must be such that they do not have coincidences
  100. # spanning across two neighbor bins assuming ms bins [0,1),[1,2),...
  101. self.test_array_1d_0 = [
  102. 1.3, 7.56, 15.87, 28.23, 30.9, 34.2, 38.2, 43.2]
  103. self.test_array_1d_1 = [
  104. 1.02, 2.71, 18.82, 28.46, 28.79, 43.6]
  105. # Build spike trains
  106. self.st_0 = neo.SpikeTrain(
  107. self.test_array_1d_0, units='ms', t_stop=50.)
  108. self.st_1 = neo.SpikeTrain(
  109. self.test_array_1d_1, units='ms', t_stop=50.)
  110. # And binned counterparts
  111. self.binned_st = conv.BinnedSpikeTrain(
  112. [self.st_0, self.st_1], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  113. binsize=1 * pq.ms)
  114. def test_corrcoef_binned(self):
  115. '''
  116. Test the correlation coefficient between two binned spike trains.
  117. '''
  118. # Calculate clipped and unclipped
  119. res_clipped = sc.corrcoef(
  120. self.binned_st, binary=True)
  121. res_unclipped = sc.corrcoef(
  122. self.binned_st, binary=False)
  123. # Check dimensions
  124. self.assertEqual(len(res_clipped), 2)
  125. self.assertEqual(len(res_unclipped), 2)
  126. # Check result unclipped against result calculated from scratch for
  127. # the off-diagonal element
  128. mat = self.binned_st.to_array()
  129. mean_0 = np.mean(mat[0])
  130. mean_1 = np.mean(mat[1])
  131. target_from_scratch = \
  132. np.dot(mat[0] - mean_0, mat[1] - mean_1) / \
  133. np.sqrt(
  134. np.dot(mat[0] - mean_0, mat[0] - mean_0) *
  135. np.dot(mat[1] - mean_1, mat[1] - mean_1))
  136. # Check result unclipped against result calculated by numpy.corrcoef
  137. target_numpy = np.corrcoef(mat)
  138. self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
  139. self.assertAlmostEqual(res_unclipped[0][1], target_from_scratch)
  140. self.assertAlmostEqual(res_unclipped[1][0], target_from_scratch)
  141. # Check result clipped against result calculated from scratch for
  142. # the off-diagonal elemant
  143. mat = self.binned_st.to_bool_array()
  144. mean_0 = np.mean(mat[0])
  145. mean_1 = np.mean(mat[1])
  146. target_from_scratch = \
  147. np.dot(mat[0] - mean_0, mat[1] - mean_1) / \
  148. np.sqrt(
  149. np.dot(mat[0] - mean_0, mat[0] - mean_0) *
  150. np.dot(mat[1] - mean_1, mat[1] - mean_1))
  151. # Check result unclipped against result calculated by numpy.corrcoef
  152. target_numpy = np.corrcoef(mat)
  153. self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
  154. self.assertAlmostEqual(res_clipped[0][1], target_from_scratch)
  155. self.assertAlmostEqual(res_clipped[1][0], target_from_scratch)
  156. def test_corrcoef_binned_same_spiketrains(self):
  157. '''
  158. Test if the correlation coefficient between two identical binned spike
  159. trains evaluates to a 2x2 matrix of ones.
  160. '''
  161. # Calculate correlation
  162. binned_st = conv.BinnedSpikeTrain(
  163. [self.st_0, self.st_0], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  164. binsize=1 * pq.ms)
  165. target = sc.corrcoef(binned_st)
  166. # Check dimensions
  167. self.assertEqual(len(target), 2)
  168. # Check result
  169. assert_array_equal(target, 1.)
  170. def test_corrcoef_binned_short_input(self):
  171. '''
  172. Test if input list of one binned spike train yields 1.0.
  173. '''
  174. # Calculate correlation
  175. binned_st = conv.BinnedSpikeTrain(
  176. self.st_0, t_start=0 * pq.ms, t_stop=50. * pq.ms,
  177. binsize=1 * pq.ms)
  178. target = sc.corrcoef(binned_st)
  179. # Check result and dimensionality of result
  180. self.assertEqual(target.ndim, 0)
  181. self.assertEqual(target, 1.)
  182. class cross_correlation_histogram_TestCase(unittest.TestCase):
  183. def setUp(self):
  184. # These two arrays must be such that they do not have coincidences
  185. # spanning across two neighbor bins assuming ms bins [0,1),[1,2),...
  186. self.test_array_1d_1 = [
  187. 1.3, 7.56, 15.87, 28.23, 30.9, 34.2, 38.2, 43.2]
  188. self.test_array_1d_2 = [
  189. 1.02, 2.71, 18.82, 28.46, 28.79, 43.6]
  190. # Build spike trains
  191. self.st_1 = neo.SpikeTrain(
  192. self.test_array_1d_1, units='ms', t_stop=50.)
  193. self.st_2 = neo.SpikeTrain(
  194. self.test_array_1d_2, units='ms', t_stop=50.)
  195. # And binned counterparts
  196. self.binned_st1 = conv.BinnedSpikeTrain(
  197. [self.st_1], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  198. binsize=1 * pq.ms)
  199. self.binned_st2 = conv.BinnedSpikeTrain(
  200. [self.st_2], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  201. binsize=1 * pq.ms)
  202. # Binned sts to check errors raising
  203. self.st_check_binsize = conv.BinnedSpikeTrain(
  204. [self.st_1], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  205. binsize=5 * pq.ms)
  206. self.st_check_t_start = conv.BinnedSpikeTrain(
  207. [self.st_1], t_start=1 * pq.ms, t_stop=50. * pq.ms,
  208. binsize=1 * pq.ms)
  209. self.st_check_t_stop = conv.BinnedSpikeTrain(
  210. [self.st_1], t_start=0 * pq.ms, t_stop=40. * pq.ms,
  211. binsize=1 * pq.ms)
  212. self.st_check_dimension = conv.BinnedSpikeTrain(
  213. [self.st_1, self.st_2], t_start=0 * pq.ms, t_stop=50. * pq.ms,
  214. binsize=1 * pq.ms)
  215. def test_cross_correlation_histogram(self):
  216. '''
  217. Test generic result of a cross-correlation histogram between two binned
  218. spike trains.
  219. '''
  220. # Calculate CCH using Elephant (normal and binary version) with
  221. # mode equal to 'full' (whole spike trains are correlated)
  222. cch_clipped, bin_ids_clipped = sc.cross_correlation_histogram(
  223. self.binned_st1, self.binned_st2, window='full',
  224. binary=True)
  225. cch_unclipped, bin_ids_unclipped = sc.cross_correlation_histogram(
  226. self.binned_st1, self.binned_st2, window='full', binary=False)
  227. cch_clipped_mem, bin_ids_clipped_mem = sc.cross_correlation_histogram(
  228. self.binned_st1, self.binned_st2, window='full',
  229. binary=True, method='memory')
  230. cch_unclipped_mem, bin_ids_unclipped_mem = sc.cross_correlation_histogram(
  231. self.binned_st1, self.binned_st2, window='full',
  232. binary=False, method='memory')
  233. # Check consistency two methods
  234. assert_array_equal(
  235. np.squeeze(cch_clipped.magnitude), np.squeeze(
  236. cch_clipped_mem.magnitude))
  237. assert_array_equal(
  238. np.squeeze(cch_clipped.times), np.squeeze(
  239. cch_clipped_mem.times))
  240. assert_array_equal(
  241. np.squeeze(cch_unclipped.magnitude), np.squeeze(
  242. cch_unclipped_mem.magnitude))
  243. assert_array_equal(
  244. np.squeeze(cch_unclipped.times), np.squeeze(
  245. cch_unclipped_mem.times))
  246. assert_array_almost_equal(bin_ids_clipped, bin_ids_clipped_mem)
  247. assert_array_almost_equal(bin_ids_unclipped, bin_ids_unclipped_mem)
  248. # Check normal correlation Note: Use numpy correlate to verify result.
  249. # Note: numpy conventions for input array 1 and input array 2 are
  250. # swapped compared to Elephant!
  251. mat1 = self.binned_st1.to_array()[0]
  252. mat2 = self.binned_st2.to_array()[0]
  253. target_numpy = np.correlate(mat2, mat1, mode='full')
  254. assert_array_equal(
  255. target_numpy, np.squeeze(cch_unclipped.magnitude))
  256. # Check correlation using binary spike trains
  257. mat1 = np.array(self.binned_st1.to_bool_array()[0], dtype=int)
  258. mat2 = np.array(self.binned_st2.to_bool_array()[0], dtype=int)
  259. target_numpy = np.correlate(mat2, mat1, mode='full')
  260. assert_array_equal(
  261. target_numpy, np.squeeze(cch_clipped.magnitude))
  262. # Check the time axis and bin IDs of the resulting AnalogSignal
  263. assert_array_almost_equal(
  264. (bin_ids_clipped - 0.5) * self.binned_st1.binsize,
  265. cch_unclipped.times)
  266. assert_array_almost_equal(
  267. (bin_ids_clipped - 0.5) * self.binned_st1.binsize,
  268. cch_clipped.times)
  269. # Calculate CCH using Elephant (normal and binary version) with
  270. # mode equal to 'valid' (only completely overlapping intervals of the
  271. # spike trains are correlated)
  272. cch_clipped, bin_ids_clipped = sc.cross_correlation_histogram(
  273. self.binned_st1, self.binned_st2, window='valid',
  274. binary=True)
  275. cch_unclipped, bin_ids_unclipped = sc.cross_correlation_histogram(
  276. self.binned_st1, self.binned_st2, window='valid',
  277. binary=False)
  278. cch_clipped_mem, bin_ids_clipped_mem = sc.cross_correlation_histogram(
  279. self.binned_st1, self.binned_st2, window='valid',
  280. binary=True, method='memory')
  281. cch_unclipped_mem, bin_ids_unclipped_mem = sc.cross_correlation_histogram(
  282. self.binned_st1, self.binned_st2, window='valid',
  283. binary=False, method='memory')
  284. # Check consistency two methods
  285. assert_array_equal(
  286. np.squeeze(cch_clipped.magnitude), np.squeeze(
  287. cch_clipped_mem.magnitude))
  288. assert_array_equal(
  289. np.squeeze(cch_clipped.times), np.squeeze(
  290. cch_clipped_mem.times))
  291. assert_array_equal(
  292. np.squeeze(cch_unclipped.magnitude), np.squeeze(
  293. cch_unclipped_mem.magnitude))
  294. assert_array_equal(
  295. np.squeeze(cch_unclipped.times), np.squeeze(
  296. cch_unclipped_mem.times))
  297. assert_array_equal(bin_ids_clipped, bin_ids_clipped_mem)
  298. assert_array_equal(bin_ids_unclipped, bin_ids_unclipped_mem)
  299. # Check normal correlation Note: Use numpy correlate to verify result.
  300. # Note: numpy conventions for input array 1 and input array 2 are
  301. # swapped compared to Elephant!
  302. mat1 = self.binned_st1.to_array()[0]
  303. mat2 = self.binned_st2.to_array()[0]
  304. target_numpy = np.correlate(mat2, mat1, mode='valid')
  305. assert_array_equal(
  306. target_numpy, np.squeeze(cch_unclipped.magnitude))
  307. # Check correlation using binary spike trains
  308. mat1 = np.array(self.binned_st1.to_bool_array()[0], dtype=int)
  309. mat2 = np.array(self.binned_st2.to_bool_array()[0], dtype=int)
  310. target_numpy = np.correlate(mat2, mat1, mode='valid')
  311. assert_array_equal(
  312. target_numpy, np.squeeze(cch_clipped.magnitude))
  313. # Check the time axis and bin IDs of the resulting AnalogSignal
  314. assert_array_equal(
  315. (bin_ids_clipped - 0.5) * self.binned_st1.binsize,
  316. cch_unclipped.times)
  317. assert_array_equal(
  318. (bin_ids_clipped - 0.5) * self.binned_st1.binsize,
  319. cch_clipped.times)
  320. # Check for wrong window parameter setting
  321. self.assertRaises(
  322. KeyError, sc.cross_correlation_histogram, self.binned_st1,
  323. self.binned_st2, window='dsaij')
  324. self.assertRaises(
  325. KeyError, sc.cross_correlation_histogram, self.binned_st1,
  326. self.binned_st2, window='dsaij', method='memory')
  327. def test_raising_error_wrong_inputs(self):
  328. '''Check that an exception is thrown if the two spike trains are not
  329. fullfilling the requirement of the function'''
  330. # Check the binsizes are the same
  331. self.assertRaises(
  332. AssertionError,
  333. sc.cross_correlation_histogram, self.binned_st1,
  334. self.st_check_binsize)
  335. # Check different t_start and t_stop
  336. self.assertRaises(
  337. AssertionError, sc.cross_correlation_histogram,
  338. self.st_check_t_start, self.binned_st2)
  339. self.assertRaises(
  340. AssertionError, sc.cross_correlation_histogram,
  341. self.st_check_t_stop, self.binned_st2)
  342. # Check input are one dimensional
  343. self.assertRaises(
  344. AssertionError, sc.cross_correlation_histogram,
  345. self.st_check_dimension, self.binned_st2)
  346. self.assertRaises(
  347. AssertionError, sc.cross_correlation_histogram,
  348. self.binned_st2, self.st_check_dimension)
  349. def test_window(self):
  350. '''Test if the window parameter is correctly interpreted.'''
  351. cch_win, bin_ids = sc.cch(
  352. self.binned_st1, self.binned_st2, window=[-30, 30])
  353. cch_win_mem, bin_ids_mem = sc.cch(
  354. self.binned_st1, self.binned_st2, window=[-30, 30])
  355. assert_array_equal(bin_ids, np.arange(-30, 31, 1))
  356. assert_array_equal(
  357. (bin_ids - 0.5) * self.binned_st1.binsize, cch_win.times)
  358. assert_array_equal(bin_ids_mem, np.arange(-30, 31, 1))
  359. assert_array_equal(
  360. (bin_ids_mem - 0.5) * self.binned_st1.binsize, cch_win.times)
  361. assert_array_equal(cch_win, cch_win_mem)
  362. cch_unclipped, _ = sc.cross_correlation_histogram(
  363. self.binned_st1, self.binned_st2, window='full', binary=False)
  364. assert_array_equal(cch_win, cch_unclipped[19:80])
  365. cch_win, bin_ids = sc.cch(
  366. self.binned_st1, self.binned_st2, window=[-25*pq.ms, 25*pq.ms])
  367. cch_win_mem, bin_ids_mem = sc.cch(
  368. self.binned_st1, self.binned_st2, window=[-25*pq.ms, 25*pq.ms],
  369. method='memory')
  370. assert_array_equal(bin_ids, np.arange(-25, 26, 1))
  371. assert_array_equal(
  372. (bin_ids - 0.5) * self.binned_st1.binsize, cch_win.times)
  373. assert_array_equal(bin_ids_mem, np.arange(-25, 26, 1))
  374. assert_array_equal(
  375. (bin_ids_mem - 0.5) * self.binned_st1.binsize, cch_win.times)
  376. assert_array_equal(cch_win, cch_win_mem)
  377. _, bin_ids = sc.cch(
  378. self.binned_st1, self.binned_st2, window=[20, 30])
  379. _, bin_ids_mem = sc.cch(
  380. self.binned_st1, self.binned_st2, window=[20, 30], method='memory')
  381. assert_array_equal(bin_ids, np.arange(20, 31, 1))
  382. assert_array_equal(bin_ids_mem, np.arange(20, 31, 1))
  383. _, bin_ids = sc.cch(
  384. self.binned_st1, self.binned_st2, window=[-30, -20])
  385. _, bin_ids_mem = sc.cch(
  386. self.binned_st1, self.binned_st2, window=[-30, -20],
  387. method='memory')
  388. assert_array_equal(bin_ids, np.arange(-30, -19, 1))
  389. assert_array_equal(bin_ids_mem, np.arange(-30, -19, 1))
  390. # Cehck for wrong assignments to the window parameter
  391. self.assertRaises(
  392. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  393. self.binned_st2, window=[-60, 50])
  394. self.assertRaises(
  395. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  396. self.binned_st2, window=[-60, 50], method='memory')
  397. self.assertRaises(
  398. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  399. self.binned_st2, window=[-50, 60])
  400. self.assertRaises(
  401. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  402. self.binned_st2, window=[-50, 60], method='memory')
  403. self.assertRaises(
  404. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  405. self.binned_st2, window=[-25.5*pq.ms, 25*pq.ms])
  406. self.assertRaises(
  407. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  408. self.binned_st2, window=[-25.5*pq.ms, 25*pq.ms], method='memory')
  409. self.assertRaises(
  410. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  411. self.binned_st2, window=[-25*pq.ms, 25.5*pq.ms])
  412. self.assertRaises(
  413. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  414. self.binned_st2, window=[-25*pq.ms, 25.5*pq.ms], method='memory')
  415. self.assertRaises(
  416. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  417. self.binned_st2, window=[-60*pq.ms, 50*pq.ms])
  418. self.assertRaises(
  419. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  420. self.binned_st2, window=[-60*pq.ms, 50*pq.ms], method='memory')
  421. self.assertRaises(
  422. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  423. self.binned_st2, window=[-50*pq.ms, 60*pq.ms])
  424. self.assertRaises(
  425. ValueError, sc.cross_correlation_histogram, self.binned_st1,
  426. self.binned_st2, window=[-50*pq.ms, 60*pq.ms], method='memory')
  427. def test_border_correction(self):
  428. '''Test if the border correction for bins at the edges is correctly
  429. performed'''
  430. cch_corrected, _ = sc.cross_correlation_histogram(
  431. self.binned_st1, self.binned_st2, window='full',
  432. border_correction=True, binary=False, kernel=None)
  433. cch_corrected_mem, _ = sc.cross_correlation_histogram(
  434. self.binned_st1, self.binned_st2, window='full',
  435. border_correction=True, binary=False, kernel=None, method='memory')
  436. cch, _ = sc.cross_correlation_histogram(
  437. self.binned_st1, self.binned_st2, window='full',
  438. border_correction=False, binary=False, kernel=None)
  439. cch_mem, _ = sc.cross_correlation_histogram(
  440. self.binned_st1, self.binned_st2, window='full',
  441. border_correction=False, binary=False, kernel=None,
  442. method='memory')
  443. self.assertNotEqual(cch.all(), cch_corrected.all())
  444. self.assertNotEqual(cch_mem.all(), cch_corrected_mem.all())
  445. def test_kernel(self):
  446. '''Test if the smoothing kernel is correctly defined, and wheter it is
  447. applied properly.'''
  448. smoothed_cch, _ = sc.cross_correlation_histogram(
  449. self.binned_st1, self.binned_st2, kernel=np.ones(3))
  450. smoothed_cch_mem, _ = sc.cross_correlation_histogram(
  451. self.binned_st1, self.binned_st2, kernel=np.ones(3),
  452. method='memory')
  453. cch, _ = sc.cross_correlation_histogram(
  454. self.binned_st1, self.binned_st2, kernel=None)
  455. cch_mem, _ = sc.cross_correlation_histogram(
  456. self.binned_st1, self.binned_st2, kernel=None, method='memory')
  457. self.assertNotEqual(smoothed_cch.all, cch.all)
  458. self.assertNotEqual(smoothed_cch_mem.all, cch_mem.all)
  459. self.assertRaises(
  460. ValueError, sc.cch, self.binned_st1, self.binned_st2,
  461. kernel=np.ones(100))
  462. self.assertRaises(
  463. ValueError, sc.cch, self.binned_st1, self.binned_st2,
  464. kernel=np.ones(100), method='memory')
  465. self.assertRaises(
  466. ValueError, sc.cch, self.binned_st1, self.binned_st2, kernel='BOX')
  467. self.assertRaises(
  468. ValueError, sc.cch, self.binned_st1, self.binned_st2, kernel='BOX',
  469. method='memory')
  470. def test_exist_alias(self):
  471. '''
  472. Test if alias cch still exists.
  473. '''
  474. self.assertEqual(sc.cross_correlation_histogram, sc.cch)
  475. if __name__ == '__main__':
  476. unittest.main()