neuroprob/inference.py
def evaluate(self, batch, obs_neuron=None, cov_samples=1, ll_samples=1, ll_mode='MC', lv_input=None):
        """
        Sample from the predictive posterior.
        
        Sample from the neural encoding model. Note when sampling the latent state (neuron=[], >1 HMM states), 
        the likelihood module will be set with *preprocess()* and affects model fitting after calling this.
        
        .. note:: The model is evaluated with its variational posterior mean values, which is identical to the 
            ML/MAP parameters when non-probabilistic model fitting is performed.
        
        Filtered and HMM inference require likelihood to be set with data Y, then validated with *validate_model()*
        
        Sampling from a conditional Poisson process, with choice of neurons to condition on.
        covariates has timesteps T. The ini_train will be taken as the spike train corresponding to the stimulus history 
        segment at the start, which is relevant with temporal filtering. Without temporal filtering, ini_train will be 
        used to extract the trial size of the sampled spike train.
        
        :param list covariates: input covariates list of time series of shape (T,), initialize hmm tuple (hmm_t, logp_0), 
                                None uses learned values stored. Discrete state is inferred with Viterbi, np.array gives 
                                observed HMM state. This is read from the last element of covariates list when self.maps>1
        :param np.array ini_spktrain: initial spike train segment of shape (trials, neurons, h_len-1)
        :param np.array neuron: neuron indices that we want to sample from, conditioned on rest
        :param np.array obs_spktrain: observed spike train of shape (trials, neurons, T)
        :param bool stochastic: take into account variational variance in the inferred rate and dispersion
        
        :returns: [samples, trial, neuron, timestep] of spiketrain generated, rate, hmm_state
        :rtype: tuple of np.array
        """
        with torch.no_grad():
            XZ, _, _, _ = self.inputs.sample_XZ(batch, cov_samples, lv_input, False)
            timesteps = XZ.shape[1]
            obs_neuron = self.likelihood._validate_neuron(obs_neuron)
            F_mu, F_var = self.mapping.compute_F(XZ)

            maps = len(F_mu) if isinstance(F_mu, list) else 1
            if maps > 1:
                if self.mapping.state_obs:
                    discrete = self.hmm_state[0].to(self.mapping.dummy.device)
                else:
                    nll = torch.stack(self._nll(F_mu, F_var, maps, 'ELBO', ll_samples, 
                                                ll_mode, batch, obs_neuron, XZ, 1.)) # (state, time)
                    discrete = self.mapping.sample_state(timesteps, self.logp_0, trials=self.inputs.trials, 
                                                         cond_nll=nll, viterbi=True)
                    
                F_mu = self.slice_F(torch.stack(F_mu), discrete)
                F_var = self.slice_F(torch.stack(F_var), discrete)
                
            else: # only one state
                discrete = torch.zeros((self.inputs.trials, timesteps), dtype=torch.long).to(self.mapping.dummy.device)
        
        rate = self.likelihood.sample_rate(F_mu, F_var, self.inputs.trials, ll_samples) # MC, trials, neuron, time   
        return XZ, rate, discrete

        Sample from the predictive posterior.
        
        Sample from the neural encoding model. Note when sampling the latent state (neuron=[], >1 HMM states), 
        the likelihood module will be set with *preprocess()* and affects model fitting after calling this.
        
        .. note:: The model is evaluated with its variational posterior mean values, which is identical to the 
            ML/MAP parameters when non-probabilistic model fitting is performed.
        
        Filtered and HMM inference require likelihood to be set with data Y, then validated with *validate_model()*
        
        Sampling from a conditional Poisson process, with choice of neurons to condition on.
        covariates has timesteps T. The ini_train will be taken as the spike train corresponding to the stimulus history 
        segment at the start, which is relevant with temporal filtering. Without temporal filtering, ini_train will be 
        used to extract the trial size of the sampled spike train.
        
        :param list covariates: input covariates list of time series of shape (T,), initialize hmm tuple (hmm_t, logp_0), 
                                None uses learned values stored. Discrete state is inferred with Viterbi, np.array gives 
                                observed HMM state. This is read from the last element of covariates list when self.maps>1
        :param np.array ini_spktrain: initial spike train segment of shape (trials, neurons, h_len-1)
        :param np.array neuron: neuron indices that we want to sample from, conditioned on rest
        :param np.array obs_spktrain: observed spike train of shape (trials, neurons, T)
        :param bool stochastic: take into account variational variance in the inferred rate and dispersion
        
        :returns: [samples, trial, neuron, timestep] of spiketrain generated, rate, hmm_state
        :rtype: tuple of np.array
        
Sample from the predictive posterior.
Sample from the neural encoding model. Note when sampling the latent state (neuron=[], >1 HMM states),
the likelihood module will be set with *preprocess()* and affects model fitting after calling this.

: The model is evaluated with its variational posterior mean values, which is identical to the
ML/MAP parameters when non-probabilistic model fitting is performed.

Filtered and HMM inference require likelihood to be set with data Y, then validated with *validate_model()

Sampling from a conditional Poisson process, with choice of neurons to condition on.
covariates has timesteps T. The ini_train will be taken as the spike train corresponding to the stimulus history
segment at the start, which is relevant with temporal filtering. Without temporal filtering, ini_train will be
used to extract the trial size of the sampled spike train.
Sample from the predictive posterior . Sample from the neural encoding model . Note when sampling the latent state ( neuron = [] > 1 HMM states ) the likelihood module will be set with * preprocess () * and affects model fitting after calling this . : The model is evaluated with its variational posterior mean values which is identical to the ML / MAP parameters when non - probabilistic model fitting is performed . Filtered and HMM inference require likelihood to be set with data Y then validated with * validate_model () Sampling from a conditional Poisson process with choice of neurons to condition on . covariates has timesteps T . The ini_train will be taken as the spike train corresponding to the stimulus history segment at the start which is relevant with temporal filtering . Without temporal filtering ini_train will be used to extract the trial size of the sampled spike train .
def evaluate(self, batch, obs_neuron=None, cov_samples=1, ll_samples=1, ll_mode='MC', lv_input=None):
        with torch.no_grad():
            XZ, _, _, _ = self.inputs.sample_XZ(batch, cov_samples, lv_input, False)
            timesteps = XZ.shape[1]
            obs_neuron = self.likelihood._validate_neuron(obs_neuron)
            F_mu, F_var = self.mapping.compute_F(XZ)
            maps = len(F_mu) if isinstance(F_mu, list) else 1
            if maps > 1:
                if self.mapping.state_obs:
                    discrete = self.hmm_state[0].to(self.mapping.dummy.device)
                else:
                    nll = torch.stack(self._nll(F_mu, F_var, maps, 'ELBO', ll_samples, 
                                                ll_mode, batch, obs_neuron, XZ, 1.)) 
                    discrete = self.mapping.sample_state(timesteps, self.logp_0, trials=self.inputs.trials, 
                                                         cond_nll=nll, viterbi=True)
                F_mu = self.slice_F(torch.stack(F_mu), discrete)
                F_var = self.slice_F(torch.stack(F_var), discrete)
            else: 
                discrete = torch.zeros((self.inputs.trials, timesteps), dtype=torch.long).to(self.mapping.dummy.device)
        rate = self.likelihood.sample_rate(F_mu, F_var, self.inputs.trials, ll_samples) 
        return XZ, rate, discrete
def evaluate ( self , batch , obs_neuron = None , cov_samples = 1 , ll_samples = 1 , ll_mode = 'MC' , lv_input = None ) : with torch . no_grad ( ) : XZ , _ , _ , _ = self . inputs . sample_XZ ( batch , cov_samples , lv_input , False ) timesteps = XZ . shape [ 1 ] obs_neuron = self . likelihood . _validate_neuron ( obs_neuron ) F_mu , F_var = self . mapping . compute_F ( XZ ) maps = len ( F_mu ) if isinstance ( F_mu , list ) else 1 if maps > 1 : if self . mapping . state_obs : discrete = self . hmm_state [ 0 ] . to ( self . mapping . dummy . device ) else : nll = torch . stack ( self . _nll ( F_mu , F_var , maps , 'ELBO' , ll_samples , ll_mode , batch , obs_neuron , XZ , 1. ) ) discrete = self . mapping . sample_state ( timesteps , self . logp_0 , trials = self . inputs . trials , cond_nll = nll , viterbi = True ) F_mu = self . slice_F ( torch . stack ( F_mu ) , discrete ) F_var = self . slice_F ( torch . stack ( F_var ) , discrete ) else : discrete = torch . zeros ( ( self . inputs . trials , timesteps ) , dtype = torch . long ) . to ( self . mapping . dummy . device ) rate = self . likelihood . sample_rate ( F_mu , F_var , self . inputs . trials , ll_samples ) return XZ , rate , discrete
Sample from the predictive posterior.
Sample from the predictive posterior .
"""
        Sample from the predictive posterior.
        
        Sample from the neural encoding model. Note when sampling the latent state (neuron=[], >1 HMM states), 
        the likelihood module will be set with *preprocess()* and affects model fitting after calling this.
        
        .. note:: The model is evaluated with its variational posterior mean values, which is identical to the 
            ML/MAP parameters when non-probabilistic model fitting is performed.
        
        Filtered and HMM inference require likelihood to be set with data Y, then validated with *validate_model()*
        
        Sampling from a conditional Poisson process, with choice of neurons to condition on.
        covariates has timesteps T. The ini_train will be taken as the spike train corresponding to the stimulus history 
        segment at the start, which is relevant with temporal filtering. Without temporal filtering, ini_train will be 
        used to extract the trial size of the sampled spike train.
        
        :param list covariates: input covariates list of time series of shape (T,), initialize hmm tuple (hmm_t, logp_0), 
                                None uses learned values stored. Discrete state is inferred with Viterbi, np.array gives 
                                observed HMM state. This is read from the last element of covariates list when self.maps>1
        :param np.array ini_spktrain: initial spike train segment of shape (trials, neurons, h_len-1)
        :param np.array neuron: neuron indices that we want to sample from, conditioned on rest
        :param np.array obs_spktrain: observed spike train of shape (trials, neurons, T)
        :param bool stochastic: take into account variational variance in the inferred rate and dispersion
        
        :returns: [samples, trial, neuron, timestep] of spiketrain generated, rate, hmm_state
        :rtype: tuple of np.array
        """ # (state, time) # only one state # MC, trials, neuron, time   
[samples, trial, neuron, timestep] of spiketrain generated, rate, hmm_state
[ samples trial neuron timestep ] of spiketrain generated rate hmm_state
tuple of np.array
self
batch
obs_neuron
cov_samples
ll_samples
ll_mode
lv_input
covariates
input covariates list of time series of shape (T,), initialize hmm tuple (hmm_t, logp_0),
None uses learned values stored. Discrete state is inferred with Viterbi, np.array gives
observed HMM state. This is read from the last element of covariates list when self.maps>1
input covariates list of time series of shape ( T ) initialize hmm tuple ( hmm_t logp_0 ) None uses learned values stored . Discrete state is inferred with Viterbi np . array gives observed HMM state . This is read from the last element of covariates list when self . maps > 1
ini_spktrain
initial spike train segment of shape (trials, neurons, h_len-1)
initial spike train segment of shape ( trials neurons h_len - 1 )
neuron
neuron indices that we want to sample from, conditioned on rest
neuron indices that we want to sample from conditioned on rest
obs_spktrain
observed spike train of shape (trials, neurons, T)
observed spike train of shape ( trials neurons T )
stochastic
take into account variational variance in the inferred rate and dispersion
take into account variational variance in the inferred rate and dispersion