LSST Applications g0603fd7c41+501e3db9f9,g0aad566f14+23d8574c86,g0dd44d6229+a1a4c8b791,g2079a07aa2+86d27d4dc4,g2305ad1205+a62672bbc1,g2bbee38e9b+047b288a59,g337abbeb29+047b288a59,g33d1c0ed96+047b288a59,g3a166c0a6a+047b288a59,g3d1719c13e+23d8574c86,g487adcacf7+cb7fd919b2,g4be5004598+23d8574c86,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+4a9e435310,g63cd9335cc+585e252eca,g858d7b2824+23d8574c86,g88963caddf+0cb8e002cc,g99cad8db69+43388bcaec,g9ddcbc5298+9a081db1e4,ga1e77700b3+a912195c07,gae0086650b+585e252eca,gb0e22166c9+60f28cb32d,gb2522980b2+793639e996,gb3a676b8dc+b4feba26a1,gb4b16eec92+63f8520565,gba4ed39666+c2a2e4ac27,gbb8dafda3b+a5d255a82e,gc120e1dc64+d820f8acdb,gc28159a63d+047b288a59,gc3e9b769f7+f4f1cc6b50,gcf0d15dbbd+a1a4c8b791,gdaeeff99f8+f9a426f77a,gdb0af172c8+b6d5496702,ge79ae78c31+047b288a59,w.2024.19
LSST Data Management Base Package
Loading...
Searching...
No Matches
cloughTocher2DInterpolator.py
Go to the documentation of this file.
1# This file is part of meas_algorithms.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22__all__ = (
23 "CloughTocher2DInterpolateConfig",
24 "CloughTocher2DInterpolateTask",
25)
26
27
28from lsst.pex.config import Config, Field, ListField
29from lsst.pipe.base import Task
30from scipy.interpolate import CloughTocher2DInterpolator
31
32from . import CloughTocher2DInterpolatorUtils as ctUtils
33
34
36 """Config for CloughTocher2DInterpolateTask."""
37
38 badMaskPlanes = ListField[str](
39 doc="List of mask planes to interpolate over.",
40 default=["BAD", "SAT", "CR"],
41 )
42 fillValue = Field[float](
43 doc="Constant value to fill outside of the convex hull of the good "
44 "pixels. A long (longer than twice the ``interpLength``) streak of "
45 "bad pixels at an edge will be set to this value.",
46 default=0.0,
47 )
48 interpLength = Field[int](
49 doc="Maximum number of pixels away from a bad pixel to include in "
50 "building the interpolant. Must be greater than or equal to 1.",
51 default=4,
52 check=lambda x: x >= 1,
53 )
54
55
57 """Interpolated over bad pixels using CloughTocher2DInterpolator.
58
59 Pixels with mask bits set to any of those listed ``badMaskPlanes`` config
60 are considered bad and are interpolated over. All good (non-bad) pixels
61 within ``interpLength`` pixels of a bad pixel in either direction are used
62 to construct the interpolant. An extended streak of bad pixels at an edge,
63 longer than ``interpLength``, is set to `fillValue`` specified in config.
64 """
65
66 ConfigClass = CloughTocher2DInterpolateConfig
67 _DefaultName = "cloughTocher2DInterpolate"
68
69 def run(
70 self,
71 maskedImage,
72 badpix=None,
73 goodpix=None,
74 ):
75 """Interpolate over bad pixels in a masked image.
76
77 This modifies the ``image`` attribute of the ``maskedImage`` in place.
78 This method returns, and accepts, the coordinates of the bad pixels
79 that were interpolated over, and the coordinates and values of the
80 good pixels that were used to construct the interpolant. This avoids
81 having to search for the bad and the good pixels repeatedly when the
82 mask plane is shared among many images, as would be the case with
83 noise realizations.
84
85 Parameters
86 ----------
87 maskedImage : `~lsst.afw.image.MaskedImageF`
88 Image on which to perform interpolation (and modify in-place).
89 badpix: `numpy.ndarray`, optional
90 N x 3 numpy array, where N is the number of bad pixels.
91 The coordinates of the bad pixels to interpolate over in the first
92 two columns, and the pixel values (unused) in the third column.
93 If None, then the coordinates of the bad pixels are determined by
94 an exhaustive search over the image. If ``goodpix`` is not
95 provided, then this parameter is ignored.
96 goodpix: `numpy.ndarray`, optional
97 M x 3 numpy array, where M is the number of good pixels.
98 The first two columns are the coordinates of the good pixels around
99 ``badpix`` that must be included when constructing the interpolant.
100 the interpolant. The values are populated from the image plane of
101 the ``maskedImage`` and returned (provided values will be ignored).
102 If ``badpix`` is not provided, then this parameter is ignored.
103
104 Returns
105 -------
106 badpix: `numpy.ndarray`
107 N x 3 numpy array, where N is the number of bad pixels.
108 The coordinates of the bad pixels that were interpolated over are
109 in the first two columns, and the corresponding pixel values in the
110 third. If ``badpix`` was provided, this is the same as the input.
111 goodpix: `numpy.ndarray`
112 M x 3 numpy array, where M is the number of bad pixels.
113 The coordinates of the good pixels that were used to construct the
114 interpolant arein the first two columns, and the corresponding
115 pixel values in the third. If ``goodpix`` was provided, the first
116 two columns are same as the input, with the third column updated
117 with the pixel values from the image plane of the ``maskedImage``.
118 """
119
120 if badpix is None or goodpix is None:
121 badpix, goodpix = ctUtils.findGoodPixelsAroundBadPixels(
122 maskedImage,
123 self.config.badMaskPlanes,
124 buffer=self.config.interpLength,
125 )
126 else:
127 # Even if badpix and goodpix is provided, make sure to update
128 # the values of goodpix.
129 ctUtils.updateArrayFromImage(goodpix, maskedImage.image)
130
131 # Construct the interpolant with goodpix.
132 interpolator = CloughTocher2DInterpolator(
133 list(zip(goodpix[:, 0], goodpix[:, 1])),
134 goodpix[:, 2],
135 fill_value=self.config.fillValue,
136 )
137
138 # Compute the interpolated values at bad pixel locations.
139 badpix[:, 2] = interpolator(badpix[:, :2])
140
141 # Fill in the bad pixels.
142 ctUtils.updateImageFromArray(maskedImage.image, badpix)
143
144 return badpix, goodpix