Coverage for python/lsst/analysis/tools/interfaces/_interfaces.py: 94%

34 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-20 13:17 +0000

1# This file is part of analysis_tools. 

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 

22from __future__ import annotations 

23 

24__all__ = ( 

25 "Scalar", 

26 "ScalarType", 

27 "KeyedData", 

28 "KeyedDataTypes", 

29 "KeyedDataSchema", 

30 "Vector", 

31 "PlotTypes", 

32 "KeyedResults", 

33) 

34 

35from abc import ABCMeta 

36from numbers import Number 

37from typing import Any, Iterable, Mapping, MutableMapping, TypeAlias 

38 

39import numpy as np 

40from healsparse import HealSparseMap 

41from lsst.verify import Measurement 

42from matplotlib.figure import Figure 

43from numpy.typing import NDArray 

44 

45 

46class ScalarMeta(ABCMeta): 

47 def __instancecheck__(cls: ABCMeta, instance: Any) -> Any: 

48 return isinstance(instance, tuple(cls.mro()[1:])) 

49 

50 

51class Scalar(Number, np.number, metaclass=ScalarMeta): # type: ignore 

52 """This is an interface only class, and is intended to abstract around all 

53 the various types of numbers used in Python. 

54 

55 This has been tried many times with various levels of success in python, 

56 and this is another attempt. However, as this class is only intended as an 

57 interface, and not something concrete to use it works. 

58 

59 Users should not directly instantiate from this class, instead they should 

60 use a built in python number type, or a numpy number. 

61 """ 

62 

63 def __init__(self) -> None: 

64 raise NotImplementedError("Scalar is only an interface and should not be directly instantiated") 

65 

66 

67ScalarType = type[Scalar] 

68"""A type alias for the Scalar interface.""" 

69 

70Vector = NDArray 

71"""A Vector is an abstraction around the NDArray interface, things that 'quack' 

72like an NDArray should be considered a Vector. 

73""" 

74 

75KeyedData = MutableMapping[str, Vector | Scalar | HealSparseMap] 

76"""KeyedData is an interface where either a `Vector` or `Scalar` can be 

77retrieved using a key which is of str type. 

78""" 

79 

80KeyedDataTypes = MutableMapping[str, type[Vector] | ScalarType | type[HealSparseMap]] 

81r"""A mapping of str keys to the Types which are valid in `KeyedData` objects. 

82This is useful in conjunction with `AnalysisAction`\ 's ``getInputSchema`` and 

83``getOutputSchema`` methods. 

84""" 

85 

86KeyedDataSchema = Iterable[tuple[str, type[Vector] | ScalarType | type[HealSparseMap]]] 

87r"""An interface that represents a type returned by `AnalysisAction`\ 's 

88``getInputSchema`` and ``getOutputSchema`` methods. 

89""" 

90 

91PlotTypes = Figure 

92"""An interface that represents the various plot types analysis tools supports. 

93""" 

94 

95KeyedResults = Mapping[str, PlotTypes | Measurement] 

96"""A mapping of the return types for an analysisTool.""" 

97 

98MetricResultType: TypeAlias = Mapping[str, Measurement] | Measurement 

99"""A type alias for the return type of a MetricAction.""" 

100 

101PlotResultType: TypeAlias = Mapping[str, PlotTypes] | PlotTypes 

102"""A type alias for the return type of a PlotAction."""