MaskedImage#
- class lsst.images.MaskedImage(image, *, mask=None, variance=None, mask_schema=None, sky_projection=None, metadata=None)#
Bases:
GeneralizedImageA multi-plane image with data (image), mask, and variance planes.
- Parameters:
image (
Image) – The main image plane. If this has aSkyProjection, it will be used for all planes unless asky_projectionis passed separately.mask (
Mask|None, default:None) – A bitmask image that annotates the main image plane. Must have the same bounding box asimageif provided. Any attachedsky_projectionis replaced (possibly byNone).variance (
Image|None, default:None) – The per-pixel uncertainty of the main image as an image of variance values. Must have the same bounding box asimageif provided, and its units must be the square ofimage.unitorNone. Values default to1.0. Any attachedsky_projectionis replaced (possibly byNone).mask_schema (
MaskSchema|None, default:None) – Schema for the mask plane. Must be provided if and only ifmaskis not provided.sky_projection (
Optional[SkyProjection[Any]], default:None) – Projection that maps the pixel grid to the sky.metadata (
dict[str,TypeAliasType] |None, default:None) – Arbitrary flexible metadata to associate with the image.
Attributes Summary
A proxy object for slicing a generalized image using absolute pixel coordinates.
An Astropy WCS for this image's pixel array.
The bounding box shared by all three image planes (
Box).The butler dataset reference for this image (
lsst.daf.butler.SerializedDatasetRef|None).The butler inputs and ID of the task quantum that produced this dataset (
lsst.daf.butler.DatasetProvenance|None).An Astropy FITS WCS for this image's pixel array.
The main image plane (
Image).A proxy object for slicing a generalized image using "local" or "array" pixel coordinates.
The mask plane (
Mask).Arbitrary flexible metadata associated with the image (
dict).The projection that maps the pixel grid to the sky (
SkyProjection|None).The units of the image plane (
astropy.units.Unit|None).The variance plane (
Image).The coordinates of the first pixel in the array (
YX[int]).Methods Summary
bbox_from_sky_circle(center, radius[, clip])Calculate the bounding box on this image corresponding to a circular region on the sky.
copy()Deep-copy the masked image and metadata.
from_hdu_list(hdu_list, *[, fits_wcs_frame])Reconstruct a
MaskedImagefrom a cut-downlsst.imagesFITS HDU list.from_legacy(legacy, *[, unit, plane_map, ...])Convert from an
lsst.afw.image.MaskedImageinstance.read(path, **kwargs)Read an instance of this class from a file.
read_legacy(uri, *[, preserve_quantization, ...])Read a FITS file written by
lsst.afw.image.MaskedImage.writeFits.serialize(archive)Serialize the masked image to an output archive.
to_legacy(*[, copy, plane_map])Convert to an
lsst.afw.image.MaskedImageinstance.write(path, **kwargs)Write this object to a file.
Attributes Documentation
- absolute#
A proxy object for slicing a generalized image using absolute pixel coordinates.
Notes
In this convention, the first row and column of the pixel grid is
bbox.start. A subimage and its parent image share the same absolute pixel coordinate system, and mostlsst.imagestypes (e.g.Box,SkyProjection,PointSpreadFunction) operate exclusively in this system.Note that
astropy.wcsandnumpy.ndarrayare not aware of thebbox.startoffset that defines tihs coordinates system; uselocalslicing for indices obtained from those.
- astropy_wcs#
An Astropy WCS for this image’s pixel array.
Notes
As expected for Astropy WCS objects, this defines pixel coordinates such that the first row and column in any associated arrays are
(0, 0), notbbox.start, as is the case forsky_projection.This object satisfies the
astropy.wcs.wcsapi.BaseHighLevelWCSandastropy.wcs.wcsapi.BaseLowLevelWCSinterfaces, but it is not anastropy.wcs.WCS(usefits_wcsfor that).
- butler_dataset#
The butler dataset reference for this image (
lsst.daf.butler.SerializedDatasetRef|None).
- butler_provenance#
The butler inputs and ID of the task quantum that produced this dataset (
lsst.daf.butler.DatasetProvenance|None).
- fits_wcs#
An Astropy FITS WCS for this image’s pixel array.
Notes
As expected for Astropy WCS objects, this defines pixel coordinates such that the first row and column in any associated arrays are
(0, 0), notbbox.start, as is the case forsky_projection.This may be an approximation or absent if
sky_projectionis not naturally representable as a FITS WCS.
- local#
A proxy object for slicing a generalized image using “local” or “array” pixel coordinates.
Notes
In this convention, the first row and column of the pixel grid is always at
(0, 0). This is also the convention used byastropy.wcsobjects. When a subimage is created from a parent image, its “local” coordinate system is offset from the coordinate systems of the parent image.Note that most
lsst.imagestypes (e.g.Box,SkyProjection,PointSpreadFunction) operate instead in “absolute” coordinates, which is shared by subimage and their parents.
- mask#
The mask plane (
Mask).Assigning a new
Mask(for example one returned byadd_planes) replaces the mask plane. The new mask must share this image’s bounding box; its sky projection is replaced with the image’s.
- metadata#
Arbitrary flexible metadata associated with the image (
dict).Notes
Metadata is shared with subimages and other views. It can be disconnected by reassigning to a copy explicitly:
image.metadata = image.metadata.copy()
- sky_projection#
The projection that maps the pixel grid to the sky (
SkyProjection|None).
- unit#
The units of the image plane (
astropy.units.Unit|None).
Methods Documentation
- bbox_from_sky_circle(center, radius, clip=False)#
Calculate the bounding box on this image corresponding to a circular region on the sky.
- Parameters:
center (
SkyCoord) – The center of the circle, as a scalarastropy.coordinates.SkyCoordin any frame.radius (
Angle) – Radius of the circle, as a scalarastropy.coordinates.Angle.clip (
bool, default:False) – IfTrue(Falseis default), clip pixel bounds when the circle extends outside the image. IfFalsean exception is raised if any part of the circle is off the image.
- Returns:
Bounding box enclosing the circle in this image’s pixel coordinates.
- Return type:
- Raises:
NotContainedError – Raised if the requested region is entirely off the image or if any part of the region is off the image and clipping is
False.ValueError – Raised if
centerorradiusis not scalar, or if this image has no sky projection.
- copy()#
Deep-copy the masked image and metadata.
- Return type:
- classmethod from_hdu_list(hdu_list, *, fits_wcs_frame=GeneralFrame(unit=Unit('pix'), frame_type='GENERAL'))#
Reconstruct a
MaskedImagefrom a cut-downlsst.imagesFITS HDU list.This assumes the
PRIMARY,IMAGE,MASK, andVARIANCEHDUs written for the masked-image cut-outs produced bydax_images_cutout: a reallsst.imagesfile with its JSON-tree, index, and any nested-archive HDUs dropped. The reconstructed object can be re-serialized as a normallsst.imagesfile (with schema and index) so it can be read with the fulllsst.imagesinfrastructure.- Parameters:
hdu_list (
HDUList) – HDU list withIMAGE,MASK, andVARIANCEextensions and a primary HDU.fits_wcs_frame (
Frame|None, default:GeneralFrame(unit=Unit("pix"), frame_type='GENERAL')) – Pixel-gridFramefor theSkyProjectionreconstructed from the FITS WCS. Defaults to a plain pixel frame; passNoneto skip attaching a projection.
- Returns:
The reconstructed masked image.
- Return type:
- Raises:
ValueError – Raised if the
MASKHDU has neitherMSKNnorMP_mask- plane cards, since the mask schema cannot then be reconstructed, or ifhdu_listcontains more than oneMASKHDU (multipleMASKextensions, distinguished byEXTVER, are not handled here and would otherwise be silently dropped).
Notes
Both mask-plane conventions are supported: the self-describing
MSKN/MSKM/MSKDcards written bylsst.images, and the legacylsst.afw.imageMP_*cards (as produced bydax_images_cutoutfrom afw-written images). Legacy masks are mapped to a new schema with the same plane-guessing used byread_legacy.Unlike
read_legacy, the legacyMP_*mask-plane cards are kept (not stripped) for backwards compatibility, since this path reconstructs a file that may still be read by legacy tooling. They are re-indexed to the reshuffled schema so eachMP_bit matches the plane’s position in the writtenMSKNlayout.The headers of the HDUs in
hdu_listare modified in place: the WCS and mask-schema cards interpreted here are stripped from the caller’s headers.
- static from_legacy(legacy, *, unit=None, plane_map=None, sky_projection=None)#
Convert from an
lsst.afw.image.MaskedImageinstance.- Parameters:
legacy (
TypeAliasType) – Anlsst.afw.image.MaskedImageinstance that will share image and variance (but not mask) pixel data with the returned object.plane_map (
Mapping[str,MaskPlane] |None, default:None) – A mapping from legacy mask plane name to the new plane name and description. If not provided, the right legacy mask plane will be guessed, but this can depend on which mask planes the legacy mask actually has set.sky_projection (
Optional[SkyProjection[Any]], default:None) – Projection from pixels to xky.
- Return type:
- classmethod read(path, **kwargs)#
Read an instance of this class from a file.
A thin convenience wrapper around
lsst.images.serialization.read_archivethat fixes the expected in-memory type to this class. The container format is inferred frompath’s extension.- Parameters:
path (
str|ParseResult|ResourcePath|Path) – File to read; convertible tolsst.resources.ResourcePath.**kwargs (
Any) – Forwarded toread_archive(e.g.bboxto read a subimage).
- Return type:
Self
- static read_legacy(uri, *, preserve_quantization=False, plane_map=None, component=None, fits_wcs_frame=None)#
Read a FITS file written by
lsst.afw.image.MaskedImage.writeFits.- Parameters:
uri (
str|ParseResult|ResourcePath|Path) – URI or file name.preserve_quantization (
bool, default:False) – IfTrue, ensure that writing the masked image back out again will exactly preserve quantization-compressed pixel values. This causes the image and variance plane arrays to be marked as read-only and stores the original binary table data for those planes in memory. If theMaskedImageis copied, the precompressed pixel values are not transferred to the copy.plane_map (
Mapping[str,MaskPlane] |None, default:None) – A mapping from legacy mask plane name to the new plane name and description. If not provided, the right legacy mask plane will be guessed, but this can depend on which mask planes the legacy mask actually has set.component (
Optional[Literal['image','mask','variance']], default:None) – A component to read instead of the full image.fits_wcs_frame (
Frame|None, default:None) – If notNoneand the HDU containing the image plane has a FITS WCS, attach aSkyProjectionto the returned masked image by converting that WCS. Whencomponentis one of"image","mask", or"variance", a FITS WCS from the component HDU is used instead (all three should have the same WCS).
- Return type:
- serialize(archive)#
Serialize the masked image to an output archive.
- Parameters:
archive (
OutputArchive[Any]) – Archive to write to.- Return type:
MaskedImageSerializationModel[Any]
- to_legacy(*, copy=None, plane_map=None)#
Convert to an
lsst.afw.image.MaskedImageinstance.- Parameters:
copy (
bool|None, default:None) – IfTrue, always copy the image and variance pixel data. IfFalse, return a view, and raiseTypeErrorif the pixel data is read-only (this is not supported by afw). IfNone, only copy if the pixel data is read-only. Mask pixel data is always copied.plane_map (
Mapping[str,MaskPlane] |None, default:None) – A mapping from legacy mask plane name to the new plane name and description.
- Return type:
TypeAliasType
- write(path, **kwargs)#
Write this object to a file.
A thin convenience wrapper around
lsst.images.serialization.write_archive. The container format is chosen frompath’s extension.- Parameters:
path (
str) – Destination file path. Must not already exist.**kwargs (
Any) – Forwarded towrite_archive(e.g.compression_optionsandcompression_seedfor FITS).
- Return type: