Coverage for python / lsst / pipe / tasks / prettyPictureMaker / _functors / _bounds_remapper.py: 34%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-22 08:53 +0000

1# This file is part of pipe_tasks. 

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 

22from __future__ import annotations 

23 

24__all__ = ("BoundsRemapper",) 

25 

26import numpy as np 

27 

28from lsst.pipe.tasks.prettyPictureMaker.types import RGBImage 

29from lsst.pex.config.configurableActions import ConfigurableAction 

30from lsst.pex.config import Field 

31 

32 

33class BoundsRemapper(ConfigurableAction): 

34 """Remaps input images to a known range of values. 

35 

36 Often input images are not mapped to any defined range of values 

37 (for instance if they are in count units). This controls how the units of 

38 an image are mapped to a zero to one range by determining an upper 

39 bound. A copy of the input image is created before processing. 

40 """ 

41 

42 quant = Field[float]( 

43 doc=( 

44 "The maximum values of each of the three channels will be multiplied by this factor to " 

45 "determine the maximum flux of the image, values larger than this quantity will be clipped." 

46 ), 

47 default=0.8, 

48 ) 

49 absMax = Field[float]( 

50 doc="Instead of determining the maximum value from the image, use this fixed value instead", 

51 default=220, 

52 optional=True, 

53 ) 

54 

55 def __call__(self, img: RGBImage) -> RGBImage: 

56 """Bound images to a range between zero and one. 

57 

58 Some images supplied aren't properly bounded with a maximum value of 1. 

59 Either the images exceed the bounds of 1, or values are nowhere near 1, 

60 implying indeterminate maximum value. This function determines 

61 an appropriate maximum either by taking the value supplied in the 

62 absMax argument or by scaling the maximum across all channels with the 

63 supplied quant variable. 

64 

65 Parameters 

66 ---------- 

67 img : `RGBImage` 

68 Input RGB image array with dimensions (height, width, 3) in RGB order. 

69 

70 Returns 

71 ------- 

72 result : `RGBImage` 

73 The remapped image with values clipped to the range [0, 1]. 

74 """ 

75 if np.max(img) == 1: 

76 return img 

77 

78 r = img[:, :, 0] 

79 g = img[:, :, 1] 

80 b = img[:, :, 2] 

81 

82 if self.absMax is not None: 

83 scale = self.absMax 

84 else: 

85 r_quant = np.quantile(r, 0.95) 

86 g_quant = np.quantile(g, 0.95) 

87 b_quant = np.quantile(b, 0.95) 

88 turnover = np.max((r_quant, g_quant, b_quant)) 

89 scale = turnover * self.quant 

90 

91 image = np.copy(img) 

92 image /= scale 

93 

94 # Clip values that exceed the bound to ensure all values are within [0, absMax] 

95 return np.clip(image, 0, 1)