|
| 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 | +from __future__ import annotations |
| 22 | + |
| 23 | +__all__ = ("InterpolateDetectorMetricPlot",) |
| 24 | + |
| 25 | +import logging |
| 26 | +from typing import Mapping, Optional |
| 27 | + |
| 28 | +import matplotlib.pyplot as plt |
| 29 | +import numpy as np |
| 30 | +from lsst.pex.config import Field |
| 31 | +from matplotlib.figure import Figure |
| 32 | +from scipy.interpolate import CloughTocher2DInterpolator |
| 33 | + |
| 34 | +from ...interfaces import KeyedData, KeyedDataSchema, PlotAction, Vector |
| 35 | +from .plotUtils import addPlotInfo |
| 36 | + |
| 37 | +_LOG = logging.getLogger(__name__) |
| 38 | + |
| 39 | + |
| 40 | +class 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 | + |
| 70 | + interp = CloughTocher2DInterpolator(list(zip(data["x"], data["y"])), data["metricValues"]) |
| 71 | + Z = interp(meshgridX, meshgridY) |
| 72 | + |
| 73 | + fig, ax = plt.subplots(1, 1, figsize=(8, 6)) |
| 74 | + pc = ax.pcolormesh(X, Y, Z, shading="auto") |
| 75 | + ax.scatter(data["x"], data["y"], s=5, c="black") |
| 76 | + cbar = fig.colorbar(pc) |
| 77 | + cbar.set_label(self.zAxisLabel, rotation=270) |
| 78 | + ax.set_xlabel(self.xAxisLabel) |
| 79 | + ax.set_ylabel(self.yAxisLabel) |
| 80 | + ax.set_aspect("equal", "box") |
| 81 | + |
| 82 | + # add general plot info |
| 83 | + if plotInfo is not None: |
| 84 | + fig = addPlotInfo(fig, plotInfo) |
| 85 | + |
| 86 | + return fig |
0 commit comments