Coverage for python/lsst/meas/extensions/piff/piffPsf.py: 44%

52 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-05 18:24 -0800

1# This file is part of meas_extensions_piff. 

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 

22import pickle 

23import numpy as np 

24from lsst.afw.typehandling import StorableHelperFactory 

25from lsst.meas.algorithms import ImagePsf 

26from lsst.afw.image import Image 

27from lsst.geom import Box2I, Point2I, Extent2I 

28 

29 

30class PiffPsf(ImagePsf): 

31 _factory = StorableHelperFactory( 

32 "lsst.meas.extensions.piff.piffPsf", 

33 "PiffPsf" 

34 ) 

35 

36 def __init__(self, width, height, piffResult): 

37 assert width == height 

38 ImagePsf.__init__(self) 

39 self.width = width 

40 self.height = height 

41 self.dimensions = Extent2I(width, height) 

42 self._piffResult = piffResult 

43 

44 @property 

45 def piffResult(self): 

46 return self._piffResult 

47 

48 # Storable overrides 

49 

50 def isPersistable(self): 

51 return True 

52 

53 def _getPersistenceName(self): 

54 return "PiffPsf" 

55 

56 def _getPythonModule(self): 

57 return "lsst.meas.extensions.piff.piffPsf" 

58 

59 def _write(self): 

60 return pickle.dumps((self.width, self.height, self._piffResult)) 

61 

62 @staticmethod 

63 def _read(pkl): 

64 return PiffPsf(*pickle.loads(pkl)) 

65 

66 # ImagePsf overrides 

67 

68 def __deepcopy__(self, meta=None): 

69 return PiffPsf(self.width, self.height, self._piffResult) 

70 

71 def resized(self, width, height): 

72 assert width == height 

73 return PiffPsf(width, height, self._piffResult) 

74 

75 def _doComputeImage(self, position, color): 

76 return self._doImage(position, center=None) 

77 

78 def _doComputeKernelImage(self, position, color): 

79 return self._doImage(position, center=True) 

80 

81 def _doComputeBBox(self, position, color): 

82 return self._doBBox(Point2I(0, 0), center=True) 

83 

84 # Internal private methods 

85 

86 def _doImage(self, position, center): 

87 # Follow Piff conventions for center. 

88 # None => draw as if star at position 

89 # True => draw in center of image 

90 gsimg = self._piffResult.draw( 

91 position.x, position.y, stamp_size=self.width, center=center 

92 ) 

93 bbox = self._doBBox(position, center) 

94 img = Image(bbox, dtype=np.float64) 

95 img.array[:] = gsimg.array 

96 img.array /= np.sum(img.array) 

97 return img 

98 

99 def _doBBox(self, position, center): 

100 origin = -(self.dimensions//2) 

101 if center is None: 

102 origin = Point2I(position) + origin 

103 return Box2I(Point2I(origin), self.dimensions)