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

Source detection+PSF docs updates #984

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ scripts
source_detection
----------------

- Support for PSF fitting (optional) for accurate centroids. [#841]
- Support for PSF fitting (optional) for accurate centroids. [#841, #984]

- Save source catalog to a structured array. [#987]

Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def check_sphinx_version(expected_version):
"matplotlib": ("http://matplotlib.org/", None),
"gwcs": ("https://gwcs.readthedocs.io/en/latest/", None),
"astropy": ("https://docs.astropy.org/en/stable/", None),
"photutils": ("https://photutils.readthedocs.io/en/stable/", None),
"webbpsf": ("https://webbpsf.readthedocs.io/en/latest/", None),
}

if sys.version_info[0] == 2:
Expand Down
8 changes: 6 additions & 2 deletions docs/roman/source_detection/arguments.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Arguments
=========

The source detection fitting step has several arguments. These can be specified
by the user by passing them to the step in a Python session, or setting them
in a parameter file.

* ``--kernel_fwhm``: A parameter for DAOStarFinder: size of Gaussian kernel in
pixels. Default is 2.0.
pixels. By default the FWHM is assumed to be the diffraction
limited PSF, given the filter used for this observation.
* ``--sharplo``: A parameter for DAOStarFinder: lower bound for sharpness.
Default is 0.0.
* ``--sharphi``: A parameter for DAOStarFinder: upper bound for sharpness.
Expand Down Expand Up @@ -42,7 +44,7 @@ in a parameter file.
* ``--bkg_boxsize``: If using `calc_threshold_img` size of box in pixels for
2D background / threshold images and if using
calc_threshold_2d, the size of the box used when detecting
sources. Default is 3.
sources. Default is 9.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! 9 is pretty tiny too. I think we use 256 for the equivalent of this in other surveys!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This number will still need to be adjusted if there's a crowding, or the source is very bright, etc.

* ``--bkg_sigma``: If using `calc_threshold_img`, n sigma for sigma clipping
for background calculation. Default is 2.0.
* ``--bkg_filter_size``: If using `calc_threshold_img` or `calc_threshold_2d`,
Expand All @@ -55,3 +57,5 @@ in a parameter file.
will be saved as a numpy array with four dimensions.
In order, these represent source ID, x centroid
position, y centroid position, and flux.
* ``--fit_psf``: If True, fit a PSF model to each detected source for more precise
source centroids and fluxes.
33 changes: 27 additions & 6 deletions docs/roman/source_detection/description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ Description

The source detection step produces catalogs of point-like sources for use by the
Tweakreg step for image alignment. It uses DAOStarFinder to detect point sources
in the image.
in the image, with an option to subsequently fit PSF models to the detected
sources for more precise centroids and fluxes.

Detecting Sources
-----------------

Sources are detected using `~photutils.detection.DAOStarFinder` from
`photutils <https://photutils.readthedocs.io/en/stable/>`_, which is an
implementation of the method `DAOFIND` from
`Stetson (1987) <https://ui.adsabs.harvard.edu/abs/1987PASP...99..191S/abstract>`_.
The algorithm can be provided limits on the source flux, radius, roundness,
sharpness, and background.

PSF Fitting
-----------

Star finding algorithms like `~photutils.detection.DAOStarFinder` provide
approximate stellar centroids. More precise centroids may be inferred by
fitting model PSFs to the observations. Setting the SourceDetectionStep's
option `fit_psf` to True will generate model Roman PSFs with
`WebbPSF <https://webbpsf.readthedocs.io/en/latest/roman.html>`_, and fit
those models to each of the sources detected by
`~photutils.detection.DAOStarFinder`. More details are in :doc:`psf`.

Outputs / Returns
=================
-----------------

By default, the resulting source catalog will be temporarily attached to the
output ImageModel in the `meta.source_catalog.tweakreg_catalog` attribute as 4D
output ImageModel in the `meta.source_catalog.tweakreg_catalog` attribute as
numpy array representing, in order, source ID, x centroid position, y centroid
position, and flux. This catalog will then be deleted from the model in the
Tweakreg step.
Expand All @@ -28,7 +49,7 @@ only be saved if it does not contain an attached catalog - to do this, use the
separately.

Options for Thresholding
========================
------------------------

The DAOStarFinder routine detects point-like sources in an image that are above
a certain, specified floating point threshold. This step provides several options
Expand All @@ -44,10 +65,10 @@ threshold value for the entire image based on the sigma-clipped average
(mean, median, or mode) background level of the whole image.

Other Options
=============
-------------

Limiting maximum number of sources
----------------------------------
++++++++++++++++++++++++++++++++++

By default, all detected sources will be returned in the final output catalog.
If you wish to limit the number of sources, this can be done with the
Expand Down
3 changes: 3 additions & 0 deletions docs/roman/source_detection/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ Source Detection

description.rst
arguments.rst
psf.rst

.. automodapi:: romancal.source_detection

.. automodapi:: romancal.lib.psf
29 changes: 29 additions & 0 deletions docs/roman/source_detection/psf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PSF Fitting
===========

A few PSF fitting utilities are included to interface between observations
within Roman datamodels and methods within dependencies that generate and
fit PSF models to observations.

Create PSF models
-----------------

`~romancal.lib.psf.create_gridded_psf_model` computes a gridded PSF model for
a given detector using `~webbpsf.gridded_library.CreatePSFLibrary` from
`WebbPSF <https://webbpsf.readthedocs.io/>`_. The defaults are chosen to
balance more accurate PSF models with the cost of increased runtime. For
further reading on the WebbPSF approach to ePSFs, see the WebbPSF docs on
`Using PSF Grids <https://webbpsf.readthedocs.io/en/latest/psf_grids.html>`_.

Fit model PSFs to an ImageModel
-------------------------------

Once PSF models are generated, you can fit those models to observations
in an ImageModel with `~romancal.lib.psf.fit_psf_to_image_model` using
`photutils <https://photutils.readthedocs.io/en/stable/psf.html>`_.
By default the fits are done with `~photutils.psf.PSFPhotometry`, and
crowded fields may benefit from using `~photutils.psf.IterativePSFPhotometry`.
For neighboring sources that are near one another on the detector, grouping
the sources and fitting their PSFs simultaneously improves the fit quality.
Initial guesses for target centroids can be given or source
detection can be performed with, e.g., `~photutils.detection.DAOStarFinder`.
10 changes: 8 additions & 2 deletions romancal/lib/psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@

from romancal.lib.dqflags import pixel as roman_dq_flag_map

__all__ = [
"create_gridded_psf_model",
"fit_psf_to_image_model",
"dq_to_boolean_mask",
]

# set loggers to debug level by default:
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -68,7 +74,7 @@ def create_gridded_psf_model(
):
"""
Compute a gridded PSF model for one SCA via
`webbpsf.gridded_library.CreatePSFLibrary`.
`~webbpsf.gridded_library.CreatePSFLibrary`.

Parameters
----------
Expand Down Expand Up @@ -210,7 +216,7 @@ def fit_psf_to_image_model(
exclude_out_of_bounds=True,
):
"""
Fit PSF models to an ImageModel.
Fit PSF models to an ``ImageModel``.

Parameters
----------
Expand Down