Coverage for python/lsst/afw/image/utils.py : 14%

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
1#
2# LSST Data Management System
3# Copyright 2008-2017 LSST/AURA.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
23__all__ = ["clipImage", "resetFilters", "defineFilter",
24 "projectImage", "getProjectionIndices"]
26from deprecated.sphinx import deprecated
28import numpy as np
30import lsst.afw.detection as afwDetect
31from .maskedImage import MaskedImage, makeMaskedImage
32from .image import Mask
33from .filter import Filter, FilterProperty
36def clipImage(im, minClip, maxClip):
37 """Clip an image to lie between minClip and maxclip (None to ignore)"""
38 if isinstance(im, MaskedImage):
39 mi = im
40 else:
41 mi = makeMaskedImage(im, Mask(im.getDimensions()))
43 if minClip is not None:
44 ds = afwDetect.FootprintSet(
45 mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
46 afwDetect.setImageFromFootprintList(
47 mi.getImage(), ds.getFootprints(), minClip)
49 if maxClip is not None:
50 ds = afwDetect.FootprintSet(mi, afwDetect.Threshold(maxClip))
51 afwDetect.setImageFromFootprintList(
52 mi.getImage(), ds.getFootprints(), maxClip)
55@deprecated(reason=("Removed with no replacement (FilterLabels do not need to be reset)."
56 " Will be removed after v22."), category=FutureWarning, version="v22")
57def resetFilters():
58 """Reset registry of filters and filter properties"""
59 Filter.reset()
60 FilterProperty.reset()
63@deprecated(reason=("Removed with no replacement (but see lsst::afw::image::TransmissionCurve for how to set"
64 "and retrieve filter wavelength information). Will be removed after v22."),
65 category=FutureWarning, version="v22")
66def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
67 """Define a filter and its properties in the filter registry"""
68 prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
69 Filter.define(prop)
70 if isinstance(alias, str):
71 Filter.defineAlias(name, alias)
72 else:
73 for a in alias:
74 Filter.defineAlias(name, a)
77def getProjectionIndices(imageBBox, targetBBox):
78 """Get the indices to project an image
80 Given an image and target bounding box,
81 calculate the indices needed to appropriately
82 slice the input image and target image to
83 project the image to the target.
85 Parameters
86 ----------
87 imageBBox: Box2I
88 Bounding box of the input image
89 targetBBox: Box2I
90 Bounding box of the target image
92 Returns
93 -------
94 targetSlices: tuple
95 Slices of the target image in the form (by, bx), (iy, ix).
96 imageIndices: tuple
97 Slices of the input image in the form (by, bx), (iy, ix).
98 """
99 def getMin(dXmin):
100 """Get minimum indices"""
101 if dXmin < 0:
102 bxStart = -dXmin
103 ixStart = 0
104 else:
105 bxStart = 0
106 ixStart = dXmin
107 return bxStart, ixStart
109 def getMax(dXmax):
110 """Get maximum indices"""
111 if dXmax < 0:
112 bxEnd = None
113 ixEnd = dXmax
114 elif dXmax != 0:
115 bxEnd = -dXmax
116 ixEnd = None
117 else:
118 bxEnd = ixEnd = None
119 return bxEnd, ixEnd
121 dXmin = targetBBox.getMinX() - imageBBox.getMinX()
122 dXmax = targetBBox.getMaxX() - imageBBox.getMaxX()
123 dYmin = targetBBox.getMinY() - imageBBox.getMinY()
124 dYmax = targetBBox.getMaxY() - imageBBox.getMaxY()
126 bxStart, ixStart = getMin(dXmin)
127 byStart, iyStart = getMin(dYmin)
128 bxEnd, ixEnd = getMax(dXmax)
129 byEnd, iyEnd = getMax(dYmax)
131 bx = slice(bxStart, bxEnd)
132 by = slice(byStart, byEnd)
133 ix = slice(ixStart, ixEnd)
134 iy = slice(iyStart, iyEnd)
135 return (by, bx), (iy, ix)
138def projectImage(image, bbox, fill=0):
139 """Project an image into a bounding box
141 Return a new image whose pixels are equal to those of
142 `image` within `bbox`, and equal to `fill` outside.
144 Parameters
145 ----------
146 image: `afw.Image` or `afw.MaskedImage`
147 The image to project
148 bbox: `Box2I`
149 The bounding box to project onto.
150 fill: number
151 The value to fill the region of the new
152 image outside the bounding box of the original.
154 Returns
155 -------
156 newImage: `afw.Image` or `afw.MaskedImage`
157 The new image with the input image projected
158 into its bounding box.
159 """
160 if image.getBBox() == bbox:
161 return image
162 (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox)
164 if isinstance(image, MaskedImage):
165 newImage = type(image.image)(bbox)
166 newImage.array[by, bx] = image.image.array[iy, ix]
167 newMask = type(image.mask)(bbox)
168 newMask.array[by, bx] = image.mask.array[iy, ix]
169 newVariance = type(image.image)(bbox)
170 newVariance.array[by, bx] = image.variance.array[iy, ix]
171 newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype)
172 else:
173 newImage = type(image)(bbox)
174 if fill != 0:
175 newImage.set(fill)
176 newImage.array[by, bx] = image.array[iy, ix]
177 return newImage