Coverage for python/lsst/analysis/tools/actions/vector/calcBinnedStats.py: 55%
56 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-07 04:48 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-07 04:48 -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
23__all__ = ("CalcBinnedStatsAction",)
25from functools import cached_property
26from typing import cast
28import numpy as np
29from lsst.pex.config import Field
30from lsst.pipe.tasks.configurableActions import ConfigurableActionField
32from ...interfaces import KeyedData, KeyedDataAction, KeyedDataSchema, Scalar, Vector
33from ..keyedData.summaryStatistics import SummaryStatisticAction
34from .selectors import RangeSelector
37class CalcBinnedStatsAction(KeyedDataAction):
38 key_vector = Field[str](doc="Vector on which to compute statistics")
39 name_prefix = Field[str](doc="Field name to append stat names to")
40 selector_range = ConfigurableActionField[RangeSelector](doc="Range selector")
42 def getInputSchema(self, **kwargs) -> KeyedDataSchema:
43 yield (self.key_vector, Vector)
44 yield from self.selector_range.getInputSchema()
46 def getOutputSchema(self) -> KeyedDataSchema:
47 return (
48 (self.name_mask, Vector),
49 (self.name_median, Scalar),
50 (self.name_sig_mad, Scalar),
51 (self.name_count, Scalar),
52 (self.name_select_maximum, Scalar),
53 (self.name_select_median, Scalar),
54 (self.name_select_minimum, Scalar),
55 ("range_maximum", Scalar),
56 ("range_minimum", Scalar),
57 )
59 @cached_property
60 def name_count(self):
61 return f"{self.name_prefix}_count"
63 @cached_property
64 def name_mask(self):
65 return f"{self.name_prefix}_mask"
67 @cached_property
68 def name_median(self):
69 return f"{self.name_prefix}_median"
71 @cached_property
72 def name_select_maximum(self):
73 return f"{self.name_prefix}_select_maximum"
75 @cached_property
76 def name_select_median(self):
77 return f"{self.name_prefix}_select_median"
79 @cached_property
80 def name_select_minimum(self):
81 return f"{self.name_prefix}_select_minimum"
83 @cached_property
84 def name_sigmaMad(self):
85 return f"{self.name_prefix}_sigmaMad"
87 def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
88 results = {}
89 mask = self.selector_range(data, **kwargs)
90 results[self.name_mask] = mask
91 kwargs["mask"] = mask
93 action = SummaryStatisticAction(vectorKey=self.key_vector)
94 # this is sad, but pex_config seems to have broken behavior that
95 # is dangerous to fix
96 action.setDefaults()
98 for name, value in action(data, **kwargs).items():
99 results[getattr(self, f"name_{name}")] = value
101 values = cast(Vector, data[self.selector_range.column][mask]) # type: ignore
102 results[self.name_select_maximum] = cast(Scalar, float(np.nanmax(values)))
103 results[self.name_select_median] = cast(Scalar, float(np.nanmedian(values)))
104 results[self.name_select_minimum] = cast(Scalar, float(np.nanmin(values)))
105 results["range_maximum"] = self.selector_range.maximum
106 results["range_minimum"] = self.selector_range.minimum
108 return results