How to use the pyglm.abstractions.Component function in pyglm

To help you get started, we’ve selected a few pyglm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github slinderman / pyglm / pyglm / internals / networks.py View on Github external
import abc

import numpy as np
from scipy.special import gammaln, psi

from pyglm.abstractions import Component

from pyglm.deps.pybasicbayes.util.stats import sample_discrete_from_log
from pyglm.deps.pybasicbayes.util.stats import sample_niw

from pyglm.internals.distributions import Bernoulli

# Import graph models from graphistician
from pyglm.deps.graphistician.networks import GaussianWeightedEigenmodel

class _NetworkComponent(Component):
    """
    Expose a graphistician network through the Component interface
    """
    __metaclass__ = abc.ABCMeta

    # Network class must be an instance of
    # GaussianWeightedNetworkDistribution
    _network_class = None

    def __init__(self, population, **network_hypers):
        self.population = population
        self.N = population.N
        self.B = population.B

        self._model = self._network_class(self.N, self.B, **network_hypers)
github slinderman / pyglm / pyglm / internals / activation.py View on Github external
"""
Activation models.
"""
import abc
import numpy as np

from pyglm.abstractions import Component
from pyglm.utils.profiling import line_profiled
PROFILING = False

class _ActivationBase(Component):
    """
    Base class for activations.
    """
    __metaclass__ =  abc.ABCMeta

    def __init__(self, population):
        self.population = population

    @property
    def N(self):
        return self.population.N

    @property
    def observation_model(self):
        return self.population.observation_model
github slinderman / pyglm / pyglm / internals / weights.py View on Github external
"""
Weight models
"""
import numpy as np
from scipy.linalg.lapack import dpotrs

from pyglm.abstractions import Component
from pyglm.internals.distributions import Bernoulli, Gaussian, TruncatedScalarGaussian
from pyglm.utils.utils import logistic, logit, normal_cdf, sample_truncnorm

from pyglm.utils.profiling import line_profiled
PROFILING = False

class NoWeights(Component):
    def __init__(self, population):
        self.population = population

        # Hard code the parameters to zero (no connections)
        self.A = np.zeros((self.N, self.N))
        self.W = np.zeros((self.N, self.N, self.B))

        # TODO: Fix hack
        self.E_A = self.A
        self.E_W = self.W
        self.E_WWT = np.zeros((self.N, self.N, self.B, self.B))

    @property
    def N(self):
        return self.population.N
github slinderman / pyglm / pyglm / internals / observations.py View on Github external
import abc
import numpy as np
from scipy.special import gammaln

import pypolyagamma as ppg

from hips.inference.slicesample import slicesample

from pyglm.abstractions import Component
from pyglm.utils.utils import logistic

from pyglm.utils.profiling import line_profiled
PROFILING = False


class _PolyaGammaAugmentedObservationsBase(Component):
    """
    Class to keep track of a set of spike count observations and the
    corresponding Polya-gamma auxiliary variables associated with them.
    """
    __metaclass__ = abc.ABCMeta

    def __init__(self, population, trunc=200):
        self.population = population
        num_threads = ppg.get_omp_num_threads()
        seeds = np.random.randint(2**16, size=num_threads)
        self.ppgs = [ppg.PyPolyaGamma(seed, trunc) for seed in seeds]
        self.N = self.population.N

    @property
    def activation(self):
        return self.population.activation_model
github slinderman / pyglm / pyglm / internals / background.py View on Github external
"""
Background models for neural spike train activations.

For example:
    - Linear Dynamical System (LDS) model for background rates

"""
import numpy as np
from pyglm.abstractions import Component

class NoBackground(Component):
    """
    Null background model.
    """
    def __init__(self, population):
        self.population = population
        self.N = self.population.N

    def generate(self, T):
        return np.zeros((T,self.N))

    def mean_background_activation(self, augmented_data):
        return np.zeros((1,self.N))

    def resample(self, augmented_data):
        pass
github slinderman / pyglm / pyglm / internals / background.py View on Github external
pass

    def meanfieldupdate(self, augmented_data):
        pass

    def get_vlb(self, augmented_data):
        return 0

    def resample_from_mf(self, augmented_data):
        pass

    def svi_step(self, augmented_data, minibatchfrac, stepsize):
        pass


class LinearDynamicalSystemBackground(Component):
    """
    Linear Dynamical System model for the background activation.
    Since the potentials for the activation are of a Gaussian form,
    we can perform conjugate Gibbs sampling or variational inference
    for a Gaussian LDS model.
    """
    def __init__(self, population, D=2,
                 A=None, C=None,
                 sigma_states=None,
                 sigma_C=1.0):

        self.population = population
        self.activation = population.activation_model
        self.N = self.population.N
        self.D = D
github slinderman / pyglm / pyglm / internals / weights.py View on Github external
pass

    def meanfieldupdate(self, augmented_data):
        pass

    def get_vlb(self, augmented_data):
        return 0

    def resample_from_mf(self, augmented_data):
        pass

    def svi_step(self, augmented_data, minibatchfrac, stepsize):
        pass


class _SpikeAndSlabGaussianWeightsBase(Component):
    def __init__(self, population):
        self.population = population

        # Initialize the parameters
        self._A = np.zeros((self.N, self.N))
        self._W = np.zeros((self.N, self.N, self.B))

    @property
    def N(self):
        return self.population.N

    @property
    def A(self):
        return self._A

    @A.setter
github slinderman / pyglm / pyglm / internals / bias.py View on Github external
"""
Bias models (of which there are only one)
"""
import numpy as np

from pyglm.abstractions import Component
from pyglm.internals.distributions import ScalarGaussian

class _GaussianBiasBase(Component):

    def __init__(self, population, mu_0=0.0, sigma_0=1.0):
        self.population = population

        # Save the prior parameters
        self.mu_0, self.sigma_0 = mu_0, sigma_0
        self.lambda_0 = 1.0/self.sigma_0

        # Initialize the parameters
        self.b = np.zeros((self.N,))

    @property
    def N(self):
        return self.population.N

    @property