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
def get_moran_drift(M, T): k = M.shape[1] if k == 2: return wrightcore.create_moran_drift_rate_k2(M, T) elif k == 3: return wrightcore.create_moran_drift_rate_k3(M, T) elif k == 4: return wrightcore.create_moran_drift_rate_k4(M, T) else: raise NotImplementedError
def get_moran_drift(M, T): #FIXME: this is dumb, #FIXME: but I'm not sure how to use variable-dimension ndarrays in cython k = M.shape[1] if k == 2: return wrightcore.create_moran_drift_rate_k2(M, T) elif k == 3: return wrightcore.create_moran_drift_rate_k3(M, T) elif k == 4: return wrightcore.create_moran_drift_rate_k4(M, T) else: raise NotImplementedError
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_()
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
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_()
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