def test_shifts(xsh,ysh,imsize,gaussfit): image,new_image,tolerance = make_offset_images(xsh, ysh, imsize) if gaussfit: xoff,yoff,exoff,eyoff = cross_correlation_shifts(image,new_image,return_error=True) print xoff,yoff,np.abs(xoff-xsh),np.abs(yoff-ysh),exoff,eyoff else: xoff,yoff = chi2_shift(image,new_image,return_error=False) print xoff,yoff,np.abs(xoff-xsh),np.abs(yoff-ysh) assert np.abs(xoff-xsh) < tolerance assert np.abs(yoff-ysh) < tolerance
def compare_methods(im1,im2, ntests=100, noise=np.std, usfac=201, **kwargs): """ Perform tests with noise added to determine the errors on the 'best-fit' offset Parameters ---------- usfac : int upsampling factor; governs accuracy of fit (1/usfac is best accuracy) ntests : int Number of tests to run noise : func or real Either a function to apply to im2 to determine the noise to use, or a fixed noise level """ try: noise = noise(im2) except TypeError: pass try: import progressbar widgets = [progressbar.FormatLabel('Processed: %(value)d offsets in %(elapsed)s'), progressbar.Percentage()] progress = progressbar.ProgressBar(widgets=widgets) except ImportError: def progress(x): yield x offsets = [] eoffsets = [] for test_number in progress(xrange(ntests)): extra_noise = np.random.randn(*im2.shape) * noise dxr, dyr, edxr, edyr = register_images(im1, im2+extra_noise, usfac=usfac, return_error=True, **kwargs) dxccs, dyccs, edxccs, edyccs = cross_correlation_shifts(im1, im2+extra_noise, errim2=im2*0+extra_noise.std(), return_error=True, **kwargs) # too slow!!! dxccg, dyccg, edxccg, edyccg = 0,0,0,0 #dxccg, dyccg, edxccg, edyccg = cross_correlation_shifts(im1, # im2+extra_noise, return_error=True, gaussfit=True, # **kwargs) dxchi, dychi, edxchi, edychi = chi2_shift(im1, im2+extra_noise, err=im2*0+noise, return_error=True, upsample_factor='auto', verbose=False, **kwargs) offsets.append([dxr,dyr,dxccs,dyccs,dxccg,dyccg,dxchi,dychi]) eoffsets.append([edxr,edyr,edxccs,edyccs,edxccg,edyccg,edxchi,edychi]) return np.array(offsets),np.array(eoffsets)
def run_tests(xsh, ysh, imsize, amp, gaussfit, nfits=1000, maxoff=20): fitted_shifts = np.array(do_n_fits(nfits, xsh, ysh, imsize, amp=amp, maxoff=maxoff)) errors = fitted_shifts.std(axis=0) x,y,ex,ey = cross_correlation_shifts( *make_offset_images(xsh, ysh, imsize, amp=amp)[:2], gaussfit=gaussfit, maxoff=maxoff, return_error=True, errim1=np.ones([imsize,imsize]), errim2=np.ones([imsize,imsize])) print "StdDev: %10.3g,%10.3g Measured: %10.3g,%10.3g "+\ " Difference: %10.3g, %10.3g Diff/Real: %10.3g,%10.3g" % ( errors[0],errors[1], ex,ey,errors[0]-ex,errors[1]-ey, (errors[0]-ex)/errors[0], (errors[1]-ey)/errors[1]) return errors[0],errors[1],ex,ey