Coverage for python/lsst/analysis/tools/actions/plot/interpolateDetectorPlot.py: 45%

49 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 03:58 -0700

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__ = ("InterpolateDetectorMetricPlot",) 

24 

25import logging 

26from typing import Mapping, Optional 

27 

28import matplotlib.pyplot as plt 

29import numpy as np 

30from lsst.pex.config import Field 

31from matplotlib.figure import Figure 

32from scipy.interpolate import CloughTocher2DInterpolator 

33 

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

35from .plotUtils import addPlotInfo 

36 

37_LOG = logging.getLogger(__name__) 

38 

39 

40class InterpolateDetectorMetricPlot(PlotAction): 

41 """Interpolate metrics evaluated at locations across a detector.""" 

42 

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

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

45 zAxisLabel = Field[str](doc="Label to use for the z axis.", optional=True) 

46 

47 xCoordSize = Field[int]("Dimensions for X direction field to interpolate", default=4000) 

48 yCoordSize = Field[int]("Dimensions for Y direction field to interpolate", default=4072) 

49 nGridPoints = Field[int]("N points in the grid for the field to interpolate", default=40) 

50 gridMargin = Field[int]("Grid margins for the field to interpolate", default=20) 

51 

52 def getInputSchema(self) -> KeyedDataSchema: 

53 base = [] 

54 

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

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

57 base.append(("metricValues", Vector)) 

58 

59 return base 

60 

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

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

63 

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

65 

66 X = np.linspace(-self.gridMargin, self.xCoordSize + self.gridMargin, self.nGridPoints) 

67 Y = np.linspace(-self.gridMargin, self.yCoordSize + self.gridMargin, self.nGridPoints) 

68 meshgridX, meshgridY = np.meshgrid(X, Y) # 2D grid for interpolation 

69 dataSelector = np.isfinite(data["metricValues"]) 

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

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

72 dataZ = data["metricValues"][dataSelector] 

73 

74 interp = CloughTocher2DInterpolator(list(zip(dataX, dataY)), dataZ) 

75 Z = interp(meshgridX, meshgridY) 

76 

77 fig, ax = plt.subplots(1, 1, figsize=(8, 6)) 

78 pc = ax.pcolormesh(X, Y, Z, shading="auto") 

79 ax.scatter(dataX, dataY, s=5, c="black") 

80 cbar = fig.colorbar(pc) 

81 cbar.set_label(self.zAxisLabel, rotation=270) 

82 ax.set_xlabel(self.xAxisLabel) 

83 ax.set_ylabel(self.yAxisLabel) 

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

85 

86 # add general plot info 

87 if plotInfo is not None: 

88 fig = addPlotInfo(fig, plotInfo) 

89 

90 return fig