<commit_before>from __future__ import division
import numpy as np

import pydare

from pybasicbayes.abstractions import Model, ModelGibbsSampling, ModelEM

from states import LDSStates

# TODO make separate versions for stationary, nonstationary,
# nonstationary-and-distinct-for-each-sequence

# NOTE: dynamics_distn should probably be an instance of AutoRegression,
# emission_distn should probably be an instance of Regression, and
# init_dynamics_distn should probably be an instance of Gaussian


############
#  mixins  #
############


class _LDSBase(Model):
    def __init__(self,dynamics_distn,emission_distn):
        self.dynamics_distn = dynamics_distn
        self.emission_distn = emission_distn
        self.states_list = []

    def add_data(self,data,**kwargs):
        self.states_list.append(LDSStates(model=self,data=data,**kwargs))
        return self

    def log_likelihood(self):
        raise NotImplementedError

    def generate(self,T,keep=True):
        s = LDSStates(model=self,T=T,initialize_from_prior=True)
        data = self._generate_obs(s)
        if keep:
            self.states_list.append(s)
        return data, s.stateseq

    def _generate_obs(self,s):
        if s.data is None:
            s.data = self.emission_distn.rvs(x=s.stateseq,return_xy=False)
        else:
            # filling in missing data
            raise NotImplementedError
        return s.data

    ### convenience properties

    @property
    def n(self):
        'latent dimension'
        return self.emission_distn.D_in

    @property
    def p(self):
        'emission dimension'
        return self.emission_distn.D_out

    @property
    def mu_init(self):
        return np.zeros(self.n)

    @property
    def sigma_init(self):
        return pydare.dlyap(self.dynamics_distn.A, self.dynamics_distn.sigma)

class _LDSGibbsSampling(_LDSBase,ModelGibbsSampling):
    def resample_model(self):<commit_msg>convenience improvements<commit_after>from __future__ import division
import numpy as np

import pydare

from pybasicbayes.abstractions import Model, ModelGibbsSampling, ModelEM

from states import LDSStates

# TODO make separate versions for stationary, nonstationary,
# nonstationary-and-distinct-for-each-sequence

# NOTE: dynamics_distn should probably be an instance of AutoRegression,
# emission_distn should probably be an instance of Regression, and
# init_dynamics_distn should probably be an instance of Gaussian


############
#  mixins  #
############


class _LDSBase(Model):
    def __init__(self,dynamics_distn,emission_distn):
        self.dynamics_distn = dynamics_distn
        self.emission_distn = emission_distn
        self.states_list = []

    def add_data(self,data,**kwargs):
        assert isinstance(data,np.ndarray)
        self.states_list.append(LDSStates(model=self,data=data,**kwargs))
        return self

    def add_datas(self,data_kwargs):
        assert isinstance(data,list)
        for kwargs in data_kwargs:
            self.add_data(**kwargs)
        return self

    def log_likelihood(self):
        raise NotImplementedError

    def generate(self,T,keep=True):
        s = LDSStates(model=self,T=T,initialize_from_prior=True)
        data = self._generate_obs(s)
        if keep:
            self.states_list.append(s)
        return data, s.stateseq

    def _generate_obs(self,s):
        if s.data is None:
            s.data = self.emission_distn.rvs(x=s.stateseq,return_xy=False)
        else:
            # filling in missing data
            raise NotImplementedError
        return s.data

    ### convenience properties

    @property
    def n(self):
        'latent dimension'
        return self.emission_distn.D_in

    @property
    def p(self):
        'emission dimension'
        return self.emission_distn.D_out

    @property
    def mu_init(self):
        return np.zeros(self.n)

    @property
    def sigma_init(self):
        return pydare.dlyap(self.dynamics_distn.A, self.dynamics_distn.sigma)

    @property
    def is_stable(self):
        return np.max(np.abs(np.linalg.eivals(self.dynamics_distn.A))) < 1.

class _LDSGibbsSampling(_LDSBase,ModelGibbsSampling):
    def resample_model(self):
{'fraction_non_alphanumeric': 0.09179780639008107, 'fraction_numerical': 0.0002384358607534573, 'mean_word_length': 3.1006842619745845, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5337841', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1272, 'n_words': 285}