<commit_before>"""

Source-filter dictionary prior learning for gamma noise model

CREATED: 2013-07-12 11:09:44 by Dawen Liang <daliang@adobe.com>

"""

import time
import sys

import numpy as np
import scipy.optimize as optimize
import scipy.special as special

class SF_Dict(object):
    def __init__(self, W, U, alpha, gamma, L=10, smoothness=100,
                 flat_init=True, seed=None):
        self.W = W.copy()
        self.T, self.F = W.shape
        self.L = L
        if seed is None:
            print 'Using random seed'
            np.random.seed()
        else:
            print 'Using fixed seed {}'.format(seed)
            np.random.seed(seed)
        self.U, self.alpha, self.gamma = U, alpha, gamma
        self.reverb = np.random.randn(self.F)
        self._init_variational(smoothness, flat_init)

    def _init_variational(self, smoothness, flat_init):
        if flat_init:
            self.a = smoothness * np.random.gamma(smoothness,
                                                  1. / smoothness,
                                                  size=(self.T, self.L))
            self.b = smoothness * np.random.gamma(smoothness,
                                                  1. / smoothness,
                                                  size=(self.T, self.L))
        else:
            EA = np.exp(special.psi(self.alpha) - np.log(self.alpha))
            self.a = smoothness * np.ones((self.T, self.L))
            self.b = smoothness * np.ones((self.T, self.L)) / EA
        self.EA, self.ElogA = comp_expect(self.a, self.b)

    def vb_e(self, cold_start=False, smoothness=100, maxiter=15000,
             verbose=True, disp=0):
        """ Perform one variational E-step to appxorimate the posterior
        P(A | -)

        Parameters
        ----------
        cold_start: bool
            Do e-step with fresh start, otherwise just do e-step with
            previous values as initialization.
        smoothness: float
            Smootheness of the variational initialization, larger value will
            lead to more concentrated initialization.
        verbose: bool
            Output log if true.
        disp: int
            Display warning from solver if > 0, mainly from LBFGS.

        """
        print 'Variational E-step...'
        if cold_start:
            # re-initialize all the variational parameters
            self._init_variational(smoothness)

        if verbose:
            last_score = self.bound()
            print('Update (initial)\tObj: {:.2f}'.format(last_score))
            start_t = time.time()
        for t in xrange(self.T):
            #last_score = self.bound()
            self.update_theta_batch(t, maxiter, disp)
            #score = self.bound()
            #if score < last_score:
            #    print('Oops, before: {}\tafter: {}\tt={}'.format(
            #        last_score, score, t))
            if verbose and not t % 20:
                sys.stdout.write('.')
        if verbose:
            t = time.time() - start_t
            sys.stdout.write('\n')
            print 'Batch update\ttime: {:.2f}'.format(t)
            score = self.bound()
            print_increment('A', last_score, score)

    def update_theta_batch(self, t, maxiter, disp):
        def f(theta):
            a, b = np.exp(theta[:self.L]), np.exp(theta[-self.L:])
            Ea, Eloga = comp_expect(a, b)
            logEexp = comp_log_exp(a[:, np.newaxis],
                                   b[:, np.newaxis], self.U)
            likeli = -(self.W[t] * np.exp(np.sum(logEexp, axis=0) - self.reverb)
                       + np.dot(Ea, self.U)) * self.gamma
            prior = (self.alpha - 1) * Eloga - self.alpha * Ea
            ent = entropy(a, b)

            return -(likeli.sum() + prior.sum() + ent.sum())

        def df(theta):
            a, b = np.exp(theta[:self.L]), np.exp(theta[-self.L:])
            logEexp = comp_log_exp(a[:, np.newaxis],
                                   b[:, np.newaxis], self.U)

            tmp = self.U / b[:, np.newaxis]
            log_term, inv_term = np.empty_like(tmp), np.empty_like(tmp)
            idx = (tmp > -1)
            # log(1 + x) is better approximated as x if x is sufficiently small
            idx_dir = np.logical_and(idx, np.abs(tmp) > 1e-12)
            idx_app = (np.abs(tmp) <= 1e-12)
            log_term[idx_dir] = np.log(1. + tmp[idx_dir])
            log_term[idx_app] = tmp[idx_app]
            log_term[-idx] = -np.inf
            inv_term[idx], inv_term[-idx] = 1. / (1. + tmp[idx]), np.inf

            grad_a = np.sum(self.W[t] * np.exp(-self.reverb) * log_term *
                            np.exp(np.sum(logEexp, axis=0)) * self.gamma -
                            self.U / b[:, np.newaxis] * self.gamma, axis=1)
            grad_a = grad_a + (self.alpha - a) * special.polygamma(1, a)
            grad_a = grad_a + 1 - self.alpha / b
            grad_b = a / b**2 * np.sum(-self.U * self.W[t] * np.exp(-self.reverb) * inv_term *
                                       np.exp(np.sum(logEexp, axis=0)) *
                                       self.gamma + self.U * self.gamma,
                                       axis=1)
            grad_b = grad_b + self.alpha * (a / b**2 - 1. / b)
            return -np.hstack((a * grad_a, b * grad_b))

        theta0 = np.hstack((np.log(self.a[t]), np.log(self.b[t])))
        theta_hat, _, d = optimize.fmin_l_bfgs_b(f, theta0, fprime=df,
                                                 maxiter=maxiter, disp=0)
        if disp and d['warnflag']:
            if d['warnflag'] == 2:
                print 'A[{}, :]: {}, f={}'.format(t,
                                                  d['task'],
                                                  f(theta_hat))
            else:
                print 'A[{}, :]: {}, f={}'.format(t,
                                                  d['warnflag'],
                                                  f(theta_hat))
            app_grad = approx_grad(f, theta_hat)
            ana_grad = df(theta_hat)
            for l in xrange(self.L):
                print_gradient('log_a[{}, {:3d}]'.format(t, l),
                               theta_hat[l],
                               ana_grad[l],
                               app_grad[l])
                print_gradient('log_b[{}, {:3d}]'.format(t, l),
                               theta_hat[l + self.L],
                               ana_grad[l + self.L],
                               app_grad[l + self.L])

        self.a[t], self.b[t] = np.exp(theta_hat[:self.L]), np.exp(
            theta_hat[-self.L:])
        assert(np.all(self.a[t] > 0))
        assert(np.all(self.b[t] > 0))
        self.EA[t], self.ElogA[t] = comp_expect(self.a[t], self.b[t])

    def vb_m(self, batch=False, maxiter=15000, verbose=True, disp=0):
        """ Perform one M-step, update the model parameters with A fixed
        from E-step

        Parameters
        ----------
        batch: bool
            Update U as a whole optimization if true. Otherwise, update U
            across different basis.
        verbose: bool
            Output log if ture.
        disp: int
            Display warning from solver if > 0, mostly from LBFGS.
        update_alpha: bool
            Update alpha if true.

        """

        print 'Variational M-step...'
        if verbose:
            old_reverb = self.reverb.copy()
            last_score = self.bound()
            print('Update (initial)\tObj: {:.2f}'.format(last_score))
            start_t = time.time()
        if batch:
            for f in xrange(self.F):
                self.update_reverb_batch(f, maxiter, disp)
                score = self.bound()
                if score < last_score:
                    print('Oops, before: {}\tafter: {}\tf={}'.format(
                        last_score, score, f))
                last_score = score
        else:
            self.update_reverb()
            if verbose:
                score = self.bound()
                print_increment('reverb', last_score, score)
                last_score = score
        if verbose:
            t = time.time() - start_t
            reverb_diff = np.mean(np.abs(self.reverb - old_reverb))
            print('reverb diff: {:.4f}\ttime: {:.2f}'.format(reverb_diff, t))

    def update_reverb(self):
        Eres = 0.
        for l in xrange(self.L):
            Eres = Eres + comp_log_exp(self.a[:, l, np.newaxis],
                                       self.b[:, l, np.newaxis],
                                       self.U[l])
        Eres = np.exp(Eres)
        self.reverb = np.log(np.mean(self.W * Eres, axis=0))

    #def update_u(self, l, maxiter, disp):
    #    def f(u):
    #        Eexp = np.exp(comp_log_exp(self.a[:, l, np.newaxis],
    #                                   self.b[:, l, np.newaxis], u))
    #        return np.sum(np.outer(self.EA[:, l], u) + self.W * Eexp * Eres)

    #    def df(u):
    #        tmp = np.exp(comp_log_exp(self.a[:, l, np.newaxis] + 1.,
    #                                  self.b[:, l, np.newaxis], u))
    #        return np.sum(self.EA[:, l, np.newaxis] *
    #                      (1 - self.W * Eres * tmp), axis=0)

    #    k_idx = np.delete(np.arange(self.L), l)
    #    Eres = 0.
    #    for k in k_idx:
    #        Eres = Eres + comp_log_exp(self.a[:, k, np.newaxis],
    #                                   self.b[:, k, np.newaxis],
    #                                   self.U[k])
    #    Eres = np.exp(Eres)

    #    u0 = self.U[l]
    #    self.U[l], _, d = optimize.fmin_l_bfgs_b(f, u0, fprime=df,
    #                                             maxiter=maxiter, disp=0)
    #    if disp and d['warnflag']:
    #        if d['warnflag'] == 2:
    #            print 'U[{}, :]: {}, f={}'.format(l, d['task'],
    #                                              f(self.U[l]))
    #        else:
    #            print 'U[{}, :]: {}, f={}'.format(l, d['warnflag'],
    #                                              f(self.U[l]))
    #        app_grad = approx_grad(f, self.U[l])
    #        ana_grad = df(self.U[l])
    #        for fr in xrange(self.F):
    #            print_gradient('U[{}, {:3d}]'.format(l, fr), self.U[l, fr],
    #                           ana_grad[fr], app_grad[fr])

    def bound(self):
        Eexp = 0.
        for l in xrange(self.L):
            Eexp = Eexp + comp_log_exp(self.a[:, l, np.newaxis],
                                       self.b[:, l, np.newaxis],
                                       self.U[l])
        Eexp = np.exp(Eexp)
        # E[log P(w|a)]
        bound = self.T * np.sum(self.gamma * np.log(self.gamma) -
                                special.gammaln(self.gamma))
        bound = bound + np.sum(-self.gamma * (np.dot(self.EA, self.U) + self.reverb) +
                               (self.gamma - 1) * np.log(self.W) -
                               self.W * np.exp(-self.reverb) * Eexp * self.gamma)
        # E[log P(a)]
        bound = bound + self.T * np.sum(self.alpha * np.log(self.alpha) -
                                        special.gammaln(self.alpha))
        bound = bound + np.sum(self.ElogA * (self.alpha - 1) -
                               self.EA * self.alpha)
        # E[loq q(a)]
        bound = bound + np.sum(entropy(self.a, self.b))
        return bound

    ## This function is deprecated
    #def comp_exp_expect(self, alpha, beta, U):
    #    ''' Compute E[exp(-au)] where a ~ Gamma(alpha, beta) and u constant

    #    This function makes extensive use of broadcasting, thus the dimension
    #    of input arguments can only be one of the following two situations:
    #         1) U has shape (L, F), alpha and beta have shape (L, 1)
    #            --> output shape (L, F)
    #         2) U has shape (F, ), alpha and beta have shape (T, 1)
    #            --> output shape (T, F)
    #    '''
    #    # using Taylor expansion for large alpha (hence beta) to more
    #    # accurately compute (1 + u/beta)**(-alpha)
    #    idx = np.logical_and(alpha < 1e10, beta < 1e10).ravel()
    #    if alpha.size == self.L:
    #        expect = np.empty_like(U)
    #        expect[idx] = (1 + U[idx] / beta[idx])**(-alpha[idx])
    #        expect[-idx] = np.exp(-U[-idx] * alpha[-idx] / beta[-idx])
    #    elif alpha.size == self.T:
    #        expect = np.empty((self.T, self.F))
    #        expect[idx] = (1 + U / beta[idx])**(-alpha[idx])
    #        expect[-idx] = np.exp(-U * alpha[-idx] / beta[-idx])
    #    else:
    #        raise ValueError('wrong dimension')
    #    expect[U <= -beta] = np.inf
    #    return expect

def print_gradient(name, val, grad, approx):
    print('{} = {:.2f}\tGradient: {:.2f}\tApprox: {:.2f}\t'
          '| Diff |: {:.3f}'.format(name, val, grad, approx,
                                    np.abs(grad - approx)))

def print_increment(name, last_score, score):
    diff_str = '+' if score > last_score else '-'
    print('Update ({})\tBefore: {:.2f}\tAfter: {:.2f}\t{}'.format(
        name, last_score, score, diff_str))

def comp_expect(alpha, beta):
    return (alpha / beta, special.psi(alpha) - np.log(beta))

def entropy(alpha, beta):
    ''' Compute the entropy of a r.v. theta ~ Gamma(alpha, beta)
    '''
    return (alpha - np.log(beta) + special.gammaln(alpha) +
            (1 - alpha) * special.psi(alpha))

def comp_log_exp(alpha, beta, U):
    ''' Compute log(E[exp(-au)]) where a ~ Gamma(alpha, beta) and u as a
    constant:
        log(E[exp(-au)]) = -alpha * log(1 + u / beta)
    Like self.comp_exp_expect (deprecated), this function makes extensive
    use of broadcasting. Therefore, the dimension of the input arguments
    (at least by design) can only be one of the following two situations:
        1) U: (L, F)    alpha, beta: (L, 1)
            --> output: (L, F)
        2) U: (F, )     alpha, beta: (T, 1)
            --> oupput: (T, F)
    '''
    tmp = U / beta
    log_exp = np.empty_like(tmp)
    idx = (tmp > -1)
    # log(1 + x) is better approximated as x if x is sufficiently small
    # otherwise, it can be directly computed
    idx_dir = np.logical_and(idx, np.abs(tmp) > 1e-12)
    idx_app = (np.abs(tmp) <= 1e-12)
    log_exp[idx_dir] = (-alpha * np.log(1. + tmp))[idx_dir]
    log_exp[idx_app] = (-alpha * tmp)[idx_app]
    log_exp[-idx] = np.inf
    return log_exp

def approx_grad(f, x, delta=1e-8, args=()):
    x = np.asarray(x).ravel()
    grad = np.zeros_like(x)
    diff = delta * np.eye(x.size)
    for i in xrange(x.size):
        grad[i] = (f(x + diff[i], *args) - f(x - diff[i], *args)) / (2 * delta)
    return grad
<commit_msg>ICASSP tag<commit_after>"""

Decoloration model based PoF

CREATED: 2013-07-12 11:09:44 by Dawen Liang <daliang@adobe.com>

"""

import time
import sys

import numpy as np
import scipy.optimize as optimize
import scipy.special as special

class SF_Dict(object):
    def __init__(self, W, U, alpha, gamma, L=10, smoothness=100,
                 flat_init=True, seed=None):
        self.W = W.copy()
        self.T, self.F = W.shape
        self.L = L
        if seed is None:
            print 'Using random seed'
            np.random.seed()
        else:
            print 'Using fixed seed {}'.format(seed)
            np.random.seed(seed)
        self.U, self.alpha, self.gamma = U, alpha, gamma
        self.reverb = np.random.randn(self.F)
        self._init_variational(smoothness, flat_init)

    def _init_variational(self, smoothness, flat_init):
        if flat_init:
            self.a = smoothness * np.random.gamma(smoothness,
                                                  1. / smoothness,
                                                  size=(self.T, self.L))
            self.b = smoothness * np.random.gamma(smoothness,
                                                  1. / smoothness,
                                                  size=(self.T, self.L))
        else:
            EA = np.exp(special.psi(self.alpha) - np.log(self.alpha))
            self.a = smoothness * np.ones((self.T, self.L))
            self.b = smoothness * np.ones((self.T, self.L)) / EA
        self.EA, self.ElogA = comp_expect(self.a, self.b)

    def vb_e(self, cold_start=False, smoothness=100, maxiter=15000,
             verbose=True, disp=0):
        """ Perform one variational E-step to appxorimate the posterior
        P(A | -)

        Parameters
        ----------
        cold_start: bool
            Do e-step with fresh start, otherwise just do e-step with
            previous values as initialization.
        smoothness: float
            Smootheness of the variational initialization, larger value will
            lead to more concentrated initialization.
        verbose: bool
            Output log if true.
        disp: int
            Display warning from solver if > 0, mainly from LBFGS.

        """
        print 'Variational E-step...'
        if cold_start:
            # re-initialize all the variational parameters
            self._init_variational(smoothness)

        if verbose:
            last_score = self.bound()
            print('Update (initial)\tObj: {:.2f}'.format(last_score))
            start_t = time.time()
        for t in xrange(self.T):
            #last_score = self.bound()
            self.update_theta_batch(t, maxiter, disp)
            #score = self.bound()
            #if score < last_score:
            #    print('Oops, before: {}\tafter: {}\tt={}'.format(
            #        last_score, score, t))
            if verbose and not t % 20:
                sys.stdout.write('.')
        if verbose:
            t = time.time() - start_t
            sys.stdout.write('\n')
            print 'Batch update\ttime: {:.2f}'.format(t)
            score = self.bound()
            print_increment('A', last_score, score)

    def update_theta_batch(self, t, maxiter, disp):
        def f(theta):
            a, b = np.exp(theta[:self.L]), np.exp(theta[-self.L:])
            Ea, Eloga = comp_expect(a, b)
            logEexp = comp_log_exp(a[:, np.newaxis],
                                   b[:, np.newaxis], self.U)
            likeli = -(self.W[t] * np.exp(np.sum(logEexp, axis=0) - self.reverb)
                       + np.dot(Ea, self.U)) * self.gamma
            prior = (self.alpha - 1) * Eloga - self.alpha * Ea
            ent = entropy(a, b)

            return -(likeli.sum() + prior.sum() + ent.sum())

        def df(theta):
            a, b = np.exp(theta[:self.L]), np.exp(theta[-self.L:])
            logEexp = comp_log_exp(a[:, np.newaxis],
                                   b[:, np.newaxis], self.U)

            tmp = self.U / b[:, np.newaxis]
            log_term, inv_term = np.empty_like(tmp), np.empty_like(tmp)
            idx = (tmp > -1)
            # log(1 + x) is better approximated as x if x is sufficiently small
            idx_dir = np.logical_and(idx, np.abs(tmp) > 1e-12)
            idx_app = (np.abs(tmp) <= 1e-12)
            log_term[idx_dir] = np.log(1. + tmp[idx_dir])
            log_term[idx_app] = tmp[idx_app]
            log_term[-idx] = -np.inf
            inv_term[idx], inv_term[-idx] = 1. / (1. + tmp[idx]), np.inf

            grad_a = np.sum(self.W[t] * np.exp(-self.reverb) * log_term *
                            np.exp(np.sum(logEexp, axis=0)) * self.gamma -
                            self.U / b[:, np.newaxis] * self.gamma, axis=1)
            grad_a = grad_a + (self.alpha - a) * special.polygamma(1, a)
            grad_a = grad_a + 1 - self.alpha / b
            grad_b = a / b**2 * np.sum(-self.U * self.W[t] * np.exp(-self.reverb) * inv_term *
                                       np.exp(np.sum(logEexp, axis=0)) *
                                       self.gamma + self.U * self.gamma,
                                       axis=1)
            grad_b = grad_b + self.alpha * (a / b**2 - 1. / b)
            return -np.hstack((a * grad_a, b * grad_b))

        theta0 = np.hstack((np.log(self.a[t]), np.log(self.b[t])))
        theta_hat, _, d = optimize.fmin_l_bfgs_b(f, theta0, fprime=df,
                                                 maxiter=maxiter, disp=0)
        if disp and d['warnflag']:
            if d['warnflag'] == 2:
                print 'A[{}, :]: {}, f={}'.format(t,
                                                  d['task'],
                                                  f(theta_hat))
            else:
                print 'A[{}, :]: {}, f={}'.format(t,
                                                  d['warnflag'],
                                                  f(theta_hat))
            app_grad = approx_grad(f, theta_hat)
            ana_grad = df(theta_hat)
            for l in xrange(self.L):
                print_gradient('log_a[{}, {:3d}]'.format(t, l),
                               theta_hat[l],
                               ana_grad[l],
                               app_grad[l])
                print_gradient('log_b[{}, {:3d}]'.format(t, l),
                               theta_hat[l + self.L],
                               ana_grad[l + self.L],
                               app_grad[l + self.L])

        self.a[t], self.b[t] = np.exp(theta_hat[:self.L]), np.exp(
            theta_hat[-self.L:])
        assert(np.all(self.a[t] > 0))
        assert(np.all(self.b[t] > 0))
        self.EA[t], self.ElogA[t] = comp_expect(self.a[t], self.b[t])

    def vb_m(self, batch=False, maxiter=15000, verbose=True, disp=0):
        """ Perform one M-step, update the model parameters with A fixed
        from E-step

        Parameters
        ----------
        batch: bool
            Update U as a whole optimization if true. Otherwise, update U
            across different basis.
        verbose: bool
            Output log if ture.
        disp: int
            Display warning from solver if > 0, mostly from LBFGS.
        update_alpha: bool
            Update alpha if true.

        """

        print 'Variational M-step...'
        if verbose:
            old_reverb = self.reverb.copy()
            last_score = self.bound()
            print('Update (initial)\tObj: {:.2f}'.format(last_score))
            start_t = time.time()
        if batch:
            for f in xrange(self.F):
                self.update_reverb_batch(f, maxiter, disp)
                score = self.bound()
                if score < last_score:
                    print('Oops, before: {}\tafter: {}\tf={}'.format(
                        last_score, score, f))
                last_score = score
        else:
            self.update_reverb()
            if verbose:
                score = self.bound()
                print_increment('reverb', last_score, score)
                last_score = score
        if verbose:
            t = time.time() - start_t
            reverb_diff = np.mean(np.abs(self.reverb - old_reverb))
            print('reverb diff: {:.4f}\ttime: {:.2f}'.format(reverb_diff, t))

    def update_reverb(self):
        Eres = 0.
        for l in xrange(self.L):
            Eres = Eres + comp_log_exp(self.a[:, l, np.newaxis],
                                       self.b[:, l, np.newaxis],
                                       self.U[l])
        Eres = np.exp(Eres)
        self.reverb = np.log(np.mean(self.W * Eres, axis=0))

    def bound(self):
        Eexp = 0.
        for l in xrange(self.L):
            Eexp = Eexp + comp_log_exp(self.a[:, l, np.newaxis],
                                       self.b[:, l, np.newaxis],
                                       self.U[l])
        Eexp = np.exp(Eexp)
        # E[log P(w|a)]
        bound = self.T * np.sum(self.gamma * np.log(self.gamma) -
                                special.gammaln(self.gamma))
        bound = bound + np.sum(-self.gamma * (np.dot(self.EA, self.U) + self.reverb) +
                               (self.gamma - 1) * np.log(self.W) -
                               self.W * np.exp(-self.reverb) * Eexp * self.gamma)
        # E[log P(a)]
        bound = bound + self.T * np.sum(self.alpha * np.log(self.alpha) -
                                        special.gammaln(self.alpha))
        bound = bound + np.sum(self.ElogA * (self.alpha - 1) -
                               self.EA * self.alpha)
        # E[loq q(a)]
        bound = bound + np.sum(entropy(self.a, self.b))
        return bound

    ## This function is deprecated
    #def comp_exp_expect(self, alpha, beta, U):
    #    ''' Compute E[exp(-au)] where a ~ Gamma(alpha, beta) and u constant

    #    This function makes extensive use of broadcasting, thus the dimension
    #    of input arguments can only be one of the following two situations:
    #         1) U has shape (L, F), alpha and beta have shape (L, 1)
    #            --> output shape (L, F)
    #         2) U has shape (F, ), alpha and beta have shape (T, 1)
    #            --> output shape (T, F)
    #    '''
    #    # using Taylor expansion for large alpha (hence beta) to more
    #    # accurately compute (1 + u/beta)**(-alpha)
    #    idx = np.logical_and(alpha < 1e10, beta < 1e10).ravel()
    #    if alpha.size == self.L:
    #        expect = np.empty_like(U)
    #        expect[idx] = (1 + U[idx] / beta[idx])**(-alpha[idx])
    #        expect[-idx] = np.exp(-U[-idx] * alpha[-idx] / beta[-idx])
    #    elif alpha.size == self.T:
    #        expect = np.empty((self.T, self.F))
    #        expect[idx] = (1 + U / beta[idx])**(-alpha[idx])
    #        expect[-idx] = np.exp(-U * alpha[-idx] / beta[-idx])
    #    else:
    #        raise ValueError('wrong dimension')
    #    expect[U <= -beta] = np.inf
    #    return expect

def print_gradient(name, val, grad, approx):
    print('{} = {:.2f}\tGradient: {:.2f}\tApprox: {:.2f}\t'
          '| Diff |: {:.3f}'.format(name, val, grad, approx,
                                    np.abs(grad - approx)))

def print_increment(name, last_score, score):
    diff_str = '+' if score > last_score else '-'
    print('Update ({})\tBefore: {:.2f}\tAfter: {:.2f}\t{}'.format(
        name, last_score, score, diff_str))

def comp_expect(alpha, beta):
    return (alpha / beta, special.psi(alpha) - np.log(beta))

def entropy(alpha, beta):
    ''' Compute the entropy of a r.v. theta ~ Gamma(alpha, beta)
    '''
    return (alpha - np.log(beta) + special.gammaln(alpha) +
            (1 - alpha) * special.psi(alpha))

def comp_log_exp(alpha, beta, U):
    ''' Compute log(E[exp(-au)]) where a ~ Gamma(alpha, beta) and u as a
    constant:
        log(E[exp(-au)]) = -alpha * log(1 + u / beta)
    Like self.comp_exp_expect (deprecated), this function makes extensive
    use of broadcasting. Therefore, the dimension of the input arguments
    (at least by design) can only be one of the following two situations:
        1) U: (L, F)    alpha, beta: (L, 1)
            --> output: (L, F)
        2) U: (F, )     alpha, beta: (T, 1)
            --> oupput: (T, F)
    '''
    tmp = U / beta
    log_exp = np.empty_like(tmp)
    idx = (tmp > -1)
    # log(1 + x) is better approximated as x if x is sufficiently small
    # otherwise, it can be directly computed
    idx_dir = np.logical_and(idx, np.abs(tmp) > 1e-12)
    idx_app = (np.abs(tmp) <= 1e-12)
    log_exp[idx_dir] = (-alpha * np.log(1. + tmp))[idx_dir]
    log_exp[idx_app] = (-alpha * tmp)[idx_app]
    log_exp[-idx] = np.inf
    return log_exp

def approx_grad(f, x, delta=1e-8, args=()):
    x = np.asarray(x).ravel()
    grad = np.zeros_like(x)
    diff = delta * np.eye(x.size)
    for i in xrange(x.size):
        grad[i] = (f(x + diff[i], *args) - f(x - diff[i], *args)) / (2 * delta)
    return grad
<|endoftext|>


<commit_before><commit_msg>Apparently the tests still run in Python 2?<commit_after><|endoftext|>


<commit_before># coding=utf-8

"""This module, views.py, defines server HTTP request responses."""

# Needed to send back a rendered HTML page.
from django.shortcuts import render
# Needed for sending a simple HttpResponse such as a string response.
from django.http import HttpResponse
# Needed for allowing POST requests without requiring a CSRF token.
from django.views.decorators.csrf import csrf_exempt
# Needed to perform HttpRequests to run Locust.
import requests
# Needed for making JsonResponses.
from django.http import JsonResponse

#from quasar_source_code.entities import entity_local_testing
# Ignore the IDE error message.
#from entities import entity_local_testing

# Define all the pages.
_TEMPLATES_BASE         = 'templates/quasar_web_server/'
TEMPLATE_HELLO_WORLD    = _TEMPLATES_BASE + 'hello_world.html'
TEMPLATE_LOG_FORMULAS   = _TEMPLATES_BASE + 'log_formulas.html'
TEMPLATE_QUICK_INFO     = _TEMPLATES_BASE + 'quick_info.html'
TEMPLATE_MATH_220       = _TEMPLATES_BASE + 'math220.html'
TEMPLATE_MATH_310       = _TEMPLATES_BASE + 'math310.html'
TEMPLATE_CS_361         = _TEMPLATES_BASE + 'cs361.html'
TEMPLATE_CS_425         = _TEMPLATES_BASE + 'cs425.html'
TEMPLATE_WEB_SOCKET     = _TEMPLATES_BASE + '/web_socket_testing/web_sockets.html'

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

def GET_hello_world(request):
	"""Temporary main page."""
	return render(request, TEMPLATE_HELLO_WORLD)

def GET_log_formulas(request):
	"""Returns the HTML page for log formulas."""
	return render(request, TEMPLATE_LOG_FORMULAS)

'''
def GET_quick_info(request):
	"""Returns a temporary page."""

	entity_manager = entity_local_testing.entity_manager

	return render(request, TEMPLATE_QUICK_INFO, {'time_blocks': entity_manager.print_todays_relevant_information()})
'''

def GET_math_220(request):
	"""Returns notes for diff eq."""
	return render(request, TEMPLATE_MATH_220)

def GET_math_310(request):
	"""Returns notes for math 310."""
	return render(request, TEMPLATE_MATH_310)

def GET_cs_361(request):
	"""Returns notes for cs 361."""
	return render(request, TEMPLATE_CS_361)

def GET_cs_425(request):
	"""Returns notes for cs 425."""
	return render(request, TEMPLATE_CS_425)

def GET_web_socket(request):
	"""TEMP test page."""
	return render(request, TEMPLATE_WEB_SOCKET)
<commit_msg>Django updating 25<commit_after># coding=utf-8

"""This module, views.py, defines server HTTP request responses."""

# Needed to send back a rendered HTML page.
from django.shortcuts import render
# Needed for sending a simple HttpResponse such as a string response.
from django.http import HttpResponse
# Needed for allowing POST requests without requiring a CSRF token.
from django.views.decorators.csrf import csrf_exempt
# Needed to perform HttpRequests to run Locust.
import requests
# Needed for making JsonResponses.
from django.http import JsonResponse

#from quasar_source_code.entities import entity_local_testing
# Ignore the IDE error message.
#from entities import entity_local_testing

# Define all the pages.
_TEMPLATES_BASE         = 'templates/quasar_web_server/'
TEMPLATE_HELLO_WORLD    = _TEMPLATES_BASE + 'hello_world.html'
TEMPLATE_LOG_FORMULAS   = _TEMPLATES_BASE + 'log_formulas.html'
TEMPLATE_QUICK_INFO     = _TEMPLATES_BASE + 'quick_info.html'
TEMPLATE_MATH_220       = _TEMPLATES_BASE + 'math220.html'
TEMPLATE_MATH_310       = _TEMPLATES_BASE + 'math310.html'
TEMPLATE_CS_361         = _TEMPLATES_BASE + 'cs361.html'
TEMPLATE_CS_425         = _TEMPLATES_BASE + 'cs425.html'
TEMPLATE_WEB_SOCKET     = _TEMPLATES_BASE + '/web_socket_servers/web_sockets.html'

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

def GET_hello_world(request):
	"""Temporary main page."""
	return render(request, TEMPLATE_HELLO_WORLD)

def GET_log_formulas(request):
	"""Returns the HTML page for log formulas."""
	return render(request, TEMPLATE_LOG_FORMULAS)

'''
def GET_quick_info(request):
	"""Returns a temporary page."""

	entity_manager = entity_local_testing.entity_manager

	return render(request, TEMPLATE_QUICK_INFO, {'time_blocks': entity_manager.print_todays_relevant_information()})
'''

def GET_math_220(request):
	"""Returns notes for diff eq."""
	return render(request, TEMPLATE_MATH_220)

def GET_math_310(request):
	"""Returns notes for math 310."""
	return render(request, TEMPLATE_MATH_310)

def GET_cs_361(request):
	"""Returns notes for cs 361."""
	return render(request, TEMPLATE_CS_361)

def GET_cs_425(request):
	"""Returns notes for cs 425."""
	return render(request, TEMPLATE_CS_425)

def GET_web_socket(request):
	"""TEMP test page."""
	return render(request, TEMPLATE_WEB_SOCKET)
<|endoftext|>


<commit_before>#!/usr/bin/env python

"""
@brief A class for testing if a point is inside a shape
@author pletzer@psu.edu
"""

import numpy

class Inside:

    def __init__(self, shape):
        """
        Constructor
        @param shape instance of Shape
        @param polys connectivty
        """
        points, polys, count = shape.csg.toVerticesAndPolygons()
        # must have at least one point
        self.ndims = len(points[0])

        # find the box corners
        self.xmins = numpy.array([min([p[i] for p in points]) for i in range(self.ndims)])
        self.xmaxs = numpy.array([max([p[i] for p in points]) for i in range(self.ndims)])

        self.eps = 1.23456789e-14

        self.polys = polys
        self.points = [numpy.array(p) for p in points]

        # matrix and right and side vector
        self.mat = numpy.zeros((self.ndims, self.ndims), numpy.float64)
        self.b = numpy.zeros((self.ndims,), numpy.float64)

        # the optimal direction for shooting the ray
        self.direction = float('inf') * \
            numpy.ones((self.ndims,), numpy.float64)

    def isInside(self, point):
        """
        Determine if a point is inside the shape
        @param point point
        @return True if inside, False otherwise
        """

        # quick check, point must be inside box
        outsideBox = reduce(lambda x, y: x or y,
                            [(point[i] < self.xmins[i] - self.eps) or
                             (point[i] > self.xmaxs[i] + self.eps)
                             for i in range(self.ndims)])

        if outsideBox:
            return False

        # any direction will do but things will run faster if the direction
        # points to the nearest domain box face (fewer intersections to
        #  compute)
        self.computeOptimalDirection(point)

        # set the first column in our matrix (independent of the poly)
        self.mat[:, 0] = self.direction

        # compute the number of intersections between the ray and the polys
        numberOfIntersections = 0
        for poly in self.polys:

            triangle = 1
            numPoints = len(poly)
            if numPoints == 4:
                triangle = 0
            # compute the overlap betwen the ray box and the face box
            if not self.areBoxesOverlapping(point, poly):
                # intersection is impossible, don't bother...
                continue

            lmbda, xis = self.computeIntersection(point, poly)

            if lmbda > -self.eps:

                # the direction is ok

                numPoints = len(poly)
                sums = 0.0
                rayIntersects = True
                for i in range(len(xis)):
                    rayIntersects &= (xis[i] > -self.eps)
                    rayIntersects &= (xis[i] < 1 - triangle*sums - self.eps)
                    sums += xis[i]
                if rayIntersects:
                    numberOfIntersections += 1

        # even number is outside (False), odd number means inside (True)
        return (numberOfIntersections % 2)

    def areBoxesOverlapping(self, point, poly):

        for i in range(self.ndims):

            xminFace = min([self.points[j][i] for j in poly])
            xmaxFace = max([self.points[j][i] for j in poly])

            p, d = point[i], self.direction[i]
            xminRay = min(p, p + d)
            xmaxRay = max(p, p + d)

            if (xmaxRay < xminFace + self.eps) or \
                    (xmaxFace < xminRay + self.eps):
                    # no overlap
                    return False
        return True

    def computeOptimalDirection(self, point):
        """
        Compute the direction of the ray to the nearest
        domain box face. This will update self.direction
        @param point starting point of the ray
        """
        # iterate over the faces of the box then find the minimum
        # distance between the point and the face.
        minDistance = float('inf')
        for pm in (-1, 1):
            pls = (1 + pm)/2.
            mns = (1 - pm)/2.
            for axis in range(self.ndims):
                # the normal vector contains very small values in place of
                # zeros in order to avoid issues with ray hitting
                # exactly a node
                normal = 100 * self.eps * numpy.array([1 for i in range(self.ndims)])
                normal[axis] = pm
                distance = pls*(self.xmaxs[axis] -
                                point[axis]) + mns*(point[axis] -
                                                    self.xmins[axis])
                if distance < minDistance:
                    # expand a little beyond the domain (1.1)
                    self.direction = normal * (1.1 * distance)
                    minDistance = distance

    def computeIntersection(self, point, poly):
        """
        Compute the intersection with either a triangle or a quadrilateral
        @param point starting point of the ray
        @param poly either a triangle or quad
        """
        ip0 = poly[0]
        self.b = self.points[ip0] - point
        self.mat[:, 1] = self.points[ip0] - self.points[poly[1]]
        self.mat[:, 2] = self.points[ip0] - self.points[poly[-1]]
        solution = numpy.linalg.solve(self.mat, self.b)
        return solution[0], solution[1:]

##############################################################################
def test():
    from icqsol.tools.geometry.icqSphere import Sphere
    shp = Sphere(origin=(0., 0., 0.), radius=1.0, n_theta=6, n_phi=3)
    inside = Inside(shp)
    pt = numpy.array([0., 0., 0.])
    assert(inside.isInside(pt))

if __name__ == '__main__':
    test()
<commit_msg> working on improving the reliability of icqInside<commit_after>#!/usr/bin/env python

"""
@brief A class for testing if a point is inside a shape
@author pletzer@psu.edu
"""

import numpy

class Inside:

    def __init__(self, shape):
        """
        Constructor
        @param shape instance of Shape
        @param polys connectivty
        """
        points, polys, count = shape.csg.toVerticesAndPolygons()
        # must have at least one point
        self.ndims = len(points[0])

        # find the box corners
        self.xmins = numpy.array([min([p[i] for p in points]) for i in range(self.ndims)])
        self.xmaxs = numpy.array([max([p[i] for p in points]) for i in range(self.ndims)])

        self.eps = 1.23456789e-14

        self.polys = polys
        self.points = [numpy.array(p) for p in points]

        # matrix and right and side vector
        self.mat = numpy.zeros((self.ndims, self.ndims), numpy.float64)
        self.b = numpy.zeros((self.ndims,), numpy.float64)

        # the optimal direction for shooting the ray
        self.direction = float('inf') * \
            numpy.ones((self.ndims,), numpy.float64)

    def isInside(self, point):
        """
        Determine if a point is inside the shape
        @param point point
        @return +1 if inside, -1 if outside, and 0 if indefinite
        """

        # quick check, point must be inside box
        outsideBox = reduce(lambda x, y: x or y,
                            [(point[i] < self.xmins[i] - self.eps) or
                             (point[i] > self.xmaxs[i] + self.eps)
                             for i in range(self.ndims)])

        if outsideBox:
            return -1

        # any direction will do but things will run faster if the direction
        # points to the nearest domain box face (fewer intersections to
        #  compute)
        self.computeOptimalDirection(point)

        # set the first column in our matrix (independent of the poly)
        self.mat[:, 0] = self.direction

        # compute the number of intersections between the ray and the polys
        numberOfIntersections = 0
        for poly in self.polys:

            triangle = 1
            numPoints = len(poly)
            if numPoints == 4:
                triangle = 0
            # compute the overlap betwen the ray box and the face box
            if not self.areBoxesOverlapping(point, poly):
                # intersection is impossible, don't bother...
                continue

            lmbda, xis = self.computeIntersection(point, poly)
            print '*** lmbda, xis = ', lmbda, xis

            if lmbda > -self.eps:

                # the direction is ok

                numPoints = len(poly)
                sums = 0.0
                rayIntersects = True
                for i in range(len(xis)):
                    rayIntersects &= (xis[i] > -self.eps)
                    rayIntersects &= (xis[i] < 1 - triangle*sums - self.eps)
                    sums += xis[i]
                if rayIntersects:
                    if abs(lmbda) <= self.eps:
                        # marginal, cannot say
                        return 0
                    else:
                        numberOfIntersections += 1

        # even number is outside, odd number means inside
        print '*** number if intersections = ', numberOfIntersections
        return 2*(numberOfIntersections % 2) - 1

    def areBoxesOverlapping(self, point, poly):

        for i in range(self.ndims):

            xminFace = min([self.points[j][i] for j in poly])
            xmaxFace = max([self.points[j][i] for j in poly])

            p, d = point[i], self.direction[i]
            xminRay = min(p, p + d)
            xmaxRay = max(p, p + d)

            if (xmaxRay < xminFace + self.eps) or \
                    (xmaxFace < xminRay + self.eps):
                    # no overlap
                    return False
        return True

    def computeOptimalDirection(self, point):
        """
        Compute the direction of the ray to the nearest
        domain box face. This will update self.direction
        @param point starting point of the ray
        """
        # iterate over the faces of the box then find the minimum
        # distance between the point and the face.
        minDistance = float('inf')
        for pm in (-1, 1):
            pls = (1 + pm)/2.
            mns = (1 - pm)/2.
            for axis in range(self.ndims):
                # the normal vector contains very small values in place of
                # zeros in order to avoid issues with ray hitting
                # exactly a node
                normal = 100 * self.eps * numpy.array([1 for i in range(self.ndims)])
                normal[axis] = pm
                distance = pls*(self.xmaxs[axis] -
                                point[axis]) + mns*(point[axis] -
                                                    self.xmins[axis])
                if distance < minDistance:
                    # expand a little beyond the domain (1.1)
                    self.direction = normal * (1.1 * distance)
                    minDistance = distance

    def computeIntersection(self, point, poly):
        """
        Compute the intersection with either a triangle or a quadrilateral
        @param point starting point of the ray
        @param poly either a triangle or quad
        """
        ip0 = poly[0]
        self.b = self.points[ip0] - point
        self.mat[:, 1] = self.points[ip0] - self.points[poly[1]]
        self.mat[:, 2] = self.points[ip0] - self.points[poly[-1]]
        solution = numpy.linalg.solve(self.mat, self.b)
        return solution[0], solution[1:]

##############################################################################
def test():

    from icqsol.tools.geometry.icqSphere import Sphere
    shp = Sphere(origin=(0., 0., 0.), radius=1.0, n_theta=6, n_phi=3)
    shp.debug()

    inside = Inside(shp)

    #pt = numpy.array([0., 0., 0.])
    #assert(inside.isInside(pt) == 1)

    #pt = numpy.array([1.01, 0., 0.])
    #assert(inside.isInside(pt) == -1)

    #pt = numpy.array([0., 0.975, 0.])
    pt = numpy.array([0., 0.98, 0.])
    res = inside.isInside(pt)
    print '*** res = ', res
    #assert(inside.isInside(pt) == 0)

if __name__ == '__main__':
    test()
<|endoftext|>


<commit_before>from docxUtils.reports import DOCXReport
from docxUtils.tables import TableMaker

class NtpAnimalBias(DOCXReport):
    """
    NTP animal potential bias report.
    """

    def add_rr_header(self, tbl, row, header):
        tbl.new_td_txt(row, 0, header)
        tbl.new_td_txt(row, 1, '')

    def add_rr_row(self, tbl, row, header, rating, rationale):
        tbl.new_td_txt(row, 0, header)
        tbl.new_td_txt(row, 1, u'{}\n{}'.format(rating, rationale))

    def build_tbl(self, study):
        colWidths = (2, 4.5)
        tbl = TableMaker(colWidths, numHeaders=1, firstRowCaption=False,
                         tblStyle="ntpTbl")

        # Table caption
        txt = u'Table X: {} ({}): {}: {}'.format(
            study['reference']['name'],
            study['species'],
            study['agent'],
            study['dosingRoute'],
        )
        self.doc.add_heading(txt, level=2)

        # write header
        tbl.new_th(0, 0, 'Study utility domain\nQuestion')
        tbl.new_th(0, 1, 'Rating\nRationale')

        # study design section
        self.add_rr_header(tbl, 1, 'Study design')

        self.add_rr_row(
            tbl, 2, 'Randomization',
            study['randomizationRating'],
            study['randomizationRationale'])

        self.add_rr_row(
            tbl, 3, 'Controls',
            study['concurrentControlsRating'],
            study['concurrentControlsRationale'])

        tbl.new_td_txt(4, 0, 'Historical data')
        response = 'Yes' if study['historicalDataAvailable'] is True else 'No'
        tbl.new_td_txt(4, 1, response)

        # exposure section
        self.add_rr_header(tbl, 5, 'Exposure')

        self.add_rr_row(
            tbl, 6, 'Chemical characterization',
            study['chemicalCharacterizationRating'],
            study['chemicalCharacterizationRationale'])

        self.add_rr_row(
            tbl, 7, 'Dosing regimen',
            study['concurrentControlsRating'],
            study['concurrentControlsRationale'])

        # outcome section
        self.add_rr_header(tbl, 8, 'Outcome')

        self.add_rr_row(
            tbl, 9, 'Pathology',
            study['outcomeMethodsRating'],
            study['outcomeMethodsRationale'])

        self.add_rr_row(
            tbl, 10, 'Consistency between groups',
            study['groupConsistencyRating'],
            study['groupConsistencyRationale'])

        # confounding section
        self.add_rr_header(tbl, 11, 'Confounding')

        self.add_rr_row(
            tbl, 12, 'Confounding',
            study['confoundingRating'],
            study['confoundingRationale'])

        # reporting & analysis section
        self.add_rr_header(tbl, 13, 'Reporting and analysis')

        self.add_rr_row(
            tbl, 14, 'Reporting data and statistics',
            study['statisticalReportingRating'],
            study['statisticalReportingRationale'])

        self.add_rr_row(
            tbl, 15, 'Combining lesions',
            study['tumorCombiningRating'],
            study['tumorCombiningRationale'])

        # sensitivity section
        self.add_rr_header(tbl, 16, 'Sensitivity')

        self.add_rr_row(
            tbl, 17, 'Animal model',
            study['animalModelSensitivityRating'],
            study['animalModelSensitivityRationale'])

        self.add_rr_row(
            tbl, 18, 'Statistical power',
            study['statisticalPowerRating'],
            study['statisticalPowerRationale'])

        self.add_rr_row(
            tbl, 19, 'Exposure duration',
            study['exposureDurationRating'],
            study['exposureDurationRationale'])

        self.add_rr_row(
            tbl, 20, 'Dose response relationships',
            study['doseResponseRating'],
            study['doseResponseRationale'])

        self.add_rr_row(
            tbl, 21, 'Study duration',
            study['durationRating'],
            study['durationRationale'])

        # render table
        tbl.render(self.doc)

        # overall utility
        txt = u'Overall utility: {}\n{}'.format(
            study['overallUtility'],
            study['overallUtilityRationale']
        )
        self.doc.add_paragraph(txt)
        self.doc.add_page_break()

    def create_content(self):
        doc = self.doc
        d = self.context

        txt = u"{} {}: potential bias".format(
            d["table"]["volumeNumber"],
            d["table"]["monographAgent"],
        )
        doc.add_heading(txt, level=1)
        doc.add_paragraph(d["table"]["name"])

        for study in d['studies']:
            self.build_tbl(study)

    def get_template_fn(self):
        return "base.docx"
<commit_msg>revise bias worksheet with AE comments<commit_after>from docxUtils.reports import DOCXReport
from docxUtils.tables import TableMaker

class NtpAnimalBias(DOCXReport):
    """
    NTP animal potential bias report.
    """

    def add_rr_header(self, tbl, row, header):
        tbl.new_th(row, 0, header, colspan=2)

    def add_rr_row(self, tbl, row, header, rating, rationale):
        tbl.new_td_txt(row, 0, header)
        tbl.new_td_txt(row, 1, u'{}\n{}'.format(rating, rationale))

    def build_tbl(self, study):
        colWidths = (2, 4.5)
        tbl = TableMaker(colWidths, numHeaders=1, firstRowCaption=False,
                         tblStyle="ntpTbl")

        # Table caption
        txt = u'Table X: {} ({} {}): {}: {}'.format(
            study['reference']['name'],
            study['sex'],
            study['species'],
            study['agent'],
            study['dosingRoute'],
        )
        self.doc.add_heading(txt, level=2)

        # write header
        tbl.new_th(0, 0, 'Study utility domain\nQuestion')
        tbl.new_th(0, 1, 'Rating\nRationale')

        # study design section
        self.add_rr_header(tbl, 1, 'Study design')

        self.add_rr_row(
            tbl, 2, 'Randomization',
            study['randomizationRating'],
            study['randomizationRationale'])

        self.add_rr_row(
            tbl, 3, 'Controls',
            study['concurrentControlsRating'],
            study['concurrentControlsRationale'])

        tbl.new_td_txt(4, 0, 'Historical data')
        response = 'Yes' if study['historicalDataAvailable'] is True else 'No'
        tbl.new_td_txt(4, 1, response)

        # exposure section
        self.add_rr_header(tbl, 5, 'Exposure')

        self.add_rr_row(
            tbl, 6, 'Chemical characterization',
            study['chemicalCharacterizationRating'],
            study['chemicalCharacterizationRationale'])

        self.add_rr_row(
            tbl, 7, 'Dosing regimen',
            study['dosingRegimenRating'],
            study['dosingRegimenRationale'])

        # outcome section
        self.add_rr_header(tbl, 8, 'Outcome')

        self.add_rr_row(
            tbl, 9, 'Pathology',
            study['outcomeMethodsRating'],
            study['outcomeMethodsRationale'])

        self.add_rr_row(
            tbl, 10, 'Consistency between groups',
            study['groupConsistencyRating'],
            study['groupConsistencyRationale'])

        # confounding section
        self.add_rr_header(tbl, 11, 'Confounding')

        self.add_rr_row(
            tbl, 12, 'Confounding',
            study['confoundingRating'],
            study['confoundingRationale'])

        # reporting & analysis section
        self.add_rr_header(tbl, 13, 'Reporting and analysis')

        self.add_rr_row(
            tbl, 14, 'Reporting data and statistics',
            study['statisticalReportingRating'],
            study['statisticalReportingRationale'])

        self.add_rr_row(
            tbl, 15, 'Combining lesions',
            study['tumorCombiningRating'],
            study['tumorCombiningRationale'])

        # sensitivity section
        self.add_rr_header(tbl, 16, 'Sensitivity')

        self.add_rr_row(
            tbl, 17, 'Animal model',
            study['animalModelSensitivityRating'],
            study['animalModelSensitivityRationale'])

        self.add_rr_row(
            tbl, 18, 'Statistical power',
            study['statisticalPowerRating'],
            study['statisticalPowerRationale'])

        self.add_rr_row(
            tbl, 19, 'Exposure duration',
            study['exposureDurationRating'],
            study['exposureDurationRationale'])

        self.add_rr_row(
            tbl, 20, 'Dose/response',
            study['doseResponseRating'],
            study['doseResponseRationale'])

        self.add_rr_row(
            tbl, 21, 'Study duration',
            study['durationRating'],
            study['durationRationale'])

        # render table
        tbl.render(self.doc)

        # overall utility
        txt = u'Overall utility: {}\n{}'.format(
            study['overallUtility'],
            study['overallUtilityRationale']
        )
        self.doc.add_paragraph(txt)
        self.doc.add_page_break()

    def create_content(self):
        doc = self.doc
        d = self.context

        txt = u"{} {}: potential bias".format(
            d["table"]["volumeNumber"],
            d["table"]["monographAgent"],
        )
        doc.add_heading(txt, level=1)
        doc.add_paragraph(d["table"]["name"])

        for study in d['studies']:
            self.build_tbl(study)

    def get_template_fn(self):
        return "base.docx"
<|endoftext|>


<commit_before>from numpy import meshgrid, linspace, pi, exp, square, sin, cos, abs, sqrt
from numpy import min, max
from numpy.random import rand

def gaborf(size, complex=True, f=None, a=None, s=None, t=None, x=None, y=None, p=None):
	"""
	Creates a 2D Gabor filter. If size is a square integer, a one-dimensional
	array containing the flattened Gabor filter is returned.

	@type  size: int/tuple
	@param size: square integer or tuple determining image size

	@type  complex: boolean
	@param complex: whether to return both real and imaginary parts

	@type  f: float
	@param f: frequency

	@type  a: float
	@param a: aspect ratio

	@type  s: float
	@param s: scale

	@type  t: float
	@param t: angle

	@type  x: float
	@param x: position in horizontal direction

	@type  y: float
	@param y: position in vertical direction

	@type  p: float
	@param p: phase

	@rtype: array
	@return: Gabor filter
	"""

	if isinstance(size, int):
		if abs(sqrt(size) - int(sqrt(size))) > 1E-10:
			raise RuntimeError('Filter size must be square.')

		scale = sqrt(size) / 16.
		xx, yy = meshgrid(
			linspace(-5 * scale, 5 * scale, sqrt(size)),
			linspace(-5 * scale, 5 * scale, sqrt(size)))
	else:
		xx, yy = meshgrid(
			linspace(-5 * size[0] / 16., 5 * size[0] / 16., size[0]),
			linspace(-5 * size[1] / 16., 5 * size[1] / 16., size[1]))

	# angle
	if t is None:
		t = rand() * pi * 2.

	# aspect ratio
	if a is None:
		a = rand() / 2. + 0.75

	# scale
	if s is None:
		s = rand() / 1. + 2.

	# frequency
	if f is None:
		f = rand() * 2. + 2.

	# center
	if x is None:
		x = rand() * (max(xx) - min(xx)) + min(xx)

	if y is None:
		y = rand() * (max(yy) - min(yy)) + min(yy)

	# phase
	if p is None:
		p = rand() * pi * 2.

	xx, yy = xx - x, yy - y
	xx, yy = xx * cos(t) + yy * sin(t), -xx * sin(t) + yy * cos(t)

	G = exp(-square(xx) / s - square(a * yy) / s) * cos(f * xx)

	if complex:
		G = G + 1j * exp(-square(xx) / s - square(a * yy) / s) * sin(f * xx + p)

	if isinstance(size, int):
		return G.flatten()
	return G
<commit_msg>Added function for determining Gabor positition and orientation.<commit_after>from numpy import meshgrid, linspace, pi, exp, square, sin, cos, abs, sqrt
from numpy import min, max, arctan2, argmax
from numpy.random import rand
from numpy.fft import fft2, fftshift

def gaborf(size, complex=True, f=None, a=None, s=None, t=None, x=None, y=None, p=None):
	"""
	Creates a 2D Gabor filter. If size is a square integer, a one-dimensional
	array containing the flattened Gabor filter is returned.

	@type  size: int/tuple
	@param size: square integer or tuple determining image size

	@type  complex: boolean
	@param complex: whether to return both real and imaginary parts

	@type  f: float
	@param f: frequency

	@type  a: float
	@param a: aspect ratio

	@type  s: float
	@param s: scale

	@type  t: float
	@param t: angle

	@type  x: float
	@param x: position in horizontal direction

	@type  y: float
	@param y: position in vertical direction

	@type  p: float
	@param p: phase

	@rtype: array
	@return: Gabor filter
	"""

	if isinstance(size, int):
		if abs(sqrt(size) - int(sqrt(size))) > 1E-10:
			raise RuntimeError('Filter size must be square.')

		scale = sqrt(size) / 16.
		xx, yy = meshgrid(
			linspace(-5 * scale, 5 * scale, sqrt(size)),
			linspace(-5 * scale, 5 * scale, sqrt(size)))
	else:
		xx, yy = meshgrid(
			linspace(-5 * size[0] / 16., 5 * size[0] / 16., size[0]),
			linspace(-5 * size[1] / 16., 5 * size[1] / 16., size[1]))

	# angle
	if t is None:
		t = rand() * pi * 2.

	# aspect ratio
	if a is None:
		a = rand() / 2. + 0.75

	# scale
	if s is None:
		s = rand() / 1. + 2.

	# frequency
	if f is None:
		f = rand() * 2. + 2.

	# center
	if x is None:
		x = rand() * (max(xx) - min(xx)) + min(xx)

	if y is None:
		y = rand() * (max(yy) - min(yy)) + min(yy)

	# phase
	if p is None:
		p = rand() * pi * 2.

	xx, yy = xx - x, yy - y
	xx, yy = xx * cos(t) + yy * sin(t), -xx * sin(t) + yy * cos(t)

	G = exp(-square(xx) / s - square(a * yy) / s) * cos(f * xx)

	if complex:
		G = G + 1j * exp(-square(xx) / s - square(a * yy) / s) * sin(f * xx + p)

	if isinstance(size, int):
		return G.flatten()
	return G

def gaborfit(gabor):
	"""
	Find position and orientation of Gabor filters.
	"""

	if gabor.ndim < 2:
		gabor = gabor.reshape(int(sqrt(gabors.shape[0]) + 0.5), -1)

	y, Y = argmax(abs(gabor), 0), max(abs(gabor), 0)
	x = argmax(Y)
	y = y[x]

	x = x - gabor.shape[0] / 2. + 1.
	y = y - gabor.shape[1] / 2. + 1.
	x = x * 10. / 16.
	y = y * 10. / 16.

	f = fftshift(fft2(gabor))

	m, M = argmax(abs(f), 0), max(abs(f), 0)
	n = argmax(M)
	m = m[n]

	m = m - gabor.shape[0] / 2
	n = n - gabor.shape[1] / 2

	t = arctan2(m, n)

	return x, y, t
<|endoftext|>


<commit_before>import sys
import os
sys.path += [os.getcwd(), os.path.join(os.getcwd(), 'protocols')]
import unittest

import inspircd
import test_proto_common
from classes import ProtocolError

class TestInspIRCdProtocol(unittest.TestCase):
    def setUp(self):
        self.irc = test_proto_common.FakeIRC(inspircd)
        self.sdata = self.irc.serverdata

    def test_connect(self):
        self.irc.proto.connect(self.irc)
        initial_messages = self.irc.takeMsgs()
        commands = self.irc.takeCommands(initial_messages)

        # SERVER pylink.unittest abcd 0 9PY :PyLink Service
        serverline = 'SERVER %s %s 0 %s :PyLink Service' % (
            self.sdata['hostname'], self.sdata['sendpass'], self.sdata['sid'])
        self.assertIn(serverline, initial_messages)
        self.assertIn('BURST', commands)
        self.assertIn('ENDBURST', commands)
        # Is it creating our lovely PyLink PseudoClient?
        self.assertIn('UID', commands)
        self.assertIn('FJOIN', commands)

    def test_checkRecvpass(self):
        # Correct recvpass here.
        self.irc.run('SERVER somehow.someday abcd 0 0AL :Somehow Server - McMurdo Station, Antarctica')
        # Incorrect recvpass here; should raise ProtocolError.
        self.assertRaises(ProtocolError, self.irc.run, 'SERVER somehow.someday BADPASS 0 0AL :Somehow Server - McMurdo Station, Antarctica')

if __name__ == '__main__':
    unittest.main()
<commit_msg>test_proto_inspircd: fix 'python3 -m unittest' compat<commit_after>import sys
import os
sys.path += [os.getcwd(), os.path.join(os.getcwd(), 'protocols')]
import unittest

import inspircd
from . import test_proto_common
from classes import ProtocolError

class TestInspIRCdProtocol(unittest.TestCase):
    def setUp(self):
        self.irc = test_proto_common.FakeIRC(inspircd)
        self.sdata = self.irc.serverdata

    def test_connect(self):
        self.irc.proto.connect(self.irc)
        initial_messages = self.irc.takeMsgs()
        commands = self.irc.takeCommands(initial_messages)

        # SERVER pylink.unittest abcd 0 9PY :PyLink Service
        serverline = 'SERVER %s %s 0 %s :PyLink Service' % (
            self.sdata['hostname'], self.sdata['sendpass'], self.sdata['sid'])
        self.assertIn(serverline, initial_messages)
        self.assertIn('BURST', commands)
        self.assertIn('ENDBURST', commands)
        # Is it creating our lovely PyLink PseudoClient?
        self.assertIn('UID', commands)
        self.assertIn('FJOIN', commands)

    def test_checkRecvpass(self):
        # Correct recvpass here.
        self.irc.run('SERVER somehow.someday abcd 0 0AL :Somehow Server - McMurdo Station, Antarctica')
        # Incorrect recvpass here; should raise ProtocolError.
        self.assertRaises(ProtocolError, self.irc.run, 'SERVER somehow.someday BADPASS 0 0AL :Somehow Server - McMurdo Station, Antarctica')

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>import unittest
from django.test import Client

class TestRedirectssWork(unittest.TestCase):
    def setUp(self):
        # Every test needs a client.
        self.client = Client()

    def test_home(self):
        # Issue a GET request.
        response = self.client.get('/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

    def test_community(self):
        # Issue a GET request.
        response = self.client.get('/community')

        # Check that the response is 302 Temporary Redirect.
        self.assertEqual(response.status_code, 302)

    def test_faqs(self):
        # Issue a GET request.
        response = self.client.get('/faqs')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

    def test_profile(self):
        # Issue a GET request.
        response = self.client.get('/profile')

        # Check that the response is 302 Temporary Redirect.
        self.assertEqual(response.status_code, 302)

    def test_teacher(self):
        # Issue a GET request.
        response = self.client.get('/teacher')

        # Check that the response is 302 Temporary Redirect.
        self.assertEqual(response.status_code, 302)

    def test_ventures(self):
        # Issue a GET request.
        response = self.client.get('/ventures')

        # Check that the response is 302 Temporary Redirect.
        self.assertEqual(response.status_code, 302)
<commit_msg>more complete testing of redirects<commit_after>import unittest
from django.test import Client

class TestRedirectssWork(unittest.TestCase):
    def setUp(self):
        # Every test needs a client.
        self.client = Client()

    def test_home(self):
        # Issue a GET request.
        response = self.client.get('/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

    def test_faqs(self):
        # Issue a GET request.
        response = self.client.get('/faqs')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

    def test_community(self):
        # Issue a GET request.
        response = self.client.get('/community', follow=True)

        # Check that the response is 302 Temporary Redirect
        # And that the next points to the correct page
        self.assertEqual(
            response.redirect_chain,
            [('http://testserver/accounts/login/?next=/community', 302)]
        )

        # Check that login loads OK.
        self.assertEqual(response.status_code, 200)

    def test_profile(self):
        # Issue a GET request.
        response = self.client.get('/profile', follow=True)

        # Check that the response is 302 Temporary Redirect
        # And that the next points to the correct page
        self.assertEqual(
            response.redirect_chain,
            [('http://testserver/accounts/login/?next=/profile', 302)]
        )

        # Check that login loads OK.
        self.assertEqual(response.status_code, 200)

    def test_teacher(self):
        # Issue a GET request.
        response = self.client.get('/teacher', follow=True)

        # Check that the response is 302 Temporary Redirect
        # And that the next points to the correct page
        self.assertEqual(
            response.redirect_chain,
            [('http://testserver/accounts/login/?next=/teacher', 302)]
        )

        # Check that login loads OK.
        self.assertEqual(response.status_code, 200)

    def test_ventures(self):
        # Issue a GET request.
        response = self.client.get('/ventures', follow=True)

       # Check that the response is 302 Temporary Redirect
        # And that the next points to the correct page
        self.assertEqual(
            response.redirect_chain,
            [('http://testserver/accounts/login/?next=/ventures', 302)]
        )

        # Check that login loads OK.
        self.assertEqual(response.status_code, 200)
<|endoftext|>


<commit_before>"""
Test continuity along a boundary with hanging nodes due to a mesh refinement.
"""
from __future__ import absolute_import
import os.path as op

import numpy as nm

from sfepy.base.testing import TestCommon
from sfepy.base.base import assert_, Struct

def eval_fun(ts, coors, mode, **kwargs):
    val = nm.sin(nm.sum(coors**2, axis=1) * nm.pi)

    val = val.reshape((coors.shape[0], 1, 1))
    return val

def _gen_lines_2_4(bbox, eps):
    from sfepy.discrete.probes import LineProbe

    LineProbe.__base__.cache = Struct(name='probe_shared_evaluate_cache')

    n_point = 100

    line0 = LineProbe([bbox[0, 0], -eps], [bbox[1, 0], -eps], n_point)
    line1 = LineProbe([bbox[0, 0], eps], [bbox[1, 0], eps], n_point)

    yield line0, line1

    line0 = LineProbe([-eps, bbox[0, 1]], [-eps, bbox[1, 1]], n_point)
    line1 = LineProbe([eps, bbox[0, 1]], [eps, bbox[1, 1]], n_point)

    yield line0, line1

def _gen_grid_3_8(bbox, eps):
    from sfepy.discrete.probes import PointsProbe

    PointsProbe.__base__.cache = Struct(name='probe_shared_evaluate_cache')

    n_point = 100

    px = nm.linspace(bbox[0, 0], bbox[1, 0], n_point)
    py = nm.linspace(bbox[0, 1], bbox[1, 1], n_point)
    pz = nm.linspace(bbox[0, 2], bbox[1, 2], n_point)

    # Test face substitutions.
    pps = nm.meshgrid(px, py, -eps)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(px, py, eps)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(px, -eps, pz)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(px, eps, pz)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(-eps, py, pz)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(eps, py, pz)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    # Test edge substitutions.
    pps = nm.meshgrid(px, -eps, -eps)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(px, eps, eps)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(-eps, py, -eps)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(eps, py, eps)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(-eps, -eps, pz)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(eps, eps, pz)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

def _build_filenames(output_dir, key, order, ip):
    return [op.join(output_dir,
                   'test_refine_hanging_lin_%s_%d_%d.vtk' % (key, order, ip)),
            op.join(output_dir,
                    'test_refine_hanging_%s_%d_%d.vtk' % (key, order, ip))]

class Test(TestCommon):

    @staticmethod
    def from_conf(conf, options):
        from sfepy.discrete.fem.geometry_element import GeometryElement

        gels = {}
        gel_names = ['2_4', '3_8']
        for key in gel_names:
            gel = GeometryElement(key)
            gel.create_surface_facet()
            gels[key] = gel

        return Test(conf=conf, options=options, gel_names=gel_names, gels=gels)

    def test_continuity(self):
        from sfepy.base.base import Struct
        from sfepy.mesh.mesh_generators import gen_block_mesh
        from sfepy.discrete import FieldVariable
        from sfepy.discrete.fem import FEDomain, Field
        from sfepy.discrete.projections import make_l2_projection_data
        import sfepy.discrete.fem.refine_hanging as rh

        dims = [1.5, 2.0, 1.3]
        shape = [3, 3, 3]
        centre = [0.0, 0.0, 0.0]
        probe_gens = {'2_4' : _gen_lines_2_4, '3_8' : _gen_grid_3_8}

        ok = True
        for key in self.gel_names:
            gel = self.gels[key]
            probe_gen = probe_gens[key]

            perms = gel.get_conn_permutations()

            dim = gel.dim
            for io, order in enumerate(range(1, 4)):
                mesh00 = gen_block_mesh(dims[:dim], shape[:dim], centre[:dim],
                                       name='block')

                for ip, perm in enumerate(perms):
                    self.report('geometry: %s, order: %d, permutation: %d: %s'
                                % (key, order, ip, perm))
                    mesh0 = mesh00.copy()
                    conn = mesh0.cmesh.get_conn(dim, 0).indices
                    conn = conn.reshape((mesh0.n_el, -1))
                    conn[-1, :] = conn[-1, perm]

                    domain0 = FEDomain('d', mesh0)

                    refine = nm.zeros(mesh0.n_el, dtype=nm.uint8)
                    refine[:-1] = 1

                    subs = None
                    domain, subs = rh.refine(domain0, refine, subs=subs)

                    omega = domain.create_region('Omega', 'all')
                    field = Field.from_args('fu', nm.float64, 1, omega,
                                            approx_order=order)

                    field.substitute_dofs(subs)

                    uvar = FieldVariable('u', 'parameter', field,
                                         primary_var_name='(set-to-None)')

                    field.restore_dofs(store=True)
                    field.substitute_dofs(subs=None, restore=True)

                    make_l2_projection_data(uvar, eval_fun)

                    field.restore_dofs()

                    bbox = domain.get_mesh_bounding_box()
                    eps = 1e-7

                    save = False
                    for ii, (probe0, probe1) in enumerate(probe_gen(bbox, eps)):
                        probe0.set_options(close_limit=0.0)
                        probe1.set_options(close_limit=0.0)

                        pars0, vals0 = probe0(uvar)
                        pars1, vals1 = probe1(uvar)

                        assert_(nm.allclose(pars0, pars1, atol=1e-14, rtol=0.0))

                        _ok = nm.allclose(vals0, vals1, atol=20.0 * eps,
                                          rtol=0.0)
                        if not _ok:
                            save = True
                            self.report('probe %d failed! (max. error: %e)'
                                        % (ii, nm.abs(vals0 - vals1).max()))

                        ok = ok and _ok

                    if (ip == 0) or save:
                        out = uvar.create_output()
                        filenames = _build_filenames(self.options.out_dir,
                                                     key, order, ip)

                        domain.mesh.write(filenames[0], out=out)

                        linearization = Struct(kind='adaptive',
                                               min_level=0,
                                               max_level=4,
                                               eps=1e-2)

                        out = uvar.create_output(linearization=linearization)
                        val = out['u']
                        mesh = val.get('mesh', domain.mesh)
                        mesh.write(filenames[1], out=out)

        return ok

    def test_preserve_coarse_entities(self):
        from sfepy.mesh.mesh_generators import gen_block_mesh
        from sfepy.discrete.fem import FEDomain
        import sfepy.discrete.fem.refine_hanging as rh

        dims = [1.5, 2.0]
        shape = [11, 11]
        centre = [0.0, 0.0]

        mesh0 = gen_block_mesh(dims, shape, centre, name='block')
        domain0 = FEDomain('d', mesh0)
        reg = domain0.create_region('surface', 'vertices of surface', 'facet',
                                    add_to_regions=False)
        cmesh0 = mesh0.cmesh

        cmesh0.vertex_groups[5::11] = 2
        cmesh0.vertex_groups[reg.vertices] = 1
        cmesh0.cell_groups[0] = 1
        cmesh0.cell_groups[50:60] = 2
        mesh0.write(op.join(self.options.out_dir,
                            'test_refine_hanging_ids0.vtk'), io='auto')

        refine = nm.zeros(mesh0.n_el, dtype=nm.uint8)
        refine[0:10] = 1
        refine[5::10] = 1

        domain, _, sub_cells = rh.refine(domain0, refine, subs=None,
                                         ret_sub_cells=True)
        domain.mesh.write(op.join(self.options.out_dir,
                                  'test_refine_hanging_ids1.vtk'), io='auto')
        cmesh1 = domain.mesh.cmesh

        ii = nm.where(refine == 0)[0]
        conn0 = mesh0.get_conn('2_4')
        v0 = conn0[ii]

        conn1 = domain.mesh.get_conn('2_4')
        v1 = conn1[ii]

        ok = (v0 == v1).all()
        self.report('coarse cells positions preserved:', ok)

        cgs0 = cmesh0.cell_groups[ii]
        cgs1 = cmesh1.cell_groups[ii]

        _ok = (cgs0 == cgs1).all()
        self.report('coarse cells cell groups preserved:', _ok)
        ok = ok and _ok

        vgs0 = cmesh0.vertex_groups[v0]
        vgs1 = cmesh1.vertex_groups[v1]

        _ok = (vgs0 == vgs1).all()
        self.report('coarse cells vertex groups preserved:', _ok)
        ok = ok and _ok

        ii = nm.where(refine == 1)[0]
        cgs0 = cmesh0.cell_groups[ii]
        cgs1 = cmesh1.cell_groups[sub_cells[:, 1:]]

        _ok = (cgs0[:, None] == cgs1).all()
        self.report('refined cells cell groups preserved:', _ok)
        ok = ok and _ok

        return ok
<commit_msg>update test_refine_hanging.py for pytest<commit_after>"""
Test continuity along a boundary with hanging nodes due to a mesh refinement.
"""
import os.path as op

import numpy as nm
import pytest

from sfepy.base.base import assert_, Struct
import sfepy.base.testing as tst

def eval_fun(ts, coors, mode, **kwargs):
    val = nm.sin(nm.sum(coors**2, axis=1) * nm.pi)

    val = val.reshape((coors.shape[0], 1, 1))
    return val

def _gen_lines_2_4(bbox, eps):
    from sfepy.discrete.probes import LineProbe

    LineProbe.__base__.cache = Struct(name='probe_shared_evaluate_cache')

    n_point = 100

    line0 = LineProbe([bbox[0, 0], -eps], [bbox[1, 0], -eps], n_point)
    line1 = LineProbe([bbox[0, 0], eps], [bbox[1, 0], eps], n_point)

    yield line0, line1

    line0 = LineProbe([-eps, bbox[0, 1]], [-eps, bbox[1, 1]], n_point)
    line1 = LineProbe([eps, bbox[0, 1]], [eps, bbox[1, 1]], n_point)

    yield line0, line1

def _gen_grid_3_8(bbox, eps):
    from sfepy.discrete.probes import PointsProbe

    PointsProbe.__base__.cache = Struct(name='probe_shared_evaluate_cache')

    n_point = 100

    px = nm.linspace(bbox[0, 0], bbox[1, 0], n_point)
    py = nm.linspace(bbox[0, 1], bbox[1, 1], n_point)
    pz = nm.linspace(bbox[0, 2], bbox[1, 2], n_point)

    # Test face substitutions.
    pps = nm.meshgrid(px, py, -eps)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(px, py, eps)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(px, -eps, pz)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(px, eps, pz)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(-eps, py, pz)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(eps, py, pz)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    # Test edge substitutions.
    pps = nm.meshgrid(px, -eps, -eps)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(px, eps, eps)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(-eps, py, -eps)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(eps, py, eps)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

    pps = nm.meshgrid(-eps, -eps, pz)
    points0 = nm.array([ii.ravel() for ii in pps]).T
    pps = nm.meshgrid(eps, eps, pz)
    points1 = nm.array([ii.ravel() for ii in pps]).T

    grid0 = PointsProbe(points0)
    grid1 = PointsProbe(points1)

    yield grid0, grid1

def _build_filenames(output_dir, key, order, ip):
    return [op.join(output_dir,
                   'test_refine_hanging_lin_%s_%d_%d.vtk' % (key, order, ip)),
            op.join(output_dir,
                    'test_refine_hanging_%s_%d_%d.vtk' % (key, order, ip))]

@pytest.fixture(scope='module')
def gels():
    from sfepy.discrete.fem.geometry_element import GeometryElement

    gels = {}
    for key in ['2_4', '3_8']:
        gel = GeometryElement(key)
        gel.create_surface_facet()
        gels[key] = gel

    return gels

def test_continuity(gels, output_dir):
    from sfepy.base.base import Struct
    from sfepy.mesh.mesh_generators import gen_block_mesh
    from sfepy.discrete import FieldVariable
    from sfepy.discrete.fem import FEDomain, Field
    from sfepy.discrete.projections import make_l2_projection_data
    import sfepy.discrete.fem.refine_hanging as rh

    dims = [1.5, 2.0, 1.3]
    shape = [3, 3, 3]
    centre = [0.0, 0.0, 0.0]
    probe_gens = {'2_4' : _gen_lines_2_4, '3_8' : _gen_grid_3_8}

    ok = True
    for key, gel in gels.items():
        probe_gen = probe_gens[key]

        perms = gel.get_conn_permutations()

        dim = gel.dim
        for io, order in enumerate(range(1, 4)):
            mesh00 = gen_block_mesh(dims[:dim], shape[:dim], centre[:dim],
                                    name='block')

            for ip, perm in enumerate(perms):
                tst.report('geometry: %s, order: %d, permutation: %d: %s'
                           % (key, order, ip, perm))
                mesh0 = mesh00.copy()
                conn = mesh0.cmesh.get_conn(dim, 0).indices
                conn = conn.reshape((mesh0.n_el, -1))
                conn[-1, :] = conn[-1, perm]

                domain0 = FEDomain('d', mesh0)

                refine = nm.zeros(mesh0.n_el, dtype=nm.uint8)
                refine[:-1] = 1

                subs = None
                domain, subs = rh.refine(domain0, refine, subs=subs)

                omega = domain.create_region('Omega', 'all')
                field = Field.from_args('fu', nm.float64, 1, omega,
                                        approx_order=order)

                field.substitute_dofs(subs)

                uvar = FieldVariable('u', 'parameter', field,
                                     primary_var_name='(set-to-None)')

                field.restore_dofs(store=True)
                field.substitute_dofs(subs=None, restore=True)

                make_l2_projection_data(uvar, eval_fun)

                field.restore_dofs()

                bbox = domain.get_mesh_bounding_box()
                eps = 1e-7

                save = False
                for ii, (probe0, probe1) in enumerate(probe_gen(bbox, eps)):
                    probe0.set_options(close_limit=0.0)
                    probe1.set_options(close_limit=0.0)

                    pars0, vals0 = probe0(uvar)
                    pars1, vals1 = probe1(uvar)

                    assert_(nm.allclose(pars0, pars1, atol=1e-14, rtol=0.0))

                    _ok = nm.allclose(vals0, vals1, atol=20.0 * eps,
                                      rtol=0.0)
                    if not _ok:
                        save = True
                        tst.report('probe %d failed! (max. error: %e)'
                                   % (ii, nm.abs(vals0 - vals1).max()))

                    ok = ok and _ok

                if (ip == 0) or save:
                    out = uvar.create_output()
                    filenames = _build_filenames(output_dir,
                                                 key, order, ip)

                    domain.mesh.write(filenames[0], out=out)

                    linearization = Struct(kind='adaptive',
                                           min_level=0,
                                           max_level=4,
                                           eps=1e-2)

                    out = uvar.create_output(linearization=linearization)
                    val = out['u']
                    mesh = val.get('mesh', domain.mesh)
                    mesh.write(filenames[1], out=out)

    assert_(ok)

def test_preserve_coarse_entities(output_dir):
    from sfepy.mesh.mesh_generators import gen_block_mesh
    from sfepy.discrete.fem import FEDomain
    import sfepy.discrete.fem.refine_hanging as rh

    dims = [1.5, 2.0]
    shape = [11, 11]
    centre = [0.0, 0.0]

    mesh0 = gen_block_mesh(dims, shape, centre, name='block')
    domain0 = FEDomain('d', mesh0)
    reg = domain0.create_region('surface', 'vertices of surface', 'facet',
                                add_to_regions=False)
    cmesh0 = mesh0.cmesh

    cmesh0.vertex_groups[5::11] = 2
    cmesh0.vertex_groups[reg.vertices] = 1
    cmesh0.cell_groups[0] = 1
    cmesh0.cell_groups[50:60] = 2
    mesh0.write(op.join(output_dir,
                        'test_refine_hanging_ids0.vtk'), io='auto')

    refine = nm.zeros(mesh0.n_el, dtype=nm.uint8)
    refine[0:10] = 1
    refine[5::10] = 1

    domain, _, sub_cells = rh.refine(domain0, refine, subs=None,
                                     ret_sub_cells=True)
    domain.mesh.write(op.join(output_dir,
                              'test_refine_hanging_ids1.vtk'), io='auto')
    cmesh1 = domain.mesh.cmesh

    ii = nm.where(refine == 0)[0]
    conn0 = mesh0.get_conn('2_4')
    v0 = conn0[ii]

    conn1 = domain.mesh.get_conn('2_4')
    v1 = conn1[ii]

    ok = (v0 == v1).all()
    tst.report('coarse cells positions preserved:', ok)

    cgs0 = cmesh0.cell_groups[ii]
    cgs1 = cmesh1.cell_groups[ii]

    _ok = (cgs0 == cgs1).all()
    tst.report('coarse cells cell groups preserved:', _ok)
    ok = ok and _ok

    vgs0 = cmesh0.vertex_groups[v0]
    vgs1 = cmesh1.vertex_groups[v1]

    _ok = (vgs0 == vgs1).all()
    tst.report('coarse cells vertex groups preserved:', _ok)
    ok = ok and _ok

    ii = nm.where(refine == 1)[0]
    cgs0 = cmesh0.cell_groups[ii]
    cgs1 = cmesh1.cell_groups[sub_cells[:, 1:]]

    _ok = (cgs0[:, None] == cgs1).all()
    tst.report('refined cells cell groups preserved:', _ok)
    ok = ok and _ok

    assert_(ok)
<|endoftext|>


<commit_before>from unittest import TestCase
from ppp_datamodel.communication import Request
from ppp_datamodel import Triple, Resource, Missing
from ppp_libmodule.tests import PPPTestCase

from ppp_questionparsing_ml_standalone import app

class RequestHandlerTest(PPPTestCase(app)):
    def testRH(self):
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': 'What is the birth date of George Washington'}}

        answer = self.request(j)

        self.assertEquals(len(answer), 1)

        self.assertTrue(isinstance(answer[0].tree, Triple))

        subject = answer[0].tree.subject
        self.assertTrue(isinstance(subject, Resource))
        self.assertEquals(subject.value, 'George Washington')

        predicate = answer[0].tree.predicate

        self.assertTrue(isinstance(predicate, Resource))
        self.assertEquals(predicate.value, 'birth date')

        object = answer[0].tree.object

        self.assertTrue(isinstance(object, Missing))
<commit_msg>Fix test.<commit_after>from unittest import TestCase
from ppp_datamodel.communication import Request
from ppp_datamodel import Triple, Resource, Missing
from ppp_libmodule.tests import PPPTestCase

from ppp_questionparsing_ml_standalone import app

class RequestHandlerTest(PPPTestCase(app)):
    def testRH(self):
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': 'birth date George Washington'}}

        answer = self.request(j)

        self.assertEquals(len(answer), 1)

        self.assertTrue(isinstance(answer[0].tree, Triple))

        subject = answer[0].tree.subject
        self.assertTrue(isinstance(subject, Resource))
        self.assertEquals(subject.value, 'George Washington')

        predicate = answer[0].tree.predicate

        self.assertTrue(isinstance(predicate, Resource))
        self.assertEquals(predicate.value, 'birth date')

        object = answer[0].tree.object

        self.assertTrue(isinstance(object, Missing))
<|endoftext|>


<commit_before># Copyright (c) 2013-2016, Freja Nordsiek
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numpy as np
import numpy.random

import hdf5storage.utilities as utils

from asserts import *
from make_randoms import *

random.seed()

def test_numpy_str_ascii_to_uint16_back():
    for i in range(10):
        data = random_numpy(shape=(1, ), dtype='U',
                            allow_unicode=False)[0]
        intermed = utils.convert_numpy_str_to_uint16(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert out.tostring() == data.tostring()
        assert_equal(out, data)

def test_numpy_str_someunicode_to_uint16_back():
    for i in range(10):
        data = random_numpy(shape=(1, ), dtype='U',
                            allow_unicode=True)[0]
        intermed = utils.convert_numpy_str_to_uint16(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert out.tostring() == data.tostring()
        assert_equal(out, data)

def test_numpy_str_ascii_to_uint32_back():
    for i in range(10):
        data = random_numpy(shape=(1, ), dtype='U',
                            allow_unicode=False)[0]
        intermed = utils.convert_numpy_str_to_uint32(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert intermed.tostring() == data.tostring()
        assert out.tostring() == data.tostring()
        assert_equal(out, data)

def test_numpy_str_someunicode_to_uint32_back():
    for i in range(10):
        data = random_numpy(shape=(1, ), dtype='U',
                            allow_unicode=True)[0]
        intermed = utils.convert_numpy_str_to_uint32(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert intermed.tostring() == data.tostring()
        assert out.tostring() == data.tostring()
        assert_equal(out, data)
<commit_msg>In the string conversion tests, the strings are made more directly.<commit_after># Copyright (c) 2013-2016, Freja Nordsiek
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numpy as np
import numpy.random

import hdf5storage.utilities as utils

from asserts import *
from make_randoms import *

random.seed()

def test_numpy_str_ascii_to_uint16_back():
    for i in range(10):
        length = random.randint(1, max_string_length)
        data = np.unicode_(random_str_ascii(length))
        intermed = utils.convert_numpy_str_to_uint16(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert out.tostring() == data.tostring()
        assert_equal(out, data)

def test_numpy_str_someunicode_to_uint16_back():
    for i in range(10):
        length = random.randint(1, max_string_length)
        data = np.unicode_(random_str_some_unicode(length))
        intermed = utils.convert_numpy_str_to_uint16(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert out.tostring() == data.tostring()
        assert_equal(out, data)

def test_numpy_str_ascii_to_uint32_back():
    for i in range(10):
        length = random.randint(1, max_string_length)
        data = np.unicode_(random_str_ascii(length))
        intermed = utils.convert_numpy_str_to_uint32(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert intermed.tostring() == data.tostring()
        assert out.tostring() == data.tostring()
        assert_equal(out, data)

def test_numpy_str_someunicode_to_uint32_back():
    for i in range(10):
        length = random.randint(1, max_string_length)
        data = np.unicode_(random_str_some_unicode(length))
        intermed = utils.convert_numpy_str_to_uint32(data)
        out = utils.convert_to_numpy_str(intermed)[0]
        assert intermed.tostring() == data.tostring()
        assert out.tostring() == data.tostring()
        assert_equal(out, data)
<|endoftext|>


<commit_before>"""Test the topic detector tool."""

from proselint.tools import topics

def test_50_Cent_detector_on_topic(self):
    """Check precision of the 50 Cent topic-detector."""
    text = """With the aid of Eminem and Dr. Dre (who produced his first
           major-label album, Get Rich or Die Tryin'), Jackson became one
           of the world's best selling rappers and rose to prominence with
           East Coast hip hop group G-Unit (which he leads de facto). """

    assert("50 Cent" in topics(text))

    text = """Hip hop was started in the early 50's."""
    assert("50 Cent" not in topics(text))

    text = """Nowadays it costs 50 cents to buy a lollipop."""
    assert("50 Cent" not in topics(text))
<commit_msg>Fixes bug in topic detector test<commit_after>"""Test the topic detector tool."""

from proselint.tools import topics

def test_50_Cent_detector_on_topic():
    """Check precision of the 50 Cent topic-detector."""
    text = """With the aid of Eminem and Dr. Dre (who produced his first
           major-label album, Get Rich or Die Tryin'), Jackson became one
           of the world's best selling rappers and rose to prominence with
           East Coast hip hop group G-Unit (which he leads de facto). """

    assert("50 Cent" in topics(text))

    text = """Hip hop was started in the early 50's."""
    assert("50 Cent" not in topics(text))

    text = """Nowadays it costs 50 cents to buy a lollipop."""
    assert("50 Cent" not in topics(text))
<|endoftext|>