File: polarizationstatistics.h

package info (click to toggle)
aoflagger 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,868 kB
  • sloc: cpp: 52,164; python: 152; sh: 60; makefile: 17
file content (74 lines) | stat: -rw-r--r-- 2,312 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
#ifndef POLARIZATIONSTATISTICS_H
#define POLARIZATIONSTATISTICS_H

#include <vector>
#include <stdexcept>
#include <string>
#include <sstream>

#include "../../structures/mask2d.h"
#include "../../structures/timefrequencydata.h"
#include "../../structures/timefrequencymetadata.h"

#include "../../util/logger.h"

class PolarizationStatistics {
 public:
  PolarizationStatistics() {}
  ~PolarizationStatistics() {}

  void Add(const class TimeFrequencyData &data) {
    unsigned polarizationCount = data.PolarizationCount();
    if (_flaggedCounts.size() == 0) {
      _polarizations = data.Polarizations();
      for (unsigned i = 0; i < polarizationCount; ++i) {
        _flaggedCounts.push_back(0);
        _totalCounts.push_back(0);
        _names.push_back(
            aocommon::Polarization::TypeToFullString(_polarizations[i]));
      }
    } else if (_polarizations != data.Polarizations()) {
      throw std::runtime_error(
          "Adding differently polarized data to statistics");
    }
    for (unsigned i = 0; i < polarizationCount; ++i) {
      Mask2DCPtr mask = data.MakeFromPolarizationIndex(i).GetSingleMask();
      _flaggedCounts[i] += mask->GetCount<true>();
      _totalCounts[i] += mask->Width() * mask->Height();
    }
  }
  bool HasData() { return !_flaggedCounts.empty(); }
  void Report() {
    if (HasData()) {
      Logger::Info << "Polarization statistics: ";
      for (unsigned i = 0; i < _flaggedCounts.size(); ++i) {
        numl_t percentage =
            (numl_t)_flaggedCounts[i] * 100.0 / (numl_t)_totalCounts[i];
        if (i != 0) Logger::Info << ", ";
        Logger::Info << _names[i] << ": " << formatPercentage(percentage)
                     << '%';
      }
      Logger::Info << '\n';
    } else {
      Logger::Info << "No polarization statistics were collected.\n";
    }
  }

 private:
  std::string formatPercentage(numl_t percentage) {
    std::ostringstream s;
    if (percentage >= 1.0)
      s << round(percentage * 10.0) / 10.0;
    else if (percentage >= 0.1)
      s << round(percentage * 100.0) / 100.0;
    else
      s << round(percentage * 1000.0) / 1000.0;
    return s.str();
  }

  std::vector<long unsigned> _flaggedCounts, _totalCounts;
  std::vector<std::string> _names;
  std::vector<aocommon::PolarizationEnum> _polarizations;
};

#endif