def _convert_dobs_to_obs(self, dobs): """ convert a Survey object dobs to an ngmix Observation """ j = ngmix.DiagonalJacobian( row=0, col=0, scale=dobs.pixel_scale, ) im=dobs.image.array #print('mean sky level:',dobs.mean_sky_level ) #print('sqrt(mean sky level):',np.sqrt(dobs.mean_sky_level )) noise = np.sqrt( dobs.mean_sky_level ) weight = im*0 + 1.0/noise**2 #psf_im=dobs.psf_image.array.copy() psf_im = dobs.psf_model.drawImage( nx=48, ny=48, scale=dobs.pixel_scale, ).array psf_im *= 1.0/psf_im.sum() pnoise = psf_im.max()/400.0 psf_s2n = np.sqrt( (psf_im**2).sum())/pnoise logger.debug(' psf s2n: %f' % psf_s2n) psf_im += self.rng.normal(scale=pnoise, size=psf_im.shape) psf_weight = psf_im*0 + 1.0/pnoise**2 #import images #images.multiview(psf_im,title='psf') #stop psf_cen = (np.array(psf_im.shape)-1.0)/2.0 psf_j = ngmix.DiagonalJacobian( row=psf_cen[0], col=psf_cen[1], scale=dobs.pixel_scale, ) psf_obs=ngmix.Observation( psf_im, weight=psf_weight, jacobian=psf_j, ) return ngmix.Observation( im, weight=weight, jacobian=j, psf=psf_obs, )
def _get_obs(rng, set_noise_image=False, noise=1.0e-6): psf_noise = 1.0e-6 scale = 0.263 psf_fwhm = 0.9 gal_fwhm = 0.7 psf = galsim.Gaussian(fwhm=psf_fwhm) obj0 = galsim.Gaussian(fwhm=gal_fwhm) obj = galsim.Convolve(psf, obj0) psf_im = psf.drawImage(scale=scale).array im = obj.drawImage(scale=scale).array psf_im += rng.normal(scale=psf_noise, size=psf_im.shape) im += rng.normal(scale=noise, size=im.shape) cen = (np.array(im.shape)-1.0)/2.0 psf_cen = (np.array(psf_im.shape)-1.0)/2.0 j = ngmix.DiagonalJacobian(row=cen[0], col=cen[1], scale=scale) pj = ngmix.DiagonalJacobian(row=psf_cen[0], col=psf_cen[1], scale=scale) wt = im*0 + 1.0/noise**2 psf_wt = psf_im*0 + 1.0/psf_noise**2 psf_obs = ngmix.Observation( psf_im, weight=psf_wt, jacobian=pj, ) if set_noise_image: nim = rng.normal(scale=noise, size=im.shape) else: nim = None bmask = np.zeros(im.shape, dtype='i2') obs = ngmix.Observation( im, weight=wt, bmask=bmask, noise=nim, jacobian=j, psf=psf_obs, ) return obs
def _get_ngmix_obs(self, gal_img, psf_img, psf_pars=[0.0, 0.0, -0.01, 0.01, 0.15, 1.0], fit_psf=True): """ """ eps=0.01 lm_pars={'maxfev': 2000, 'xtol': 5.0e-5, 'ftol': 5.0e-5} img_shape = gal_img.shape # Jacobian gal_jacob=ngmix.DiagonalJacobian(scale=self._pixel_scale, x=int((img_shape[0]-1)/2.), y=int((img_shape[1]-1)/2.)) psf_jacob=ngmix.DiagonalJacobian(scale=self._pixel_scale, x=int((img_shape[0]-1)/2.), y=int((img_shape[1]-1)/2.)) # PSF fittitng psf_noise = np.sqrt(np.sum(psf_img**2)) / 500 psf_weight = np.ones_like(psf_img) / psf_noise**2 psf_obs=ngmix.Observation(psf_img, weight=psf_weight, jacobian=psf_jacob) if fit_psf: pfitter=ngmix.fitting.LMSimple(psf_obs,'gauss',lm_pars=lm_pars) guess=np.array(psf_pars) guess[0] += urand(low=-eps,high=eps) guess[1] += urand(low=-eps,high=eps) guess[2] += urand(low=-eps, high=eps) guess[3] += urand(low=-eps, high=eps) guess[4] *= (1.0 + urand(low=-eps, high=eps)) guess[5] *= (1.0 + urand(low=-eps, high=eps)) pfitter.go(guess) # print(np.abs((pfitter.get_result()['g']-np.array([-0.01, 0.01]))/np.array([-0.01, 0.01]))) psf_gmix_fit = pfitter.get_gmix() psf_obs.set_gmix(psf_gmix_fit) # Gal fitting # Get noise level direclty from the image sigma_noise = self.mad(gal_img) noise = np.random.normal(size=img_shape) * sigma_noise weight = np.ones_like(gal_img) * 1/sigma_noise**2. obs = ngmix.Observation(gal_img, weight=weight, noise=noise, jacobian=gal_jacob, psf=psf_obs) return obs
def test_gaussmom_flags(): """ test we get flags for very noisy data """ rng = np.random.RandomState(seed=100) ntrial = 10 noise = 100000 scale = 0.263 dims = [32] * 2 weight = np.zeros(dims) + 1.0 / noise**2 cen = (np.array(dims) - 1) / 2 jacobian = ngmix.DiagonalJacobian(row=cen[0], col=cen[1], scale=scale) flags = np.zeros(ntrial) for i in range(ntrial): im = rng.normal(scale=noise, size=dims) obs = Observation( image=im, weight=weight, jacobian=jacobian, ) fitter = GaussMom(fwhm=1.2) res = fitter.go(obs) flags[i] = res['flags'] assert np.any(flags != 0)
def __call__(self): """ get a simulated ngmix.MultiBandObsList """ iconf = self['image'] band_images, band_weights, obj_data = self._get_noisy_images() jacobian = ngmix.DiagonalJacobian( row=0, col=0, scale=iconf['pixel_scale'], ) mbobs = ngmix.MultiBandObsList() for image, weight in zip(band_images, band_weights): obs = ngmix.Observation( image, weight=weight, jacobian=jacobian, psf=self.get_psf_obs(), ignore_zero_weight=False, ) obslist = ngmix.ObsList() obslist.append(obs) mbobs.append(obslist) obj_data_with_cen = self._set_centers(obs, obj_data) mbobs.meta['obj_data'] = obj_data_with_cen return mbobs
def _make_obs(self): mbobs = ngmix.MultiBandObsList() for im in self.imlist: jacobian = ngmix.DiagonalJacobian(row=0, col=0, scale=self['pixel_scale']) if self['coadd']: wt = np.zeros(im.shape) + 1.0 / self['coadd_noise_sigma']**2 else: wt = np.zeros(im.shape) + 1.0 / self['noise_sigma']**2 obs = ngmix.Observation( im, weight=wt, jacobian=jacobian, psf=self.get_psf_obs(), ) olist = ngmix.ObsList() olist.append(obs) mbobs.append(olist) self.obs = mbobs
def get_psf_obs(self): kw = {'scale': self['pixel_scale']} dims = self.get('psf_dims', None) if dims is not None: kw['nx'], kw['ny'] = dims[1], dims[0] psf_im = self.psf.drawImage(**kw).array dims = np.array(psf_im.shape) pcen = (dims - 1.0) / 2.0 pjac = ngmix.DiagonalJacobian(row=pcen[0], col=pcen[1], scale=self['pixel_scale']) psf_im += self.rng.normal( scale=self['psf']['noise_sigma'], size=dims, ) psf_wt = np.zeros(dims) + 1.0 / self['psf']['noise_sigma']**2 return ngmix.Observation( psf_im, weight=psf_wt, jacobian=pjac, )
def _set_psf(self): import galsim self.psf = galsim.Gaussian(fwhm=0.9) kw = {'scale': self['pixel_scale']} dims = self.get('psf_dims', None) if dims is not None: kw['nx'], kw['ny'] = dims[1], dims[0] self.psf_im = self.psf.drawImage(**kw).array dims = np.array(self.psf_im.shape) pcen = (dims - 1.0) / 2.0 pjac = ngmix.DiagonalJacobian(row=pcen[0], col=pcen[1], scale=self['pixel_scale']) self.psf_im += self.rng.normal( scale=self['psf_noise_sigma'], size=dims, ) psf_wt = np.zeros(dims) + 1.0 / self['psf_noise_sigma']**2 self.psf_obs = ngmix.Observation( self.psf_im, weight=psf_wt, jacobian=pjac, ) psf_gmix = self._fit_psf_admom(self.psf_obs) self.psf_obs.set_gmix(psf_gmix)
def _get_jacobian(self, dims): cen = (np.array(dims) - 1.0) / 2.0 return ngmix.DiagonalJacobian( row=cen[0], col=cen[1], scale=self.pixel_scale, )
def _get_obs(rng): """ obs with noise image included """ noise = 0.1 psf_noise = 1.0e-6 scale = 0.263 psf_fwhm = 0.9 gal_fwhm = 0.7 psf = galsim.Gaussian(fwhm=psf_fwhm) obj0 = galsim.Gaussian(fwhm=gal_fwhm) obj = galsim.Convolve(psf, obj0) psf_im = psf.drawImage(scale=scale).array im = obj.drawImage(scale=scale).array cen = (np.array(im.shape) - 1.0) / 2.0 psf_cen = (np.array(psf_im.shape) - 1.0) / 2.0 j = ngmix.DiagonalJacobian(row=cen[0], col=cen[1], scale=scale) pj = ngmix.DiagonalJacobian(row=psf_cen[0], col=psf_cen[1], scale=scale) wt = im * 0 + 1.0 / noise**2 psf_wt = psf_im * 0 + 1.0 / psf_noise**2 psf_obs = ngmix.Observation( psf_im, weight=psf_wt, jacobian=pj, ) im += rng.normal(scale=noise, size=im.shape) psf_im += rng.normal(scale=psf_noise, size=psf_im.shape) nim = rng.normal(scale=noise, size=im.shape) obs = ngmix.Observation( im, weight=wt, noise=nim, jacobian=j, psf=psf_obs, ) return obs
def _set_jacobian(self): """ set the psf as a gaussian """ import ngmix self.jacobian = ngmix.DiagonalJacobian( row=0, col=0, scale=self.scale, )
def _set_jacobian(self, scale=1.0, jacobian=None): if jacobian is not None: self.jacobian = jacobian.copy() self.jacobian.set_cen(0, 0) else: import ngmix self.jacobian = ngmix.DiagonalJacobian( row=0, col=0, scale=scale, )
def mbobs(): mbobs = MultiBandObsList() for i in range(3): ol = ObsList() for j in range(4): o = Observation(image=np.ones((32, 32)) * (j + 1), weight=np.ones((32, 32)) * (j + 1), jacobian=ngmix.DiagonalJacobian(scale=0.25, row=0, col=0)) ol.append(o) mbobs.append(ol) return mbobs
def test_ml_fitting_galsim_moffat_smoke(): rng = np.random.RandomState(seed=2312) scale = 0.263 fwhm = 0.9 beta = 2.5 image_size = 33 flux = 1.0 noise = 1.0e-5 moff = galsim.Moffat(fwhm=fwhm, beta=beta, flux=flux) im = moff.drawImage( nx=image_size, ny=image_size, scale=scale, ).array im += rng.normal(scale=noise, size=im.shape) weight = im * 0 + 1.0 / noise**2 cen = (image_size - 1.0) / 2.0 jac = ngmix.DiagonalJacobian( y=cen, x=cen, scale=scale, ) obs = ngmix.Observation( image=im, weight=weight, jacobian=jac, ) fitter = ngmix.fitting.GalsimMoffatFitter() guess = np.zeros(7) guess[0] = rng.uniform(low=-0.1, high=0.1) guess[1] = rng.uniform(low=-0.1, high=0.1) guess[2] = rng.uniform(low=-0.1, high=0.1) guess[3] = rng.uniform(low=-0.1, high=0.1) guess[4] = moff.half_light_radius * rng.uniform(low=0.9, high=1.1) guess[5] = rng.uniform(low=1.5, high=3) guess[6] = flux * rng.uniform(low=0.9, high=1.1) res = fitter.go(obs=obs, guess=guess) assert res['flags'] == 0
def _set_psf(self): """ set the psf and psf observation """ import galsim pconf = self['psf'] if pconf['model'] == 'moffat': self._psf = galsim.Moffat( fwhm=pconf['fwhm'], beta=pconf['beta'], ) elif pconf['model'] == 'gauss': self._psf = galsim.Gaussian(fwhm=self['psf']['fwhm'], ) else: raise ValueError('bad psf model: "%s"' % pconf['model']) psf_image = self._psf.drawImage( scale=self['image']['pixel_scale'], ).array psf_noise = 0.0001 psf_image += self.rng.normal( size=psf_image.shape, scale=psf_noise, ) psf_weight = psf_image * 0 + 1.0 / psf_noise**2 cen = (np.array(psf_image.shape) - 1) / 2 jac = ngmix.DiagonalJacobian( row=cen[0], col=cen[1], scale=self['image']['pixel_scale'], ) self._psf_obs = ngmix.Observation( psf_image, psf_weight, jacobian=jac, )
def test_tf_ngmix(): """ This test generates a simple galaxy and measure moments with ngmix, vs. tf_ngmix. """ gals, _ = autometacal.datasets.galaxies.make_data( Ngals=Ngals, snr=100, gal_g1=np.random.uniform(-.7, .7, Ngals), gal_g2=np.random.uniform(-.7, .7, Ngals), scale=scale) weight_fwhm = scale * stamp_size / 2 # <- this sets everything for the window function results_ngmix = [] # ngmix version fitter = ngmix.gaussmom.GaussMom(fwhm=weight_fwhm) for gal in gals: obs = ngmix.Observation(gal.numpy(), jacobian=ngmix.DiagonalJacobian( row=stamp_size // 2, col=stamp_size // 2, scale=scale)) e = fitter._measure_moments(obs)['e'] results_ngmix.append(np.array(ngmix.shape.e1e2_to_g1g2(e[0], e[1]))) results_ngmix = np.array(results_ngmix) # our version: @tf.function def get_ellipticity(im): return autometacal.get_moment_ellipticities(im, scale=0.2, fwhm=weight_fwhm) result_tf_ngmix = get_ellipticity(gals) assert_allclose(results_ngmix, result_tf_ngmix, rtol=1e-6, atol=1e-6)
def __func(j, x): retvals = [] for i, y in enumerate(np.linspace(start, end, n)): _psf = func(x, y) _psf = _psf.drawImage(scale=0.125, nx=35, ny=35) cen = (35 - 1) // 2 obs = ngmix.Observation(image=_psf.array, jacobian=ngmix.DiagonalJacobian( scale=0.125, row=cen, col=cen)) try: am = run_admom(obs, 1) gm = am.get_gmix() g1, g2, sigma = gm.get_g1g2sigma() fwhm = sigma * 2.355 except Exception as e: raise e # fwhm = _psf.calculateFWHM() # g1, g2 = 0, 0 # mom = galsim.hsm.FindAdaptiveMom( # _psf, # precision=1e-4, # hsmparams=galsim.hsm.HSMParams(max_mom2_iter=1000)) retvals.append((i, j, fwhm, g1, g2)) return retvals
def make_data(rng, noise, g1=0.05, g2=-0.02, flux=100.0): """ simulate an exponential object with moffat psf Parameters ---------- rng: np.random.RandomState The random number generator noise: float Noise for the image g1: float object g1, default 0.05 g2: float object g2, default -0.02 flux: float, optional default 100 Returns ------- ngmix.Observation, pars dict """ psf_noise = 1.0e-6 scale = 0.263 psf_fwhm = 0.9 gal_hlr = 0.5 dy, dx = rng.uniform(low=-scale/2, high=scale/2, size=2) psf = galsim.Moffat( beta=2.5, fwhm=psf_fwhm, ).shear( g1=-0.01, g2=-0.01, ) obj0 = galsim.Exponential( half_light_radius=gal_hlr, flux=flux, ).shear( g1=g1, g2=g2, ).shift( dx=dx, dy=dy, ) obj = galsim.Convolve(psf, obj0) psf_im = psf.drawImage(scale=scale).array im = obj.drawImage(scale=scale).array psf_im += rng.normal(scale=psf_noise, size=psf_im.shape) im += rng.normal(scale=noise, size=im.shape) cen = (np.array(im.shape)-1.0)/2.0 psf_cen = (np.array(psf_im.shape)-1.0)/2.0 jacobian = ngmix.DiagonalJacobian( row=cen[0] + dy/scale, col=cen[1] + dx/scale, scale=scale, ) psf_jacobian = ngmix.DiagonalJacobian( row=psf_cen[0], col=psf_cen[1], scale=scale, ) wt = im*0 + 1.0/noise**2 psf_wt = psf_im*0 + 1.0/psf_noise**2 psf_obs = ngmix.Observation( psf_im, weight=psf_wt, jacobian=psf_jacobian, ) obs = ngmix.Observation( im, weight=wt, jacobian=jacobian, psf=psf_obs, ) obj_pars = { 'g1': g1, 'g2': g2, 'flux': flux, } return obs, obj_pars
def make_data(rng, noise, shear): """ simulate an exponential object with moffat psf the hlr of the exponential is drawn from a gaussian with mean 0.4 arcseconds and sigma 0.2 Parameters ---------- rng: np.random.RandomState The random number generator noise: float Noise for the image shear: (g1, g2) The shear in each component Returns ------- ngmix.Observation """ psf_noise = 1.0e-6 scale = 0.263 psf_fwhm = 0.9 gal_hlr = rng.normal(loc=0.4, scale=0.2) dy, dx = rng.uniform(low=-scale/2, high=scale/2, size=2) psf = galsim.Moffat( beta=2.5, fwhm=psf_fwhm, ).shear( g1=0.02, g2=-0.01, ) obj0 = galsim.Exponential( half_light_radius=gal_hlr, ).shear( g1=shear[0], g2=shear[1], ).shift( dx=dx, dy=dy, ) obj = galsim.Convolve(psf, obj0) psf_im = psf.drawImage(scale=scale).array im = obj.drawImage(scale=scale).array psf_im += rng.normal(scale=psf_noise, size=psf_im.shape) im += rng.normal(scale=noise, size=im.shape) cen = (np.array(im.shape)-1.0)/2.0 psf_cen = (np.array(psf_im.shape)-1.0)/2.0 jacobian = ngmix.DiagonalJacobian( row=cen[0] + dy/scale, col=cen[1] + dx/scale, scale=scale, ) psf_jacobian = ngmix.DiagonalJacobian( row=psf_cen[0], col=psf_cen[1], scale=scale, ) wt = im*0 + 1.0/noise**2 psf_wt = psf_im*0 + 1.0/psf_noise**2 psf_obs = ngmix.Observation( psf_im, weight=psf_wt, jacobian=psf_jacobian, ) obs = ngmix.Observation( im, weight=wt, jacobian=jacobian, psf=psf_obs, ) return obs
def test(ntrial=1, dim=2000, show=False): import galsim import biggles import images rng = np.random.RandomState() nobj_per = 4 nknots = 100 knot_flux_frac = 0.001 nknots_low, nknots_high = 1, 100 nband = 3 noises = [0.0005, 0.001, 0.0015] scale = 0.263 psf = galsim.Gaussian(fwhm=0.9) dims = 64, 64 flux_low, flux_high = 0.5, 1.5 r50_low, r50_high = 0.1, 2.0 fracdev_low, fracdev_high = 0.001, 0.99 bulge_colors = np.array([0.5, 1.0, 1.5]) disk_colors = np.array([1.25, 1.0, 0.75]) knots_colors = np.array([1.5, 1.0, 0.5]) sigma = dims[0]/2.0/4.0*scale maxrad = dims[0]/2.0/2.0 * scale tm0 = time.time() nobj_meas = 0 for trial in range(ntrial): print("trial: %d/%d" % (trial+1, ntrial)) all_band_obj = [] for i in range(nobj_per): nknots = int(rng.uniform(low=nknots_low, high=nknots_high)) r50 = rng.uniform(low=r50_low, high=r50_high) flux = rng.uniform(low=flux_low, high=flux_high) dx, dy = rng.normal(scale=sigma, size=2).clip( min=-maxrad, max=maxrad, ) g1d, g2d = rng.normal(scale=0.2, size=2).clip(max=0.5) g1b = 0.5*g1d+rng.normal(scale=0.02) g2b = 0.5*g2d+rng.normal(scale=0.02) fracdev = rng.uniform(low=fracdev_low, high=fracdev_high) flux_bulge = fracdev*flux flux_disk = (1-fracdev)*flux flux_knots = nknots*knot_flux_frac*flux_disk print("fracdev:", fracdev, "nknots:", nknots) bulge_obj = galsim.DeVaucouleurs( half_light_radius=r50 ).shear(g1=g1b, g2=g2b) disk_obj = galsim.Exponential( half_light_radius=r50 ).shear(g1=g1d, g2=g2d) knots_obj = galsim.RandomWalk( npoints=nknots, profile=disk_obj, ) # .shear(g1=g1d, g2=g2d) band_objs = [] for band in range(nband): band_disk = disk_obj.withFlux(flux_disk*disk_colors[band]) band_bulge = bulge_obj.withFlux(flux_bulge*bulge_colors[band]) band_knots = knots_obj.withFlux(flux_knots*knots_colors[band]) obj = galsim.Sum(band_disk, band_bulge, band_knots).shift( dx=dx, dy=dy, ) obj = galsim.Convolve(obj, psf) band_objs.append(obj) all_band_obj.append(band_objs) jacob = ngmix.DiagonalJacobian( row=0, col=0, scale=scale, ) wcs = jacob.get_galsim_wcs() psfim = psf.drawImage(wcs=wcs).array psf_obs = get_psf_obs(psfim, jacob) dlist = [] for band in range(nband): band_objects = [o[band] for o in all_band_obj] obj = galsim.Sum(band_objects) im = obj.drawImage(nx=dims[1], ny=dims[0], wcs=wcs).array im = obj.drawImage(nx=dims[1], ny=dims[0], scale=scale).array im += rng.normal(scale=noises[band], size=im.shape) wt = im*0 + 1.0/noises[band]**2 dlist.append( dict( image=im, weight=wt, wcs=wcs, ) ) mer = MEDSifier(dlist) mg = mer.get_meds(0) mr = mer.get_meds(1) mi = mer.get_meds(2) nobj = mg.size print(" found", nobj, "objects") nobj_meas += nobj list_of_obs = [] for i in range(nobj): gobslist = mg.get_obslist(i, weight_type='uberseg') robslist = mr.get_obslist(i, weight_type='uberseg') iobslist = mi.get_obslist(i, weight_type='uberseg') mbo = ngmix.MultiBandObsList() mbo.append(gobslist) mbo.append(robslist) mbo.append(iobslist) list_of_obs.append(mbo) for mbo in list_of_obs: for obslist in mbo: for obs in obslist: obs.set_psf(psf_obs) prior = moflib.get_mof_prior(list_of_obs, "bdf", rng) mof_fitter = moflib.MOFStamps( list_of_obs, "bdf", prior=prior, ) band = 2 guess = moflib.get_stamp_guesses(list_of_obs, band, "bdf", rng) mof_fitter.go(guess) if show: # corrected images tab = biggles.Table(1, 2) rgb = images.get_color_image( dlist[2]['image'].transpose(), dlist[1]['image'].transpose(), dlist[0]['image'].transpose(), nonlinear=0.1, ) rgb *= 1.0/rgb.max() tab[0, 0] = images.view_mosaic( [rgb, mer.seg, mer.detim], titles=['image', 'seg', 'detim'], show=False, # dims=[dim, dim], ) imlist = [] for iobj, mobs in enumerate(list_of_obs): cmobs = mof_fitter.make_corrected_obs(iobj) gim = images.make_combined_mosaic( [mobs[0][0].image, cmobs[0][0].image], ) rim = images.make_combined_mosaic( [mobs[1][0].image, cmobs[1][0].image], ) iim = images.make_combined_mosaic( [mobs[2][0].image, cmobs[2][0].image], ) rgb = images.get_color_image( iim.transpose(), rim.transpose(), gim.transpose(), nonlinear=0.1, ) rgb *= 1.0/rgb.max() imlist.append(rgb) plt = images.view_mosaic(imlist, show=False) tab[0, 1] = plt tab.show(width=dim*2, height=dim) if ntrial > 1: if 'q' == input("hit a key: "): return total_time = time.time()-tm0 print("time per group:", total_time/ntrial) print("time per object:", total_time/nobj_meas)
def make_sim_obs(rng, noise, shear, show=False): """ simulate an exponential object with moffat psf the hlr of the exponential is drawn from a gaussian with mean 0.4 arcseconds and sigma 0.2 Parameters ---------- rng: np.random.RandomState The random number generator noise: float Noise for the image shear: (g1, g2) The shear in each component Returns ------- ngmix.Observation """ psf_noise = 1.0e-6 nobj = rng.poisson(NOBJ_MEAN) psf_fwhm = 0.9 psf = galsim.Moffat( beta=2.5, fwhm=psf_fwhm, ).shear( g1=0.02, g2=-0.01, ) objs0 = [] for i in range(nobj): dx, dy = rng.uniform(low=-OFFSET_MAX, high=OFFSET_MAX, size=2) gal_hlr = rng.normal(loc=0.4, scale=0.2) obj0 = galsim.Exponential( half_light_radius=gal_hlr, ).shear( g1=shear[0], g2=shear[1], ).shift( dx=dx, dy=dy, ) objs0.append(obj0) objs0 = galsim.Sum(objs0) objs = galsim.Convolve(psf, objs0) psf_im = psf.drawImage(scale=SCALE).array im = objs.drawImage(nx=DIM, ny=DIM, scale=SCALE).array psf_im += rng.normal(scale=psf_noise, size=psf_im.shape) im += rng.normal(scale=noise, size=im.shape) cen = (np.array(im.shape)-1.0)/2.0 psf_cen = (np.array(psf_im.shape)-1.0)/2.0 jacobian = ngmix.DiagonalJacobian( row=cen[0] + dy/SCALE, col=cen[1] + dx/SCALE, scale=SCALE, ) psf_jacobian = ngmix.DiagonalJacobian( row=psf_cen[0], col=psf_cen[1], scale=SCALE, ) wt = im*0 + 1.0/noise**2 psf_wt = psf_im*0 + 1.0/psf_noise**2 psf_obs = ngmix.Observation( psf_im, weight=psf_wt, jacobian=psf_jacobian, ) obs = ngmix.Observation( im, weight=wt, jacobian=jacobian, psf=psf_obs, ) if show: from espy import images images.view(im) return obs
def meas_one_im(*, g1, g2, seed, n_stars=0, sym_nfold=None, interp=False): rng = np.random.RandomState(seed=seed) obj = galsim.Exponential(half_light_radius=0.5).shear(g1=g1, g2=g2) psf = galsim.Gaussian(fwhm=0.9).withFlux(1e6) obj = galsim.Convolve([obj, psf]) dim = 53 cen = (dim - 1) // 2 offset = rng.uniform(low=-0.5, high=0.5, size=2) im = obj.drawImage(nx=dim, ny=dim, scale=0.2, offset=offset).array jac = jac = ngmix.DiagonalJacobian( scale=0.2, row=cen + offset[1], col=cen + offset[0], ) psf_im = psf.drawImage(nx=dim, ny=dim, scale=0.2).array psf_jac = ngmix.DiagonalJacobian(scale=0.2, row=cen, col=cen) psf_obs = ngmix.Observation( image=psf_im, weight=np.ones_like(psf_im), jacobian=psf_jac, ) wgt = np.ones_like(im) bmask = np.zeros_like(im, dtype=np.int32) if True: for _ in range(n_stars): srad = np.power(10, rng.uniform(low=1, high=3)) ang = rng.uniform(low=0, high=2.0 * np.pi) lrad = rng.uniform(low=(srad - (dim / 2 - 3)), high=srad - (dim / 2 - 10)) + dim / 2 xc = lrad * np.cos(ang) + dim / 2 yc = lrad * np.sin(ang) + dim / 2 srad2 = srad * srad x, y = np.meshgrid(np.arange(dim), np.arange(dim)) msk = ((x - xc)**2 + (y - yc)**2) < srad2 bmask[msk] = 1 else: import scipy.ndimage msk = np.zeros_like(bmask) angle = rng.uniform(low=0, high=360) * 0 col = int(rng.uniform(low=dim / 2, high=dim - 1)) msk[:, col:] = 1 msk = scipy.ndimage.rotate( msk, angle, reshape=False, order=1, mode='constant', cval=1.0, ) bmask[msk == 1] = 1 if sym_nfold is not None: bmask = symmetrize_bmask_nfold(bmask=bmask, nfolds=sym_nfold) msk = bmask != 0 wgt[msk] = 0 im[msk] = np.nan if interp: nse = rng.normal(size=im.shape) iim, inse = interpolate_image_and_noise(image=im, noises=[nse], weight=wgt, bmask=bmask, bad_flags=1, rng=rng, fill_isolated_with_noise=True) obs = ngmix.Observation( image=im, weight=wgt, jacobian=jac, psf=psf_obs, bmask=bmask, ) mom = GaussMom(fwhm=1.2) res = mom.go(obs=obs) gauss_wgt = ngmix.GMixModel( [0, 0, 0, 0, ngmix.moments.fwhm_to_T(1.2), 1], 'gauss', ) cobs = obs.copy() cobs.image = 1.0 - wgt cobs.weight = np.ones_like(wgt) stats = gauss_wgt.get_weighted_sums( cobs, 1.2 * 2, ) mfrac = stats["sums"][5] / stats["wsum"] return res["e"][0], res["e"][1], obs, mfrac
def _set_coadd_obs_same(self): """ base the wcs for the coadd off the first observation """ for i, obs in enumerate(self.observations): if i == 0: ny, nx = obs.image.shape pny, pnx = obs.psf.image.shape else: tny, tnx = obs.image.shape tpny, tpnx = obs.psf.image.shape if ny != tny or nx != tnx: raise ValueError("ps sizes don't match: " "[%d,%d] vs [%d,%d]" % (ny, nx, tny, tnx)) if pny != tpny or pnx != tpnx: raise ValueError("psf ps sizes don't match: " "[%d,%d] vs [%d,%d]" % (pny, pnx, tpny, tpnx)) tim = galsim.ImageD(nx, ny) ptim = galsim.ImageD(pnx, pny) self.canonical_center = self._get_canonical_center(tim) self.psf_canonical_center = self._get_canonical_center(ptim) self.nx = nx self.ny = ny self.psf_nx = pnx self.psf_ny = pny obs0 = self.observations[0] ojac = obs0.jacobian opjac = obs0.psf.jacobian if self.flat_wcs: jac = ngmix.DiagonalJacobian( row=ojac.get_row0(), col=ojac.get_col0(), scale=ojac.get_scale(), ) pjac = ngmix.DiagonalJacobian( row=opjac.get_row0(), col=opjac.get_col0(), scale=opjac.get_scale(), ) else: jac = ojac.copy() pjac = opjac.copy() psf_obs = ngmix.Observation( ptim.array, weight=ptim.array * 0 + 1.0, jacobian=pjac, ) self.coadd_obs = ngmix.Observation( tim.array, weight=tim.array * 0 + 1.0, jacobian=jac, psf=psf_obs, )
def _set_coadd_obs(self): """ base the coadd off the observation with largest postage stamp But for consistency, we always take the jacobian from the first. This way we always know which wcs has been used """ nxs = np.zeros(len(self.observations), dtype='i8') nys = nxs.copy() pnxs = nxs.copy() pnys = nxs.copy() for i, obs in enumerate(self.observations): ny, nx = obs.image.shape nxs[i] = nx nys[i] = ny pny, pnx = obs.psf.image.shape pnxs[i] = pnx pnys[i] = pny argx = nxs.argmax() argy = nys.argmax() pargx = pnxs.argmax() pargy = pnys.argmax() assert argx == argy nx = nxs[argx] ny = nys[argy] pnx = pnxs[pargx] pny = pnys[pargy] tim = galsim.ImageD(nx, ny) ptim = galsim.ImageD(pnx, pny) self.canonical_center = self._get_canonical_center(tim) self.psf_canonical_center = self._get_canonical_center(ptim) self.nx = nx self.ny = ny self.psf_nx = pnx self.psf_ny = pny # we reset the center of the jacobian to # the canonical center later if self.jacobian is not None: jac = self.jacobian.copy() pjac = self.jacobian.copy() else: obs0 = self.observations[0] ojac = obs0.jacobian opjac = obs0.psf.jacobian if self.flat_wcs: jac = ngmix.DiagonalJacobian( row=ojac.get_row0(), col=ojac.get_col0(), scale=ojac.get_scale(), ) pjac = ngmix.DiagonalJacobian( row=opjac.get_row0(), col=opjac.get_col0(), scale=opjac.get_scale(), ) else: jac = ojac.copy() pjac = opjac.copy() psf_obs = ngmix.Observation( ptim.array, weight=ptim.array * 0 + 1.0, jacobian=pjac, ) self.coadd_obs = ngmix.Observation( tim.array, weight=tim.array * 0 + 1.0, jacobian=jac, psf=psf_obs, )
def make_obs( *, n_grid=6, dim=235, buff=20, scale=0.2, psf_fwhm=0.9, hlr=0.5, nse=1e-7, star_dxdy=117, star_rad=1, n_stars=5, seed=10, shear=(0.02, 0.0), mcal_shear=(0.0, 0.0) ): rng = np.random.RandomState(seed=seed) n_gals = n_grid**2 tot_dim = dim + 2*buff tot_cen = (tot_dim-1)/2 gloc = (np.arange(n_grid) + 0.5) * (dim / n_grid) - dim/2 gloc *= scale dx, dy = np.meshgrid(gloc, gloc) dx = dx.ravel() + rng.uniform(low=-0.5, high=0.5, size=n_gals) * scale dy = dy.ravel() + rng.uniform(low=-0.5, high=0.5, size=n_gals) * scale ds = np.arange(n_gals) / (n_gals-1) * 0 + 1 gals = galsim.Sum([ galsim.Exponential( half_light_radius=hlr * _ds ).shift( _dx, _dy ).shear( g1=shear[0], g2=shear[1] ).shear( g1=mcal_shear[0], g2=mcal_shear[1] ) for _ds, _dx, _dy in zip(ds, dx, dy) ]) psf = galsim.Gaussian(fwhm=psf_fwhm) objs = galsim.Convolve([gals, psf]) im = objs.drawImage(nx=tot_dim, ny=tot_dim, scale=scale).array im += rng.normal(size=im.shape, scale=nse) nim = rng.normal(size=im.shape, scale=nse) psf_dim = 53 psf_cen = (psf_dim-1)/2 psf_im = psf.drawImage(nx=psf_dim, ny=psf_dim, scale=scale).array # make bmask bmask = np.zeros_like(im, dtype=np.int32) x, y = np.meshgrid(np.arange(tot_dim), np.arange(tot_dim)) sdata = [] for _ in range(n_stars): sr2 = np.power(10.0, rng.uniform(low=star_rad, high=star_rad+0.2))**2 sx = rng.uniform(low=-star_dxdy, high=star_dxdy) + tot_cen sy = rng.uniform(low=-star_dxdy, high=star_dxdy) + tot_cen dr2 = (x - sx)**2 + (y - sy)**2 msk = dr2 < sr2 bmask[msk] |= 2**0 im[msk] = 0 sdata.append((sx, sy, np.sqrt(sr2))) psf_obs = ngmix.Observation( image=psf_im, weight=np.ones_like(psf_im) / nse**2, jacobian=ngmix.DiagonalJacobian(scale=scale, row=psf_cen, col=psf_cen) ) wgt = np.ones_like(im) / nse**2 msk = bmask != 0 wgt[msk] = 0.0 mfrac = np.zeros_like(im) mfrac[msk] = 1.0 obs = ngmix.Observation( image=im, noise=nim, weight=wgt, bmask=bmask, ormask=bmask, jacobian=ngmix.DiagonalJacobian(scale=scale, row=tot_cen, col=tot_cen), psf=psf_obs ) obs.mfrac = mfrac mbobs = ngmix.MultiBandObsList() obsl = ngmix.ObsList() obsl.append(obs) mbobs.append(obsl) mbobs.meta["sdata"] = sdata return mbobs
def get_mof_ngal_model(self): sim, nobj, nfail, objects, coord_list, fit_model, noise, dims = self.sim( ) ####Created simple simulation for testing scale = 0.263 psf = galsim.Gaussian(fwhm=0.9) psf_gsim = psf.drawImage(nx=25, ny=25, scale=scale) psf_im = psf_gsim.array psf_err = 0.0001 noise_psf = np.random.normal(scale=psf_err, size=psf_im.shape) psf_im += noise_psf psf_weight = psf_im * 0 + 1.0 / psf_err**2 obj = galsim.Gaussian( half_light_radius=.5, flux=6000., ) dx1 = np.random.uniform(low=-0.5, high=0.5) dy1 = np.random.uniform(low=-0.5, high=0.5) obj = obj.shift(dx=dx1 * scale, dy=dy1 * scale) obj = galsim.Convolve(obj, psf) gsim = obj.drawImage( nx=dims[0], ny=dims[1], scale=scale, ) im = gsim.array err = 0.001 noise = np.random.normal(scale=err, size=im.shape) im += noise weight = im * 0 + 1.0 / err**2 psf_cen = (np.array(psf_im.shape) - 1.0) / 2.0 psf_jac = ngmix.DiagonalJacobian(scale=scale, row=psf_cen[0], col=psf_cen[1]) psf_obs = ngmix.Observation( psf_im, weight=psf_weight, jacobian=psf_jac, ) cen = (np.array(dims) - 1.0) / 2.0 coord1 = (dy1 + cen[1], dx1 + cen[0]) jac = ngmix.DiagonalJacobian(scale=scale, row=cen[0] + dy1, col=cen[1] + dx1) obs = ngmix.Observation( im, weight=weight, jacobian=jac, psf=psf_obs, ) noise = np.random.normal(scale=err, size=im.shape) obs.noise = noise #coord2 = (dy2+cen[1],dx2+cen[0]) coord_list = [coord1] # obs = observation(im,0.001,coord1[0],coord1[1],0.0001,psf_im) # obs.noise = noise #noise = np.random.normal(scale=0.001,size=(dims[0],dims[1])) return [obs], coord_list jac = sim.obs.jacobian jac.set_cen(row=objects['y'][0], col=objects['x'][0]) sim.obs.jacobian = jac return [sim.obs], coord_list ###Orginal start of get ngal model prior = self._get_mof_prior(coord_list, 1, sim.obs.jacobian, objects) fitter = minimof.MOF( sim.obs, fit_model, len(coord_list), prior=prior, ) nobs = [] if len(coord_list) != 0: for i in range(2): guess = self._get_mof_guess(coord_list, sim.obs, sim.obs.jacobian, objects) fitter.go(guess) res = fitter.get_result() if res['flags'] == 0: break if res['flags'] == 0: ngmix.print_pars(res['pars'], front=" fit:") for i in range(len(coord_list)): obs = fitter.make_corrected_obs(i, band=None, obsnum=None, recenter=True) obs[0][0].noise = sim.obs.noise nobs.append(obs) return nobs, coord_list