Coverage for python/lsst/analysis/tools/actions/vector/calcBinnedStats.py: 53%
58 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-12 03:16 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-12 03:16 -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/>.
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 name_suffix = Field[str](doc="Field name to append to stat names")
41 selector_range = ConfigurableActionField[RangeSelector](doc="Range selector")
43 def getInputSchema(self, **kwargs) -> KeyedDataSchema:
44 yield (self.key_vector, Vector)
45 yield from self.selector_range.getInputSchema()
47 def getOutputSchema(self) -> KeyedDataSchema:
48 return (
49 (self.name_mask, Vector),
50 (self.name_median, Scalar),
51 (self.name_sig_mad, Scalar),
52 (self.name_count, Scalar),
53 (self.name_select_maximum, Scalar),
54 (self.name_select_median, Scalar),
55 (self.name_select_minimum, Scalar),
56 ("range_maximum", Scalar),
57 ("range_minimum", Scalar),
58 )
60 @cached_property
61 def name_count(self):
62 return f"{self.name_prefix}count{self.name_suffix}"
64 @cached_property
65 def name_mask(self):
66 return f"{self.name_prefix}mask{self.name_suffix}"
68 @cached_property
69 def name_median(self):
70 return f"{self.name_prefix}median{self.name_suffix}"
72 @cached_property
73 def name_select_maximum(self):
74 return f"{self.name_prefix}select_maximum{self.name_suffix}"
76 @cached_property
77 def name_select_median(self):
78 return f"{self.name_prefix}select_median{self.name_suffix}"
80 @cached_property
81 def name_select_minimum(self):
82 return f"{self.name_prefix}select_minimum{self.name_suffix}"
84 @cached_property
85 def name_sigmaMad(self):
86 return f"{self.name_prefix}sigmaMad{self.name_suffix}"
88 def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
89 results = {}
90 mask = self.selector_range(data, **kwargs)
91 results[self.name_mask] = mask
92 kwargs["mask"] = mask
94 action = SummaryStatisticAction(vectorKey=self.key_vector)
95 # this is sad, but pex_config seems to have broken behavior that
96 # is dangerous to fix
97 action.setDefaults()
99 for name, value in action(data, **kwargs).items():
100 results[getattr(self, f"name_{name}")] = value
102 values = cast(Vector, data[self.selector_range.key][mask]) # type: ignore
103 valid = np.sum(np.isfinite(values)) > 0
104 results[self.name_select_maximum] = cast(Scalar, float(np.nanmax(values)) if valid else np.nan)
105 results[self.name_select_median] = cast(Scalar, float(np.nanmedian(values)) if valid else np.nan)
106 results[self.name_select_minimum] = cast(Scalar, float(np.nanmin(values)) if valid else np.nan)
107 results["range_maximum"] = self.selector_range.maximum
108 results["range_minimum"] = self.selector_range.minimum
110 return results