def test_mapcube_coalign_by_match_template(aia171_test_mc, aia171_test_map_layer_shape): # Define these local variables to make the code more readable ny = aia171_test_map_layer_shape[0] nx = aia171_test_map_layer_shape[1] # Get the calculated test displacements test_displacements = calculate_match_template_shift(aia171_test_mc) # Test passing in displacements test_mc = mapcube_coalign_by_match_template(aia171_test_mc, shift=test_displacements) # Make sure the output is a mapcube assert isinstance(test_mc, map.MapCube) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=False) assert test_mc[0].data.shape == aia171_test_map_layer_shape assert test_mc[1].data.shape == aia171_test_map_layer_shape # Test the returned mapcube using the default - clipping on. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc) x_displacement_pixels = test_displacements["x"] / test_mc[0].scale.x y_displacement_pixels = test_displacements["y"] / test_mc[0].scale.y expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1]))] assert test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value) assert test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value) # Test the returned mapcube explicitly using clip=True. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=True) x_displacement_pixels = test_displacements["x"] / test_mc[0].scale.x y_displacement_pixels = test_displacements["y"] / test_mc[0].scale.y expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1]))] assert test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value) assert test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value) # Test that the reference pixel of each map in the coaligned mapcube is # correct. for im, m in enumerate(aia171_test_mc): for i_s, s in enumerate(["x", "y"]): assert_allclose( aia171_test_mc[im].reference_pixel[i_s] - test_mc[im].reference_pixel[i_s], test_displacements[s][im] / m.scale[i_s], rtol=5e-2, atol=0, )
def test_mapcube_coalign_by_match_template(aia171_test_mc, aia171_test_map_layer_shape): # Define these local variables to make the code more readable ny = aia171_test_map_layer_shape[0] nx = aia171_test_map_layer_shape[1] # Get the calculated test displacements test_displacements = calculate_match_template_shift(aia171_test_mc) # Test passing in displacements test_mc = mapcube_coalign_by_match_template(aia171_test_mc, shift=test_displacements) # Make sure the output is a mapcube assert(isinstance(test_mc, map.MapCube)) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=False) assert(test_mc[0].data.shape == aia171_test_map_layer_shape) assert(test_mc[1].data.shape == aia171_test_map_layer_shape) # Test the returned mapcube using the default - clipping on. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc) x_displacement_pixels = test_displacements['x'] / test_mc[0].scale.x y_displacement_pixels = test_displacements['y'] / test_mc[0].scale.y expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1]))] assert(test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) assert(test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) # Test the returned mapcube explicitly using clip=True. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=True) x_displacement_pixels = test_displacements['x'] / test_mc[0].scale.x y_displacement_pixels = test_displacements['y'] / test_mc[0].scale.y expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1]))] assert(test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) assert(test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) # Test that the reference pixel of each map in the coaligned mapcube is # correct. for im, m in enumerate(aia171_test_mc): for i_s, s in enumerate(['x', 'y']): assert_allclose(aia171_test_mc[im].reference_pixel[i_s] - test_mc[im].reference_pixel[i_s], test_displacements[s][im] / m.scale[i_s], rtol=5e-2, atol=0)
def test_mapcube_coalign_by_match_template(aia171_test_mc, aia171_test_map_layer_shape): # Define these local variables to make the code more readable ny = aia171_test_map_layer_shape[0] nx = aia171_test_map_layer_shape[1] # Get the test displacements test_displacements = calculate_match_template_shift(aia171_test_mc) # Test passing in displacements test_mc = mapcube_coalign_by_match_template(aia171_test_mc, shift=test_displacements) # Make sure the output is a mapcube assert(isinstance(test_mc, map.MapCube)) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=False) assert(test_mc[0].data.shape == aia171_test_map_layer_shape) assert(test_mc[1].data.shape == aia171_test_map_layer_shape) # Test the returned mapcube using the default - clipping on. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc) x_displacement_pixels = test_displacements['x'] / test_mc[0].scale.x y_displacement_pixels = test_displacements['y'] / test_mc[0].scale.y expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1]))] assert(test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) assert(test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) # Test the returned mapcube explicitly using clip=True. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=True) x_displacement_pixels = test_displacements['x'] / test_mc[0].scale.x y_displacement_pixels = test_displacements['y'] / test_mc[0].scale.y expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1]))] assert(test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) assert(test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value))
def test_mapcube_coalign_by_match_template(): # take the AIA image and shift it # Pixel displacements have the y-displacement as the first entry pixel_displacements = np.asarray([1.6, 10.1]) known_displacements = {'x':np.asarray([0.0, pixel_displacements[1] * testmap.scale['x']]), 'y':np.asarray([0.0, pixel_displacements[0] * testmap.scale['y']])} # Create a map that has been shifted a known amount. d1 = shift(testmap.data, pixel_displacements) m1 = map.Map((d1, testmap.meta)) # Create the mapcube mc = map.Map([testmap, m1], cube=True) # Test to see if the code can recover the displacements. Do the coalignment # using the "return_displacements_only" option test_displacements = mapcube_coalign_by_match_template(mc, return_displacements_only=True) # Assert assert_allclose(test_displacements['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], known_displacements['y'], rtol=5e-2, atol=0 ) # Test setting the template as a ndarray template_ndarray = testmap.data[ny / 4: 3 * ny / 4, nx / 4: 3 * nx / 4] test_displacements = mapcube_coalign_by_match_template(mc, template=template_ndarray, return_displacements_only=True) # Assert assert_allclose(test_displacements['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], known_displacements['y'], rtol=5e-2, atol=0 ) # Test setting the template as GenericMap submap = testmap.submap([nx / 4, 3 * nx / 4], [ny / 4, 3 * ny / 4], units='pixels') test_displacements = mapcube_coalign_by_match_template(mc, template=submap, return_displacements_only=True) # Assert assert_allclose(test_displacements['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], known_displacements['y'], rtol=5e-2, atol=0 ) # Test setting the template as something other than a ndarray and a # GenericMap. This should throw a ValueError. try: test_displacements = mapcube_coalign_by_match_template(mc, template='broken') except ValueError: pass # Test passing in displacements test_apply_displacements = {'x':-test_displacements['x'], 'y':-test_displacements['y']} test_displacements = mapcube_coalign_by_match_template(mc, apply_displacements=test_apply_displacements, return_displacements_only=True) assert_allclose(test_displacements['x'], test_apply_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], test_apply_displacements['y'], rtol=5e-2, atol=0) # Test returning using the "with_displacements" option test_output = mapcube_coalign_by_match_template(mc, with_displacements=True) # Assert assert(isinstance(test_output[0], map.MapCube)) assert_allclose(test_output[1]['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_output[1]['y'], known_displacements['y'], rtol=5e-2, atol=0 ) # Test returning with no extra options - the code returns a mapcube only test_output = mapcube_coalign_by_match_template(mc) assert(isinstance(test_output, map.MapCube)) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = mapcube_coalign_by_match_template(mc, clip=False) assert(test_mc[0].data.shape == testmap.data.shape) assert(test_mc[1].data.shape == testmap.data.shape)
def test_mapcube_coalign_by_match_template(aia171_test_mc, aia171_test_map_layer_shape): # Define these local variables to make the code more readable ny = aia171_test_map_layer_shape[0] nx = aia171_test_map_layer_shape[1] # Get the test displacements test_displacements = calculate_match_template_shift(aia171_test_mc) # Test passing in displacements test_mc = mapcube_coalign_by_match_template(aia171_test_mc, shift=test_displacements) # Make sure the output is a mapcube assert (isinstance(test_mc, map.MapCube)) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=False) assert (test_mc[0].data.shape == aia171_test_map_layer_shape) assert (test_mc[1].data.shape == aia171_test_map_layer_shape) # Test the returned mapcube using the default - clipping on. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc) x_displacement_pixels = test_displacements['x'].to( 'arcsec').value / test_mc[0].scale['x'] * u.pix y_displacement_pixels = test_displacements['y'].to( 'arcsec').value / test_mc[0].scale['y'] * u.pix expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [ np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1])) ] assert (test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) assert (test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) # Test the returned mapcube explicitly using clip=True. # All output layers should have the same size # which is smaller than the input by a known amount test_mc = mapcube_coalign_by_match_template(aia171_test_mc, clip=True) x_displacement_pixels = test_displacements['x'].to( 'arcsec').value / test_mc[0].scale['x'] * u.pix y_displacement_pixels = test_displacements['y'].to( 'arcsec').value / test_mc[0].scale['y'] * u.pix expected_clipping = calculate_clipping(y_displacement_pixels, x_displacement_pixels) number_of_pixels_clipped = [ np.sum(np.abs(expected_clipping[0])), np.sum(np.abs(expected_clipping[1])) ] assert (test_mc[0].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value)) assert (test_mc[1].data.shape == (ny - number_of_pixels_clipped[0].value, nx - number_of_pixels_clipped[1].value))
def modifyData(self, data_model: CompositeMapModel) -> DataModel: mc = Map(data_model.getMaps(), cube=True) coaligned = mapcube_coalign_by_match_template(mc) data_model.updateMaps(coaligned.maps) return data_model
def im_coalign(self): self.mapcube = mapcube_coalign_by_match_template(self.mapcube, layer_index=np.round(len(self.mapcube)/2))