def get_exp_list(self, psf2=None): if psf2 is None: psf2 = self.psfs obs_list = ObsList() psf_list = ObsList() w = [] for i in range(len(self.gals)): im = self.gals[i].array im_psf = self.psfs[i].array im_psf2 = psf2[i].array weight = 1 / self.skys[i].array jacob = self.gals[i].wcs.jacobian() dx = self.offsets[i].x dy = self.offsets[i].y gal_jacob = Jacobian(row=self.gals[i].true_center.y + dy, col=self.gals[i].true_center.x + dx, dvdrow=jacob.dvdy, dvdcol=jacob.dvdx, dudrow=jacob.dudy, dudcol=jacob.dudx) psf_jacob2 = gal_jacob mask = np.where(weight != 0) w.append(np.mean(weight[mask])) noise = old_div(np.ones_like(weight), w[-1]) psf_obs = Observation(im_psf, jacobian=gal_jacob, meta={ 'offset_pixels': None, 'file_id': None }) psf_obs2 = Observation(im_psf2, jacobian=psf_jacob2, meta={ 'offset_pixels': None, 'file_id': None }) obs = Observation(im, weight=weight, jacobian=gal_jacob, psf=psf_obs, meta={ 'offset_pixels': None, 'file_id': None }) obs.set_noise(noise) obs_list.append(obs) psf_list.append(psf_obs2) return obs_list, psf_list, np.array(w)
def test_obslist_set(): rng = np.random.RandomState(seed=11) meta = {'duh': 5} obslist = ObsList(meta=meta) for _ in range(3): obslist.append(Observation(image=rng.normal(size=(13, 15)))) assert obslist.meta == meta new_meta = {'blah': 6} obslist.meta = new_meta assert obslist.meta == new_meta obslist.meta = None assert len(obslist.meta) == 0 with pytest.raises(TypeError): obslist.meta = [10] with pytest.raises(TypeError): obslist.set_meta([10]) new_meta = {'bla': 6} new_meta.update(obslist.meta) obslist.update_meta_data({'bla': 6}) assert obslist.meta == new_meta with pytest.raises(TypeError): obslist.update_meta_data([10]) rng = np.random.RandomState(seed=12) new_obs = Observation(image=rng.normal(size=(13, 15))) rng = np.random.RandomState(seed=11) for obs in obslist: assert np.all(obs.image == rng.normal(size=(13, 15))) obslist[1] = new_obs assert np.all(obslist[1].image == new_obs.image)
def obslist_data(): dims = (12, 12) rng = np.random.RandomState(seed=10) jac = DiagonalJacobian(x=5.5, y=5.5, scale=0.25) obslist = ObsList() obslist.append( Observation( image=rng.normal(size=dims), jacobian=jac.copy(), weight=np.exp(rng.normal(size=dims)), meta={"blue": 10}, ) ) obslist.append( Observation( image=rng.normal(size=dims), jacobian=jac.copy(), weight=np.exp(rng.normal(size=dims)), meta={"blue": 8}, ) ) psf = Observation( image=rng.normal(size=dims), weight=np.exp(rng.normal(size=dims)), jacobian=jac.copy(), meta={"blue": 5}, ) obslist[1].psf = psf return obslist
def test_multibandobslist_set(): rng = np.random.RandomState(seed=11) meta = {'duh': 5} mbobs = MultiBandObsList(meta=meta) for _ in range(5): obslist = ObsList() for _ in range(3): obslist.append(Observation(image=rng.normal(size=(13, 15)))) mbobs.append(obslist) assert mbobs.meta == meta new_meta = {'blah': 6} mbobs.meta = new_meta assert mbobs.meta == new_meta mbobs.meta = None assert len(mbobs.meta) == 0 with pytest.raises(TypeError): mbobs.meta = [10] with pytest.raises(TypeError): mbobs.set_meta([10]) new_meta = {'bla': 6} new_meta.update(mbobs.meta) mbobs.update_meta_data({'bla': 6}) assert mbobs.meta == new_meta with pytest.raises(TypeError): mbobs.update_meta_data([10]) rng = np.random.RandomState(seed=12) new_obs = Observation(image=rng.normal(size=(13, 15))) rng = np.random.RandomState(seed=11) for obslist in mbobs: for obs in obslist: assert np.all(obs.image == rng.normal(size=(13, 15))) mbobs[1][2] = new_obs assert np.all(mbobs[1][2].image == new_obs.image) rng = np.random.RandomState(seed=13) obslist = ObsList() for _ in range(4): obslist.append(Observation(image=rng.normal(size=(13, 15)))) mbobs[2] = obslist rng = np.random.RandomState(seed=13) for obs in mbobs[2]: assert np.all(obs.image == rng.normal(size=(13, 15)))
def get_exp_list(gals_array, psfs_array, offsets, skys_array, gal_true, gal_jacobs, psf2=None): #def get_exp_list(gal, psf, sky_stamp, psf2=None): if psf2 is None: psf2 = psfs_array obs_list=ObsList() psf_list=ObsList() w = [] for i in range(len(gals_array)): im = gals_array[i] im_psf = psfs_array[i] im_psf2 = psf2[i] weight = 1/skys_array[i] jacob = gal_jacobs[i] dx = offsets[i].x dy = offsets[i].y gal_jacob = Jacobian( row=gal_true[i].y+dy, col=gal_true[i].x+dx, dvdrow=jacob.dvdy, dvdcol=jacob.dvdx, dudrow=jacob.dudy, dudcol=jacob.dudx) psf_jacob2 = gal_jacob print(gal_jacob) mask = np.where(weight!=0) w.append(np.mean(weight[mask])) noise = old_div(np.ones_like(weight),w[-1]) psf_obs = Observation(im_psf, jacobian=gal_jacob, meta={'offset_pixels':None,'file_id':None}) psf_obs2 = Observation(im_psf2, jacobian=psf_jacob2, meta={'offset_pixels':None,'file_id':None}) obs = Observation(im, weight=weight, jacobian=gal_jacob, psf=psf_obs, meta={'offset_pixels':None,'file_id':None}) obs.set_noise(noise) obs_list.append(obs) psf_list.append(psf_obs2) return obs_list,psf_list,np.array(w)
def test_obslist_smoke(): rng = np.random.RandomState(seed=11) meta = {'duh': 5} obslist = ObsList(meta=meta) for _ in range(3): obslist.append(Observation(image=rng.normal(size=(13, 15)))) assert obslist.meta == meta rng = np.random.RandomState(seed=11) for obs in obslist: assert np.all(obs.image == rng.normal(size=(13, 15)))
def create_mb(mb_mod, bg_rms, coord1, coord2, psf_bg_rms, psf_im, noise): #creats multiband object mb = MultiBandObsList() for i in range(len(mb_mod[:])): o = observation(mb_mod[i], bg_rms, coord1, coord2, psf_bg_rms, psf_im) o.noise = noise[i] olist = ObsList() olist.append(o) mb.append(olist) return mb
def test_obslist_s2n_zeroweight(): rng = np.random.RandomState(seed=11) obslist = ObsList() for _ in range(3): obs = Observation( image=rng.normal(size=(13, 15)), weight=np.zeros((13, 15)), store_pixels=False, ) obslist.append(obs) assert np.allclose(obslist.get_s2n(), -9999)
def test_obslist_s2n(): rng = np.random.RandomState(seed=11) obslist = ObsList() numer = 0 denom = 0 for _ in range(3): obs = Observation(image=rng.normal(size=(13, 15))) numer += np.sum(obs.image) denom += np.sum(1.0 / obs.weight) obslist.append(obs) s2n = obslist.get_s2n() assert s2n == numer / np.sqrt(denom)
def _get_mof_obs(self): im, psf_im, coords, dims, dx1, dy1, noises = self.sim() bg_rms = self.sim['Image']['Bgrms'] / np.sqrt(len(im)) bg_rms_psf = self.sim['Psf']['Bgrms_psf'] psf_ccen = (np.array(psf_im.shape) - 1.0) / 2.0 psf_jacob = ngmix.UnitJacobian( row=psf_ccen[0], col=psf_ccen[1], ) psf_weight = psf_im * 0 + 1.0 / bg_rms_psf**2 psf_obs = ngmix.Observation( psf_im, weight=psf_weight, jacobian=psf_jacob, ) psf_gmix = self._fit_psf_admom(psf_obs) psf_obs.set_gmix(psf_gmix) mb = MultiBandObsList() for i in range(len(im)): if self.show: import images tim = im[i] / im[i].max() tim = np.log10(tim - tim.min() + 1.0) images.view(tim) if 'q' == input('hit a key: (q to quit) '): stop weight = im[i] * 0 + 1.0 / bg_rms**2 jacobian = ngmix.UnitJacobian( row=0, col=0, ) obs = ngmix.Observation( im[i], weight=weight, jacobian=jacobian, psf=psf_obs, ) obs.noise = noises[i] olist = ObsList() olist.append(obs) mb.append(olist) return mb, coords
def test_get_mbobs(): rng = np.random.RandomState(seed=11) obs = Observation(image=rng.normal(size=(11, 13))) mbobs = get_mb_obs(obs) rng = np.random.RandomState(seed=11) assert np.all(mbobs[0][0].image == rng.normal(size=(11, 13))) assert len(mbobs) == 1 assert len(mbobs[0]) == 1 rng = np.random.RandomState(seed=12) obslist = ObsList() for _ in range(3): obslist.append(Observation(image=rng.normal(size=(11, 13)))) mbobs = get_mb_obs(obslist) rng = np.random.RandomState(seed=12) for obs in mbobs[0]: assert np.all(obs.image == rng.normal(size=(11, 13))) assert len(mbobs) == 1 assert len(mbobs[0]) == 3 rng = np.random.RandomState(seed=13) mbobs = MultiBandObsList() for _ in range(5): obslist = ObsList() for _ in range(3): obslist.append(Observation(image=rng.normal(size=(13, 15)))) mbobs.append(obslist) new_mbobs = get_mb_obs(mbobs) rng = np.random.RandomState(seed=13) for obslist in new_mbobs: for obs in obslist: assert np.all(obs.image == rng.normal(size=(13, 15))) with pytest.raises(ValueError): get_mb_obs(None)
def test_multibandobslist_smoke(): rng = np.random.RandomState(seed=11) meta = {'duh': 5} mbobs = MultiBandObsList(meta=meta) for _ in range(5): obslist = ObsList() for _ in range(3): obslist.append(Observation(image=rng.normal(size=(13, 15)))) mbobs.append(obslist) assert mbobs.meta == meta rng = np.random.RandomState(seed=11) for obslist in mbobs: for obs in obslist: assert np.all(obs.image == rng.normal(size=(13, 15)))
def test_multibandobslist_s2n(): rng = np.random.RandomState(seed=11) mbobs = MultiBandObsList() numer = 0 denom = 0 for _ in range(5): obslist = ObsList() for _ in range(3): img = rng.normal(size=(13, 15)) obslist.append(Observation(image=img)) numer += np.sum(img) denom += np.sum(1.0 / obslist[-1].weight) mbobs.append(obslist) s2n = mbobs.get_s2n() assert s2n == numer / np.sqrt(denom)
def test_multibandobslist_s2n_zeroweight(): rng = np.random.RandomState(seed=11) mbobs = MultiBandObsList() for _ in range(5): obslist = ObsList() for _ in range(3): img = rng.normal(size=(13, 15)) obslist.append( Observation( image=img, weight=np.zeros((13, 15)), store_pixels=False, )) mbobs.append(obslist) assert np.allclose(mbobs.get_s2n(), -9999)
def get_obslist(self, iobj, weight_type='weight'): """Get an ngmix ObsList for all observations. Parameters ---------- iobj : int Index of the object. weight_type: string, optional Weight type. can be one of 'weight': the actual weight map 'uberseg': uberseg modified weight map 'cweight': weight map zeroed outside the object's seg map 'cseg': weight map zeroed outside of circular aperture that doesn't touch any other object. 'cseg-canonical': same as 'cseg' but uses the postage stamp center instead of the object's position. Default is 'weight' Returns ------- obslist : ngmix.ObsList An `ObsList` of all observations. """ obslist = ObsList() for icut in range(self._cat['ncutout'][iobj]): try: obs = self.get_obs(iobj, icut, weight_type=weight_type) obslist.append(obs) except GMixFatalError: print('zero weight observation found, skipping') if len(obslist) > 0: obs = obslist[0] if 'flux' in obs.meta: obslist.meta['flux'] = obs.meta['flux'] if 'T' in obs.meta: obslist.meta['T'] = obs.meta['T'] return obslist
def make_multiband_coadd_stamp(): oversample = 4 H = './fiducial_H158_2285117.fits' J = './fiducial_J129_2285117.fits' F = './fiducial_F184_2285117.fits' truth = fio.FITS('/hpc/group/cosmology/phy-lsst/my137/roman_H158/g1002/truth/fiducial_lensing_galaxia_g1002_truth_gal.fits')[-1] m_H158 = meds.MEDS(H) m_J129 = meds.MEDS(J) m_F184 = meds.MEDS(F) indices_H = np.arange(len(m_H158['number'][:])) indices_J = np.arange(len(m_J129['number'][:])) indices_F = np.arange(len(m_F184['number'][:])) roman_H158_psfs = get_psf_SCA('H158') roman_J129_psfs = get_psf_SCA('J129') roman_F184_psfs = get_psf_SCA('F184') for i,ii in enumerate(indices_H): if i%100==0: print('made it to object',i) try_save = False ind = m_H158['number'][ii] t = truth[ind] if (ind not in m_J129['number']) or (ind not in m_F184['number']): continue print(i) # if i==13: sca_Hlist = m_H158[ii]['sca'] # List of SCAs for the same object in multiple observations. ii_J = m_J129[m_J129['number']==ind]['id'][0] sca_Jlist = m_J129[ii_J]['sca'] m2_H158_coadd = [roman_H158_psfs[j-1] for j in sca_Hlist[:m_H158['ncutout'][i]]] m2_J129_coadd = [roman_J129_psfs[j-1] for j in sca_Jlist[:m_J129['ncutout'][ii_J]]] ii_F = m_F184[m_F184['number']==ind]['id'][0] sca_Flist = m_F184[ii_F]['sca'] m2_F184_coadd = [roman_F184_psfs[j-1] for j in sca_Flist[:m_F184['ncutout'][ii_F]]] obs_Hlist,psf_Hlist,included_H,w_H = get_exp_list_coadd(m_H158,ii,oversample,m2=m2_H158_coadd) obs_Jlist,psf_Jlist,included_J,w_J = get_exp_list_coadd(m_J129,ii_J,oversample,m2=m2_J129_coadd) obs_Flist,psf_Flist,included_F,w_F = get_exp_list_coadd(m_F184,ii_F,oversample,m2=m2_F184_coadd) # check if masking is less than 20% if len(obs_Hlist)==0 or len(obs_Jlist)==0 or len(obs_Flist)==0: continue coadd_H = psc.Coadder(obs_Hlist,flat_wcs=True).coadd_obs coadd_H.psf.image[coadd_H.psf.image<0] = 0 # set negative pixels to zero. coadd_H.psf.set_meta({'offset_pixels':None,'file_id':None}) coadd_H.set_meta({'offset_pixels':None,'file_id':None}) coadd_J = psc.Coadder(obs_Jlist,flat_wcs=True).coadd_obs coadd_J.psf.image[coadd_J.psf.image<0] = 0 # set negative pixels to zero. coadd_J.psf.set_meta({'offset_pixels':None,'file_id':None}) coadd_J.set_meta({'offset_pixels':None,'file_id':None}) coadd_F = psc.Coadder(obs_Flist,flat_wcs=True).coadd_obs coadd_F.psf.image[coadd_F.psf.image<0] = 0 # set negative pixels to zero. coadd_F.psf.set_meta({'offset_pixels':None,'file_id':None}) coadd_F.set_meta({'offset_pixels':None,'file_id':None}) obs_list = ObsList() multiband = [coadd_H, coadd_J, coadd_F] for f in range(3): obs_list.append(multiband[f]) multiband_coadd = psc.Coadder(obs_list,flat_wcs=True).coadd_obs print('single snr', get_snr(obs_Jlist), get_snr(obs_Hlist), get_snr(obs_Flist)) print('coadd snr', get_snr([coadd_J]), get_snr([coadd_H]), get_snr([coadd_F])) print('final', get_snr(obs_list))
def single_vs_coadd_images(): local_Hmeds = './fiducial_F184_2285117.fits' truth = fio.FITS('/hpc/group/cosmology/phy-lsst/my137/roman_F184/g1002/truth/fiducial_lensing_galaxia_g1002_truth_gal.fits')[-1] m_H158 = meds.MEDS(local_Hmeds) indices_H = np.arange(len(m_H158['number'][:])) roman_H158_psfs = get_psf_SCA('F184') oversample = 4 metacal_keys=['noshear', '1p', '1m', '2p', '2m'] res_noshear=np.zeros(len(m_H158['number'][:]),dtype=[('ind',int), ('ra',float), ('dec',float), ('flags',int),('int_e1',float), ('int_e2',float),('coadd_px',float), ('coadd_py',float), ('coadd_flux',float), ('coadd_snr',float), ('coadd_e1',float), ('coadd_e2',float), ('coadd_hlr',float),('coadd_psf_e1',float), ('coadd_psf_e2',float), ('coadd_psf_T',float)]) res_1p=np.zeros(len(m_H158['number'][:]),dtype=[('ind',int), ('ra',float), ('dec',float), ('flags',int),('int_e1',float), ('int_e2',float),('coadd_px',float), ('coadd_py',float), ('coadd_flux',float), ('coadd_snr',float), ('coadd_e1',float), ('coadd_e2',float), ('coadd_hlr',float),('coadd_psf_e1',float), ('coadd_psf_e2',float), ('coadd_psf_T',float)]) res_1m=np.zeros(len(m_H158['number'][:]),dtype=[('ind',int), ('ra',float), ('dec',float), ('flags',int),('int_e1',float), ('int_e2',float),('coadd_px',float), ('coadd_py',float), ('coadd_flux',float), ('coadd_snr',float), ('coadd_e1',float), ('coadd_e2',float), ('coadd_hlr',float),('coadd_psf_e1',float), ('coadd_psf_e2',float), ('coadd_psf_T',float)]) res_2p=np.zeros(len(m_H158['number'][:]),dtype=[('ind',int), ('ra',float), ('dec',float), ('flags',int),('int_e1',float), ('int_e2',float),('coadd_px',float), ('coadd_py',float), ('coadd_flux',float), ('coadd_snr',float), ('coadd_e1',float), ('coadd_e2',float), ('coadd_hlr',float),('coadd_psf_e1',float), ('coadd_psf_e2',float), ('coadd_psf_T',float)]) res_2m=np.zeros(len(m_H158['number'][:]),dtype=[('ind',int), ('ra',float), ('dec',float), ('flags',int),('int_e1',float), ('int_e2',float),('coadd_px',float), ('coadd_py',float), ('coadd_flux',float), ('coadd_snr',float), ('coadd_e1',float), ('coadd_e2',float), ('coadd_hlr',float),('coadd_psf_e1',float), ('coadd_psf_e2',float), ('coadd_psf_T',float)]) res_tot=[res_noshear, res_1p, res_1m, res_2p, res_2m] for i,ii in enumerate(indices_H): # looping through all the objects in meds file. if i%100==0: print('object number ',i) # if i not in [1,600]: # continue ind = m_H158['number'][ii] t = truth[ind] sca_Hlist = m_H158[ii]['sca'] # List of SCAs for the same object in multiple observations. m2_H158_coadd = [roman_H158_psfs[j-1] for j in sca_Hlist[:m_H158['ncutout'][i]]] obs_Hlist,psf_Hlist,included_H,w_H = get_exp_list_coadd(m_H158,ii,oversample,m2=m2_H158_coadd) s2n_test = get_snr(obs_Hlist) # if i in [1,600]: #in [ 309, 444, 622, 644, 854, 1070, 1282, 1529]: # for l in range(len(obs_Hlist)): # #print(i, obs_Hlist[l].jacobian, obs_Hlist[l].psf.jacobian) # print(i, obs_Hlist[l].weight) # np.savetxt('/hpc/group/cosmology/masaya/wfirst_simulation/paper/single_image_oversample4_08scaling_'+str(i)+'_'+str(l)+'.txt', obs_Hlist[l].image) # np.savetxt('/hpc/group/cosmology/masaya/wfirst_simulation/paper/single_psf_oversample4_08scaling_'+str(i)+'.txt', obs_Hlist[0].psf.image) coadd_H = psc.Coadder(obs_Hlist,flat_wcs=True).coadd_obs coadd_H.psf.image[coadd_H.psf.image<0] = 0 # set negative pixels to zero. coadd_H.set_meta({'offset_pixels':None,'file_id':None}) obs_list = ObsList() if oversample == 4: new_coadd_psf_block = block_reduce(coadd_H.psf.image, block_size=(4,4), func=np.sum) new_coadd_psf_jacob = Jacobian( row=15.5, col=15.5, dvdrow=(coadd_H.psf.jacobian.dvdrow*oversample), dvdcol=(coadd_H.psf.jacobian.dvdcol*oversample), dudrow=(coadd_H.psf.jacobian.dudrow*oversample), dudcol=(coadd_H.psf.jacobian.dudcol*oversample)) coadd_psf_obs = Observation(new_coadd_psf_block, jacobian=new_coadd_psf_jacob, meta={'offset_pixels':None,'file_id':None}) coadd_H.psf = coadd_psf_obs obs_list.append(coadd_H) s2n_coadd = get_snr(obs_list) # if i in [1,600]: # print(i, coadd_H.weight) #np.savetxt('/hpc/group/cosmology/masaya/wfirst_simulation/paper/coadd_image_oversample4_08scaling_'+str(i)+'.txt', coadd_H.image) # np.savetxt('/hpc/group/cosmology/masaya/wfirst_simulation/paper/coadd_weight_oversample4_08scaling_'+str(i)+'.txt', coadd_H.weight) #np.savetxt('/hpc/group/cosmology/masaya/wfirst_simulation/paper/coadd_psf_over-downsample4_08scaling_'+str(i)+'.txt', coadd_H.psf.image) iteration=0 for key in metacal_keys: res_tot[iteration]['ind'][i] = ind res_tot[iteration]['ra'][i] = t['ra'] res_tot[iteration]['dec'][i] = t['dec'] res_tot[iteration]['int_e1'][i] = t['int_e1'] res_tot[iteration]['int_e2'][i] = t['int_e2'] iteration+=1 res_ = measure_shape_metacal(obs_list, t['size'], method='bootstrap', fracdev=t['bflux'],use_e=[t['int_e1'],t['int_e2']]) print('signal to noise test', i, s2n_test, s2n_coadd, res_['noshear']['s2n_r']) # if res_[key]['s2n'] > 1e7: # print('coadd snr', res_[key]['s2n']) # np.savetxt('large_coadd_snr_image.txt', coadd_H.image) iteration=0 for key in metacal_keys: if res_==0: res_tot[iteration]['ind'][i] = 0 elif res_[key]['flags']==0: res_tot[iteration]['coadd_px'][i] = res_[key]['pars'][0] res_tot[iteration]['coadd_py'][i] = res_[key]['pars'][1] res_tot[iteration]['coadd_snr'][i] = res_[key]['s2n'] res_tot[iteration]['coadd_e1'][i] = res_[key]['pars'][2] res_tot[iteration]['coadd_e2'][i] = res_[key]['pars'][3] res_tot[iteration]['coadd_hlr'][i] = res_[key]['pars'][4] iteration+=1 mask=res_tot[0]['ind']!=0 print(len(res_tot[0]), len(res_tot[0][mask])) #print(res_['noshear'].dtype.names) print('done')
def get_exp_list_coadd(m, i, m2=None): def make_jacobian(dudx, dudy, dvdx, dvdy, x, y): j = galsim.JacobianWCS(dudx, dudy, dvdx, dvdy) return j.withOrigin(galsim.PositionD(x, y)) oversample = 1 #def psf_offset(i,j,star_): m3 = [0] #relative_offset=[0] for jj, psf_ in enumerate(m2): # m2 has psfs for each observation. if jj == 0: continue gal_stamp_center_row = m['orig_start_row'][i][jj] + m['box_size'][ i] / 2 - 0.5 # m['box_size'] is the galaxy stamp size. gal_stamp_center_col = m['orig_start_col'][i][jj] + m['box_size'][ i] / 2 - 0.5 # m['orig_start_row/col'] is in SCA coordinates. psf_stamp_size = 32 # Make the bounds for the psf stamp. b = galsim.BoundsI( xmin=(m['orig_start_col'][i][jj] + (m['box_size'][i] - 32) / 2. - 1) * oversample + 1, xmax=(m['orig_start_col'][i][jj] + (m['box_size'][i] - 32) / 2. + psf_stamp_size - 1) * oversample, ymin=(m['orig_start_row'][i][jj] + (m['box_size'][i] - 32) / 2. - 1) * oversample + 1, ymax=(m['orig_start_row'][i][jj] + (m['box_size'][i] - 32) / 2. + psf_stamp_size - 1) * oversample) # Make wcs for oversampled psf. wcs_ = make_jacobian( m.get_jacobian(i, jj)['dudcol'] / oversample, m.get_jacobian(i, jj)['dudrow'] / oversample, m.get_jacobian(i, jj)['dvdcol'] / oversample, m.get_jacobian(i, jj)['dvdrow'] / oversample, m['orig_col'][i][jj] * oversample, m['orig_row'][i][jj] * oversample) # Taken from galsim/roman_psfs.py line 266. Update each psf to an object-specific psf using the wcs. scale = galsim.PixelScale(wfirst.pixel_scale / oversample) psf_ = wcs_.toWorld(scale.toImage(psf_), image_pos=galsim.PositionD(wfirst.n_pix / 2, wfirst.n_pix / 2)) # Convolve with the star model and get the psf stamp. #st_model = galsim.DeltaFunction(flux=1.) #st_model = st_model.evaluateAtWavelength(wfirst.getBandpasses(AB_zeropoint=True)['H158'].effective_wavelength) #st_model = st_model.withFlux(1.) #st_model = galsim.Convolve(st_model, psf_) psf_ = galsim.Convolve(psf_, galsim.Pixel(wfirst.pixel_scale)) psf_stamp = galsim.Image(b, wcs=wcs_) # Galaxy is being drawn with some subpixel offsets, so we apply the offsets when drawing the psf too. offset_x = m['orig_col'][i][jj] - gal_stamp_center_col offset_y = m['orig_row'][i][jj] - gal_stamp_center_row offset = galsim.PositionD(offset_x, offset_y) if (offset_x <= -1.0 or offset_y <= -1.0): print(offset) elif (offset_x >= 1.0 or offset_y >= 1.0): print(offset) psf_.drawImage(image=psf_stamp, offset=offset, method='no_pixel') m3.append(psf_stamp.array) obs_list = ObsList() psf_list = ObsList() included = [] w = [] # For each of these objects create an observation for j in range(m['ncutout'][i]): if j == 0: continue # if j>1: # continue im = m.get_cutout(i, j, type='image') weight = m.get_cutout(i, j, type='weight') im_psf = m3[j] im_psf2 = im_psf if np.sum(im) == 0.: #print(local_meds, i, j, np.sum(im)) print('no flux in image ', i, j) continue jacob = m.get_jacobian(i, j) # Get a galaxy jacobian. gal_jacob = Jacobian( row=(m['orig_row'][i][j] - m['orig_start_row'][i][j]), col=(m['orig_col'][i][j] - m['orig_start_col'][i][j]), dvdrow=jacob['dvdrow'], dvdcol=jacob['dvdcol'], dudrow=jacob['dudrow'], dudcol=jacob['dudcol']) psf_center = (32 / 2.) + 0.5 # Get a oversampled psf jacobian. if oversample == 1: psf_jacob2 = Jacobian( row=15.5 + (m['orig_row'][i][j] - m['orig_start_row'][i][j] + 1 - (m['box_size'][i] / 2. + 0.5)) * oversample, col=15.5 + (m['orig_col'][i][j] - m['orig_start_col'][i][j] + 1 - (m['box_size'][i] / 2. + 0.5)) * oversample, dvdrow=jacob['dvdrow'] / oversample, dvdcol=jacob['dvdcol'] / oversample, dudrow=jacob['dudrow'] / oversample, dudcol=jacob['dudcol'] / oversample) elif oversample == 4: psf_jacob2 = Jacobian( row=63.5 + (m['orig_row'][i][j] - m['orig_start_row'][i][j] + 1 - (m['box_size'][i] / 2. + 0.5)) * oversample, col=63.5 + (m['orig_col'][i][j] - m['orig_start_col'][i][j] + 1 - (m['box_size'][i] / 2. + 0.5)) * oversample, dvdrow=jacob['dvdrow'] / oversample, dvdcol=jacob['dvdcol'] / oversample, dudrow=jacob['dudrow'] / oversample, dudcol=jacob['dudcol'] / oversample) # Create an obs for each cutout mask = np.where(weight != 0) if 1. * len(weight[mask]) / np.product(np.shape(weight)) < 0.8: continue w.append(np.mean(weight[mask])) noise = np.ones_like(weight) / w[-1] psf_obs = Observation(im_psf, jacobian=gal_jacob, meta={ 'offset_pixels': None, 'file_id': None }) psf_obs2 = Observation(im_psf2, jacobian=psf_jacob2, meta={ 'offset_pixels': None, 'file_id': None }) #obs = Observation(im, weight=weight, jacobian=gal_jacob, psf=psf_obs, meta={'offset_pixels':None,'file_id':None}) # oversampled PSF obs = Observation(im, weight=weight, jacobian=gal_jacob, psf=psf_obs2, meta={ 'offset_pixels': None, 'file_id': None }) obs.set_noise(noise) obs_list.append(obs) psf_list.append(psf_obs2) included.append(j) return obs_list, psf_list, np.array(included) - 1, np.array(w)
def test_obslist_append_err(): obslist = ObsList() with pytest.raises(AssertionError): obslist.append(None)
def multiband_coadd(): local_Hmeds = './fiducial_H158_2285117.fits' local_Jmeds = './fiducial_J129_2285117.fits' local_Fmeds = './fiducial_F184_2285117.fits' truth = fio.FITS( '/hpc/group/cosmology/phy-lsst/my137/roman_H158/g1002/truth/fiducial_lensing_galaxia_g1002_truth_gal.fits' )[-1] m_H158 = meds.MEDS(local_Hmeds) m_J129 = meds.MEDS(local_Jmeds) m_F184 = meds.MEDS(local_Fmeds) indices_H = np.arange(len(m_H158['number'][:])) indices_J = np.arange(len(m_J129['number'][:])) indices_F = np.arange(len(m_F184['number'][:])) roman_H158_psfs = get_psf_SCA('H158') roman_J129_psfs = get_psf_SCA('J129') roman_F184_psfs = get_psf_SCA('F184') oversample = 1 metacal_keys = ['noshear', '1p', '1m', '2p', '2m'] res_noshear = np.zeros(len(m_H158['number'][:]), dtype=[('ind', int), ('ra', float), ('dec', float), ('flags', int), ('coadd_px', float), ('coadd_py', float), ('coadd_flux', float), ('coadd_snr', float), ('coadd_e1', float), ('coadd_e2', float), ('coadd_hlr', float), ('coadd_psf_e1', float), ('coadd_psf_e2', float), ('coadd_psf_T', float)]) res_1p = np.zeros(len(m_H158['number'][:]), dtype=[('ind', int), ('ra', float), ('dec', float), ('flags', int), ('coadd_px', float), ('coadd_py', float), ('coadd_flux', float), ('coadd_snr', float), ('coadd_e1', float), ('coadd_e2', float), ('coadd_hlr', float), ('coadd_psf_e1', float), ('coadd_psf_e2', float), ('coadd_psf_T', float)]) res_1m = np.zeros(len(m_H158['number'][:]), dtype=[('ind', int), ('ra', float), ('dec', float), ('flags', int), ('coadd_px', float), ('coadd_py', float), ('coadd_flux', float), ('coadd_snr', float), ('coadd_e1', float), ('coadd_e2', float), ('coadd_hlr', float), ('coadd_psf_e1', float), ('coadd_psf_e2', float), ('coadd_psf_T', float)]) res_2p = np.zeros(len(m_H158['number'][:]), dtype=[('ind', int), ('ra', float), ('dec', float), ('flags', int), ('coadd_px', float), ('coadd_py', float), ('coadd_flux', float), ('coadd_snr', float), ('coadd_e1', float), ('coadd_e2', float), ('coadd_hlr', float), ('coadd_psf_e1', float), ('coadd_psf_e2', float), ('coadd_psf_T', float)]) res_2m = np.zeros(len(m_H158['number'][:]), dtype=[('ind', int), ('ra', float), ('dec', float), ('flags', int), ('coadd_px', float), ('coadd_py', float), ('coadd_flux', float), ('coadd_snr', float), ('coadd_e1', float), ('coadd_e2', float), ('coadd_hlr', float), ('coadd_psf_e1', float), ('coadd_psf_e2', float), ('coadd_psf_T', float)]) res_tot = [res_noshear, res_1p, res_1m, res_2p, res_2m] for i, ii in enumerate( indices_H): # looping through all the objects in meds file. if i % 100 == 0: print('object number ', i) ind = m_H158['number'][ii] t = truth[ind] sca_Hlist = m_H158[ii][ 'sca'] # List of SCAs for the same object in multiple observations. sca_Jlist = m_J129[ii]['sca'] sca_Flist = m_F184[ii]['sca'] m2_H158_coadd = [ roman_H158_psfs[j - 1] for j in sca_Hlist[:m_H158['ncutout'][i]] ] m2_J129_coadd = [ roman_J129_psfs[j - 1] for j in sca_Jlist[:m_J129['ncutout'][i]] ] m2_F184_coadd = [ roman_F184_psfs[j - 1] for j in sca_Flist[:m_F184['ncutout'][i]] ] obs_Hlist, psf_Hlist, included_H, w_H = get_exp_list_coadd( m_H158, ii, m2=m2_H158_coadd) coadd_H = psc.Coadder(obs_Hlist, flat_wcs=True).coadd_obs coadd_H.psf.image[ coadd_H.psf.image < 0] = 0 # set negative pixels to zero. coadd_H.set_meta({'offset_pixels': None, 'file_id': None}) obs_Jlist, psf_Jlist, included_J, w_J = get_exp_list_coadd( m_J129, ii, m2=m2_J129_coadd) coadd_J = psc.Coadder(obs_Jlist, flat_wcs=True).coadd_obs coadd_J.psf.image[ coadd_J.psf.image < 0] = 0 # set negative pixels to zero. coadd_J.set_meta({'offset_pixels': None, 'file_id': None}) obs_Flist, psf_Flist, included_F, w_F = get_exp_list_coadd( m_F184, ii, m2=m2_F184_coadd) coadd_F = psc.Coadder(obs_Flist, flat_wcs=True).coadd_obs coadd_F.psf.image[ coadd_F.psf.image < 0] = 0 # set negative pixels to zero. coadd_F.set_meta({'offset_pixels': None, 'file_id': None}) coadd = [coadd_H, coadd_J, coadd_F] mb_obs_list = MultiBandObsList() #coadd = [coadd_H] for band in range(3): obs_list = ObsList() new_coadd_psf_block = block_reduce(coadd[band].psf.image, block_size=(4, 4), func=np.sum) new_coadd_psf_jacob = Jacobian( row=15.5, col=15.5, dvdrow=(coadd[band].psf.jacobian.dvdrow * oversample), dvdcol=(coadd[band].psf.jacobian.dvdcol * oversample), dudrow=(coadd[band].psf.jacobian.dudrow * oversample), dudcol=(coadd[band].psf.jacobian.dudcol * oversample)) coadd_psf_obs = Observation(new_coadd_psf_block, jacobian=new_coadd_psf_jacob, meta={ 'offset_pixels': None, 'file_id': None }) coadd[band].psf = coadd_psf_obs obs_list.append(coadd[band]) mb_obs_list.append(obs_list) iteration = 0 for key in metacal_keys: res_tot[iteration]['ind'][i] = ind res_tot[iteration]['ra'][i] = t['ra'] res_tot[iteration]['dec'][i] = t['dec'] iteration += 1 #print(i, t['size'], mb_obs_list[0][0].image.sum(), mb_obs_list[1][0].image.sum(), mb_obs_list[2][0].image.sum()) res_ = measure_shape_metacal_multiband( mb_obs_list, t['size'], method='bootstrap', fracdev=t['bflux'], use_e=[t['int_e1'], t['int_e2']]) iteration = 0 for key in metacal_keys: if res_ == 0: res_tot[iteration]['ind'][i] = 0 elif res_[key]['flags'] == 0: res_tot[iteration]['coadd_px'][i] = res_[key]['pars'][0] res_tot[iteration]['coadd_py'][i] = res_[key]['pars'][1] res_tot[iteration]['coadd_snr'][i] = res_[key]['s2n'] res_tot[iteration]['coadd_e1'][i] = res_[key]['pars'][2] res_tot[iteration]['coadd_e2'][i] = res_[key]['pars'][3] res_tot[iteration]['coadd_hlr'][i] = res_[key]['pars'][4] iteration += 1 mask = res_tot[0]['ind'] != 0 print(len(res_tot[0]), len(res_tot[0][mask])) #print(res_['noshear'].dtype.names) print('done')
def get_exp_list(gal, psf, offsets, sky_stamp, psf2=None): #def get_exp_list(gal, psf, sky_stamp, psf2=None): if psf2 is None: psf2 = psf obs_list = ObsList() psf_list = ObsList() w = [] for i in range(len(gal)): im = gal[i].array im_psf = psf[i].array im_psf2 = psf2[i].array weight = 1 / sky_stamp[i].array jacob = gal[i].wcs.jacobian() dx = offsets[i].x dy = offsets[i].y gal_jacob = Jacobian(row=gal[i].true_center.y + dy, col=gal[i].true_center.x + dx, dvdrow=jacob.dvdy, dvdcol=jacob.dvdx, dudrow=jacob.dudy, dudcol=jacob.dudx) #gal_jacob = Jacobian( # row=gal[i].true_center.x+dx, # col=gal[i].true_center.y+dy, # dvdrow=jacob.dudx, # dvdcol=jacob.dudy, # dudrow=jacob.dvdx, # dudcol=jacob.dvdy) psf_jacob2 = gal_jacob mask = np.where(weight != 0) w.append(np.mean(weight[mask])) noise = old_div(np.ones_like(weight), w[-1]) psf_obs = Observation(im_psf, jacobian=gal_jacob, meta={ 'offset_pixels': None, 'file_id': None }) psf_obs2 = Observation(im_psf2, jacobian=psf_jacob2, meta={ 'offset_pixels': None, 'file_id': None }) obs = Observation(im, weight=weight, jacobian=gal_jacob, psf=psf_obs, meta={ 'offset_pixels': None, 'file_id': None }) obs.set_noise(noise) obs_list.append(obs) psf_list.append(psf_obs2) #print(obs_list) return obs_list, psf_list, np.array(w)