Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AL-850: adding tweakreg into stcal #267

Merged
merged 38 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9ce63b4
moved tweakreg into stcal
emolter Jul 1, 2024
a4e86c0
remove dependence on ModelContainer, operate on one model at a time
emolter Jul 2, 2024
cabe852
removed dependency on datamodels, plus bugfixes
emolter Jul 3, 2024
a771e7c
pinned dependency for requests, removed commented tests
emolter Jul 3, 2024
1c2c3e8
fix typo from conflict merge
emolter Jul 3, 2024
f6c15c4
added tweakwcs dependency
emolter Jul 3, 2024
7b235ad
pinning dependencies
emolter Jul 8, 2024
55e20f8
merging upstream
emolter Jul 8, 2024
84bdeba
added docs to tweakreg
emolter Jul 8, 2024
da9c4ba
add changelog entry
emolter Jul 8, 2024
c3d54f3
added more tests of tweakreg
emolter Jul 9, 2024
7c818c6
fixing failed unit tests
emolter Jul 9, 2024
ce4fb46
refactor to remove local_align_failed parameter
emolter Jul 9, 2024
1786e93
restoring bounding box check to construct_wcs_corrector
emolter Jul 10, 2024
1a18c06
fixing doc build
emolter Jul 10, 2024
04acbf3
attempted fix of jwst regtest failures
emolter Jul 11, 2024
f6377b4
refactor to remove datamodel dependencies
emolter Jul 15, 2024
fe50a52
attempting to fix doc issue
emolter Jul 15, 2024
f6d8409
change gitignore to ignore autogenerated api docs
emolter Jul 15, 2024
b6a9aec
forgot to remove the folder
emolter Jul 15, 2024
649fcde
changes per @kmacdonald-stsci, attempted fix docs build
emolter Jul 17, 2024
b45b81a
fixes per @mairanteodoro comments, plus better typing of wcsinfo
emolter Jul 17, 2024
0f945df
fix docstring build
emolter Jul 17, 2024
32bda6a
responding to @braingram comments
emolter Jul 18, 2024
25ec18d
updates to Wcsinfo protocol
emolter Jul 19, 2024
8912873
pyproject.toml config for mypy
emolter Jul 19, 2024
a44d758
nitpick ignore to make docs build pass
emolter Jul 19, 2024
76bda86
run mypy in ci
braingram Jul 19, 2024
0800b19
Merge pull request #1 from braingram/patch_01
emolter Jul 19, 2024
3768ea2
Update tox.ini
emolter Jul 19, 2024
3a810e5
Update tox.ini
emolter Jul 19, 2024
e82ac17
changelog bump to 1.8.0
emolter Jul 22, 2024
b2cab4a
revert isinstance check to address jwst repo failures
emolter Jul 23, 2024
fd5c2f4
reworking compute_s_region functions
emolter Jul 23, 2024
9b55e08
remove check of required keywords, should be handled by default error…
emolter Jul 23, 2024
9cb89d9
reverting inadvertent change to s_region_imaging default value
emolter Jul 24, 2024
f6320dc
add wcsinfo docs, make compute_s_region return None if NaNs
emolter Jul 25, 2024
37ea975
Update pyproject.toml
emolter Jul 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
envs: |
- linux: check-style
- linux: check-types
- linux: check-build
test:
uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@v1
Expand Down
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ instance/
# Sphinx documentation
docs/_build/

# auto-generated API docs
docs/source/api
docs/api
.DS_Store

# PyBuilder
.pybuilder/
target/
Expand Down Expand Up @@ -146,9 +151,5 @@ src/stcal/ramp_fitting/ols_cas22/*.html
# setuptools-scm generated module
src/stcal/_version.py

# auto-generated API docs
docs/source/api
.DS_Store

# VSCode stuff
.vscode
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
General
-------

-
- Add TweakReg submodule. [#267]

Changes to API
--------------
Expand Down
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def setup(app):
"matplotlib": ("https://matplotlib.org/stable", None),
"gwcs": ("https://gwcs.readthedocs.io/en/latest/", None),
"astropy": ("https://docs.astropy.org/en/stable/", None),
"tweakwcs": ("https://tweakwcs.readthedocs.io/en/latest/", None),
}

nitpick_ignore = [("py:class", "optional"), ("py:class", "np.ndarray")]

extensions = [
"pytest_doctestplus.sphinx.doctestplus",
"sphinx.ext.autodoc",
Expand Down
1 change: 1 addition & 0 deletions docs/stcal/package_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Package Index
jump/index.rst
ramp_fitting/index.rst
alignment/index.rst
tweakreg/index.rst
12 changes: 12 additions & 0 deletions docs/stcal/tweakreg/astrometric_utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=================
astrometric_utils
=================

The ``astrometric_utils`` module provides functions for generating
astrometric catalogs of sources for the field-of-view covered by a
set of images.

.. currentmodule:: stcal.tweakreg.astrometric_utils

.. automodapi:: stcal.tweakreg.astrometric_utils
:noindex:
228 changes: 228 additions & 0 deletions docs/stcal/tweakreg/description.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
Description
===========

Overview
--------
This step takes as input image catalogs of point-like sources,
which are used to compute corrections to the WCS of
the input images such that sky catalogs obtained from the
image catalogs using the corrected WCS will align on the sky.

Source Catalogs
---------------
The input catalog must be in a format automatically recognized by
`~astropy.table.Table.read`. The catalog must contain
either ``'x'`` and ``'y'`` or ``'xcentroid'`` and ``'ycentroid'`` columns which
indicate source *image* coordinates (in pixels). Pixel coordinates are
0-indexed. An optional column in the catalog is the ``'weight'`` column,
which when present, will be used in fitting.

Relative Alignment
------------------
Relative alignment is performed by
the `~stcal.tweakreg.relative_align` function.
The source catalogs for each input image are compared to each other
and linear (affine) coordinate transformations that align these
catalogs are derived. This fit ensures that all the input images
are aligned relative to each other. This step produces a combined
source catalog for the entire set of input images as if they were
combined into a single mosaic.

Absolute Alignment
------------------

Absolute alignment is performed by
the `~stcal.tweakreg.absolute_align` function.
If the parameter ``abs_refcat`` is set to 'GAIADR3', 'GAIADR2', or 'GAIADR1',
an astrometric reference catalog then gets generated by querying
a GAIA-based astrometric catalog web service for all astrometrically
measured sources in the combined field-of-view of the set of input
images. This catalog is generated from the catalogs available
through the `STScI MAST Catalogs`_ and has the ability to account
for proper motion to a given epoch. The epoch is computed from the observation date and time
of the input data.

.. _STScI MAST Catalogs: https://outerspace.stsci.edu/display/MASTDATA/Catalog+Access

The combined source catalog
then gets cross-matched and fit to this astrometric reference catalog.
The pipeline initially supports fitting to the
GAIADR3 catalog, with the option to select the GAIADR2 or GAIADR1 instead.
The results of this one fit then gets back-propagated to all the
input images to align them all to the astrometric reference frame while
maintaining the relative alignment between the images.

For this part of alignment, instead of 'GAIADR1', 'GAIADR2', or 'GAIADR3', users can
supply an external reference catalog by providing a path to an existing
file. A user-supplied catalog must contain ``'RA'`` and ``'DEC'`` columns
indicating reference source world coordinates (in degrees). An optional column
in the catalog is the ``'weight'`` column, which when present, will be used
in fitting. The catalog must be in a format automatically recognized by
`~astropy.table.Table.read`.

Grouping
--------

Images taken at the same time (e.g., NIRCam images from all short-wave
detectors) can be aligned together; that is, a single correction
can be computed and applied to all these images because any error in
telescope pointing will be identical in all these images and it is assumed
that the relative positions of (e.g., NIRCam) detectors do not change.
Identification of images that belong to the same "exposure" and therefore
can be grouped together is based on several attributes
*change from here to end of section* described in
`~jwst.datamodels.ModelContainer`. This grouping is performed automatically
in the ``tweakreg`` step using the
`~jwst.datamodels.ModelContainer.models_grouped` property, which assigns
a group ID to each input image model in ``meta.group_id``.

However, when detector calibrations are not accurate, alignment of groups
of images may fail (or result in poor alignment). In this case, it may be
desirable to align each image independently. This can be achieved either by
setting the ``image_model.meta.group_id`` attribute to a unique string or integer
value for each image, or by adding the ``group_id`` attribute to the ``members`` of the input ASN
table - see `~jwst.datamodels.ModelContainer` for more details.

.. note::
Group ID (``group_id``) is used by both ``tweakreg`` and ``skymatch`` steps
and so modifying it for one step will affect the results in another step.
If it is desirable to apply different grouping strategies to the ``tweakreg``
and ``skymatch`` steps, one may need to run each step individually and
provide a different ASN as input to each step.

WCS Correction
--------------
The linear coordinate transformation computed in the previous step
is used to define tangent-plane corrections that need to be applied
to the GWCS pipeline in order to correct input image WCS.
This correction is implemented by inserting a ``v2v3corr`` frame with
tangent plane corrections into the GWCS pipeline of the image's WCS.

Arguments
---------
The ``tweakreg`` step has the following optional arguments:


**Relative alignment parameters:**

Parameters used for relative alignment between input images. These parameters
are passed into the `~stcal.tweakreg.relative_align` function.

* ``expand_refcat``: A boolean indicating whether or not to expand reference
catalog with new sources from other input images that have been already
aligned to the reference image. (Default=False)

* ``minobj``: A positive `int` indicating minimum number of objects acceptable
for matching. (Default=15)

* ``searchrad``: A `float` indicating the search radius in arcsec for a match.
(Default=2.0)

* ``use2dhist``: A boolean indicating whether to use 2D histogram to find
initial offset. (Default=True)

* ``separation``: Minimum object separation in arcsec. It **must be** at least
``sqrt(2)`` times larger than ``tolerance``. (Default=1.0)

* ``tolerance``: Matching tolerance for ``xyxymatch`` in arcsec. (Default=0.7)

* ``xoffset``: Initial guess for X offset in arcsec. (Default=0.0)

* ``yoffset``: Initial guess for Y offset in arcsec. (Default=0.0)

* ``fitgeometry``: A `str` value indicating the type of affine transformation
to be considered when fitting catalogs. Allowed values:

- ``'shift'``: x/y shifts only
- ``'rshift'``: rotation and shifts
- ``'rscale'``: rotation and scale
- ``'general'``: shift, rotation, and scale

The default value is "rshift".

.. note::
Mathematically, alignment of images observed in different tangent planes
requires ``fitgeometry='general'`` in order to fit source catalogs
in the different images even if mis-alignment is caused only by a shift
or rotation in the tangent plane of one of the images.

However, under certain circumstances, such as small alignment errors or
minimal dithering during observations that keep tangent planes of the
images to be aligned almost parallel, then it may be more robust to
use a ``fitgeometry`` setting with fewer degrees of freedom such as
``'rshift'``, especially for "ill-conditioned" source catalogs such as
catalogs with very few sources, or large errors in source positions, or
sources placed along a line or bunched in a corner of the image (not
spread across/covering the entire image).

* ``nclip``: A non-negative integer number of clipping iterations
to use in the fit. (Default=3)

* ``sigma``: A positive `float` indicating the clipping limit, in sigma units,
used when performing fit. (Default=3.0)

**Absolute Astrometric fitting parameters:**

Parameters used for absolute astrometry to a reference catalog.
These parameters
are passed into the `~stcal.tweakreg.absolute_align` function.

* ``abs_refcat``: String indicating what astrometric catalog should be used.
Currently supported options: 'GAIADR1', 'GAIADR2', 'GAIADR3', a path to an existing
reference catalog, `None`, or `''`. See
`stcal.tweakreg.tweakreg.SINGLE_GROUP_REFCAT`
for an up-to-date list of supported built-in reference catalogs.

When ``abs_refcat`` is `None` or an empty string, alignment to the
absolute astrometry catalog will be turned off.
(Default= `''`)

* ``abs_minobj``: A positive `int` indicating minimum number of objects
acceptable for matching. (Default=15)

* ``abs_searchrad``: A `float` indicating the search radius in arcsec for
a match. It is recommended that a value larger than ``searchrad`` be used for
this parameter (e.g. 3 times larger) (Default=6.0)

* ``abs_use2dhist``: A boolean indicating whether to use 2D histogram to find
initial offset. It is strongly recommended setting this parameter to `True`.
Otherwise the initial guess for the offsets will be set to zero
(Default=True)

* ``abs_separation``: Minimum object separation in arcsec. It **must be** at
least ``sqrt(2)`` times larger than ``abs_tolerance``. (Default=1.0)

* ``abs_tolerance``: Matching tolerance for ``xyxymatch`` in arcsec.
(Default=0.7)

* ``abs_fitgeometry``: A `str` value indicating the type of affine
transformation to be considered when fitting catalogs. Allowed values:

- ``'shift'``: x/y shifts only
- ``'rshift'``: rotation and shifts
- ``'rscale'``: rotation and scale
- ``'general'``: shift, rotation, and scale

The default value is "rshift". Note that the same conditions/restrictions
that apply to ``fitgeometry`` also apply to ``abs_fitgeometry``.

* ``abs_nclip``: A non-negative integer number of clipping iterations
to use in the fit. (Default=3)

* ``abs_sigma``: A positive `float` indicating the clipping limit, in sigma
units, used when performing fit. (Default=3.0)

* ``save_abs_catalog``: A boolean specifying whether or not to write out the
astrometric catalog used for the fit as a separate product. (Default=False)

Further Documentation
---------------------
The underlying algorithms as well as formats of source catalogs are described
in more detail at

https://tweakwcs.readthedocs.io/en/latest/

.. currentmodule:: stcal.tweakreg.tweakreg

.. automodapi:: stcal.tweakreg.tweakreg
:noindex:
20 changes: 20 additions & 0 deletions docs/stcal/tweakreg/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. _tweakreg_step:

========
TweakReg
========

.. toctree::
:maxdepth: 2

description.rst

**Also See:**

.. toctree::
:maxdepth: 1

utils.rst
astrometric_utils.rst

.. automodapi:: stcal.tweakreg
11 changes: 11 additions & 0 deletions docs/stcal/tweakreg/utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=================
Utility Functions
=================

Currently, the ``utils`` module provides helpful functions for
manually applying corrections to an imaging WCS.

.. currentmodule:: stcal.tweakreg.utils

.. automodapi:: stcal.tweakreg.utils
:noindex:
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies = [
"opencv-python-headless >=4.6.0.66",
"asdf >=2.15.0",
"gwcs >= 0.18.1",
"tweakwcs >=0.8.6",
emolter marked this conversation as resolved.
Show resolved Hide resolved
emolter marked this conversation as resolved.
Show resolved Hide resolved
"requests >=2.22",
]
dynamic = [
"version",
Expand Down Expand Up @@ -194,6 +196,26 @@ convention = "numpy"
[tool.ruff.lint.flake8-annotations]
ignore-fully-untyped = true # Turn of annotation checking for fully untyped code

[tool.mypy]
python_version = "3.12"
warn_return_any = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = [
"astropy.*",
"gwcs.*",
"tweakwcs.*",
"stdatamodels.*",
"asdf.*",
"scipy.*",
# don't complain about the installed c parts of this library
"stcal.ramp_fitting.ols_cas22._fit",
"stcal.ramp_fitting.ols_cas22._jump",
"stcal.ramp_fitting.slope_fitter",
]
ignore_missing_imports = true

[tool.cython-lint]
max-line-length = 110

Expand Down
Loading
Loading