Coverage for python/lsst/cbp/maskInfo.py: 29%

33 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-29 16:56 +0000

1# This file is part of cbp. 

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"""MaskInfo class: information about a CBP mask""" 

22 

23__all__ = ["MaskInfo"] 

24 

25from collections import OrderedDict 

26 

27from lsst.geom import Point2D 

28 

29 

30class MaskInfo: 

31 """Information about a CBP mask. 

32 

33 Parameters 

34 ---------- 

35 name : `str` 

36 Name of mask. 

37 defaultHole : `str` or `int` 

38 Name or index of default hole. 

39 holePositions : `iterable` of (`float`, `float`) 

40 Position of a fiducial point for each hole in the mask (x, y mm). 

41 holeNames : `iterable` of `str` or None 

42 Name of each hole in the mask, in the same order as 

43 ``holePositions``. 

44 If None then set name = str(index) for each hole. 

45 

46 Raises 

47 ------ 

48 ValueError 

49 If ``defaultHole`` is `None`. 

50 LookupError 

51 If ``defaultHole`` is not a valid name or index. 

52 ValueError 

53 If ``holeNames`` is not `None` and has a different length 

54 than ``holePositions``. 

55 

56 Notes 

57 ----- 

58 **Attributes** 

59 

60 name : `str` 

61 Name of mask. 

62 defaultBeam : `str` 

63 Name of default hole or beam. 

64 """ 

65 

66 def __init__(self, name, defaultHole, holePositions, holeNames=None): 

67 self.name = name 

68 if holeNames is not None: 

69 if len(holePositions) != len(holeNames): 

70 raise ValueError("Number of hole positions = {} != Number of hole names = {}".format( 

71 len(holePositions), len(holeNames) 

72 )) 

73 else: 

74 holeNames = [str(i) for i in range(len(holePositions))] 

75 self._holePosDict = OrderedDict() 

76 for holeName, holePos in zip(holeNames, holePositions): 

77 self._holePosDict[holeName] = Point2D(*holePos) 

78 # parse "defaultBeam" after "holes", so we can check the default 

79 if defaultHole is None: 

80 raise ValueError("defaultHole cannot be None") 

81 self.defaultBeam = self.asHoleName(defaultHole) 

82 

83 def asHoleName(self, hole): 

84 """Read a hole index, name or None as a name, and validate it. 

85 

86 Parameters 

87 ---------- 

88 hole : `int`, `str` or `None` 

89 If `None` then return the default beam. 

90 If an integer index then return the corresponding beam name. 

91 If a string then return unchanged. 

92 

93 Raises 

94 ------ 

95 `LookupError` if beam is an integer and is out of range 

96 or if beam is a string and the name is unknown. 

97 """ 

98 if hole is None: 

99 return self.defaultBeam 

100 if isinstance(hole, int): 

101 return [name for name in self._holePosDict][hole] 

102 if hole not in self._holePosDict: 

103 raise KeyError("Unknown name {!r}".format(hole)) 

104 return hole 

105 

106 def getHolePos(self, beam): 

107 """Return the position of a hole in focal plane x,y mm. 

108 

109 Parameters 

110 ---------- 

111 hole : `int`, `str` or `None` 

112 If `None` then use the default beam. 

113 If an integer index then use the corresponding beam name. 

114 """ 

115 return self._holePosDict[self.asHoleName(beam)] 

116 

117 @property 

118 def numHoles(self): 

119 """The number of holes (read only)""" 

120 return len(self._holePosDict) 

121 

122 @property 

123 def holeNames(self): 

124 """An iterable of hole names, in index order (read only)""" 

125 return self._holePosDict.keys()