Exemplo n.º 1
0
def cartesian(x, y=None, z=None):

    max_dim = 1
    for val in (x, y, z):
        if is_array(type(val)):
            if val.size > max_dim:
                max_dim = val.size

    if max_dim != 1:
        if is_scalar(type(x)) or (is_array(type(x)) and x.shape == (1, )):
            x = ones(max_dim) * x
        if is_scalar(type(y)) or (is_array(type(y)) and y.shape == (1, )):
            y = ones(max_dim) * y
        if is_scalar(type(z)) or (is_array(type(z)) and z.shape == (1, )):
            z = ones(max_dim) * z

    if type(x) == type(None):
        x = zeros(max_dim)
    if type(y) == type(None):
        y = zeros(max_dim)
    if type(z) == type(None):
        z = zeros(max_dim)

    if x.ndim == y.ndim == z.ndim == 1:
        if x.shape == y.shape == z.shape:
            return vstack((x, y, z)).T

    print 'EE: CoordinateSystem.cartesian() - This should not happen!'
Exemplo n.º 2
0
def cartesian(x, y=None, z=None):
   
   max_dim = 1
   for val in (x, y, z):
      if is_array(type(val)):
         if val.size > max_dim:
            max_dim = val.size
   
   if max_dim != 1:
      if is_scalar(type(x)) or (is_array(type(x)) and x.shape == (1,)):
         x = ones(max_dim)*x
      if is_scalar(type(y)) or (is_array(type(y)) and y.shape == (1,)):
         y = ones(max_dim)*y
      if is_scalar(type(z)) or (is_array(type(z)) and z.shape == (1,)):
         z = ones(max_dim)*z
         
   if type(x) == type(None):
      x = zeros(max_dim)
   if type(y) == type(None):
      y = zeros(max_dim)
   if type(z) == type(None):
      z = zeros(max_dim)
      
   if x.ndim == y.ndim == z.ndim == 1:
      if x.shape == y.shape == z.shape:
         return vstack((x,y,z)).T

   print 'EE: CoordinateSystem.cartesian() - This should not happen!'
Exemplo n.º 3
0
def spherical(r, theta=None, phi=None):
   
#   def repetiveStuff():
#      self.type   = 'spherical'
#      self.shape  = self.p.shape
   
   max_dim = 1
   for val in (r, theta, phi):
      if is_array(type(val)):
         if val.size > max_dim:
            max_dim = val.size
   
   if max_dim != 1:
      if is_scalar(type(r)) or (is_array(type(r)) and r.shape == (1,)):
         r = ones(max_dim)*r
      if is_scalar(type(theta)) or (is_array(type(theta)) and theta.shape == (1,)):
         theta = ones(max_dim)*theta
      if is_scalar(type(phi)) or (is_array(type(phi)) and phi.shape == (1,)):
         phi = ones(max_dim)*phi
         
   if type(r) == type(None):
      r = zeros(max_dim)
   if type(theta) == type(None):
      theta = zeros(max_dim)
   if type(phi) == type(None):
      phi = zeros(max_dim)
      
   if r.ndim == theta.ndim == phi.ndim == 1:
      if r.shape == theta.shape == phi.shape:
         p = vstack((r,theta,phi)).T

#            repetiveStuff()
         return p
   
   print 'EE: CoordinateSystem.spherical() - This should not happen!'
Exemplo n.º 4
0
    def __new__(self, M=10, phi=0, normalised=True):

        # Create the window
        if phi == 0:
            win = np.ones((M, ), dtype=None) / M
        else:
            wc = np.ones(M, dtype=complex)  # Window coefficients
            m = np.arange(0, M)  # Create M indeces from 0 to 1
            a = exp(-1j * 2 * pi * m * phi)  # Steering vector
            ws = dot(wc, a)  # Normalisation factor
            win = a * wc / ws  # Steered and normalised window

        w = np.Ndarray.__new__(self, win)
        #                               axes=('M',),
        #                               desc = 'Rectangular (phi=%d)'%phi)
        #                               desc='Rectangular (phi=%d)'%phi,
        #                               shape_desc=('M','1'))
        return w
Exemplo n.º 5
0
def spherical(r, theta=None, phi=None):

    #   def repetiveStuff():
    #      self.type   = 'spherical'
    #      self.shape  = self.p.shape

    max_dim = 1
    for val in (r, theta, phi):
        if is_array(type(val)):
            if val.size > max_dim:
                max_dim = val.size

    if max_dim != 1:
        if is_scalar(type(r)) or (is_array(type(r)) and r.shape == (1, )):
            r = ones(max_dim) * r
        if is_scalar(type(theta)) or (is_array(type(theta))
                                      and theta.shape == (1, )):
            theta = ones(max_dim) * theta
        if is_scalar(type(phi)) or (is_array(type(phi))
                                    and phi.shape == (1, )):
            phi = ones(max_dim) * phi

    if type(r) == type(None):
        r = zeros(max_dim)
    if type(theta) == type(None):
        theta = zeros(max_dim)
    if type(phi) == type(None):
        phi = zeros(max_dim)

    if r.ndim == theta.ndim == phi.ndim == 1:
        if r.shape == theta.shape == phi.shape:
            p = vstack((r, theta, phi)).T

            #            repetiveStuff()
            return p

    print 'EE: CoordinateSystem.spherical() - This should not happen!'
Exemplo n.º 6
0
def optimalLightness(npts, cmap_type, l_range=(0.0, 1.0)):
    """
    Helper function defining the optimality condition for a colormap.
    Depending on the colormap this might mean different things. The
    l_range argument can be used to restrict the lightness range so it
    does have to equal the full range.
    """
    if cmap_type == "sequential_rising":
        opt = np.linspace(l_range[0], l_range[1], npts)
    elif cmap_type == "sequential_falling":
        opt = np.linspace(l_range[1], l_range[0], npts)
    elif cmap_type == "diverging_center_top":
        s = npts // 2
        opt = np.empty(npts)
        opt[:s] = np.linspace(l_range[0], l_range[1], s)
        opt[s:] = np.linspace(l_range[1], l_range[0], npts - s)
    elif cmap_type == "diverging_center_bottom":
        s = npts // 2
        opt = np.empty(npts)
        opt[:s] = np.linspace(l_range[1], l_range[0], s)
        opt[s:] = np.linspace(l_range[0], l_range[1], npts - s)
    elif cmap_type == "flat":
        opt = np.ones(npts) * l_range[0]
    return opt
Exemplo n.º 7
0
 def beta(t):
     if t==(len(X)-1):
         return np.ones(self.bins)
     return # TODO return the apprpriate value
Exemplo n.º 8
0
 def beta(t):
     if t==(len(X)):
         return np.ones(self.bins)
     evidence = np.array([self.pEmission(z,X[t-1]) for z in self.states])
     return np.array(M(t)*np.matrix(evidence*beta(t+1)).T)[...,0]
Exemplo n.º 9
0
            return # TODO return the appropriate value

        # backwards algorithm
        def beta(t):
            if t==(len(X)-1):
                return np.ones(self.bins)
            return # TODO return the apprpriate value

        res = alpha(t)*beta(t)
        return res/sum(res)

if __name__ == "__main__":
    import matplotlib.pyplot as plt

    # Define size of network
    T = 100 # number of timebinss
    N = 3   # number of candidates
    M = 5   # number of polls

    # Randomly model parameters based on priors
    I = 2*np.ones(N)
    B = 10*np.random.randn(M,N)
    B = np.exp(B)
    W = np.concatenate((.5*np.random.randn(1) + 7,np.random.randn(M)))
    W = np.exp(W)

    model = Model(i=I,b=B,w=W)
    Z, X = model.generate(T)

    plt.plot(range(T),Z)
    plt.show()
Exemplo n.º 10
0
def normalize(A, delLoops=False):
    B = deepcopy(A)
    J = np.ones((len(A), len(A)))
    output = B / (B @ J)
    output[np.isnan(output)] = 0
    return output
Exemplo n.º 11
0
        res = 1
        for i in xrange(self.num_polls):
            alpha = self.b[i]*z
            res *= Beta.pdf(x[i,0], alpha[0], alpha[1])
        return res

if __name__ == "__main__":
    import matplotlib.pyplot as plt

    # Generate a test case
    # Define size of network
    T = 100 # number of time steps
    M = 3   # number of polls

    # Randomly model parameters based on priors
    I = 2*np.ones(2)
    B = np.random.randn(M,2) 
    B = np.exp(B)
    W = np.concatenate((.5*np.random.randn(1) + 7,np.random.randn(M)))
    W = np.exp(W)

    model = BetaModel(i=I,b=B,w=W)

    ## Generate Test Data
    Z, X = model.generate(T)

    ## Plot Test Data
    plt.plot(range(T),Z)
    plt.show()

    print np.sum(model.pState(X,1))
Exemplo n.º 12
0
        y_parts = f.readline().split(" ")
        x_coord = float(x_parts[-1][:-2])
        y_coord = float(y_parts[-1][:-1])
        output[currID].append(x_coord)
        output[currID].append(y_coord)
        for j in range(3):
            f.readline()
    return output


if __name__ == '__main__':

    betas = np.linspace(1e-3, 1, 25)
    num_betas = 8
    name = 'mod'
    x = np.ones((num_betas, 3))
    y = np.ones((num_betas, 3))

    # blue to purple, use 8
    x[:, 0:3] = (0, .5, .5)
    y[:, 0:3] = (.5, 0, .5)
    # green to orange, use 6
    # x[:, 0:3] = (0, .5, 0)
    # y[:, 0:3] = (1, .5, 0)

    #sw colorscheme
    # x[:, 0:3] = (.75, .25, 0)
    # y[:, 0:3] = (0, .75, .5)
    c = np.linspace(0, 1, num_betas)[:, None]
    gradient = x + (y - x) * c
Exemplo n.º 13
0
def getWindow(
        image,
        tx_coords,  # x,y
        tx_angle,  # radians
        img_coords,  # x_min, x_max, y_min, y_max
        width=0.8,
        scale=1):
    Nx, Ny = image.shape

    N = Nx

    img_width = img_coords[1] - img_coords[0]
    img_length = img_coords[3] - img_coords[2]
    x_mean = np.mean(img_coords[0:2])
    y_mean = np.mean(img_coords[2:4])
    dx = float(img_width) / Nx
    dy = float(img_length) / Ny

    image_window = np.ones(image.shape)
    x_old = np.zeros(N)
    x_new = np.zeros(N)

    #    tx_img_range = img_coords[2]
    r = img_coords[2] + dy / 2
    has_overflowed = False
    for row in range(Ny):
        row_data = image_window[:, row]

        x_cut = r * np.sin(tx_angle / 2)

        #          x_cut_rel = x_cut / extent[0]
        #          w_idx = np.linspace(0,1,N)

        x_old[:] = np.linspace(x_mean - (2 - width) * x_cut,
                               x_mean + (2 - width) * x_cut,
                               N)  #*0.5/img_width + 0.5
        x_new[:] = np.linspace(img_coords[0], img_coords[1],
                               Nx)  #*0.5/img_width + 0.5

        up = x_new[x_new > x_old[0]]
        updown = up[up <= x_old[-1]]

        #       print(x_cut, updown.shape[0])

        Nlower = Nx - up.shape[0]
        Nhigher = Nx - updown.shape[0] - Nlower

        transition = (0.5 + 0.5 * np.cos(
            np.linspace(0, np.pi, int(Nx * width * x_cut /
                                      (2 * img_width))))) * scale + (1 - scale)

        #       fn = None
        #       if row == 0:
        #          fn = pl.figure()
        #          ax = fn.add_subplot(121)
        #          ax.plot(transition)

        Nt = transition.shape[0]
        if N <= 2 * Nt:
            w = np.ones(N)
            has_overflowed = True
        else:
            w = np.hstack(
                (2 - scale - transition, np.ones(N - 2 * Nt), transition))

        w_new_center = np.interpolate1D(x_old,
                                        w,
                                        updown,
                                        kind='linear',
                                        fill_value=1 - scale)

        w_new = np.hstack((np.ones(Nlower) * (1 - scale), w_new_center,
                           np.ones(Nhigher) * (1 - scale)))

        image_window[:, row] = w_new

        #       if row == 0:
        #          ax = fn.add_subplot(122)
        #          ax.plot(w_new)
        #          fn.savefig('test2.eps')

        r = r + dy

    if has_overflowed:
        print("WARNING: Not making a window")

    return image_window