RobustEstimateZapper.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2020 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/genutil/RobustEstimateZapper.h
10
11#ifndef __RobustEstimateZapper_h
12#define __RobustEstimateZapper_h
13
14// #define _DEBUG 1
15
16#include "TextInterface.h"
17#include "Estimate.h"
18#include "debug.h"
19
20#include <algorithm>
21
23class RobustEstimateZapper : public Reference::Able
24{
25private:
26 bool error;
27 bool logarithmic;
28 bool scale;
29 float threshold_min;
30 float threshold_max;
31
32public:
33
34 RobustEstimateZapper ();
35
37 bool get_error_bar () const { return error; }
38 void set_error_bar (bool flag) { error = flag; }
39
41 bool get_log () const { return logarithmic; }
42 void set_log (bool flag) { logarithmic = flag; }
43
45 bool get_scale () const { return scale; }
46 void set_scale (bool flag) { scale = flag; }
47
49 float get_cutoff_threshold () const { return threshold_max; }
50 void set_cutoff_threshold (float val) { threshold_max = threshold_min = val; }
51
53 float get_cutoff_threshold_min () const { return threshold_min; }
54 void set_cutoff_threshold_min (float val) { threshold_min = val; }
55
57 float get_cutoff_threshold_max () const { return threshold_max; }
58 void set_cutoff_threshold_max (float val) { threshold_max = val; }
59
61 TextInterface::Parser* get_interface();
62
63 class Interface;
64
65 float get_value (const Estimate<float>& datum)
66 {
67 float val;
68
69 if (error)
70 val = sqrt(datum.var);
71 else
72 val = datum.val;
73
74 if (logarithmic)
75 val = log10 (val);
76
77 return val;
78 }
79
80 template<class Container, class Size, class Get, class Valid>
81 void excise (unsigned iparam, Container* container, Size size, Get get, Valid valid)
82 {
83 unsigned npt = (container->*size)();
84 DEBUG("RobustEstimateZapper::excise iparam=" << iparam << " npt=" << npt);
85
86 std::vector<float> work (npt);
87 unsigned iwork = 0;
88
89 for (unsigned ipt=0; ipt<npt; ipt++)
90 {
91 Estimate<float> val = (container->*get)(iparam, ipt);
92 if (val.var == 0.0)
93 continue;
94
95 work[iwork] = get_value (val);
96 iwork ++;
97 }
98
99 // find the median value
100 std::nth_element (work.begin(), work.begin()+iwork/2, work.begin()+iwork);
101 float median = work[ iwork/2 ];
102
103 DEBUG("RobustEstimateZapper::excise work size=" << work.size() << " median=" << median);
104
105 float madm = 1.0;
106
107 if (scale)
108 {
109 // compute the absolute deviation from the median
110 for (unsigned ipt=0; ipt < iwork; ipt++)
111 work[ipt] = fabs(work[ipt] - median);
112
113 // find the median absolute deviation from the median
114 std::nth_element (work.begin(), work.begin()+iwork/2, work.begin()+iwork);
115 madm = work[ iwork/2 ];
116
117 DEBUG("RobustEstimateZapper::excise scale by madm=" << madm);
118 }
119
120 DEBUG("RobustEstimateZapper::excise iwork=" << iwork
121 << " iparam=" << iparam << " median=" << median
122 << " madm=" << madm);
123
124 unsigned excised = 0;
125
126 double max_extreme = median + threshold_max * madm;
127 double min_extreme = median - threshold_min * madm;
128
129 for (unsigned ipt=0; ipt<npt; ipt++)
130 {
131 Estimate<float> val = (container->*get)(iparam, ipt);
132 if (val.var == 0.0)
133 continue;
134
135 double test = get_value(val);
136
137 if (threshold_max && test > max_extreme)
138 {
139 DEBUG("RobustEstimateZapper::excise zapping >max iparam=" << iparam << " ipt=" << ipt);
140
141 excised ++;
142 (container->*valid)(ipt, false);
143 }
144
145 if (threshold_min && test < min_extreme)
146 {
147 DEBUG("RobustEstimateZapper::excise zapping <min iparam=" << iparam << " ipt=" << ipt);
148
149 excised ++;
150 (container->*valid)(ipt, false);
151 }
152
153 }
154
155 DEBUG("RobustEstimateZapper::excise excised=" << excised);
156 }
157};
158
159#endif
T val
Definition Estimate.h:43
U var
Definition Estimate.h:45
Manages Reference::To references to the instance.
Definition ReferenceAble.h:35
bool get_error_bar() const
Flag outliers based on their error bar.
Definition RobustEstimateZapper.h:37
TextInterface::Parser * get_interface()
Return a text interface that can be used to access this instance.
Definition RobustEstimateZapper.C:37
bool get_log() const
Flag outliers based on the logarithm of either the value or the error bar.
Definition RobustEstimateZapper.h:41
float get_cutoff_threshold_max() const
Set threshold used to identify outliers (multiple of madm)
Definition RobustEstimateZapper.h:57
float get_cutoff_threshold_min() const
Set threshold used to identify outliers (multiple of madm)
Definition RobustEstimateZapper.h:53
bool get_scale() const
Do not multiply threshold by MADM.
Definition RobustEstimateZapper.h:45
float get_cutoff_threshold() const
Set threshold used to identify outliers (multiple of madm)
Definition RobustEstimateZapper.h:49

Generated using doxygen 1.14.0