def test_wrong_input(): # Dimensionality mismatch image = np.ones((5, 5, 1)) template = np.ones((5, 5)) with testing.raises(ValueError): register_translation(template, image) # Size mismatch image = np.ones((5, 5)) template = np.ones((4, 4)) with testing.raises(ValueError): register_translation(template, image)
def test_4d_input_pixel(): phantom = img_as_float(binary_blobs(length=32, n_dim=4)) reference_image = np.fft.fftn(phantom) shift = (-2., 1., 5., -3) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.05)
def test_4d_input_pixel(): phantom = img_as_float(binary_blobs(length=32, n_dim=4)) reference_image = fft.fftn(phantom) shift = (-2., 1., 5., -3) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.05)
def test_subpixel_precision(): reference_image = np.fft.fftn(camera()) subpixel_shift = (-2.4, 1.32) shifted_image = fourier_shift(reference_image, subpixel_shift) # subpixel precision result, error, diffphase = register_translation(reference_image, shifted_image, 100, space="fourier") assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
def test_correlation(): reference_image = np.fft.fftn(camera()) shift = (-7, 12) shifted_image = fourier_shift(reference_image, shift) # pixel precision result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result[:2], -np.array(shift))
def test_real_input(): reference_image = camera() subpixel_shift = (-2.4, 1.32) shifted_image = fourier_shift(np.fft.fftn(reference_image), subpixel_shift) shifted_image = np.fft.ifftn(shifted_image) # subpixel precision result, error, diffphase = register_translation(reference_image, shifted_image, 100) assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
def test_3d_input(): phantom = img_as_float(binary_blobs(length=32, n_dim=3)) reference_image = fft.fftn(phantom) shift = (-2., 1., 5.) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.05) # subpixel precision now available for 3-D data subpixel_shift = (-2.3, 1.7, 5.4) shifted_image = fourier_shift(reference_image, subpixel_shift) result, error, diffphase = register_translation(reference_image, shifted_image, 100, space="fourier") assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
def test_size_one_dimension_input(): # take a strip of the input image reference_image = np.fft.fftn(camera()[:, 15]).reshape((-1, 1)) subpixel_shift = (-2.4, 4) shifted_image = fourier_shift(reference_image, subpixel_shift) # subpixel precision result, error, diffphase = register_translation(reference_image, shifted_image, 100, space="fourier") assert_allclose(result[:2], -np.array((-2.4, 0)), atol=0.05)
def test_3d_input(): phantom = img_as_float(binary_blobs(length=32, n_dim=3)) reference_image = np.fft.fftn(phantom) shift = (-2., 1., 5.) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.05) # subpixel precision not available for 3-D data subpixel_shift = (-2.3, 1., 5.) shifted_image = fourier_shift(reference_image, subpixel_shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.5) assert_raises(NotImplementedError, register_translation, reference_image, shifted_image, upsample_factor=100, space="fourier")
def test_3d_input(): phantom = img_as_float(binary_blobs(length=32, n_dim=3)) reference_image = np.fft.fftn(phantom) shift = (-2., 1., 5.) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.05) # subpixel precision not available for 3-D data subpixel_shift = (-2.3, 1., 5.) shifted_image = fourier_shift(reference_image, subpixel_shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.5) with pytest.raises(NotImplementedError): register_translation( reference_image, shifted_image, upsample_factor=100, space="fourier")
def test_masked_registration_vs_register_translation(): """masked_register_translation should give the same results as register_translation in the case of trivial masks.""" reference_image = camera() shift = (-7, 12) shifted = np.real(fft.ifft2(fourier_shift( fft.fft2(reference_image), shift))) trivial_mask = np.ones_like(reference_image) nonmasked_result, *_ = register_translation(reference_image, shifted) masked_result = masked_register_translation( reference_image, shifted, trivial_mask, overlap_ratio=1 / 10) assert_equal(nonmasked_result, masked_result)
def test_3d_input(): phantom = img_as_float(binary_blobs(length=32, n_dim=3)) reference_image = np.fft.fftn(phantom) shift = (-2., 1., 5.) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = register_translation(reference_image, shifted_image, space="fourier") assert_allclose(result, -np.array(shift), atol=0.05) # subpixel precision now available for 3-D data # skip the test for numpy older than 1.12, which does not have # einsum optimization if LooseVersion(np.__version__) >= LooseVersion("1.12"): subpixel_shift = (-2.3, 1.7, 5.4) shifted_image = fourier_shift(reference_image, subpixel_shift) result, error, diffphase = register_translation(reference_image, shifted_image, 100, space="fourier") assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
def test_masked_registration_vs_register_translation(): """masked_register_translation should give the same results as register_translation in the case of trivial masks.""" reference_image = camera() shift = (-7, 12) shifted = np.real(np.fft.ifft2(fourier_shift( np.fft.fft2(reference_image), shift))) trivial_mask = np.ones_like(reference_image) nonmasked_result, *_ = register_translation(reference_image, shifted) masked_result = masked_register_translation( reference_image, shifted, trivial_mask, overlap_ratio=1 / 10) assert_equal(nonmasked_result, masked_result)
def test_wrong_input(): # Dimensionality mismatch image = np.ones((5, 5, 1)) template = np.ones((5, 5)) with pytest.raises(ValueError): register_translation(template, image) # Greater than 2 dimensions does not support subpixel precision # (TODO: should support 3D at some point.) image = np.ones((5, 5, 5)) template = np.ones((5, 5, 5)) with pytest.raises(NotImplementedError): register_translation(template, image, 2) # Size mismatch image = np.ones((5, 5)) template = np.ones((4, 4)) with pytest.raises(ValueError): register_translation(template, image)
# show the images: plot_grid(sciencecube, science_list) plt.show() selection = raw_input( 'Type the number of the desired reference image for alignment. (1-' + str(len(flat_dedark_science_list)) + '): ') zero_shift_image = flat_dedark_science_list[int(selection) - 1] print "\nShifting and stacking images..." # find all shifts for other images: imshifts = {} # dictionary to hold the x and y shift pairs for each image for image in flat_dedark_science_list: # register_translation is a function that calculates shifts by comparing 2-D arrays result, error, diffphase = register_translation( flat_dedark_data_out[zero_shift_image], flat_dedark_data_out[image], 750) imshifts[image] = result # print imshifts ## for troubleshooting # new list for shifted image names: shifted_science_list = ['shifted_' + im for im in flat_dedark_science_list] # new dictionary for shifted image data: shifted_science_data = {} for i in range(len(shifted_science_list)): # interp.shift is the function doing the heavy lifting here, # it's reinterpolating each array into the new, shifted one shifted_science_data[shifted_science_list[i]] = interp.shift( flat_dedark_data_out[flat_dedark_science_list[i]],
def test_no_4D(): # Dimensionality mismatch image = np.ones((5, 5, 5, 5)) template = np.ones((5, 5, 5, 5)) with testing.raises(NotImplementedError): register_translation(template, image, upsample_factor=5)
def test_unknown_space_input(): image = np.ones((5, 5)) with pytest.raises(ValueError): register_translation( image, image, space="frank")
print "\nRecord the index (first image is 1) of the reference image to use for alignment, and close the figure." # show the images: plot_grid(sciencecube, science_list) plt.show() selection = raw_input( 'Type the number of the desired reference image for alignment. (1-' + str(len(science_list)) + '): ') zero_shift_image = science_list[int(selection) - 1] print "\nShifting images..." # find all shifts for other images: imshifts = {} # dictionary to hold the x and y shift pairs for each image for image in science_list: # register_translation is a function that calculates shifts by comparing 2-D arrays result, error, diffphase = register_translation( image_data[zero_shift_image], image_data[image], 1000) imshifts[image] = result # print imshifts ## for troubleshooting # new list for shifted image names: shifted_science_list = ['shifted_' + im for im in science_list] # new dictionary for shifted image data: shifted_science_data = {} for i in range(len(shifted_science_list)): # interp.shift is the function doing the heavy lifting here, # it's reinterpolating each array into the new, shifted one shifted_science_data[shifted_science_list[i]] = interp.shift( image_data[science_list[i]], imshifts[science_list[i]])