Example #1
0
    def test_dichotomized_gaussian(self):
        bin_means = np.array([7, 9])
        bin_cov = np.array([[7, 3], [3, 9]])
        num_samples = 10000

        np.random.seed(42)

        # calculate marginal distribution of Poisson
        pmfs, cmfs, supports = poisson_marginals(bin_means)

        self.assertEqual(len(pmfs), 2)
        self.assertTrue(sum(map(sum, pmfs)) - 2. < 1e-4)
        self.assertEqual(len(pmfs[0]), len(supports[0]))
        self.assertEqual(len(pmfs[1]), len(supports[1]))
        self.assertEqual(len(pmfs[0]), len(cmfs[0]))
        self.assertEqual(len(pmfs[1]), len(cmfs[1]))

        # find paramters of DG
        gauss_means, gauss_covs, joints = find_dg_any_marginal(pmfs, bin_cov, supports)

        # generate samples
        np.random.seed(0)
        samples, hists = sample_dg_any_marginal(gauss_means, gauss_covs, num_samples, supports)

        self.assertTrue(samples[:, 0].mean() - 7, 1e-2)
        self.assertTrue(samples[:, 1].mean() - 9, 1e-2)
Example #2
0
    def sample_from_model(self, trials=None, reshape=False):
        """
        Missing documentation
        
        Parameters
        ----------
        trials : Type, optional
            Description (default None)
        reshape : bool, optional
            Description (default False)
        
        Returns
        -------
        Value : Type
            Description
        """

        trials = trials or range(self._original_spikes.T)
        spikes_windowed = self._original_spikes.to_windowed(
            self._window_size, trials)
        X = np.zeros(
            (len(trials), self._original_spikes.N, self._original_spikes.M))

        # statistics
        for c, t in enumerate(trials):
            bin_means = spikes_windowed.spikes[t, :, :].mean(axis=1)
            bin_cov = np.cov(spikes_windowed.spikes[t, :, :])

            # calculate marginal distribution of Poisson
            pmfs, cmfs, supports = poisson_marginals(bin_means)

            # find paramters of DG
            gauss_means, gauss_cov, joints = find_dg_any_marginal(
                pmfs, bin_cov, supports)

            # generate samples
            samples, hists = sample_dg_any_marginal(gauss_means, gauss_cov,
                                                    self._original_spikes.M,
                                                    supports)

            X[c, :, :] = samples.T

        if reshape:
            Y = np.zeros((X.shape[0] * X.shape[2], X.shape[1]))
            tot = 0
            for t in xrange(len(trials)):
                for c in xrange(X.shape[2]):
                    Y[tot, :] = X[t, :, c]
                    tot += 1
            return Y

        return Spikes(spikes=X)
Example #3
0
    def sample_from_model(self, trials=None, reshape=False):
        """
        Missing documentation
        
        Parameters
        ----------
        trials : Type, optional
            Description (default None)
        reshape : bool, optional
            Description (default False)
        
        Returns
        -------
        Value : Type
            Description
        """

        trials = trials or range(self._original_spikes.T)
        spikes_windowed = self._original_spikes.to_windowed(self._window_size, trials)
        X = np.zeros((len(trials), self._original_spikes.N, self._original_spikes.M))

        # statistics
        for c, t in enumerate(trials):
            bin_means = spikes_windowed.spikes[t, :, :].mean(axis=1)
            bin_cov = np.cov(spikes_windowed.spikes[t, :, :])

            # calculate marginal distribution of Poisson
            pmfs, cmfs, supports = poisson_marginals(bin_means)

            # find paramters of DG
            gauss_means, gauss_cov, joints = find_dg_any_marginal(pmfs, bin_cov, supports)

            # generate samples
            samples, hists = sample_dg_any_marginal(gauss_means, gauss_cov, self._original_spikes.M, supports)

            X[c, :, :] = samples.T

        if reshape:
            Y = np.zeros((X.shape[0] * X.shape[2], X.shape[1]))
            tot = 0
            for t in xrange(len(trials)):
                for c in xrange(X.shape[2]):
                    Y[tot, :] = X[t, :, c]
                    tot += 1
            return Y

        return Spikes(spikes=X)