Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
ChannelMaskTest.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/channel_mask/test/ChannelMaskTest.h"
25 #include "cheetah/channel_mask/ChannelMask.h"
26 #include "cheetah/data/TimeFrequency.h"
27 #include <limits>
28 
29 
30 namespace ska {
31 namespace cheetah {
32 namespace channel_mask {
33 namespace test {
34 
35 
36 ChannelMaskTest::ChannelMaskTest()
37  : ::testing::Test()
38 {
39 }
40 
41 ChannelMaskTest::~ChannelMaskTest()
42 {
43 }
44 
45 void ChannelMaskTest::SetUp()
46 {
47 }
48 
49 void ChannelMaskTest::TearDown()
50 {
51 }
52 
53 template<typename NumericalRep>
54 struct TestPolicy {
55  public:
57  : _call_count(0)
58  , _expected_data(data)
59  {}
60 
61  template<typename DataType>
62  void operator()(DataType& data) {
63  ++_call_count;
64  for(auto& sample : data)
65  {
66  sample = std::numeric_limits<NumericalRep>::max();
67  }
68  // check data falls in the expected range
69  ASSERT_GE(&*data.begin(), &*_expected_data.begin());
70  ASSERT_LE(&*data.end(), &*_expected_data.end());
71  }
72 
73  std::size_t call_count() const { return _call_count; }
74 
75  private:
76  std::size_t _call_count;
77  data::TimeFrequency<Cpu, NumericalRep> const& _expected_data;
78 };
79 
80 template<typename NumericalRep>
81 class TestChannelMask : public ChannelMask<NumericalRep, TestPolicy<NumericalRep>>
82 {
84  public:
86  : BaseT(config, data)
87  {
88  }
89 
90  TestPolicy<NumericalRep>& policy() { return this->_policy; }
91 };
92 
93 TEST_F(ChannelMaskTest, test_mask)
94 {
95  typedef uint8_t NumericalRep;
96  typedef TestChannelMask<NumericalRep> ChannelMaskType;
98 
99  DataType data(data::DimensionSize<data::Frequency>(10)
100  , data::DimensionSize<data::Time>(6));
101  std::fill(data.begin(), data.end(), std::numeric_limits<NumericalRep>::min());
102 
103  {
104  // no changes expected
105  Config config;
106  ChannelMaskType mask(config, data);
107 
108  mask(data);
109  ASSERT_EQ(0U, mask.policy().call_count());
110  }
111  typedef data::DimensionSpan<data::Frequency> SpanType;
112  Config config;
113  // take out a single channel
114  config.flagged_channels().insert(SpanType(data::DimensionIndex<data::Frequency>(1), data::DimensionSize<data::Frequency>(1)));
115  std::fill(data.begin(), data.end(), std::numeric_limits<NumericalRep>::min());
116  auto channel0=data.channel(0);
117  auto channel1=data.channel(1);
118  {
119  // single contiguos slice
120  ChannelMaskType mask(config, data);
121 
122  mask(data);
123  ASSERT_EQ(1U, mask.policy().call_count());
124  // check first channel is clear
125  for(auto const& sample : channel0)
126  {
127  ASSERT_EQ(std::numeric_limits<NumericalRep>::min(), sample);
128  }
129  // channel 1 should be changed
130  for(auto const& sample : channel1)
131  {
132  ASSERT_EQ(std::numeric_limits<NumericalRep>::max(), sample);
133  }
134  // remaining channels hould not be changed
135  for(unsigned channel_num=2; channel_num<10; ++channel_num)
136  {
137  auto channel=data.channel(channel_num);
138  for(auto const& sample : channel)
139  {
140  ASSERT_EQ(std::numeric_limits<NumericalRep>::min(), sample);
141  }
142  }
143  }
144  // take out another disjoint channel
145  std::fill(data.begin(), data.end(), std::numeric_limits<NumericalRep>::min());
146  config.flagged_channels().insert(SpanType(data::DimensionIndex<data::Frequency>(4), data::DimensionSize<data::Frequency>(2)));
147  {
148  // single contiguos slice
149  ChannelMaskType mask(config, data);
150 
151  mask(data);
152  ASSERT_EQ(2U, mask.policy().call_count());
153  for(auto const& sample : channel0)
154  {
155  ASSERT_EQ(std::numeric_limits<NumericalRep>::min(), sample);
156  }
157  // channel 1 shpould be changed
158  for(auto const& sample : channel1)
159  {
160  ASSERT_EQ(std::numeric_limits<NumericalRep>::max(), sample);
161  }
162  // inteemediate channels 2,3 shpould be unchanged
163  for(unsigned channel_num=2; channel_num<4; ++channel_num)
164  {
165  auto channel=data.channel(channel_num);
166  for(auto const& sample : channel)
167  {
168  ASSERT_EQ(std::numeric_limits<NumericalRep>::min(), sample);
169  }
170  }
171  // channels 4,5 should be changed
172  for(unsigned channel_num=4; channel_num<6; ++channel_num)
173  {
174  auto channel=data.channel(channel_num);
175  for(auto const& sample : channel)
176  {
177  ASSERT_EQ(std::numeric_limits<NumericalRep>::max(), sample) << "channel=" << channel_num;
178  }
179  }
180  // remaining channels hould not be changed
181  for(unsigned channel_num=6; channel_num<10; ++channel_num)
182  {
183  auto channel=data.channel(channel_num);
184  for(auto const& sample : channel)
185  {
186  ASSERT_EQ(std::numeric_limits<NumericalRep>::min(), sample);
187  }
188  }
189  }
190 }
191 
192 TEST_F(ChannelMaskTest, test_ranges_outside_data_size_limits)
193 {
194  typedef uint8_t NumericalRep;
195  typedef TestChannelMask<NumericalRep> ChannelMaskType;
197  typedef data::DimensionSpan<data::Frequency> SpanType;
198 
199  DataType data(data::DimensionSize<data::Frequency>(5)
200  , data::DimensionSize<data::Time>(6));
201  // If the span is beyond the dimension then only change channels until the end of the block
202  std::fill(data.begin(), data.end(), std::numeric_limits<NumericalRep>::min());
203 
204  Config config;
205  config.flagged_channels().insert(SpanType(data::DimensionIndex<data::Frequency>(8), data::DimensionSize<data::Frequency>(4)));
206  {
207  // single contiguos slice
208  ChannelMaskType mask(config, data);
209 
210  mask(data);
211  ASSERT_EQ(0U, mask.policy().call_count());
212  }
213 }
214 
215 
216 TEST_F(ChannelMaskTest, test_ranges_cross_data_size_limits)
217 {
218  typedef uint8_t NumericalRep;
219  typedef TestChannelMask<NumericalRep> ChannelMaskType;
221  typedef data::DimensionSpan<data::Frequency> SpanType;
222 
223  DataType data(data::DimensionSize<data::Frequency>(10)
224  , data::DimensionSize<data::Time>(6));
225  // If the span is beyond the dimension then only change channels until the end of the block
226  std::fill(data.begin(), data.end(), std::numeric_limits<NumericalRep>::min());
227 
228  Config config;
229  config.flagged_channels().insert(SpanType(data::DimensionIndex<data::Frequency>(8), data::DimensionSize<data::Frequency>(4)));
230  {
231  // single contiguos slice
232  ChannelMaskType mask(config, data);
233 
234  mask(data);
235  ASSERT_EQ(1U, mask.policy().call_count());
236 
237  for(unsigned channel_num=0; channel_num<8; ++channel_num)
238  {
239  auto channel=data.channel(channel_num);
240  for(auto const& sample : channel) {
241  ASSERT_EQ(std::numeric_limits<NumericalRep>::min(), sample);
242  }
243  }
244  // channels 8 to 10 should be flagged
245  for(unsigned channel_num=8; channel_num<10; ++channel_num)
246  {
247  auto channel=data.channel(channel_num);
248  for(auto const& sample : channel) {
249  ASSERT_EQ(std::numeric_limits<NumericalRep>::max(), sample);
250  }
251  }
252  }
253 }
254 
255 } // namespace test
256 } // namespace channel_mask
257 } // namespace cheetah
258 } // namespace ska
Apply a policy to channels specified in the Config.
Definition: ChannelMask.h:41
void insert(pss::astrotypes::DimensionSpan< data::Frequency > const &span)
insert the span into the glagged list in the appropriate place.
Some limits and constants for FLDO.
Definition: Brdz.h:35