LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
statistic.py
Go to the documentation of this file.
1# This file is part of pipe_tasks.
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
22from abc import ABCMeta, abstractmethod
23from astropy.stats import mad_std
24from dataclasses import dataclass
25import numpy as np
26from scipy.stats import iqr
27
28__all__ = ['Statistic', 'Count', 'Median', 'Percentile', 'StandardDeviation', 'SigmaIQR', 'SigmaMAD',
29 'Statistics']
30
31
32class Statistic(metaclass=ABCMeta):
33 """Compute a statistic from a list of values.
34 """
35 # TODO: Make this a property after upgrade to Python 3.9
36 @classmethod
37 @abstractmethod
38 def name(cls):
39 pass
40
41 @abstractmethod
42 def value(self, values):
43 """Return the value of the statistic given a set of values.
44
45 Parameters
46 ----------
47 values : `Collection` [`float`]
48 A set of values to compute the statistic for.
49 Returns
50 -------
51 statistic : `float`
52 The value of the statistic.
53 """
54 pass
55
56
58 @classmethod
59 def name(cls):
60 return "count"
61
62 """The median of a set of values."""
63 def value(self, values):
64 return len(values)
65
66
68 @classmethod
69 def name(cls):
70 return "median"
71
72 """The median of a set of values."""
73 def value(self, values):
74 return np.median(values)
75
76
77@dataclass(frozen=True)
79 """An arbitrary percentile.
80
81 Parameters
82 ----------
83 percentile : `float`
84 A valid percentile (0 <= p <= 100).
85 """
86 percentile: float
87
88 @classmethod
89 def name(cls):
90 return "percentile"
91
92 def value(self, values):
93 return np.percentile(values, self.percentile)
94
95
97 """The standard deviation (sigma)."""
98 @classmethod
99 def name(cls):
100 return "std"
101
102 def value(self, values):
103 return np.std(values)
104
105
107 """The re-scaled inter-quartile range (sigma equivalent)."""
108 @classmethod
109 def name(cls):
110 return "sigma_iqr"
111
112 def value(self, values):
113 return iqr(values, scale='normal')
114
115
117 """The re-scaled median absolute deviation (sigma equivalent)."""
118 @classmethod
119 def name(cls):
120 return "sigma_mad"
121
122 def value(self, values):
123 return mad_std(values)
124
125
126Statistics = {
127 stat.name(): stat
128 for stat in (Count, Median, Percentile, StandardDeviation, SigmaIQR, SigmaMAD)
129}
def value(self, values)
Definition: statistic.py:63