Coverage for python / lsst / analysis / tools / actions / plot / quiverPlot.py: 33%

60 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-07 08:53 +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/>. 

21from __future__ import annotations 

22 

23__all__ = ("QuiverPlot",) 

24 

25import logging 

26from collections.abc import Mapping 

27 

28import matplotlib.pyplot as plt 

29import numpy as np 

30from matplotlib.figure import Figure 

31 

32from lsst.pex.config import Field 

33 

34from ...interfaces import KeyedData, KeyedDataSchema, PlotAction, Scalar, Vector 

35from .plotUtils import addPlotInfo 

36 

37_LOG = logging.getLogger(__name__) 

38 

39 

40class QuiverPlot(PlotAction): 

41 """Plots vectors on the detector focal plane. 

42 

43 Given the posisions on the detector in x and y, the quiver 

44 plot draws arrows of length `length` and angle `angle`. 

45 The quiver key label `qKeyLabel` and size `qKeySize` can be also set 

46 up to show reference vector length. In a similar manner as in 

47 interpolateDetectorPlot the size of the 

48 """ 

49 

50 xAxisLabel = Field[str](doc="Label to use for the x axis.", default="x (pixel)", optional=True) 

51 yAxisLabel = Field[str](doc="Label to use for the y axis.", default="y (pixel)", optional=True) 

52 zAxisLabel = Field[str](doc="Label to use for the arrows.", optional=True) 

53 qKeyLabel = Field[str](doc="Label to use for the optional quiver Key", optional=True) 

54 qKeySize = Field[float](doc="Size of the vector to use for the optional quiver Key", optional=True) 

55 xCoordSize = Field[int]("Dimensions for X detector axis", default=4096) 

56 yCoordSize = Field[int]("Dimensions for Y detector axis", default=4096) 

57 

58 def getInputSchema(self, **kwargs) -> KeyedDataSchema: 

59 base = [] 

60 base.append(("x", Vector)) 

61 base.append(("y", Vector)) 

62 base.append(("angle", Vector)) 

63 base.append(("length", Vector)) 

64 

65 return base 

66 

67 def _validateInput(self, data: KeyedData, **kwargs) -> None: 

68 """NOTE currently can only check that something is not a Scalar, not 

69 check that the data is consistent with Vector 

70 """ 

71 needed = self.getInputSchema(**kwargs) 

72 if remainder := {key.format(**kwargs) for key, _ in needed} - { 

73 key.format(**kwargs) for key in data.keys() 

74 }: 

75 raise ValueError(f"Task needs keys {remainder} but they were not found in input") 

76 for name, typ in needed: 

77 isScalar = issubclass((colType := type(data[name.format(**kwargs)])), Scalar) 

78 if isScalar and typ != Scalar: 

79 raise ValueError(f"Data keyed by {name} has type {colType} but action requires type {typ}") 

80 

81 def __call__(self, data: KeyedData, **kwargs) -> Mapping[str, Figure] | Figure: 

82 self._validateInput(data, **kwargs) 

83 return self.makePlot(data, **kwargs) 

84 

85 def makePlot(self, data: KeyedData, plotInfo: Mapping[str, str] | None = None, **kwargs) -> Figure: 

86 

87 quiverConf = { 

88 "pivot": "mid", 

89 "color": "blue", 

90 "width": 0.004, 

91 } 

92 

93 dataSelector = np.isfinite(data["angle"]) & np.isfinite(data["length"]) 

94 dataX = data["x"][dataSelector] 

95 dataY = data["y"][dataSelector] 

96 dataA = data["angle"][dataSelector] 

97 dataL = data["length"][dataSelector] 

98 

99 U = dataL * np.cos(dataA) 

100 V = dataL * np.sin(dataA) 

101 

102 fig = plt.figure(dpi=300) 

103 ax = fig.add_subplot(111) 

104 

105 q = ax.quiver(dataX, dataY, U, V, **quiverConf) 

106 if hasattr(self, "qKeyLabel") and hasattr(self, "qKeySize"): 

107 ax.quiverkey(q, 0.5, 0.9, self.qKeySize, self.qKeyLabel, labelpos="E", coordinates="figure") 

108 

109 ax.set_xlim(-10, self.xCoordSize + 10) 

110 ax.set_ylim(-10, self.yCoordSize + 10) 

111 ax.set_xlabel(self.xAxisLabel) 

112 ax.set_ylabel(self.yAxisLabel) 

113 ax.set_aspect("equal", "box") 

114 

115 plt.subplots_adjust(wspace=0.0, hspace=0.0, right=0.85) 

116 

117 # add general plot info 

118 if plotInfo is not None: 

119 fig = addPlotInfo(fig, plotInfo) 

120 

121 return fig