|
| 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__ = ("FractionFoundFakesDiaSnrMetric", "FractionFoundFakesDiaMagMetric") |
| 24 | + |
| 25 | +import numpy as np |
| 26 | +from lsst.pex.config import Field |
| 27 | + |
| 28 | +from ..actions.scalar import CountAction, FracThreshold |
| 29 | +from ..actions.vector import DownselectVector, RangeSelector |
| 30 | +from ..interfaces import AnalysisTool |
| 31 | + |
| 32 | + |
| 33 | +class FractionFoundFakesDiaSnrMetric(AnalysisTool): |
| 34 | + """Calculate the fraction of fake DIA Sources found within the |
| 35 | + given SNR range""" |
| 36 | + |
| 37 | + parameterizedBand: bool = False |
| 38 | + |
| 39 | + snrMin = Field[float](doc="Minimum SNR for fake sources metric calculation.", default=0) |
| 40 | + |
| 41 | + snrMax = Field[float](doc="Maximum SNR for fake sources metric calculation.", default=np.inf) |
| 42 | + |
| 43 | + fluxType = Field[str]( |
| 44 | + "Flux type for fake sources metric calculation.", default="forced_base_PsfFlux_instFlux" |
| 45 | + ) |
| 46 | + |
| 47 | + def setDefaults(self): |
| 48 | + super().setDefaults() |
| 49 | + |
| 50 | + def finalize(self): |
| 51 | + # There is no need to calculate the SNR as it is already estimated |
| 52 | + # Select the fake sources using their SNR values for the given range |
| 53 | + # and flux type estimation |
| 54 | + self.process.filterActions.fakeSourcesDiaSrcId = DownselectVector( |
| 55 | + vectorKey="diaSourceId", |
| 56 | + selector=RangeSelector( |
| 57 | + vectorKey=f"{self.fluxType}_SNR", maximum=self.snrMax, minimum=self.snrMin |
| 58 | + ), |
| 59 | + ) |
| 60 | + |
| 61 | + self.process.calculateActions.numTotalFakeSources = CountAction(vectorKey="fakeSourcesDiaSrcId") |
| 62 | + |
| 63 | + self.process.calculateActions.numFoundFakeSources = CountAction( |
| 64 | + vectorKey="fakeSourcesDiaSrcId", op="gt", threshold=0 |
| 65 | + ) |
| 66 | + self.process.calculateActions.fractionFoundFakesDiaAll = FracThreshold( |
| 67 | + vectorKey="fakeSourcesDiaSrcId", op="gt", threshold=0 |
| 68 | + ) |
| 69 | + # the units for the quantity (count, an astropy quantity) |
| 70 | + self.produce.metric.units = { |
| 71 | + "numTotalFakeSources": "ct", |
| 72 | + "numFoundFakeSources": "ct", |
| 73 | + "fractionFoundFakesDiaAll": "", |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +class FractionFoundFakesDiaMagMetric(AnalysisTool): |
| 78 | + """Calculate the fraction of fake DIA Sources found within the given |
| 79 | + magnitude range""" |
| 80 | + |
| 81 | + parameterizedBand: bool = False |
| 82 | + |
| 83 | + magMin = Field[float](doc="Minimum magnitude for fake sources metric calculation.", default=18) |
| 84 | + |
| 85 | + magMax = Field[float](doc="Maximum magnitude for fake sources metric calculation.", default=22) |
| 86 | + |
| 87 | + def finalize(self): |
| 88 | + # Selecting the fake sources using the truth magnitude values. |
| 89 | + self.process.filterActions.fakeSourcesDiaSrcId = DownselectVector( |
| 90 | + vectorKey="diaSourceId", |
| 91 | + selector=RangeSelector(vectorKey="mag", maximum=self.magMax, minimum=self.magMin), |
| 92 | + ) |
| 93 | + |
| 94 | + self.process.calculateActions.numTotalFakeSources = CountAction(vectorKey="fakeSourcesDiaSrcId") |
| 95 | + |
| 96 | + self.process.calculateActions.numFoundFakeSources = CountAction( |
| 97 | + vectorKey="fakeSourcesDiaSrcId", op="gt", threshold=0 |
| 98 | + ) |
| 99 | + self.process.calculateActions.fractionFoundFakesDiaAll = FracThreshold( |
| 100 | + vectorKey="fakeSourcesDiaSrcId", op="gt", threshold=0 |
| 101 | + ) |
| 102 | + # the units for the quantity (count, an astropy quantity) |
| 103 | + self.produce.metric.units = { |
| 104 | + "numTotalFakeSources": "ct", |
| 105 | + "numFoundFakeSources": "ct", |
| 106 | + "fractionFoundFakesDiaAll": "", |
| 107 | + } |
0 commit comments