modpath, data, pad, maxiter=maxiter, gtol=gtol, anneal=anneal, resnorm=resnorm, loss=loss, log=logit, inverse=inverse) # initval = None initval = np.random.normal(0, 1, size=nc**3).reshape(nc, nc, nc).astype( config['dtype']) #truth if stdinit: initval = standardinit(config, datap, hposd, final, R=8) #initval = tools.readbigfile(dpath + ftype%(bs, nc, 900, step) + 'mesh/s/') #initval = np.ones((nc, nc, nc)) if truthinit: initval = truth.copy() losses = [] literals = {'losses': losses, 'truemeshes': truemeshes, 'bs': bs, 'nc': nc} tstart = time() lcallback = lambda x: loss_callback(x, literals=literals, nprint=nprint, nsave=nsave, maxiter=maxiter, t0=tstart) with tf.Session(graph=recong) as session:
print('\nDo reconstruction\n') recong = rmods.graphhposft1(config, modpath, data, pad, maxiter=maxiter, gtol=gtol, anneal=anneal, resnorm=resnorm) # initval = None initval = np.random.normal(1, 0.5, size=nc**3).reshape(nc, nc, nc).astype( config['dtype']) #truth initval = standardinit(config, data, hposd, final, R=8) #initval = tools.readbigfile(dpath + ftype%(bs, nc, 900, step) + 'mesh/s/') #initval = np.ones((nc, nc, nc)) #initval = truth.copy() losses = [] literals = {'losses': losses, 'truemeshes': truemeshes, 'bs': bs, 'nc': nc} tstart = time() lcallback = lambda x: loss_callback(x, literals=literals, nprint=nprint, nsave=nsave, maxiter=maxiter, t0=tstart) with tf.Session(graph=recong) as session:
def main(_): infield = True dtype = tf.float32 mesh_shape = mtf.convert_to_shape(FLAGS.mesh_shape) nc, bs = FLAGS.nc, FLAGS.box_size a0, a, nsteps = FLAGS.a0, FLAGS.af, FLAGS.nsteps stages = np.linspace(a0, a, nsteps, endpoint=True) numd = 1e-3 ##Begin here klin = np.loadtxt('../flowpm/data/Planck15_a1p00.txt').T[0] plin = np.loadtxt('../flowpm/data/Planck15_a1p00.txt').T[1] ipklin = iuspline(klin, plin) #pypath = '/global/cscratch1/sd/chmodi/cosmo4d/output/version2/L0400_N0128_05step-fof/lhd_S0100/n10/opt_s999_iM12-sm3v25off/meshes/' final = tools.readbigfile( '/project/projectdirs/m3058/chmodi/cosmo4d/data/L0400_N0128_S0100_05step/mesh/d/' ) ic = tools.readbigfile( '/project/projectdirs/m3058/chmodi/cosmo4d/data/L0400_N0128_S0100_05step/mesh/s/' ) fpos = tools.readbigfile( '/project/projectdirs/m3058/chmodi/cosmo4d/data/L0400_N0128_S0100_05step/dynamic/1/Position/' ) aa = 1 zz = 1 / aa - 1 rsdfactor = float(100 / (aa**2 * cosmo.H(zz).value**1)) print('\nRsdfactor used is : ', rsdfactor) hpos = tools.readbigfile( '/project/projectdirs/m3058/chmodi/cosmo4d/data/L0400_N0512_S0100_40step/FOF/PeakPosition//' )[1:int(bs**3 * numd)] hvel = tools.readbigfile( '/project/projectdirs/m3058/chmodi/cosmo4d/data/L0400_N0512_S0100_40step/FOF/CMVelocity//' )[1:int(bs**3 * numd)] rsdpos = hpos + hvel * rsdfactor * np.array([0, 0, 1]) print('Effective displacement : ', (hvel[:, -1] * rsdfactor).std()) hmass = tools.readbigfile( '/project/projectdirs/m3058/chmodi/cosmo4d/data/L0400_N0512_S0100_40step/FOF/Mass//' )[1:int(bs**3 * numd)].flatten() meshpos = tools.paintcic(rsdpos, bs, nc) meshmass = tools.paintcic(rsdpos, bs, nc, hmass.flatten() * 1e10) data = meshmass kv = tools.fftk([nc, nc, nc], bs, symmetric=True, dtype=np.float32) datasm = tools.fingauss(data, kv, 3, np.pi * nc / bs) ic, data = np.expand_dims(ic, 0), np.expand_dims(data, 0).astype(np.float32) datasm = np.expand_dims(datasm, 0).astype(np.float32) print("Min in data : %0.4e" % datasm.min()) # #################################################### stdinit = srecon.standardinit(bs, nc, meshpos, hpos, final, R=8) recon_estimator = tf.estimator.Estimator(model_fn=model_fn, model_dir=fpath) def predict_input_fn(data=data, M0=0., w=3., R0=0., off=None, istd=None, x0=None): features = {} features['datasm'] = data features['rsdfactor'] = rsdfactor features['M0'] = M0 features['w'] = w features['R0'] = R0 features['off'] = off features['istd'] = istd features['x0'] = x0 return features, None eval_results = recon_estimator.predict( input_fn=lambda: predict_input_fn(x0=ic), yield_single_examples=False) for i, pred in enumerate(eval_results): if i > 0: break suff = '-model' dg.saveimfig(suff, [pred['ic'], pred['model']], [ic, data], fpath + '/figs/') dg.save2ptfig(suff, [pred['ic'], pred['model']], [ic, data], fpath + '/figs/', bs) np.save(fpath + '/reconmeshes/ic_true' + suff, pred['ic']) np.save(fpath + '/reconmeshes/fin_true' + suff, pred['final']) np.save(fpath + '/reconmeshes/model_true' + suff, pred['model']) # randominit = np.random.normal(size=data.size).reshape(data.shape) #eval_results = recon_estimator.predict(input_fn=lambda : predict_input_fn(x0 = np.expand_dims(stdinit, 0)), yield_single_examples=False) eval_results = recon_estimator.predict( input_fn=lambda: predict_input_fn(x0=randominit), yield_single_examples=False) for i, pred in enumerate(eval_results): if i > 0: break suff = '-init' dg.saveimfig(suff, [pred['ic'], pred['model']], [ic, data], fpath + '/figs/') dg.save2ptfig(suff, [pred['ic'], pred['model']], [ic, data], fpath + '/figs/', bs) np.save(fpath + '/reconmeshes/ic_init' + suff, pred['ic']) np.save(fpath + '/reconmeshes/fin_init' + suff, pred['final']) np.save(fpath + '/reconmeshes/model_init' + suff, pred['model']) # # Train and evaluate model. mms = [1e12, 1e11] wws = [1., 2., 3.] RRs = [4., 2., 1., 0.5, 0.] niter = 100 iiter = 0 for mm in mms: noisefile = '/project/projectdirs/m3058/chmodi/cosmo4d/train/L0400_N0128_05step-n10/width_3/Wts_30_10_1/r1rf1/hlim-13_nreg-43_batch-5/eluWts-10_5_1/blim-20_nreg-23_batch-100/hist_M%d_na.txt' % ( np.log10(mm) * 10) offset, ivar = setnoise(datasm, noisefile, noisevar=0.25) istd = ivar**0.5 if not FLAGS.offset: offset = None if not FLAGS.istd: istd = None for R0 in RRs: for ww in wws: print('\nFor iteration %d\n' % iiter) print('With mm=%0.2e, R0=%0.2f, ww=%d \n' % (mm, R0, ww)) def train_input_fn(): features = {} features['datasm'] = datasm features['rsdfactor'] = rsdfactor features['M0'] = mm features['w'] = ww features['R0'] = R0 features['off'] = offset features['istd'] = istd features['x0'] = np.expand_dims( stdinit, 0 ) #np.random.normal(size=datasm.size).reshape(datasm.shape) features['lr'] = 0.01 return features, None recon_estimator.train(input_fn=train_input_fn, max_steps=iiter + niter) eval_results = recon_estimator.predict( input_fn=predict_input_fn, yield_single_examples=False) for i, pred in enumerate(eval_results): if i > 0: break iiter += niter # suff = '-%d-M%d-R%d-w%d' % (iiter, np.log10(mm), R0, ww) dg.saveimfig(suff, [pred['ic'], pred['model']], [ic, data], fpath + '/figs/') dg.save2ptfig(suff, [pred['ic'], pred['model']], [ic, data], fpath + '/figs/', bs) suff = '-M%d-R%d-w%d' % (np.log10(mm), R0, ww) np.save(fpath + '/reconmeshes/ic' + suff, pred['ic']) np.save(fpath + '/reconmeshes/fin' + suff, pred['final']) np.save(fpath + '/reconmeshes/model' + suff, pred['model']) RRs = [1., 0.5, 0.] wws = [3.] niter = 200 sys.exit(0) ## exit(0)