text:
"""Summary

"""
import numpy as np
import pdb
from scipy.cluster.vq import kmeans2

from utils import *
from kernels import *
from config import *
from base_models import Base_Model, Base_SGP_Layer, Base_SGPR, Base_SGPLVM, Base_SGPSSM
from lik_layers import Gauss_Layer, Probit_Layer, Gauss_Emis

class SGPR_collapsed(Base_Model):
    """Summary

    Attributes:
        Din (TYPE): Description
        Dout (TYPE): Description
        ls (TYPE): Description
        M (TYPE): Description
        N (TYPE): Description
        sf (int): Description
        sn (int): Description
        x_train (TYPE): Description
        zu (TYPE): Description
    """

    def __init__(self, x_train, y_train, no_pseudo):
        '''
        This only works with real-valued (Gaussian) regression

        Args:
            x_train (TYPE): Description
            y_train (TYPE): Description
            no_pseudo (TYPE): Description

        '''

        super(SGPR_collapsed, self).__init__(y_train)
        self.N = N = y_train.shape[0]
        self.Dout = Dout = y_train.shape[1]
        self.Din = Din = x_train.shape[1]
        self.M = M = no_pseudo
        self.x_train = x_train

        # variables for the hyperparameters
        self.zu = np.zeros([M, Din])
        self.ls = np.zeros([Din, ])
        self.sf = 0
        self.sn = 0

    def objective_function(self, params, idxs=None, alpha=1.0, prop_mode=None):
        """Summary

        Args:
            params (TYPE): Description
            idxs (None, optional): Description
            alpha (float, optional): Description
            prop_mode (None, optional): Description

        Returns:
            TYPE: Description
        """
        x = self.x_train
        y = self.y_train
        N = self.N
        Dout = self.Dout
        M = self.M
        # update model with new hypers
        self.update_hypers(params)
        sf2 = np.exp(2 * self.sf)
        sn2 = np.exp(2 * self.sn)

        # compute the approximate log marginal likelihood
        Kuu_noiseless = compute_kernel(
            2 * self.ls, 2 * self.sf, self.zu, self.zu)
        Kuu = Kuu_noiseless + np.diag(JITTER * np.ones((M, )))
        Lu = np.linalg.cholesky(Kuu)
        Kuf = compute_kernel(2 * self.ls, 2 * self.sf, self.zu, x)
        V = np.linalg.solve(Lu, Kuf)
        r = sf2 - np.sum(V * V, axis=0)
        G = alpha * r + sn2
        iG = 1.0 / G
        A = np.eye(M) + np.dot(V * iG, V.T)
        La = np.linalg.cholesky(A)
        B = np.linalg.solve(La, V)
        iGy = iG[:, None] * y
        z_tmp = np.dot(B.T, np.dot(B, iGy)) * iG[:, None]
        z = iGy - z_tmp
        term1 = 0.5 * np.sum(z * y)
        term2 = Dout * np.sum(np.log(np.diag(La)))
        term3 = 0.5 * Dout * np.sum(np.log(G))
        term4 = 0.5 * N * Dout * np.log(2 * np.pi)
        c4 = 0.5 * Dout * (1 - alpha) / alpha
        term5 = c4 * np.sum(np.log(1 + alpha * r / sn2))
        energy = term1 + term2 + term3 + term4 + term5
        # print term1, term2 + term3, term4, term5

        zus = self.zu / np.exp(self.ls)
        xs = x / np.exp(self.ls)
        R = np.linalg.solve(Lu.T, V)
        RiG = R * iG
        RdQ = -np.dot(np.dot(R, z), z.T) + RiG - \
            np.dot(np.dot(RiG, B.T), B) * iG
        dG = z**2 + (-iG + iG**2 * np.sum(B * B, axis=0))[:, None]
        tmp = alpha * dG - (1 - alpha) / (1 + alpha * r[:, None] / sn2) / sn2
        RdQ2 = RdQ + R * tmp.T
        KW = Kuf * RdQ2
        KWR = Kuu_noiseless * (np.dot(RdQ2, R.T))
        P = (np.dot(KW, xs) - np.dot(KWR, zus)
             + (np.sum(KWR, axis=1) - np.sum(KW, axis=1))[:, None] * zus)
        dzu = P / np.exp(self.ls)
        dls = (-np.sum(P * zus, axis=0)
               - np.sum((np.dot(KW.T, zus) - np.sum(KW, axis=0)[:, None] * xs) * xs, axis=0))
        dsn = -np.sum(dG) * sn2 - (1 - alpha) * \
            np.sum(r / (1 + alpha * r / sn2)) / sn2
        dsf = (np.sum(Kuf * RdQ) - alpha * np.sum(r[:, None] * dG)
               + (1 - alpha) * np.sum(r / (1 + alpha * r / sn2)) / sn2)

        grad_all = {'zu': dzu, 'ls': dls, 'sn': dsn, 'sf': dsf}
        for p in self.fixed_params:
            grad_all[p] = np.zeros_like(grad_all[p])

        energy /= N
        for key in grad_all.keys():
            grad_all[key] /= N

        return energy, grad_all

    def predict_f(self, inputs, alpha=0.01, marginal=True):
        """Summary

        Args:
            inputs (TYPE): Description
            alpha (float, optional): Description
            marginal (bool, optional): Description

        Returns:
            TYPE: Description
        """
        x = self.x_train
        y = self.y_train
        N = self.N
        Dout = self.Dout
        M = self.M
        sf2 = np.exp(2 * self.sf)
        sn2 = np.exp(2 * self.sn)

        # compute the approximate log marginal likelihood
        Kuu_noiseless = compute_kernel(
            2 * self.ls, 2 * self.sf, self.zu, self.zu)
        Kuu = Kuu_noiseless + np.diag(JITTER * np.ones((M, )))
        Lu = np.linalg.cholesky(Kuu)
        Kuf = compute_kernel(2 * self.ls, 2 * self.sf, self.zu, x)
        Kut = compute_kernel(2 * self.ls, 2 * self.sf, self.zu, inputs)
        V = np.linalg.solve(Lu, Kuf)
        r = sf2 - np.sum(V * V, axis=0)
        G = alpha * r + sn2
        iG = 1.0 / G
        A = np.eye(M) + np.dot(V * iG, V.T)
        La = np.linalg.cholesky(A)
        B = np.linalg.solve(La, V)
        yhat = y * iG[:, None]
        yhatB = np.dot(yhat.T, B.T)
        beta = np.linalg.solve(Lu.T, np.linalg.solve(La.T, yhatB.T))
        W1 = np.eye(M) - np.linalg.solve(La.T,
                                         np.linalg.solve(La, np.eye(M))).T
        W = np.linalg.solve(Lu.T, np.linalg.solve(Lu.T, W1).T)
        KtuW = np.dot(Kut.T, W)
        mf = np.dot(Kut.T, beta)
        if marginal:
            vf = np.exp(2 * self.sf) - np.sum(KtuW * Kut.T, axis=1)
        else:
            Ktt = compute_kernel(2 * self.ls, 2 * self.sf, inputs, inputs)
            Ktt += np.diag(JITTER * np.ones((inputs.shape[0], )))
            vf = Ktt - np.dot(KtuW, Kut)
        return mf, vf

    def sample_f(self, inputs, no_samples=1):
        """Summary

        Args:
            inputs (TYPE): Description
            no_samples (int, optional): Description

        Returns:
            TYPE: Description
        """
        # TODO
        """Summary

        Args:
            inputs (TYPE): Description
            no_samples (int, optional): Description

        Returns:
            TYPE: Description
        """
        K = no_samples
        fs = np.zeros((inputs.shape[0], self.Dout, K))
        # TODO: implement this
        return fs

    def predict_y(self, inputs, alpha=0.01, marginal=True):
        """Summary

        Args:
            inputs (TYPE): Description
            alpha (float, optional): Description
            marginal (bool, optional): Description

        Returns:
            TYPE: Description
        """
        mf, vf = self.predict_f(inputs, alpha, marginal)
        if marginal:
            my, vy = mf, vf + np.exp(2 * self.sn)
        else:
            my, vy = mf, vf + np.exp(2 * self.sn) * np.eye(my.shape[0])
        return my, vy

    def init_hypers(self, y_train):
        """Summary

        Returns:
            TYPE: Description

        Args:
            y_train (TYPE): Description
        """
        N = self.N
        M = self.M
        Din = self.Din
        Dout = self.Dout
        x_train = self.x_train
        if N < 10000:
            centroids, label = kmeans2(x_train, M, minit='points')
        else:
            randind = np.random.permutation(N)
            centroids = x_train[randind[0:M], :]
        zu = centroids
        if N < 10000:
            X1 = np.copy(x_train)
        else:
            randind = np.random.permutation(N)
            X1 = X[randind[:5000], :]
        x_dist = cdist(X1, X1, 'euclidean')
        triu_ind = np.triu_indices(N)
        ls = np.zeros((Din, ))
        d2imed = np.median(x_dist[triu_ind])
        for i in range(Din):
            ls[i] = 2 * np.log(d2imed + 1e-16)
        sf = np.log(np.array([0.5]))

        params = dict()
        params['sf'] = sf
        params['ls'] = ls
        params['zu'] = zu
        params['sn'] = np.log(0.01)
        return params

    def get_hypers(self):
        """Summary

        Returns:
            TYPE: Description
        """
        params = {}
        M = self.M
        Din = self.Din
        Dout = self.Dout
        params['ls'] = self.ls
        params['sf'] = self.sf
        params['zu'] = self.zu
        params['sn'] = self.sn
        return params

    def update_hypers(self, params):
        """Summary

        Args:
            params (TYPE): Description
        """
        self.ls = params['ls']
        self.sf = params['sf']
        self.zu = params['zu']
        self.sn = params['sn']

class SGP_Layer(Base_SGP_Layer):
    """Sparse Gaussian process layer
    """

    def __init__(self, no_train, input_size, output_size, no_pseudo,
            nat_param=True):
        """Initialisation

        Args:
            no_train (int): Number of training points
            input_size (int): Number of input dimensions
            output_size (int): Number of output dimensions
            no_pseudo (int): Number of pseudo-points
        """
        super(SGP_Layer, self).__init__(
            no_train, input_size, output_size, no_pseudo, nat_param)

    def compute_KL(self):
        """Summary

        Returns:
            TYPE: Description
        """
        M, Dout = self.M, self.Dout
        # log det prior
        (sign, logdet) = np.linalg.slogdet(self.Kuu)
        logdet_prior = Dout * logdet
        # log det posterior
        (sign, logdet) = np.linalg.slogdet(self.Su)
        logdet_post = np.sum(logdet)
        # trace term
        trace_term = np.sum(self.Kuuinv * self.Splusmm)
        KL = 0.5 * (logdet_prior - logdet_post - Dout * M + trace_term)
        return KL

    @profile
    def backprop_grads_lvm_mm(self, m, v, dm, dv, psi1, psi2, mx, vx):
        """Summary

        Args:
            m (TYPE): Description
            v (TYPE): Description
            dm (TYPE): Description
            dv (TYPE): Description
            psi1 (TYPE): Description
            psi2 (TYPE): Description
            mx (TYPE): Description
            vx (TYPE): Description

        Returns:
            TYPE: Description
        """
        N = self.N
        M = self.M
        ls = np.exp(self.ls)
        sf2 = np.exp(2 * self.sf)
        triu_ind = np.triu_indices(M)
        diag_ind = np.diag_indices(M)
        mu = self.mu
        Su = self.Su
        Suinv = self.Suinv
        Spmm = self.Splusmm
        Kuuinv = self.Kuuinv
        Kuu = self.Kuu

        # compute grads wrt psi1 and psi2
        dm_all = dm - 2 * dv * m
        dpsi1 = np.einsum('nd,dm->nm', dm_all, self.A)
        dpsi2 = np.einsum('nd,dab->nab', dv, self.B_sto)
        dsf2, dls, dzu, dmx, dvx = compute_psi_derivatives(
            dpsi1, psi1, dpsi2, psi2, ls, sf2, mx, vx, self.zu)

        dv_sum = np.sum(dv)
        dls *= ls
        dsf2 += dv_sum
        dsf = 2 * sf2 * dsf2

        # compute grads wrt theta1 and theta2 via log lik exp term
        dA = np.einsum('nd,nm->dm', dm_all, psi1)
        dB = np.einsum('nd,nab->dab', dv, psi2)
        dSu = np.einsum('ab,dbc,ce->dae', Kuuinv, dB, Kuuinv)
        dmu = 2 * np.einsum('dab,db->da', dSu, mu) \
            + np.einsum('ab,db->da', Kuuinv, dA)
        # add in contribution from the KL term
        dmu += np.einsum('ab,db->da', Kuuinv, mu)
        dSu += 0.5 * (Kuuinv - Suinv)
        deta1_R, deta2, dKuuinv_via_u = self.compute_posterior_grad_u(dmu, dSu)

        # grads wrt Kuu
        dKuuinv_KL = - 0.5 * self.Dout * Kuu + 0.5 * np.sum(Spmm, axis=0)
        dKuuinv_A = np.einsum('da,db->ab', dA, mu)
        KuuinvSmmd = np.einsum('ab,dbc->dac', Kuuinv, Spmm)
        dKuuinv_B = 2 * np.einsum('dab,dac->bc', KuuinvSmmd, dB) \
            - np.sum(dB, axis=0)
        dKuuinv = dKuuinv_A + dKuuinv_B + dKuuinv_via_u + dKuuinv_KL
        M_inner = - np.dot(Kuuinv, np.dot(dKuuinv, Kuuinv))
        dhyp = d_trace_MKzz_dhypers(
            2 * self.ls, 2 * self.sf, self.zu, M_inner,
            self.Kuu - np.diag(JITTER * np.ones(self.M)))

        dzu += dhyp[2]
        dls += 2 * dhyp[1]
        dsf += 2 * dhyp[0]

        grad_hyper = {
            'sf': dsf, 'ls': dls, 'zu': dzu,
            'eta1_R': deta1_R, 'eta2': deta2}
        grad_input = {'mx': dmx, 'vx': dvx}

        return grad_hyper, grad_input

    #TODO
    @profile
    def backprop_grads_lvm_mc(self, m, v, dm, dv, kfu, x):
        """Summary

        Args:
            m (TYPE): Description
            v (TYPE): Description
            dm (TYPE): Description
            dv (TYPE): Description
            kfu (TYPE): Description
            x (TYPE): Description

        Returns:
            TYPE: Description
        """
        N = self.N
        M = self.M
        ls = np.exp(self.ls)
        sf2 = np.exp(2 * self.sf)
        triu_ind = np.triu_indices(M)
        diag_ind = np.diag_indices(M)
        mu = self.mu
        Su = self.Su
        Suinv = self.Suinv
        Spmm = self.Splusmm
        Kuuinv = self.Kuuinv
        Kuu = self.Kuu
        kfuKuuinv = np.dot(kfu, Kuuinv)
        dm = dm.reshape(m.shape)
        dv = dv.reshape(v.shape)

        # compute grads wrt kfu
        dkfu_m = np.einsum('nd,dm->nm', dm, self.A)
        dkfu_v = 2 * np.einsum('nd,dab,na->nb', dv, self.B_det, kfu)
        dkfu = dkfu_m + dkfu_v
        dsf2, dls, dzu, dx = compute_kfu_derivatives(
            dkfu, kfu, ls, sf2, x, self.zu, grad_x=True)
        dv_sum = np.sum(dv)
        dls *= ls
        dsf2 += dv_sum
        dsf = 2 * sf2 * dsf2

        # compute grads wrt mean and covariance matrix via log lik exp term
        dmu = np.einsum('nd,nm->dm', dm, kfuKuuinv)
        dSu = np.einsum('na,nd,nb->dab', kfuKuuinv, dv, kfuKuuinv)
        # add in contribution from the KL term
        dmu += np.einsum('ab,db->da', Kuuinv, mu)
        dSu += 0.5 * (Kuuinv - Suinv)
        deta1_R, deta2, dKuuinv_via_u = self.compute_posterior_grad_u(dmu, dSu)

        # get contribution of Ahat and Bhat to Kuu and add to Minner
        dA = np.einsum('nd,nm->dm', dm, kfu)
        dKuuinv_m = np.einsum('da,db->ab', dA, mu)
        KuuinvS = np.einsum('ab,dbc->dac', Kuuinv, Su)
        dB = np.einsum('nd,na,nb->dab', dv, kfu, kfu)
        dKuuinv_v = 2 * np.einsum('dab,dac->bc', KuuinvS, dB) - np.sum(dB, axis=0)
        dKuuinv_KL = - 0.5 * self.Dout * Kuu + 0.5 * np.sum(Spmm, axis=0)
        dKuuinv = dKuuinv_m + dKuuinv_v + dKuuinv_via_u + dKuuinv_KL
        M_inner = - np.dot(Kuuinv, np.dot(dKuuinv, Kuuinv))
        dhyp = d_trace_MKzz_dhypers(
            2 * self.ls, 2 * self.sf, self.zu, M_inner,
            self.Kuu - np.diag(JITTER * np.ones(self.M)))

        dzu += dhyp[2]
        dls += 2 * dhyp[1]
        dsf += 2 * dhyp[0]

        grad_hyper = {
            'sf': dsf, 'ls': dls, 'zu': dzu,
            'eta1_R': deta1_R, 'eta2': deta2
        }

        return grad_hyper, dx

    @profile
    def backprop_grads_reg(self, m, v, dm, dv, kfu, x):
        """Summary

        Args:
            m (TYPE): Description
            v (TYPE): Description
            dm (TYPE): Description
            dv (TYPE): Description
            kfu (TYPE): Description
            x (TYPE): Description

        Returns:
            TYPE: Description
        """
        N = self.N
        M = self.M
        ls = np.exp(self.ls)
        sf2 = np.exp(2 * self.sf)
        triu_ind = np.triu_indices(M)
        diag_ind = np.diag_indices(M)
        mu = self.mu
        Su = self.Su
        Suinv = self.Suinv
        Spmm = self.Splusmm
        Kuuinv = self.Kuuinv
        Kuu = self.Kuu
        kfuKuuinv = np.dot(kfu, Kuuinv)

        # compute grads wrt kfu
        dkfu_m = np.einsum('nd,dm->nm', dm, self.A)
        dkfu_v = 2 * np.einsum('nd,dab,na->nb', dv, self.B_det, kfu)
        dkfu = dkfu_m + dkfu_v
        dsf2, dls, dzu = compute_kfu_derivatives(
            dkfu, kfu, ls, sf2, x, self.zu)
        dv_sum = np.sum(dv)
        dls *= ls
        dsf2 += dv_sum
        dsf = 2 * sf2 * dsf2

        # compute grads wrt mean and covariance matrix via log lik exp term
        dmu = np.einsum('nd,nm->dm', dm, kfuKuuinv)
        dSu = np.einsum('na,nd,nb->dab', kfuKuuinv, dv, kfuKuuinv)
        # add in contribution from the KL term
        dmu += np.einsum('ab,db->da', Kuuinv, mu)
        dSu += 0.5 * (Kuuinv - Suinv)
        deta1_R, deta2, dKuuinv_via_u = self.compute_posterior_grad_u(dmu, dSu)

        # get contribution of Ahat and Bhat to Kuu and add to Minner
        dA = np.einsum('nd,nm->dm', dm, kfu)
        dKuuinv_m = np.einsum('da,db->ab', dA, mu)
        KuuinvS = np.einsum('ab,dbc->dac', Kuuinv, Su)
        dB = np.einsum('nd,na,nb->dab', dv, kfu, kfu)
        dKuuinv_v = 2 * np.einsum('dab,dac->bc', KuuinvS, dB) - np.sum(dB, axis=0)
        dKuuinv_KL = - 0.5 * self.Dout * Kuu + 0.5 * np.sum(Spmm, axis=0)
        dKuuinv = dKuuinv_m + dKuuinv_v + dKuuinv_via_u + dKuuinv_KL
        M_inner = - np.dot(Kuuinv, np.dot(dKuuinv, Kuuinv))
        dhyp = d_trace_MKzz_dhypers(
            2 * self.ls, 2 * self.sf, self.zu, M_inner,
            self.Kuu - np.diag(JITTER * np.ones(self.M)))

        dzu += dhyp[2]
        dls += 2 * dhyp[1]
        dsf += 2 * dhyp[0]

        grad_hyper = {
            'sf': dsf, 'ls': dls, 'zu': dzu,
            'eta1_R': deta1_R, 'eta2': deta2
        }

        return grad_hyper

class SGPR(Base_SGPR):
    """Uncollapsed sparse Gaussian process approximations

    Attributes:
        sgp_layer (TYPE): Description
        updated (bool): Description
    """

    def __init__(self, x_train, y_train, no_pseudo, lik='Gaussian', nat_param=True):
        """Summary

        Args:
            x_train (TYPE): Description
            y_train (TYPE): Description
            no_pseudo (TYPE): Description
            lik (str, optional): Description
        """
        super(SGPR, self).__init__(x_train, y_train, no_pseudo, lik, nat_param)
        self.sgp_layer = SGP_Layer(self.N, self.Din, self.Dout, self.M, nat_param)

    @profile
    def objective_function(self, params, mb_size, alpha='not_used',
        prop_mode='not_used'):
        """Summary

        Args:
            params (TYPE): Description
            mb_size (TYPE): Description
            alpha (str, optional): Description
            prop_mode (TYPE, optional): Description

        Returns:
            TYPE: Description
        """
        N = self.N
        if mb_size >= N:
            xb = self.x_train
            yb = self.y_train
        else:
            idxs = np.random.choice(N, mb_size, replace=False)
            xb = self.x_train[idxs, :]
            yb = self.y_train[idxs, :]
        batch_size = yb.shape[0]
        scale_loglik = - N * 1.0 / batch_size
        # scale_loglik = 0

        # update model with new hypers
        self.update_hypers(params)

        # propagate x forward
        mout, vout, kfu = self.sgp_layer.forward_prop_thru_post(xb, return_info=True)
        # compute logZ and gradients
        logZ, dm, dv = self.lik_layer.compute_log_lik_exp(mout, vout, yb)
        logZ_scale = scale_loglik * logZ
        dm_scale = scale_loglik * dm
        dv_scale = scale_loglik * dv
        sgp_grad_hyper = self.sgp_layer.backprop_grads_reg(
            mout, vout, dm_scale, dv_scale, kfu, xb)
        lik_grad_hyper = self.lik_layer.backprop_grads_log_lik_exp(
            mout, vout, dm, dv, yb, scale_loglik)

        grad_all = {}
        for key in sgp_grad_hyper.keys():
            grad_all[key] = sgp_grad_hyper[key]

        for key in lik_grad_hyper.keys():
            grad_all[key] = lik_grad_hyper[key]

        # compute objective
        sgp_KL_term = self.sgp_layer.compute_KL()
        energy = logZ_scale + sgp_KL_term
        # energy = logZ_scale
        # energy = sgp_KL_term

        for p in self.fixed_params:
            grad_all[p] = np.zeros_like(grad_all[p])

        energy /= N
        for key in grad_all.keys():
            grad_all[key] /= N

        return energy, grad_all

    def predict_f(self, inputs):
        """Summary

        Args:
            inputs (TYPE): Description

        Returns:
            TYPE: Description
        """
        if not self.updated:
            self.sgp_layer.update_posterior()
            self.updated = True
        mf, vf = self.sgp_layer.forward_prop_thru_post(inputs)
        return mf, vf

    def sample_f(self, inputs, no_samples=1):
        """Summary

        Args:
            inputs (TYPE): Description
            no_samples (int, optional): Description

        Returns:
            TYPE: Description
        """
        if not self.updated:
            self.sgp_layer.update_posterior()
            self.updated = True
        K = no_samples
        fs = np.zeros((inputs.shape[0], self.Dout, K))
        # TODO: remove for loop here
        for k in range(K):
            fs[:, :, k] = self.sgp_layer.sample(inputs)
        return fs

    def predict_y(self, inputs):
        """Summary

        Args:
            inputs (TYPE): Description

        Returns:
            TYPE: Description
        """
        if not self.updated:
            self.sgp_layer.update_posterior()
            self.updated = True
        mf, vf = self.sgp_layer.forward_prop_thru_post(inputs)
        my, vy = self.lik_layer.output_probabilistic(mf, vf)
        return my, vy

    def init_hypers(self, y_train):
        """Summary

        Args:
            y_train (TYPE): Description

        Returns:
            TYPE: Description
        """
        sgp_params = self.sgp_layer.init_hypers(self.x_train)
        lik_params = self.lik_layer.init_hypers()
        init_params = dict(sgp_params)
        init_params.update(lik_params)
        return init_params

    def get_hypers(self):
        """Summary

        Returns:
            TYPE: Description
        """
        sgp_params = self.sgp_layer.get_hypers()
        lik_params = self.lik_layer.get_hypers()
        params = dict(sgp_params)
        params.update(lik_params)
        return params

    def update_hypers(self, params):
        """Summary

        Args:
            params (TYPE): Description
        """
        self.sgp_layer.update_hypers(params)
        self.lik_layer.update_hypers(params)

class SGPLVM(Base_SGPLVM):
    """Summary

    Attributes:
        sgp_layer (TYPE): Description

    """

    def __init__(self, y_train, hidden_size, no_pseudo,
                 lik='Gaussian', prior_mean=0, prior_var=1, nat_param=True):
        """Summary

        Args:
            y_train (TYPE): Description
            hidden_size (TYPE): Description
            no_pseudo (TYPE): Description
            lik (str, optional): Description
            prior_mean (int, optional): Description
            prior_var (int, optional): Description
        """
        super(SGPLVM, self).__init__(
            y_train, hidden_size, no_pseudo, lik,
            prior_mean, prior_var, nat_param)
        self.sgp_layer = SGP_Layer(self.N, self.Din, self.Dout, self.M, nat_param)

    @profile
    def objective_function(self, params, mb_size, alpha='not_used', prop_mode=PROP_MM):
        """Summary

        Args:
            params (TYPE): Description
            mb_size (TYPE): Description
            alpha (float, optional): Description
            prop_mode (TYPE, optional): Description

        Returns:
            TYPE: Description

        Raises:
            NotImplementedError: Description
        """
        N = self.N
        sgp_layer = self.sgp_layer
        lik_layer = self.lik_layer
        if mb_size == N:
            idxs = np.arange(N)
            yb = self.y_train
        else:
            idxs = np.random.choice(N, mb_size, replace=False)
            yb = self.y_train[idxs, :]
        batch_size = yb.shape[0]
        scale_log_lik = - N * 1.0 / batch_size
        # scale_log_lik = 0

        # update model with new hypers
        self.update_hypers(params)
        # compute posterior for x
        mx, vx = self.get_posterior_x(idxs)
        if prop_mode == PROP_MM:
            # propagate x forward
            mout, vout, psi1, psi2 = sgp_layer.forward_prop_thru_post(mx, vx, return_info=True)
            # compute logZ and gradients
            logZ, dm, dv = lik_layer.compute_log_lik_exp(mout, vout, yb)
            logZ_scale = scale_log_lik * logZ
            dm_scale = scale_log_lik * dm
            dv_scale = scale_log_lik * dv
            sgp_grad_hyper, sgp_grad_input = sgp_layer.backprop_grads_lvm_mm(
                mout, vout, dm_scale, dv_scale, psi1, psi2, mx, vx)
            lik_grad_hyper = lik_layer.backprop_grads_log_lik_exp(
                mout, vout, dm, dv, yb, scale_log_lik)
        elif prop_mode == PROP_MC:
            # propagate x forward
            res, res_s = sgp_layer.forward_prop_thru_post(mx, vx, PROP_MC, return_info=True)
            m, v, kfu, x, eps = res[0], res[1], res[2], res[3], res[4]
            m_s, v_s, kfu_s, x_s, eps_s = (
                res_s[0], res_s[1], res_s[2], res_s[3], res_s[4])
            # compute logZ and gradients
            logZ, dm, dv = lik_layer.compute_log_lik_exp(m, v, yb)
            logZ_scale = scale_log_lik * logZ
            dm_scale = scale_log_lik * dm
            dv_scale = scale_log_lik * dv
            sgp_grad_hyper, dx = sgp_layer.backprop_grads_lvm_mc(
                m_s, v_s, dm_scale, dv_scale, kfu_s, x_s)
            sgp_grad_input = sgp_layer.backprop_grads_reparam(
                dx, mx, vx, eps)
            lik_grad_hyper = lik_layer.backprop_grads_log_lik_exp(
                m, v, dm, dv, yb, scale_log_lik)
        else:
            raise NotImplementedError('propagation mode not implemented')

        grad_all = {}
        for key in sgp_grad_hyper.keys():
            grad_all[key] = sgp_grad_hyper[key]

        for key in lik_grad_hyper.keys():
            grad_all[key] = lik_grad_hyper[key]

        # compute grad wrt x params
        dmx = sgp_grad_input['mx']
        dvx = sgp_grad_input['vx']

        x_KL_term, dkl_dmx, dkl_dvx = self.compute_KL_x(
            mx, vx, self.prior_mean, self.prior_var)
        scale_x = N * 1.0 / batch_size
        dmx += scale_x * dkl_dmx
        dvx += scale_x * dkl_dvx
        grads_x = self.compute_posterior_grad_x(dmx, dvx, idxs)
        for key in grads_x.keys():
            grad_all[key] = grads_x[key]

        # compute objective
        sgp_KL_term = self.sgp_layer.compute_KL()
        x_KL_term = scale_x * x_KL_term
        energy = logZ_scale + x_KL_term + sgp_KL_term
        # energy = logZ_scale + x_KL_term
        for p in self.fixed_params:
            grad_all[p] = np.zeros_like(grad_all[p])

        energy /= N
        for key in grad_all.keys():
            grad_all[key] /= N

        return energy, grad_all

    def compute_KL_x(self, mx, vx, m0, v0):
        """Summary

        Args:
            mx (TYPE): Description
            vx (TYPE): Description
            m0 (TYPE): Description
            v0 (TYPE): Description

        Returns:
            TYPE: Description
        """
        kl = 0.5 * (np.log(v0) - np.log(vx) + (vx + (mx - m0)**2) / v0 - 1)
        kl_sum = np.sum(kl)
        dkl_dmx = (mx - m0) / v0
        dkl_dvx = - 0.5 / vx + 0.5 / v0
        return kl_sum, dkl_dmx, dkl_dvx

class SGPSSM(Base_SGPSSM):
    """Summary

    Attributes:
        dyn_layer (TYPE): Description
        emi_layer (TYPE): Description
    """

    def __init__(self, y_train, hidden_size, no_pseudo,
                 lik='Gaussian', prior_mean=0, prior_var=1, x_control=None,
                 gp_emi=False, control_to_emi=True, nat_param=True):
        """Summary

        Args:
            y_train (TYPE): Description
            hidden_size (TYPE): Description
            no_pseudo (TYPE): Description
            lik (str, optional): Description
            prior_mean (int, optional): Description
            prior_var (int, optional): Description
            x_control (None, optional): Description
            gp_emi (bool, optional): Description
            control_to_emi (bool, optional): Description
        """
        super(SGPSSM, self).__init__(
            y_train, hidden_size, no_pseudo, lik, prior_mean, prior_var,
            x_control, gp_emi, control_to_emi, nat_param)
        self.dyn_layer = SGP_Layer(
            self.N - 1, self.Din + self.Dcon_dyn, self.Din, self.M, nat_param)
        if gp_emi:
            self.emi_layer = SGP_Layer(
                self.N, self.Din + self.Dcon_emi, self.Dout, self.M, nat_param)

    @profile
    def objective_function(self, params, mb_size, alpha='not_used', prop_mode=PROP_MM):
        """Summary

        Args:
            params (TYPE): Description
            mb_size (TYPE): Description
            alpha (float, optional): Description
            prop_mode (TYPE, optional): Description

        Returns:
            TYPE: Description

        Raises:
            NotImplementedError: Description
        """
        N = self.N
        dyn_layer = self.dyn_layer
        emi_layer = self.emi_layer
        if self.gp_emi:
            lik_layer = self.lik_layer
        if mb_size >= N:
            dyn_idxs = np.arange(0, N - 1)
            emi_idxs = np.arange(0, N)
            yb = self.y_train
        else:
            start_idx = np.random.randint(0, N - mb_size)
            end_idx = start_idx + mb_size
            emi_idxs = np.arange(start_idx, end_idx)
            dyn_idxs = np.arange(start_idx, end_idx - 1)
            yb = self.y_train[emi_idxs, :]
        batch_size_dyn = dyn_idxs.shape[0]
        scale_log_lik_dyn = - (N - 1) * 1.0 / batch_size_dyn
        # scale_log_lik_dyn = 0
        batch_size_emi = emi_idxs.shape[0]
        scale_log_lik_emi = - N * 1.0 / batch_size_emi
        # scale_log_lik_emi = 0

        # update model with new hypers
        self.update_hypers(params)
        # compute posterior
        post_m, post_v = self.get_posterior_x(emi_idxs)
        idxs_prev = dyn_idxs + 1
        post_t_m, post_t_v = post_m[1:, :], post_v[1:, :]
        idxs_next = dyn_idxs
        post_tm1_m, post_tm1_v = post_m[0:-1, :], post_v[0:-1, :]
        if self.Dcon_dyn > 0:
            post_tm1_mc = np.hstack((post_tm1_m, self.x_control[idxs_next, :]))
            post_tm1_vc = np.hstack(
                (post_tm1_v, np.zeros((batch_size_dyn, self.Dcon_dyn))))
        else:
            post_tm1_mc, post_tm1_vc = post_tm1_m, post_tm1_v

        post_up_m, post_up_v = post_m, post_v
        if self.Dcon_emi > 0:
            post_up_mc = np.hstack((post_up_m, self.x_control[emi_idxs, :]))
            post_up_vc = np.hstack(
                (post_up_v, np.zeros((batch_size_emi, self.Dcon_emi))))
        else:
            post_up_mc, post_up_vc = post_up_m, post_up_v

        if prop_mode == PROP_MM:
            # deal with the transition/dynamic factors here
            mprop, vprop, psi1, psi2 = dyn_layer.forward_prop_thru_post(
                post_tm1_mc, post_tm1_vc, return_info=True)
            logZ_dyn, dmprop, dvprop, dmt, dvt, dsn = self.compute_transition_log_lik_exp(
                mprop, vprop, post_t_m, post_t_v, scale_log_lik_dyn)
            sgp_grad_hyper, sgp_grad_input = dyn_layer.backprop_grads_lvm_mm(
                mprop, vprop, dmprop, dvprop,
                psi1, psi2, post_tm1_mc, post_tm1_vc)
            if self.gp_emi:
                # deal with the emission factors here
                mout, vout, psi1, psi2 = emi_layer.forward_prop_thru_post(
                    post_up_mc, post_up_vc, return_info=True)
                logZ_emi, dm, dv = lik_layer.compute_log_lik_exp(
                    mout, vout, yb)
                logZ_emi = scale_log_lik_emi * logZ_emi
                dm_scale = scale_log_lik_emi * dm
                dv_scale = scale_log_lik_emi * dv
                emi_grad_hyper, emi_grad_input = emi_layer.backprop_grads_lvm_mm(
                    mout, vout, dm_scale, dv_scale,
                    psi1, psi2, post_up_mc, post_up_vc)
                lik_grad_hyper = lik_layer.backprop_grads_log_lik_exp(
                    mout, vout, dm, dv, yb, scale_log_lik_emi)
        elif prop_mode == PROP_MC:
            # TODO
            # deal with the transition/dynamic factors here
            res, res_s = dyn_layer.forward_prop_thru_post(
                post_tm1_mc, post_tm1_vc, PROP_MC, return_info=True)
            m, v, kfu, x, eps = res[0], res[1], res[2], res[3], res[4]
            m_s, v_s, kfu_s, x_s, eps_s = (
                res_s[0], res_s[1], res_s[2], res_s[3], res_s[4])
            logZ_dyn, dmprop, dvprop, dmt, dvt, dsn = self.compute_transition_log_lik_exp(
                m, v, post_t_m, post_t_v, scale_log_lik_dyn)
            sgp_grad_hyper, dx = dyn_layer.backprop_grads_lvm_mc(
                m_s, v_s, dmprop, dvprop, kfu_s, x_s)
            sgp_grad_input = dyn_layer.backprop_grads_reparam(
                dx, post_tm1_mc, post_tm1_vc, eps)
            if self.gp_emi:
                # deal with the emission factors here
                res, res_s = emi_layer.forward_prop_thru_post(
                    post_up_mc, post_up_vc, PROP_MC, return_info=True)
                m, v, kfu, x, eps = res[0], res[1], res[2], res[3], res[4]
                m_s, v_s, kfu_s, x_s, eps_s = (
                    res_s[0], res_s[1], res_s[2], res_s[3], res_s[4])
                # compute logZ and gradients
                logZ_emi, dm, dv = lik_layer.compute_log_lik_exp(m, v, yb)
                logZ_emi = scale_log_lik_emi * logZ_emi
                dm_scale = scale_log_lik_emi * dm
                dv_scale = scale_log_lik_emi * dv
                emi_grad_hyper, dx = emi_layer.backprop_grads_lvm_mc(
                    m_s, v_s, dm_scale, dv_scale, kfu_s, x_s)
                emi_grad_input = emi_layer.backprop_grads_reparam(
                    dx, post_up_mc, post_up_vc, eps)
                lik_grad_hyper = lik_layer.backprop_grads_log_lik_exp(
                    m, v, dm, dv, yb, scale_log_lik_emi)
        else:
            raise NotImplementedError('propgation mode not implemented')

        if not self.gp_emi:
            logZ_emi, emi_grad_input, emi_grad_hyper = emi_layer.compute_emission_log_lik_exp(
                post_up_mc, post_up_vc, scale_log_lik_emi, emi_idxs)

        # collect emission and GP hyperparameters
        grad_all = {'sn': dsn}
        for key in sgp_grad_hyper.keys():
            grad_all[key + '_dynamic'] = sgp_grad_hyper[key]
        for key in emi_grad_hyper.keys():
            grad_all[key + '_emission'] = emi_grad_hyper[key]
        if self.gp_emi:
            for key in lik_grad_hyper.keys():
                grad_all[key + '_emission'] = lik_grad_hyper[key]

        dm_up = emi_grad_input['mx'][:, :self.Din]
        dv_up = emi_grad_input['vx'][:, :self.Din]
        dm_next, dv_next = dmt, dvt
        dm_prev = sgp_grad_input['mx'][:, :self.Din]
        dv_prev = sgp_grad_input['vx'][:, :self.Din]

        # compute entropy
        scale_entropy = - N * 1.0 / batch_size_emi
        # scale_entropy = 0
        x_entrop = batch_size_emi * self.Din * (0.5 + 0.5 * np.log(2*np.pi))
        x_entrop += np.sum(0.5 * np.log(post_v))
        x_entrop *= scale_entropy
        dv_entrop = scale_entropy * 0.5 / post_v

        # TODO ignore x prior term for now
        prior_contrib = 0
        dm0 = 0
        dv0 = 0

        # aggregate gradients for x param
        dm = dm_up
        dm[1:, :] += dm_next
        dm[0:-1, :] += dm_prev
        dv = dv_up + dv_entrop
        dv[1:, :] += dv_next
        dv[0:-1, :] += dv_prev
        grads_x = self.compute_posterior_grad_x(dm, dv, emi_idxs)
        for key in grads_x.keys():
            grad_all[key] = grads_x[key]

        # compute objective
        dyn_KL = dyn_layer.compute_KL()
        if self.gp_emi:
            emi_KL = emi_layer.compute_KL()
        else:
            emi_KL = 0
        energy = logZ_dyn + logZ_emi + x_entrop + dyn_KL + emi_KL
        # print logZ_dyn, logZ_emi, x_entrop, dyn_KL, emi_KL
        for p in self.fixed_params:
            grad_all[p] = np.zeros_like(grad_all[p])

        energy /= N
        for key in grad_all.keys():
            grad_all[key] /= N

        return energy, grad_all

    def compute_transition_log_lik_exp(self, m_prop, v_prop, m_t, v_t, scale):
        """Summary

        Args:
            m_prop (TYPE): Description
            v_prop (TYPE): Description
            m_t (TYPE): Description
            v_t (TYPE): Description
            scale (TYPE): Description

        Returns:
            TYPE: Description

        Raises:
            RuntimeError: Description
        """
        sn2 = np.exp(2 * self.sn)
        if m_prop.ndim == 2:
            term_1 = - 0.5 * np.log(2 * np.pi * sn2)
            term_2 = - 0.5 / sn2 * (m_t**2 + v_t - 2 * m_t * m_prop + m_prop**2 + v_prop)
            logZ = scale * np.sum(term_1 + term_2)
            dmt = - scale / sn2 * (m_t - m_prop)
            dmprop = - dmt
            dvt = - scale * 0.5 / sn2 * np.ones_like(v_t)
            dvprop = dvt
            dsn = scale * np.sum(-1 - 2*term_2)
        elif m_prop.ndim == 3:
            K = m_prop.shape[0]
            term_1 = - 0.5 * np.log(2 * np.pi * sn2)
            term_2 = - 0.5 / sn2 * (m_t**2 + v_t - 2 * m_t * m_prop + m_prop**2 + v_prop)
            logZ = scale * np.sum(term_1 + term_2) / K
            dmprop = scale / sn2 * (m_t - m_prop) / K
            dmt = - np.sum(dmprop, axis=0)
            dvprop = - scale * 0.5 / sn2 * np.ones_like(v_prop) / K
            dvt = np.sum(dvprop, axis=0)
            dsn = scale * np.sum(-1 - 2*term_2) / K
        else:
            raise RuntimeError('invalid ndim, ndim=%d' % mout.ndim)

        return logZ, dmprop, dvprop, dmt, dvt, dsn

max_stars_repo_name: MattAshman/geepee

max_issues_repo_name: yohanJung/geepee

max_forks_repo_name: yohanJung/geepee


text:
import numpy as np
import tensorflow as tf
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC

__author__ = "Marcin Stachowiak"
__version__ = "1.0"
__email__ = "marcin.stachowiak.ms@gmail.com"

class EnsembleVotingModel:
    """
    Class represents model for ensemble voting algorithm.
    """

    def __init__(self, X, Y):
        self._X = X
        self._Y = Y
        self._models = []

    def with_SVM_model(self):
        estimatior = SVC(kernel='rbf')
        self._models.append(('svm', estimatior))
        return self

    def with_RandomForest_model(self):
        estimatior = RandomForestClassifier(random_state=1)
        self._models.append(('rf', estimatior))
        return self

    def with_LogisticRegression(self):
        estimatior = LogisticRegression(random_state=1)
        self._models.append(('lr', estimatior))
        return self

    def with_SVM_AdaBoost_model(self, n_estimators=2):
        estimatior = AdaBoostClassifier(SVC(kernel='rbf', probability=True), n_estimators=n_estimators)
        self._models.append(('svm_ada', estimatior))
        return self

    def with_RandomForest_AdaBoost_model(self, n_estimators=2):
        estimatior = AdaBoostClassifier(RandomForestClassifier(random_state=1), n_estimators=n_estimators)
        self._models.append(('rf_ada', estimatior))
        return self

    def with_LogisticRegression_AdaBoost_model(self, n_estimators=2):
        estimatior = AdaBoostClassifier(LogisticRegression(random_state=1), n_estimators=n_estimators)
        self._models.append(('lr_ada', estimatior))
        return self

    def train(self):
        print('Building ensemble model')
        self.voting_model = VotingClassifier(estimators=self._models, voting='hard')
        print('Training ensemble model')
        self.voting_model.fit(self._X, self._Y)
        return self

    def predict(self, X):
        return self.voting_model.predict(X)

    def score(self, X, Y):
        return self.voting_model.score(X, Y)

class NeuralNetworkModel:
    """
       Class represents model for multilayer perceptron.
       """

    def __init__(self, X, Y, learning_rate=1e-4, num_steps=10000, batch_size=64, display_step=100, n_hidden_1=1024):
        self._X = X
        self._Y = Y
        self._learning_rate = learning_rate
        self._num_steps = num_steps
        self._batch_size = batch_size
        self._display_step = display_step
        self._n_hidden_1 = n_hidden_1
        self._num_input = X.shape[1]
        self._num_classes = Y.shape[1]

    def predict(self, X):
        return self._session.run(self._prediction, feed_dict={self._X_p: X})

    def score(self, X, Y):
        return self._session.run(self._accuracy, feed_dict={self._X_p: X, self._Y_p: Y})

    def _random_batch_data(self, data_x, data_y):
        num_images = len(data_x)
        idx = np.random.choice(num_images, size=self._batch_size, replace=False)
        x_batch = data_x[idx]
        y_batch = data_y[idx]

        return x_batch, y_batch

    def _build_graph(self):
        self._X_p = tf.placeholder("float", [None, self._num_input])
        self._Y_p = tf.placeholder("float", [None, self._num_classes])

        # Construct model
        logits = self._build_net_model(self._X_p)

        # Define loss and optimizer
        self._loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=self._Y_p))
        optimizer = tf.train.AdamOptimizer(learning_rate=self._learning_rate)
        self._train_op = optimizer.minimize(self._loss_op)

        # Evaluate model (with test logits, for dropout to be disabled)
        self._prediction = tf.argmax(logits, 1)
        correct_pred = tf.equal(self._prediction, tf.argmax(self._Y_p, 1))
        self._accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    def _build_net_model(self, x):
        weights = {
            'h1': tf.Variable(tf.random_normal([self._num_input, self._n_hidden_1])),
            'out': tf.Variable(tf.random_normal([self._n_hidden_1, self._num_classes]))
        }
        biases = {
            'b1': tf.Variable(tf.random_normal([self._n_hidden_1])),
            'out': tf.Variable(tf.random_normal([self._num_classes]))
        }

        # Hidden fully connected layer with 256 neurons
        layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
        # Output fully connected layer with a neuron for each class
        out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
        return out_layer

    def train(self):
        self._build_graph()
        # Initialize the variables (i.e. assign their default value)
        init = tf.global_variables_initializer()

        self._session = tf.Session()
        self._session.run(init)

        for step in range(1, self._num_steps + 1):
            batch_x, batch_y = self._random_batch_data(self._X, self._Y)
            # Run optimization op (backprop)
            self._session.run(self._train_op, feed_dict={self._X_p: batch_x, self._Y_p: batch_y})
            if step % self._display_step == 0 or step == 1:
                # Calculate batch loss and accuracy
                loss, acc = self._session.run([self._loss_op, self._accuracy],
                                              feed_dict={self._X_p: batch_x, self._Y_p: batch_y})
                print("Step " + str(step) + ", Minibatch Loss= " + \
                      "{:.4f}".format(loss) + ", Training Accuracy= " + \
                      "{:.3f}".format(acc))
        print("Optimization Finished!")

        return self

ext: py

reason: import numpy


text:
import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
import math
import time
import os
import cv2 as cv

try:
    sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
except:
    pass

hist_size = [255]
hist_range = [0,256]
dataset = []
path = r'C:\Users\rajsh\Desktop\GMM\Training Set\Yellow2'

vid = cv.VideoCapture("detectbuoy.avi")
for i in os.listdir(path):
    dataset.append(i)

green_data = 29
orange_data = 118
yellow = 138

def GaussianEquation(sigma, x, mean):
    equation = (1/(sigma*math.sqrt(2*math.pi)))*np.exp(-0.5*(x-mean)**2/sigma**2)
    return equation

def AverageHistogram():
    mean_b = []
    mean_g = []
    mean_r = []

    std_dev_b = []
    std_dev_g = []
    std_dev_r = []
    histogram_r = np.zeros((255,1))
    histogram_g = np.zeros((255,1))
    histogram_b = np.zeros((255,1))

    # Iterate for no of green buoy images
    for i in range(0,green_data):
        string_path = path+"\yellow"+str(i)+".jpg"
        img = cv.imread(string_path)
        color = ("b","g","r")

        # New Mean Calculation
        mask= np.zeros((img.shape[0],img.shape[0],3), np.uint8)
        coordinates = np.indices((img.shape[0], img.shape[0]))
        coordinates = coordinates.reshape(2, -1)
        x , y=coordinates[0] , coordinates[1]
        indices=np.where(img[x,y]!=[0,0,0])
        xnew,ynew=x[indices[0]],y[indices[0]]
        mask[xnew,ynew]=img[xnew,ynew]
        #cv.imshow("e",mask)
        pixels=img[xnew,ynew]
        mean=np.sum(pixels, axis=0) / len(pixels)
        stds=[np.std(pixels[:,0]),np.std(pixels[:,1]),np.std(pixels[:,2])]

        for j,c in enumerate(color):
            if c == "b":
                temp_b = cv.calcHist([img],[j],None,hist_size,hist_range, accumulate = 1)
                histogram_b = np.column_stack((histogram_b, temp_b))
                mean_b.append(mean[0])
                std_dev_b.append(stds[0])
            if c == "g":
                temp_g = cv.calcHist([img],[j],None,hist_size,hist_range, accumulate = 1)
                histogram_g = np.column_stack((histogram_g, temp_g))
                mean_g.append(mean[1])
                std_dev_g.append(stds[1])
            if c == "r":
                temp_r = cv.calcHist([img],[j],None,hist_size,hist_range, accumulate = 1)
                histogram_r = np.column_stack((histogram_r, temp_r))
                mean_r.append(mean[2])
                std_dev_r.append(stds[2])

    histogram_avg_b = np.sum(histogram_b, axis=1) / (green_data)
    histogram_avg_g = np.sum(histogram_g, axis=1) / (green_data)
    histogram_avg_r = np.sum(histogram_r, axis=1) / (green_data)

    #Uncomment to plot histograms
    #plt.subplot(3,1,1)
    #plt.plot(histogram_avg_b, color = "b")
    #plt.subplot(3,1,2)
    #plt.plot(histogram_avg_g, color = "g")
    #plt.subplot(3,1,3)
    #plt.plot(histogram_avg_r, color = "r")
    #plt.show()

    return mean_r, mean_g, mean_b, std_dev_r, std_dev_g, std_dev_b

def EM():
    K = 5
    datapoint_b = []
    datapoint_g = []
    datapoint_r = []
    a=[]

    for i in range(0, green_data):
        string_path = path+"\yellow"+str(i)+".jpg"
        img = cv.imread(string_path)
        blue_chan_img = img[:,:,0]
        green_chan_img = img[:,:,1]
        red_chan_img = img[:,:,2]
        for i in range(0, img.shape[0]):
            for j in range(0, img.shape[0]):
                a=img[i,j]
                if a[0]!=0 and a[1]!=0 and a[2]!=0:
                    datapoint_b.append(blue_chan_img[i,j])
                    datapoint_g.append(green_chan_img[i,j])
                    datapoint_r.append(red_chan_img[i,j])

    #intital estimates
    mean_b_init = 115
    mean_g_init = 240
    mean_r_init = 210
    std_dev_b_init = 30
    std_dev_g_init = 30
    std_dev_r_init = 30

    iterations = 0

    while(iterations <=10):
        responsibility_1 = []
        responsibility_2 = []
        responsibility_3 = []

        probability_dist_1 = []
        probability_dist_2 = []
        probability_dist_3 = []

        pi_k_1 = []
        pi_k_2 = []
        pi_k_3 = []

        #perform e and m for each datapoint
        for i in range(len(datapoint_b)):
            #calculate probabilty at that pixel
            probability_1 = GaussianEquation(std_dev_b_init, datapoint_b[i], mean_b_init)
            probability_2 = GaussianEquation(std_dev_g_init, datapoint_g[i], mean_g_init)
            probability_3 = GaussianEquation(std_dev_r_init, datapoint_r[i], mean_r_init)

            #gaussian of 3 channels
            probability_dist_1.append(probability_1)
            probability_dist_2.append(probability_2)
            probability_dist_3.append(probability_3)

            temp_pi_1 = probability_1/(probability_1 + probability_2 + probability_3)
            temp_pi_2 = probability_2/(probability_1 + probability_2 + probability_3)
            temp_pi_3 = probability_3/(probability_1 + probability_2 + probability_3)

            pi_k_1.append(temp_pi_1)
            pi_k_2.append(temp_pi_2)
            pi_k_3.append(temp_pi_3)

        #formula for calculating new mean from pdf
        mean_b_init = np.sum(np.array(pi_k_1)*np.array(datapoint_b))/np.sum(np.array(pi_k_1))
        mean_g_init = np.sum(np.array(pi_k_2)*np.array(datapoint_g))/np.sum(np.array(pi_k_2))
        mean_r_init = np.sum(np.array(pi_k_3)*np.array(datapoint_r))/np.sum(np.array(pi_k_3))

        #calculating SD from mean and data points
        std_dev_b_init = (np.sum(np.array(pi_k_1) * ((np.array(datapoint_b))
        - mean_b_init) ** (2)) / np.sum(np.array(pi_k_1))) ** (1 / 2)
        std_dev_g_init = (np.sum(np.array(pi_k_2) * ((np.array(datapoint_g))
        - mean_g_init) ** (2)) / np.sum(np.array(pi_k_2))) ** (1 / 2)
        std_dev_r_init = (np.sum(np.array(pi_k_1) * ((np.array(datapoint_r))
        - mean_r_init) ** (2)) / np.sum(np.array(pi_k_3))) ** (1 / 2)

        iterations = iterations + 1
        print(iterations)
    return mean_b_init, mean_g_init, mean_r_init, std_dev_b_init, std_dev_g_init, std_dev_r_init

if __name__ == "__main__":
    mean_r, mean_g, mean_b, std_dev_r, std_dev_g, std_dev_b = AverageHistogram()
    avg_mean_b = sum(mean_b)/len(mean_b)
    avg_mean_g = sum(mean_g)/len(mean_g)
    avg_mean_r = sum(mean_r)/len(mean_r)

    avg_std_dev_b = sum(std_dev_b)/len(std_dev_b)
    avg_std_dev_g = sum(std_dev_g)/len(std_dev_g)
    avg_std_dev_r = sum(std_dev_r)/len(std_dev_r)

    gaussian_b = GaussianEquation(avg_std_dev_b, list(range(0,256)), avg_mean_b)
    gaussian_g = GaussianEquation(avg_std_dev_g, list(range(0,256)), avg_mean_g)
    gaussian_r = GaussianEquation(avg_std_dev_r, list(range(0,256)), avg_mean_r)

    mean_b_init, mean_g_init, mean_r_init, std_dev_b_init, std_dev_g_init, std_dev_r_init = EM()

    greenboi_r = GaussianEquation(std_dev_r_init, list(range(0,256)), mean_r_init)
    greenboi_g = GaussianEquation(std_dev_g_init, list(range(0,256)), mean_g_init)
    greenboi_b = GaussianEquation(std_dev_b_init, list(range(0,256)), mean_b_init)
    plt.plot(greenboi_r, "r", greenboi_g, "g", greenboi_b, "b")
    plt.show()

    while True:
      ret,frame = vid.read()
      if frame is not None:
        frame_b=frame[:,:,0]
        frame_g=frame[:,:,1]
        frame_r=frame[:,:,2]
        if ret == True:
            frame_updated=np.zeros(frame_g.shape, dtype = np.uint8)
            coordinates = np.indices((frame_g.shape[0], frame_g.shape[1]))
            coordinates = coordinates.reshape(2, -1)
            x,y=coordinates[0],coordinates[1]
            pixel_valr=frame_r[x,y]
            pixel_valg=frame_g[x,y]
            pixel_valb=frame_b[x,y]
            indices1=np.where((greenboi_g[pixel_valg]>0.13) & (greenboi_b[pixel_valb]>0.005) & (greenboi_r[pixel_valr]>0.002))
            x1,y1=x[indices1[0]],y[indices1[0]]
            frame_updated[x1,y1]=255

            kernel_square = np.ones((10,10),np.uint8)
            kernel_ellipse = cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
            np.array([[0, 0, 1, 0, 0],
                   [1, 1, 1, 1, 1],
                   [1, 1, 1, 1, 1],
                   [1, 1, 1, 1, 1],
                   [0, 0, 1, 0, 0]], dtype=np.uint8)
            blur = cv.blur(frame_updated,(10,10))
            ret_thresh, thresholded = cv.threshold(blur, 50, 255, cv.THRESH_BINARY)
            edges = cv.Canny(thresholded, 200, 300)
            dilated = cv.dilate(thresholded, kernel_ellipse, iterations = 1)
            _,contours, _ = cv.findContours(dilated, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
            #cont_img = cv.drawContours(frame, contours, -1, (0,0,255), 5)

            # Draw circle to fit the contours enclosing specified area
            for c in contours:
                if cv.contourArea(c) > 40:
                    print("inside1")
                    (x,y),r = cv.minEnclosingCircle(c)
                    center = (int(x),int(y))
                    r = int(r)
                    print(r)
                    if r > 9 and r < 37 and y<400 and y>150:
                        print("inside")
                        cv.circle(frame,center,r,(0,255,255),2)
            cv.imshow("threshold", frame)
            k = cv.waitKey(15) & 0xff
            if k == 27:
                break

        else:
            break

    vid.release()
    cv.destroyAllWindows()


text:
import torch.nn as nn
import json
import numpy as np
from up.utils.general.registry_factory import MODULE_ZOO_REGISTRY
from up.utils.model.initializer import init_bias_focal, initialize_from_cfg

__all__ = ['AnchorHeadSingle']

@MODULE_ZOO_REGISTRY.register('base_anchor_head')
class BaseAnchorHead(nn.Module):
    def __init__(self, inplanes, num_classes):
        super(BaseAnchorHead, self).__init__()
        self.prefix = self.__class__.__name__
        self.num_classes = num_classes
        assert self.num_classes > 0

        if isinstance(inplanes, list):
            inplanes_length = len(inplanes)
            for i in range(1, inplanes_length):
                if inplanes[i] != inplanes[0]:
                    raise ValueError('list inplanes elements are inconsistent with {}'.format(inplanes[i]))
            inplanes = inplanes[0]
        assert isinstance(inplanes, int)
        self.inplanes = inplanes

    def forward(self, **kwargs):
        raise NotImplementedError

@MODULE_ZOO_REGISTRY.register('anchor_head_single')
class AnchorHeadSingle(BaseAnchorHead):
    def __init__(self, inplanes, num_classes, base_anchors_file, code_size, num_dir_bins,
                 use_direction_classifier=True, initializer=None, class_activation='sigmoid', init_prior=0.01):
        super(AnchorHeadSingle, self).__init__(inplanes, num_classes)

        self.class_activation = class_activation
        with open(base_anchors_file, 'r') as f:
            anchor_classes = np.array(json.load(f))
        num_anchors_per_location = []
        for anchor_class in anchor_classes:
            num_rotations = len(anchor_class['anchor_rotations'])
            num_size = len(anchor_class['anchor_sizes'])
            num_class = len(anchor_class['anchor_bottom_heights'])
            num_anchors_per_location.append(num_rotations * num_size * num_class)
        self.num_anchors = sum(num_anchors_per_location)

        self.cls_subnet_pred = nn.Conv2d(
            inplanes, self.num_anchors * self.num_classes, kernel_size=1)
        self.box_subnet_pred = nn.Conv2d(
            inplanes, self.num_anchors * code_size, kernel_size=1)

        if use_direction_classifier:
            self.dir_subnet_pred = nn.Conv2d(
                inplanes, self.num_anchors * num_dir_bins, kernel_size=1)
        else:
            self.dir_subnet_pred = None

        init_bias_focal(self.cls_subnet_pred, self.class_activation, init_prior, self.num_classes)
        initialize_from_cfg(self.box_subnet_pred, initializer)

    def forward_net(self, x):
        cls_preds = self.cls_subnet_pred(x)
        box_preds = self.box_subnet_pred(x)
        cls_preds = cls_preds.permute(0, 2, 3, 1).contiguous()
        box_preds = box_preds.permute(0, 2, 3, 1).contiguous()

        if self.dir_subnet_pred is not None:
            dir_cls_preds = self.dir_subnet_pred(x)
            dir_cls_preds = dir_cls_preds.permute(0, 2, 3, 1).contiguous()
        else:
            dir_cls_preds = None
        return cls_preds, box_preds, dir_cls_preds

    def forward(self, input):
        features = input['spatial_features_2d']
        output = {}
        cls_preds, box_preds, dir_cls_preds = self.forward_net(features)
        output.update({'cls_preds': cls_preds, 'box_preds': box_preds,
                      'dir_pred': dir_cls_preds, 'num_anchors': self.num_anchors})
        return output

max_stars_repo_name: ModelTC/EOD

max_issues_repo_name: ModelTC/EOD

max_forks_repo_name: ModelTC/EOD


text:
from utils import Thread
from signals import MultiChannelFrequencySignal
import numpy as np
from scipy import signal
import time
from datetime import datetime
from pipeline import InterfaceEvents

class Process(Thread):
    def __init__(self, queue_in, queue_out):
        super(Process, self).__init__()
        self.name = 'process'

        self.queue_in = queue_in
        self.queue_out = queue_out
        self.data = None

        self.duration_process = 0.0  # in seconds

    def process(self, data_in):
        return None

    def refresh(self):
        while True:
            try:
                data_in = self.queue_in.get()
                time_now = datetime.now()
                self.data = self.process(data_in)
                self.queue_in.task_done()
                self.duration_process = (datetime.now() - time_now).total_seconds()
                if self.duration_process == 0.0:
                    freq = 0.0
                else:
                    freq = 1.0 / self.duration_process
                #print 'Frequency process (' + self.name + ') = ' + str(round(freq, 2))
                # because we want that the speed of the pipeline is the same as the speed of the slowest process, we need to
                # wait for the next process to finish before adding a new item in the queue
                self.queue_out.put(self.data, block=True, timeout=None)
                time.sleep(0.001)
            except KeyboardInterrupt:
                import sys
                print('KeyboardInterrupt on line {}'.format(sys.exc_info()[-1].tb_lineno))
                sys.exit(2)
            except Exception as e:
                import sys
                print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
                print(str(e))
                sys.exit(2)

class FFT(Process):
    def __init__(self, queue_in, queue_out):
        super(FFT, self).__init__(queue_in, queue_out)
        self.name = 'fft'

    def process(self, data_in):
        k = np.arange(data_in.length)
        T = data_in.length / data_in.estimated_acquisition_freq
        frq = k / T  # two sides frequency range
        x_frq = frq[range(data_in.length / 2)]  # one side frequency range

        data_out_fft = np.fft.fft(data_in.data) / data_in.length  # fft computing and normalization
        data_out_fft = data_out_fft[:, range(data_in.length / 2)]

        data_out = MultiChannelFrequencySignal(length=data_in.length,
                                               estimated_acquisition_freq=data_in.estimated_acquisition_freq,
                                               number_of_channels=data_in.number_of_channels,
                                               label_channels=data_in.label_channels,
                                               data=data_out_fft,
                                               freq=x_frq)
        return data_out

class ButterFilter(Process):
    def __init__(self, queue_in, queue_out, param):
        super(ButterFilter, self).__init__(queue_in, queue_out)
        self.name = 'butterfilter'
        if 'filter_type' in param:
            self.filter_type = str(param['filter_type'])
        else:
            self.filter_type = 'low'
        if 'order' in param:
            self.order = int(param['order'])
        else:
            self.order = 1
        if 'cutoff_frequency' in param:
            self.cutoff_frequency = [np.float(f) for f in param['cutoff_frequency'].split(',')]  # not normalized
        else:
            self.cutoff_frequency = [35.0]
        if 'acquisition_freq' in param:
            self.acquisition_freq = float(param['acquisition_freq'])
        else:
            self.acquisition_freq = 220.0

        if self.filter_type in ['lowpass', 'highpass']:
            self.filter_param = self.getcoeffshigh(self.cutoff_frequency[0], self.acquisition_freq, self.order, self.filter_type)
        elif self.filter_type in ['bandpass', 'bandstop']:
            self.filter_param = self.getcoeffsband(self.cutoff_frequency[0], self.cutoff_frequency[1], self.acquisition_freq, self.order, self.filter_type)

    def getcoeffshigh(self, fcut, fs, order=1, filter_type='lowpass'):
        fnyq = 0.5 * fs  # Get the Nyquist frequency from sampling frequency
        fr = fcut / fnyq  # Normalize cutoff frequency by the Nyquist frequency
        b, a = signal.butter(order, fr, btype=filter_type)  # Get the filter coefficients
        return b, a

    def getcoeffsband(self, lowfcut, highfcut, fs, order=1, filter_type='bandpass'):
        fnyq = 0.5 * fs
        low = lowfcut / fnyq
        high = highfcut / fnyq
        b, a = signal.butter(order, [low, high], btype=filter_type)
        return b, a

    def process(self, data_in):
        # print data_in.data.shape[1]  # len of data vector in channel
        #data_in.data = signal.filtfilt(self.filter_param[0], self.filter_param[1], data_in.data, method='gust')
        data_in.data = signal.filtfilt(self.filter_param[0], self.filter_param[1], data_in.data, method='pad', padtype='even')
        return data_in

class AssociateEvent(Process):
    def __init__(self, queue_in, queue_out, param):
        super(AssociateEvent, self).__init__(queue_in, queue_out)
        self.name = 'associate_event'
        self.events_interface = InterfaceEvents()

    def start(self):
        self.events_interface.start()
        super(AssociateEvent, self).start()

    def process(self, data_in):
        data_in.event_related = self.events_interface.find_closest_event(data_in.datetimes[0])
        return data_in

class WriteToFile(Process):
    def __init__(self, queue_in, queue_out, param):
        super(WriteToFile, self).__init__(queue_in, queue_out)
        self.name = 'writetofile'
        self.last_save = datetime.now()
        self.data_to_write = []
        self.time_to_write = []
        if 'file_name' in param:
            self.file_name = str(param['file_name'])
        else:
            self.file_name = 'dataAcquisition.csv'
        if 'save_delay' in param:
            self.save_delay = param['save_delay']
        else :
            self.save_delay = 180.0

    def process(self, data_in):
        time_now = datetime.now()
        seconds_passed = (time_now - self.last_save).total_seconds()
        if seconds_passed > self.save_delay:
            self.data_to_write.append(data_in.data)
            self.time_to_write.append(data_in.datetimes[0])

            f_handle = open(self.file_name, 'a')
            for i, line in enumerate(self.data_to_write):
                strheader = str(self.time_to_write[i]) + ', Channels: ' + str(data_in.number_of_channels) + ', ' + ', '.join(data_in.label_channels) + ', event, ' + data_in.related_event
                np.savetxt(f_handle, line, header=strheader, newline='\n', delimiter=',')
            f_handle.close()
            self.last_save = datetime.now()

            self.data_to_write = []
            self.time_to_write = []
        else:
            self.data_to_write.append(data_in.data)
            self.time_to_write.append(data_in.datetimes[0])

        return data_in

reason: import numpy,from scipy


text:
import numpy as np
from matplotlib import pyplot as plt
from lifelines.statistics import logrank_test
import pandas as pd

data = pd.read_csv('total_summary.csv', header=0, index_col=0)

def Kaplan_Meier(T, E):
    """T : times of events
       E : how many events are associated to the corresponding entry in T"""
    #start with n0, that is everyone
    n = len(T)
    n0 = n
    ni = n0
    steps = []
    varss = []
    end = max(T)
    #print(end)
    S_hat = []
    Var_hat = []
    for i in range(end):
        #get rid of people who have already left the study
        #the people whoes times have passed
        Ti = np.array(T[T>=i])
        Ei = np.array(E[T>=i])
        #we want the people that fulfill the below criteria
        mask = [True if (Ti[j] != i or Ei[j] != 0) else False for j in range(len(Ti))]
        #censored_mask = np.logical_or(Ti, Ei, where=Ti != i and  Ei != 0)
        Tf = Ti[mask]
        Ef = Ei[mask]
        if i == 0:
            ni = n
        else:
            ni = len(Tf)
        #get the people who "died"
        event_happened = Tf[Ef == 1]
        #get the people who "died" at time t
        event_happened_i = event_happened[event_happened == i]
        di = len(event_happened_i)
        #calculate step for time i
        frac = (ni-di)/ni
        steps.append(frac)
        S_hat_t = np.prod(steps)
        S_hat.append(S_hat_t)
        #Use Greenwood's Formula
        var = di/(ni*(ni-di))
        varss.append(var)
        #with a sum so everything isn't zero
        Var_hat_t = (S_hat_t**2)*np.sum(varss)
        Var_hat.append(Var_hat_t)
    return S_hat, Var_hat

S, V = Kaplan_Meier(data['Time'], data['Event'])

N = max(data['Time'])
domain = np.linspace(1, N, N)
plt.plot(domain, S, c='green', drawstyle='steps-post', label='KM Estimate')
y1 = S + np.sqrt(V)
y2 = S - np.sqrt(V)
plt.fill_between(domain, y1, y2, step='post', color='lightgreen')
plt.xlabel('Number of Words said before \'Christ\' is said in the Talk')
plt.ylabel('Estimated Probability of Not Saying Christ')
plt.title('Kaplan Meier Survival Function Estimating\nthe Probability of Saying Christ in General Conference')
plt.legend(loc='best')
plt.savefig('event_time_total.png')
plt.show()

#apostle mask
apostle = data['Apostle'] == True
data_apostle = data[apostle]
no_apostle = data[~apostle]

S_apostle, V_apostle = Kaplan_Meier(data_apostle['Time'], data_apostle['Event'])
S_no, V_no = Kaplan_Meier(no_apostle['Time'], no_apostle['Event'])

N_yes = max(data_apostle['Time'])
N_no = max(no_apostle['Time'])
domain_apostle = np.linspace(1, N_yes, N_yes)
domain_no = np.linspace(1, N_no, N_no)
plt.plot(domain_apostle, S_apostle, c='green', drawstyle='steps-post', label='Apostles')
y1_yes = S_apostle + np.sqrt(V_apostle)
y2_yes = S_apostle - np.sqrt(V_apostle)
plt.fill_between(domain_apostle, y1_yes, y2_yes, step='post', color='lightgreen')

plt.plot(domain_no, S_no, c='blue', drawstyle='steps-post', label='Other Speakers')
y1_no = S_no + np.sqrt(V_no)
y2_no = S_no - np.sqrt(V_no)
plt.fill_between(domain_no, y1_no, y2_no, step='post', color='lightblue')

plt.xlabel('Number of Words said before \'Christ\' is said in the Talk', fontsize=14)
plt.ylabel('Estimated Probability of Not Saying Christ', fontsize=14)
plt.title('Kaplan Meier Survival Function Estimating Probability\nof Saying Christ in General Conference Talk', fontsize=16)
plt.legend(loc='best')
plt.savefig('event_time_apostle.png')

max_stars_repo_name: lodeous/gencon-nlp

max_issues_repo_name: lodeous/gencon-nlp

max_forks_repo_name: lodeous/gencon-nlp


text:
from yafs.stats import Stats
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, date_range
from matplotlib.ticker import FormatStrFormatter
# for size in [100,1000,10000,100000,1000000]:
time_loops = [["M.Action"]]

#### ANALYSE FILES YAFS

app1 = "app1"
app2 = "app2"

size = 12000

path = "examples/DynamicAllocation/"

#s = Stats(defaultPath=path+"Results_%s"%(size))
s1 = Stats(defaultPath=path+"Results_%s_singleApp1" % (size))
#s2 = Stats(defaultPath=path+"Results_%s_singleApp2" % (size))
#Network
#s.showResults2(size, time_loops=time_loops)
#app1 based on dinamic Edge
s1.showResults2(size, time_loops=time_loops)
#app2 based on static Cloud
#s2.showResults2(size, time_loops=time_loops)

df = s1.df
dfl = s1.df_link

#df["date"]=df.time_in.astype('timedelta64[s]')
df["date"]=df.time_in.astype('datetime64[s]')
df.index = df.date

dfap1 = df[df["app"]=="app1"]
dfap1 = dfap1.resample('300s').agg(dict(time_latency='mean'))

timeLatency = dfap1.time_latency.values

ticks = range(len(timeLatency))
ticksV = np.array(ticks)*300

#index = dfap1.index
#rng = pd.date_range('01/01/1970',periods=20,freq="300s")

values = range(len(ticks))

for x in range(len(ticks)):
    if x<10:
        values[x]=1
    elif x>28:
        values[x]=20
    else:
        values[x]=values[x-1]+1

#OK
        ### Latency Time and Allocation replicas
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(ticks, timeLatency, '-')
ax1.set_xlabel("Simulation time", fontsize=16)
ax1.set_ylabel("Latency time", fontsize=16)
ax2 = ax1.twinx()
ax2.set_ylabel("Number of allocation replicas", fontsize=16)
ax2.set_yticks(ticks)
ax2.set_xlim(0,35)
ax2.plot(ticks, values, c="g",linestyle='-', marker="o",markersize=4)

time_labels = []
for t in ax2.get_xticks():
    time_labels.append( "%3.0f"%(t*300))
ax1.set_xticklabels(time_labels)

dfl["date"]=dfl.ctime.astype('datetime64[s]')
dfl.index = dfl.date
dfl1 = dfl.resample('300s').agg(dict(buffer='mean'))

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(ticks, dfl1.buffer.values, '-')
ax1.set_xlabel("Simulation time", fontsize=16)
ax1.set_ylabel("Number of messages enqueued", fontsize=16)

time_labels = []
for t in ax1.get_xticks():
    time_labels.append( "%3.0f"%(t*300))
ax1.set_xticklabels(time_labels)


text:
import numpy as np

def pixel_to_sensor(pixels, pixel_size, rows, cols):
    i, j = pixels[:,0], pixels[:,1]
    return np.column_stack(((j-cols/2) * pixel_size, (rows/2-i) * pixel_size))

def sensor_to_pixel(sensors, pixel_size, rows, cols):
    x, y = sensors[:,0], sensors[:,1]
    return np.column_stack((rows/2 - y/pixel_size, x/pixel_size + cols/2))

max_stars_repo_name: victorpoughon/master-thesis

max_issues_repo_name: victorpoughon/master-thesis

max_forks_repo_name: victorpoughon/master-thesis


text:
import numpy as np
from ..base_channel import MatrixFactorization
from .AMP_matrix_factorization import VAMP_matrix_factorization
from .SE_matrix_factorization import SE_matrix_factorization

class LowRankFactorization(MatrixFactorization):
    """Low rank factorization x = u v^T / sqrt(N).

    Notes
    -----
    - u : array of shape (M, K)
    - v : array of shape (N, K)
    - x : array of shape (M, N)
    In components x_ij = u_i.v_j / sqrt(N)
    """

    def __init__(self, M, N, K):
        self.M = M
        self.N = N
        self.K = K
        self.repr_init()

    def sample(self, U, V):
        if U.shape != (self.M, self.K):
            raise ValueError("Bad shape for U")
        if V.shape != (self.N, self.K):
            raise ValueError("Bad shape for V")
        X = U @ V.T / np.sqrt(self.N)
        return X

    def math(self):
        return r"$uv^T$"

    def second_moment(self, tau_u, tau_v):
        tau_x = self.K * tau_u * tau_v / self.N
        return tau_x

    def compute_forward_posterior(self, az, bz, ax, bx):
        "estimate x; for x = uv^T / sqrt(N) and z = [u, v]"
        au, av = az
        bu, bv = bz
        # FIXME : derive forward posterior for matrix factorization
        # Using placeholders
        rx, vx = np.ones_like(bx), 1.
        return rx, vx

    def compute_backward_posterior(self, az, bz, ax, bx):
        "estimate z; for x = uv^T / sqrt(N) and z = [u, v]"
        au, av = az
        bu, bv = bz
        assert bu.shape == (self.M, self.K)
        assert bv.shape == (self.N, self.K)
        assert bx.shape == (self.M, self.N)
        VAMP = VAMP_matrix_factorization(
            K=self.K, N=self.N, M=self.M, model='UV',
            au_av_bu_bv=[au, av, bu, bv], ax_bx=[ax, bx], verbose=False
        )
        rz_u, vz_u, rz_v, vz_v = VAMP.VAMP_training()
        # posterior for z = [u, v]
        rz = [rz_u, rz_v]
        vz = [vz_u, vz_v]
        return rz, vz

    def compute_forward_error(self, az, ax, tau_z):
        raise NotImplementedError

    def compute_backward_error(self, az, ax, tau_z):
        au, av = az
        SE = SE_matrix_factorization(
            K=self.K, N=self.N, M=self.M, model='UV',
            au_av=[au, av], ax=ax, verbose=False
        )
        vz_u,  vz_v = SE.main()
        # error for z = [u, v]
        vz = [vz_u, vz_v]
        return vz

max_stars_repo_name: mdnls/tramp

max_issues_repo_name: mdnls/tramp

max_forks_repo_name: mdnls/tramp


text:
import re
import os
import sys
import time
import json
import nltk
import torch
import random
import argparse
import operator
import progressbar
import numpy as np
from torch import nn
import torch.nn.functional as F
from operator import itemgetter

def restore_text(text, mode):
    if mode == 'bs':
        text = re.sub(' is ', ' ', text)
    elif mode == 'da':
        pass
    else:
        raise Exception('Wrong Restore Mode!!!')
    text = re.sub(' , ', ' ', text)
    text = ' '.join(text.split()).strip()
    return text

def e2e_batch_generate(model, one_inference_batch, input_contain_db, data):
    is_cuda = next(model.parameters()).is_cuda
    if is_cuda:
        #device = next(model.parameters()).device
        device = torch.device('cuda')
        if torch.cuda.device_count() > 1: # multi-gpu training
            model = model.module
        else: # single gpu training
            pass
    else:
        device = 0

    max_span_len, max_response_len = 80, 120
    tokenizer = data.tokenizer
    bs_batch, da_batch, nlg_batch, parse_dict_batch = one_inference_batch
    batch_size = len(parse_dict_batch)
    res_batch_parse_dict = parse_dict_batch

    # if input_contain_db == True: then we first generate the belief state and get the db result
    # otherwise, we perform the generation all in-parallel

    bs_tensor, bs_mask = data.pad_batch(bs_batch)
    if is_cuda:
        bs_tensor = bs_tensor.cuda(device)
        bs_mask = bs_mask.cuda(device)

    batch_bs_text = model.batch_generate(bs_tensor, bs_mask, generate_mode='bs', max_decode_len=max_response_len)

    # the belief state sequence could be long
    batch_bs_restore_text = []
    for idx in range(batch_size):
        one_bs_text = batch_bs_text[idx]
        res_batch_parse_dict[idx]['bspn_gen'] = one_bs_text

    if input_contain_db:
        # we need to query the db base
        batch_db_input_id_list = []
        for idx in range(batch_size):
            one_queried_db_result = \
            data.reader.bspan_to_DBpointer(batch_bs_text[idx], res_batch_parse_dict[idx]['turn_domain'])
            one_db_text = '<sos_db> ' + one_queried_db_result + ' <eos_db>'
            one_db_token_id_input = tokenizer.convert_tokens_to_ids(tokenizer.tokenize(one_db_text))
            batch_db_input_id_list.append(one_db_token_id_input)
    else:
        batch_db_input_id_list = []
        for _ in range(batch_size):
            batch_db_input_id_list.append([])

    if input_contain_db:
        # then we generate the dialogue action
        da_batch_input_id_list = []
        for idx in range(batch_size):
            one_da_input_id_list = da_batch[idx] + batch_db_input_id_list[idx]
            da_batch_input_id_list.append(one_da_input_id_list)
        da_tensor, da_mask = data.pad_batch(da_batch_input_id_list)
    else:
        da_tensor, da_mask = data.pad_batch(da_batch)

    if is_cuda:
        da_tensor = da_tensor.cuda(device)
        da_mask = da_mask.cuda(device)
    batch_da_text = model.batch_generate(da_tensor, da_mask, generate_mode='da', max_decode_len=max_span_len)

    for idx in range(batch_size):
        res_batch_parse_dict[idx]['aspn_gen'] = batch_da_text[idx]

    if input_contain_db:
        # finally, we generate the response
        nlg_batch_input_id_list = []
        for idx in range(batch_size):
            one_nlg_input_id_list = nlg_batch[idx] + batch_db_input_id_list[idx]
            nlg_batch_input_id_list.append(one_nlg_input_id_list)
        nlg_tensor, nlg_mask = data.pad_batch(nlg_batch_input_id_list)
    else:
        nlg_tensor, nlg_mask = data.pad_batch(nlg_batch)

    if is_cuda:
        nlg_tensor = nlg_tensor.cuda(device)
        nlg_mask = nlg_mask.cuda(device)
    batch_nlg_text = model.batch_generate(nlg_tensor, nlg_mask, generate_mode='nlg', max_decode_len=max_response_len)
    for idx in range(batch_size):
        res_batch_parse_dict[idx]['resp_gen'] = batch_nlg_text[idx]
    return res_batch_parse_dict


text:
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.3.0
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_1dplots:
#
# Plotting 1D data
# ================
#
# ProPlot adds new features to various `~matplotlib.axes.Axes` plotting
# methods using a set of wrapper functions. When a plotting method like
# `~matplotlib.axes.Axes.plot` is "wrapped" by one of these functions, it
# accepts the same parameters as the wrapper. These features are a strict
# *superset* of the matplotlib API.
# This section documents the features added by wrapper functions to 1D
# plotting commands like `~matplotlib.axes.Axes.plot`,
# `~matplotlib.axes.Axes.scatter`, `~matplotlib.axes.Axes.bar`, and
# `~matplotlib.axes.Axes.barh`.

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_cycle_changer:
#
# Property cycles
# ---------------
#
# It is often useful to create on-the-fly `property cycles
# <https://matplotlib.org/tutorials/intermediate/color_cycle.html#sphx-glr-tutorials-intermediate-color-cycle-py>`__
# and use different property cycles for different plot elements. You can create and
# apply property cycles on-the-fly using the `cycle` and `cycle_kw` arguments, available
# with any plotting method wrapped by `~proplot.axes.cycle_changer`. `cycle` and
# `cycle_kw` are passed to the `~proplot.constructor.Cycle` constructor function, and
# the resulting property cycle is used for the plot. You can specify `cycle`
# once with 2D input data (in which case each column is plotted in succession
# according to the property cycle) or call a plotting command multiple times with the
# same `cycle` argument each time (the property cycle is not reset). For more
# information on property cycles, see the :ref:`color cycles section <ug_cycles>` and
# `this matplotlib tutorial
# <https://matplotlib.org/tutorials/intermediate/color_cycle.html#sphx-glr-tutorials-intermediate-color-cycle-py>`__.

# %%
import proplot as plot
import numpy as np
N = 4
state = np.random.RandomState(51423)
data1 = state.rand(6, N)
data2 = state.rand(6, N) * 1.5
with plot.rc.context({'lines.linewidth': 3}):
    fig, axs = plot.subplots(ncols=2)
    axs.format(xlabel='xlabel', ylabel='ylabel', suptitle='Local property cycles demo')

    # Use property cycle for columns of 2D input data
    axs[0].plot(
        data1 * data2,
        cycle='black',
        cycle_kw={'linestyle': ('-', '--', '-.', ':')}
    )

    # Use property cycle with successive plot() calls
    for i in range(data1.shape[1]):
        axs[1].plot(data1[:, i], cycle='Reds', cycle_kw={'N': N, 'left': 0.3})
    for i in range(data1.shape[1]):
        axs[1].plot(data2[:, i], cycle='Blues', cycle_kw={'N': N, 'left': 0.3})

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_1dstd:
#
# Standardized arguments
# ----------------------
#
# The `~proplot.axes.standardize_1d` wrapper standardizes
# positional arguments across all 1D plotting methods.
# `~proplot.axes.standardize_1d` allows you to optionally omit *x*
# coordinates, in which case they are inferred from the data. It also permits
# passing 2D *y* coordinate arrays to any plotting method, in which case the
# plotting method is called for each column of the array.

# %%
import proplot as plot
import numpy as np

# Figure and sample data
N = 5
state = np.random.RandomState(51423)
with plot.rc.context({'axes.prop_cycle': plot.Cycle('Grays', N=N, left=0.3)}):
    fig, axs = plot.subplots(ncols=2, share=False)
    x = np.linspace(-5, 5, N)
    y = state.rand(N, 5)
    axs.format(xlabel='xlabel', ylabel='ylabel')
    axs.format(suptitle='Standardized arguments demonstration')

    # Plot by passing both x and y coordinates
    ax = axs[0]
    ax.area(x, -1 * y / N, stacked=True)
    ax.bar(x, y, linewidth=0, alpha=1, width=0.8)
    ax.plot(x, y + 1, linewidth=2)
    ax.scatter(x, y + 2, marker='s', markersize=5**2)
    ax.format(title='Manual x coordinates')

    # Plot by passing just y coordinates
    # Default x coordinates are inferred from DataFrame,
    # inferred from DataArray, or set to np.arange(0, y.shape[0])
    ax = axs[1]
    ax.area(-1 * y / N, stacked=True)
    ax.bar(y, linewidth=0, alpha=1)
    ax.plot(y + 1, linewidth=2)
    ax.scatter(y + 2, marker='s', markersize=5**2)
    ax.format(title='Auto x coordinates')

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_1dintegration:
#
# Pandas and xarray integration
# -----------------------------
#
# The `~proplot.axes.standardize_1d` wrapper integrates 1D plotting
# methods with pandas `~pandas.DataFrame`\ s and xarray `~xarray.DataArray`\ s.
# When you pass a DataFrame or DataArray to any plotting command, the x-axis
# label, y-axis label, legend label, colorbar label, and/or title are
# configured from the metadata. This restores some of the convenience you get
# with the builtin `pandas <https://pandas.pydata.org>`__ and `xarray
# <https://pandas.pydata.org>`__ plotting functions. This feature is
# *optional*; installation of pandas and xarray are not required.

# %%
import xarray as xr
import numpy as np
import pandas as pd

# DataArray
state = np.random.RandomState(51423)
data = (
    np.sin(np.linspace(0, 2 * np.pi, 20))[:, None]
    + state.rand(20, 8).cumsum(axis=1)
)
da = xr.DataArray(data, dims=('x', 'cat'), coords={
    'x': xr.DataArray(
        np.linspace(0, 1, 20),
        dims=('x',),
        attrs={'long_name': 'distance', 'units': 'km'}
    ),
    'cat': xr.DataArray(
        np.arange(0, 80, 10),
        dims=('cat',),
        attrs={'long_name': 'parameter', 'units': 'K'}
    )
}, name='position series')

# DataFrame
data = (
    (np.cos(np.linspace(0, 2 * np.pi, 20))**4)[:, None] + state.rand(20, 5)**2
)
ts = pd.date_range('1/1/2000', periods=20)
df = pd.DataFrame(data, index=ts, columns=['foo', 'bar', 'baz', 'zap', 'baf'])
df.name = 'time series'
df.index.name = 'time (s)'
df.columns.name = 'columns'

# %%
import proplot as plot
fig, axs = plot.subplots(ncols=2, axwidth=2.2, share=0)
axs.format(suptitle='Automatic subplot formatting')

# Plot DataArray
cycle = plot.Cycle('dark blue', fade=90, space='hpl', N=da.shape[1])
axs[0].scatter(da, cycle=cycle, lw=3, colorbar='ul', colorbar_kw={'locator': 20})

# Plot Dataframe
cycle = plot.Cycle('dark green', fade=90, space='hpl', N=df.shape[1])
axs[1].plot(df, cycle=cycle, lw=3, legend='uc')

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_errorbars:
#
# Shading and error bars
# ----------------------
#
# The `~proplot.axes.indicate_error` wrapper lets you draw error bars
# and error shading on-the-fly by passing certain keyword arguments to
# `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.scatter`,
# `~matplotlib.axes.Axes.bar`, or `~matplotlib.axes.Axes.barh`.
#
# If you pass 2D arrays to these methods with ``means=True`` or
# ``medians=True``, the means or medians of each column are drawn as points,
# lines, or bars, and *error bars* or *shading* is drawn to represent the spread
# of the distribution for each column. You can also specify the error bounds
# *manually* with the `bardata`, `boxdata`, `shadedata`, and `fadedata` keywords.
# `~proplot.axes.indicate_error` can draw thin error bars with optional whiskers,
# thick "boxes" overlayed on top of these bars (think of this as a miniature boxplot),
# and up to 2 regions of shading. See `~proplot.axes.indicate_error` for details.

# %%
import proplot as plot
import numpy as np
import pandas as pd
plot.rc['title.loc'] = 'uc'

# Generate sample data
state = np.random.RandomState(51423)
data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1]
data = data + 20 * state.normal(size=(20, 8)) + 30
data = pd.DataFrame(data, columns=np.arange(0, 16, 2))
data.name = 'variable'

# Generate figure
fig, axs = plot.subplots(
    nrows=3, aspect=1.5, axwidth=4,
    share=0, hratios=(2, 1, 1)
)
axs.format(suptitle='Indicating error bounds with various plotting commands')
axs[1:].format(xlabel='column number', xticks=1, xgrid=False)

# Automatically calculate medians and display default percentile range
ax = axs[0]
obj = ax.barh(
    data,
    color='light red', legend=True,
    medians=True, boxpctiles=True, barpctiles=(5, 95),
)
ax.format(title='Column statistics')
ax.format(ylabel='column number', title='Bar plot', ygrid=False)

# Automatically calculate means and display requested standard deviation range
ax = axs[1]
ax.scatter(
    data,
    color='denim', marker='x', markersize=8**2, linewidth=0.8,
    means=True, shadestds=(-1, 1), shadelabel=True, legend='ll',
)
ax.format(title='Scatter plot')

# Manually supply error bar data and legend labels
ax = axs[2]
means = data.mean(axis=0)
means.name = data.name
shadedata = np.percentile(data, (25, 75), axis=0)  # dark shading
fadedata = np.percentile(data, (5, 95), axis=0)  # light shading
ax.plot(
    means,
    shadedata=shadedata, fadedata=fadedata,
    shadelabel='50% CI', fadelabel='90% CI',
    color='ocean blue', barzorder=0, boxmarker=False, legend='ll',
)
ax.format(title='Line plot')
plot.rc.reset()

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_bar:
#
# Bar plots and area plots
# ------------------------
#
# The `~matplotlib.axes.Axes.bar` and `~matplotlib.axes.Axes.barh` methods
# are wrapped by `~proplot.axes.bar_wrapper`,
# `~proplot.axes.cycle_changer`, and `~proplot.axes.standardize_1d`.
# You can now *group* or *stack* columns of data by passing 2D arrays to
# `~matplotlib.axes.Axes.bar` or `~matplotlib.axes.Axes.barh`, just like in
# `pandas`, or use different colors for negative and positive bars by
# passing ``negpos=True``. Also, `~matplotlib.axes.Axes.bar` and
# `~matplotlib.axes.Axes.barh` now employ "default" *x* coordinates if you
# failed to provide them explicitly.
#
# To make filled "area" plots, use the new `~proplot.axes.Axes.area` and
# `~proplot.axes.Axes.areax` methods. These are alises for
# `~matplotlib.axes.Axes.fill_between` and
# `~matplotlib.axes.Axes.fill_betweenx`, which are wrapped by
# `~proplot.axes.fill_between_wrapper` and
# `~proplot.axes.fill_betweenx_wrapper`. You can now *stack* or *overlay*
# columns of data by passing 2D arrays to `~proplot.axes.Axes.area` and
# `~proplot.axes.Axes.areax`, just like in `pandas`. You can also now draw
# area plots that change color when the fill boundaries cross each other by
# passing ``negpos=True`` to `~matplotlib.axes.Axes.fill_between`. The most
# common use case for this is highlighting negative and positive areas with
# different colors, as shown below.

# %%
import proplot as plot
import numpy as np
import pandas as pd
state = np.random.RandomState(51423)
data = state.rand(5, 5).cumsum(axis=0).cumsum(axis=1)[:, ::-1]
data = pd.DataFrame(
    data, columns=pd.Index(np.arange(1, 6), name='column'),
    index=pd.Index(['a', 'b', 'c', 'd', 'e'], name='row idx')
)

# Generate figure
plot.rc.titleloc = 'uc'
fig, axs = plot.subplots(nrows=2, aspect=2, axwidth=4.8, share=0, hratios=(3, 2))

# Side-by-side bars
ax = axs[0]
obj = ax.bar(
    data, cycle='Reds', colorbar='ul',
    edgecolor='red9', colorbar_kw={'frameon': False}
)
ax.format(
    xlocator=1, xminorlocator=0.5, ytickminor=False,
    title='Side-by-side', suptitle='Bar plot demo'
)

# Stacked bars
ax = axs[1]
obj = ax.barh(
    data.iloc[::-1, :], cycle='Blues',
    legend='ur', edgecolor='blue9', stacked=True
)
ax.format(title='Stacked')
axs.format(grid=False)
plot.rc.reset()

# %%
import proplot as plot
import numpy as np
state = np.random.RandomState(51423)
data = state.rand(5, 3).cumsum(axis=0)
cycle = ('gray3', 'gray5', 'gray7')

# Generate figure
fig, axs = plot.subplots(ncols=2, axwidth=2.3, share=0)
axs.format(grid=False, xlabel='xlabel', ylabel='ylabel', suptitle='Area plot demo')

# Overlaid area patches
ax = axs[0]
ax.area(
    np.arange(5), data, data + state.rand(5)[:, None], cycle=cycle, alpha=0.7,
    legend='uc', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},
)
ax.format(title='Fill between columns')

# Stacked area patches
ax = axs[1]
ax.area(
    np.arange(5), data, stacked=True, cycle=cycle, alpha=0.8,
    legend='ul', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},
)
ax.format(title='Stack between columns')

# %%
import proplot as plot
import numpy as np
state = np.random.RandomState(51423)
data = 4 * (state.rand(50) - 0.5)

# Generate figure
fig, axs = plot.subplots(nrows=2, width=5, aspect=2)
axs.format(
    xmargin=0, xlabel='xlabel', ylabel='ylabel', grid=True,
    suptitle='Positive and negative colors demo',
)
axs.axhline(0, color='k', linewidth=1)  # zero line

# Bar plot
axs[0].bar(data, width=1, edgecolor='none', negpos=True)
axs[0].format(title='Bar plot')

# Area plot
axs[1].area(data, negpos=True)
axs[1].format(title='Area plot')

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_boxplots:
#
# Box plots and violin plots
# --------------------------
#
# The `~matplotlib.axes.Axes.boxplot` and `~matplotlib.axes.Axes.violinplot`
# methods are now wrapped with `~proplot.axes.boxplot_wrapper`,
# `~proplot.axes.violinplot_wrapper`, `~proplot.axes.cycle_changer`,
# and `~proplot.axes.standardize_1d`. These wrappers add some useful
# options and apply aesthetically pleasing default settings. They also
# automatically apply axis labels based on the `~pandas.DataFrame` column
# labels or the input *x* coordinate labels.

# %%
import proplot as plot
import numpy as np
import pandas as pd

# Generate sample data
N = 500
state = np.random.RandomState(51423)
data = state.normal(size=(N, 5)) + 2 * (state.rand(N, 5) - 0.5) * np.arange(5)
data = pd.DataFrame(
    data,
    columns=pd.Index(['a', 'b', 'c', 'd', 'e'], name='xlabel')
)

# Generate figure
fig, axs = plot.subplots(ncols=2, axwidth=2.5)
axs.format(grid=False, suptitle='Boxes and violins demo')

# Box plots
ax = axs[0]
obj1 = ax.boxplot(
    data, lw=0.7, marker='x', fillcolor='gray5',
    medianlw=1, mediancolor='k'
)
ax.format(title='Box plots', titleloc='uc')

# Violin plots
ax = axs[1]
obj2 = ax.violinplot(
    data, lw=0.7, fillcolor='gray7',
    points=500, bw_method=0.3, means=True
)
ax.format(title='Violin plots', titleloc='uc')

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_parametric:
#
# Parametric plots
# ----------------
#
# To make "parametric" plots, use the new `~proplot.axes.Axes.parametric`
# method. Parametric plots are
# `~matplotlib.collections.LineCollection`\ s that map individual line
# segments to individual colors, where each segment represents a "parametric"
# coordinate (e.g. time). The parametric coordinates are specified with the
# `values` keyword argument. See `~proplot.axes.Axes.parametric` for details.
# As shown below, it is also easy to build colorbars from the
# `~matplotlib.collections.LineCollection` returned by
# `~proplot.axes.Axes.parametric`.

# %%
import proplot as plot
import numpy as np
fig, axs = plot.subplots(
    share=0, ncols=2, wratios=(2, 1),
    width='16cm', aspect=(2, 1)
)
axs.format(suptitle='Parametric plots demo')
cmap = 'IceFire'

# Parametric line with smooth gradations
ax = axs[0]
state = np.random.RandomState(51423)
N = 50
x = (state.rand(N) - 0.52).cumsum()
y = state.rand(N)
c = np.linspace(-N / 2, N / 2, N)  # color values
m = ax.parametric(
    x, y, c, cmap=cmap, lw=7, interp=5, capstyle='round', joinstyle='round'
)
ax.format(xlabel='xlabel', ylabel='ylabel', title='Line with smooth gradations')
ax.colorbar(m, loc='b', label='parametric coordinate', locator=5)

# Parametric line with stepped gradations
N = 12
ax = axs[1]
radii = np.linspace(1, 0.2, N + 1)
angles = np.linspace(0, 4 * np.pi, N + 1)
x = radii * np.cos(1.4 * angles)
y = radii * np.sin(1.4 * angles)
c = np.linspace(-N / 2, N / 2, N + 1)
m = ax.parametric(x, y, c, cmap=cmap, lw=15)
ax.format(
    xlim=(-1, 1), ylim=(-1, 1), title='Step gradations',
    xlabel='cosine angle', ylabel='sine angle'
)
ax.colorbar(m, loc='b', maxn=10, label='parametric coordinate')

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_scatter:
#
# Other plotting methods
# ----------------------
#
# The `~matplotlib.axes.Axes.scatter` method is now wrapped by
# `~proplot.axes.scatter_wrapper`, `~proplot.axes.cycle_changer`, and
# `~proplot.axes.standardize_1d`. This means that
# `~matplotlib.axes.Axes.scatter` now accepts 2D arrays, just like
# `~matplotlib.axes.Axes.plot`. Also, successive calls to
# `~matplotlib.axes.Axes.scatter` now use the property cycler properties
# (e.g.  `color`, `marker`, and `markersize`), and
# `~matplotlib.axes.Axes.scatter` now optionally accepts keywords that look
# like `~matplotlib.axes.Axes.plot` keywords (e.g. `color` instead of `c` and
# `markersize` instead of `s`).
#
# ProPlot also supports property cycling for `~proplot.axes.Axes.step` plots
# and wraps the `~matplotlib.axes.Axes.vlines` and `~matplotlib.axes.Axes.hlines`
# methods with `~proplot.axes.vlines_wrapper` and `~proplot.axes.hlines_wrapper`,
# which adds the ability to use different colors for "negative" and "positive" lines.

# %%
import proplot as plot
import numpy as np
import pandas as pd
fig, axs = plot.subplots(ncols=2, share=1)
state = np.random.RandomState(51423)
x = (state.rand(20) - 0).cumsum()
data = (state.rand(20, 4) - 0.5).cumsum(axis=0)
data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))

# Scatter plot with property cycler
ax = axs[0]
ax.format(suptitle='Scatter plot demo', title='Extra prop cycle properties')
obj = ax.scatter(
    x, data, legend='ul', cycle='Set2', legend_kw={'ncols': 2},
    cycle_kw={'marker': ['x', 'o', 'x', 'o'], 'markersize': [5, 10, 20, 30]}
)

# Scatter plot with colormap
ax = axs[1]
ax.format(title='Scatter plot with cmap')
data = state.rand(2, 100)
obj = ax.scatter(
    *data, color=data.sum(axis=0), size=state.rand(100), smin=3, smax=30,
    marker='o', cmap='dark red', colorbar='lr', vmin=0, vmax=2,
    colorbar_kw={'label': 'label', 'locator': 0.5}
)
axs.format(xlabel='xlabel', ylabel='ylabel')

# %%
import proplot as plot
import numpy as np
state = np.random.RandomState(51423)
fig, axs = plot.subplots(ncols=2, nrows=2, share=0)
axs.format(suptitle='Line plots demo', xlabel='xlabel', ylabel='ylabel')

# Step
ax = axs[0]
data = state.rand(20, 4).cumsum(axis=1).cumsum(axis=0)
cycle = ('blue7', 'gray5', 'red7', 'gray5')
ax.step(data, cycle=cycle, labels=list('ABCD'), legend='ul', legend_kw={'ncol': 2})
ax.format(title='Step plot')

# Stems
ax = axs[1]
data = state.rand(20)
ax.stem(data, linefmt='k-')
ax.format(title='Stem plot')

# Vertical lines
gray = 'gray7'
data = state.rand(20) - 0.5
ax = axs[2]
ax.area(data, color=gray, alpha=0.2)
ax.vlines(data, negpos=True, linewidth=2)
ax.format(title='Vertical lines')

# Horizontal lines
ax = axs[3]
ax.areax(data, color=gray, alpha=0.2)
ax.hlines(data, negpos=True, linewidth=2)
ax.format(title='Horizontal lines')


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
import lilv
import sys
import wave
import numpy

# Read command line arguments
if len(sys.argv) != 4:
    print 'USAGE: lv2_apply.py PLUGIN_URI INPUT_WAV OUTPUT_WAV'
    sys.exit(1)

# Initialise Lilv
world = lilv.World()
world.load_all()

plugin_uri   = world.new_uri(sys.argv[1])
wav_in_path  = sys.argv[2]
wav_out_path = sys.argv[3]

# Find plugin
plugin = world.get_all_plugins().get_by_uri(plugin_uri)
if not plugin:
    print "Unknown plugin `%s'\n" % plugin_uri
    sys.exit(1)

lv2_InputPort  = world.new_uri(lilv.LILV_URI_INPUT_PORT)
lv2_OutputPort = world.new_uri(lilv.LILV_URI_OUTPUT_PORT)
lv2_AudioPort  = world.new_uri(lilv.LILV_URI_AUDIO_PORT)

n_audio_in  = plugin.get_num_ports_of_class(lv2_InputPort,  lv2_AudioPort)
n_audio_out = plugin.get_num_ports_of_class(lv2_OutputPort, lv2_AudioPort)
if n_audio_out == 0:
    print "Plugin has no audio outputs\n"
    sys.exit(1)

# Open input file
wav_in = wave.open(wav_in_path, 'r')
if not wav_in:
    print "Failed to open input `%s'\n" % wav_in_path
    sys.exit(1)
if wav_in.getnchannels() != n_audio_in:
    print "Input has %d channels, but plugin has %d audio inputs\n" % (
        wav_in.getnchannels(), n_audio_in)
    sys.exit(1)

# Open output file
wav_out = wave.open(wav_out_path, 'w')
if not wav_out:
    print "Failed to open output `%s'\n" % wav_out_path
    sys.exit(1)

# Set output file to same format as input (except possibly nchannels)
wav_out.setparams(wav_in.getparams())
wav_out.setnchannels(n_audio_out)

rate    = wav_in.getframerate()
nframes = wav_in.getnframes()

# Instantiate plugin
instance = lilv.Instance(plugin, rate)

def read_float(wf, nframes):
    wav = wf.readframes(nframes)
    if wf.getsampwidth() == 4:
        wav = wave.struct.unpack("<%ul" % (len(wav) / 4), wav)
        wav = [ i / float(math.pow(2, 32)) for i in wav ]
    elif wf.getsampwidth() == 2:
        wav = wave.struct.unpack("<%uh" % (len(wav) / 2), wav)
        wav = [ i / float(math.pow(2, 16)) for i in wav ]
    else:
        wav = wave.struct.unpack("%uB"  % (len(wav)),     wav)
        wav = [ s - 128 for s in wav ]
        wav = [ i / float(math.pow(2, 8)) for i in wav ]

    n_channels = wf.getnchannels()
    wavs       = []
    if n_channels > 1:
        for i in xrange(n_channels):
            wavs.append([ wav[j] for j in xrange(0, len(wav), n_channels) ])
    else:
        wavs = [ wav ]

    return wavs

in_buf = read_float(wav_in, nframes)

# TODO: buffer marshaling
#instance.connect_port(3, in_buf)

print '%s => %s => %s @ %d Hz' % (wav_in_path, plugin.get_name(), wav_out_path, rate)

instance.connect_port(3, in_buf)

max_stars_repo_name: bit-trade-one/SoundModuleAP

max_issues_repo_name: bit-trade-one/SoundModuleAP

max_forks_repo_name: bit-trade-one/SoundModuleAP