def test_simple_add_imagehdu_conserves_flux(self, image_hdu_square, image_hdu_rect): fields = [image_hdu_rect, image_hdu_square] hdr = imp_utils.get_canvas_header(pixel_scale=1 * u.arcsec, hdu_or_table_list=fields) print(wcs.WCS(image_hdu_rect)) print(wcs.WCS(hdr)) implane = opt_imp.ImagePlane(hdr) implane.add(image_hdu_rect) if PLOTS: plt.imshow(image_hdu_rect.data) x, y = wcs.WCS(image_hdu_rect).wcs_world2pix(0, 0, 1) print(x, y) plt.plot(x, y, "ro") plt.show() plt.imshow(implane.data) x, y = wcs.WCS(image_hdu_rect).wcs_world2pix(0, 0, 1) print(x, y) plt.plot(x, y, "ro") plt.show() assert np.sum(implane.data) == approx(np.sum(image_hdu_rect.data))
def test_mm_image_is_added_to_small_canvas(self, image_hdu_rect_mm, image_hdu_square_mm): im_hdu = image_hdu_rect_mm im_hdu.header["CRVAL1D"] -= 150 im_hdu.header["CRVAL2D"] += 40 hdr = imp_utils.get_canvas_header([im_hdu, image_hdu_square_mm], pixel_scale=1 * u.mm) im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"])) canvas_hdu = fits.ImageHDU(header=hdr, data=im) canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(im_hdu, canvas_hdu, wcs_suffix="D") assert np.sum(canvas_hdu.data) == approx(np.sum(im_hdu.data)) if PLOTS: for im in [im_hdu, image_hdu_square_mm]: x, y = imp_utils.calc_footprint(im.header, "D") x, y = imp_utils.val2pix(canvas_hdu.header, x, y, "D") plt.plot(x, y, "r-") x0, y0 = imp_utils.val2pix(canvas_hdu.header, 0, 0, "D") plt.plot(x0, y0, "ro") plt.gca().set_aspect(1) plt.imshow(canvas_hdu.data, origin="lower") plt.show()
def test_image_is_added_to_small_canvas(self, image_hdu_rect, image_hdu_square): im_hdu = image_hdu_rect im_hdu.header["CRVAL1"] -= 150 * u.arcsec.to(u.deg) im_hdu.header["CRVAL2"] += 40 * u.arcsec.to(u.deg) hdr = imp_utils.get_canvas_header([im_hdu, image_hdu_square]) im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"])) canvas_hdu = fits.ImageHDU(header=hdr, data=im) canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(im_hdu, canvas_hdu) canvas_hdu = imp_utils.add_imagehdu_to_imagehdu( image_hdu_square, canvas_hdu) flux = np.sum(im_hdu.data) + np.sum(image_hdu_square.data) assert np.sum(canvas_hdu.data) == approx(flux, rel=1e-2) if PLOTS: for im in [im_hdu, image_hdu_square]: x, y = imp_utils.calc_footprint(im.header) x, y = imp_utils.val2pix(canvas_hdu.header, x, y) plt.plot(x, y, "r-") x0, y0 = imp_utils.val2pix(canvas_hdu.header, 0, 0) plt.plot(x0, y0, "ro") plt.gca().set_aspect(1) plt.imshow(canvas_hdu.data, origin="lower") plt.show()
def test_mm_points_are_added_to_massive_canvas(self, input_table_mm): tbl1 = deepcopy(input_table_mm) tbl2 = deepcopy(input_table_mm) tbl3 = deepcopy(input_table_mm) tbl1["y_mm"] += 50 tbl2["x_mm"] += 20 tbl3["x_mm"] -= 25 tbl3["y_mm"] -= 25 hdr = imp_utils.get_canvas_header([tbl1, tbl2, tbl3], pixel_scale=1 * u.mm) im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"])) canvas_hdu = fits.ImageHDU(header=hdr, data=im) for tbl in [tbl1, tbl2, tbl3]: canvas_hdu = imp_utils.add_table_to_imagehdu(tbl, canvas_hdu, True, wcs_suffix="D") total_flux = np.sum([tbl1["flux"], tbl1["flux"], tbl1["flux"]]) assert np.sum(canvas_hdu.data) == total_flux if PLOTS: x, y = imp_utils.val2pix(hdr, 0, 0, "D") plt.plot(x, y, "ro") "top left is green, top right is yellow" plt.imshow(canvas_hdu.data, origin="lower") plt.show()
def test_initialises_with_header_with_hdu(self, image_hdu_square, image_hdu_rect): hdr = imp_utils.get_canvas_header( pixel_scale=0.1 * u.arcsec, hdu_or_table_list=[image_hdu_rect, image_hdu_square]) implane = opt_imp.ImagePlane(hdr) assert isinstance(implane, opt_imp.ImagePlane) assert isinstance(implane.hdu, fits.ImageHDU)
def test_header_contains_spread_out_regions(self, offset, image_hdu_square, image_hdu_rect): image_hdu_rect.header["CRVAL1"] += offset * u.arcsec.to(u.deg) hdr = imp_utils.get_canvas_header([image_hdu_square, image_hdu_rect]) image_width = image_hdu_square.header["NAXIS1"] // 2 + \ image_hdu_rect.header["NAXIS1"] // 2 + abs(offset) + 2 assert hdr["NAXIS1"] == image_width
def test_simple_add_table_conserves_flux(self, image_hdu_rect): x = [75, -75] * u.arcsec y = [0, 0] * u.arcsec flux = [30, 20] * u.Unit("ph s-1") tbl = Table(names=["x", "y", "flux"], data=[x, y, flux]) hdr = imp_utils.get_canvas_header( pixel_scale=0.1 * u.arcsec, hdu_or_table_list=[image_hdu_rect, tbl]) implane = opt_imp.ImagePlane(hdr) implane.add(tbl) assert np.isclose(np.sum(implane.data), np.sum(flux.value))
def test_header_has_correct_size_based_on_table_extremes( self, offset, input_table): tbl1 = input_table tbl2 = deepcopy(input_table) tbl3 = deepcopy(input_table) tbl2["x"] += offset tbl3["y"] += offset hdr = imp_utils.get_canvas_header([tbl1, tbl2, tbl3], pixel_scale=0.1 * u.arcsec) assert hdr["NAXIS1"] == np.max(tbl1["x"] + tbl2["x"]) * 10 + 4 assert hdr["NAXIS2"] == np.max(tbl1["y"] + tbl3["y"]) * 10 + 4
def test_mm_image_and_tables_on_large_canvas(self, input_table_mm, image_hdu_rect_mm, image_hdu_square_mm): image_hdu_rect = image_hdu_rect_mm image_hdu_square = image_hdu_square_mm tbl1 = deepcopy(input_table_mm) tbl2 = deepcopy(input_table_mm) tbl1["y_mm"] -= 100 tbl2["x_mm"] += 100 tbl2["y_mm"] += 100 im_hdu = image_hdu_rect im_hdu.header["CRVAL1D"] -= 150 im_hdu.header["CRVAL2D"] += 20 hdr = imp_utils.get_canvas_header( [im_hdu, image_hdu_square, tbl1, tbl2], pixel_scale=3 * u.mm) im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"])) canvas_hdu = fits.ImageHDU(header=hdr, data=im) canvas_hdu = imp_utils.add_table_to_imagehdu(tbl1, canvas_hdu, wcs_suffix="D") canvas_hdu = imp_utils.add_table_to_imagehdu(tbl2, canvas_hdu, wcs_suffix="D") canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(im_hdu, canvas_hdu, wcs_suffix="D") canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(image_hdu_square, canvas_hdu, wcs_suffix="D") total_flux = np.sum(tbl1["flux"]) + np.sum(tbl2["flux"]) + \ np.sum(im_hdu.data) + np.sum(image_hdu_square.data) assert np.sum(canvas_hdu.data) == approx(total_flux) if PLOTS: for im in [im_hdu, image_hdu_square]: x, y = imp_utils.calc_footprint(im, "D") x, y = imp_utils.val2pix(canvas_hdu, x, y, "D") plt.plot(x, y, "r-") x0, y0 = imp_utils.val2pix(canvas_hdu, 0, 0, "D") plt.plot(x0, y0, "ro") plt.gca().set_aspect(1) plt.imshow(canvas_hdu.data, origin="lower") plt.show()
def test_all_5_objects_are_inside_header_wcs(self, image_hdu_square, image_hdu_rect, input_table): tbl1 = deepcopy(input_table) tbl2 = deepcopy(input_table) tbl3 = deepcopy(input_table) tbl2["x"] -= 150 tbl3["y"] -= 100 image_hdu_rect.header["CRVAL1"] += 100 * u.arcsec.to(u.deg) image_hdu_square.header["CRVAL1"] += 0 * u.arcsec.to(u.deg) image_hdu_square.header["CRVAL2"] += 100 * u.arcsec.to(u.deg) hdr = imp_utils.get_canvas_header( [image_hdu_square, tbl1, tbl2, tbl3, image_hdu_rect], pixel_scale=1 * u.arcsec) for im in [image_hdu_square.header, image_hdu_rect.header]: x, y = imp_utils.calc_footprint(im) x, y = imp_utils.val2pix(hdr, x, y) for xi, yi in zip(x, y): assert 0 <= xi < hdr["NAXIS1"] assert 0 <= yi < hdr["NAXIS2"] as2deg = u.arcsec.to(u.deg) for tbl in [tbl1, tbl2, tbl3]: x, y = imp_utils.val2pix(hdr, tbl["x"] * as2deg, tbl["y"] * as2deg) for xi, yi in zip(x, y): assert 0 <= xi < hdr["NAXIS1"] assert 0 <= yi < hdr["NAXIS2"] if PLOTS: x, y = imp_utils.calc_footprint(hdr) x, y = imp_utils.val2pix(hdr, x, y) plt.plot(x, y, "b") x0, y0 = imp_utils.val2pix(hdr, 0, 0) plt.plot(x0, y0, "ro") for tbl in [tbl1, tbl2, tbl3]: x, y = imp_utils.val2pix(hdr, tbl["x"] * as2deg, tbl["y"] * as2deg) plt.plot(x, y, "k.") for im in [image_hdu_square.header, image_hdu_rect.header]: x, y = imp_utils.calc_footprint(im) x, y = imp_utils.val2pix(hdr, x, y) plt.plot(x, y, "r-") plt.gca().set_aspect(1) plt.show()
def test_all_5_objects_are_inside_mm_header_wcs(self, image_hdu_square_mm, image_hdu_rect_mm, input_table_mm): tbl1 = deepcopy(input_table_mm) tbl2 = deepcopy(input_table_mm) tbl3 = deepcopy(input_table_mm) tbl2["x_mm"] -= 150 tbl3["y_mm"] -= 100 image_hdu_rect_mm.header["CRVAL1D"] += 100 image_hdu_square_mm.header["CRVAL1D"] += 0 image_hdu_square_mm.header["CRVAL2D"] += 100 hdr = imp_utils.get_canvas_header( [image_hdu_square_mm, tbl1, tbl2, tbl3, image_hdu_rect_mm], pixel_scale=1 * u.mm) for im in [image_hdu_square_mm.header, image_hdu_rect_mm.header]: x, y = imp_utils.calc_footprint(im, "D") x, y = imp_utils.val2pix(hdr, x, y, "D") for xi, yi in zip(x, y): assert 0 <= xi < hdr["NAXIS1"] assert 0 <= yi < hdr["NAXIS2"] for tbl in [tbl1, tbl2, tbl3]: x, y = imp_utils.val2pix(hdr, tbl["x_mm"], tbl["y_mm"], "D") for xi, yi in zip(x, y): assert 0 <= xi < hdr["NAXIS1"] assert 0 <= yi < hdr["NAXIS2"] if PLOTS: x, y = imp_utils.calc_footprint(hdr, "D") x, y = imp_utils.val2pix(hdr, x, y, "D") plt.plot(x, y, "b") x0, y0 = imp_utils.val2pix(hdr, 0, 0, "D") plt.plot(x0, y0, "ro") for tbl in [tbl1, tbl2, tbl3]: x, y = imp_utils.val2pix(hdr, tbl["x_mm"], tbl["y_mm"], "D") plt.plot(x, y, "k.") for im in [image_hdu_square_mm.header, image_hdu_rect_mm.header]: x, y = imp_utils.calc_footprint(im, "D") x, y = imp_utils.val2pix(hdr, x, y, "D") plt.plot(x, y, "r-") plt.gca().set_aspect(1) plt.show()
def test_points_are_added_to_small_canvas(self, input_table): tbl1 = deepcopy(input_table) hdr = imp_utils.get_canvas_header([tbl1]) im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"])) canvas_hdu = fits.ImageHDU(header=hdr, data=im) canvas_hdu = imp_utils.add_table_to_imagehdu(tbl1, canvas_hdu) assert np.sum(canvas_hdu.data) == np.sum(tbl1["flux"]) if PLOTS: "top left is green, top right is yellow" plt.imshow(canvas_hdu.data, origin="lower") plt.show()
def test_compound_add_image_and_table_conserves_flux(self, image_hdu_rect): x = [75, -75] * u.arcsec y = [0, 0] * u.arcsec flux = [30, 20] * u.Unit("ph s-1") tbl = Table(names=["x", "y", "flux"], data=[x, y, flux]) hdr = imp_utils.get_canvas_header( pixel_scale=0.1 * u.arcsec, hdu_or_table_list=[image_hdu_rect, tbl]) implane = opt_imp.ImagePlane(hdr) implane.add(tbl) implane.add(image_hdu_rect) out_sum = np.sum(implane.data) in_sum = np.sum(flux.value) + np.sum(image_hdu_rect.data) assert out_sum == approx(in_sum, rel=1e-3)
def test_add_many_mm_tables_and_imagehdus(self, input_table_mm, image_hdu_rect_mm, image_hdu_square_mm): image_hdu_rect = image_hdu_rect_mm image_hdu_square = image_hdu_square_mm tbl1 = deepcopy(input_table_mm) tbl2 = deepcopy(input_table_mm) tbl1["y_mm"] -= 50 tbl2["x_mm"] += 50 tbl2["y_mm"] += 50 im_hdu = image_hdu_rect im_hdu.header["CRVAL1D"] -= 150 # mm im_hdu.header["CRVAL2D"] += 20 fields = [im_hdu, tbl1, tbl2, image_hdu_square] hdr = imp_utils.get_canvas_header(fields, pixel_scale=1 * u.mm) implane = opt_imp.ImagePlane(hdr) implane.add(fields, wcs_suffix="D") total_flux = np.sum(tbl1["flux"]) + np.sum(tbl2["flux"]) + \ np.sum(im_hdu.data) + np.sum(image_hdu_square.data) assert np.sum(implane.data) == approx(total_flux) if PLOTS: for im in [im_hdu, image_hdu_square]: x, y = imp_utils.calc_footprint(im.header, "D") x, y = imp_utils.val2pix(implane.header, x, y, "D") plt.plot(x, y, "r-") for tbl in [tbl1, tbl2]: hdr = imp_utils._make_bounding_header_for_tables( [tbl], pixel_scale=1 * u.mm) x, y = imp_utils.calc_footprint(hdr, "D") x, y = imp_utils.val2pix(implane.header, x, y, "D") plt.plot(x, y, "r-") x0, y0 = imp_utils.val2pix(implane.header, 0, 0, "D") plt.plot(x0, y0, "ro") plt.gca().set_aspect(1) plt.imshow(implane.data, origin="lower", norm=LogNorm()) plt.show()
def test_add_many_tables_and_imagehdus(self, input_table, image_hdu_rect, image_hdu_square): tbl1 = deepcopy(input_table) tbl2 = deepcopy(input_table) tbl1["y"] -= 50 tbl2["x"] += 50 tbl2["y"] += 50 im_hdu = image_hdu_rect im_hdu.header["CRVAL1"] -= 150 * u.arcsec.to(u.deg) im_hdu.header["CRVAL2"] += 20 * u.arcsec.to(u.deg) fields = [im_hdu, tbl1, tbl2, image_hdu_square] hdr = imp_utils.get_canvas_header(fields, pixel_scale=1 * u.arcsec) implane = opt_imp.ImagePlane(hdr) implane.add(fields) total_flux = np.sum(tbl1["flux"]) + np.sum(tbl2["flux"]) + \ np.sum(im_hdu.data) + np.sum(image_hdu_square.data) assert np.sum(implane.data) == approx(total_flux) if PLOTS: for im in [im_hdu, image_hdu_square]: x, y = imp_utils.calc_footprint(im.header) x, y = imp_utils.val2pix(implane.header, x, y) plt.plot(x, y, "r-") for tbl in [tbl1, tbl2]: hdr = imp_utils._make_bounding_header_for_tables([tbl]) x, y = imp_utils.calc_footprint(hdr) x, y = imp_utils.val2pix(implane.header, x, y) plt.plot(x, y, "r-") x0, y0 = imp_utils.val2pix(implane.header, 0, 0) plt.plot(x0, y0, "ro") plt.gca().set_aspect(1) plt.imshow(implane.data, origin="lower", norm=LogNorm()) plt.show()
def test_header_contains_future_naxis_pixel_sizes(self, image_hdu_square, image_hdu_rect): hdr = imp_utils.get_canvas_header([image_hdu_square, image_hdu_rect]) assert hdr["NAXIS1"] == 100 + 2 assert hdr["NAXIS2"] == 200 + 2