neuroprob/inference.py
def VI_filtering(model, ini_X, spikes, VI_steps, fitting_options, past_spikes=None):
    """
    Decode causally taking into account covariate prior. In the most general case, treat model as LVM 
    but fix the latent variables at all timesteps except the last and fit model with fixed tuning. Note 
    filtering has to be done recursively, simultaneous inference of LVM is smoothing.

    Past covariate values before the decoding window (relevant in e.g. GP priors) are inserted via ``ini_X``, 
    identical to the procedure of initialization with *preprocess()*. Observed covariates will be variational 
    distributions with zero standard deviation.

    All model parameters are kept constant, only the decoded variables are optimized. Optimizers need to 
    be set up before with *set_optimizers()*.

    Reduces to Kalman filtering when Gaussian and Euclidean.

    :param list ini_X: 
    :param np.array spikes: 
    :param np.array past_spikes: spikes before the decoding window (relevant for GLMs with filter_len>1)
    """
    assert self.maps == 1 # no HMM supported

    rc_t = np.concatenate((past_spikes, spikes), dim=-1) if past_spikes is not None else spikes
    decode_len = spikes.shape[-1]
    past_len = past_spikes.shape[-1] if past_spikes is not None else 0
    resamples = rc_t.shape[-1]

    # set all parameters to not learnable
    label_for_constrain = []
    for name, param in self.named_parameters():
        if name[:16] == 'inputs.lv_':
            label_for_constrain.append(param)
            continue
        param.requires_grad = False

    def func(obj):
        decode_past = obj.saved_covariates[0].shape[0]
        k = 0
        for name, param in obj.named_parameters():
            if name[:16] == 'inputs.lv_':
                param.data[:decode_past] = obj.saved_covariates[k].shape[0]
                k += 1

    for k in range(past_len+1, past_len+decode_len):
        bs = [k+1, resamples-k-1] if resamples-k-1 > 0 else k+1
        model.inputs.set_XZ(ini_X, resamples, rc_t, bs) # non-continous batches
        model.likelihood.set_Y(rc_t)
        
        model.batches = 1 # only optimize first batch
        model.saved_covariates = []
        for p in label_for_constrain:
            model.saved_covariates.append(p.data[:k-1])

        # infer only last LV in batch 0
        model.fit(VI_steps, *fitting_options, callback=func) #loss_margin=-1e2, margin_epochs=100, anneal_func=annealing, 
             #cov_samples=32, ll_samples=1, bound='ELBO', ll_mode='MC'

    # set all parameters to learnable
    for param in self.parameters():
        param.requires_grad = True

    del model.saved_covariates

    return

    Decode causally taking into account covariate prior. In the most general case, treat model as LVM 
    but fix the latent variables at all timesteps except the last and fit model with fixed tuning. Note 
    filtering has to be done recursively, simultaneous inference of LVM is smoothing.

    Past covariate values before the decoding window (relevant in e.g. GP priors) are inserted via ``ini_X``, 
    identical to the procedure of initialization with *preprocess()*. Observed covariates will be variational 
    distributions with zero standard deviation.

    All model parameters are kept constant, only the decoded variables are optimized. Optimizers need to 
    be set up before with *set_optimizers()*.

    Reduces to Kalman filtering when Gaussian and Euclidean.

    :param list ini_X: 
    :param np.array spikes: 
    :param np.array past_spikes: spikes before the decoding window (relevant for GLMs with filter_len>1)
    
Decode causally taking into account covariate prior. In the most general case, treat model as LVM
but fix the latent variables at all timesteps except the last and fit model with fixed tuning. Note
filtering has to be done recursively, simultaneous inference of LVM is smoothing.

Past covariate values before the decoding window (relevant in e.g. GP priors) are inserted via ``ini_X``,
identical to the procedure of initialization with *preprocess()*. Observed covariates will be variational
distributions with zero standard deviation.

All model parameters are kept constant, only the decoded variables are optimized. Optimizers need to
be set up before with *set_optimizers()*.

Reduces to Kalman filtering when Gaussian and Euclidean.
Decode causally taking into account covariate prior . In the most general case treat model as LVM but fix the latent variables at all timesteps except the last and fit model with fixed tuning . Note filtering has to be done recursively simultaneous inference of LVM is smoothing . Past covariate values before the decoding window ( relevant in e . g . GP priors ) are inserted via ` ` ini_X ` ` identical to the procedure of initialization with * preprocess () * . Observed covariates will be variational distributions with zero standard deviation . All model parameters are kept constant only the decoded variables are optimized . Optimizers need to be set up before with * set_optimizers () * . Reduces to Kalman filtering when Gaussian and Euclidean .
def VI_filtering(model, ini_X, spikes, VI_steps, fitting_options, past_spikes=None):
    assert self.maps == 1 
    rc_t = np.concatenate((past_spikes, spikes), dim=-1) if past_spikes is not None else spikes
    decode_len = spikes.shape[-1]
    past_len = past_spikes.shape[-1] if past_spikes is not None else 0
    resamples = rc_t.shape[-1]
    label_for_constrain = []
    for name, param in self.named_parameters():
        if name[:16] == 'inputs.lv_':
            label_for_constrain.append(param)
            continue
        param.requires_grad = False
    def func(obj):
        decode_past = obj.saved_covariates[0].shape[0]
        k = 0
        for name, param in obj.named_parameters():
            if name[:16] == 'inputs.lv_':
                param.data[:decode_past] = obj.saved_covariates[k].shape[0]
                k += 1
    for k in range(past_len+1, past_len+decode_len):
        bs = [k+1, resamples-k-1] if resamples-k-1 > 0 else k+1
        model.inputs.set_XZ(ini_X, resamples, rc_t, bs) 
        model.likelihood.set_Y(rc_t)
        model.batches = 1 
        model.saved_covariates = []
        for p in label_for_constrain:
            model.saved_covariates.append(p.data[:k-1])
        model.fit(VI_steps, *fitting_options, callback=func) 
    for param in self.parameters():
        param.requires_grad = True
    del model.saved_covariates
    return
def VI_filtering ( model , ini_X , spikes , VI_steps , fitting_options , past_spikes = None ) : assert self . maps == 1 rc_t = np . concatenate ( ( past_spikes , spikes ) , dim = - 1 ) if past_spikes is not None else spikes decode_len = spikes . shape [ - 1 ] past_len = past_spikes . shape [ - 1 ] if past_spikes is not None else 0 resamples = rc_t . shape [ - 1 ] label_for_constrain = [ ] for name , param in self . named_parameters ( ) : if name [ : 16 ] == 'inputs.lv_' : label_for_constrain . append ( param ) continue param . requires_grad = False def func ( obj ) : decode_past = obj . saved_covariates [ 0 ] . shape [ 0 ] k = 0 for name , param in obj . named_parameters ( ) : if name [ : 16 ] == 'inputs.lv_' : param . data [ : decode_past ] = obj . saved_covariates [ k ] . shape [ 0 ] k += 1 for k in range ( past_len + 1 , past_len + decode_len ) : bs = [ k + 1 , resamples - k - 1 ] if resamples - k - 1 > 0 else k + 1 model . inputs . set_XZ ( ini_X , resamples , rc_t , bs ) model . likelihood . set_Y ( rc_t ) model . batches = 1 model . saved_covariates = [ ] for p in label_for_constrain : model . saved_covariates . append ( p . data [ : k - 1 ] ) model . fit ( VI_steps , * fitting_options , callback = func ) for param in self . parameters ( ) : param . requires_grad = True del model . saved_covariates return
Decode causally taking into account covariate prior.
Decode causally taking into account covariate prior .
"""
    Decode causally taking into account covariate prior. In the most general case, treat model as LVM 
    but fix the latent variables at all timesteps except the last and fit model with fixed tuning. Note 
    filtering has to be done recursively, simultaneous inference of LVM is smoothing.

    Past covariate values before the decoding window (relevant in e.g. GP priors) are inserted via ``ini_X``, 
    identical to the procedure of initialization with *preprocess()*. Observed covariates will be variational 
    distributions with zero standard deviation.

    All model parameters are kept constant, only the decoded variables are optimized. Optimizers need to 
    be set up before with *set_optimizers()*.

    Reduces to Kalman filtering when Gaussian and Euclidean.

    :param list ini_X: 
    :param np.array spikes: 
    :param np.array past_spikes: spikes before the decoding window (relevant for GLMs with filter_len>1)
    """ # no HMM supported # set all parameters to not learnable # non-continous batches # only optimize first batch # infer only last LV in batch 0 #loss_margin=-1e2, margin_epochs=100, anneal_func=annealing,  #cov_samples=32, ll_samples=1, bound='ELBO', ll_mode='MC' # set all parameters to learnable
model
ini_X
None
spikes
None
VI_steps
fitting_options
past_spikes
spikes before the decoding window (relevant for GLMs with filter_len>1)
spikes before the decoding window ( relevant for GLMs with filter_len > 1 )