Exemple #1
0
    def func(self, X, V):
        k = self.C.TFdata.k
        v1 = self.C.TFdata.v1
        w1 = self.C.TFdata.w1
        
        if k >=0:
            J_coords = self.F.sysfunc.J_coords
            w = sqrt(k)
        
            q = v1 - (1j/w)*matrixmultiply(self.F.sysfunc.J_coords,v1)
            p = w1 + (1j/w)*matrixmultiply(transpose(self.F.sysfunc.J_coords),w1)
            
            p /= linalg.norm(p)
            q /= linalg.norm(q)

            p = reshape(p,(p.shape[0],))
            q = reshape(q,(q.shape[0],))
            
            direc = conjugate(1/matrixmultiply(transpose(conjugate(p)),q))
            p = direc*p

            l1 = firstlyapunov(X, self.F.sysfunc, w, J_coords=J_coords, p=p, q=q)
            
            return array([l1])
        else:
            return array([1])
Exemple #2
0
def sem(im, direction='X'):
    r"""
    Simulates an SEM photograph looking into the porous material in the
    specified direction.  Features are colored according to their depth into
    the image, so darker features are further away.

    Parameters
    ----------
    im : array_like
        ND-image of the porous material with the solid phase marked as 1 or
        True

    direction : string
        Specify the axis along which the camera will point.  Options are
        'X', 'Y', and 'Z'.

    Returns
    -------
    A 2D greyscale image suitable for use in matplotlib\'s ```imshow```
    function.
    """
    im = sp.array(~im, dtype=int)
    if direction in ['Y', 'y']:
        im = sp.transpose(im, axes=[1, 0, 2])
    if direction in ['Z', 'z']:
        im = sp.transpose(im, axes=[2, 1, 0])
    t = im.shape[0]
    depth = sp.reshape(sp.arange(0, t), [t, 1, 1])
    im = im * depth
    im = sp.amax(im, axis=0)
    return im
Exemple #3
0
def xray(im, direction='X'):
    r"""
    Simulates an X-ray radiograph looking through the porouls material in the
    specfied direction.  The resulting image is colored according to the amount
    of attenuation an X-ray would experience, so regions with more solid will
    appear darker.

    Parameters
    ----------
    im : array_like
        ND-image of the porous material with the solid phase marked as 1 or
        True

    direction : string
        Specify the axis along which the camera will point.  Options are
        'X', 'Y', and 'Z'.

    Returns
    -------
    A 2D greyscale image suitable for use in matplotlib\'s ```imshow```
    function.
    """
    im = sp.array(~im, dtype=int)
    if direction in ['Y', 'y']:
        im = sp.transpose(im, axes=[1, 0, 2])
    if direction in ['Z', 'z']:
        im = sp.transpose(im, axes=[2, 1, 0])
    im = sp.sum(im, axis=0)
    return im
Exemple #4
0
    def LonVsLatArray(self):
        """  """

        self.glatbins = arange(self.glatlim[0], self.glatlim[1] + self.glatstp,
                               self.glatstp)

        for glat in self.glatbins:

            hwm14Obj = HWM14(alt=self.alt,
                             ap=self.ap,
                             glat=glat,
                             glonlim=self.glonlim,
                             glonstp=self.glonstp,
                             option=4,
                             verbose=self.verbose,
                             ut=self.ut)

            Uwind = reshape(hwm14Obj.Uwind, (len(hwm14Obj.Uwind), 1))
            Vwind = reshape(hwm14Obj.Vwind, (len(hwm14Obj.Vwind), 1))
            self.Uwind = Uwind if glat == self.glatlim[0] else append(
                self.Uwind, Uwind, axis=1)
            self.Vwind = Vwind if glat == self.glatlim[0] else append(
                self.Vwind, Vwind, axis=1)

        self.glonbins = hwm14Obj.glonbins

        self.Uwind = transpose(self.Uwind)
        self.Vwind = transpose(self.Vwind)
Exemple #5
0
    def func(self, X, V):
        H = self.F.sysfunc.hess(X, self.F.coords, self.F.coords)
        q = self.F.testfunc.data.v/linalg.norm(self.F.testfunc.data.v)
        p = self.F.testfunc.data.w/matrixmultiply(transpose(self.F.testfunc.data.w),q)

        return array(0.5*matrixmultiply(transpose(p), reshape([bilinearform(H[i,:,:], q, q) \
             for i in range(H.shape[0])],(H.shape[0],1)))[0])
Exemple #6
0
def Problem3Real():
    beta = 0.9
    N = 1000
    u = lambda c: sp.sqrt(c)
    W = sp.linspace(0,1,N)
    X, Y = sp.meshgrid(W,W)
    Wdiff = sp.transpose(X-Y)
    index = Wdiff <0
    Wdiff[index] = 0
    util_grid = u(Wdiff)
    util_grid[index] = -10**10
    
    Vprime = sp.zeros((N,1))
    psi = sp.zeros((N,1))
    delta = 1.0
    tol = 10**-9
    it = 0
    max_iter = 500
    
    while (delta >= tol) and (it < max_iter):
        V = Vprime
        it += 1;
        #print(it)
        val = util_grid + beta*sp.transpose(V)
        Vprime = sp.amax(val, axis = 1)
        Vprime = Vprime.reshape((N,1))
        psi_ind = sp.argmax(val,axis = 1)
        psi    = W[psi_ind]
        delta = sp.dot(sp.transpose(Vprime - V),Vprime-V)
    
    return psi
def get_data(**kwargs):
    data_file_name = kwargs['file_name'] + '.input'
    true_file_name = kwargs['file_name'] + '.true'

    reads_matrix = pd.DataFrame.from_csv(data_file_name, sep="\t").as_matrix()
    var_reads = [scipy.transpose(reads_matrix[:,1])]
    ref_reads = [scipy.transpose(reads_matrix[:,0])]
    # Initialize the VAF_matrix, so skip the first two columns
    VAF_matrix = scipy.transpose( reads_matrix[:,1]/ map(float, reads_matrix[:,0]))
    # Iterate over columns minus the first two
    reads_matrix = reads_matrix.T[2:]
    for i, column in enumerate(reads_matrix):
        # Skip every other column
        if i % 2 != 0:
            continue
        var_reads.append(reads_matrix[i+1])
        ref_reads.append(reads_matrix[i])
        VAF_matrix = scipy.vstack((VAF_matrix, reads_matrix[i+1]/map(float, reads_matrix[i])))

    var_reads = map(scipy.ndarray.tolist, var_reads)
    ref_reads = map(scipy.ndarray.tolist, ref_reads)

    true_VAFs, true_clusters = read_true_file(true_file_name)
    Data = bnpy.data.XData(X=scipy.transpose(scipy.matrix(VAF_matrix)))

    return (Data, var_reads, ref_reads, true_clusters, true_VAFs)
Exemple #8
0
def mlr(x,y,order):
    """Multiple linear regression fit of the columns of matrix x 
    (dependent variables) to constituent vector y (independent variables)
    
    order -     order of a smoothing polynomial, which can be included 
                in the set of independent variables. If order is
                not specified, no background will be included.
    b -         fit coeffs
    f -         fit result (m x 1 column vector)
    r -         residual   (m x 1 column vector)
    """
    
    if order > 0:
        s=scipy.ones((len(y),1))
        for j in range(order):
            s=scipy.concatenate((s,(scipy.arange(0,1+(1.0/(len(y)-1)),1.0/(len(y)-1))**j)[:,nA]),1)
        X=scipy.concatenate((x, s),1)
    else:
        X = x
    
    #calc fit b=fit coefficients
    b = scipy.dot(scipy.dot(scipy.linalg.pinv(scipy.dot(scipy.transpose(X),X)),scipy.transpose(X)),y)
    f = scipy.dot(X,b)
    r = y - f

    return b,f,r
def plot_disc_policy():
    #First compute policy function...==========================================
    N = 500
    w = sp.linspace(0,100,N)
    w = w.reshape(N,1)
    u = lambda c: sp.sqrt(c)
    util_vec = u(w)
    alpha = 0.5
    alpha_util = u(alpha*w)
    alpha_util_grid = sp.repeat(alpha_util,N,1)
    
    m = 20
    v = 200
    f = discretelognorm(w,m,v)
    
    VEprime = sp.zeros((N,1))
    VUprime    = sp.zeros((N,N))
    EVUprime = sp.zeros((N,1))
    psiprime = sp.ones((N,1))
    gamma = 0.1
    beta = 0.9
    
    m = 15
    tol = 10**-9
    delta = 1+tol
    it = 0
    while (delta >= tol):
        it += 1
        
        psi = psiprime.copy()
        arg1 = sp.repeat(sp.transpose(VEprime),N,0)
        arg2 = sp.repeat(EVUprime,N,1)
        arg = sp.array([arg2,arg1])
        psiprime = sp.argmax(arg,axis = 0) 
        
        for j in sp.arange(0,m):
            VE = VEprime.copy()
            VU = VUprime.copy()
            EVU = EVUprime.copy()
            VEprime = util_vec + beta*((1-gamma)*VE + gamma*EVU)
            arg1 = sp.repeat(sp.transpose(VE),N,0)*psiprime
            arg2 = sp.repeat(EVU,N,1)*(1-psiprime)
            arg = arg1+arg2
            VUprime = alpha_util_grid + beta*arg
            EVUprime = sp.dot(VUprime,f)  
    
        
    
        delta = sp.linalg.norm(psiprime -psi) 

    wr_ind = sp.argmax(sp.diff(psiprime), axis = 1)
    wr = w[wr_ind]
    print w[250],wr[250]
        
    #Then plot=================================================================
    plt.plot(w,psiprime[250,:]) 
    plt.ylim([-.5,1.5])      
    plt.xlabel(r'$w\prime$')
    plt.yticks([0,1])
    plt.savefig('disc_policy.pdf')
Exemple #10
0
 def updatedata(self, A):
     if self.update:
         if self.corr:
             self.data.B = self.data.w*(linalg.norm(A,1)/linalg.norm(self.data.w,1))
             self.data.C = self.data.v*(linalg.norm(A,Inf)/linalg.norm(self.data.v,1))
         else:
             # Note: Problem when singular vectors switch smallest singular value (See NewLorenz).
             #       To overcome this, I have implemented a 1e-8 random nudge.
             try:
                 ALU = linalg.lu_factor(A)
                 BC = linalg.lu_solve(ALU, c_[linalg.lu_solve(ALU, self.data.B + 1e-8*self.data.Brand), \
                                      self.data.C + 1e-8*self.data.Crand], trans=1)
                 C = linalg.lu_solve(ALU, BC[:,-1*self.data.q:])
                 B = BC[:,0:self.data.p]
             except:
                 if self.C.verbosity >= 1:
                     print 'Warning: Problem updating border vectors.  Using svd...'
                 U, S, Vh = linalg.svd(A)
                 B = U[:,-1*self.data.p:]
                 C = num_transpose(Vh)[:,-1*self.data.q:]
         
             bmult = cmult = 1
             if matrixmultiply(transpose(self.data.B), B) < 0:
                 bmult = -1
             if matrixmultiply(transpose(self.data.C), C) < 0:
                 cmult = -1
             self.data.B = bmult*B*(linalg.norm(A,1)/linalg.norm(B))
             self.data.C = cmult*C*(linalg.norm(A,Inf)/linalg.norm(C))
def Problem3Real():
    beta = 0.9
    N = 1000
    u = lambda c: sp.sqrt(c)
    W = sp.linspace(0, 1, N)
    X, Y = sp.meshgrid(W, W)
    Wdiff = sp.transpose(X - Y)
    index = Wdiff < 0
    Wdiff[index] = 0
    util_grid = u(Wdiff)
    util_grid[index] = -10**10

    Vprime = sp.zeros((N, 1))
    psi = sp.zeros((N, 1))
    delta = 1.0
    tol = 10**-9
    it = 0
    max_iter = 500

    while (delta >= tol) and (it < max_iter):
        V = Vprime
        it += 1
        #print(it)
        val = util_grid + beta * sp.transpose(V)
        Vprime = sp.amax(val, axis=1)
        Vprime = Vprime.reshape((N, 1))
        psi_ind = sp.argmax(val, axis=1)
        psi = W[psi_ind]
        delta = sp.dot(sp.transpose(Vprime - V), Vprime - V)

    return psi
Exemple #12
0
 def updatedata(self, A):
     # Update b, c
     try:
         ALU = linalg.lu_factor(A)
         BC = linalg.lu_solve(ALU, c_[linalg.lu_solve(ALU, self.data.b + 1e-8*self.data.Brand[:,:1]), \
                              self.data.c + 1e-8*self.data.Crand[:,:1]], trans=1)
         C = linalg.lu_solve(ALU, BC[:,-1:])
         B = BC[:,:1]
     except:
         if self.C.verbosity >= 1:
             print 'Warning: Problem updating border vectors.  Using svd...'
         U, S, Vh = linalg.svd(A)
         B = U[:,-1:]
         C = num_transpose(Vh)[:,-1:]
 
     bmult = cmult = 1
     if matrixmultiply(transpose(self.data.b), B) < 0:
         bmult = -1
     if matrixmultiply(transpose(self.data.c), C) < 0:
         cmult = -1
     self.data.b = bmult*B*(linalg.norm(A,1)/linalg.norm(B))
     self.data.c = cmult*C*(linalg.norm(A,Inf)/linalg.norm(C))
     
     # Update
     if self.update:
         self.data.B[:,0] = self.data.b*(linalg.norm(A,1)/linalg.norm(self.data.b))
         self.data.C[:,0] = self.data.c*(linalg.norm(A,Inf)/linalg.norm(self.data.c))
         
         self.data.B[:,1] = self.data.w[:,2]*(linalg.norm(A,1)/linalg.norm(self.data.w,1))
         self.data.C[:,1] = self.data.v[:,2]*(linalg.norm(A,Inf)/linalg.norm(self.data.v,1))
         
         self.data.D[0,1] = self.data.g[0,1]
         self.data.D[1,0] = self.data.g[1,0]
def update_image(original_im, ci_red, ci_green, ci_blue):

    # diagnostics = dict()

    original_im = scipy.transpose(original_im)
    # diagnostics['original_im'] = original_im
    # diagnostics['ci_red'] = ci_red
    # diagnostics['ci_green'] = ci_green
    # diagnostics['ci_blue'] = ci_blue

    new_r = scipy.multiply(original_im[0], original_im[0] > ci_red)

    new_g = scipy.multiply(original_im[1], original_im[1] > ci_green)

    new_b = scipy.multiply(original_im[2], original_im[2] > ci_blue)

    new_im = (new_r, new_g, new_b)

    new_im = scipy.transpose(new_im)
    # diagnostics['new_im'] = new_im

    # with open('/Users/lages/Documents/sauceda/pictures_processed/diagnostics'
    #           '.p', 'wb') as f:
    #     pickle.dump(diagnostics, f)

    return new_im
Exemple #14
0
def baseline1(myarray):
    """Set first bin of each row to zero
    """
    size = myarray.shape
    take_array = scipy.transpose(
        scipy.resize(scipy.transpose(myarray[:, 0]), (size[1], size[0])))
    return myarray - take_array
Exemple #15
0
def _padarray(myarray, frame, type):
    """Used in a number of funcs to pad out array cols at start and
    end so that the original shape of the array is maintained
    following processing"""
    (div, mod) = divmod(frame,
                        2)  #pad array to keep original shape after averaging
    if mod <> 0:
        pad = (frame - 1) / 2
    else:
        pad = frame / 2
    size = myarray.shape
    if type == 'av':
        start = scipy.transpose(
            scipy.resize(scipy.transpose(scipy.mean(myarray[:, 0:pad], 1)),
                         (pad, size[0])))
        end = scipy.transpose(
            scipy.resize(
                scipy.transpose(
                    scipy.mean(myarray[:, size[1] - pad:size[1]], 1)),
                (pad, size[0])))
    elif type == 'zero':
        start = end = scipy.transpose(
            scipy.resize(scipy.zeros((size[0], 1)), (pad, size[0])))
    padarray = scipy.concatenate((start, myarray, end), 1)

    return padarray, size
Exemple #16
0
def hyperplane_equation_by_nullspace_from_points(points, t_equivalence='True'):
    t_ = []
    if t_equivalence == True:
        debug("original points:\n", points)
        for point in points:
            t_.append(eT(point))
        points = t_
    debug("points:\n", points)
    try:
        debug("determinant of points:", scipy.linalg.det(points))
    except:
        pass
    homogeneous_points = []
    for point in points:
        homogeneous_points.append(scipy.append(point, [1]))
    debug("homogeneous points:", homogeneous_points)
    nullspace = scipy.linalg.null_space(homogeneous_points)
    nullspace = scipy.ndarray.flatten(nullspace[:-1])
    debug("nullspace from homogeneous points:", nullspace)
    constant_term = scipy.dot(scipy.transpose(nullspace), points[0])
    debug("constant_term:", constant_term)
    norm = scipy.linalg.norm(nullspace)
    debug("norm:", norm)
    unit_normal = scipy.divide(nullspace, norm)
    print("unit normal vector:", unit_normal)
    constant_term = scipy.dot(scipy.transpose(unit_normal), points[0])
    print("constant_term:", constant_term)
    return unit_normal, constant_term
Exemple #17
0
def simplicial_grid_2d(n):
    """
    Create an NxN 2d grid in the unit square
    
    The number of vertices along each axis is (N+1) for a total of (N+1)x(N+1) vertices
    
    A tuple (vertices,indices) of arrays is returned
    """
    vertices = zeros(((n + 1)**2, 2))
    vertices[:, 0] = ravel(resize(arange(n + 1), (n + 1, n + 1)))
    vertices[:, 1] = ravel(transpose(resize(arange(n + 1), (n + 1, n + 1))))
    vertices /= n

    indices = zeros((2 * (n**2), 3), scipy.int32)

    t1 = transpose(
        concatenate((matrix(arange(n)), matrix(arange(
            1, n + 1)), matrix(arange(n + 2, 2 * n + 2))),
                    axis=0))
    t2 = transpose(
        concatenate((matrix(arange(n)), matrix(arange(
            n + 2, 2 * n + 2)), matrix(arange(n + 1, 2 * n + 1))),
                    axis=0))
    first_row = concatenate((t1, t2))

    for i in xrange(n):
        indices[(2 * n * i):(2 * n * (i + 1)), :] = first_row + i * (n + 1)

    return (vertices, indices)
Exemple #18
0
def sqcover(A,n):
    edge = sp.sqrt(A) # the length of an edge
    d = edge/n # the distance between two adjacent points
    r = d/2 # the "radius of "
    end = edge - r # end point
    base = sp.linspace(r, end, n)
    first_line = sp.transpose(sp.vstack((base, r*sp.ones(n))))
    increment = sp.transpose(sp.vstack((sp.zeros(n), d*sp.ones(n))))
    pts = first_line
    y_diff = increment
    for i in range(n-1):
        pts = sp.vstack((pts, first_line + y_diff))
        y_diff = y_diff + increment
    
    # Color matter
    colors = []
    for p in pts:
        cval = n*p[0] + p[1] # the x-coord has a higher weight
        cval = colormap.Spectral(cval/((n+1)*end)) # normalize by the max value that cval can take.
        colors.append(cval)

    colors = sp.array(colors)

    cover = (pts, r, colors)
    return cover
Exemple #19
0
def plot_delta():     
    beta = 0.99
    N = 1000
    u = lambda c: sp.sqrt(c)
    W = sp.linspace(0,1,N)
    X, Y = sp.meshgrid(W,W)
    Wdiff = sp.transpose(X-Y)
    index = Wdiff <0
    Wdiff[index] = 0
    util_grid = u(Wdiff)
    util_grid[index] = -10**10
    
    Vprime = sp.zeros((N,1))
    delta = sp.ones(1)
    tol = 10**-9
    it = 0
    max_iter = 500
    
    while (delta[-1] >= tol) and (it < max_iter):
        V = Vprime
        it += 1;
        print(it)
        val = util_grid + beta*sp.transpose(V)
        Vprime = sp.amax(val, axis = 1)
        Vprime = Vprime.reshape((N,1))
        delta = sp.append(delta,sp.dot(sp.transpose(Vprime - V),Vprime-V))
        
    plt.figure()
    plt.plot(delta[1:])
    plt.ylabel(r'$\delta_k$')
    plt.xlabel('iteration')
    plt.savefig('convergence.pdf')
def plot_delta():     
    beta = 0.99
    N = 1000
    u = lambda c: sp.sqrt(c)
    W = sp.linspace(0,1,N)
    X, Y = sp.meshgrid(W,W)
    Wdiff = sp.transpose(X-Y)
    index = Wdiff <0
    Wdiff[index] = 0
    util_grid = u(Wdiff)
    util_grid[index] = -10**10
    
    Vprime = sp.zeros((N,1))
    delta = sp.ones(1)
    tol = 10**-9
    it = 0
    max_iter = 500
    
    while (delta[-1] >= tol) and (it < max_iter):
        V = Vprime
        it += 1;
        print(it)
        val = util_grid + beta*sp.transpose(V)
        Vprime = sp.amax(val, axis = 1)
        Vprime = Vprime.reshape((N,1))
        delta = sp.append(delta,sp.dot(sp.transpose(Vprime - V),Vprime-V))
        
    plt.figure()
    plt.plot(delta[1:])
    plt.ylabel(r'$\delta_k$')
    plt.xlabel('iteration')
    plt.savefig('convergence.pdf')
Exemple #21
0
    def load(self):
        self.load_images()
        self.load_logfile()

        self.times = set()
        for ts in self.timestamp_to_image.keys():
            self.times.add(ts)
        for ts in self.timestamp_to_fread.keys():
            self.times.add(ts)
        for ts in self.timestamp_to_odom.keys():
            self.times.add(ts)

        self.times = list(self.times)
        self.times.sort()
        self.times = array(self.times)

        #get freadings XY representation
        t_to_f = self.timestamp_to_fread
        self.freadings_XY = transpose(
            [[t_to_f[i].location.x, t_to_f[i].location.y]
             for i in sorted(self.timestamp_to_fread.keys())])

        self.freadings_Th = transpose([
            t_to_f[i].location.theta
            for i in sorted(self.timestamp_to_fread.keys())
        ])
Exemple #22
0
def getHessFull(gammas=None, amounts=None, numExps=3):
    if gammas is None:
        gammas = getGammas(numExps)
    if amounts is None:
        amounts = getAmounts(numExps)

    numExps = scipy.size(gammas)

    hessgg = getHessGG(gammas)

    hessAA = getHessAA(amounts, gammas)

    numerAg1 = -scipy.outer(amounts, amounts * gammas)
    denomAg1 = scipy.outer(scipy.ones(numExps), gammas)
    denomAg2 = scipy.transpose(denomAg1) + denomAg1
    denomAg3 = denomAg2 * denomAg2
    hessAg = old_div(numerAg1, denomAg3)

    numergA1 = -scipy.outer(amounts * gammas, amounts)
    denomgA1 = scipy.outer(gammas, scipy.ones(numExps))
    denomgA2 = scipy.transpose(denomgA1) + denomgA1
    denomgA3 = denomgA2 * denomgA2
    hessgA = old_div(numergA1, denomgA3)

    hessFull = scipy.bmat([[hessAA, hessAg], [hessgA, hessgg]])

    return hessFull
Exemple #23
0
def calc_BB_Y_2s_ham_3s(A_m1, A_p2, C, C_m1, Vlh, Vrh_p1, l_m2, r_p2, l_s_m1,
                        l_si_m1, r_s_p1, r_si_p1):
    Vr_p1 = sp.transpose(Vrh_p1, axes=(0, 2, 1)).conj()

    Vrri_p1 = sp.zeros_like(Vr_p1)
    try:
        for s in xrange(Vrri_p1.shape[0]):
            Vrri_p1[s] = r_si_p1.dot_left(Vr_p1[s])
    except AttributeError:
        for s in xrange(Vrri_p1.shape[0]):
            Vrri_p1[s] = Vr_p1[s].dot(r_si_p1)

    Vl = sp.transpose(Vlh, axes=(0, 2, 1)).conj()
    liVl = sp.zeros_like(Vl)
    for s in xrange(liVl.shape[0]):
        liVl[s] = l_si_m1.dot(Vl[s])

    Y = sp.zeros((Vlh.shape[1], Vrh_p1.shape[2]), dtype=Vrh_p1.dtype)
    if not A_p2 is None:
        for s in xrange(C.shape[0]):
            Y += Vlh[s].dot(
                l_s_m1.dot(eps_r_op_2s_C12(r_p2, C[s], Vrri_p1, A_p2)))
    if not A_m1 is None:
        for u in xrange(C_m1.shape[2]):
            Y += eps_l_op_2s_A1_A2_C34(l_m2, A_m1, liVl,
                                       C_m1[:, :,
                                            u]).dot(r_s_p1.dot(Vrh_p1[u]))

    etaBB_sq = mm.adot(Y, Y)

    return Y, etaBB_sq
def trueFeatureStats(T, R, fMap, discountFactor, stateProp=1, MAT_LIMIT=1e8):
    """ Gather the statistics needed for LSTD,
    assuming infinite data (true probabilities).
    Option: if stateProp is  < 1, then only a proportion of all 
    states will be seen as starting state for transitions """
    dim = len(fMap)
    numStates = len(T)
    statMatrix = zeros((dim, dim))
    statResidual = zeros(dim)
    ss = range(numStates)
    repVersion = False
    
    if stateProp < 1:
        ss = random.sample(ss, int(numStates * stateProp))
    elif dim * numStates**2 < MAT_LIMIT:
        repVersion = True
    
    # two variants, depending on how large we can afford our matrices to become.        
    if repVersion:    
        tmp1 = tile(fMap, (numStates,1,1))
        tmp2 = transpose(tmp1, (2,1,0))
        tmp3 = tmp2 - discountFactor * tmp1            
        tmp4 = tile(T, (dim,1,1))
        tmp4 *= transpose(tmp1, (1,2,0))
        statMatrix = tensordot(tmp3, tmp4, axes=[[0,2], [1,2]]).T
        statResidual = dot(R, dot(fMap, T).T)
    else:
        for sto in ss:
            tmp = fMap - discountFactor * repmat(fMap[:, sto], numStates, 1).T
            tmp2 = fMap * repmat(T[:, sto], dim, 1)
            statMatrix += dot(tmp2, tmp.T)             
            statResidual += R[sto] * dot(fMap, T[:, sto])
    return statMatrix, statResidual
Exemple #25
0
    def LonVsHeiArray(self):
        """     """

        self.altbins = arange(self.altlim[0], self.altlim[1] + self.altstp,
                              self.altstp)

        for alt in self.altbins:

            if True:

                hwm14Obj = HWM14(alt=alt,
                                 ap=self.ap,
                                 glat=self.glat,
                                 glonlim=self.glonlim,
                                 glonstp=self.glonstp,
                                 option=self.option,
                                 verbose=self.verbose,
                                 ut=self.ut)

            else:

                pass

            Uwind = reshape(hwm14Obj.Uwind, (len(hwm14Obj.Uwind), 1))
            Vwind = reshape(hwm14Obj.Vwind, (len(hwm14Obj.Vwind), 1))
            self.Uwind = Uwind if alt == self.altlim[0] else append(
                self.Uwind, Uwind, axis=1)
            self.Vwind = Vwind if alt == self.altlim[0] else append(
                self.Vwind, Vwind, axis=1)

        self.glonbins = hwm14Obj.glonbins

        self.Uwind = transpose(self.Uwind)
        self.Vwind = transpose(self.Vwind)
Exemple #26
0
    def read_input_data(self):
        """
            On init, read the data file.
            """
        true_file_name = self.data_dir + ".input"
        reads_matrix = pd.DataFrame.from_csv(true_file_name,
                                             sep="\t").as_matrix()
        var_reads = [scipy.transpose(reads_matrix[:, 1])]
        ref_reads = [scipy.transpose(reads_matrix[:, 0])]
        # Initialize the VAF_matrix, so skip the first two columns
        VAF_matrix = scipy.transpose(reads_matrix[:, 1] /
                                     map(float, reads_matrix[:, 0]))
        # Iterate over columns minus the first two
        reads_matrix = reads_matrix.T[2:]
        for i, column in enumerate(reads_matrix):
            # Skip every other column
            if i % 2 != 0:
                continue
            var_reads.append(reads_matrix[i + 1])
            ref_reads.append(reads_matrix[i])
            VAF_matrix = scipy.vstack(
                (VAF_matrix,
                 reads_matrix[i + 1] / map(float, reads_matrix[i])))

        var_reads = map(scipy.ndarray.tolist, var_reads)
        ref_reads = map(scipy.ndarray.tolist, ref_reads)

        self.VAFs = VAF_matrix
        self.reads_matrix = reads_matrix
        self.var_reads = var_reads
        self.ref_reads = ref_reads
Exemple #27
0
    def mlr(self, x, y):
        """Multiple linear regression fit of the columns of matrix x
		(dependent variables) to constituent vector y (independent variables)

		order -     order of a smoothing polynomial, which can be included
					in the set of independent variables. If order is
					not specified, no background will be included.
		b -         fit coeffs
		f -         fit result (m x 1 column vector)
		r -         residual   (m x 1 column vector)
		"""

        if self.order > 0:
            s = scipy.ones((len(y), 1))
            for j in range(self.order):
                s = scipy.concatenate(
                    (s, (scipy.arange(0, 1 + (1.0 / (len(y) - 1)), 1.0 /
                                      (len(y) - 1))**j)[:, nA]), 1)
            X = scipy.concatenate((x, s), 1)
        else:
            X = x

        #calc fit b=fit coefficients
        b = scipy.dot(
            scipy.dot(scipy.linalg.pinv(scipy.dot(scipy.transpose(X), X)),
                      scipy.transpose(X)), y)
        f = scipy.dot(X, b)
        r = y - f

        return b, f, r
Exemple #28
0
    def sdc_to_distributions(self, mysdc):
        """
        Convert the SDC to distributions used in the inference.    
        """
        print "*******************************"
        if "right" in mysdc["verb"]:
            D_mat = transpose(self.T_mat_right)
        elif "left" in mysdc["verb"]:
            D_mat = transpose(self.T_mat_left)
        elif "turn_around" in mysdc["verb"]:
            D_mat = transpose(self.T_mat_back * self.T_mat_face)
        elif "face" in mysdc["verb"]:
            print "using face"
            D_mat = transpose(self.T_mat_face)
        else:
            D_mat = transpose(self.T_mat_str)
            D_mat = ones(self.T_mat_str.shape)

        print "before vertical"
        print_tmat(transpose(D_mat), 40, 42)

        if "up" in mysdc["verb"]:
            print "using up"
            D_mat = D_mat * transpose(self.T_mat_up)
        elif "down" in mysdc["verb"]:
            D_mat = D_mat * transpose(self.T_mat_down)
        else:
            print "using stay"
            D_mat = D_mat * transpose(self.T_mat_stay)

        print "dmat final"
        print_tmat(transpose(D_mat), 40, 42)

        T_mat = ones([len(D_mat), len(D_mat[0])]) * 1.0

        if mysdc["sr"] != None and len(
                mysdc["landmarks"]) > 0 and self.use_spatial_relations:
            sr_i = self.sr_class.engineToIdx(mysdc["sr"])
            SR_mat = self.srel_mat[sr_i, :, :, :]
            L_mat = self.get_prob_landmark_given_sdc_modifiers(mysdc)
        else:
            SR_mat = None
            L_mat = None

        if mysdc["landmark"] != None:
            landmark_i = self.names_to_index[mysdc["landmark"]]
            O_mat_oriented = self.O_mat_oriented[:, landmark_i]
            O_mat_topo = self.O_mat[:, landmark_i]
            if "face" in mysdc["verb"]:
                print "using O_mat_oriented"
                O_mat = O_mat_oriented
            else:
                O_mat = O_mat_topo
            print "shape", O_mat_topo.shape
            print "O_mat_topo", O_mat_topo[18]
            print "O_mat_oriented", O_mat_oriented[18]
        else:
            O_mat = None

        return O_mat, T_mat, SR_mat, L_mat, D_mat
Exemple #29
0
def calc_BB_Y_2s_ham_3s(A_m1, A_p2, C, C_m1, Vlh, Vrh_p1, l_m2, r_p2, l_s_m1, l_si_m1, r_s_p1, r_si_p1):
    Vr_p1 = sp.transpose(Vrh_p1, axes=(0, 2, 1)).conj()
    
    Vrri_p1 = sp.zeros_like(Vr_p1)
    try:
        for s in xrange(Vrri_p1.shape[0]):
            Vrri_p1[s] = r_si_p1.dot_left(Vr_p1[s])
    except AttributeError:
        for s in xrange(Vrri_p1.shape[0]):
            Vrri_p1[s] = Vr_p1[s].dot(r_si_p1)
    
    Vl = sp.transpose(Vlh, axes=(0, 2, 1)).conj()        
    liVl = sp.zeros_like(Vl)            
    for s in xrange(liVl.shape[0]):
        liVl[s] = l_si_m1.dot(Vl[s])

    Y = sp.zeros((Vlh.shape[1], Vrh_p1.shape[2]), dtype=Vrh_p1.dtype)
    if not A_p2 is None:
        for s in xrange(C.shape[0]):
            Y += Vlh[s].dot(l_s_m1.dot(eps_r_op_2s_C12(r_p2, C[s], Vrri_p1, A_p2)))
    if not A_m1 is None:
        for u in xrange(C_m1.shape[2]):
            Y += eps_l_op_2s_A1_A2_C34(l_m2, A_m1, liVl, C_m1[:, :, u]).dot(r_s_p1.dot(Vrh_p1[u]))

    etaBB_sq = mm.adot(Y, Y)
    
    return Y, etaBB_sq
Exemple #30
0
def plot_resid(d, savename='resfig1.png'):
    """
        Plots the residual frequency after the first wipe using the TLE velocity.
    """
    flim = [-2.e3, 2.e3]
    t = d['tvec']

    dates = [dt.datetime.fromtimestamp(ts) for ts in t]
    datenums = md.date2num(dates)
    xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')

    fig1 = plt.figure(figsize=(7, 9))
    doppler_residual = sp.interpolate.interp1d(d['tvec'], d['dopfit'])
    fvec = d["fvec"]
    res0 = d["res0"]
    res1 = d["res1"]
    plt.subplot(211)
    mesh = plt.pcolormesh(datenums,
                          fvec,
                          sp.transpose(10. * sp.log10(res0 + 1e-12)),
                          vmin=-5,
                          vmax=25)
    plt.plot(datenums, (150.0 / 400.0) * doppler_residual(t),
             "r--",
             label="doppler resid")
    ax = plt.gca()
    ax.xaxis.set_major_formatter(xfmt)
    plt.ylim(flim)
    plt.subplots_adjust(bottom=0.2)
    plt.xticks(rotation=25)
    plt.xlabel("UTC")
    plt.ylabel("Frequency (Hz)")
    plt.title("Power ch0 (dB) %1.2f MHz" % (150.012))
    plt.legend()
    plt.colorbar(mesh, ax=ax)

    # quicklook spectra of residuals spectra along with measured Doppler residual from second channel.
    plt.subplot(212)
    mesh = plt.pcolormesh(datenums,
                          fvec,
                          sp.transpose(10. * sp.log10(res1 + 1e-12)),
                          vmin=-5,
                          vmax=25)
    plt.plot(datenums, doppler_residual(t), "r--", label="doppler resid")
    ax = plt.gca()
    ax.xaxis.set_major_formatter(xfmt)
    plt.ylim(flim)
    plt.xlabel("UTC")
    plt.ylabel("Frequency (Hz)")
    plt.title("Power ch1 (dB), %1.2f MHz" % (400.032))
    plt.subplots_adjust(bottom=0.2)
    plt.xticks(rotation=25)
    plt.legend()
    plt.colorbar(mesh, ax=ax)

    plt.tight_layout()
    print('Saving residual plots: ' + savename)
    plt.savefig(savename, dpi=300)
    plt.close(fig1)
def __interpolateBetweenBinaryObjects(obj1, obj2, slices):
    """
    Takes two binary objects and puts slices slices in-between them, each of which
    contains a smooth binary transition between the objects.
    @note private inner function
    """
    if not obj1.shape == obj2.shape:
        raise AttributeError(
            "The two supplied objects have to be of the same shape, not {} and {}.".format(obj1.shape, obj2.shape)
        )

    # constant
    offset = 0.5  # must be a value smaller than the minimal distance possible
    temporal_dimension = 3

    # get all voxel position
    obj1_voxel = scipy.nonzero(obj1)
    obj2_voxel = scipy.nonzero(obj2)

    # get smallest pairwise distances between all object voxels
    distances = cdist(scipy.transpose(obj1_voxel), scipy.transpose(obj2_voxel))

    # keep for each True voxel of obj1 only the smallest distance to a True voxel in obj2
    min_distances = distances.min(1)

    # test if all seems to work
    if len(min_distances) != len(obj1_voxel[0]):
        raise Exception("Invalid number of minimal distances received.")

    # replace True voxels in obj1 with their respective distances to the True voxels in obj2
    thr_obj = obj1.copy()
    thr_obj = thr_obj.astype(scipy.float_)
    thr_obj[obj1_voxel] = min_distances
    thr_obj[obj1_voxel] += offset  # previous steps distances include zeros, therefore this is required

    # compute the step size for each slice that is added
    maximum = min_distances.max()
    step = maximum / float(slices + 1)
    threshold = maximum

    # control step: see if thr_obj really corresponds to obj1
    if not scipy.all(thr_obj.astype(scipy.bool_) == obj1.astype(scipy.bool_)):
        raise Exception("First created object does not correspond to obj1.")

    # assemble return volume
    return_volume = [thr_obj.astype(scipy.bool_)]  # corresponds to obj1
    for _ in range(slices):
        threshold -= step
        # remove all value higher than the threshold
        thr_obj[thr_obj > threshold] = 0
        # add binary volume to list /makes a copy)
        return_volume.append(thr_obj.astype(scipy.bool_))

    # add last slice (corresponds to es obj2 slice)
    thr_obj[thr_obj > offset] = 0
    return_volume.append(thr_obj.astype(scipy.bool_))

    # return binary scipy array
    return scipy.rollaxis(scipy.asarray(return_volume, dtype=scipy.bool_), 0, temporal_dimension + 1)
Exemple #32
0
def disc_policy():
    #First compute policy function...==========================================
    N = 500
    w = sp.linspace(0, 100, N)
    w = w.reshape(N, 1)
    u = lambda c: sp.sqrt(c)
    util_vec = u(w)
    alpha = 0.5
    alpha_util = u(alpha * w)
    alpha_util_grid = sp.repeat(alpha_util, N, 1)

    m = 20
    v = 200
    f = discretelognorm(w, m, v)

    VEprime = sp.zeros((N, 1))
    VUprime = sp.zeros((N, N))
    EVUprime = sp.zeros((N, 1))
    psiprime = sp.ones((N, 1))
    gamma = 0.1
    beta = 0.9

    m = 15
    tol = 10**-9
    delta = 1 + tol
    it = 0
    while (delta >= tol):
        it += 1

        psi = psiprime.copy()
        arg1 = sp.repeat(sp.transpose(VEprime), N, 0)
        arg2 = sp.repeat(EVUprime, N, 1)
        arg = sp.array([arg2, arg1])
        psiprime = sp.argmax(arg, axis=0)

        for j in sp.arange(0, m):
            VE = VEprime.copy()
            VU = VUprime.copy()
            EVU = EVUprime.copy()
            VEprime = util_vec + beta * ((1 - gamma) * VE + gamma * EVU)
            arg1 = sp.repeat(sp.transpose(VE), N, 0) * psiprime
            arg2 = sp.repeat(EVU, N, 1) * (1 - psiprime)
            arg = arg1 + arg2
            VUprime = alpha_util_grid + beta * arg
            EVUprime = sp.dot(VUprime, f)

        delta = sp.linalg.norm(psiprime - psi)

    wr_ind = sp.argmax(sp.diff(psiprime), axis=1)
    wr = w[wr_ind]
    print w[250], wr[250]

    #Then plot=================================================================
    plt.plot(w, psiprime[250, :])
    plt.ylim([-.5, 1.5])
    plt.xlabel(r'$w\prime$')
    plt.yticks([0, 1])
    plt.savefig('disc_policy.pdf')
    plt.clf()
Exemple #33
0
def calculate_ld(snps):
        #filter non binary snps
        snps_t = scipy.transpose(snps)
        snps_stand = scipy.transpose((snps_t - scipy.mean(snps, 1)) / scipy.std(snps, 1))
        r2_values =scipy.dot(snps_stand, scipy.transpose(snps_stand))
        r2_values *= (1.0 / snps.shape[1])
        r2_values **= 2
        return r2_values
Exemple #34
0
def baseline2(myarray):
    """Subtract average of the first and last bin from each bin
    """
    size = myarray.shape
    take_array = scipy.transpose(
        scipy.resize(scipy.transpose((myarray[:, 0] + myarray[:, size[1] - 1]) / 2), (size[1], size[0]))
    )
    return myarray - take_array
Exemple #35
0
def calculate_ld(snps):
        #filter non binary snps
        snps_t = scipy.transpose(snps)
        snps_stand = scipy.transpose((snps_t - scipy.mean(snps, 1)) / scipy.std(snps, 1))
        r2_values =scipy.dot(snps_stand, scipy.transpose(snps_stand))
        r2_values *= (1.0 / snps.shape[1])
        r2_values **= 2
        return r2_values
Exemple #36
0
def __interpolateBetweenBinaryObjects(obj1, obj2, slices):
    """
    Takes two binary objects and puts slices slices in-between them, each of which
    contains a smooth binary transition between the objects.
    @note private inner function
    """
    if not obj1.shape == obj2.shape:
        raise AttributeError('The two supplied objects have to be of the same shape, not {} and {}.'.format(obj1.shape, obj2.shape))
    
    # constant
    offset = 0.5 # must be a value smaller than the minimal distance possible
    temporal_dimension = 3
    
    # get all voxel position
    obj1_voxel = scipy.nonzero(obj1)
    obj2_voxel = scipy.nonzero(obj2)
    
    # get smallest pairwise distances between all object voxels
    distances = cdist(scipy.transpose(obj1_voxel),
                      scipy.transpose(obj2_voxel))
    
    # keep for each True voxel of obj1 only the smallest distance to a True voxel in obj2 
    min_distances = distances.min(1)
    
    # test if all seems to work
    if len(min_distances) != len(obj1_voxel[0]):
        raise Exception('Invalid number of minimal distances received.')
    
    # replace True voxels in obj1 with their respective distances to the True voxels in obj2
    thr_obj = obj1.copy()
    thr_obj = thr_obj.astype(scipy.float_)
    thr_obj[obj1_voxel] = min_distances
    thr_obj[obj1_voxel] += offset # previous steps distances include zeros, therefore this is required
    
    # compute the step size for each slice that is added
    maximum = min_distances.max()
    step = maximum / float(slices + 1)
    threshold = maximum
    
    # control step: see if thr_obj really corresponds to obj1
    if not scipy.all(thr_obj.astype(scipy.bool_) == obj1.astype(scipy.bool_)):
        raise Exception('First created object does not correspond to obj1.')
    
    # assemble return volume
    return_volume = [thr_obj.astype(scipy.bool_)] # corresponds to obj1
    for _ in range(slices):
        threshold -= step
        # remove all value higher than the threshold
        thr_obj[thr_obj > threshold] = 0
        # add binary volume to list /makes a copy)
        return_volume.append(thr_obj.astype(scipy.bool_)) 
    
    # add last slice (corresponds to es obj2 slice)
    thr_obj[thr_obj > offset] = 0
    return_volume.append(thr_obj.astype(scipy.bool_)) 
    
    # return binary scipy array
    return scipy.rollaxis(scipy.asarray(return_volume, dtype=scipy.bool_), 0, temporal_dimension + 1)
Exemple #37
0
def nb_vals(matrix, indices):
    matrix = scipy.array(matrix)
    indices = tuple(scipy.transpose(scipy.atleast_2d(indices)))
    arr_shape = scipy.shape(matrix)
    dist = scipy.ones(arr_shape)
    dist[indices] = 0
    dist = scipy.ndimage.distance_transform_cdt(dist, metric='chessboard')
    nb_indices = scipy.transpose(scipy.nonzero(dist == 1))
    return [matrix[tuple(ind)] for ind in nb_indices]
def least_squares_interpolant(x, y):
    xmat = sp.zeros((len(x), 2))
    for i in range(len(x)):
        for j in range(2):
            xmat[i][j] = x[i] ** j
    a = sp.matmul(sp.transpose(xmat), xmat)
    b = sp.matmul(sp.transpose(xmat), y)
    ans = numpy.linalg.solve(a, b)
    return ans, xmat
Exemple #39
0
 def to_minimize_fidelity(theta):
     temp_z_gate = np.matmul(
         sp.linalg.expm(-2 * np.pi * 1j * theta * self.H_zeeman),
         U_ideal)
     temp_m = np.matmul(sp.conjugate(sp.transpose(target)),
                        temp_z_gate)
     return np.real(1. - (sp.trace(
         np.matmul(temp_m, sp.conjugate(sp.transpose(temp_m)))) +
                          np.abs(sp.trace(temp_m))**2.) / 20.)
Exemple #40
0
def baseline2(myarray):
    """Subtract average of the first and last bin from each bin
    """
    size = myarray.shape
    take_array = scipy.transpose(
        scipy.resize(
            scipy.transpose((myarray[:, 0] + myarray[:, size[1] - 1]) / 2),
            (size[1], size[0])))
    return myarray - take_array
Exemple #41
0
def mls(p):

    data = ascii.read("datos.dat")  
    
    x=data["col1"]
    y=data["col2"]
    z=data["col3"]
    
    sig=30*(10**(-6))
    
    Y=sy.subtract(y,1)
    
    A=[]

    

    for m in x:
        
        pol=[]
        for i in range(p+1):
            pol.append(m**i)
        if m <0.4 or m>0.7:
            pol.append(0)
            A.append(pol) 
      
        else:
            pol.append(-1)
            A.append(pol)
            
    
    
    theta= sy.dot( sy.linalg.inv(sy.dot( sy.transpose(A),A )) , sy.dot(sy.transpose(A),Y) )

    modelo=[]
    
    for i in x:
        
        poli=1
        for s in range(p+1):
            poli+=(theta[s]*(i**s))    
        
        e=sy.random.normal(0,sig)
        if i <0.4 or i>0.7:
            modelo.append(poli)
      
        else:
            modelo.append(poli - theta[len(theta)-1])   
        
    
    chi2=0

    for h in range(len(x)):
        
        chi2+= ((y[h]-modelo[h]) / (sig) ) **2       
        
    return modelo, theta , len(x) ,sig ,chi2
def trim(image):  # 255 - white
    tr_image = transpose(image)
    start = 0
    while sum(tr_image[start]) == 255 * len(tr_image[start]):
        # condition on i is not needed, because of the balance between white and black
        start += 1
    finish = len(tr_image) - 1
    while sum(tr_image[finish]) == 255 * len(tr_image[finish]):
        finish -= 1
    return transpose(tr_image[start: finish + 1])
Exemple #43
0
def Problem6Real():
    N = 500
    w = sp.linspace(0,100,N)
    w = w.reshape(N,1)
    u = lambda c: sp.sqrt(c)
    util_vec = u(w)
    alpha = 0.5
    alpha_util = u(alpha*w)
    alpha_util_grid = sp.repeat(alpha_util,N,1)
    
    m = 20
    v = 200
    f = discretelognorm(w,m,v)
    
    VEprime = sp.zeros((N,1))
    VUprime    = sp.zeros((N,N))
    EVUprime = sp.zeros((N,1))
    psiprime = sp.ones((N,1))
    gamma = 0.1
    beta = 0.9
    
    m = 15
    tol = 10**-9
    delta = 1+tol
    it = 0
    while (delta >= tol):
        it += 1
        
        psi = psiprime.copy()
        arg1 = sp.repeat(sp.transpose(VEprime),N,0)
        arg2 = sp.repeat(EVUprime,N,1)
        arg = sp.array([arg2,arg1])
        psiprime = sp.argmax(arg,axis = 0)    
        
        for j in sp.arange(0,m):
            VE = VEprime.copy()
            VU = VUprime.copy()
            EVU = EVUprime.copy()
            VEprime = util_vec + beta*((1-gamma)*VE + gamma*EVU)
            arg1 = sp.repeat(sp.transpose(VE),N,0)*psiprime
            arg2 = sp.repeat(EVU,N,1)*(1-psiprime)
            arg = arg1+arg2
            VUprime = alpha_util_grid + beta*arg
            EVUprime = sp.dot(VUprime,f)  
    
        
    
        delta = sp.linalg.norm(psiprime -psi)
        #print(delta)    
        
    wr_ind = sp.argmax(sp.diff(psiprime), axis = 1)
    wr = w[wr_ind]
    plt.plot(w,wr)
    plt.show()
    return wr
Exemple #44
0
def calc_BB_Y_2s_tp(C_tp, Vlh, Vrh_p1, l_s_m1, r_s_p1):
    Vl = sp.transpose(Vlh, axes=(0, 2, 1)).conj().copy()
    Vr_p1 = sp.transpose(Vrh_p1, axes=(0, 2, 1)).conj().copy()

    Y = 0
    for al in xrange(len(C_tp)):
        Y += eps_l_noop(l_s_m1, Vl, C_tp[al][0]).dot(eps_r_noop(r_s_p1, C_tp[al][1], Vr_p1))

    etaBB_sq = mm.adot(Y, Y)
    
    return Y, etaBB_sq
Exemple #45
0
def param(y):
    Y=sy.subtract(y,1)    
    
    A=[]
    
    for m in x:
        A.append([1,m,m**2,m**3,m**4,m**5]) 
    
    
    theta= sy.dot( sy.linalg.inv(sy.dot( sy.transpose(A),A )) , sy.dot(sy.transpose(A),Y) )
    return theta
Exemple #46
0
def shift_back_front(x,phi,neq,dx):
	n = len(x)/neq
	u = scipy.reshape(x,(neq,n))
	u_left = scipy.transpose([u[:,0]])
	u_right = scipy.transpose([u[:,-1]])
	u_ext = scipy.c_[u_left,u,u_right]
	dudx = 1./(2*dx)*(u_ext[:,2:]-u_ext[:,:-2])
	#u_ext = scipy.c_[u_left,u_left,u,u_right,u_right]
	#dudx = 1./(12*dx)*(-u_ext[:,4:]+8*u_ext[:,3:-1]-8*u_ext[:,1:-3]+u_ext[:,:-4])
	x_shifted=scipy.reshape(u+phi*dudx,(neq*n,))
	return x_shifted
Exemple #47
0
 def shplot3d(self):
     fig = plt.figure()
     space = axs.Axes3D(fig)
     # plot the wall 
     points = self.xyzxyz()
     top = sp.transpose(points[0])
     space.scatter(top[0],top[1],top[2])
     # plot the pan bottom
     bottom = sp.transpose(points[1])
     space.scatter(bottom[0],bottom[1],bottom[2])
     plt.show()
    def __locate_newton(self, X, C):
        """x[0:self.dim] = (x,alpha)
           x[self.dim] = beta
           x[self.dim+1:2*self.dim] = p
        """
        J_coords = C.CorrFunc.jac(X[0:C.dim], C.coords)
        J_params = C.CorrFunc.jac(X[0:C.dim], C.params)

        return r_[C.CorrFunc(X[0:C.dim]) + X[C.dim]*X[C.dim+1:], \
                  matrixmultiply(transpose(J_coords),X[C.dim+1:]), \
                  matrixmultiply(transpose(X[C.dim+1:]),J_params), \
                  matrixmultiply(transpose(X[C.dim+1:]),X[C.dim+1:]) - 1]
Exemple #49
0
def param(y):
    #Y=sy.subtract(y,1)
    Y=y    
    
    A=[]
    
    for m in x:
        A.append([1,m,m**2,m**3,m**4,m**5,m**6,m**7,m**8,m**9,m**10,m**11,m**12,m**13,m**14,m**15]) 
    
    
    theta= sy.dot( sy.linalg.inv(sy.dot( sy.transpose(A),A )) , sy.dot(sy.transpose(A),Y) )
    return theta
Exemple #50
0
 def get_chords(self, direction, spacing=10, trim_edges=True):
     if direction == 'x':
         swap_axes = [0, 1, 2]
     elif direction == 'y':
         swap_axes = [1, 0, 2]
     elif direction == 'z':
         swap_axes = [2, 1, 0]
     image = sp.transpose(self.image, swap_axes)
     image = self._apply_chords(image=image,
                                spacing=spacing,
                                trim_edges=trim_edges)
     image = sp.transpose(image, swap_axes)
     return image
Exemple #51
0
def struct_dropout(xx, pivots):
    lam = 1
    xfreq = xx[pivots,:]
    P = xfreq * transpose(xx)
    print('finish computing P')
    normvec = np.squeeze(np.asarray(xx.sum(1)))
    normvec = normvec.astype(float)
    d = len(normvec)
    for i in xrange(d): normvec[i] = 1 / (normvec[i] + lam)
    Q = spdiags([normvec],[0],d,d)
    print('finish computing Q')
    W = P * Q
    print('finish computing W')
    return transpose(csc_matrix(W * xx).tanh().todense())
def _sampling_matrix(hessian, cutoff=None, diag = None, temperature=1, step_scale=1):
    # basically need SVD of hessian - singular values and eigenvectors
    # hessian = u * diag(singVals) * vh
    #u, sing_vals, vh = scipy.linalg.svd(hessian)

    # scroll through the singular values and find the ones whose inverses will
    # be huge and set them to zero also, load up the array of singular values 
    # that we store
    # cutoff = (1.0/_.singVals[0])*1.0e03
    # double cutoff = _.singVals[0]*1.0e-02
    # when cutoff is set to zero it means that all values are included
    # cutoff*(sloppiest eigenvalue)
    
    if cutoff:
        u, sing_vals, vh = scipy.linalg.svd(hessian)
        cutoff_sing_val = cutoff * max(sing_vals)
        #when cutoff is set to zero it means that all values are included
        D = 1.0/scipy.maximum(sing_vals, cutoff_sing_val)
        samp_mat = scipy.transpose(vh)*scipy.sqrt(D)
    # instead of cutoff use another method, adding diagonal term to hessian
    elif diag is not None:
        u, sing_vals, vh = scipy.linalg.svd(hessian+diag)
        D = 1.0/sing_vals
        samp_mat = scipy.transpose(vh)*scipy.sqrt(D)
        cutoff_sing_val = diag[0,0]
    else: 
        u, sing_vals, vh = scipy.linalg.svd(hessian)
        D = 1.0/sing_vals
        samp_mat = scipy.transpose(vh)*scipy.sqrt(D)
        cutoff_sing_val = 0

    ## now fill in the sampling matrix ("square root" of the Hessian)
    ## note that sqrt(D[i]) is taken here whereas Kevin took sqrt(D[j])
    ## this is because vh is the transpose of his PT -JJW
    #samp_mat = scipy.transpose(vh) * scipy.sqrt(D)

    # Divide the sampling matrix by an additional factor such
    # that the expected quadratic increase in cost will be about 1.
    cutoff_vals = scipy.compress(sing_vals < cutoff_sing_val, sing_vals)
    if len(cutoff_vals):
        scale = scipy.sqrt(len(sing_vals) - len(cutoff_vals)
                           + sum(cutoff_vals)/cutoff_sing_val)
    else:
        scale = scipy.sqrt(len(sing_vals))

    samp_mat /= scale
    samp_mat *= step_scale
    samp_mat *= scipy.sqrt(temperature)

    return samp_mat
Exemple #53
0
def param(y):
    Y=sy.subtract(y,1)    
    
    A=[]
    
    for m in x:
        if m <0.4 or m>0.7:
            A.append([1,m,m**2,m**3,m**4,m**5,0]) 
      
        else:
            A.append([1,m,m**2,m**3,m**4,m**5,-1])   
    
    theta= sy.dot( sy.linalg.inv(sy.dot( sy.transpose(A),A )) , sy.dot(sy.transpose(A),Y) )
    return theta
def bialttoeig(q, p, n, A):
	v1, v2 = invwedge(q, n)
	w1, w2 = invwedge(p, n)
	
	A11 = bilinearform(A,v1,v1)
	A22 = bilinearform(A,v2,v2)
	A12 = bilinearform(A,v1,v2)
	A21 = bilinearform(A,v2,v1)
	v11 = matrixmultiply(transpose(v1),v1)
	v22 = matrixmultiply(transpose(v2),v2)
	v12 = matrixmultiply(transpose(v1),v2)
	D = v11*v22 - v12*v12
	k = (A11*A22 - A12*A21)/D

	return k[0][0], v1, w1
    def setdata(self, X, V):
        A = self.bialtprodeye(2*self.F.J_coords)
        """Note: p, q <= min(n,m)"""

        self.data.Brand = 2*(random((A.shape[0],self.data.p))-0.5)
        self.data.Crand = 2*(random((A.shape[1],self.data.q))-0.5)
        self.data.B = zeros((A.shape[0],self.data.p), Float)
        self.data.C = zeros((A.shape[1],self.data.q), Float)
        self.data.D = zeros((self.data.q,self.data.p), Float)

        U, S, Vh = linalg.svd(A)
        self.data.b = U[:,-1:]
        self.data.c = num_transpose(Vh)[:,-1:]
        
        if self.update:
            self.data.B[:,1] = self.data.b
            self.data.C[:,1] = self.data.c
            
            U2, S2, Vh2 = linalg.svd(c_[r_[A, transpose(self.data.C[:,1])], r_[self.data.B[:,1], [[0]]]])
            self.data.B[:,2] = U2[0:A.shape[0],-1:]
            self.data.C[:,2] = num_transpose(Vh2)[0:A.shape[1],-1:]
            self.data.D[0,1] = U2[A.shape[0],-1]
            self.data.D[1,0] = num_transpose(Vh2)[A.shape[1],-1]
        else:
            self.data.B = self.data.Brand
            self.data.C = self.data.Crand