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

59 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-18 12:23 -0700

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, Point2D 

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 self._averagePosition = None 

44 

45 @property 

46 def piffResult(self): 

47 return self._piffResult 

48 

49 # Storable overrides 

50 

51 def isPersistable(self): 

52 return True 

53 

54 def _getPersistenceName(self): 

55 return "PiffPsf" 

56 

57 def _getPythonModule(self): 

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

59 

60 def _write(self): 

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

62 

63 @staticmethod 

64 def _read(pkl): 

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

66 

67 # ImagePsf overrides 

68 

69 def __deepcopy__(self, meta=None): 

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

71 

72 def resized(self, width, height): 

73 assert width == height 

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

75 

76 def _doComputeImage(self, position, color): 

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

78 

79 def _doComputeKernelImage(self, position, color): 

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

81 

82 def _doComputeBBox(self, position, color): 

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

84 

85 def getAveragePosition(self): 

86 if self._averagePosition is None: 

87 x = np.mean([star.field_pos.x for star in self._piffResult.stars]) 

88 y = np.mean([star.field_pos.y for star in self._piffResult.stars]) 

89 self._averagePosition = Point2D(x, y) 

90 return self._averagePosition 

91 

92 # Internal private methods 

93 

94 def _doImage(self, position, center): 

95 # Follow Piff conventions for center. 

96 # None => draw as if star at position 

97 # True => draw in center of image 

98 gsimg = self._piffResult.draw( 

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

100 ) 

101 bbox = self._doBBox(position, center) 

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

103 img.array[:] = gsimg.array 

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

105 return img 

106 

107 def _doBBox(self, position, center): 

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

109 if center is None: 

110 origin = Point2I(position) + origin 

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