Exemple #1
0
 def test_victor_purpura_algorithm_comparison(self):
     assert_array_almost_equal(
         stds.victor_purpura_distance([self.st21, self.st22, self.st23],
                                      self.q3),
         stds.victor_purpura_distance([self.st21, self.st22, self.st23],
                                      self.q3,
                                      algorithm='intuitive'))
Exemple #2
0
    def test_victor_purpura_matlab_comparison_int(self):

        repo_path =\
            r"unittest/spike_train_dissimilarity/victor_purpura_distance/data"

        files_to_download = [
            ("times_int.npy", "aa1411c04da3f58d8b8913ae2f935057"),
            ("matlab_results_int.npy", "7edd32e50edde12dc1ef4aa5f57f70fb")
        ]

        for filename, checksum in files_to_download:
            download_datasets(repo_path=f"{repo_path}/{filename}",
                              checksum=checksum)

        times_int = np.load(ELEPHANT_TMP_DIR / 'times_int.npy')
        mat_res_int = np.load(ELEPHANT_TMP_DIR / 'matlab_results_int.npy')

        r_int = SpikeTrain(times_int[0],
                           units='ms',
                           t_start=0,
                           t_stop=1000 * ms)
        s_int = SpikeTrain(times_int[1],
                           units='ms',
                           t_start=0,
                           t_stop=1000 * ms)
        t_int = SpikeTrain(times_int[2],
                           units='ms',
                           t_start=0,
                           t_stop=1000 * ms)

        vic_pur_result_int = stds.victor_purpura_distance(
            [r_int, s_int, t_int],
            cost_factor=1.0 / ms,
            kernel=None,
            sort=True,
            algorithm='intuitive')

        assert_array_equal(vic_pur_result_int, mat_res_int)
Exemple #3
0
    def test_victor_purpura_matlab_comparison_float(self):

        repo_path =\
            r"unittest/spike_train_dissimilarity/victor_purpura_distance/data"

        files_to_download = [
            ("times_float.npy", "ed1ff4d2c0eeed4a2b50a456803656be"),
            ("matlab_results_float.npy", "a17f049e7ad0ddf7ca812e86fdb92646")
        ]

        for filename, checksum in files_to_download:
            download_datasets(repo_path=f"{repo_path}/{filename}",
                              checksum=checksum)

        times_float = np.load(ELEPHANT_TMP_DIR / 'times_float.npy')
        mat_res_float = np.load(ELEPHANT_TMP_DIR / 'matlab_results_float.npy')

        r_float = SpikeTrain(times_float[0],
                             units='ms',
                             t_start=0,
                             t_stop=1000 * ms)
        s_float = SpikeTrain(times_float[1],
                             units='ms',
                             t_start=0,
                             t_stop=1000 * ms)
        t_float = SpikeTrain(times_float[2],
                             units='ms',
                             t_start=0,
                             t_stop=1000 * ms)

        vic_pur_result_float = stds.victor_purpura_distance(
            [r_float, s_float, t_float],
            cost_factor=1.0 / ms,
            kernel=None,
            sort=True,
            algorithm='intuitive')

        assert_array_almost_equal(vic_pur_result_float, mat_res_float)
Exemple #4
0
    def test_wrong_input(self):
        self.assertRaises(TypeError, stds.victor_purpura_distance,
                          [self.array1, self.array2], self.q3)
        self.assertRaises(TypeError, stds.victor_purpura_distance,
                          [self.qarray1, self.qarray2], self.q3)
        self.assertRaises(TypeError, stds.victor_purpura_distance,
                          [self.qarray1, self.qarray2], 5.0 * ms)

        self.assertRaises(TypeError,
                          stds.victor_purpura_distance,
                          [self.array1, self.array2],
                          self.q3,
                          algorithm='intuitive')
        self.assertRaises(TypeError,
                          stds.victor_purpura_distance,
                          [self.qarray1, self.qarray2],
                          self.q3,
                          algorithm='intuitive')
        self.assertRaises(TypeError,
                          stds.victor_purpura_distance,
                          [self.qarray1, self.qarray2],
                          5.0 * ms,
                          algorithm='intuitive')

        self.assertRaises(TypeError, stds.van_rossum_distance,
                          [self.array1, self.array2], self.tau3)
        self.assertRaises(TypeError, stds.van_rossum_distance,
                          [self.qarray1, self.qarray2], self.tau3)
        self.assertRaises(TypeError, stds.van_rossum_distance,
                          [self.qarray1, self.qarray2], 5.0 * Hz)

        self.assertRaises(TypeError, stds.victor_purpura_distance,
                          [self.st11, self.st13], self.tau2)
        self.assertRaises(TypeError, stds.victor_purpura_distance,
                          [self.st11, self.st13], 5.0)
        self.assertRaises(TypeError,
                          stds.victor_purpura_distance, [self.st11, self.st13],
                          self.tau2,
                          algorithm='intuitive')
        self.assertRaises(TypeError,
                          stds.victor_purpura_distance, [self.st11, self.st13],
                          5.0,
                          algorithm='intuitive')
        self.assertRaises(TypeError, stds.van_rossum_distance,
                          [self.st11, self.st13], self.q4)
        self.assertRaises(TypeError, stds.van_rossum_distance,
                          [self.st11, self.st13], 5.0)

        self.assertRaises(NotImplementedError,
                          stds.victor_purpura_distance, [self.st01, self.st02],
                          self.q3,
                          kernel=kernels.Kernel(2.0 / self.q3))
        self.assertRaises(NotImplementedError,
                          stds.victor_purpura_distance, [self.st01, self.st02],
                          self.q3,
                          kernel=kernels.SymmetricKernel(2.0 / self.q3))
        self.assertEqual(
            stds.victor_purpura_distance(
                [self.st01, self.st02],
                self.q1,
                kernel=kernels.TriangularKernel(
                    2.0 / (np.sqrt(6.0) * self.q2)))[0, 1],
            stds.victor_purpura_distance(
                [self.st01, self.st02],
                self.q3,
                kernel=kernels.TriangularKernel(
                    2.0 / (np.sqrt(6.0) * self.q2)))[0, 1])
        self.assertEqual(
            stds.victor_purpura_distance(
                [self.st01, self.st02],
                kernel=kernels.TriangularKernel(
                    2.0 / (np.sqrt(6.0) * self.q2)))[0, 1], 1.0)
        self.assertNotEqual(
            stds.victor_purpura_distance(
                [self.st01, self.st02],
                kernel=kernels.AlphaKernel(2.0 / (np.sqrt(6.0) * self.q2)))[0,
                                                                            1],
            1.0)

        self.assertRaises(NameError,
                          stds.victor_purpura_distance, [self.st11, self.st13],
                          self.q2,
                          algorithm='slow')
Exemple #5
0
 def test_victor_purpura_distance_intuitive(self):
     # Tests of distances of simplest spike trains
     self.assertEqual(
         stds.victor_purpura_distance([self.st00, self.st00],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 0.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st00, self.st01],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 1.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st00],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 1.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st01],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 0.0)
     # Tests of distances under elementary spike operations
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st02],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 1.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st03],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 1.9)
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st04],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 2.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st05],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 2.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st00, self.st07],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 2.0)
     self.assertAlmostEqual(
         stds.victor_purpura_distance([self.st07, self.st08],
                                      self.q4,
                                      algorithm='intuitive')[0, 1], 0.4)
     self.assertAlmostEqual(
         stds.victor_purpura_distance([self.st07, self.st10],
                                      self.q3,
                                      algorithm='intuitive')[0, 1], 2.6)
     self.assertEqual(
         stds.victor_purpura_distance([self.st11, self.st14],
                                      self.q2,
                                      algorithm='intuitive')[0, 1], 1)
     # Tests on timescales
     self.assertEqual(
         stds.victor_purpura_distance([self.st11, self.st14],
                                      self.q1,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st11, self.st14],
                                      self.q5,
                                      algorithm='intuitive')[0, 1])
     self.assertEqual(
         stds.victor_purpura_distance([self.st07, self.st11],
                                      self.q0,
                                      algorithm='intuitive')[0, 1], 6.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st07, self.st11],
                                      self.q1,
                                      algorithm='intuitive')[0, 1], 6.0)
     self.assertAlmostEqual(
         stds.victor_purpura_distance([self.st07, self.st11],
                                      self.q5,
                                      algorithm='intuitive')[0, 1], 2.0, 5)
     self.assertEqual(
         stds.victor_purpura_distance([self.st07, self.st11],
                                      self.q6,
                                      algorithm='intuitive')[0, 1], 2.0)
     # Tests on unordered spiketrains
     self.assertEqual(
         stds.victor_purpura_distance([self.st11, self.st13],
                                      self.q4,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st12, self.st13],
                                      self.q4,
                                      algorithm='intuitive')[0, 1])
     self.assertNotEqual(
         stds.victor_purpura_distance([self.st11, self.st13],
                                      self.q4,
                                      sort=False,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st12, self.st13],
                                      self.q4,
                                      sort=False,
                                      algorithm='intuitive')[0, 1])
     # Tests on metric properties with random spiketrains
     # (explicit calculation of second metric axiom in particular case,
     # because from dist_matrix it is trivial)
     dist_matrix = stds.victor_purpura_distance(
         [self.st21, self.st22, self.st23], self.q3, algorithm='intuitive')
     for i in range(3):
         for j in range(3):
             self.assertGreaterEqual(dist_matrix[i, j], 0)
             if dist_matrix[i, j] == 0:
                 assert_array_equal(self.rd_st_list[i], self.rd_st_list[j])
     assert_array_equal(
         stds.victor_purpura_distance([self.st21, self.st22],
                                      self.q3,
                                      algorithm='intuitive'),
         stds.victor_purpura_distance([self.st22, self.st21],
                                      self.q3,
                                      algorithm='intuitive'))
     self.assertLessEqual(dist_matrix[0, 1],
                          dist_matrix[0, 2] + dist_matrix[1, 2])
     self.assertLessEqual(dist_matrix[0, 2],
                          dist_matrix[1, 2] + dist_matrix[0, 1])
     self.assertLessEqual(dist_matrix[1, 2],
                          dist_matrix[0, 1] + dist_matrix[0, 2])
     # Tests on proper unit conversion
     self.assertAlmostEqual(
         stds.victor_purpura_distance([self.st14, self.st16],
                                      self.q3,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st15, self.st16],
                                      self.q3,
                                      algorithm='intuitive')[0, 1])
     self.assertAlmostEqual(
         stds.victor_purpura_distance([self.st16, self.st14],
                                      self.q3,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st16, self.st15],
                                      self.q3,
                                      algorithm='intuitive')[0, 1])
     self.assertEqual(
         stds.victor_purpura_distance([self.st01, self.st05],
                                      self.q3,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st01, self.st05],
                                      self.q7,
                                      algorithm='intuitive')[0, 1])
     # Tests on algorithmic behaviour for equal spike times
     self.assertEqual(
         stds.victor_purpura_distance([self.st31, self.st34],
                                      self.q3,
                                      algorithm='intuitive')[0, 1],
         0.8 + 1.0)
     self.assertEqual(
         stds.victor_purpura_distance([self.st31, self.st34],
                                      self.q3,
                                      algorithm='intuitive')[0, 1],
         stds.victor_purpura_distance([self.st32, self.st33],
                                      self.q3,
                                      algorithm='intuitive')[0, 1])
     self.assertEqual(
         stds.victor_purpura_distance(
             [self.st31, self.st33], self.q3, algorithm='intuitive')[0, 1] *
         2.0,
         stds.victor_purpura_distance([self.st32, self.st34],
                                      self.q3,
                                      algorithm='intuitive')[0, 1])
     # Tests on spike train list lengthes smaller than 2
     self.assertEqual(
         stds.victor_purpura_distance([self.st21],
                                      self.q3,
                                      algorithm='intuitive')[0, 0], 0)
     self.assertEqual(
         len(
             stds.victor_purpura_distance([],
                                          self.q3,
                                          algorithm='intuitive')), 0)