File: complexplaneplotwindow.cpp

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 (358 lines) | stat: -rw-r--r-- 13,476 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "complexplaneplotwindow.h"

#include <set>
#include <sstream>

#include <gtkmm/messagedialog.h>

#include "../structures/antennainfo.h"
#include "../structures/date.h"
#include "../structures/image2d.h"

#include "../strategy/plots/rfiplots.h"

#include "../strategy/algorithms/combinatorialthresholder.h"
#include "../strategy/algorithms/fringestoppingfitter.h"

#include "../plot/plotmanager.h"

#include "../imaging/uvimager.h"

#include "rfiguiwindow.h"

ComplexPlanePlotWindow::ComplexPlanePlotWindow(RFIGuiWindow &rfiGuiWindow, PlotManager &plotManager)
	: _rfiGuiWindow(rfiGuiWindow),
		_plotManager(plotManager),
		_detailsFrame("Location details"),
		_detailsLabel(),
		_xPositionLabel("time start position:"),
		_yPositionLabel("frequency start position:"),
		_lengthLabel("time length:"),
		_ySumLengthLabel("Frequency averaging size:"),
		_xPositionScale(Gtk::ORIENTATION_HORIZONTAL),
		_yPositionScale(Gtk::ORIENTATION_HORIZONTAL),
		_lengthScale(Gtk::ORIENTATION_HORIZONTAL),
		_ySumLengthScale(Gtk::ORIENTATION_HORIZONTAL),
		_realVersusImaginaryButton("Real versus imaginary"),
		_timeVersusRealButton("Time versus real"),
		_allValuesButton("All values"),
		_unmaskedValuesButton("Unmasked values"),
		_maskedValuesButton("Masked values"),
		_fittedValuesButton("Fitted values (constant freq)"),
		_individualSampleFitButton("Channel fitted values (varying freq)"),
		_fringeFitButton("Fringe fitted (varying freq)"),
		_dynamicFringeFitButton("Dynamic fringe fitted (varying freq+amp)"),
		_plotButton("Plot"),
		_xMax(_rfiGuiWindow.GetOriginalData().ImageWidth()),
		_yMax(_rfiGuiWindow.GetOriginalData().ImageHeight())
{
	_detailsFrame.add(_detailsBox);
	_detailsBox.show();

	_detailsBox.pack_start(_detailsLabel);
	_detailsLabel.set_line_wrap(true);
	_detailsLabel.set_max_width_chars(40);
	_detailsLabel.show();

	_mainBox.pack_start(_detailsFrame);
	_detailsFrame.show();
	
	_mainBox.pack_start(_xPositionLabel);
	_xPositionLabel.show();
	_xPositionScale.set_range(0.0, _rfiGuiWindow.GetOriginalData().ImageWidth());
	_xPositionScale.signal_value_changed().
		connect(sigc::mem_fun(*this, &ComplexPlanePlotWindow::onTimeStartChanged));
	_mainBox.pack_start(_xPositionScale);
	_xPositionScale.show();

	_mainBox.pack_start(_yPositionLabel);
	_yPositionLabel.show();
	_yPositionScale.set_range(0.0, _rfiGuiWindow.GetOriginalData().ImageHeight());
	_yPositionScale.signal_value_changed().
		connect(sigc::mem_fun(*this, &ComplexPlanePlotWindow::onFreqChanged));
	_mainBox.pack_start(_yPositionScale);
	_yPositionScale.show();

	_mainBox.pack_start(_lengthLabel);
	_lengthLabel.show();
	_lengthScale.set_range(1.0, _rfiGuiWindow.GetOriginalData().ImageWidth());
	_lengthScale.signal_value_changed().
		connect(sigc::mem_fun(*this, &ComplexPlanePlotWindow::onTimeDurationChanged));
	_mainBox.pack_start(_lengthScale);
	_lengthScale.show();

	_mainBox.pack_start(_ySumLengthLabel);
	_ySumLengthLabel.show();
	_ySumLengthScale.set_range(1.0, _rfiGuiWindow.GetOriginalData().ImageHeight());
	_ySumLengthScale.signal_value_changed().
		connect(sigc::mem_fun(*this, &ComplexPlanePlotWindow::onFreqSizeChanged));
	_mainBox.pack_start(_ySumLengthScale);
	_ySumLengthScale.show();

	_mainBox.pack_start(_realVersusImaginaryButton);
	_realVersusImaginaryButton.show();
	Gtk::RadioButtonGroup group = _realVersusImaginaryButton.get_group();
	_timeVersusRealButton.set_group(group);
	_realVersusImaginaryButton.set_active(true);
	_mainBox.pack_start(_timeVersusRealButton);
	_timeVersusRealButton.show();

	_mainBox.pack_start(_allValuesButton);
	_allValuesButton.set_active(true);
	_allValuesButton.show();

	_mainBox.pack_start(_maskedValuesButton);
	_maskedValuesButton.show();

	_mainBox.pack_start(_unmaskedValuesButton);
	_unmaskedValuesButton.show();

	_mainBox.pack_start(_fittedValuesButton);
	_fittedValuesButton.set_active(true);
	_fittedValuesButton.show();

	_mainBox.pack_start(_individualSampleFitButton);
	_individualSampleFitButton.show();

	_mainBox.pack_start(_fringeFitButton);
	_fringeFitButton.show();

	_mainBox.pack_start(_dynamicFringeFitButton);
	_dynamicFringeFitButton.show();

	_plotButton.signal_clicked().connect(sigc::mem_fun(*this, &ComplexPlanePlotWindow::onPlotPressed));
	_buttonBox.pack_start(_plotButton);
	_plotButton.show();

	_mainBox.pack_start(_buttonBox);
	_buttonBox.show();

	add(_mainBox);
	_mainBox.show();

	_observationTimes = _rfiGuiWindow.SelectedMetaData()->ObservationTimes();

	setDetailsLabel();
}

ComplexPlanePlotWindow::~ComplexPlanePlotWindow()
{
}

void ComplexPlanePlotWindow::onPlotPressed()
{
	if(_rfiGuiWindow.HasImage())
	{
		try {
			Plot2D &plot = _plotManager.NewPlot2D("Complex plane");
			size_t x = (size_t) _xPositionScale.get_value();
			size_t y = (size_t) _yPositionScale.get_value();
			size_t length = (size_t) _lengthScale.get_value();
			size_t avgSize = (size_t) _ySumLengthScale.get_value();
			bool realVersusImaginary = _realVersusImaginaryButton.get_active();
			const TimeFrequencyData &data = _rfiGuiWindow.GetActiveData();

			if(_allValuesButton.get_active())
			{
				Plot2DPointSet *pointSet;
				if(realVersusImaginary)
					pointSet = &plot.StartLine("Data", Plot2DPointSet::DrawPoints);
				else
					pointSet = &plot.StartLine("Data (real)", Plot2DPointSet::DrawPoints);
				Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_rfiGuiWindow.AltMask()->Width(), _rfiGuiWindow.AltMask()->Height());
				RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, false);
	
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Data (imaginary)", Plot2DPointSet::DrawPoints);
					RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, true);
				}
			}

			if(_unmaskedValuesButton.get_active())
			{
				Plot2DPointSet *pointSet = &plot.StartLine("Without RFI");
				RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, _rfiGuiWindow.AltMask(), realVersusImaginary, false);
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Without RFI (I)");
					RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, _rfiGuiWindow.AltMask(), realVersusImaginary, true);
				}
			}
	
			if(_maskedValuesButton.get_active())
			{
				Plot2DPointSet *pointSet = &plot.StartLine("Only RFI");
				Mask2DPtr mask = Mask2D::MakePtr(*_rfiGuiWindow.AltMask());
				mask->Invert();
				RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, false);
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Only RFI (I)");
					RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, true);
				}
			}
	
			if(_fittedValuesButton.get_active())
			{
				Plot2DPointSet *pointSet;
				if(realVersusImaginary)
					pointSet = &plot.StartLine("Fit");
				else
					pointSet = &plot.StartLine("Fit (real)");
				size_t middleY = (2*y + avgSize) / 2;
				Baseline baseline(_rfiGuiWindow.SelectedMetaData()->Antenna1(), _rfiGuiWindow.SelectedMetaData()->Antenna2());
				long double fringeCount =
					UVImager::GetFringeCount(x, x+length, middleY, _rfiGuiWindow.SelectedMetaData());
				long double fringeFrequency = fringeCount / length;
				Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_rfiGuiWindow.AltMask()->Width(), _rfiGuiWindow.AltMask()->Height());
				RFIPlots::MakeFittedComplexPlot(*pointSet, data, x, length, y, avgSize, mask, fringeFrequency, realVersusImaginary, false);
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Fit (imaginary)");
					RFIPlots::MakeFittedComplexPlot(*pointSet, data, x, length, y, avgSize, mask, fringeFrequency, realVersusImaginary, true);
				}
			}

			if(_individualSampleFitButton.get_active())
			{
				FringeStoppingFitter fitter;
				fitter.Initialize(data);
				fitter.SetFitChannelsIndividually(true);
				fitter.SetFringesToConsider(1.0L);
				fitter.SetMaxWindowSize(256);
				fitter.SetReturnFittedValue(true);
				fitter.SetReturnMeanValue(false);
				
				fitter.SetMetaData(_rfiGuiWindow.SelectedMetaData());
				fitter.PerformStaticFrequencyFitOnOneChannel(y);

				Plot2DPointSet *pointSet;
				if(realVersusImaginary)
					pointSet = &plot.StartLine("Fit");
				else
					pointSet = &plot.StartLine("Fit (real)");
				Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_rfiGuiWindow.AltMask()->Width(), _rfiGuiWindow.AltMask()->Height());
				RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false);
	
				fitter.SetReturnFittedValue(false);
				fitter.SetReturnMeanValue(true);
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Fit (imaginary)");
					RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true);
				}

				fitter.PerformStaticFrequencyFitOnOneChannel(y);

				pointSet = &plot.StartLine("Center");
				RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false);
	
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Center (I)");
					RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true);
				}
			}
	
			if(_fringeFitButton.get_active() || _dynamicFringeFitButton.get_active())
			{
				/*FringeStoppingFitter fitter;
				Image2DPtr zero = Image2D::CreateZeroImagePtr(data.ImageWidth(), data.ImageHeight());
				Image2DPtr ones = Image2D::CreateZeroImagePtr(data.ImageWidth(), data.ImageHeight());
				for(size_t yi=0;yi<ones->Height();++yi)
					for(size_t xi=0;xi<ones->Width();++xi)
						ones->SetValue(xi, yi, 1.0L);
				TimeFrequencyData data(StokesIPolarisation, ones, zero);
				fitter.Initialize(data);
				fitter.SetFitChannelsIndividually(true);
				
				fitter.SetMetaData(_rfiGuiWindow.TimeFrequencyMetaData());
				fitter.PerformFringeStop();

				plot.StartLine("Fringe rotation");
				Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_rfiGuiWindow.AltMask()->Width(), _rfiGuiWindow.AltMask()->Height());
				RFIPlots::MakeComplexPlanePlot(plot, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false);
	
				if(!realVersusImaginary)
				{
					plot.StartLine("Fringe rotation (I)");
					RFIPlots::MakeComplexPlanePlot(plot, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true);
				}*/

				FringeStoppingFitter fitter;
				fitter.Initialize(data);
				
				fitter.SetMetaData(_rfiGuiWindow.SelectedMetaData());
				//fitter.PerformFringeStop();
				fitter.SetReturnFittedValue(true);
				if(_dynamicFringeFitButton.get_active())
					fitter.PerformDynamicFrequencyFit(y, y + avgSize, 200);
				else
					fitter.PerformDynamicFrequencyFit(y, y + avgSize);

				Plot2DPointSet *pointSet;
				if(realVersusImaginary)
					pointSet = &plot.StartLine("Fit");
				else
					pointSet = &plot.StartLine("Fit (real)");
				Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_rfiGuiWindow.AltMask()->Width(), _rfiGuiWindow.AltMask()->Height());
				RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false);
	
				if(!realVersusImaginary)
				{
					pointSet = &plot.StartLine("Fit (imaginary)");
					RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true);
				}
			}

			_plotManager.Update();
			
		} catch(std::exception &e)
		{
			Gtk::MessageDialog dialog(*this, e.what(), false, Gtk::MESSAGE_ERROR);
			dialog.run();
		}
	}
}

void ComplexPlanePlotWindow::setDetailsLabel()
{
	size_t x = (size_t) _xPositionScale.get_value();
	size_t y = (size_t) _yPositionScale.get_value();
	size_t length = (size_t) _lengthScale.get_value();
	size_t avgSize = (size_t) _ySumLengthScale.get_value();
	size_t middleY = (2*y + avgSize) / 2;
	TimeFrequencyMetaDataCPtr metaData = _rfiGuiWindow.SelectedMetaData();

	double timeStart = _observationTimes[x];
	double deltaTime;
	if(_observationTimes.size()>1)
		deltaTime = _observationTimes[1] - _observationTimes[0];
	else
		deltaTime = 1.0;
	long double frequency = metaData->Band().channels[middleY].frequencyHz;
	Baseline baseline(metaData->Antenna1(), metaData->Antenna2());
	long double delayRA = metaData->Field().delayDirectionRA;
	long double delayDec = metaData->Field().delayDirectionDec;
	long double intFringeFreq =
		UVImager::GetFringeCount(x, x+length, y, metaData);
	long double midFringeFreq =
		UVImager::GetFringeStopFrequency((x*2 + length)/2, baseline, delayRA, delayDec, frequency, metaData);

	std::stringstream s;
	s << "Start time: " << Date::AipsMJDToString(timeStart) << std::endl
		<< "Frequency: " << frequency/1000000.0L << "Mhz" << std::endl
		<< "Baseline: " << baseline.Distance() << "m" << std::endl
		<< "Delay direction: " << delayRA << "RA, " << delayDec << "dec." << std::endl
		<< "(=" << RightAscension::ToString(delayRA) << " RA, " << Declination::ToString(delayDec) << " dec.)" << std::endl
		<< "Mid fringe stopping freq: " << midFringeFreq << "(Hz)" << std::endl
		<< "Fringe count: " << intFringeFreq << std::endl
		<< "Fringe length: " << 1.0L/intFringeFreq << "(s)" << std::endl
		<< "Time step: " << deltaTime << "(s)" << std::endl
		<< "Samples/fringe: " << (1.0L / (deltaTime * intFringeFreq)) << std::endl
		<< "Fringes in domain: " << intFringeFreq << std::endl;
	
	_detailsLabel.set_text(s.str());
}