def residuals(p, im1, im2): xsh, ysh = p['xsh'].value,p['ysh'].value if use_fft: shifted_img = shift.shiftnd(im2, (-ysh, -xsh)) else: # identical to skimage shifted_img = scipy.ndimage.map_coordinates(im2, [yy+ysh,xx+xsh], mode=mode) if maxoff is not None: xslice = slice(xcen-maxoff,xcen+maxoff,None) yslice = slice(ycen-maxoff,ycen+maxoff,None) # divide by sqrt(number of samples) = sqrt(maxoff**2) residuals = np.abs(np.ravel((im1[yslice,xslice]-shifted_img[yslice,xslice])) / maxoff) else: if ignore_outside: outsidex = min([(xlen-2*xsh)/2,xcen]) outsidey = min([(ylen-2*ysh)/2,xcen]) xslice = slice(xcen-outsidex,xcen+outsidex,None) yslice = slice(ycen-outsidey,ycen+outsidey,None) residuals = ( np.abs( np.ravel( (im1[yslice,xslice]-shifted_img[yslice,xslice]))) / (2*outsidex*2*outsidey)**0.5 ) else: xslice = slice(None) yslice = slice(None) residuals = np.abs(np.ravel((im1-shifted_img))) / im1.size**0.5 if err is None: return residuals elif hasattr(err,'shape'): if use_fft: shifted_err = shift.shiftnd(err, (-ysh, -xsh)) else: shifted_err = scipy.ndimage.map_coordinates(err, [yy+ysh,xx+xsh], mode=mode) return residuals / shifted_err[yslice,xslice].flat else: return residuals / err
def test_shifts_1d(imsize,dx): x = np.arange(imsize) g = gaussian(x - (imsize-1)/2. ) sg = shift.shiftnd(g,(dx,)) assert np.all(np.abs(sg-gaussian(x-(imsize-1.)/2.-dx)) < 0.05)
def register(ref, toshift, method, outdir=None, center=None, size=None, overwrite=False): '''Register and shift images''' hdu_ref = fits.open(ref) hdu_shift = fits.open(toshift) if center is None: # use center of image center = [int(x / 2.) for x in hdu_ref[0].shape] # resize if size is not None: refdat = Cutout2D(hdu_ref[0].data, position=center, size=size).data shiftdat = Cutout2D(hdu_shift[0].data, position=center, size=size).data else: refdat = hdu_ref[0].data shiftdat = hdu_shift[0].data if method == 'point': xoff, yoff = point_align(refdat, shiftdat) elif method == 'extended': xoff, yoff = extended_align(refdat, shiftdat) else: raise (NotImplementedError("method %s unknown" % method)) # shift hdu_shift[0].data = shift.shiftnd(hdu_shift[0].data, (-yoff, -xoff)) hdu_shift[0].header.add_history( '%s - %s' % (os.path.basename(__file__), Time(Time.now(), format='fits'))) hdu_shift[0].header.add_history( '%s - aligned to %s' % (os.path.basename(__file__), os.path.basename(ref))) hdu_shift[0].header.add_history('%s - shift_x: %.3f, shift_y: %.3f' % (os.path.basename(__file__), -xoff, -yoff)) hdu_shift[0].header['ALIGNED'] = (True, 'Image aligned to reference') hdu_shift[0].header['REFALGND'] = (os.path.basename(ref), 'Reference file for alignment') hdu_shift[0].header['SXOFF'] = (-xoff, 'X shift') hdu_shift[0].header['SYOFF'] = (-yoff, 'Y shift') if outdir: #write file and return filename outfile = os.path.join(outdir, os.path.basename(toshift)) with warnings.catch_warnings(): warnings.simplefilter('ignore', VerifyWarning) hdu_shift.writeto(outfile, overwrite=overwrite, output_verify='silentfix') return outfile else: # if outdir is None, return hdu return hdu_shift
def make_offset_extended(img, xsh, ysh, noise=1.0, mode='wrap', noise_taper=False): noise = np.random.randn(*img.shape)*noise if noise_taper: noise /= edge_weight(img.shape[0]) #newimage = scipy.ndimage.map_coordinates(img+noise, [yy,xx], mode=mode) newimage = np.real(shift.shiftnd(img, (ysh, xsh))+noise) return newimage
def chi2(im1, im2, dx, dy, err=None, upsample=1): """ Compute chi^2 between two images after shifting """ im1 = np.nan_to_num(im1) im2 = np.nan_to_num(im2) if err is None: err = im2*0 + 1 if upsample > 1: im1 = upsample_image(im1, upsample_factor=upsample, output_size=im1.shape, ) im2s = upsample_image(im2, upsample_factor=upsample, output_size=im2.shape, xshift=-dx*upsample, yshift=-dy*upsample) err = upsample_image(err, upsample_factor=upsample, output_size=im2.shape, xshift=-dx*upsample, yshift=-dy*upsample) #im2s = np.abs(shift(im2, -dx*upsample, -dy*upsample)) else: im2s = np.abs(shift.shiftnd(im2, (-dy,-dx))) err = np.abs(shift.shiftnd(err, (-dy,-dx))) return ((im1-im2s)**2/err**2).sum()
def test_shifts_2d(imsize,dx,dy): inds = np.indices([imsize]*2) rr = ((inds[0] - (imsize-1)/2.)**2 + (inds[1] - (imsize-1)/2.)**2)**0.5 gg = gaussian(rr) sg = shift.shiftnd(gg,(dy,dx)) rr2 = ((inds[0] - (imsize-1)/2. - dy)**2 + (inds[1] - (imsize-1)/2. - dx)**2)**0.5 assert np.all(np.abs(sg-gaussian(rr2) < 0.05))
def residuals(p, im1, im2): xsh, ysh = p['xsh'].value, p['ysh'].value if use_fft: shifted_img = shift.shiftnd(im2, (-ysh, -xsh)) else: # identical to skimage shifted_img = scipy.ndimage.map_coordinates(im2, [yy + ysh, xx + xsh], mode=mode) if maxoff is not None: xslice = slice(xcen - maxoff, xcen + maxoff, None) yslice = slice(ycen - maxoff, ycen + maxoff, None) # divide by sqrt(number of samples) = sqrt(maxoff**2) residuals = np.abs( np.ravel((im1[yslice, xslice] - shifted_img[yslice, xslice])) / maxoff) else: if ignore_outside: outsidex = min([(xlen - 2 * xsh) / 2, xcen]) outsidey = min([(ylen - 2 * ysh) / 2, xcen]) xslice = slice(xcen - outsidex, xcen + outsidex, None) yslice = slice(ycen - outsidey, ycen + outsidey, None) residuals = (np.abs( np.ravel( (im1[yslice, xslice] - shifted_img[yslice, xslice]))) / (2 * outsidex * 2 * outsidey)**0.5) else: xslice = slice(None) yslice = slice(None) residuals = np.abs(np.ravel( (im1 - shifted_img))) / im1.size**0.5 if err is None: return residuals elif hasattr(err, 'shape'): if use_fft: shifted_err = shift.shiftnd(err, (-ysh, -xsh)) else: shifted_err = scipy.ndimage.map_coordinates( err, [yy + ysh, xx + xsh], mode=mode) return residuals / shifted_err[yslice, xslice].flat else: return residuals / err
def test_shifts_2d_asymm(imsize,dx,dy): inds = np.indices([imsize,imsize*2]) rr = ((inds[0] - (imsize-1)/2.)**2 + (inds[1] - (imsize*2-1)/2.)**2)**0.5 gg = gaussian(rr) sg = shift.shiftnd(gg,(dy,dx)) rr2 = ((inds[0] - (imsize-1)/2. - dy)**2 + (inds[1] - (imsize*2-1)/2. - dx)**2)**0.5 thr = gaussian(rr2) assert np.all(np.abs(sg-thr < 0.05))
def register(ref,toshift,method,outdir=None,center=None,size=None,overwrite=False): '''Register and shift images''' hdu_ref = fits.open(ref) hdu_shift = fits.open(toshift) if center is None: # use center of image center = [int(x/2.) for x in hdu_ref[0].shape] # resize if size is not None: refdat = Cutout2D(hdu_ref[0].data,position=center,size=size).data shiftdat = Cutout2D(hdu_shift[0].data,position=center,size=size).data else: refdat = hdu_ref[0].data shiftdat = hdu_shift[0].data if method == 'point': xoff,yoff = point_align(refdat,shiftdat) elif method == 'extended': xoff,yoff = extended_align(refdat,shiftdat) else: raise(NotImplementedError("method %s unknown"%method)) # shift hdu_shift[0].data = shift.shiftnd(hdu_shift[0].data,(-yoff,-xoff)) hdu_shift[0].header.add_history('%s - %s' % (os.path.basename(__file__),Time(Time.now(),format='fits'))) hdu_shift[0].header.add_history('%s - aligned to %s' % (os.path.basename(__file__),os.path.basename(ref))) hdu_shift[0].header.add_history('%s - shift_x: %.3f, shift_y: %.3f' % (os.path.basename(__file__),-xoff,-yoff)) hdu_shift[0].header['ALIGNED'] = (True,'Image aligned to reference') hdu_shift[0].header['REFALGND'] = (os.path.basename(ref),'Reference file for alignment') hdu_shift[0].header['SXOFF'] = (-xoff,'X shift') hdu_shift[0].header['SYOFF'] = (-yoff,'Y shift') if outdir: #write file and return filename outfile = os.path.join(outdir,os.path.basename(toshift)) with warnings.catch_warnings(): warnings.simplefilter('ignore',VerifyWarning) hdu_shift.writeto(outfile,overwrite=overwrite,output_verify='silentfix') return outfile else: # if outdir is None, return hdu return hdu_shift
def chi2(location): x = 0 template = glob.glob(location[:-4] + '/templates/*.fits') images = glob.glob(location + '/*_a_.fits') if len(template) == 1: ref_data = fits.getdata(template[0]) ref_data = np.array(ref_data, dtype='float64') print("\n-> Aligning images with chi2...") for i in images: data = fits.getdata(i) data = np.array(data, dtype='float64') dx, dy, edx, edy = chi2_shift(ref_data, data, upsample_factor='auto') corrected_image = shift.shiftnd(data, (-dx, -dy)) hdu = fits.PrimaryHDU(corrected_image) hdu.writeto(i[:-8] + '_A_.fits') os.remove(i) x += 1 print("-> %.1f%% aligned..." % (float(x) / float(len(images)) * 100)) else: print("-> Alignment failed: Template missing")
chi2shift = image_registration.chi2_shift(proj_7mmto3mm, cutout_3mm.data, err=errest, upsample_factor=1000) print(chi2shift) print(chi2shift[:2] * cutout_3mm.wcs.wcs.cdelt * 3600) print(chi2shift[:2] * cutout_3mm.wcs.wcs.cdelt) print((((chi2shift[:2] * cutout_3mm.wcs.wcs.cdelt * 3600)**2).sum())**0.5) """ [-4.295500000000004, 3.9625000000000057, 0.0034999999999999892, 0.003500000000000003] [0.0214775 0.0198125] [5.96597222e-06 5.50347222e-06] """ ichi2shift = image_registration.chi2_shift_iterzoom(proj_7mmto3mm, cutout_3mm.data, err=errest, upsample_factor=1000) print(ichi2shift) from image_registration.fft_tools import shift xoff, yoff = chi2shift[:2] corrected_7mm = shift.shiftnd(proj_7mmto3mm, (yoff, xoff)) import pylab as pl pl.figure(1).clf() pl.subplot(2, 2, 1).imshow(proj_7mmto3mm, origin='lower') pl.subplot(2, 2, 2).imshow(cutout_3mm.data, origin='lower') pl.subplot(2, 2, 3).imshow(proj_7mmto3mm * 3 - cutout_3mm.data, origin='lower') pl.subplot(2, 2, 4).imshow(corrected_7mm * 3 - cutout_3mm.data, origin='lower')
""" import glob from astropy.io import fits from image_registration import chi2_shift from image_registration.fft_tools import shift import numpy as np image = '/home/andrew/sdi/targets/NGC6744/19:09:46.104_-63:51:27.00/rp/20.0/data/NORM_17:53:30.954_ref_A_.fits' data = fits.getdata(image) data = np.array(data, dtype='float64') #image = '/home/andrew/sdi/targets/NGC6744/19:09:46.104_-63:51:27.00/rp/20.0/data/17:34:05.966_N_.fits' #data = fits.getdata(image) #Min = np.min(data) #Max = np.max(data) #new_min = 1 #new_max = -1 #data = (data-Min)*((new_max-new_min)/(Max-Min)) #data += new_min #data -= np.mean(data) #hdu = fits.PrimaryHDU(data) #hdu.writeto('/home/andrew/sdi/targets/NGC6744/19:09:46.104_-63:51:27.00/rp/20.0/data/NORM3_17:34:05.966_N_.fits') image2 = '/home/andrew/sdi/targets/NGC6744/19:09:46.104_-63:51:27.00/rp/20.0/data/NORM3_17:34:05.966_N_.fits' data2 = fits.getdata(image2) data2 = np.array(data2, dtype='float64') dx, dy, edx, edy = chi2_shift(data, data2, upsample_factor='auto') corrected_image = shift.shiftnd(data2, (-dy, -dx)) hdu2 = fits.PrimaryHDU(corrected_image) hdu2.writeto( '/home/andrew/sdi/targets/NGC6744/19:09:46.104_-63:51:27.00/rp/20.0/data/NORM5_align.fits' )