File: histogrampagecontroller.cpp

package info (click to toggle)
aoflagger 3.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,688 kB
  • sloc: cpp: 83,116; python: 10,187; sh: 260; makefile: 178
file content (305 lines) | stat: -rw-r--r-- 11,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "histogrampagecontroller.h"

#include "../histogrampage.h"

#include "../../quality/histogramtablesformatter.h"
#include "../../quality/histogramcollection.h"
#include "../../quality/rayleighfitter.h"

#include "../../structures/msmetadata.h"

#ifndef HAVE_EXP10
#define exp10(x) exp((2.3025850929940456840179914546844) * (x))
#endif

HistogramPageController::HistogramPageController()
    : _page(nullptr),
      _histograms(nullptr),
      _summedPolarizationHistograms(nullptr) {}

HistogramPageController::~HistogramPageController() { CloseStatistics(); }

void HistogramPageController::readFromFile() {
  CloseStatistics();
  HistogramTablesFormatter histogramTables(_statFilename);
  if (histogramTables.HistogramsExist()) {
    const MSMetaData set(_statFilename);

    const unsigned polarizationCount = set.PolarizationCount();

    _histograms.reset(new HistogramCollection(polarizationCount));
    _histograms->Load(histogramTables);
  }
}

void HistogramPageController::CloseStatistics() {
  _statFilename = std::string();
  _histograms.reset();
  _summedPolarizationHistograms.reset();
}

void HistogramPageController::SetHistograms(
    const HistogramCollection* histograms) {
  CloseStatistics();
  _histograms.reset(new HistogramCollection(*histograms));
  _summedPolarizationHistograms.reset(
      _histograms->CreateSummedPolarizationCollection());
  _histograms->CreateMissingBins();
  _summedPolarizationHistograms->CreateMissingBins();
  updatePlot();
}

void HistogramPageController::updatePlot() {
  if (HasStatistics()) {
    _plot.Clear();

    const unsigned polarizationCount = _histograms->PolarizationCount();
    if (_drawXX) plotPolarization(*_histograms, 0);
    if (_drawXY && polarizationCount >= 2) plotPolarization(*_histograms, 1);
    if (_drawYX && polarizationCount >= 3) plotPolarization(*_histograms, 2);
    if (_drawYY && polarizationCount >= 4) plotPolarization(*_histograms, 3);
    if (_drawSum) plotPolarization(*_summedPolarizationHistograms, 0);

    if (_page != nullptr) _page->Redraw();
  }
}

void HistogramPageController::plotPolarization(
    const HistogramCollection& histogramCollection, unsigned polarization) {
  LogHistogram totalHistogram, rfiHistogram;
  histogramCollection.GetTotalHistogramForCrossCorrelations(polarization,
                                                            totalHistogram);
  histogramCollection.GetRFIHistogramForCrossCorrelations(polarization,
                                                          rfiHistogram);
  plotPolarization(totalHistogram, rfiHistogram);
}

std::string HistogramPageController::SlopeText(const LogHistogram& histogram) {
  if (_automaticSlopeRange) {
    histogram.GetRFIRegion(_slopeStart, _slopeEnd);
  }
  std::stringstream str;
  const double slope = histogram.NormalizedSlope(_slopeStart, _slopeEnd),
               powerLawExp = histogram.PowerLawExponent(_slopeStart),
               powerLawExpError =
                   histogram.PowerLawExponentStdError(_slopeStart, powerLawExp),
               offset = histogram.NormalizedSlopeOffset(_slopeStart, _slopeEnd,
                                                        slope),
               error = histogram.NormalizedSlopeStdError(_slopeStart, _slopeEnd,
                                                         slope),
               errorB = histogram.NormalizedSlopeStdDevBySampling(
                   _slopeStart, _slopeEnd, slope, _deltaS),
               upperLimit = histogram.PowerLawUpperLimit(_slopeStart, slope,
                                                         exp10(offset)),
               lowerLimit = histogram.PowerLawLowerLimit(
                   _slopeStart, slope, exp10(offset), _slopeRFIRatio),
               lowerError =
                   fabs(lowerLimit - histogram.PowerLawLowerLimit(
                                         _slopeStart, slope - error,
                                         exp10(offset), _slopeRFIRatio)),
               lowerLimit2 = histogram.PowerLawLowerLimit2(
                   _slopeStart, slope, exp10(offset), _slopeRFIRatio);
  str << slope << "±" << error << "\n/±" << errorB << "\nb=" << exp10(offset)
      << "\nPL:" << powerLawExp << "±" << powerLawExpError << "\n["
      << log10(lowerLimit) << "±" << lowerError << ';' << log10(upperLimit)
      << ']' << '\n'
      << log10(lowerLimit2);
  return str.str();
}

void HistogramPageController::plotSlope(const LogHistogram& histogram,
                                        const std::string& title,
                                        bool useLowerLimit2) {
  double start, end;
  if (_automaticSlopeRange) {
    histogram.GetRFIRegion(start, end);
  } else {
    start = _slopeStart;
    end = _slopeEnd;
  }
  double xMin = log10(histogram.MinPositiveAmplitude()),
         rfiRatio = _slopeRFIRatio,
         slope = histogram.NormalizedSlope(start, end),
         offset = histogram.NormalizedSlopeOffset(start, end, slope),
         upperLimit =
             log10(histogram.PowerLawUpperLimit(start, slope, exp10(offset))),
         lowerLimit = useLowerLimit2
                          ? log10(histogram.PowerLawLowerLimit2(
                                start, slope, exp10(offset), rfiRatio))
                          : log10(histogram.PowerLawLowerLimit(
                                start, slope, exp10(offset), rfiRatio));
  double xStart, xEnd;
  if (std::isfinite(lowerLimit))
    xStart = lowerLimit;
  else
    xStart = log10(start) - 1.0;
  if (std::isfinite(upperLimit))
    xEnd = upperLimit;
  else
    xEnd = log10(histogram.MaxAmplitude());
  double yStart = xStart * slope + offset, yEnd = xEnd * slope + offset;
  _plot.StartLine(title, "Amplitude in arbitrary units (log)",
                  "Frequency (log)");
  if (useLowerLimit2 && std::isfinite(xMin)) _plot.PushDataPoint(xMin, yStart);
  _plot.PushDataPoint(xStart, yStart);
  _plot.PushDataPoint(xEnd, yEnd);
}

void HistogramPageController::plotPolarization(
    const LogHistogram& totalHistogram, const LogHistogram& rfiHistogram) {
  if (_totalHistogram) {
    _plot.StartLine("Total histogram", "Amplitude in arbitrary units (log)",
                    "Frequency (log)");
    addHistogramToPlot(totalHistogram);

    if (_fit || _subtractFit) {
      plotFit(totalHistogram, "Fit to total");
    }
    if (_drawSlope) {
      plotSlope(totalHistogram, "Fitted slope", false);
    }
    if (_drawSlope2) {
      plotSlope(totalHistogram, "Fitted slope", true);
    }
    const std::string str = SlopeText(totalHistogram);
    _page->SetSlopeFrame(str);
  }

  if (_rfiHistogram) {
    _plot.StartLine("RFI histogram", "Amplitude in arbitrary units (log)",
                    "Frequency (log)");
    addHistogramToPlot(rfiHistogram);

    if (_fit || _subtractFit) {
      plotFit(rfiHistogram, "Fit to RFI");
    }
    const std::string str = SlopeText(rfiHistogram);
    _page->SetSlopeFrame(str);
    if (_drawSlope) {
      plotSlope(rfiHistogram, "Fitted slope", false);
    }
    if (_drawSlope2) {
      plotSlope(rfiHistogram, "Fitted slope", true);
    }
  }

  if (_notRFIHistogram) {
    _plot.StartLine("Non-RFI histogram", "Amplitude in arbitrary units (log)",
                    "Frequency (log)");
    LogHistogram histogram(totalHistogram);
    histogram -= rfiHistogram;
    addHistogramToPlot(histogram);

    if (_fit || _subtractFit) {
      plotFit(histogram, "Fit to Non-RFI");
    }
  }
}

void HistogramPageController::plotFit(const LogHistogram& histogram,
                                      const std::string& title) {
  double sigmaEstimate;
  sigmaEstimate = RayleighFitter::SigmaEstimate(histogram);
  if (_automaticFitRange) {
    RayleighFitter::FindFitRangeUnderRFIContamination(
        histogram.MinPositiveAmplitude(), sigmaEstimate, _fitStart, _fitEnd);
  }
  double sigma = RayleighFitter::SigmaEstimate(histogram, _fitStart, _fitEnd),
         n = RayleighFitter::NEstimate(histogram, _fitStart, _fitEnd);
  RayleighFitter fitter;
  fitter.SetFitLogarithmic(_fitLogarithmic);
  fitter.Fit(_fitStart, _fitEnd, histogram, sigma, n);
  if (_fit) {
    _plot.StartLine(title, "Amplitude in arbitrary units (log)",
                    "Frequency (log)");
    addRayleighToPlot(histogram, sigma, n);
  }
  if (_subtractFit) {
    _plot.StartLine(title, "Amplitude in arbitrary units (log)",
                    "Frequency (log)");
    addRayleighDifferenceToPlot(histogram, sigma, n);
  }

  std::stringstream str;
  str << "σ=1e" << log10(sigma) << ",n=1e" << log10(n) << '\n'
      << "n_t=1e" << log10(histogram.NormalizedTotalCount()) << '\n'
      << "mode=1e" << log10(histogram.AmplitudeWithMaxNormalizedCount()) << '\n'
      << "ε_R="
      << RayleighFitter::ErrorOfFit(histogram, _fitStart, _fitEnd, sigma, n);
  _page->SetFitText(str.str());
}

void HistogramPageController::addRayleighToPlot(const LogHistogram& histogram,
                                                double sigma, double n) {
  const bool derivative = _derivative;
  double x = histogram.MinPositiveAmplitude();
  const double xend = sigma * 5.0;
  const double sigmaP2 = sigma * sigma;
  while (x < xend) {
    const double logx = log10(x);
    if (derivative) {
      const double dc = -(exp10(2.0 * x) - sigmaP2) / sigmaP2;
      if (std::isfinite(logx) && std::isfinite(dc))
        _plot.PushDataPoint(logx, dc);
    } else {
      const double c = n * x / (sigmaP2)*exp(-x * x / (2 * sigmaP2));
      const double logc = log10(c);
      if (std::isfinite(logx) && std::isfinite(logc))
        _plot.PushDataPoint(logx, logc);
    }
    x *= 1.05;
  }
}

void HistogramPageController::addRayleighDifferenceToPlot(
    const LogHistogram& histogram, double sigma, double n) {
  const double sigmaP2 = sigma * sigma;
  const double minCount = histogram.MinPosNormalizedCount();
  for (LogHistogram::iterator i = histogram.begin(); i != histogram.end();
       ++i) {
    const double x = i.value();

    const double c = n * x / (sigmaP2)*exp(-x * x / (2 * sigmaP2));
    const double diff = fabs(i.normalizedCount() - c);
    if (diff >= minCount) {
      const double logx = log10(x);
      const double logc = log10(diff);
      if (std::isfinite(logx) && std::isfinite(logc))
        _plot.PushDataPoint(logx, logc);
    }
  }
}

void HistogramPageController::addHistogramToPlot(
    const LogHistogram& histogram) {
  const bool derivative = _derivative;
  const bool staircase = _staircaseFunction;
  const bool normalize = _normalize;
  double deltaS = _deltaS;
  if (deltaS <= 1.0001) deltaS = 1.0001;
  for (LogHistogram::iterator i = histogram.begin(); i != histogram.end();
       ++i) {
    double x = i.value(), logxStart, logxEnd;
    if (staircase) {
      logxStart = log10(i.binStart());
      logxEnd = log10(i.binEnd());
    } else {
      logxStart = log10(x);
      logxEnd = 0.0;  // unused, but to avoid warning
    }
    if (derivative) {
      const double cslope = histogram.NormalizedSlope(x / deltaS, x * deltaS);
      // if(std::isfinite(logxStart) && std::isfinite(cslope))
      _plot.PushDataPoint(logxStart, cslope);
      if (staircase)  // && std::isfinite(logxEnd) && std::isfinite(cslope))
        _plot.PushDataPoint(logxEnd, cslope);
    } else {
      const double logc =
          log10(normalize ? i.normalizedCount() : i.unnormalizedCount());
      // if(std::isfinite(logxStart) && std::isfinite(logc))
      _plot.PushDataPoint(logxStart, logc);
      if (staircase)  // && std::isfinite(logxEnd) && std::isfinite(logc))
        _plot.PushDataPoint(logxEnd, logc);
    }
  }
}