コード例 #1
0
ファイル: volumetric.py プロジェクト: argriffing/yolo-tyrion
def old_main(args):
    alpha = args.alpha
    N = args.N
    k = 4
    print 'alpha:', alpha
    print 'N:', N
    print 'k:', k
    print
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    R_mut = wrightcore.create_mutation(M, T)
    R_drift = wrightcore.create_moran_drift_rate_k4(M, T)
    Q = alpha * R_mut + R_drift
    # pick out the correct eigenvector
    W, V = scipy.linalg.eig(Q.T)
    w, v = min(zip(np.abs(W), V.T))
    print 'rate matrix:'
    print Q
    print
    print 'transpose of rate matrix:'
    print Q.T
    print
    print 'eigendecomposition of transpose of rate matrix as integers:'
    print scipy.linalg.eig(Q.T)
    print
    print 'transpose of rate matrix in mathematica notation:'
    print MatrixUtil.m_to_mathematica_string(Q.T.astype(int))
    print
    print 'abs eigenvector corresponding to smallest abs eigenvalue:'
    print np.abs(v)
    print
コード例 #2
0
ファイル: volumetric.py プロジェクト: argriffing/yolo-tyrion
def old_main(args):
    alpha = args.alpha
    N = args.N
    k = 4
    print 'alpha:', alpha
    print 'N:', N
    print 'k:', k
    print
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    R_mut = wrightcore.create_mutation(M, T)
    R_drift = wrightcore.create_moran_drift_rate_k4(M, T)
    Q = alpha * R_mut + R_drift
    # pick out the correct eigenvector
    W, V = scipy.linalg.eig(Q.T)
    w, v = min(zip(np.abs(W), V.T))
    print 'rate matrix:'
    print Q
    print
    print 'transpose of rate matrix:'
    print Q.T
    print
    print 'eigendecomposition of transpose of rate matrix as integers:'
    print scipy.linalg.eig(Q.T)
    print
    print 'transpose of rate matrix in mathematica notation:'
    print MatrixUtil.m_to_mathematica_string(Q.T.astype(int))
    print
    print 'abs eigenvector corresponding to smallest abs eigenvalue:'
    print np.abs(v)
    print
コード例 #3
0
ファイル: check-fold.py プロジェクト: argriffing/yolo-tyrion
def get_full_simplex(m_factor, N, distn_helper):
    """
    Note that this uses the non-moran formulation of drift.
    The distn_helper function taken as an argument is expected
    to be either moran_distn_helper or wright_distn_helper.
    @param m_factor: the mutation rate matrix is multiplied by this number
    @param N: population size
    @param distn_helper: a function (M, T, R_mut) -> v
    @return: M, T, v
    """
    k = 4
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    R_mut = m_factor * wrightcore.create_mutation(M, T)
    v = distn_helper(M, T, R_mut)
    return M, T, v
コード例 #4
0
ファイル: check-fold.py プロジェクト: argriffing/yolo-tyrion
def get_full_simplex(m_factor, N, distn_helper):
    """
    Note that this uses the non-moran formulation of drift.
    The distn_helper function taken as an argument is expected
    to be either moran_distn_helper or wright_distn_helper.
    @param m_factor: the mutation rate matrix is multiplied by this number
    @param N: population size
    @param distn_helper: a function (M, T, R_mut) -> v
    @return: M, T, v
    """
    k = 4
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    R_mut = m_factor * wrightcore.create_mutation(M, T)
    v = distn_helper(M, T, R_mut)
    return M, T, v
コード例 #5
0
def do_full_simplex_then_collapse(mutrate, popsize):
    #mutrate = 0.01
    #mutrate = 0.2
    #mutrate = 10
    #mutrate = 100
    #mutrate = 1
    N = popsize
    k = 4
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    # Create the joint site pair mutation rate matrix.
    R = mutrate * wrightcore.create_mutation(M, T)
    # Create the joint site pair drift transition matrix.
    lmcs = wrightcore.get_lmcs(M)
    lps = wrightcore.create_selection_neutral(M)
    log_drift = wrightcore.create_neutral_drift(lmcs, lps, M)
    # Define the drift and mutation transition matrices.
    P_drift = np.exp(log_drift)
    P_mut = scipy.linalg.expm(R)
    # Define the composite per-generation transition matrix.
    P = np.dot(P_mut, P_drift)
    # Solve a system of equations to find the stationary distribution.
    v = MatrixUtil.get_stationary_distribution(P)
    for state, value in zip(M, v):
        print state, value
    # collapse the two middle states
    nstates_collapsed = multinomstate.get_nstates(N, k-1)
    M_collapsed = np.array(list(multinomstate.gen_states(N, k-1)), dtype=int)
    T_collapsed = multinomstate.get_inverse_map(M_collapsed)
    v_collapsed = np.zeros(nstates_collapsed)
    for i, bigstate in enumerate(M):
        AB, Ab, aB, ab = bigstate.tolist()
        Ab_aB = Ab + aB
        j = T_collapsed[AB, Ab_aB, ab]
        v_collapsed[j] += v[i]
    for state, value in zip(M_collapsed, v_collapsed):
        print state, value
    # draw an equilateral triangle
    #drawtri(M_collapsed, T_collapsed, v_collapsed)
    #test_mesh()
    return M_collapsed, T_collapsed, v_collapsed
コード例 #6
0
def do_full_simplex_then_collapse(mutrate, popsize):
    #mutrate = 0.01
    #mutrate = 0.2
    #mutrate = 10
    #mutrate = 100
    #mutrate = 1
    N = popsize
    k = 4
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    # Create the joint site pair mutation rate matrix.
    R = mutrate * wrightcore.create_mutation(M, T)
    # Create the joint site pair drift transition matrix.
    lmcs = wrightcore.get_lmcs(M)
    lps = wrightcore.create_selection_neutral(M)
    log_drift = wrightcore.create_neutral_drift(lmcs, lps, M)
    # Define the drift and mutation transition matrices.
    P_drift = np.exp(log_drift)
    P_mut = scipy.linalg.expm(R)
    # Define the composite per-generation transition matrix.
    P = np.dot(P_mut, P_drift)
    # Solve a system of equations to find the stationary distribution.
    v = MatrixUtil.get_stationary_distribution(P)
    for state, value in zip(M, v):
        print state, value
    # collapse the two middle states
    nstates_collapsed = multinomstate.get_nstates(N, k - 1)
    M_collapsed = np.array(list(multinomstate.gen_states(N, k - 1)), dtype=int)
    T_collapsed = multinomstate.get_inverse_map(M_collapsed)
    v_collapsed = np.zeros(nstates_collapsed)
    for i, bigstate in enumerate(M):
        AB, Ab, aB, ab = bigstate.tolist()
        Ab_aB = Ab + aB
        j = T_collapsed[AB, Ab_aB, ab]
        v_collapsed[j] += v[i]
    for state, value in zip(M_collapsed, v_collapsed):
        print state, value
    # draw an equilateral triangle
    #drawtri(M_collapsed, T_collapsed, v_collapsed)
    #test_mesh()
    return M_collapsed, T_collapsed, v_collapsed
コード例 #7
0
ファイル: volumetric.py プロジェクト: argriffing/yolo-tyrion
def main(args):
    alpha = args.alpha
    N = args.N
    k = 4
    print 'alpha:', alpha
    print 'N:', N
    print 'k:', k
    print
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    R_mut = wrightcore.create_mutation(M, T)
    R_drift = wrightcore.create_moran_drift_rate_k4(M, T)
    Q = alpha * R_mut + R_drift
    P = scipy.linalg.expm(Q)
    v = MatrixUtil.get_stationary_distribution(P)
    #
    # Define the volumetric data using the stationary distribution.
    max_prob = np.max(v)
    d2 = np.zeros((N + 1, N + 1, N + 1, 4), dtype=float)
    U = np.array([
        [0, 0, 0],
        [0, 1, 1],
        [1, 0, 1],
        [1, 1, 0],
    ], dtype=int)
    for p, state in zip(v, M):
        x, y, z = np.dot(state, U).tolist()
        # r, g, b, alpha
        d2[x, y, z] = np.array(
            [
                255 * (p / max_prob),
                0,
                0,
                255 * (p / max_prob),
                #100,
            ],
            dtype=float)
        #d2[x, y, z, 0] = 255 * (p / max_prob)
        #d2[x, y, z, 1] = 0
        #d2[x, y, z, 2] = 0
        #d2[x, y, z, 3] = 100
    # fill the empty states
    for x in range(N + 1):
        for y in range(N + 1):
            for z in range(N + 1):
                if (x + y + z) % 2 == 1:
                    p_accum = np.zeros(4, dtype=float)
                    n_accum = 0
                    for dx in (-1, 1):
                        if 0 <= x + dx <= N:
                            p_accum += d2[x + dx, y, z]
                            n_accum += 1
                    for dy in (-1, 1):
                        if 0 <= y + dy <= N:
                            p_accum += d2[x, y + dy, z]
                            n_accum += 1
                    for dz in (-1, 1):
                        if 0 <= z + dz <= N:
                            p_accum += d2[x, y, z + dz]
                            n_accum += 1
                    d2[x, y, z] = p_accum / n_accum
    #
    # Do things that the example application does.
    app = QtGui.QApplication([])
    w = gl.GLViewWidget()
    w.opts['distance'] = 2 * N
    w.show()
    #
    # a visual grid or something
    #g = gl.GLGridItem()
    #g.scale(10, 10, 1)
    #w.addItem(g)
    #
    # Do some more things that the example application does.
    vol = gl.GLVolumeItem(d2, sliceDensity=1, smooth=True)
    #vol.translate(-5,-5,-10)
    vol.translate(-0.5 * N, -0.5 * N, -0.5 * N)
    w.addItem(vol)
    #
    # add an axis thingy
    #ax = gl.GLAxisItem()
    #w.addItem(ax)
    if sys.flags.interactive != 1:
        app.exec_()
コード例 #8
0
def main(args):
    alpha = args.alpha
    N = args.N
    k = 4
    print 'alpha:', alpha
    print 'N:', N
    print 'k:', k
    print
    print 'defining the state vectors...'
    M = np.array(list(gen_states_for_induction(N)), dtype=int)
    #M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    print 'M.shape:', M.shape
    print 'M:'
    print M
    print
    if args.dense:
        print 'defining the state vector inverse map...'
        T = multinomstate.get_inverse_map(M)
        print 'T.shape:', T.shape
        print 'constructing dense mutation rate matrix...'
        R_mut = wrightcore.create_mutation(M, T)
        print 'constructing dense drift rate matrix...'
        R_drift = wrightcore.create_moran_drift_rate_k4(M, T)
        Q = alpha * R_mut + R_drift
        # pick out the correct eigenvector
        print 'taking eigendecomposition of dense rate matrix...'
        W, V = scipy.linalg.eig(Q.T)
        w, v = min(zip(np.abs(W), V.T))
        if args.eigvals:
            print 'eigenvalues:'
            print W
            # get integer approximations of eigenvalues
            d = collections.defaultdict(int)
            for raw_eigval in W:
                int_eigval = int(np.round(raw_eigval.real))
                d[int_eigval] += 1
            arr = []
            for int_eigval in reversed(sorted(d)):
                s = '%d^%d' % (-int_eigval, d[int_eigval])
                arr.append(s)
            print ' '.join(arr)
        else:
            print 'rate matrix:'
            print Q
            print
            print 'transpose of rate matrix:'
            print Q.T
            print
            print 'eigendecomposition of transpose of rate matrix as integers:'
            print(W, V)
            print
            print 'rate matrix in mathematica notation:'
            print MatrixUtil.m_to_mathematica_string(Q.astype(int))
            print
            print 'abs eigenvector corresponding to smallest abs eigenvalue:'
            print np.abs(v)
            print
    if args.sparse or args.shift_invert:
        print 'defining the state vector inverse dict...'
        T = multinomstate.get_inverse_dict(M)
        print 'sys.getsizeof(T):', sys.getsizeof(T)
        print 'constructing sparse coo mutation+drift rate matrix...'
        R_coo = create_coo_moran(M, T, alpha)
        print 'converting to sparse csr transpose rate matrix...'
        RT_csr = scipy.sparse.csr_matrix(R_coo.T)
        if args.shift_invert:
            print 'compute an eigenpair using shift-invert mode...'
            W, V = scipy.sparse.linalg.eigs(RT_csr, k=1, sigma=1)
        else:
            print 'compute an eigenpair using "small magnitude" mode...'
            W, V = scipy.sparse.linalg.eigs(RT_csr, k=1, which='SM')
        #print 'dense form of sparsely constructed matrix:'
        #print RT_csr.todense()
        #print
        print 'sparse eigenvalues:'
        print W
        print
        print 'sparse stationary distribution eigenvector:'
        print V[:, 0]
        print
        v = abs(V[:, 0])
        v /= np.sum(v)
        autosave_filename = 'full-moran-autosave.txt'
        print 'writing the stationary distn to', autosave_filename, '...'
        with open(autosave_filename, 'w') as fout:
            for p, (X, Y, Z, W) in zip(v, M):
                print >> fout, X, Y, Z, W, p
コード例 #9
0
ファイル: volumetric.py プロジェクト: argriffing/yolo-tyrion
def main(args):
    alpha = args.alpha
    N = args.N
    k = 4
    print 'alpha:', alpha
    print 'N:', N
    print 'k:', k
    print
    M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    T = multinomstate.get_inverse_map(M)
    R_mut = wrightcore.create_mutation(M, T)
    R_drift = wrightcore.create_moran_drift_rate_k4(M, T)
    Q = alpha * R_mut + R_drift
    P = scipy.linalg.expm(Q)
    v = MatrixUtil.get_stationary_distribution(P)
    #
    # Define the volumetric data using the stationary distribution.
    max_prob = np.max(v)
    d2 = np.zeros((N+1, N+1, N+1, 4), dtype=float)
    U = np.array([
        [0, 0, 0],
        [0, 1, 1],
        [1, 0, 1],
        [1, 1, 0],
        ], dtype=int)
    for p, state in zip(v, M):
        x, y, z = np.dot(state, U).tolist()
        # r, g, b, alpha
        d2[x, y, z] = np.array([
            255 * (p / max_prob),
            0,
            0,
            255 * (p / max_prob),
            #100,
            ], dtype=float)
        #d2[x, y, z, 0] = 255 * (p / max_prob)
        #d2[x, y, z, 1] = 0
        #d2[x, y, z, 2] = 0
        #d2[x, y, z, 3] = 100
    # fill the empty states
    for x in range(N+1):
        for y in range(N+1):
            for z in range(N+1):
                if (x + y + z) % 2 == 1:
                    p_accum = np.zeros(4, dtype=float)
                    n_accum = 0
                    for dx in (-1, 1):
                        if 0 <= x+dx <= N:
                            p_accum += d2[x+dx, y, z]
                            n_accum += 1
                    for dy in (-1, 1):
                        if 0 <= y+dy <= N:
                            p_accum += d2[x, y+dy, z]
                            n_accum += 1
                    for dz in (-1, 1):
                        if 0 <= z+dz <= N:
                            p_accum += d2[x, y, z+dz]
                            n_accum += 1
                    d2[x, y, z] = p_accum / n_accum
    #
    # Do things that the example application does.
    app = QtGui.QApplication([])
    w = gl.GLViewWidget()
    w.opts['distance'] = 2*N
    w.show()
    #
    # a visual grid or something
    #g = gl.GLGridItem()
    #g.scale(10, 10, 1)
    #w.addItem(g)
    #
    # Do some more things that the example application does.
    vol = gl.GLVolumeItem(d2, sliceDensity=1, smooth=True)
    #vol.translate(-5,-5,-10)
    vol.translate(-0.5*N, -0.5*N, -0.5*N)
    w.addItem(vol)
    #
    # add an axis thingy
    #ax = gl.GLAxisItem()
    #w.addItem(ax)
    if sys.flags.interactive != 1:
        app.exec_()
コード例 #10
0
def main(args):
    alpha = args.alpha
    N = args.N
    k = 4
    print 'alpha:', alpha
    print 'N:', N
    print 'k:', k
    print
    print 'defining the state vectors...'
    M = np.array(list(gen_states_for_induction(N)), dtype=int)
    #M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
    print 'M.shape:', M.shape
    print 'M:'
    print M
    print
    if args.dense:
        print 'defining the state vector inverse map...'
        T = multinomstate.get_inverse_map(M)
        print 'T.shape:', T.shape
        print 'constructing dense mutation rate matrix...'
        R_mut = wrightcore.create_mutation(M, T)
        print 'constructing dense drift rate matrix...'
        R_drift = wrightcore.create_moran_drift_rate_k4(M, T)
        Q = alpha * R_mut + R_drift
        # pick out the correct eigenvector
        print 'taking eigendecomposition of dense rate matrix...'
        W, V = scipy.linalg.eig(Q.T)
        w, v = min(zip(np.abs(W), V.T))
        if args.eigvals:
            print 'eigenvalues:'
            print W
            # get integer approximations of eigenvalues
            d = collections.defaultdict(int)
            for raw_eigval in W:
                int_eigval = int(np.round(raw_eigval.real))
                d[int_eigval] += 1
            arr = []
            for int_eigval in reversed(sorted(d)):
                s = '%d^%d' % (-int_eigval, d[int_eigval])
                arr.append(s)
            print ' '.join(arr)
        else:
            print 'rate matrix:'
            print Q
            print
            print 'transpose of rate matrix:'
            print Q.T
            print
            print 'eigendecomposition of transpose of rate matrix as integers:'
            print (W, V)
            print
            print 'rate matrix in mathematica notation:'
            print MatrixUtil.m_to_mathematica_string(Q.astype(int))
            print
            print 'abs eigenvector corresponding to smallest abs eigenvalue:'
            print np.abs(v)
            print
    if args.sparse or args.shift_invert:
        print 'defining the state vector inverse dict...'
        T = multinomstate.get_inverse_dict(M)
        print 'sys.getsizeof(T):', sys.getsizeof(T)
        print 'constructing sparse coo mutation+drift rate matrix...'
        R_coo = create_coo_moran(M, T, alpha)
        print 'converting to sparse csr transpose rate matrix...'
        RT_csr = scipy.sparse.csr_matrix(R_coo.T)
        if args.shift_invert:
            print 'compute an eigenpair using shift-invert mode...'
            W, V = scipy.sparse.linalg.eigs(RT_csr, k=1, sigma=1)
        else:
            print 'compute an eigenpair using "small magnitude" mode...'
            W, V = scipy.sparse.linalg.eigs(RT_csr, k=1, which='SM')
        #print 'dense form of sparsely constructed matrix:'
        #print RT_csr.todense()
        #print
        print 'sparse eigenvalues:'
        print W
        print
        print 'sparse stationary distribution eigenvector:'
        print V[:, 0]
        print
        v = abs(V[:, 0])
        v /= np.sum(v)
        autosave_filename = 'full-moran-autosave.txt'
        print 'writing the stationary distn to', autosave_filename, '...'
        with open(autosave_filename, 'w') as fout:
            for p, (X, Y, Z, W) in zip(v, M):
                print >> fout, X, Y, Z, W, p