コード例 #1
0
 def test_get_seq_sqrt(self):
     data = [self.data2[0]]
     seqs = gpfa_util.get_seqs(data, bin_size=self.bin_size)
     seqs_not_sqrt = gpfa_util.get_seqs(data, bin_size=self.bin_size,
                                        use_sqrt=False)
     self.assertEqual(seqs['T'], seqs_not_sqrt['T'])
     self.assertEqual(seqs['y'].shape, seqs_not_sqrt['y'].shape)
コード例 #2
0
 def _format_training_data(self, spiketrains):
     seqs = gpfa_util.get_seqs(spiketrains, self.bin_size)
     # Remove inactive units based on training set
     self.has_spikes_bool = np.hstack(seqs['y']).any(axis=1)
     for seq in seqs:
         seq['y'] = seq['y'][self.has_spikes_bool, :]
     return seqs
コード例 #3
0
 def test_cut_trials_larger_length(self):
     data = [self.data2[0]]
     seqs = gpfa_util.get_seqs(data, bin_size=self.bin_size)
     seg_length = seqs[0]['T'] + 1
     with self.assertWarns(UserWarning):
         gpfa_util.cut_trials(seqs, seg_length=seg_length)
コード例 #4
0
 def test_cut_trials_same_length(self):
     data = [self.data2[0]]
     seqs = gpfa_util.get_seqs(data, bin_size=self.bin_size)
     seg_length = seqs[0]['T']
     seqs_cut = gpfa_util.cut_trials(seqs, seg_length=seg_length)
     assert_array_almost_equal(seqs[0]['y'], seqs_cut[0]['y'])
コード例 #5
0
 def test_cut_trials_zero_length(self):
     seqs = gpfa_util.get_seqs(self.data2, bin_size=self.bin_size)
     with self.assertRaises(ValueError):
         gpfa_util.cut_trials(seqs, seg_length=0)
コード例 #6
0
    def transform(self, spiketrains, returned_data=['latent_variable_orth']):
        """
        Obtain trajectories of neural activity in a low-dimensional latent
        variable space by inferring the posterior mean of the obtained GPFA
        model and applying an orthonormalization on the latent variable space.

        Parameters
        ----------
        spiketrains : list of list of neo.SpikeTrain
            Spike train data to be transformed to latent variables.
            The outer list corresponds to trials and the inner list corresponds
            to the neurons recorded in that trial, such that
            `spiketrains[l][n]` is the spike train of neuron `n` in trial `l`.
            Note that the number and order of `neo.SpikeTrain` objects per
            trial must be fixed such that `spiketrains[l][n]` and
            `spiketrains[k][n]` refer to spike trains of the same neuron
            for any choices of `l`, `k`, and `n`.
        returned_data : list of str
            The dimensionality reduction transform generates the following
            resultant data:

               'latent_variable_orth': orthonormalized posterior mean of latent
               variable

               'latent_variable': posterior mean of latent variable before
               orthonormalization

               'Vsm': posterior covariance between latent variables

               'VsmGP': posterior covariance over time for each latent variable

               'y': neural data used to estimate the GPFA model parameters

            `returned_data` specifies the keys by which the data dict is
            returned.

            Default is ['latent_variable_orth'].

        Returns
        -------
        np.ndarray or dict
            When the length of `returned_data` is one, a single np.ndarray,
            containing the requested data (the first entry in `returned_data`
            keys list), is returned. Otherwise, a dict of multiple np.ndarrays
            with the keys identical to the data names in `returned_data` is
            returned.

            N-th entry of each np.ndarray is a np.ndarray of the following
            shape, specific to each data type, containing the corresponding
            data for the n-th trial:

                `latent_variable_orth`: (#latent_vars, #bins) np.ndarray

                `latent_variable`:  (#latent_vars, #bins) np.ndarray

                `y`:  (#units, #bins) np.ndarray

                `Vsm`:  (#latent_vars, #latent_vars, #bins) np.ndarray

                `VsmGP`:  (#bins, #bins, #latent_vars) np.ndarray

            Note that the num. of bins (#bins) can vary across trials,
            reflecting the trial durations in the given `spiketrains` data.

        Raises
        ------
        ValueError
            If the number of neurons in `spiketrains` is different from that
            in the training spiketrain data.

            If `returned_data` contains keys different from the ones in
            `self.valid_data_names`.
        """
        if len(spiketrains[0]) != len(self.has_spikes_bool):
            raise ValueError("'spiketrains' must contain the same number of "
                             "neurons as the training spiketrain data")
        invalid_keys = set(returned_data).difference(self.valid_data_names)
        if len(invalid_keys) > 0:
            raise ValueError("'returned_data' can only have the following "
                             "entries: {}".format(self.valid_data_names))
        seqs = gpfa_util.get_seqs(spiketrains, self.bin_size)
        for seq in seqs:
            seq['y'] = seq['y'][self.has_spikes_bool, :]
        seqs, ll = gpfa_core.exact_inference_with_ll(seqs,
                                                     self.params_estimated,
                                                     get_ll=True)
        self.transform_info['log_likelihood'] = ll
        self.transform_info['num_bins'] = seqs['T']
        Corth, seqs = gpfa_core.orthonormalize(self.params_estimated, seqs)
        self.transform_info['Corth'] = Corth
        if len(returned_data) == 1:
            return seqs[returned_data[0]]
        return {x: seqs[x] for x in returned_data}