def make_regular_frequencies(*Lens): # Generate all waves, then eliminate the all zero one linspaces = [np.linspace(0,2.0*np.pi,n) for n in Lens] W = utils.make_points(*linspaces)[1:,:] (K,D) = W.shape assert(D == 2) W = np.vstack([W,W]) Phi = np.empty(2*K) Phi[:K] = 0 Phi[K:] = np.pi / 2.0 return (W.T,Phi)
def plot_vertices(nodes,faces,fn,cmap=None,interp='linear',G=640): fn = np.ma.array(fn,mask=~np.isfinite(fn)) assert(fn.size == nodes.shape[0]) if cmap is None: cmap = plt.get_cmap('jet') cmap.set_bad('w',1.) (P,(X,Y)) = make_points([np.linspace(np.min(nodes[:,i]), np.max(nodes[:,i]),G) for i in [0,1]],True) Z = griddata(nodes,fn,P,method=interp) Z = np.reshape(Z,(G,G)) plt.gca() plt.pcolormesh(X,Y,Z,lw=0,cmap=cmap) plt.triplot(nodes[:,0],nodes[:,1],faces,'-k',alpha=0.25) plt.colorbar()
t *= b assert np.all(np.linalg.eigvals(C - t * dC) > 0) while f(a - t *da, C - t * dC) - F > - s * t * grad_norm and t > t0: print "\tBacktracking",t,f(a + t *da, C + t * dC) - F t *= b assert np.all(np.linalg.eigvals(C - t * dC) > 0) a -= t * da C -= t * dC return (a,C) G = 128 (P,[X,Y]) = make_points([np.linspace(-5,5,G)]*2,True) Z = gauss(P,2,np.array([[3,-1],[-1,2]])) Z[np.where(np.isnan(Z))[0]] = 1 (a,C) = gradient_descent(P,Z) print "Final a:",a print "Final C:",C plt.subplot(2,2,1) plt.pcolormesh(X,Y,np.reshape(Z,X.shape)) plt.subplot(2,2,2) plt.pcolormesh(X,Y,np.reshape(gauss(P,a,C),X.shape)) plt.subplot(2,2,3) plt.pcolormesh(X,Y,np.reshape(Z - gauss(P,a,C),X.shape)) plt.colorbar() plt.show()
data = [] for filename in filenames: res = pattern.match(filename) if res is None: continue i = int(res.group(1)) unarch = Unarchiver(filedir+filename) data.append(unarch.data) data = np.array(data) np.save(filedir + "summary",data) grid = [np.linspace(np.min(data[:,i]),np.max(data[:,i]),128) for i in xrange(2)] (P,(X,Y)) = make_points(grid,True) #Z = smooth(P,data[:,:2],data[:,2],3) fig = plt.figure() for (i,p) in enumerate([5,50,95]): plt.subplot(2,2,i+1) fn = lambda x: np.percentile(x,p) # 2 is residual, 3 is iter count Q = local_fn(fn,P,data[:,:2],data[:,2],1.5) Q = np.reshape(Q,X.shape) Qm = ma.masked_where(np.isnan(Q),Q) plt.pcolormesh(X,Y,Qm) plt.colorbar() plt.title("Percentile " + str(p))
V = vertices.shape[0] T = tetrahedra.shape[0] print 'Reading INRIA .mesh file',meshfile print '\tFound', V, 'vertices' print '\tFound', T, 'tetrahedra' bbox = np.empty((3,2)) for i in xrange(3): bbox[i,0] = np.min(vertices[:,i]) bbox[i,1] = np.max(vertices[:,i]) kernel = stats.gaussian_kde(vertices.T,0.1) G = 40 grids = [np.linspace(bbox[i,0],bbox[i,1],G) for i in xrange(3)] P = make_points(grids) P += 0.1*np.random.randn(*P.shape) Z = kernel(P.T) stdZ = standardize(Z) cmap = plt.get_cmap('spectral') C = cmap(stdZ) C[:,3] = (stdZ)**1.5 mask = (C[:,3] > 0.025) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(P[mask,0],P[mask,1],P[mask,2],c=C[mask,:], s=125,lw=0) plt.show()
def plot_mesh_slice(f,bound,meshfile,**kwargs): G = kwargs.get('grid_points',64) flat = kwargs.get('flat',True) assert((3,2) == bound.shape) idx = np.where(bound[:,0] == bound[:,1])[0] nidx = np.where(bound[:,0] != bound[:,1])[0] if 2 != nidx.size: print "Check slice bounds, need exactly 2 non-trivial dimensions" assert 1 == idx.size bound = np.hstack([bound,G*np.ones((3,1))]) bound[idx,2] = 1 grids = [np.linspace(*list(bound[i,:])) for i in xrange(3)] (points,meshes) = make_points(grids,True) timestamp = str(time.time()) point_file = "/tmp/points." + timestamp value_file = "/tmp/value." + timestamp out_file = "/tmp/out." + timestamp arch = Archiver(points=points) arch.write(point_file) arch.clear() arch.add(values=f) arch.write(value_file) (base,ext) = os.path.splitext(meshfile) assert '.mesh' == ext cmd = ['cdiscrete/tet_interp', '--mesh',base + '.ctri', '--points',point_file, '--values',value_file, '--out',out_file] cmd = ' '.join(cmd) print cmd try: subprocess.check_call(cmd,shell=True) except Exception: print "Interpolation failed; check .ctri file?" quit() unarch = Unarchiver(out_file) F = np.reshape(unarch.interp,(G,G)) Fm = np.ma.masked_where(np.isnan(F),F) if flat: plt.gcf() [X,Y] = [meshes[i].squeeze() for i in nidx] plt.pcolormesh(X,Y,Fm) else: Fm = standardize(Fm) [X,Y,Z] = [mesh.squeeze() for mesh in meshes] fig = plt.gcf() ax = fig.gca(projection='3d') cmap = plt.get_cmap('jet') colors = cmap(Fm) colors[...,3]= 0.25*(1-Fm)**1.5 p = ax.plot_surface(X,Y,Z, rstride=1,cstride=1, facecolors=colors, shade=False)
model_preds = np.array(model_preds.tolist(), dtype=np.float) ale_pred = np.array(ale_pred.tolist(), dtype=np.float) # model_preds = model_preds.data.numpy() # ale_pred = ale_pred.data.numpy() return model_preds, ale_pred if __name__ == '__main__': # args = parse_predict_args() file_path = f'./saved_models/pred_output/seed60_test/hidden_vector_0.npy' mol_fp = np.load(file_path) # mol_i = mol_fp[0, :] print(mol_fp.shape) grad_rmss = [] grad_maxx = [] # smiles = pd.read_csv('./saved_models/qm9_ens_woN/fold_0/qm9_N.csv').values[:, 0] smiles = pd.read_csv('./saved_models/pred_output/seed60_test/test_pred.csv').values[:, 0] for i, smile in zip(range(mol_fp.shape[0]), smiles): # mol_fp.shape[0] mol_i = mol_fp[i, :] # print(mol_i[:200]) points = make_points(mol_i) # print(points[0]) pred, ale = predict_i(points) grad_rms, grad_max = gradient(pred) grad_rmss.append(grad_rms) grad_maxx.append(grad_max) print(smile, i+2, grad_rms, grad_max, pred[0, 0], ale[0, 0]) # , pred[0], ale[0] pd.DataFrame(np.array([list(smiles), grad_rmss, grad_maxx], dtype=np.float).T, index=None, columns=['smiles', 'grad_rms', 'grad_max']).\ to_csv('./saved_models/pred_output/woN_test/grad_m.csv', index=False)
import numpy as np import scipy.spatial as spt from utils import make_points import matplotlib.pyplot as plt N = 5 points = make_points([np.linspace(0,1,5)]*2) points = np.vstack([points,np.random.rand(25,2)]) tri = spt.Delaunay(points,False,True) target = np.random.rand(2) simplex = tri.find_simplex(target) vertices = tri.simplices[simplex,:] print 'Target:',target print 'Simplex:',simplex print 'Vertices:', vertices plt.triplot(points[:,0], points[:,1], tri.simplices.copy()) plt.plot(points[:,0], points[:,1], '.b') plt.fill(points[vertices,0],points[vertices,1],'r',alpha=0.25) plt.plot(target[0],target[1],'y*',lw=2,markersize=15) plt.show()