def compute_spikes_covariance(self,
                               binned_spikes_trains,
                               binsize=None,
                               num_bins=None,
                               **kwargs):
     """A method to compute the covariances
        among the spikes' trains of a set of elephant.conversion.BinnedSpikeTrain,
        using the lephant.spike_train_correlation.covariance method.
        - binned_spikes_trains: an elephant.conversion.BinnedSpikeTrain instance, or a
                                sequence (array, list, tuple) of Spikes Trains or of spikes' times arrays
        - binsize: the size (float, in ms) of the bin to be used. Default=None.
         - num_bins: the number (integer > 0) of bins to be used. Default=None.
         If none of binsize or num_bins if given, a bin size equal to the sampling period is used.
        Returns:
         - a dictionary of the following key-value pair(s):
          "covariance": correlation_coefficient array
          "binned_spikes_trains": the elephant.conversion.BinnedSpikeTrain instance used for the computation
     """
     binned_spikes_trains = self._assert_binned_spikes_trains(
         binned_spikes_trains, binsize, num_bins)
     from elephant.spike_train_correlation import covariance
     return {
         self._get_comput_res_type(): covariance(binned_spikes_trains,
                                                 **kwargs),
         self.binned_spikes_trains_name: binned_spikes_trains
     }
    def test_covariance_binned(self):
        '''
        Test covariance between two binned spike trains.
        '''

        # Calculate clipped and unclipped
        res_clipped = sc.covariance(
            self.binned_st, binary=True)
        res_unclipped = sc.covariance(
            self.binned_st, binary=False)

        # Check dimensions
        self.assertEqual(len(res_clipped), 2)
        self.assertEqual(len(res_unclipped), 2)

        # Check result unclipped against result calculated from scratch for
        # the off-diagonal element
        mat = self.binned_st.to_array()
        mean_0 = np.mean(mat[0])
        mean_1 = np.mean(mat[1])
        target_from_scratch = \
            np.dot(mat[0] - mean_0, mat[1] - mean_1) / (len(mat[0]) - 1)

        # Check result unclipped against result calculated by numpy.corrcoef
        target_numpy = np.cov(mat)

        self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
        self.assertAlmostEqual(res_unclipped[0][1], target_from_scratch)
        self.assertAlmostEqual(res_unclipped[1][0], target_from_scratch)

        # Check result clipped against result calculated from scratch for
        # the off-diagonal elemant
        mat = self.binned_st.to_bool_array()
        mean_0 = np.mean(mat[0])
        mean_1 = np.mean(mat[1])
        target_from_scratch = \
            np.dot(mat[0] - mean_0, mat[1] - mean_1) / (len(mat[0]) - 1)

        # Check result unclipped against result calculated by numpy.corrcoef
        target_numpy = np.cov(mat)

        self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
        self.assertAlmostEqual(res_clipped[0][1], target_from_scratch)
        self.assertAlmostEqual(res_clipped[1][0], target_from_scratch)
Ejemplo n.º 3
0
    def test_covariance_binned(self):
        '''
        Test covariance between two binned spike trains.
        '''

        # Calculate clipped and unclipped
        res_clipped = sc.covariance(
            self.binned_st, binary=True)
        res_unclipped = sc.covariance(
            self.binned_st, binary=False)

        # Check dimensions
        self.assertEqual(len(res_clipped), 2)
        self.assertEqual(len(res_unclipped), 2)

        # Check result unclipped against result calculated from scratch for
        # the off-diagonal element
        mat = self.binned_st.to_array()
        mean_0 = np.mean(mat[0])
        mean_1 = np.mean(mat[1])
        target_from_scratch = \
            np.dot(mat[0] - mean_0, mat[1] - mean_1) / (len(mat[0]) - 1)

        # Check result unclipped against result calculated by numpy.corrcoef
        target_numpy = np.cov(mat)

        self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
        self.assertAlmostEqual(res_unclipped[0][1], target_from_scratch)
        self.assertAlmostEqual(res_unclipped[1][0], target_from_scratch)

        # Check result clipped against result calculated from scratch for
        # the off-diagonal elemant
        mat = self.binned_st.to_bool_array()
        mean_0 = np.mean(mat[0])
        mean_1 = np.mean(mat[1])
        target_from_scratch = \
            np.dot(mat[0] - mean_0, mat[1] - mean_1) / (len(mat[0]) - 1)

        # Check result unclipped against result calculated by numpy.corrcoef
        target_numpy = np.cov(mat)

        self.assertAlmostEqual(target_from_scratch, target_numpy[0][1])
        self.assertAlmostEqual(res_clipped[0][1], target_from_scratch)
        self.assertAlmostEqual(res_clipped[1][0], target_from_scratch)
Ejemplo n.º 4
0
    def test_covariance_binned_short_input(self):
        '''
        Test if input list of only one binned spike train yields correct result
        that matches numpy.cov (covariance with itself)
        '''
        # Calculate correlation
        binned_st = conv.BinnedSpikeTrain(self.st_0,
                                          t_start=0 * pq.ms,
                                          t_stop=50. * pq.ms,
                                          binsize=1 * pq.ms)
        result = sc.covariance(binned_st, binary=True, fast=False)

        # Check result unclipped against result calculated by numpy.corrcoef
        mat = binned_st.to_bool_array()
        target = np.cov(mat)

        # Check result and dimensionality of result
        self.assertEqual(result.ndim, target.ndim)
        assert_array_almost_equal(result, target)
        assert_array_almost_equal(
            target, sc.covariance(binned_st, binary=True, fast=True))
    def produce_covariances(self,
                            spiketrain_list=None,
                            binary=False,
                            **kwargs):
        """
        Calculates the covariances between all pairs of spike trains.

        Parameters
        ----------
        spiketrain_list : list of neo.SpikeTrain (default None)
            If no list is passed the function tries to access the class
            parameter 'spiketrains'.

        binary: bool (default False)
            Parameter is passed to
            elephant.spike_train_correlation.covariance()

        kwargs:
            Passed to elephant.conversion.BinnedSpikeTrain()

        Returns : list of floats
            list of covariances of length = (N^2 - N)/2 where N is the number
            of spike trains.
        -------
        """
        try:

            def robust_BinnedSpikeTrain(spiketrains,
                                        binsize=2 * ms,
                                        num_bins=None,
                                        t_start=None,
                                        t_stop=None,
                                        **add_args):
                return BinnedSpikeTrain(spiketrains,
                                        binsize=binsize,
                                        num_bins=num_bins,
                                        t_start=t_start,
                                        t_stop=t_stop)

            if spiketrain_list is None:
                # assuming the class has the property 'spiketrains' and it
                # contains a list of neo.Spiketrains
                binned_sts = robust_BinnedSpikeTrain(self.spiketrains,
                                                     **kwargs)
            else:
                binned_sts = robust_BinnedSpikeTrain(spiketrain_list, **kwargs)
            cov_matrix = covariance(binned_sts, binary=binary)
            idx = triu_indices(len(cov_matrix), 1)
            return cov_matrix[idx]
        except:
            self.unimplemented()
    def test_covariance_binned_same_spiketrains(self):
        '''
        Test if the covariation between two identical binned spike
        trains evaluates to the expected 2x2 matrix.
        '''
        # Calculate correlation
        binned_st = conv.BinnedSpikeTrain(
            [self.st_0, self.st_0], t_start=0 * pq.ms, t_stop=50. * pq.ms,
            binsize=1 * pq.ms)
        target = sc.covariance(binned_st)

        # Check dimensions
        self.assertEqual(len(target), 2)
        # Check result
        assert_array_equal(target[0][0], target[1][1])
Ejemplo n.º 7
0
    def test_covariance_binned_same_spiketrains(self):
        '''
        Test if the covariation between two identical binned spike
        trains evaluates to the expected 2x2 matrix.
        '''
        # Calculate correlation
        binned_st = conv.BinnedSpikeTrain(
            [self.st_0, self.st_0], t_start=0 * pq.ms, t_stop=50. * pq.ms,
            binsize=1 * pq.ms)
        target = sc.covariance(binned_st)

        # Check dimensions
        self.assertEqual(len(target), 2)
        # Check result
        assert_array_equal(target[0][0], target[1][1])
    def test_covariance_binned_short_input(self):
        '''
        Test if input list of only one binned spike train yields correct result
        that matches numpy.cov (covariance with itself)
        '''
        # Calculate correlation
        binned_st = conv.BinnedSpikeTrain(
            self.st_0, t_start=0 * pq.ms, t_stop=50. * pq.ms,
            binsize=1 * pq.ms)
        target = sc.covariance(binned_st)

        # Check result unclipped against result calculated by numpy.corrcoef
        mat = binned_st.to_bool_array()
        target_numpy = np.cov(mat)

        # Check result and dimensionality of result
        self.assertEqual(target.ndim, target_numpy.ndim)
        self.assertAlmostEqual(target, target_numpy)
Ejemplo n.º 9
0
 def test_covariance_fast_mode(self):
     np.random.seed(27)
     st = homogeneous_poisson_process(rate=10 * pq.Hz, t_stop=10 * pq.s)
     binned_st = conv.BinnedSpikeTrain(st, num_bins=10)
     assert_array_almost_equal(sc.covariance(binned_st, fast=False),
                               sc.covariance(binned_st, fast=True))