Coverage for python / lsst / dax / apdb / recordIds.py: 70%

63 statements  

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

1# This file is part of dax_apdb. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ["DiaForcedSourceId", "DiaObjectId", "DiaSourceId"] 

25 

26from dataclasses import dataclass 

27from typing import Any 

28 

29 

30@dataclass(kw_only=True, slots=True) 

31class DiaObjectId: 

32 """Collection of data items identifying DIAObject records. 

33 

34 DIAObject primary key is ``diaObjectId``, in Cassandra the table is 

35 partitioned by spatial coordinates (``ra`` and ``dec``). 

36 """ 

37 

38 diaObjectId: int 

39 """ID of DIAObject record.""" 

40 

41 ra: float 

42 """DIAObject ra, in degrees. Not required to be exact, but needs to be 

43 close to the value in database record. 

44 """ 

45 

46 dec: float 

47 """DIAObject dec, in degrees. Not required to be exact, but needs to be 

48 close to the value in database record. 

49 """ 

50 

51 @classmethod 

52 def from_named_tuple(cls, named_tuple: Any) -> DiaObjectId: 

53 """Construct DiaObjectId from a named tuple. 

54 

55 Parameters 

56 ---------- 

57 named_tuple : 

58 Named tuple which includes the same attributes as this class, e.g. 

59 a tuple returned from ``pandas.DataFrame.itertuples()``. Any 

60 additional attributes are ignored. 

61 

62 Returns 

63 ------- 

64 object_id : `DiaObjectId` 

65 Instance of this class. 

66 """ 

67 # Input tuple most likely comes from Pandas DataFrame, in that case 

68 # items may have numpy types, need to convert them to Python types. 

69 return cls(diaObjectId=int(named_tuple.diaObjectId), ra=named_tuple.ra, dec=named_tuple.dec) 

70 

71 def __eq__(self, other: Any) -> bool: 

72 if isinstance(other, DiaObjectId): 

73 return self.diaObjectId == other.diaObjectId 

74 return NotImplemented 

75 

76 def __hash__(self) -> int: 

77 return hash(self.diaObjectId) 

78 

79 

80@dataclass(kw_only=True, slots=True) 

81class DiaSourceId: 

82 """Collection of data items identifying DIASource records. 

83 

84 DIASource primary key is ``diaSourceId``, in Cassandra the table is 

85 partitioned by both spatial coordinates (``ra`` and ``dec``) and time 

86 (``midpointMjdTai``). 

87 """ 

88 

89 diaSourceId: int 

90 """ID of DIASource record.""" 

91 

92 ra: float 

93 """DIASource ra, in degrees. Not required to be exact, but needs to be 

94 close to the value in database record. 

95 """ 

96 

97 dec: float 

98 """DIASource dec, in degrees. Not required to be exact, but needs to be 

99 close to the value in database record. 

100 """ 

101 

102 midpointMjdTai: float 

103 """DIASource midpointMjdTai, not required to be exact, but needs to be 

104 close to the value in database record. 

105 """ 

106 

107 @classmethod 

108 def from_named_tuple(cls, named_tuple: Any) -> DiaSourceId: 

109 """Construct DiaSourceId from a named tuple. 

110 

111 Parameters 

112 ---------- 

113 named_tuple : 

114 Named tuple which includes the same attributes as this class, e.g. 

115 a tuple returned from ``pandas.DataFrame.itertuples()``. Any 

116 additional attributes are ignored. 

117 

118 Returns 

119 ------- 

120 object_id : `DiaSourceId` 

121 Instance of this class. 

122 """ 

123 # Input tuple most likely comes from Pandas DataFrame, in that case 

124 # items may have numpy types, need to convert them to Python types. 

125 return cls( 

126 diaSourceId=int(named_tuple.diaSourceId), 

127 ra=named_tuple.ra, 

128 dec=named_tuple.dec, 

129 midpointMjdTai=named_tuple.midpointMjdTai, 

130 ) 

131 

132 def __eq__(self, other: Any) -> bool: 

133 if isinstance(other, DiaSourceId): 

134 return self.diaSourceId == other.diaSourceId 

135 return NotImplemented 

136 

137 def __hash__(self) -> int: 

138 return hash(self.diaSourceId) 

139 

140 

141@dataclass(kw_only=True, slots=True) 

142class DiaForcedSourceId: 

143 """Collection of data items identifying DIAForcedSource records. 

144 

145 DIAForcedSource primary key is ``(diaObjectId, visit, detector)``, in 

146 Cassandra the table is partitioned by both spatial coordinates (``ra`` and 

147 ``dec``) and time (``midpointMjdTai``). 

148 """ 

149 

150 diaObjectId: int 

151 """ID of parent DIAObject record.""" 

152 

153 visit: int 

154 """Visit ID.""" 

155 

156 detector: int 

157 """Detector ID.""" 

158 

159 ra: float 

160 """DIAForcedSource ra, in degrees. Not required to be exact, but needs to 

161 be close to the value in database record. 

162 """ 

163 

164 dec: float 

165 """DIAForcedSource dec, in degrees. Not required to be exact, but needs to 

166 be close to the value in database record. 

167 """ 

168 

169 midpointMjdTai: float 

170 """DIAForcedSource midpointMjdTai, not required to be exact, but needs to 

171 be close to the value in database record. 

172 """ 

173 

174 @classmethod 

175 def from_named_tuple(cls, named_tuple: Any) -> DiaForcedSourceId: 

176 """Construct DiaForcedSourceId from a named tuple. 

177 

178 Parameters 

179 ---------- 

180 named_tuple : 

181 Named tuple which includes the same attributes as this class, e.g. 

182 a tuple returned from ``pandas.DataFrame.itertuples()``. Any 

183 additional attributes are ignored. 

184 

185 Returns 

186 ------- 

187 object_id : `DiaForcedSourceId` 

188 Instance of this class. 

189 """ 

190 # Input tuple most likely comes from Pandas DataFrame, in that case 

191 # items may have numpy types, need to convert them to Python types. 

192 return cls( 

193 diaObjectId=int(named_tuple.diaObjectId), 

194 visit=int(named_tuple.visit), 

195 detector=int(named_tuple.detector), 

196 ra=named_tuple.ra, 

197 dec=named_tuple.dec, 

198 midpointMjdTai=named_tuple.midpointMjdTai, 

199 ) 

200 

201 def __eq__(self, other: Any) -> bool: 

202 if isinstance(other, DiaForcedSourceId): 

203 return ( 

204 self.diaObjectId == other.diaObjectId 

205 and self.visit == other.visit 

206 and self.detector == other.detector 

207 ) 

208 return NotImplemented 

209 

210 def __hash__(self) -> int: 

211 return hash((self.diaObjectId, self.visit, self.detector))