File: samplerow.h

package info (click to toggle)
aoflagger 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,232 kB
  • sloc: cpp: 61,805; python: 60; sh: 23; makefile: 8
file content (325 lines) | stat: -rw-r--r-- 7,714 bytes parent folder | download | duplicates (2)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#ifndef SAMPLEROW_H
#define SAMPLEROW_H

#include <algorithm>
#include <limits>
#include <cmath>
#include <memory>
#include <vector>

#include "image2d.h"
#include "mask2d.h"

#include "../strategy/algorithms/convolutions.h"

class SampleRow {
	public:
		explicit SampleRow(size_t size) :
			_values(size)
		{ }
		
		static SampleRow MakeEmpty(size_t size)
		{
			return SampleRow(size);
		}
		
		static SampleRow MakeZero(size_t size) {
			SampleRow row(size);
			std::fill(row._values.begin(), row._values.end(), 0.0);
			return row;
		}
		
		static SampleRow MakeFromRow(const Image2D* image, size_t y)
		{
			SampleRow row(image->Width());
			std::copy(image->ValuePtr(0, y), image->ValuePtr(0, y) + image->Width(), row._values.data());
			return row;
		}
		
		static SampleRow MakeFromRow(const Image2D* image, size_t xStart, size_t length, size_t y)
		{
			SampleRow row(length);
			std::copy(image->ValuePtr(xStart, y), image->ValuePtr(xStart, y) + length, row._values.data());
			return row;
		}
		
		static SampleRow MakeFromRowWithMissings(const Image2D* image, const Mask2D* mask, size_t y)
		{
			SampleRow row(image->Width());
			for(size_t x=0;x<image->Width();++x)
			{
				if(mask->Value(x, y))
					row._values[x] = std::numeric_limits<num_t>::quiet_NaN();
				else
					row._values[x] = image->Value(x, y);
			}
			return row;
		}
		
		static SampleRow MakeAmplitudeFromRow(const Image2D* real, const Image2D* imaginary, size_t y)
		{
			SampleRow row(real->Width());
			for(size_t x=0;x<real->Width();++x)
			{
				row._values[x] = sqrtn(real->Value(x,y)*real->Value(x,y) + imaginary->Value(x,y)*imaginary->Value(x,y));
			}
			return row;
		}
		
		static SampleRow MakeFromColumn(const Image2D* image, size_t x)
		{
			SampleRow row(image->Height());
			for(size_t y=0;y<image->Height();++y)
				row._values[y] = image->Value(x, y);
			return row;
		}
		
		static SampleRow MakeFromColumnWithMissings(const Image2D* image, const Mask2D* mask, size_t x)
		{
			SampleRow row(image->Height());
			for(size_t y=0;y<image->Height();++y)
			{
				if(mask->Value(x, y))
					row._values[y] = std::numeric_limits<num_t>::quiet_NaN();
				else
					row._values[y] = image->Value(x, y);
			}
			return row;
		}
		
		static SampleRow MakeFromRowSum(const Image2D* image, size_t yStart, size_t yEnd)
		{
			if(yEnd > yStart) {
				SampleRow row = MakeFromRow(image, yStart);
	
				for(size_t y=yStart+1;y<yEnd;++y) {
					for(size_t x=0;x<image->Width();++x)
						row._values[x] += image->Value(x, y);
				}
				return row;
			} else {
				return MakeZero(image->Width());
			}
		}
		
		static SampleRow MakeFromColumnSum(const Image2D* image, size_t xStart, size_t xEnd)
		{
			if(xEnd > xStart) {
				SampleRow row = MakeFromColumn(image, xStart);
	
				for(size_t x=xStart+1;x<xEnd;++x) {
					for(size_t y=0;y<image->Width();++y)
						row._values[y] += image->Value(x, y);
				}
				return row;
			} else {
				return MakeZero(image->Width());
			}
		}
		
		void SetHorizontalImageValues(Image2D* image, unsigned y) const noexcept
		{
			for(size_t i=0;i<_values.size();++i)
			{
				image->SetValue(i, y, _values[i]);
			}
		}
		
		void SetHorizontalImageValues(Image2D* image, unsigned xStart, unsigned y) const noexcept
		{
			for(size_t i=0;i<_values.size();++i)
			{
				image->SetValue(i+xStart, y, _values[i]);
			}
		}
		
		void SetVerticalImageValues(Image2D* image, unsigned x) const noexcept
		{
			for(size_t i=0;i<_values.size();++i)
			{
				image->SetValue(x, i, _values[i]);
			}
		}
		
		void SetVerticalImageValues(Image2D* image, unsigned x, unsigned yStart) const noexcept
		{
			for(size_t i=0;i<_values.size();++i)
			{
				image->SetValue(x, i+yStart, _values[i]);
			}
		}
		
		num_t Value(size_t i) const noexcept { return _values[i]; }
		
		void SetValue(size_t i, num_t newValue) noexcept { _values[i] = newValue; }

		size_t Size() const noexcept { return _values.size(); }
		
		size_t IndexOfMax() const noexcept
		{
			size_t maxIndex = 0;
			num_t maxValue = _values[0];
			for(size_t i = 1; i<_values.size();++i)
			{
				if(_values[i] > maxValue)
				{
					maxIndex = i;
					maxValue = _values[i];
				}
			}
			return maxIndex;
		}

		numl_t RMS() const noexcept
		{
			if(_values.empty()) return std::numeric_limits<numl_t>::quiet_NaN();
			numl_t sum = 0.0;
			for(num_t v : _values)
				sum += v * v;
			return sqrtnl(sum / _values.size());
		}
		
		num_t Median() const noexcept
		{
			if(_values.empty()) return std::numeric_limits<num_t>::quiet_NaN();

			std::vector<num_t> copy(_values);
			if(_values.size() % 2 == 0)
			{
				size_t
					m = _values.size() / 2 - 1;
				std::nth_element(copy.begin(), copy.begin() + m, copy.end());
				num_t leftMid = *(copy.begin() + m);
				std::nth_element(copy.begin(), copy.begin() + m + 1, copy.end());
				num_t rightMid = *(copy.begin() + m + 1);
				return (leftMid + rightMid) / 2;
			} else {
				size_t
					m = _values.size() / 2;
				std::nth_element(copy.begin(), copy.begin() + m, copy.end());
				num_t mid = *(copy.begin() + m);
				return mid;
			}
		}
		
		numl_t Mean() const noexcept
		{
			numl_t mean = 0.0;
			for(num_t v : _values)
				mean += v;
			return mean / _values.size();
		}
		
		numl_t MeanWithMissings() const noexcept
		{
			numl_t mean = 0.0;
			size_t count = 0;
			for(num_t v : _values)
			{
				if(std::isfinite(v))
				{
					mean += v;
					++count;
				}
			}
			return mean / count;
		}
		
		numl_t StdDev(double mean) const noexcept
		{
			numl_t stddev = 0.0;
			for(num_t v : _values)
				stddev += (v - mean) * (v - mean);
			return sqrtnl(stddev / _values.size());
		}
		
		num_t RMSWithMissings() const
		{
			return MakeWithoutMissings().RMS();
		}
		
		num_t MedianWithMissings() const
		{
			return MakeWithoutMissings().Median();
		}
		
		num_t StdDevWithMissings(double mean) const
		{
			return MakeWithoutMissings().StdDev(mean);
		}
		
		SampleRow MakeWithoutMissings() const;
		
		bool ValueIsMissing(size_t i) const
		{
			return !std::isfinite(Value(i));
		}
		
		void SetValueMissing(size_t i)
		{
			SetValue(i, std::numeric_limits<num_t>::quiet_NaN());
		}
		
		void ConvolveWithGaussian(num_t sigma)
		{
			Convolutions::OneDimensionalGausConvolution(_values.data(), _values.size(), sigma);
		}
		
		void ConvolveWithSinc(num_t frequency)
		{
			Convolutions::OneDimensionalSincConvolution(_values.data(), _values.size(), frequency);
		}
		
		SampleRow* operator-=(const SampleRow& source) noexcept
		{
			for(unsigned i=0;i<_values.size();++i)
				_values[i] -= source._values[i];
			return this;
		}
		
		num_t WinsorizedMean() const
		{
			std::vector<num_t> data(_values);
			std::sort(data.begin(), data.end(), numLessThanOperator);
			size_t lowIndex = (size_t) floor(0.1 * data.size());
			size_t highIndex = (size_t) ceil(0.9 * data.size())-1;
			num_t lowValue = data[lowIndex];
			num_t highValue = data[highIndex];

			// Calculate mean
			num_t mean = 0.0;
			for(size_t x = 0;x < data.size(); ++x) {
				const num_t value = data[x];
				if(value < lowValue)
					mean += lowValue;
				else if(value > highValue)
					mean += highValue;
				else
					mean += value;
			}
			return mean / (num_t) data.size();
		}
		
		num_t WinsorizedMeanWithMissings() const
		{
			return MakeWithoutMissings().WinsorizedMean();
		}
		
	private:
		std::vector<num_t> _values;
		
		// We need this less than operator, because the normal operator
		// does not enforce a strictly ordered set, because a<b != !(b<a) in the case
		// of nans/infs.
		static bool numLessThanOperator(const num_t& a, const num_t& b) noexcept {
			if(std::isfinite(a)) {
				if(std::isfinite(b))
					return a < b;
				else
					return true;
			}
			return false;
		}
};

#endif