Skip to content

Commit

Permalink
Add tests that will fail without fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed Apr 21, 2023
1 parent 08a1b27 commit dfaa0e4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 170 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
56 changes: 56 additions & 0 deletions jdaviz/configs/imviz/tests/test_simple_aper_phot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import pytest
import numpy as np
from astropy import units as u
from astropy.io import fits
from astropy.tests.helper import assert_quantity_allclose
from astropy.utils.data import get_pkg_data_filename
from glue.core.edit_subset_mode import ReplaceMode
from numpy.testing import assert_allclose, assert_array_equal
from photutils.aperture import (ApertureStats, CircularAperture, EllipticalAperture,
RectangularAperture, EllipticalAnnulus)
from regions import PixCoord, CirclePixelRegion, RectanglePixelRegion

from jdaviz.configs.imviz.plugins.aper_phot_simple.aper_phot_simple import (
_curve_of_growth, _radial_profile)
Expand Down Expand Up @@ -190,6 +193,59 @@ def test_plugin_no_wcs(self):
assert_array_equal(tbl['sky_center'], None)


class TestAdvancedAperPhot:
@pytest.fixture(autouse=True)
def setup_class(self, imviz_helper):
# Reference image
fn_1 = get_pkg_data_filename('data/gauss100_fits_wcs.fits')
imviz_helper.load_data(fn_1)
# Different pixel scale
imviz_helper.load_data(get_pkg_data_filename('data/gauss100_fits_wcs_block_reduced.fits'))
# Different pixel scale + rotated
imviz_helper.load_data(get_pkg_data_filename('data/gauss100_fits_wcs_block_reduced_rotated.fits')) # noqa: E501

# Reference image again but without any WCS
data = fits.getdata(fn_1, ext=0)
imviz_helper.load_data(data, data_label='no_wcs')

# Link them by WCS
imviz_helper.link_data(link_type='wcs')

# Regions to be used for aperture photometry
regions = []
positions = [(145.1, 168.3), (84.7, 224.1), (48.3, 200.3)]
for x, y in positions:
regions.append(CirclePixelRegion(center=PixCoord(x=x, y=y), radius=5))
regions.append(RectanglePixelRegion(center=PixCoord(x=229, y=152), width=17, height=5))
imviz_helper.load_regions(regions)

self.imviz = imviz_helper
self.viewer = imviz_helper.default_viewer
self.phot_plugin = imviz_helper.plugins["Imviz Simple Aperture Photometry"]._obj

# Data has a mean background of 5.
self.phot_plugin.bg_subset_selected = 'Manual'
self.phot_plugin.background_value = 5.0

@pytest.mark.parametrize('data_label', [
'gauss100_fits_wcs[PRIMARY,1]',
'gauss100_fits_wcs_block_reduced[PRIMARY,1]',
'gauss100_fits_wcs_block_reduced_rotated[PRIMARY,1]',
'no_wcs'])
@pytest.mark.parametrize(('subset_label', 'expected_sum'), [
('Subset 1', 738.8803424408962),
('Subset 2', 348.33262363800094),
('Subset 3', 857.5194857987592),
('Subset 4', 762.3263959884644)])
def test_aperphot(self, data_label, subset_label, expected_sum):
"""All data should give similar result for the same Subset."""
self.phot_plugin.dataset_selected = data_label
self.phot_plugin.subset_selected = subset_label
self.phot_plugin.vue_do_aper_phot()
tbl = self.imviz.get_aperture_photometry_results()
assert_allclose(tbl['sum'][-1], expected_sum)


def test_annulus_background(imviz_helper):
from photutils.datasets import make_4gaussians_image

Expand Down
189 changes: 19 additions & 170 deletions notebooks/concepts/imviz_advanced_aper_phot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from astropy import coordinates as coord\n",
"from astropy import units as u\n",
"from astropy.modeling import models\n",
"from astropy.nddata import NDData, block_reduce\n",
"from gwcs import coordinate_frames as cf\n",
"from gwcs.wcs import WCS as GWCS\n",
"from photutils.datasets import make_100gaussians_image, make_wcs\n",
"from astropy.utils.data import get_pkg_data_filename\n",
"from regions import PixCoord, CirclePixelRegion, RectanglePixelRegion\n",
"from reproject import reproject_interp\n",
"\n",
"from jdaviz import Imviz\n",
"\n",
Expand Down Expand Up @@ -58,23 +50,12 @@
{
"cell_type": "code",
"execution_count": null,
"id": "431e826e",
"id": "4741f22b",
"metadata": {},
"outputs": [],
"source": [
"data = make_100gaussians_image()\n",
"w = make_wcs(data.shape)\n",
"image_1 = NDData(data, wcs=w)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea52b644",
"metadata": {},
"outputs": [],
"source": [
"imviz.load_data(image_1, data_label=\"fits_wcs\")"
"file1 = get_pkg_data_filename('data/gauss100_fits_wcs.fits', package='jdaviz.configs.imviz.tests')\n",
"imviz.load_data(file1)"
]
},
{
Expand Down Expand Up @@ -132,175 +113,53 @@
"id": "8e6fc84a-a53d-4de3-ba26-8d6ee4285e02",
"metadata": {},
"source": [
"### Image 2: GWCS with rotation and different pixel scale\n",
"### Image 2: WCS with a different pixel scale\n",
"\n",
"Now we need an image would result in the Subset being hard to work with. Say, this image would have a different rotation and pixel scale. Offset of the center is also implicitly tested by using a different pixel scale.\n",
"Now we need an image would result in the Subset being hard to work with. Say, this image would have a different pixel scale. Offset of the center is also implicitly tested by using a different pixel scale.\n",
"\n",
"*Note: Originally, distortion was also planned but it added a huge amount of computation time to reproject and linking; hence it was abandoned from the test case.*\n",
"\n",
"We start with different pixel scale by down-sampling it by half each dimension."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1185f672-52d4-42c6-b952-7fe587287b07",
"metadata": {},
"outputs": [],
"source": [
"# Properties from Image 1\n",
"print(data.shape)\n",
"w.to_header()"
"We define a different pixel scale by down-sampling it by half each dimension."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5a18a91-7eb1-483e-ae0a-ab3a78e6596c",
"id": "b7f71606",
"metadata": {},
"outputs": [],
"source": [
"# We want it for the new pixel scale.\n",
"new_shape = tuple((sh // 2) for sh in data.shape)\n",
"new_shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6120c794-a00a-45f4-8b49-634e69b7774d",
"metadata": {},
"outputs": [],
"source": [
"# https://gwcs.readthedocs.io/en/latest/index.html#getting-started\n",
"\n",
"shift_by_crpix = (models.Shift(-(new_shape[1] - 125 - 0.75) * u.pix) &\n",
" models.Shift(-(new_shape[0] - 75 - 0.75) * u.pix))\n",
"\n",
"matrix = np.array([[-1.3888888888888893e-05, 2.4056261216234408e-05],\n",
" [2.4056261216234408e-05, 1.3888888888888893e-05]])\n",
"rotation = models.AffineTransformation2D(matrix * u.deg,\n",
" translation=[0, 0] * u.deg)\n",
"rotation.input_units_equivalencies = {\"x\": u.pixel_scale(2 * (u.deg/u.pix)),\n",
" \"y\": u.pixel_scale(2 * (u.deg/u.pix))}\n",
"rotation.inverse = models.AffineTransformation2D(np.linalg.inv(matrix) * u.pix,\n",
" translation=[0, 0] * u.pix)\n",
"rotation.inverse.input_units_equivalencies = {\"x\": u.pixel_scale(0.5 * (u.pix / u.deg)),\n",
" \"y\": u.pixel_scale(0.5 * (u.pix / u.deg))}\n",
"\n",
"tan = models.Pix2Sky_TAN()\n",
"celestial_rotation = models.RotateNative2Celestial(\n",
" 197.8925 * u.deg, -1.36555556 * u.deg, 180 * u.deg)\n",
"\n",
"det2sky = shift_by_crpix | rotation | tan | celestial_rotation\n",
"det2sky.name = \"linear_transform\"\n",
"\n",
"detector_frame = cf.Frame2D(name=\"detector\", axes_names=(\"x\", \"y\"),\n",
" unit=(u.pix, u.pix))\n",
"sky_frame = cf.CelestialFrame(reference_frame=coord.ICRS(), name='icrs',\n",
" unit=(u.deg, u.deg))\n",
"\n",
"pipeline = [(detector_frame, det2sky),\n",
" (sky_frame, None)]\n",
"gw = GWCS(pipeline)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bac3b7c2-a9e0-4b30-a092-826e00078862",
"metadata": {},
"outputs": [],
"source": [
"data_sm = block_reduce(data, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "874e9cef-85ea-4055-b1d5-3802efcba7a8",
"metadata": {},
"outputs": [],
"source": [
"image_2 = NDData(data_sm, wcs=gw)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05832fda-22b4-42de-98fb-254ef396b03d",
"metadata": {},
"outputs": [],
"source": [
"imviz.load_data(image_2, data_label=\"gwcs\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c2793e8-65d4-46ed-a946-347767d1309d",
"metadata": {},
"outputs": [],
"source": [
"# imviz.link_data(link_type='wcs')"
"file2 = get_pkg_data_filename('data/gauss100_fits_wcs_block_reduced.fits', package='jdaviz.configs.imviz.tests')\n",
"imviz.load_data(file2)"
]
},
{
"cell_type": "markdown",
"id": "987c96f0-ba8b-42b1-9ffd-6f50db3f190c",
"metadata": {},
"source": [
"### Image 2.1: Add rotation to downsampled image"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91dc1847-1ae1-4a51-aa44-feac12f4c290",
"metadata": {},
"outputs": [],
"source": [
"# Like above but with extra rotation\n",
"celestial_rotation = models.RotateNative2Celestial(\n",
" 197.8925 * u.deg, -1.36555556 * u.deg, 190 * u.deg)\n",
"\n",
"det2sky = shift_by_crpix | rotation | tan | celestial_rotation\n",
"det2sky.name = \"linear_transform\"\n",
"### Image 3: Add rotation to downsampled image\n",
"\n",
"pipeline = [(detector_frame, det2sky),\n",
" (sky_frame, None)]\n",
"gw_rot = GWCS(pipeline)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43797cca-c6e1-4bc0-ad66-46ba5b16bcfe",
"metadata": {},
"outputs": [],
"source": [
"# This takes much longer when GWCS has distortion.\n",
"data_rot, _ = reproject_interp(image_2, gw_rot, shape_out=data_sm.shape)"
"Like above but with extra rotation."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b52e436-2cd3-4a42-b7a8-d489a6fb06ad",
"id": "91dc1847-1ae1-4a51-aa44-feac12f4c290",
"metadata": {},
"outputs": [],
"source": [
"image_2_1 = NDData(data_rot, wcs=gw_rot)"
"file3 = get_pkg_data_filename('data/gauss100_fits_wcs_block_reduced_rotated.fits', package='jdaviz.configs.imviz.tests')\n",
"imviz.load_data(file3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42cadf07-99cc-4816-b6c3-43847c73478e",
"cell_type": "markdown",
"id": "4be18968",
"metadata": {},
"outputs": [],
"source": [
"imviz.load_data(image_2_1, data_label=\"gwcs_rot\")"
"### Link them by WCS"
]
},
{
Expand All @@ -318,7 +177,7 @@
"id": "fe16d7c9-5b41-45fe-b7a5-3f6c1e91ec95",
"metadata": {},
"source": [
"## How is glue projecting the rectangle?\n",
"### How is glue projecting the rectangle?\n",
"\n",
"How is `glue` really projecting the rectangle for all the data?"
]
Expand Down Expand Up @@ -360,16 +219,6 @@
"Even when unrotated, a different pixel scale does affect the projected mask dimension. When rotated, it is not even a rectangle anymore; furthermore, even though this projection follows WCS linking, the array from `to_mask()` does not account for rotation that we see in the viewer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5efe10a8-4e83-4c88-92ea-cc7ad4b42538",
"metadata": {},
"outputs": [],
"source": [
"# TODO: Now how do we use this test case?"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit dfaa0e4

Please sign in to comment.