def do_position_loop(self): """Loop over all possible angular positions for this neighbour realisation.""" # Some empty datavectors De1 = [] De2 = [] snr = [] theta= np.linspace(0,2,10)*np.pi for i,t in enumerate(theta): print " -- Level 2 iteration: %d (theta=%f)"%(i,t) x = self.neighbour_distance*np.cos(t) y = self.neighbour_distance*np.sin(t) gal, psf = i3s.setup_simple(boxsize=32, shear=self.g, flux=self.central_flux, psf_ellipticity=self.psf_e, psf_size=self.psf_size, neighbour_ellipticity=self.neighbour_e, neighbour=[x,y], neighbour_flux=self.neighbour_flux, neighbour_size=self.neighbour_radius, wcs=self.wcs, opt=self.opt) if self.neighbour_distance< 23: wts = self.generate_mock_weightmap(gal.array, x, y) else: wts = np.ones_like(gal.array) res, cat = i3s.run(gal, psf, weights=wts, opt=self.opt, return_cat=True, show=False) De1.append(res.e1) De2.append(res.e2) snr.append(cat.snr) # Compute the mean over all positions return np.mean(snr), np.mean(De1), np.mean(De2)
def do_main_calculation(self, central_data, neighbour_data, sel): self.generate_central_realisation(central_data, sel) if self.complexity != 0: self.generate_neighbour_realisation(neighbour_data, central_data, sel) # Draw this neighbour realisation repeatedly on a ring of angular position snr, e1, e2 = self.do_position_loop() else: noise = self.generate_effective_noise(neighbour_data, central_data, sel) gal, psf = i3s.setup_simple(boxsize=32, shear=self.g, flux=self.central_flux, psf_ellipticity=self.psf_e, psf_size=self.psf_size, neighbour=[np.inf, np.inf], wcs=self.wcs, opt=self.opt) res, cat = i3s.run(gal + noise, psf, opt=self.opt, return_cat=True, show=False) snr, e1, e2 = cat.snr, res.e1, res.e2 return snr, e1, e2
def do_main_calculation(self, central_data, neighbour_data, sel): self.generate_central_realisation(central_data, sel) if self.complexity!=0: self.generate_neighbour_realisation(neighbour_data, central_data, sel) # Draw this neighbour realisation repeatedly on a ring of angular position snr, e1, e2 = self.do_position_loop() else: noise = self.generate_effective_noise(neighbour_data, central_data, sel) gal, psf = i3s.setup_simple(boxsize=32, shear=self.g, flux=self.central_flux, psf_ellipticity=self.psf_e, psf_size=self.psf_size, neighbour=[np.inf,np.inf], wcs=self.wcs, opt=self.opt) res, cat = i3s.run(gal+noise, psf, opt=self.opt, return_cat=True, show=False) snr, e1, e2 = cat.snr, res.e1, res.e2 return snr, e1, e2
def do_position_loop(self): """Loop over all possible angular positions for this neighbour realisation.""" # Some empty datavectors De1 = [] De2 = [] snr = [] theta = np.linspace(0, 2, 10) * np.pi for i, t in enumerate(theta): print " -- Level 2 iteration: %d (theta=%f)" % (i, t) x = self.neighbour_distance * np.cos(t) y = self.neighbour_distance * np.sin(t) gal, psf = i3s.setup_simple(boxsize=32, shear=self.g, flux=self.central_flux, psf_ellipticity=self.psf_e, psf_size=self.psf_size, neighbour_ellipticity=self.neighbour_e, neighbour=[x, y], neighbour_flux=self.neighbour_flux, neighbour_size=self.neighbour_radius, wcs=self.wcs, opt=self.opt) if self.neighbour_distance < 23: wts = self.generate_mock_weightmap(gal.array, x, y) else: wts = np.ones_like(gal.array) res, cat = i3s.run(gal, psf, weights=wts, opt=self.opt, return_cat=True, show=False) De1.append(res.e1) De2.append(res.e2) snr.append(cat.snr) # Compute the mean over all positions return np.mean(snr), np.mean(De1), np.mean(De2)
def run(self, distributions, niterations=2000, filename="mc_toy_model-results", size=1, rank=0): # Highest level loop - Sets model parameters #----------------------------------------------------------------------- self.m = [] self.centroid = [] print "Setting up model" meds = s.meds_wrapper( "/share/des/disc8/cambridge/meds/DES2111+0043-r-sim-ohioA6-meds-y1a1-beta.fits.fz" ) shears = [-0.02, 0.02] angles = np.linspace(0, 2 * np.pi, 31)[:-1] self.params = { "fc": 0, "fn": 0, "dgn": 0, "Rc": 0, "Rn": 0, "psf_size": 0 } self.priors = { "fc": [200, 8000], "fn": [2, 9000], "dgn": [1, 70], "Rc": [0.1, 6.0], "Rn": [0.1, 6.0], "psf_size": [0.1, 4.0] } index_c = np.random.choice(distributions.size, niterations * 50) index_n = np.random.choice(distributions.size, niterations * 50) idone = 0 for ireal, (icent, ineigh) in enumerate(zip(index_c, index_n)): if ireal % size != rank: continue self.get_realisation(distributions, icent, ineigh, ireal) outside_allowed = self.sanity_check() if outside_allowed: continue if idone > niterations: continue evec_g = [] restart = False # Second level loop - input shear #----------------------------------------------------------------------- print "Will evaluate m using %d shear values" % len(shears) for ishear, g in enumerate(shears): print "g = (%2.2f, 0.00)" % g evec_t = [] centroid = [] # Third level loop - neighbour position #----------------------------------------------------------------------- print "Will use %d neighbour angles" % len(angles) for ipos, theta in enumerate(angles): if restart: continue x = self.params["dgn"] * np.cos(theta) y = self.params["dgn"] * np.sin(theta) print "theta = %2.3f degrees, position = (%3.2f,%3.2f)" % ( theta * 60., x, y) gal, psf = i3s.setup_simple( boxsize=96, shear=(g, 0.0), psf_size=self.params["psf_size"], size=self.params["Rc"], neighbour_ellipticity=(0.0, 0.0), neighbour_flux=self.params["fn"], flux=self.params["fc"], neighbour_size=self.params["Rn"], neighbour=[x, y], opt=meds.options) res = i3s.i3s([gal.array], [psf], meds=meds) evec_t.append([res.e1, res.e2]) centroid.append( np.sqrt(res.ra_as * res.ra_as + res.dec_as * res.dec_as)) meane1 = np.array(evec_t).T[0].mean() meane2 = np.array(evec_t).T[1].mean() evec_g.append([meane1, meane2]) # Finally we have a vector, containing one mean measured shape for each of the input shear values # Calculate m residual_e1 = np.array(evec_g).T[0] - np.array(shears) residual_e2 = np.array(evec_g).T[1] m = (residual_e1[-1] - residual_e1[0]) / (shears[-1] - shears[0]) print "---------------------- m=%f" % m print centroid self.m.append([ (residual_e1[-1] - residual_e1[0]) / (shears[-1] - shears[0]), (residual_e2[-1] - residual_e2[0]) / (shears[-1] - shears[0]) ]) self.centroid.append( [np.array(centroid).mean(), np.array(centroid).max()]) if abs(self.m[-1][0]) > 2: continue if (self.m[-1][0] < -0.01) and (np.array(centroid).mean() < 1): import pdb pdb.set_trace() self.write_output_line(filename) idone += 1 print "Done all loops"
def make_sersic(e1,e2): gal = i3s.setup_simple(boxsize=32,shear=(e1,e2), psf_size=Rp, size=Rc_med, neighbour_ellipticity=(0.0,0.0), neighbour_flux=fn_med, flux=fc_med, neighbour_size=Rn_med, neighbour=[np.inf,0], opt=m.options) return gal[0].array
Rn=2.9 fc=1650 fn=473 Rc_med=2.1 fc_med=945 fn_med=475 Rn_med=1.47 m=s.meds_wrapper("/share/des/disc8/cambridge/meds/DES2111+0043-r-sim-ohioA6-meds-y1a1-beta.fits.fz") def make_sersic(e1,e2): gal = i3s.setup_simple(boxsize=32,shear=(e1,e2), psf_size=Rp, size=Rc_med, neighbour_ellipticity=(0.0,0.0), neighbour_flux=fn_med, flux=fc_med, neighbour_size=Rn_med, neighbour=[np.inf,0], opt=m.options) return gal[0].array g,p=i3s.setup_simple(boxsize=32,shear=(0.05,0.2), psf_size=Rp, size=Rc_med, neighbour_ellipticity=(0.0,0.0), neighbour_flux=fn_med, flux=fc_med, neighbour_size=Rn_med, neighbour=[np.inf,0], opt=m.options) os.system("touch /home/samuroff/hoopoe_paper/toy_model_data/simple_1parfit_e_maxsamples_g1_0.05_g2_0.2_noise8.txt") gvec=np.linspace(-0.06,0.2,200) for i in xrange(30000): lvec_noisy0=[] noise=np.random.normal(size=32*32).reshape((32,32))*8 for g1 in gvec: im=make_sersic(g1,0.4) chi2=((im-g.array+noise)*(im-g.array+noise) ).sum() ; lvec_noisy0.append(-0.5*chi2) ; print g1 lvec_noisy0=np.array(lvec_noisy0) noisy_max = gvec[(lvec_noisy0==lvec_noisy0.max())]
for pe1 in pshears: e1ofg = [] e2ofg = [] for g1 in gshears: e1vec = [] e2vec = [] for i, theta in enumerate(angles): x = params["dgn"] * np.cos(theta) y = params["dgn"] * np.sin(theta) gal, psf = i3s.setup_simple(boxsize=48, shear=(g1, 0.0), psf_ellipticity=(pe1, 0), psf_size=params["Rp"], size=params["Rc"], neighbour_ellipticity=(0.0, 0.0), neighbour_flux=params["fn"], flux=params["fc"], neighbour_size=params["Rn"], neighbour=[x, y], opt=m.options) ra, transform, res, model, img = i3s.i3s([gal], [psf], meds=m, return_all_i3s=True) e1vec.append(res.e1) e2vec.append(res.e2) e1ofg.append([np.mean(e1vec), np.std(e1vec)]) e2ofg.append([np.mean(e2vec), np.std(e2vec)]) d1 = np.array(e1ofg).T[0] - gshears d2 = np.array(e2ofg).T[0] m1ofp.append((d1[1] - d1[0]) / (shears[1] - shears[0]))