Skip to content

Commit

Permalink
Assign colors for unknown mask planes automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertLuptonTheGood authored and leannep committed Oct 3, 2022
1 parent 47ad8d2 commit 7c6fe57
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion python/lsst/afw/display/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,12 @@ def getMaskPlaneColor(self, name=None):
if name is None:
return self._maskPlaneColors
else:
return self._maskPlaneColors.get(name)
color = self._maskPlaneColors.get(name)

if color is None:
color = self._defaultMaskPlaneColor.get(name)

return color

def setMaskTransparency(self, transparency=None, name=None):
"""Specify display's mask transparency (percent); or `None` to not set it when loading masks
Expand Down Expand Up @@ -509,6 +514,22 @@ def show(self):
"""
return self._impl._show()

def __addMissingMaskPlanes(self, mask):
"""Assign colours to any missing mask planes found in mask"""

maskPlanes = mask.getMaskPlaneDict()
nMaskPlanes = max(maskPlanes.values()) + 1

planes = {} # build inverse dictionary from mask plane index to name
for key in maskPlanes:
planes[maskPlanes[key]] = key

colorGenerator = self.display.maskColorGenerator(omitBW=True)
for p in range(nMaskPlanes):
name = planes[p] # ordered by plane index
if name not in self._defaultMaskPlaneColor:
self.setDefaultMaskPlaneColor(name, next(colorGenerator))

def mtv(self, data, title="", wcs=None):
"""Display an `~lsst.afw.image.Image` or `~lsst.afw.image.Mask` on a display
Expand Down Expand Up @@ -541,13 +562,15 @@ def mtv(self, data, title="", wcs=None):
self._impl._mtv(data, None, wcs, title)
# it's a Mask; display it, bitplane by bitplane
elif isinstance(data, afwImage.Mask):
self.__addMissingMaskPlanes(data)
#
# Some displays can't display a Mask without an image; so display an Image too,
# with pixel values set to the mask
#
self._impl._mtv(afwImage.ImageI(data.getArray()), data, wcs, title)
# it's a MaskedImage; display Image and overlay Mask
elif isinstance(data, afwImage.MaskedImage):
self.__addMissingMaskPlanes(data.mask)
self._impl._mtv(data.getImage(), data.getMask(), wcs, title)
else:
raise RuntimeError(f"Unsupported type {data!r}")
Expand Down

0 comments on commit 7c6fe57

Please sign in to comment.