Coverage for python/lsst/analysis/tools/actions/keyedData/stellarLocusFit.py: 21%
56 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-18 03:12 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-18 03:12 -0800
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/>.
22from __future__ import annotations
24__all__ = ("StellarLocusFitAction",)
26from typing import cast
28import numpy as np
29from lsst.pex.config import DictField
31from ...interfaces import KeyedData, KeyedDataAction, KeyedDataSchema, Scalar, Vector
32from ...statistics import sigmaMad
33from ..plot.plotUtils import perpDistance, stellarLocusFit
36class StellarLocusFitAction(KeyedDataAction):
37 r"""Determine Stellar Locus fit parameters from given input `Vector`\ s."""
39 stellarLocusFitDict = DictField[str, float](
40 doc="The parameters to use for the stellar locus fit. The default parameters are examples and are "
41 "not useful for any of the fits. The dict needs to contain xMin/xMax/yMin/yMax which are the "
42 "limits of the initial box for fitting the stellar locus, mHW and bHW are the initial "
43 "intercept and gradient for the fitting.",
44 default={"xMin": 0.1, "xMax": 0.2, "yMin": 0.1, "yMax": 0.2, "mHW": 0.5, "bHW": 0.0},
45 )
47 def getInputSchema(self) -> KeyedDataSchema:
48 return (("x", Vector), ("y", Vector))
50 def getOutputSchema(self) -> KeyedDataSchema:
51 value = (
52 (f"{self.identity or ''}_sigmaMAD", Scalar),
53 (f"{self.identity or ''}_median", Scalar),
54 (f"{self.identity or ''}_hardwired_sigmaMAD", Scalar),
55 (f"{self.identity or ''}_hardwired_median", Scalar),
56 )
57 return value
59 def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
60 xs = cast(Vector, data["x"])
61 ys = cast(Vector, data["y"])
62 fitParams = stellarLocusFit(xs, ys, self.stellarLocusFitDict)
63 fitPoints = np.where(
64 (xs > fitParams["xMin"]) # type: ignore
65 & (xs < fitParams["xMax"]) # type: ignore
66 & (ys > fitParams["yMin"]) # type: ignore
67 & (ys < fitParams["yMax"]) # type: ignore
68 )[0]
70 if np.fabs(fitParams["mHW"]) > 1:
71 ysFitLineHW = np.array([fitParams["yMin"], fitParams["yMax"]])
72 xsFitLineHW = (ysFitLineHW - fitParams["bHW"]) / fitParams["mHW"]
73 ysFitLine = np.array([fitParams["yMin"], fitParams["yMax"]])
74 xsFitLine = (ysFitLine - fitParams["bODR"]) / fitParams["mODR"]
75 ysFitLine2 = np.array([fitParams["yMin"], fitParams["yMax"]])
76 xsFitLine2 = (ysFitLine2 - fitParams["bODR2"]) / fitParams["mODR2"]
78 else:
79 xsFitLineHW = np.array([fitParams["xMin"], fitParams["xMax"]])
80 ysFitLineHW = fitParams["mHW"] * xsFitLineHW + fitParams["bHW"]
81 xsFitLine = [fitParams["xMin"], fitParams["xMax"]]
82 ysFitLine = np.array(
83 [
84 fitParams["mODR"] * xsFitLine[0] + fitParams["bODR"],
85 fitParams["mODR"] * xsFitLine[1] + fitParams["bODR"],
86 ]
87 )
88 xsFitLine2 = [fitParams["xMin"], fitParams["xMax"]]
89 ysFitLine2 = np.array(
90 [
91 fitParams["mODR2"] * xsFitLine2[0] + fitParams["bODR2"],
92 fitParams["mODR2"] * xsFitLine2[1] + fitParams["bODR2"],
93 ]
94 )
96 # Calculate the distances to that line
97 # Need two points to characterise the lines we want
98 # to get the distances to
99 p1 = np.array([xsFitLine[0], ysFitLine[0]])
100 p2 = np.array([xsFitLine[1], ysFitLine[1]])
102 p1HW = np.array([xsFitLine[0], ysFitLineHW[0]])
103 p2HW = np.array([xsFitLine[1], ysFitLineHW[1]])
105 # Convert this to mmag
106 distsHW = np.array(perpDistance(p1HW, p2HW, zip(xs[fitPoints], ys[fitPoints]))) * 1000
107 dists = np.array(perpDistance(p1, p2, zip(xs[fitPoints], ys[fitPoints]))) * 1000
109 # Now we have the information for the perpendicular line we
110 # can use it to calculate the points at the ends of the
111 # perpendicular lines that intersect at the box edges
112 if np.fabs(fitParams["mHW"]) > 1:
113 xMid = (fitParams["yMin"] - fitParams["bODR2"]) / fitParams["mODR2"]
114 xs = np.array([xMid - 0.5, xMid, xMid + 0.5])
115 ys = fitParams["mPerp"] * xs + fitParams["bPerpMin"]
116 else:
117 xs = np.array([fitParams["xMin"] - 0.2, fitParams["xMin"], fitParams["xMin"] + 0.2])
118 ys = xs * fitParams["mPerp"] + fitParams["bPerpMin"]
120 if np.fabs(fitParams["mHW"]) > 1:
121 xMid = (fitParams["yMax"] - fitParams["bODR2"]) / fitParams["mODR2"]
122 xs = np.array([xMid - 0.5, xMid, xMid + 0.5])
123 ys = fitParams["mPerp"] * xs + fitParams["bPerpMax"]
124 else:
125 xs = np.array([fitParams["xMax"] - 0.2, fitParams["xMax"], fitParams["xMax"] + 0.2])
126 ys = xs * fitParams["mPerp"] + fitParams["bPerpMax"]
128 fitParams[f"{self.identity or ''}_sigmaMAD"] = sigmaMad(dists)
129 fitParams[f"{self.identity or ''}_median"] = np.median(dists)
130 fitParams[f"{self.identity or ''}_hardwired_sigmaMAD"] = sigmaMad(distsHW)
131 fitParams[f"{self.identity or ''}_hardwired_median"] = np.median(distsHW)
133 return fitParams # type: ignore