def test_jacobian_missing_kwargs(kind): dudcol = 0.25 dudrow = 0.1 dvdcol = -0.4 dvdrow = 0.34 col = 5.6 row = -10.4 if kind == 'row-col': kwargs = dict(col=col, row=row, dudcol=dudcol, dudrow=dudrow, dvdcol=dvdcol, dvdrow=dvdrow) elif kind == 'x-y': kwargs = dict(x=col, y=row, dudx=dudcol, dudy=dudrow, dvdx=dvdcol, dvdy=dvdrow) for key in kwargs: tst_kwargs = {} tst_kwargs.update(kwargs) del tst_kwargs[key] with pytest.raises(ValueError): Jacobian(**tst_kwargs)
def test_pixels_smoke(x, y, wcs_g1, wcs_g2, ignore_zero_weight): gs_wcs = galsim.ShearWCS(0.25, galsim.Shear(g1=wcs_g1, g2=wcs_g2)).jacobian() jac = Jacobian(y=y, x=x, dudx=gs_wcs.dudx, dudy=gs_wcs.dudy, dvdx=gs_wcs.dvdx, dvdy=gs_wcs.dvdy) dims = (13, 15) rng = np.random.RandomState(seed=11) image = rng.normal(size=dims) weight = np.exp(rng.normal(size=dims)) weight[10, 9] = 0 weight[8, 7] = 0 pixels = make_pixels(image, weight, jac, ignore_zero_weight=ignore_zero_weight) assert np.allclose(pixels['area'], jac.area) found_zero = 0 for i in range(len(pixels)): y, x = jac.get_rowcol(pixels['v'][i], pixels['u'][i]) assert np.allclose(x, int(x + 0.5)) assert np.allclose(y, int(y + 0.5)) x = int(x + 0.5) y = int(y + 0.5) assert pixels['val'][i] == image[y, x] assert np.allclose(pixels['ierr'][i], np.sqrt(weight[y, x])) if x == 9 and y == 10: found_zero += 1 if y == 8 and x == 7: found_zero += 1 if ignore_zero_weight: assert found_zero == 0 else: assert found_zero == 2
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_unit_jacobian_missing_kwargs(kind): col = 5.6 row = -10.4 if kind == 'row-col': kwargs = dict(col=col, row=row) elif kind == 'x-y': kwargs = dict(x=col, y=row) for key in kwargs: tst_kwargs = {} tst_kwargs.update(kwargs) del tst_kwargs[key] with pytest.raises(Exception): Jacobian(**tst_kwargs)
def get_jacobian(self, type='galaxy'): """ get the jacobian and return a Jacobian object """ jdict = self.meds.get_jacobian(self.mindex, 0) row0 = jdict['row0'] col0 = jdict['col0'] if self['fix_centroid_bug'] and type == 'galaxy': print("fixing centroid bug") row0 = row0 - 1 col0 = col0 - 1 jacob = Jacobian(row0, col0, jdict['dudrow'], jdict['dudcol'], jdict['dvdrow'], jdict['dvdcol']) return jacob
def test_coords_smoke(x, y, wcs_g1, wcs_g2): gs_wcs = galsim.ShearWCS(0.25, galsim.Shear(g1=wcs_g1, g2=wcs_g2)).jacobian() jac = Jacobian(y=y, x=x, dudx=gs_wcs.dudx, dudy=gs_wcs.dudy, dvdx=gs_wcs.dvdx, dvdy=gs_wcs.dvdy) dims = (13, 15) coords = make_coords(dims, jac) loc = 0 for y in range(dims[0]): for x in range(dims[1]): v, u = jac(y, x) assert u == coords['u'][loc] assert v == coords['v'][loc] loc += 1
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 get_ngmix_jacobian(self, iobj, icutout): """Get an ngmix.Jacobian representation. Parameters ---------- iobj : int Index of the object. icutout : int Index of the cutout for this object. Returns ------- jacob : ngmix.Jacobian The `Jacobian` for the cutout. """ jd = self.get_jacobian(iobj, icutout) return Jacobian(row=jd['row0'], col=jd['col0'], dudrow=jd['dudrow'], dudcol=jd['dudcol'], dvdrow=jd['dvdrow'], dvdcol=jd['dvdcol'])
def test_jacobian_smoke(kind): dudcol = 0.25 dudrow = 0.1 dvdcol = -0.4 dvdrow = 0.34 col = 5.6 row = -10.4 if kind == 'row-col': jac = Jacobian(col=col, row=row, dudcol=dudcol, dudrow=dudrow, dvdcol=dvdcol, dvdrow=dvdrow) elif kind == 'x-y': jac = Jacobian(x=col, y=row, dudx=dudcol, dudy=dudrow, dvdx=dvdcol, dvdy=dvdrow) else: wcs = galsim.JacobianWCS(dudx=dudcol, dudy=dudrow, dvdx=dvdcol, dvdy=dvdrow) jac = Jacobian(x=col, y=row, wcs=wcs) assert np.allclose(jac.row0, row) assert np.allclose(jac.col0, col) assert np.allclose(jac.dudcol, dudcol) assert np.allclose(jac.dudrow, dudrow) assert np.allclose(jac.dvdcol, dvdcol) assert np.allclose(jac.dvdrow, dvdrow) assert np.allclose(jac.det, dudcol * dvdrow - dudrow * dvdcol) assert np.allclose(jac.sdet, np.sqrt(np.abs(dudcol * dvdrow - dudrow * dvdcol))) assert np.allclose(jac.scale, np.sqrt(np.abs(dudcol * dvdrow - dudrow * dvdcol))) r, c = 20.0, -44.5 v, u = jac.get_vu(r, c) assert np.allclose(v, dvdrow * (r - row) + dvdcol * (c - col)) assert np.allclose(u, dudrow * (r - row) + dudcol * (c - col)) v, u = jac(r, c) assert np.allclose(v, dvdrow * (r - row) + dvdcol * (c - col)) assert np.allclose(u, dudrow * (r - row) + dudcol * (c - col)) v, u = 20.0, -44.5 r, c = jac.get_rowcol(v, u) assert np.allclose(r, (dudcol * v - dvdcol * u) / jac.det + row) assert np.allclose(c, (-dudrow * v + dvdrow * u) / jac.det + col) gs_wcs = jac.get_galsim_wcs() assert np.allclose(gs_wcs.dudx, jac.dudcol) assert np.allclose(gs_wcs.dudy, jac.dudrow) assert np.allclose(gs_wcs.dvdx, jac.dvdcol) assert np.allclose(gs_wcs.dvdy, jac.dvdrow) cpy_jac = jac.copy() cpy_jac.set_cen(row=-11, col=-12) assert np.allclose(jac.row0, row) assert np.allclose(jac.col0, col) assert np.allclose(cpy_jac.row0, -11) assert np.allclose(cpy_jac.col0, -12)
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_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 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_nbrs_data(self,cen_id,band,cutout_index): """ return cen and nbrs info for a given cen_id Parameters ---------- cen_id: id of central object band: integer for band (e.g., 0 for griz MOF) cutout_index: index of epoch in MEDS file for nbrs Returns ------- cen_ind: index of central intp returned fit_data cen_flags: flags for central from nbrs_data cen_psf_gmix: an ngmix GMix for PSF of central cen_jac: an ngmix Jacobian for the central nbrs_inds: list of indexes into returned fit_data of nbrs nbrs_flags: flags of nbrs (only use flags == 0) nbrs_psf_gmixes: a list of ngmix GMixes of the PSF models for the nbrs nbrs_jacs: a list of ngmix Jacobians for the nbrs fit_data: entries of input fit_data corresponding to cen_ind and nbrs_inds pixel_scale: the pixel_scale of the jacobian associated with the image will return None if thete is an error or if not all quantities are found """ fit_inds = [] # get cen stuff q, = numpy.where(self.fit_data['id'] == cen_id) if len(q) != 1: return None cen_ind = 0 fit_inds.append(q[0]) q, = numpy.where( (self.nbrs_data['id'] == cen_id) & (self.nbrs_data['nbr_id'] == cen_id) & (self.nbrs_data['band_num'] == band) & (self.nbrs_data['cutout_index'] == cutout_index) ) if len(q) != 1: return None ind = q[0] cen_flags = self.nbrs_data['nbr_flags'][ind] if cen_flags == 0: cen_jac = Jacobian( row=self.nbrs_data['nbr_jac_row0'][ind], col=self.nbrs_data['nbr_jac_col0'][ind], dudrow=self.nbrs_data['nbr_jac_dudrow'][ind], dudcol=self.nbrs_data['nbr_jac_dudcol'][ind], dvdrow=self.nbrs_data['nbr_jac_dvdrow'][ind], dvdcol=self.nbrs_data['nbr_jac_dvdcol'][ind] ) cen_psf_gmix = GMix(pars=self.nbrs_data['nbr_psf_fit_pars'][ind,:]) else: cen_psf_gmix = None cen_jac = None pixel_scale = self.nbrs_data['pixel_scale'][ind] # get nbr ids q, = numpy.where( (self.nbrs_data['id'] == cen_id) & (self.nbrs_data['nbr_id'] != cen_id) & (self.nbrs_data['band_num'] == band) & (self.nbrs_data['cutout_index'] == cutout_index) ) if len(q) == 0: return None n1 = len(numpy.unique(self.nbrs_data['nbr_id'][q])) n2 = len(self.nbrs_data['nbr_id'][q]) mess=("nbrs list for given band %d and cutout_index " "%d for object %d is not unique!" % (band,cutout_index,cen_id)) assert n1==n2, mess nbrs_inds = [i+1 for i in xrange(len(q))] # find location of nbrs in fit_data for nbr_id in self.nbrs_data['nbr_id'][q]: qq, = numpy.where(self.fit_data['id'] == nbr_id) if len(qq) != 1: return None fit_inds.append(qq[0]) # doing fit data fit_data = self.fit_data[fit_inds] # build flag, psf and jacobian lists nbrs_flags = [] nbrs_psf_gmixes = [] nbrs_jacs = [] for i,ind in enumerate(q): check=fit_data['id'][i+1] == self.nbrs_data['nbr_id'][ind] assert check,"Matching of nbr_ids to ids in fit_data did not work!" nbrs_flags.append(self.nbrs_data['nbr_flags'][ind]) if nbrs_flags[-1] == 0: nbrs_jacs.append( Jacobian( row=self.nbrs_data['nbr_jac_row0'][ind], col=self.nbrs_data['nbr_jac_col0'][ind], dudrow=self.nbrs_data['nbr_jac_dudrow'][ind], dudcol=self.nbrs_data['nbr_jac_dudcol'][ind], dvdrow=self.nbrs_data['nbr_jac_dvdrow'][ind], dvdcol=self.nbrs_data['nbr_jac_dvdcol'][ind] ) ) tgm = GMix(pars=self.nbrs_data['nbr_psf_fit_pars'][ind,:]) nbrs_psf_gmixes.append(tgm) else: nbrs_jacs.append(None) nbrs_psf_gmixes.append(None) return cen_ind, cen_flags, cen_psf_gmix, cen_jac, \ nbrs_inds, nbrs_flags, nbrs_psf_gmixes, nbrs_jacs, \ fit_data, pixel_scale
def render_central(self, cen_id, meds_data, mindex, cutout_index, model, band, image_shape): """ just render the central. This is needed since Matt does not store data for the central i the nbrs data structure if there are no neighbors you must send epoch_data= to the constructor """ import esutil as eu # # set and check some parameters # if self.epoch_data is None: raise RuntimeError( "send epoch_data= to the constructor to render centrals" ) pars_tag = '%s_max_pars' % model if model == 'cm': fracdev_tag = 'cm_fracdev' TdByTe_tag = 'cm_TdByTe' else: fracdev_tag=None TdByTe_tag=None # # get data needed to render the central # we,=numpy.where( (self.epoch_data['id'] == cen_id) & (self.epoch_data['cutout_index'] == cutout_index) ) if we.size == 0: print(" central not in epochs data") return None psf_pars=self.epoch_data['psf_fit_pars'][we[0],:] psf_gmix = GMix(pars=psf_pars) e1,e2,T=psf_gmix.get_e1e2T() if T <= 0.0: print(" bad psf fit for central") return None pixel_scale=self.epoch_data['pixel_scale'][we[0]] jdict=meds_data.get_jacobian(mindex, cutout_index) jac=Jacobian( row=jdict['row0'], col=jdict['col0'], dudrow=jdict['dudrow'], dudcol=jdict['dudcol'], dvdrow=jdict['dvdrow'], dvdcol=jdict['dvdcol'], ) # fit information for central wfit, = numpy.where(self.fit_data['id'] == cen_id) if wfit.size != 1: # not sure why this would happen print(" Central not found in fit_data") return None fit_data = self.fit_data[wfit] cen_flags=fit_data['flags'][0] if cen_flags != 0: print(" bad central fit:",cen_flags) return None cen_img = RenderNGmixNbrs._render_single( model, band, image_shape, pars_tag, fit_data, psf_gmix, jac, fracdev_tag=fracdev_tag,TdByTe_tag=TdByTe_tag, ) if cen_img is None: return None return cen_img, pixel_scale
def _get_nbrs_data(self, cen_id, band, cutout_index): """ return cen and nbrs info for a given cen_id Parameters ---------- cen_id: id of central object band: integer for band (e.g., 0 for griz MOF) cutout_index: index of epoch in MEDS file for nbrs Returns ------- cen_ind: index of central intp returned fit_data cen_flags: flags for central from nbrs_data cen_psf_gmix: an ngmix GMix for PSF of central cen_jac: an ngmix Jacobian for the central nbrs_inds: list of indexes into returned fit_data of nbrs nbrs_flags: flags of nbrs (only use flags == 0) nbrs_psf_gmixes: a list of ngmix GMixes of the PSF models for the nbrs nbrs_jacs: a list of ngmix Jacobians for the nbrs fit_data: entries of input fit_data corresponding to cen_ind and nbrs_inds pixel_scale: the pixel_scale of the jacobian associated with the image will return None if thete is an error or if not all quantities are found """ fit_inds = [] # get cen stuff qids, = numpy.where(self.fit_ids == cen_id) if len(qids) != 1: print(" no cen match in fits") return None cen_ind = 0 fit_inds.append(qids[0]) qnbrs_ids, = numpy.where(self.nbrs_ids == cen_id) if len(qnbrs_ids) == 0: return None # extract just the data we need nbrs_data = self._fits['nbrs_data'][qnbrs_ids] qcen, = numpy.where((nbrs_data['nbr_id'] == cen_id) & (nbrs_data['band_num'] == band) & (nbrs_data['cutout_index'] == cutout_index)) if len(qcen) != 1: return None ind = qcen[0] cen_flags = nbrs_data['nbr_flags'][ind] if cen_flags == 0: cen_jac = Jacobian(row=nbrs_data['nbr_jac_row0'][ind], col=nbrs_data['nbr_jac_col0'][ind], dudrow=nbrs_data['nbr_jac_dudrow'][ind], dudcol=nbrs_data['nbr_jac_dudcol'][ind], dvdrow=nbrs_data['nbr_jac_dvdrow'][ind], dvdcol=nbrs_data['nbr_jac_dvdcol'][ind]) cen_psf_gmix = GMix(pars=nbrs_data['nbr_psf_fit_pars'][ind, :]) else: cen_psf_gmix = None cen_jac = None pixel_scale = nbrs_data['pixel_scale'][ind] # get nbr ids qnbr, = numpy.where((nbrs_data['nbr_id'] != cen_id) & (nbrs_data['band_num'] == band) & (nbrs_data['cutout_index'] == cutout_index)) if len(qnbr) == 0: return None n1 = len(numpy.unique(nbrs_data['nbr_id'][qnbr])) n2 = len(nbrs_data['nbr_id'][qnbr]) if n1 != n2: # not sure why this happens, but it seems to be very rare. # For now ignore mess = ("nbrs list for given band %d and cutout_index " "%d for object %d is not unique! %d:%d" % (band, cutout_index, cen_id, n1, n2)) print(nbrs_data['nbr_id'][qnbr]) print(mess) return None #raise RuntimeError(mess) nbrs_inds = [i + 1 for i in xrange(len(qnbr))] # find location of nbrs in fit_data for nbr_id in nbrs_data['nbr_id'][qnbr]: qq, = numpy.where(self.fit_ids == nbr_id) if len(qq) != 1: print(" nbr", nbr_id, "not found in fit data") return None fit_inds.append(qq[0]) # doing fit data # the data come out in row order, we need to reorder fit_data_list = [] for tmp_ind in fit_inds: fit_data_list.append(self._fits['model_fits'][tmp_ind]) fit_data = eu.numpy_util.combine_arrlist(fit_data_list) # build flag, psf and jacobian lists nbrs_flags = [] nbrs_psf_gmixes = [] nbrs_jacs = [] for i, ind in enumerate(qnbr): check = fit_data['id'][i + 1] == nbrs_data['nbr_id'][ind] assert check, "Matching of nbr_ids to ids in fit_data did not work!" nbrs_flags.append(nbrs_data['nbr_flags'][ind]) if nbrs_flags[-1] == 0: nbrs_jacs.append( Jacobian(row=nbrs_data['nbr_jac_row0'][ind], col=nbrs_data['nbr_jac_col0'][ind], dudrow=nbrs_data['nbr_jac_dudrow'][ind], dudcol=nbrs_data['nbr_jac_dudcol'][ind], dvdrow=nbrs_data['nbr_jac_dvdrow'][ind], dvdcol=nbrs_data['nbr_jac_dvdcol'][ind])) tgm = GMix(pars=nbrs_data['nbr_psf_fit_pars'][ind, :]) nbrs_psf_gmixes.append(tgm) else: nbrs_jacs.append(None) nbrs_psf_gmixes.append(None) return cen_ind, cen_flags, cen_psf_gmix, cen_jac, \ nbrs_inds, nbrs_flags, nbrs_psf_gmixes, nbrs_jacs, \ fit_data, pixel_scale
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)