def compute_regions(self, finalshape, box=200, corners=True): regions = [] # A square of 100x100 in the center of the image xref_cross = finalshape[1] // 2 yref_cross = finalshape[0] // 2 # self.logger.debug("Reference position is (x,y) %d %d", xref_cross + 1, yref_cross + 1) self.logger.debug("Reference regions size is %d", 2 * box + 1) region = image_box2d(xref_cross, yref_cross, finalshape, (box, box)) regions.append(region) # corners if corners: xref_c = finalshape[1] // 4 yref_c = finalshape[0] // 4 for xi in [xref_c, 3 * xref_c]: for yi in [yref_c, 3 * yref_c]: self.logger.debug("Reference position is (x,y) %d %d", xi + 1, yi + 1) self.logger.debug("Reference regions size is %d", 2 * box + 1) region = image_box2d(xi, yi, finalshape, (box, box)) regions.append(region) return regions
def test_coor(images, order, refine): arrs = images shape = arrs[0].shape xref_cross = shape[1] // 2 yref_cross = shape[0] // 2 box = 50 if refine: result = numpy.array([ [0.00000000e+00, 0.00000000e+00], [-1.99935691e+01, -1.00047803e+01], [4.98913940e+00, -1.41737914e-02], [2.93443492e-02, -3.53881751e-02] ] ) else: result = numpy.array([ [0.0, 0.0], [-20.0, -10.0], [5.0, 0.0], [0.0, 0.0] ] ) if order == 'xy': result = result[:, ::-1] region = utils.image_box2d(xref_cross, yref_cross, shape, (box, box)) computed = offsets_from_crosscor(arrs, region, refine=refine, order=order) assert numpy.allclose(result, computed)
def test_coor(images, order, refine): arrs = images shape = arrs[0].shape xref_cross = shape[1] // 2 yref_cross = shape[0] // 2 box = 50 if refine: result = numpy.array([[0.00000000e+00, 0.00000000e+00], [-1.99935691e+01, -1.00047803e+01], [4.98913940e+00, -1.41737914e-02], [2.93443492e-02, -3.53881751e-02]]) else: result = numpy.array([[0.0, 0.0], [-20.0, -10.0], [5.0, 0.0], [0.0, 0.0]]) if order == 'xy': result = result[:, ::-1] region = utils.image_box2d(xref_cross, yref_cross, shape, (box, box)) computed = offsets_from_crosscor(arrs, region, refine=refine, order=order) assert numpy.allclose(result, computed)
def compute_regions_from_objs(self, step, arr, finalshape, box=50, corners=True): regions = [] # create catalog of objects skipping a border around the image catalog, mask = self.create_object_catalog(arr, border=300) self.save_intermediate_array(mask, 'objmask_i{}.fits'.format(step)) # with the catalog, compute the brightest NKEEP objects LIMIT_AREA = 5000 NKEEP = 3 idx_small = catalog['npix'] < LIMIT_AREA objects_small = catalog[idx_small] idx_flux = objects_small['flux'].argsort() objects_nth = objects_small[idx_flux][-NKEEP:] for obj in objects_nth: self.logger.debug('ref is (x,y) = (%s, %s)', obj['x'], obj['y']) region = nautils.image_box2d(obj['x'], obj['y'], finalshape, (box, box)) regions.append(region) return regions
def test_coor_raises(images): arrs = images shape = arrs[0].shape xref_cross = shape[1] // 2 yref_cross = shape[0] // 2 box = 50 region = utils.image_box2d(xref_cross, yref_cross, shape, (box, box)) with pytest.raises(ValueError): offsets_from_crosscor(arrs, region, order="sksjd")
def compute_regions_from_objs(self, arr, finalshape, box=50, corners=True): regions = [] catalog, mask = self.create_object_catalog(arr, border=300) self.save_intermediate_array(mask, 'objmask.fits') # with the catalog, compute 5 objects LIMIT_AREA = 5000 NKEEP = 1 idx_small = catalog['npix'] < LIMIT_AREA objects_small = catalog[idx_small] idx_flux = objects_small['flux'].argsort() objects_nth = objects_small[idx_flux][-NKEEP:] for obj in objects_nth: region = image_box2d(obj['x'], obj['y'], finalshape, (box, box)) regions.append(region) return regions
def compute_regions_from_objs(self, arr, finalshape, box=50, corners=True): regions = [] catalog, mask = self.create_object_catalog(arr, border=300) self.save_intermediate_array(mask, 'objmask.fits') # with the catalog, compute 5 objects LIMIT_AREA = 5000 NKEEP = 1 idx_small = catalog['npix'] < LIMIT_AREA objects_small = catalog[idx_small] idx_flux = objects_small['flux'].argsort() objects_nth = objects_small[idx_flux][-NKEEP:] for obj in objects_nth: print('ref is', obj['x'], obj['y']) region = nautils.image_box2d(obj['x'], obj['y'], finalshape, (box, box)) print(region) regions.append(region) return regions
def moments(data, x0, y0, half_box): sl1 = image_box2d(x0, y0, data.shape, half_box) part1 = data[sl1] norm = part1.sum() Y1, X1 = np.mgrid[sl1] xo = X1 - x0 yo = Y1 - y0 Mxx = (xo**2 * part1).sum() / norm Myy = (yo**2 * part1).sum() / norm Mxy = (xo * yo * part1).sum() / norm e = math.sqrt((Mxx - Myy)**2 + (2 * Mxy)**2) / (Mxx + Myy) pa = 0.5 * math.atan(2 * Mxy / (Mxx - Myy)) return Mxx, Myy, Mxy, e, pa
def rim(data, xinit, yinit, recenter_half_box=5, recenter_nloop=10, recenter_maxdist=10.0, buff=3, width=5, niter=10, rplot=8): fine_recentering = False sigma0 = 1.0 rad = 3 * sigma0 * FWHM_G box = recenter_half_box recenter_half_box = (box, box) plot_half_box = (3*box, 3*box) print('C initial', xinit, yinit) x0, y0, _1, _2, _3 = centering_centroid(data, xinit, yinit, box=recenter_half_box, maxdist=recenter_maxdist, nloop=recenter_nloop ) print('C final', x0, y0) if fine_recentering: print('Fine recentering') print('C initial', x0, y0) x1, y1, _back, _status, _msg = centering_centroid( data, x0, y0, box=(1, 1), maxdist=2*math.sqrt(2), nloop=1 ) print('C final', x1, y1) sl = image_box2d(x0, y0, data.shape, plot_half_box) part = data[sl] xx0 = x0 - sl[1].start yy0 = y0 - sl[0].start Y, X = np.mgrid[sl] # Photometry D = np.sqrt((X-x0)**2 + (Y-y0)**2) m = D < rplot r1 = D[m] fitter = fitting.LevMarLSQFitter() model = models.Gaussian1D(amplitude=1.0, mean=0, stddev=1.0) model.mean.fixed = True # Mean is always 0.0 irad = rad for i in range(niter): rs1 = rad + buff rs2 = rs1 + width bckestim = AnnulusBackgroundEstimator(r1=rs1, r2=rs2) bck = bckestim(part, xx0, yy0) part_s = part - bck ca = CircularAperture([(xx0, yy0)], rad) m = aperture_photometry(part_s, ca) flux_aper = m['aperture_sum'][0] f1 = part_s[m] g1d_f = fitter(model, r1, f1, weights=(r1+1e-12)**-1) rpeak = g1d_f.amplitude.value # sometimes the fit is negative rsigma = abs(g1d_f.stddev.value) rfwhm = rsigma * FWHM_G dpeak, dfwhm, smsg = compute_fwhm_enclosed_direct(part_s, xx0, yy0) rad = 3 * dfwhm if abs(rad-irad) < 1e-3: # reached convergence print('convergence in iter %d' % (i+1)) break else: irad = rad else: print('no convergence in photometric radius determination') print('P, aper rad', rad, 'flux_aper', flux_aper[0]) print('P, annulus background:', bck, 'radii', rs1, rs2) eamp, efwhm, epeak, emsg = compute_fwhm_enclosed_grow( part_s, xx0, yy0, maxrad=rs1 ) print('Enclosed fit, peak:', epeak, 'fwhm', efwhm) print('Radial fit, peak:', rpeak, 'fwhm', rfwhm) print('Direct enclosed, peak:', dpeak, 'dfwhm', dfwhm) lpeak, fwhm_x, fwhm_y = compute_fwhm_1d_simple(part_s, xx0, yy0) print('Simple, peak:', lpeak, 'fwhm x', fwhm_x, 'fwhm y', fwhm_y) # Fit in a smaller box fit2d_rad = int(math.ceil(0.5 * rad)) fit2d_half_box = (fit2d_rad, fit2d_rad) sl1 = image_box2d(x0, y0, data.shape, fit2d_half_box) part1 = data[sl1] Y1, X1 = np.mgrid[sl1] g2d = models.Gaussian2D(amplitude=rpeak, x_mean=x0, y_mean=y0, x_stddev=1.0, y_stddev=1.0) g2d_f = fitter(g2d, X1, Y1, part1 - bck) print('Gauss2D fit') print(g2d_f) moments_half_box = fit2d_half_box Mxx, Myy, Mxy, e, pa = moments(data, x0, y0, moments_half_box) print(Mxx, Myy, Mxy, e, pa)