Coverage for python/lsst/daf/butler/registry/dimensions/skypix.py: 86%

34 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-21 09:54 +0000

1# This file is part of daf_butler. 

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/>. 

21from __future__ import annotations 

22 

23__all__ = ["BasicSkyPixDimensionRecordStorage"] 

24 

25from typing import TYPE_CHECKING 

26 

27import sqlalchemy 

28from lsst.daf.relation import Calculation, ColumnExpression, Join, Relation 

29 

30from ...core import ( 

31 DataCoordinate, 

32 DimensionKeyColumnTag, 

33 DimensionRecord, 

34 DimensionRecordColumnTag, 

35 SkyPixDimension, 

36) 

37from ..interfaces import SkyPixDimensionRecordStorage 

38 

39if TYPE_CHECKING: 

40 from .. import queries 

41 

42 

43class BasicSkyPixDimensionRecordStorage(SkyPixDimensionRecordStorage): 

44 """A storage implementation specialized for `SkyPixDimension` records. 

45 

46 `SkyPixDimension` records are never stored in a database, but are instead 

47 generated-on-the-fly from a `sphgeom.Pixelization` instance. 

48 

49 Parameters 

50 ---------- 

51 dimension : `SkyPixDimension` 

52 The dimension for which this instance will simulate storage. 

53 """ 

54 

55 def __init__(self, dimension: SkyPixDimension): 

56 self._dimension = dimension 

57 

58 @property 

59 def element(self) -> SkyPixDimension: 

60 # Docstring inherited from DimensionRecordStorage.element. 

61 return self._dimension 

62 

63 def clearCaches(self) -> None: 

64 # Docstring inherited from DimensionRecordStorage.clearCaches. 

65 pass 

66 

67 def join_results_postprocessed(self) -> bool: 

68 # Docstring inherited. 

69 return True 

70 

71 def join( 

72 self, 

73 relation: Relation, 

74 join: Join, 

75 context: queries.SqlQueryContext, 

76 ) -> Relation: 

77 # Docstring inherited. 

78 assert join.predicate.as_trivial(), "Expected trivial join predicate for skypix relation." 

79 id_column = DimensionKeyColumnTag(self._dimension.name) 

80 assert id_column in relation.columns, "Guaranteed by QueryBuilder.make_dimension_relation." 

81 function_name = f"{self._dimension.name}_region" 

82 context.iteration_engine.functions[function_name] = self._dimension.pixelization.pixel 

83 calculation = Calculation( 

84 tag=DimensionRecordColumnTag(self._dimension.name, "region"), 

85 expression=ColumnExpression.function(function_name, ColumnExpression.reference(id_column)), 

86 ) 

87 return calculation.apply( 

88 relation, preferred_engine=context.iteration_engine, transfer=True, backtrack=True 

89 ) 

90 

91 def insert(self, *records: DimensionRecord, replace: bool = False, skip_existing: bool = False) -> None: 

92 # Docstring inherited from DimensionRecordStorage.insert. 

93 raise TypeError(f"Cannot insert into SkyPix dimension {self._dimension.name}.") 

94 

95 def sync(self, record: DimensionRecord, update: bool = False) -> bool: 

96 # Docstring inherited from DimensionRecordStorage.sync. 

97 raise TypeError(f"Cannot sync SkyPixdimension {self._dimension.name}.") 

98 

99 def fetch_one(self, data_id: DataCoordinate, context: queries.SqlQueryContext) -> DimensionRecord: 

100 # Docstring inherited from DimensionRecordStorage. 

101 index = data_id[self._dimension.name] 

102 return self._dimension.RecordClass(id=index, region=self._dimension.pixelization.pixel(index)) 

103 

104 def digestTables(self) -> list[sqlalchemy.schema.Table]: 

105 # Docstring inherited from DimensionRecordStorage.digestTables. 

106 return []