def simresults(basename, basedir): '''Retrieve approximate solution from saved output''' mydict = loadPickle(basename, basedir) l = mydict['l'] S = mydict['S'] F = [] Finv = [] P = [] N = l[0].shape[0] M = l[0].shape[1] for k in range(len(mydict['t'])): Ft = SD2D.vectorGrad(l[k], mydict['pdict']['gridspc'], N, M) Ftemp = np.reshape(Ft, (N * M, 2, 2)) Ftinv = CM.matinv2x2(Ftemp) Ftinv = np.reshape(Ftinv, (N, M, 2, 2)) F.append(Ft.copy()) Finv.append(Ftinv.copy()) stress = np.zeros((N, M, 2, 2)) for j in range(N): for m in range(M): stress[j, m, :, :] = S[k][j, m, :, :] * Ftinv[j, m, :, :].transpose() P.append(stress.copy()) return l, P, S, F, Finv, mydict
def checkSwimmerLength(basename, basedir): plt.close() mydict = loadPickle(basename, basedir) fpts = mydict['fpts'] plt.figure() L = mydict['pdict']['forcedict']['L'] h = mydict['pdict']['forcedict']['h'] critterlen = [] hmax = [] hmin = [] for k in range(len(mydict['t'])): clen = np.sqrt((fpts[k][2:-1:2] - fpts[k][:-3:2])**2 + (fpts[k][3::2] - fpts[k][1:-2:2])**2) critterlen.append(clen.sum() / L) hmax.append(np.max(clen) / h) hmin.append(np.min(clen) / h) plt.plot(mydict['t'], critterlen) plt.xlabel('Time') plt.ylabel('Normalized length') plt.title('Swimmer length vs time') plt.savefig(basedir + basename + '/SwimmerLengthvsTime.pdf') plt.clf() plt.plot(mydict['t'], hmax) plt.xlabel('Time') plt.ylabel('Normalized max segment') plt.title('Max segment length vs time') plt.savefig(basedir + basename + '/MaxSegLengthvsTime.pdf') plt.clf() plt.plot(mydict['t'], hmin) plt.xlabel('Time') plt.ylabel('Normalized min segment') plt.title('Min segment length vs time') plt.savefig(basedir + basename + '/MinSegLengthvsTime.pdf')
def stressComponentsMaxMin(basename, basedir): plt.close() mydict = loadPickle(basename, basedir) fname = os.path.expanduser(basedir + basename + '/4RMCompsOverTime') S11 = np.zeros((len(mydict['t']), 2)) S12 = np.zeros((len(mydict['t']), 2)) S22 = np.zeros((len(mydict['t']), 2)) for k in range(0, len(mydict['t'])): S11[k, 0] = np.max(mydict['S'][k][:, :, 0, 0]) S11[k, 1] = np.min(mydict['S'][k][:, :, 0, 0]) S12[k, 0] = np.max(mydict['S'][k][:, :, 0, 1]) S12[k, 1] = np.min(mydict['S'][k][:, :, 0, 1]) S22[k, 0] = np.max(mydict['S'][k][:, :, 1, 1]) S22[k, 1] = np.min(mydict['S'][k][:, :, 1, 1]) plt.plot(mydict['t'], S11[:, 0], 'k-', label='S11 max') plt.plot(mydict['t'], S11[:, 1], 'k--', label='S11 min') plt.plot(mydict['t'], S22[:, 0], 'b-', label='S22 max') plt.plot(mydict['t'], S22[:, 1], 'b--', label='S22 min') plt.plot(mydict['t'], S12[:, 0], 'r-', label='S12 max') plt.plot(mydict['t'], S12[:, 1], 'r--', label='S12 min') xmin, xmax, ymin, ymax = plt.axis() plt.ylim(ymin - (ymax - ymin) / 10., ymax) plt.legend(bbox_to_anchor=(0., 0., 1., .102), loc=3, ncol=3, mode="expand", borderaxespad=0.) plt.title('Stress components over time') plt.savefig(fname) plt.clf()
def stressTraceContour(basename, basedir): plt.close() mydict = loadPickle(basename, basedir) myvars = stressVars(mydict) fname = os.path.expanduser(basedir + basename + '/tracecontourframe') for k in range(1, len(mydict['t'])): # ph=plt.contour(mydict['l'][k][:,:,0],mydict['l'][k][:,:,1],mydict['Strace'][k][:,:],30,cmap=cm.cool) ph2 = plt.contourf(mydict['l'][k][:, :, 0], mydict['l'][k][:, :, 1], mydict['Strace'][k][:, :], np.arange(myvars[4], myvars[5] + 0.1, (myvars[5] - myvars[4]) / 50), cmap=cm.cool) # ph2.set_clim(myvars[4],myvars[5]) ph2.set_clim(2, 3) plt.colorbar(ph2) plt.plot([mydict['fpts'][0][0], mydict['fpts'][0][0]], [myvars[6], myvars[7]], 'w', linewidth=2.0) plt.plot(mydict['fpts'][k][:-1:2], mydict['fpts'][k][1::2], 'k', linewidth=2.0) # plt.axis('equal') plt.axis(myvars[:4]) plt.title('Time = ' + str(mydict['t'][k])) plt.savefig(os.path.expanduser(basedir + fname + '%03d' % k)) plt.clf()
def specificPointTraj(basename, basedir): ''' Don't use with regridded data sets. ''' plt.close() mydict = loadPickle(basename, basedir) myvars = stressVars(mydict) l = mydict['l'] # pts = [(0,0),(5,0),(0,5),(-5,0),(0,-5),(5,10),(10,5),(-5,10),(10,-5), # (5,-10),(-10,5),(-5,-10),(-10,-5)] pts = [(0, 0), (-5, 5), (5, -5), (-10, 10), (10, -10), (-15, 15), (15, -15)] # pts=[(15,20)] x = np.zeros((len(pts), len(mydict['t']))) y = np.zeros((len(pts), len(mydict['t']))) for k in range(len(mydict['t'])): ind = l[k].shape[0] / 2 for p in range(len(pts)): x[p, k] = l[k][ind + pts[p][0], ind + pts[p][1], 0] y[p, k] = l[k][ind + pts[p][0], ind + pts[p][1], 1] for p in range(len(pts)): plt.plot(x[p, :], y[p, :], 'k') plt.plot(x[p, 0], y[p, 0], 'ro') plt.title('Time %03f to time %03f' % (mydict['t'][0], mydict['t'][-1])) plt.savefig( os.path.expanduser(basedir + basename + '/0trajlinenoregrid.pdf'))
def plotQuiverDiff(basename, basedir, velfunc): ''' Don't use with regridded data sets. ''' plt.close() mydict = loadPickle(basename, basedir) fname = os.path.expanduser(basedir + basename + '/quivframe') myvars = stressVars(mydict) for k in range(1, len(mydict['t']) - 1): lk = mydict['l'][k] lkm1 = mydict['l'][k - 1] lkp1 = mydict['l'][k + 1] if lkm1.shape == lkp1.shape: l2col = np.reshape(lk, (lk.shape[0] * lk.shape[1], 2)) u, junk = velfunc(mydict['pdict'], lk.flatten(), l2col) unum = (lkp1 - lkm1) / (2 * mydict['dt']) unum = np.reshape(unum, (unum.shape[0] * unum.shape[1], 2)) plt.quiver( l2col[::4, 0], l2col[1::4, 1], u[::8], u[1::8], color='r' ) #, units='x', linewidths=(2,), edgecolors=('k'), headaxislength=5) plt.quiver( l2col[::4, 0], l2col[1::4, 1], unum[::4, 0], unum[::4, 1], color='b' ) #, units='x',linewidths=(2,), edgecolors=('k'), headaxislength=5) plt.axis(myvars[:4]) plt.title('Time = %03f' % mydict['t'][k]) plt.savefig( os.path.expanduser(basedir + basename + fname + '%03d.pdf') % k) plt.clf()
def outputDreams(checkpoint, directory): from utilities import loadPickle result = loadPickle(checkpoint) eprint(" [+] Loaded checkpoint", checkpoint) g = result.grammars[-1] if directory is None: randomStr = ''.join(random.choice('0123456789') for _ in range(10)) directory = "/tmp/" + randomStr eprint(" Dreaming into", directory) os.system("mkdir -p %s" % directory) dreamFromGrammar(g, directory)
def swimmerOnlyComp(vebasename, vebasedir, stbasename, stbasedir, swimdir): plt.close() mydictve = loadPickle(vebasename, vebasedir) mydictst = loadPickle(stbasename, stbasedir) fname = os.path.expanduser(vebasedir + vebasename + '/comp2stokesframe') myvars = swimmerVars(mydictve) vefpts = mydictve['fpts'] stfpts = mydictst['fpts'] if swimdir == 'right': barind = -2 elif swimdir == 'left': barind = 0 else: print('Swimming direction not recognized. Choose "left" or "right".') sys.exit() fig = plt.figure() for k in range(len(mydictve['t'])): plt.plot([vefpts[0][barind], vefpts[0][barind]], [myvars[4], myvars[5]], 'b', linewidth=4.0) plt.plot(stfpts[k][:-1:2], stfpts[k][1::2], 'r', linewidth=4.0, label='Stokes') plt.plot(vefpts[k][:-1:2], vefpts[k][1::2], 'k', linewidth=4.0, label='OB') plt.axis(myvars[:4]) plt.legend(loc='upper right') plt.title('Time = ' + str(mydictve['t'][k])) plt.savefig(os.path.expanduser(fname + '%03d' % k)) plt.clf() plt.close()
def makeEllipses(basedir, basename): plt.close() fname = os.path.expanduser(basedir + basename + '/ellipseframe') mydict = loadPickle(basename, basedir) myvars = stressVars(mydict) fpts = mydict['fpts'] gridspc = mydict['pdict']['gridspc'] Nt = len(mydict['t']) N = mydict['S'][0].shape[0] M = mydict['S'][0].shape[1] fig = plt.figure() for i in range(Nt): S = mydict['S'][i] l = mydict['l'][i] ax = fig.add_subplot(111) #,aspect='equal' ax.set_xlim(myvars[:2]) ax.set_ylim(myvars[2:4]) ells = [] for j in range(N): for k in range(M): # S = 1/np.sqrt(2)*np.array([[1,2],[-1,2]]) # w,V = np.linalg.eigh(S) w, V = np.linalg.eigh(S[j, k, :, :]) # print('Eigenvalues') # print(w) # print('Eigenvector dot prod') # print(np.dot(V[:,0],V[:,1])) center = l[j, k, :] ind = np.nonzero(w == np.max(w)) ind = ind[0][0] horzdist = w[ ind] * gridspc #put major eigval on x-axis and scale by the grid spacing (to fit in graph) vertdist = w[np.mod(ind + 1, 2)] * gridspc ang = np.arccos( V[0, ind] * 1 + V[1, ind] * 0 ) #calculate angle of rotation from eigenvector (using orthogonality of eigvecs here) ells.append( mpl.patches.Ellipse(xy=center, width=horzdist, height=vertdist, angle=ang * 180 / np.pi)) for e in ells: ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_facecolor('w') plt.plot(fpts[i][:-1:2], fpts[i][1::2], 'k', linewidth=4.0) plt.title('Time = ' + str(mydict['t'][i])) plt.savefig(os.path.expanduser(fname + '%03d' % i)) plt.clf()
def swimmerOnly(basename, basedir): plt.close() mydict = loadPickle(basename, basedir) myvars = swimmerVars(mydict) fpts = mydict['fpts'] fig = plt.figure() for k in range(len(mydict['t'])): plt.plot([fpts[0][-2], fpts[0][-2]], [myvars[4], myvars[5]], 'b', linewidth=4.0) plt.plot(fpts[k][:-1:2], fpts[k][1::2], 'r', linewidth=4.0) plt.axis('equal') plt.xlim(myvars[:2]) plt.title('Time = ' + str(mydict['t'][k])) plt.savefig(basedir + basename + '/frame%03d' % k) plt.clf() plt.close()
def plotFinalPositionUnnormalized(basedir, bnamelist, xvals, xlab, fnameend): xf = [] for k in range(len(bnamelist)): print(xvals[k]) basename = bnamelist[k] mydict = loadPickle(basename, basedir) xf.append( np.abs( np.max(mydict['fpts'][0][:-1:2]) - np.max(mydict['fpts'][-1][:-1:2]))) plt.close() plt.plot(xvals, xf, linewidth=2) plt.xlabel(xlab) plt.ylabel('dist') plt.title('Distance traveled in x at time %.01f' % mydict['t'][-1]) plt.savefig(basedir + 'finaldistance' + fnameend + '.pdf') return xf
def stressTraceExtension(basename, basedir): plt.close() mydict = loadPickle(basename, basedir) fname = os.path.expanduser(basedir + basename + '/traceframe') myvars = stressVars(mydict) for k in range(0, len(mydict['t'])): ph = plt.pcolor(mydict['l'][k][:, :, 0], mydict['l'][k][:, :, 1], mydict['Strace'][k][:, :], vmin=myvars[4], vmax=myvars[5], cmap=cm.RdGy) plt.colorbar(ph) # plt.axis('equal') plt.axis(myvars[:4]) # plt.axis('off') plt.title('Time = ' + str(mydict['t'][k])) plt.savefig(fname + '%03d' % k + '.pdf') plt.clf()
def makeEllipses_Deviation(basedir, basename): plt.close() fname = os.path.expanduser(basedir + basename + '/devellipseframe') mydict = loadPickle(basename, basedir) myvars = stressVars(mydict) fpts = mydict['fpts'] gridspc = mydict['pdict']['gridspc'] Nt = len(mydict['t']) N = mydict['S'][0].shape[0] M = mydict['S'][0].shape[1] fig = plt.figure() for i in range(Nt): S = mydict['S'][i] l = mydict['l'][i] ax = fig.add_subplot(111) #,aspect='equal' ax.set_xlim(myvars[:2]) ax.set_ylim(myvars[2:4]) ells = [] for j in range(N): for k in range(M): w, V = np.linalg.eigh(S[j, k, :, :] - np.eye(2, 2)) center = l[j, k, :] ind = np.nonzero(w == np.max(w)) ind = ind[0][0] horzdist = w[ind] * gridspc vertdist = w[np.mod(ind + 1, 2)] * gridspc ang = np.arccos( V[0, ind] * 1 + V[1, ind] * 0 ) #calculate angle of rotation from eigenvector (using orthogonality of eigvecs here) ells.append( mpl.patches.Ellipse(xy=center, width=horzdist, height=vertdist, angle=ang * 180 / np.pi)) for e in ells: ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_facecolor('w') plt.plot(fpts[i][:-1:2], fpts[i][1::2], 'k', linewidth=4.0) plt.title('Time = ' + str(mydict['t'][i])) plt.savefig(os.path.expanduser(fname + '%03d.pdf' % i)) plt.clf()
def pointTraj(basename, basedir): ''' Don't use with regridded data sets. ''' plt.close() mydict = loadPickle(basename, basedir) myvars = stressVars(mydict) l = mydict['l'] ksave = [0] k = -1 while k < len(mydict['t']) - 1: x = np.zeros( (len(mydict['t']), l[ksave[-1]].shape[0], l[ksave[-1]].shape[1])) y = np.zeros( (len(mydict['t']), l[ksave[-1]].shape[0], l[ksave[-1]].shape[1])) for k in range(ksave[-1], len(mydict['t'])): print(k) if k == ksave[-1] or l[k - 1].shape == l[k].shape: x[k, :, :] = l[k][:, :, 0] y[k, :, :] = l[k][:, :, 1] else: ksave.append(k) print('regridding changed size of domain') break fname = os.path.expanduser(basedir + basename + '/0traj_%03d' % (k - 1, )) if k == len(mydict['t']) - 1: ksave.append(len(mydict['t'])) for i in range(0, l[0].shape[0], 2): for j in range(0, l[0].shape[1], 2): plt.plot(x[ksave[-2]:ksave[-1], i, j], y[ksave[-2]:ksave[-1], i, j], 'k') plt.axis(myvars[:4]) # plt.setp(plt.gca().get_xticklabels(), visible=False) # plt.setp(plt.gca().get_yticklabels(), visible=False) # plt.gca().xaxis.set_ticks( [] ) # plt.gca().yaxis.set_ticks( [] ) print(ksave) plt.title('Time %03f to time %03f' % (mydict['t'][ksave[-2]], mydict['t'][ksave[-1] - 1])) plt.savefig(fname + '.pdf') plt.clf()
def simresults(basename, basedir): '''Retrieve approximate solution from saved output''' mydict = loadPickle(basename, basedir) l = mydict['l'] regridinds = findRegridTimes(l) S=mydict['S'] P=[] for k in range(len(mydict['t'])): # N and M can change because of regridding N = l[k].shape[0] M = l[k].shape[1] Ft = SD2D.vectorGrad(l[k],mydict['pdict']['gridspc'],N,M) Ftemp = np.reshape(Ft,(N*M,2,2)) Ftinv = CM.matinv2x2(Ftemp) Ftinv = np.reshape(Ftinv,(N,M,2,2)) stressP = np.zeros((N,M,2,2)) for j in range(N): for m in range(M): stressP[j,m,:,:] = S[k][j,m,:,:]*Ftinv[j,m,:,:].transpose() P.append(stressP.copy()) return l, P, S, mydict, regridinds
def stressTrace(basename, basedir, swimdir): ''' FIXME: Rewrite so that swimmer can be plotted over any of the other plots. Maybe pass function handle. ''' plt.close() mydict = loadPickle(basename, basedir) fname = os.path.expanduser(basedir + basename + '/traceframe') myvars = stressVars(mydict) myswimvars = swimmerVars(mydict) if swimdir == 'right': barind = -2 elif swimdir == 'left': barind = 0 else: print('Swimming direction not recognized. Choose "left" or "right".') sys.exit() fig = plt.figure() for k in range(len(mydict['t'])): ph = plt.pcolor(mydict['l'][k][:, :, 0], mydict['l'][k][:, :, 1], mydict['Strace'][k][:, :], vmin=myvars[4], vmax=myvars[5], cmap=cm.RdGy) fig.colorbar(ph) plt.plot([mydict['fpts'][0][barind], mydict['fpts'][0][barind]], [myswimvars[4], myswimvars[5]], 'w', linewidth=4.0) plt.plot(mydict['fpts'][k][:-1:2], mydict['fpts'][k][1::2], 'k', linewidth=4.0) plt.axis(myvars[:4]) # plt.axis([-0.2,1.8,-0.4,1.0]) # plt.axis('equal') plt.title('Time = ' + str(mydict['t'][k])) plt.savefig(os.path.expanduser(fname + '%03d' % k)) plt.clf()
def stressComponentsPColor(basename, basedir, i, j): ''' S[i,j] = indices of stress component, S_{i+1,j+1} in matrix indices ''' plt.clf() mydict = loadPickle(basename, basedir) fname = os.path.expanduser(basedir + basename + '/S%d%d_' % (i + 1, j + 1)) myvars = stressVars(mydict) for k in range(0, len(mydict['t']) / 2): vmin = np.min(mydict['S'][k][:, :, i, j]) vmax = np.max(mydict['S'][k][:, :, i, j]) ph = plt.pcolor(mydict['l'][k][:, :, 0], mydict['l'][k][:, :, 1], mydict['S'][k][:, :, i, j], vmin=vmin, vmax=vmax, cmap=cm.RdGy) plt.colorbar(ph) # plt.axis('equal') # plt.axis(myvars[:4]) plt.title('Time = ' + str(mydict['t'][k])) plt.savefig(fname + '%03d' % k) plt.clf()
def enumerateDreams(checkpoint, directory): from recognition import backgroundHelmholtzEnumeration from utilities import loadPickle, standardDeviation, mean result = loadPickle(checkpoint) eprint(" [+] Loaded checkpoint", checkpoint) g = result.grammars[-1] if directory is None: assert False, "please specify a directory" eprint(" Dreaming into", directory) os.system("mkdir -p %s" % directory) frontiers = backgroundHelmholtzEnumeration( makeTasks(None, None), g, 100, evaluationTimeout=0.01, special=LogoFeatureCNN.special)() print(f"{len(frontiers)} total frontiers.") MDL = 0 def L(f): return -list(f.entries)[0].logPrior frontiers.sort(key=lambda f: -L(f)) while len(frontiers) > 0: # get frontiers whose MDL is between [MDL,MDL + 1) fs = [] while len(frontiers) > 0 and L(frontiers[-1]) < MDL + 1: fs.append(frontiers.pop(len(frontiers) - 1)) if fs: random.shuffle(fs) print(f"{len(fs)} programs with MDL between [{MDL}, {MDL + 1})") fs = fs[:500] os.system(f"mkdir {directory}/{MDL}") dreamFromGrammar([list(f.entries)[0].program for f in fs], f"{directory}/{MDL}") MDL += 1
def stressTrace4RMContour(basename, basedir): plt.close() mydict = loadPickle(basename, basedir) fname = os.path.expanduser(basedir + basename + '/tracecontourframe') # vmax=np.max(mydict['Strace']) # x = mydict['l'][:,:,:,0] # y = mydict['l'][:,:,:,1] xmin = np.min(mydict['l'][-1][:, :, 0]) + 0.5 xmax = np.max(mydict['l'][-1][:, :, 0]) + 0.5 ymin = np.min(mydict['l'][-1][:, :, 1]) + 0.5 ymax = np.max(mydict['l'][-1][:, :, 1]) + 0.5 for k in range(0, len(mydict['t']), 100): lvls = np.linspace(np.min(mydict['Strace'][k][:, :]), np.max(mydict['Strace'][k][:, :]), 21) ph = plt.contour(mydict['l'][k][:, :, 0], mydict['l'][k][:, :, 1], mydict['Strace'][k][:, :], levels=lvls) # plt.clabel(ph, inline=1, fontsize=10) # plt.axis('equal') # plt.axis([xmin,xmax,ymin,ymax]) plt.title('Time = ' + str(mydict['t'][k])) plt.savefig(fname + '%03d' % k) plt.clf()
import sys sys.exit(0) dreamCheckpoint = args.pop("dreamCheckpoint") dreamDirectory = args.pop("dreamDirectory") proto = args.pop("proto") if dreamCheckpoint is not None: #outputDreams(dreamCheckpoint, dreamDirectory) enumerateDreams(dreamCheckpoint, dreamDirectory) sys.exit(0) animateCheckpoint = args.pop("animate") if animateCheckpoint is not None: animateSolutions(loadPickle(animateCheckpoint).allFrontiers) sys.exit(0) target = args.pop("target") red = args.pop("reduce") save = args.pop("save") prefix = args.pop("prefix") prefix_dreams = prefix + "/dreams/" + ('_'.join(target)) + "/" prefix_pickles = prefix + "/logo." + ('.'.join(target)) if not os.path.exists(prefix_dreams): os.makedirs(prefix_dreams) tasks = makeTasks(target, proto) eprint("Generated", len(tasks), "tasks") os.chdir("prototypical-networks") subprocess.Popen(["python", "./protonet_server.py"])