Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
Metrics.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/rfim/Metrics.h"
25 
26 
27 namespace ska {
28 namespace cheetah {
29 namespace rfim {
30 
31 
32 Metrics::Metrics(TimeFrequencyFlagsType const& expected, TimeFrequencyFlagsType const& actual)
33 {
34  compare(expected, actual);
35 }
36 
37 Metrics::~Metrics()
38 {
39 }
40 
41 std::size_t Metrics::num_correct() const
42 {
43  return _correct_num;
44 }
45 
46 std::size_t Metrics::num_rfi() const
47 {
48  return _rfi_num;
49 }
50 
52 {
53  if(_rfi_num)
54  return (100.0*_correct_num)/_rfi_num;
55  return 100.0;
56 }
57 
59 {
60  return 100.0*((double)(_false_positives.data_size() - (_false_positives_num + _false_negatives_num))/_false_positives.data_size());
61 }
62 
63 float Metrics::false_positives_percentage() const
64 {
65  return (100.0*_false_positives_num)/_false_positives.data_size();
66 }
67 
68 float Metrics::false_negatives_percentage() const
69 {
70  return (100.0*_false_negatives_num)/_false_negatives.data_size();
71 }
72 
74 {
75  return _false_positives;
76 }
77 
78 std::size_t Metrics::num_false_positives() const
79 {
80  return _false_positives_num;
81 }
82 
84 {
85  return _false_negatives;
86 }
87 
88 std::size_t Metrics::num_false_negatives() const
89 {
90  return _false_negatives_num;
91 }
92 
93 std::size_t Metrics::compare(TimeFrequencyFlagsType const& expected, TimeFrequencyFlagsType const& actual)
94 {
95  _false_positives_num = 0;
96  _false_negatives_num = 0;
97  _correct_num = 0;
98  _rfi_num = 0;
99 
100  data::DimensionSize<data::Time> max_spectra(std::max(expected.number_of_spectra(), actual.number_of_spectra()));
101  data::DimensionSize<data::Frequency> max_channels(std::max(expected.number_of_channels(), actual.number_of_channels()));
102  data::DimensionSize<data::Time> min_samples(std::min(expected.number_of_spectra(), actual.number_of_spectra()));
103  data::DimensionSize<data::Frequency> min_channels(std::min(expected.number_of_channels(), actual.number_of_channels()));
104  PANDA_LOG_DEBUG << "max_spectra=" << max_spectra << " max_channels=" << max_channels;
105  if(expected.data_size() != actual.data_size())
106  {
107  panda::Error e("unmatched flag size: samples(");
108  e << max_spectra << " vs " << min_samples;
109  e << ") channels(" << max_channels << " vs " << min_channels;
110  }
111 
112  _false_positives.resize(max_spectra, max_channels);
113  _false_negatives.resize(max_spectra, max_channels);
114 
115  // now ananlyse the overlapping flags
116  auto it = expected.begin();
117  auto actual_it = actual.begin();
118  auto fp_it = _false_positives.begin();
119  auto fn_it = _false_negatives.begin();
120  while(it != expected.end())
121  {
122  if(*it) ++_rfi_num;
123  if(*it == *actual_it) {
124  *fp_it = false;
125  *fn_it = false;
126  if(*it) ++_correct_num;
127  }
128  else{
129  if(*it) {
130  *fp_it = false;
131  *fn_it = true;
132  ++_false_negatives_num;
133  }
134  else {
135  *fp_it = true;
136  ++_false_positives_num;
137  *fn_it = false;
138  }
139  }
140  // note: probably should investigate intrusive containers to reduce overhead of maintianing multiple iterators
141  ++it;
142  ++actual_it;
143  ++fp_it;
144  ++fn_it;
145  }
146  return _false_positives_num + _false_negatives_num;
147 }
148 
149 } // namespace rfim
150 } // namespace cheetah
151 } // namespace ska
float correct_percentage() const
return the total of any correct flags as a percentage
Definition: Metrics.cpp:58
float rfi_detected_percentage() const
return the total of any correct flags as a percentage
Definition: Metrics.cpp:51
TimeFrequencyFlagsType const & false_negatives() const
returnn a set of flags indicating each false negative found
Definition: Metrics.cpp:83
std::size_t num_correct() const
return the total of any correctly identified flags found
Definition: Metrics.cpp:41
Some limits and constants for FLDO.
Definition: Brdz.h:35
std::size_t num_false_positives() const
return the total of any false positives detected
Definition: Metrics.cpp:78
std::size_t compare(TimeFrequencyFlagsType const &expected, TimeFrequencyFlagsType const &actual)
calculate the metrics by comparing the two sets of flags
Definition: Metrics.cpp:93
std::size_t num_false_negatives() const
return the number of any false negatives detected
Definition: Metrics.cpp:88
std::size_t num_rfi() const
return the number of rfi flags in the expected data
Definition: Metrics.cpp:46
TimeFrequencyFlagsType const & false_positives() const
returnn a set of flags indicating each false positive fopund
Definition: Metrics.cpp:73