Coverage for python/lsst/afw/image/image/multiband.py : 74%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
# This file is part of afw. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Multiband Pixel class
This represent acts as a container for a single pixel (scalar) in multiple bands.
Parameters ---------- singles: `sequence` Either a list of single band objects or an array of values. filters: `list` List of filter names. If `singles` is an `OrderedDict` or a `MultibandPixel` then this argument is ignored, otherwise it is required. position: `Point2I` Location of the pixel in the parent image. Unlike other objects that inherit from `MultibandBase`, `MultibandPixel` objects don't have a full `Box2I` bounding box, since they only contain a single pixel, so the bounding box cannot be inherited from the list of `singles`. """ err = "Expected an array of `singles`, a list of `filters, and a `bbox`" raise NotImplementedError(err)
# Make sure that singles is an array
# In this case we want self.singles to be an array
# Make sure that the bounding box has been setup properly
"""Data cube array in multiple bands
Since `self._singles` is just a 1D array, `array` just returns `self._singles`. """
"""Make a copy of the current instance
`MultibandPixel.singles` is an array, so this just makes a copy of the array (as opposed to a view of the parent array). """
"""Get a slice of the underlying array
Since a `MultibandPixel` is a scalar in the spatial dimensions, it can only be indexed with a filter name, number, or slice. """ err = ("Too many indices: " "`MultibandPixel has no spatial dimensions and " "only accepts a filterIndex") raise IndexError(err) # Make the index into a valid numpy index or slice # The user only requested a pixel in a single band, so return it
# No need to update the bounding box, since pixels can only be sliced in the filter dimension
pass
"""Multiband Image class
This class acts as a container for multiple `afw.Image` objects. All images must be contained in the same bounding box, and have the same data type. The data is stored in a 3D array (filters, y, x), and the single band `Image` instances have an internal array that points to the 3D multiband array, so that the single band objects and multiband array are always in agreement.
Parameters ---------- filters: `list` List of filter names. array: 3D numpy array Array (filters, y, x) of multiband data. If this is used to initialize a `MultibandImage`, either `bbox` or `singles` is also required. singleType: `type` Type of the single band object (eg. `Image`, `Mask`) to convert the array into a tuple of single band objects that point to the image array. bbox: `Box2I` Location of the array in a larger single band image. If `bbox` is `None` then the bounding box is initialized at the origin. """ # Create the single band objects from the array raise ValueError("`array` and `filters` must have the same length")
# Make sure that all of the parameters have been setup appropriately
"""Data cube array in multiple bands
Returns ------- self._array: array The resulting 3D data cube with shape (filters, y, x). """
"""Set the values of the array"""
"""Copy the current object
Parameters ---------- deep: `bool` Whether or not to make a deep copy """ else:
"""Slice the current object and return the result
See `MultibandBase._slice` for a list of the parameters. """
# Return a scalar or MultibandPixel # if the image indices are integers return array[0] singles=array, filters=filters, position=Point2I(sx + self.x0, sy + self.y0) ) else:
# Check that the image and array shapes agree len(result.filters), result.getBBox().getHeight(), result.getBBox().getWidth() )
"""Set a subset of the MultibandImage """ else:
# Return the single band object if the first # index is not a list or slice. else: self._array[filterIndex, sy, sx] = value.array else:
"""Bounding box """ raise ValueError("Unrecognized origin, expected either PARENT or LOCAL")
"""Construct a MultibandImage from a collection of single band images
Parameters ---------- filters: `list` List of filter names. singles: `list` A list of single band objects. If `array` is not `None`, then `singles` is ignored """ raise ValueError("Single band images did not all have the same bounding box")
"""Build a MultibandImage from a set of keyword arguments
Parameters ---------- filters: `list` List of filter names. singleType: class Class of the single band objects. This is ignored unless `singles` and `array` are both `None`, in which case it is required. filterKwargs: `dict` Keyword arguments to initialize a new instance of an inherited class that are different for each filter. The keys are the names of the arguments and the values should also be dictionaries, with filter names as keys and the value of the argument for a given filter as values. kwargs: `dict` Keyword arguments to initialize a new instance of an inherited class that are the same in all bands. """ # Attempt to load a set of images singles = [] for f in filters: if filterKwargs is not None: for key, value in filterKwargs: kwargs[key] = value[f] singles.append(singleType(**kwargs)) return cls.makeImageFromSingles(filters, singles)
"""Multiband Image class
See `MultibandImageBase` for a description of the parameters. """
def fromImages(filters, singles): """Construct a MultibandImage from a collection of single band images
see `fromSingles` for a description of parameters """
"""Build a MultibandImage from a set of keyword arguments
see `makeImageFromKwargs` for a description of parameters """ return makeImageFromKwargs(MultibandImage, filters, filterKwargs, singleType, **kwargs)
"""Multiband Mask class
See `MultibandImageBase` for a description of the parameters. """ # Set Mask specific properties
def fromMasks(filters, singles): """Construct a MultibandImage from a collection of single band images
see `fromSingles` for a description of parameters """
"""Build a MultibandImage from a set of keyword arguments
see `makeImageFromKwargs` for a description of parameters """ return makeImageFromKwargs(MultibandMask, filters, filterKwargs, singleType, **kwargs)
"""Get the bit number of a mask in the `MaskPlaneDict`
Each `key` in the mask plane has an associated bit value in the mask. This method returns the bit number of the `key` in the `MaskPlaneDict`. This is in contrast to `getPlaneBitMask`, which returns the value of the bit number.
For example, if `getMaskPlane` returns `8`, then `getPlaneBitMask` returns `256`.
Parameters ---------- key: `str` Name of the key in the `MaskPlaneDict`
Returns ------- bit: `int` Bit number for mask `key` """
"""Get the bit number of a mask in the `MaskPlaneDict`
Each `key` in the mask plane has an associated bit value in the mask. This method returns the bit number of the `key` in the `MaskPlaneDict`. This is in contrast to `getPlaneBitMask`, which returns the value of the bit number.
For example, if `getMaskPlane` returns `8`, then `getPlaneBitMask` returns `256`.
Parameters ---------- names: `str` or list of `str` Name of the key in the `MaskPlaneDict` or a list of keys. If multiple keys are used, the value returned is the integer value of the number with all of the bit values in `names`.
For example if `MaskPlaneDict("CR")=3` and `MaskPlaneDict("NO_DATA)=8`, then `getPlaneBitMask(("CR", "NO_DATA"))=264`
Returns ------- bit value: `int` Bit value for all of the combined bits described by `names`. """ return self._refMask.getPlaneBitMask(names)
"""Maximum number of mask planes available
This is required to be the same for all of the single band `Mask` objects. """ return self._refMask.getNumPlanesMax()
"""Number of mask planes used
This is required to be the same for all of the single band `Mask` objects. """
"""Dictionary of Mask Plane bit values """
def clearMaskPlaneDict(): """Reset the mask plane dictionary """ Mask[MaskPixel].clearMaskPlaneDict()
def addMaskPlane(name): """Add a mask to the mask plane
Parameters ---------- name: `str` Name of the new mask plane
Returns ------- index: `int` Bit value of the mask in the mask plane. """ idx = Mask[MaskPixel].addMaskPlane(name) return idx
def removeMaskPlane(name): """Remove a mask from the mask plane
Parameters ---------- name: `str` Name of the mask plane to remove """ Mask[MaskPixel].removeMaskPlaneDict()
"""Clear all the pixels """ self._refMask.clearAllMaskPlanes()
"""Check if two masks can be combined
This method checks that `self` and `others` have the same number of bands, or if others is a single value, creates a list to use for updating all of the `singles`. """ msg = "Both `MultibandMask` objects must have the same number of bands to combine" raise ValueError(msg) else: result = [others]*len(self.singles)
"""MultibandTripleBase class
This is a base class inherited by multiband classes with `image`, `mask`, and `variance` objects, such as `MultibandMaskedImage` and `MultibandExposure`.
Parameters ---------- filters: `list` List of filter names. If `singles` is an `OrderedDict` then this argument is ignored, otherwise it is required. image: `list` or `MultibandImage` List of `Image` objects that represent the image in each band or a `MultibandImage`. Ignored if `singles` is not `None`. mask: `list` or `MultibandMask` List of `Mask` objects that represent the mask in each bandor a `MultibandMask`. Ignored if `singles` is not `None`. variance: `list` or `MultibandImage` List of `Image` objects that represent the variance in each bandor a `MultibandImage`. Ignored if `singles` is not `None`. """ # Convert single band images into multiband images mask = MultibandMask.fromMasks(filters, mask) variance = MultibandImage.fromImages(filters, variance)
"""Shift the bounding box but keep the same Extent This is different than `MultibandBase.setXY0` because the multiband `image`, `mask`, and `variance` objects must all have their bounding boxes updated. Parameters ---------- xy0: `Point2I` New minimum bounds of the bounding box """
"""Shift the bounding box but keep the same Extent
This is different than `MultibandBase.shiftedTo` because the multiband `image`, `mask`, and `variance` objects must all have their bounding boxes updated.
Parameters ---------- xy0: `Point2I` New minimum bounds of the bounding box
Returns ------- result: `MultibandBase` A copy of the object, shifted to `xy0`. """ raise NotImplementedError("shiftedTo not implemented until DM-10781") result = self.clone(False) result._image = result.image.shiftedTo(xy0) if self.mask is not None: result._mask = result.mask.shiftedTo(xy0) if self.variance is not None: result._variance = result.variance.shiftedTo(xy0) result._bbox = result.image.getBBox() return result
"""Make a copy of the current instance """ else: mask = None else: variance = None
"""Slice the current object and return the result
See `Multiband._slice` for a list of the parameters. """ else: mask = None else: variance = None
# If only a single pixel is selected, return the tuple of MultibandPixels
"""Check that the new image, mask, or variance is valid
This basically means checking that the update to the property matches the current bounding box and inherits from the `MultibandBase` class. """ for prop in [image, mask, variance]: if prop is not None: if prop.getBBox() != self.getBBox(): raise ValueError("Bounding box does not match the current class") if not isinstance(prop, MultibandBase): err = "image, mask, and variance should all inherit from the MultibandBase class" raise ValueError(err)
def image(self): """The image of the MultibandMaskedImage"""
def mask(self): """The mask of the MultibandMaskedImage"""
def variance(self): """The variance of the MultibandMaskedImage"""
"""Bounding box """ elif origin == LOCAL: return Box2I(Point2I(0, 0), self._bbox.getDimensions()) raise ValueError("Unrecognized origin, expected either PARENT or LOCAL")
"""Construct a MultibandTriple from a collection of single band objects
Parameters ---------- filters: `list` List of filter names. singles: `list` A list of single band objects. If `array` is not `None`, then `singles` is ignored """ raise ValueError("Single band images did not all have the same bounding box")
"""Construct a MultibandTriple from a set of arrays
Parameters ---------- filters: `list` List of filter names. image: array Array of image values mask: array Array of mask values variance: array Array of variance values bbox: `Box2I` Location of the array in a larger single band image. This argument is ignored if `singles` is not `None`. """ bbox = Box2I(Point2I(0, 0), Extent2I(image.shape[1], image.shape[0])) mMask = MultibandMask(filters, mask, bbox) else: mVariance = MultibandImage(filters, variance, bbox) else:
"""Build a MultibandImage from a set of keyword arguments
Parameters ---------- filters: `list` List of filter names. singleType: `class` Class of the single band objects. This is ignored unless `singles` and `array` are both `None`, in which case it is required. filterKwargs: `dict` Keyword arguments to initialize a new instance of an inherited class that are different for each filter. The keys are the names of the arguments and the values should also be dictionaries, with filter names as keys and the value of the argument for a given filter as values. kwargs: `dict` Keyword arguments to initialize a new instance of an inherited class that are the same in all bands. """ # Attempt to load a set of images singles = [] for f in filters: if filterKwargs is not None: for key, value in filterKwargs: kwargs[key] = value[f] singles.append(singleType(**kwargs)) return tripleFromSingles(cls, filters, singles)
"""MultibandMaskedImage class
This class acts as a container for multiple `afw.MaskedImage` objects. All masked images must have the same bounding box, and the associated images must all have the same data type. The `image`, `mask`, and `variance` are all stored separately into a `MultibandImage`, `MultibandMask`, and `MultibandImage` respectively, which each have their own internal 3D arrays (filter, y, x).
See `MultibandTripleBase` for parameter definitions. """
def fromImages(filters, singles): """Construct a MultibandImage from a collection of single band images
see `tripleFromImages` for a description of parameters """
"""Construct a MultibandMaskedImage from a collection of arrays
see `tripleFromArrays` for a description of parameters """
"""Build a MultibandImage from a set of keyword arguments
see `makeTripleFromKwargs` for a description of parameters """ return makeTripleFromKwargs(MultibandMaskedImage, filters, filterKwargs, singleType, **kwargs)
"""Make a new list of single band objects
Parameters ---------- image: `MultibandImage` `MultibandImage` object that represent the image in each band. mask: `MultibandMask` `MultibandMask` object that represent the mask in each band. variance: `MultibandImage` `MultibandImage` object that represent the variance in each band.
Returns ------- singles: `tuple` Tuple of `MaskedImage` objects for each band, where the `image`, `mask`, and `variance` of each `single` point to the multiband objects. """ image = self.image
else: else: |