def _fit_one(self, source_id, pars=None): """ Workhorse method to perfom home-brewed metacalibration on an object. Returns the unsheared ellipticities of each galaxy, as well as a label indicating whether the galaxy passed the selection cuts for each shear step (i.e. no_shear,1p,1m,2p,2m). """ index = source_id jaclist = self._get_jacobians(source_id) obslist = self._get_source_observations(source_id) mcal_shear = 0.01 if pars is None: lm_pars = {'maxfev': 2000, 'xtol': 5.0e-5, 'ftol': 5.0e-5} max_pars = {'method': 'lm', 'lm_pars': lm_pars} psf_model = 'gauss' # should come up with diagnostics for PSF quality gal_model = 'gauss' mcal_obs = ngmix.metacal.get_all_metacal(obslist) # Make a dictionary to hold the results result = {} mcal_obs_keys = ['noshear', '1p', '1m', '2p', '2m'] # Run the actual metacalibration fits on the observed galaxies with Bootstrapper() # and flag for ikey in mcal_obs_keys: boot = ngmix.Bootstrapper(mcal_obs[ikey]) boot.fit_psfs(psf_model, 1.) boot.fit_max(gal_model, max_pars) res = boot.get_fitter().get_result() #result.update({ikey:res['g']}) try: res['Tpsf'] = boot.psf_fitter._gm.get_T() except: res['Tpsf'] = boot.psf_fitter._result['T'] result.update({ikey: res}) R1 = (result['1p']['g'][0] - result['1m']['g'][0]) / (2 * mcal_shear) R2 = (result['2p']['g'][1] - result['2m']['g'][1]) / (2 * mcal_shear) print(f"R1: {R1:.3} \nR2:{R2:.3} ") mcr = result self.metacal = mcr # # # Also get regular galaxy shape EM fit; if this fails you're in trouble gal_obs = self._one_gal_obs(source_id=index) boot = ngmix.Bootstrapper(gal_obs) boot.fit_psfs(psf_model, 1.) boot.fit_max(gal_model=gal_model, pars=max_pars) gal_fit = boot.get_fitter() self.gal_fit = gal_fit return mcr, gal_fit
def _fit_one(self, source_id, pars=None): """ workhorse method to perfom home-brewed metacalibration on an object """ index = source_id psf_cutout = self.medsObj.get_cutout(index, 0, type='psf') image_cutout = self.medsObj.get_cutout(index, 0) weight_cutout = self.medsObj.get_cutout(index, 0, type='weight') # to help Bootstrapper distinguish between object and background image_cutout[image_cutout <= 0] = 1E-2 psf_noise = 1e-6 this_psf = psf_cutout + 1e-6 * np.random.randn(psf_cutout.shape[0], psf_cutout.shape[1]) psf_weight_image = np.zeros_like(this_psf) + 1. / psf_noise**2 weight_image = np.ones_like(image_cutout) * 1e9 jj_im = ngmix.jacobian.DiagonalJacobian(scale=0.206, x=(image_cutout.shape[0]) / 2, y=(image_cutout.shape[1]) / 2) jj_psf = ngmix.jacobian.DiagonalJacobian(scale=0.206, x=psf_cutout.shape[0] / 2, y=psf_cutout.shape[1] / 2) psf_obs = ngmix.Observation(psf_cutout, weight=psf_weight_image, jacobian=jj_psf) gal_obs = ngmix.Observation(image_cutout, weight=weight_image, jacobian=jj_im, psf=psf_obs) mcal_shear = 0.01 if pars is None: lm_pars = {'maxfev': 2000, 'xtol': 5.0e-5, 'ftol': 5.0e-5} max_pars = {'method': 'lm', 'lm_pars': lm_pars} psf_model = 'gauss' gal_model = 'exp' mcal_obs = ngmix.metacal.get_all_metacal(gal_obs) # Make a dictionary to hold the results. result = {} # Run the actual metacalibration fits on the observed galaxies with Bootstrapper() for ikey in mcal_obs.keys(): boot = ngmix.Bootstrapper(mcal_obs[ikey]) boot.fit_psfs('gauss', 1.) boot.fit_max('exp', max_pars) res = boot.get_fitter().get_result() result.update({ikey: res['g']}) #fit,ax = plt.subplots(nrows=1,ncols=2,figsize=(14,7)) R1 = (result['1p'][0] - result['1m'][0]) / (2 * mcal_shear) R2 = (result['2p'][1] - result['2m'][1]) / (2 * mcal_shear) print(f"R1: {R1:.3} \nR2:{R2:.3} ") mcr = result #mcr = {'g1_noshear':result['noshear'][0], 'g2_noshear':result['noshear'][1], 'r11':R1, 'r22':R2} self.metacal = mcr # # # Also get regular galaxy shape EM fit, allowing for exception boot = ngmix.Bootstrapper(gal_obs) boot.fit_psfs(psf_model, 1.) boot.fit_max(gal_model='exp', pars=max_pars) gal_fit = boot.get_fitter() self.gal_fit = gal_fit return mcr, gal_fit
psf=psfObs) image_obslist_realsigma.append(imageObs) del (this_gal_stamp, imageObs) # What does it look like to you? plt.imshow(image_cutout) #### #### do ngmix fitting #### mcal_obs_realsigma = ngmix.metacal.get_all_metacal(image_obslist_realsigma) # just do no_shear fit boot_realsigma = ngmix.Bootstrapper(mcal_obs_realsigma['noshear']) boot_realsigma.fit_psfs(psf_model, 1.) # Alright, now let's see the actual galaxy fit # wake up. This means... running the boot fit, and then plotting up that galaxy boot_realsigma.fit_max(gal_model, max_pars, prior=prior) res_realsigma = boot_realsigma.get_fitter().get_result() # Wanna see the galaxy model get rendered? junk_im = [] for i in range(n_obs): junk_im.append(image_obslist_realsigma[i].image) junk_avg = np.mean(junk_im, axis=0) gal_gm2 = boot_realsigma.get_fitter().get_convolved_gmix() gal_model_im2 = gal_gm2.make_image(image_cutout.shape, jacobian=jj_im)
weight=weight_image, jacobian=jj_im, psf=psf_obs) # Make the metacal observations. mcal_obs = ngmix.metacal.get_all_metacal(gal_obs) # Make a dictionary to hold the results. result = {} # Do the fit. lm_pars = {'maxfev': 2000, 'xtol': 5.0e-5, 'ftol': 5.0e-5} max_pars = {'method': 'lm', 'lm_pars': lm_pars} for ikey in mcal_obs.keys(): boot = ngmix.Bootstrapper(mcal_obs[ikey]) boot.fit_psfs('gauss', 1.) boot.fit_max('exp', max_pars) res = boot.get_fitter().get_result() result.update({ikey: res['g']}) #fit,ax = plt.subplots(nrows=1,ncols=2,figsize=(14,7)) R1 = (result['1p'][0] - result['1m'][0]) / (2 * mcal_shear) R2 = (result['2p'][1] - result['2m'][1]) / (2 * mcal_shear) print(f"R1: {R1:.3} \nR2:{R2:.3} ") gal_sheared = gal_ideal.shear(galsim.Shear(g1=true_shear, g2=0.0)) gal_sheared_observed = galsim.Convolve([gal_sheared, psf_metacal]) gal_sheared_image = gal_sheared_observed.drawImage(scale=0.206) gal_sheared_weight = np.ones_like(gal_sheared_image.array) * 1e9
def _fit_one(self, source_id, pars=None): """ Workhorse method to perfom home-brewed metacalibration on an object. Returns the unsheared ellipticities of each galaxy, as well as a label indicating whether the galaxy passed the selection cuts for each shear step (i.e. no_shear,1p,1m,2p,2m). """ jaclist = _get_jacobians(source_id) obslist = self._get_source_observations(source_id) obslist = image_obslist mcal_shear = 0.01 lm_pars = {'maxfev': 2000, 'xtol': 5.0e-5, 'ftol': 5.0e-5} max_pars = {'method': 'lm', 'lm_pars': lm_pars, 'find_center': True} prior = _get_priors() psf_model = 'em5' # should come up with diagnostics for PSF quality gal_model = 'gauss' mcal_obs = ngmix.metacal.get_all_metacal(obslist) # Make a dictionary to hold the results result = {} mcal_obs_keys = ['noshear', '1p', '1m', '2p', '2m'] # Run the actual metacalibration fits on the observed galaxies with Bootstrapper() # and flag #for ikey in mcal_obs_keys: ikey = 'noshear' boot = ngmix.Bootstrapper(mcal_obs[ikey]) boot.fit_psfs(psf_model, 1.) boot.fit_max(gal_model, max_pars, prior=prior) res = boot.get_fitter().get_result() #result.update({ikey:res['g']}) try: res['Tpsf'] = boot.psf_fitter._gm.get_T() except: res['Tpsf'] = boot.psf_fitter._result['T'] result.update({ikey: res}) t_arr = [] for p in boot.mb_obs_list[0]: print(p.psf.get_gmix().get_e1e2T()) t_arr.append(p.psf.get_gmix().get_e1e2T()[2]) np.mean(t_arr) """ Wanna see the PSF model get rendered?? """ #psf_cutout = psf_cutouts[3] #psf_cutout = gpsf_cutout.array psf_gm = boot.psf_fitter.get_gmix() psf_model_im = psf_gm.make_image(psf_cutout.shape, jacobian=jj_psf) fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(21, 7)) ax1.imshow(psf_cutout) #,vmin=0,vmax=(psf_cutout.max()*.90)) ax2.imshow(psf_model_im) #,vmin=0,vmax=(psf_cutout.max()*.90)) ax3.imshow(psf_cutout - psf_model_im) """ Wanna see the galaxy model get rendered? """ image_cutout = image_cutouts[i] gal_gm = boot.get_fitter().get_convolved_gmix() gal_model_im = gal_gm.make_image(image_cutout.shape, jacobian=jj) fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(21, 7)) ax1.imshow(junk_avg) #,vmin=0,vmax=(psf_cutout.max()*.90)) ax2.imshow(gal_model_im) #,vmin=0,vmax=(psf_cutout.max()*.90)) ax3.imshow(junk_avg - gal_model_im) R1 = (result['1p']['g'][0] - result['1m']['g'][0]) / (2 * mcal_shear) R2 = (result['2p']['g'][1] - result['2m']['g'][1]) / (2 * mcal_shear) print(f"R1: {R1:.3} \nR2:{R2:.3} ") return boot.get_fitter()