Esempio n. 1
0
def age_standardize_approx(name, age_weights, mu_age, age_start, age_end,
                           ages):
    """ Generate PyMC objects for approximating the integral of gamma from age_start[i] to age_end[i]

    Parameters
    ----------
    name : str
    age_weights : array, len == len(ages)
    mu_age : pymc.Node with values of PCGP
    age_start, age_end : array

    Results
    -------
    Returns dict of PyMC objects, including 'mu_interval'
    the approximate integral of gamma  data predicted stochastic
    """
    cum_sum_weights = pl.cumsum(age_weights)

    @mc.deterministic(name='cum_sum_mu_%s' % name)
    def weighted_sum_mu(mu_age=mu_age, age_weights=age_weights):
        return pl.cumsum(mu_age * age_weights)

    age_start = age_start.__array__().clip(ages[0], ages[-1]) - ages[
        0]  # FIXME: Pandas bug, makes clip require __array__()
    age_end = age_end.__array__().clip(ages[0], ages[-1]) - ages[0]
    pl.seterr('ignore')

    @mc.deterministic(name='mu_interval_%s' % name)
    def mu_interval(weighted_sum_mu=weighted_sum_mu,
                    cum_sum_weights=cum_sum_weights,
                    mu_age=mu_age,
                    age_start=pl.array(age_start, dtype=int),
                    age_end=pl.array(age_end, dtype=int)):
        mu = (weighted_sum_mu[age_end] - weighted_sum_mu[age_start]) / (
            cum_sum_weights[age_end] - cum_sum_weights[age_start])

        # correct cases where age_start == age_end
        i = age_start == age_end
        if pl.any(i):
            mu[i] = mu_age[age_start[i]]

        return mu

    return dict(mu_interval=mu_interval)
Esempio n. 2
0
def age_integrate_approx(name, age_weights, mu_age, age_start, age_end, ages):
    """ Generate PyMC objects for approximating the integral of gamma from age_start[i] to age_end[i]

    Parameters
    ----------
    name : str
    age_weights : list of strings, each str a semicolon-separated list of floats
    mu_age : pymc.Node with values of PCGP
    age_start, age_end : array

    Results
    -------
    Returns dict of PyMC objects, including 'mu_interval'
    the approximate integral of gamma  data predicted stochastic
    """
    # FIXME: should use final age weight
    age_weights = [
        pl.array([1.e-9 + float(w_ia) for w_ia in w_i.split(';')][:-1])
        for w_i in age_weights
    ]
    wt_sum = pl.array([sum(w_i) for w_i in age_weights])

    age_start = age_start.__array__().clip(ages[0], ages[-1]) - ages[
        0]  # FIXME: Pandas bug, makes clip require __array__()
    age_end = age_end.__array__().clip(ages[0], ages[-1]) - ages[0]
    pl.seterr('ignore')

    @mc.deterministic(name='mu_interval_%s' % name)
    def mu_interval(age_weights=age_weights,
                    wt_sum=wt_sum,
                    mu_age=mu_age,
                    age_start=pl.array(age_start, dtype=int),
                    age_end=pl.array(age_end, dtype=int)):
        N = len(age_weights)
        mu = pl.zeros(N)

        for i in range(N):
            mu[i] = pl.dot(age_weights[i],
                           mu_age[age_start[i]:age_end[i]]) / wt_sum[i]

        return mu

    return dict(mu_interval=mu_interval)
Esempio n. 3
0
def age_standardize_approx(name, age_weights, mu_age, age_start, age_end, ages):
    """ Generate PyMC objects for approximating the integral of gamma from age_start[i] to age_end[i]

    Parameters
    ----------
    name : str
    age_weights : array, len == len(ages)
    mu_age : pymc.Node with values of PCGP
    age_start, age_end : array

    Results
    -------
    Returns dict of PyMC objects, including 'mu_interval'
    the approximate integral of gamma  data predicted stochastic
    """
    cum_sum_weights = pl.cumsum(age_weights)

    @mc.deterministic(name='cum_sum_mu_%s'%name)
    def weighted_sum_mu(mu_age=mu_age, age_weights=age_weights):
        return pl.cumsum(mu_age*age_weights)

    age_start = age_start.__array__().clip(ages[0], ages[-1]) - ages[0]  # FIXME: Pandas bug, makes clip require __array__()
    age_end = age_end.__array__().clip(ages[0], ages[-1]) - ages[0]
    pl.seterr('ignore')
    @mc.deterministic(name='mu_interval_%s'%name)
    def mu_interval(weighted_sum_mu=weighted_sum_mu, cum_sum_weights=cum_sum_weights,
                    mu_age=mu_age,
                    age_start=pl.array(age_start, dtype=int),
                    age_end=pl.array(age_end, dtype=int)):
        mu = (weighted_sum_mu[age_end] - weighted_sum_mu[age_start]) / (cum_sum_weights[age_end] - cum_sum_weights[age_start])
        
        # correct cases where age_start == age_end
        i = age_start == age_end
        if pl.any(i):
            mu[i] = mu_age[age_start[i]]

        return mu

    return dict(mu_interval=mu_interval)
Esempio n. 4
0
def age_integrate_approx(name, age_weights, mu_age, age_start, age_end, ages):
    """ Generate PyMC objects for approximating the integral of gamma from age_start[i] to age_end[i]

    Parameters
    ----------
    name : str
    age_weights : list of strings, each str a semicolon-separated list of floats
    mu_age : pymc.Node with values of PCGP
    age_start, age_end : array

    Results
    -------
    Returns dict of PyMC objects, including 'mu_interval'
    the approximate integral of gamma  data predicted stochastic
    """
    # FIXME: should use final age weight
    age_weights = [pl.array([1.e-9+float(w_ia) for w_ia in w_i.split(';')][:-1]) for w_i in age_weights]
    wt_sum = pl.array([sum(w_i) for w_i in age_weights])

    age_start = age_start.__array__().clip(ages[0], ages[-1]) - ages[0]  # FIXME: Pandas bug, makes clip require __array__()
    age_end = age_end.__array__().clip(ages[0], ages[-1]) - ages[0]
    pl.seterr('ignore')
    @mc.deterministic(name='mu_interval_%s'%name)
    def mu_interval(age_weights=age_weights,
                    wt_sum=wt_sum,
                    mu_age=mu_age,
                    age_start=pl.array(age_start, dtype=int),
                    age_end=pl.array(age_end, dtype=int)):
        N = len(age_weights)
        mu = pl.zeros(N)

        for i in range(N):
            mu[i] = pl.dot(age_weights[i], mu_age[age_start[i]:age_end[i]]) / wt_sum[i]

        return mu

    return dict(mu_interval=mu_interval)
Esempio n. 5
0
""" Script to distribute the Random Effects validation jobs on the
IHME cluster, run each job, and compile the results"""

# matplotlib will open windows during testing unless you do the following
import matplotlib
matplotlib.use("AGG")

import optparse
import subprocess

import pylab as pl
import pandas

pl.seterr('ignore')

import validate_covariates
reload(validate_covariates)


def tally_results():
    import glob
    results = pandas.DataFrame(columns='N delta sigma param bias mae mare pc'.split())
    for fname in sorted(glob.glob('/home/j/Project/dismod/re_validation/*-*-*-*-*.csv')):
        N, delta, sigma, ess, rep = fname.split('/')[-1].split('-')

        df = pandas.read_csv(fname, index_col=None)
        df['N'] = int(N)
        df['delta'] = float(delta)
        df['sigma'] = float(sigma)
        df['ess'] = float(ess)
        results = results.append(df, ignore_index=True)
Esempio n. 6
0
""" Script to distribute the validation jobs on the
IHME cluster"""

# matplotlib will open windows during testing unless you do the following
import matplotlib

matplotlib.use("AGG")

import optparse
import subprocess

import pandas
import pylab as pl

pl.seterr("ignore")
# add to path, to make importing possible
import sys

sys.path += [".", ".."]

import dismod3

reload(dismod3)

import validate_age_group

reload(validate_age_group)

output_dir = "/home/j/Project/dismod"
validation_name = "age_group_validation"
Esempio n. 7
0
import pylab
import numpy as np
import matplotlib.pyplot as plt

pylab.seterr(all='ignore') # ignora todos os reais de floating

N = 101 # Numero de pontos na coordenada x e y
a, b = np.linspace(-5, 5, N), np.linspace(-5, 5, N) # Setando ponto espacados dentro do intervalo -5 e 5
xa, ya = np.meshgrid(a, b)     # Um meshgrid no plano xy. xa conten as cooordenadas x
Ex = pylab.zeros_like(xa)         # Array 2D para armazenar as compenentes dos campos Ex e 
Ey = pylab.zeros_like(ya)         # Ey
 
Q = [ (-5, -2, -2), (5, 2, -2)]  # Cargas (Valor, coord-x, coord-y)
#(-8, 0, 3),
cor = ['r', 'b']
for q in Q:  # marca os locais das cargas
    pylab.text(q[1], q[2], 'o', color = 'r', fontsize=15, va='center', ha='center')

for c in cor:
    pylab.text(q[1], q[2], 'o', color = c, fontsize=15, va='center', ha='center')    

for i in range(N):      # calcula Ex e Ey em cada ponto no grid, devido todas as cargas
    for j in range(N):
        x, y = xa[i,j], ya[i,j]
        for k in range(len(Q)): # somas sobre todas as cargas, usando a equacao do resnick
            
            Ex[i,j] += Q[k][0]*(x-Q[k][1])/ ((x-Q[k][1])**2+(y-Q[k][2])**2)**(1.5)
            Ey[i,j] += Q[k][0]*(y-Q[k][2])/ ((x-Q[k][1])**2+(y-Q[k][2])**2)**(1.5)

plt.streamplot(xa, ya, Ex, Ey, color='g', density= 1.6) # plota as linhas de campo usando a funcao streamplot
plt.show() # mostra o grafico