File: rfiguicontroller.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (614 lines) | stat: -rw-r--r-- 20,340 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include "rfiguicontroller.h"

#include "../../algorithms/svdmitigater.h"
#include "../../algorithms/testsetgenerator.h"

#include "../../python/pythonstrategy.h"

#include "../../imagesets/h5imageset.h"
#include "../../imagesets/imageset.h"
#include "../../imagesets/joinedspwset.h"
#include "../../imagesets/msimageset.h"
#include "../../imagesets/msoptions.h"
#include "../../imagesets/multibandmsimageset.h"

#include "../../lua/luastrategy.h"
#include "../../lua/scriptdata.h"
#include "../../lua/telescopefile.h"

#include "../../msio/singlebaselinefile.h"

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

#include "../../quality/histogramcollection.h"

#include "../../structures/spatialmatrixmetadata.h"

#include "../../util/multiplot.h"
#include "../../util/progress/progresslistener.h"
#include "../../util/process.h"
#include "../../util/rfiplots.h"

#include "../rfiguiwindow.h"

#include "../../external/npy.hpp"

#include "imagecomparisoncontroller.h"

#include <aocommon/system.h>

#include <gtkmm/messagedialog.h>

#include <algorithm>
#include <array>
#include <filesystem>
#include <system_error>
#include <thread>

using algorithms::SVDMitigater;
using algorithms::TestSetGenerator;

RFIGuiController::RFIGuiController()
    : _showOriginalFlags(true),
      _showAlternativeFlags(true),
      _showPP(true),
      _showPQ(false),
      _showQP(false),
      _showQQ(true),
      _rfiGuiWindow(nullptr),
      _tfController() {
  _plotManager.reset(new class PlotManager());

  _settings.Load();
  NewDefaultStrategy();
}

RFIGuiController::~RFIGuiController() {
  try {
    _settings.Save();
  } catch (std::exception& e) {
    Logger::Error << "Failed to write config file: " << e.what() << '\n';
  }
}

bool RFIGuiController::IsImageLoaded() const {
  return _tfController.Plot().HasImage();
}

TimeFrequencyData RFIGuiController::ActiveData() const {
  return _tfController.GetActiveData();
}

TimeFrequencyData RFIGuiController::OriginalData() const {
  return _tfController.OriginalData();
}

TimeFrequencyMetaDataCPtr RFIGuiController::SelectedMetaData() const {
  return _tfController.Plot().GetSelectedMetaData();
}

void RFIGuiController::plotMeanSpectrum(bool weight) {
  if (IsImageLoaded()) {
    const std::string title = weight ? "Sum spectrum" : "Mean spectrum";
    XYPlot& plot = _plotManager->NewPlot2D(title);

    const TimeFrequencyData data = ActiveData();
    Mask2DCPtr mask =
        Mask2D::CreateSetMaskPtr<false>(data.ImageWidth(), data.ImageHeight());
    XYPointSet& beforeSet = plot.StartLine("Without flagging");
    if (weight)
      RFIPlots::MakeMeanSpectrumPlot<true>(beforeSet, data, mask,
                                           SelectedMetaData());
    else
      RFIPlots::MakeMeanSpectrumPlot<false>(beforeSet, data, mask,
                                            SelectedMetaData());

    mask.reset(new Mask2D(*data.GetSingleMask()));
    if (!mask->AllFalse()) {
      XYPointSet& afterSet = plot.StartLine("Flagged");
      if (weight)
        RFIPlots::MakeMeanSpectrumPlot<true>(afterSet, data, mask,
                                             SelectedMetaData());
      else
        RFIPlots::MakeMeanSpectrumPlot<false>(afterSet, data, mask,
                                              SelectedMetaData());
    }

    _plotManager->Update();
  }
}

void RFIGuiController::PlotDist() {
  if (IsImageLoaded()) {
    XYPlot& plot = _plotManager->NewPlot2D("Distribution");

    const TimeFrequencyData activeData = ActiveData();
    const Image2DCPtr image = activeData.GetSingleImage();
    Mask2DPtr mask =
        Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height());
    XYPointSet& totalSet = plot.StartLine("Total");
    RFIPlots::MakeDistPlot(totalSet, image, mask);

    XYPointSet& uncontaminatedSet = plot.StartLine("Uncontaminated");
    mask.reset(new Mask2D(*activeData.GetSingleMask()));
    RFIPlots::MakeDistPlot(uncontaminatedSet, image, mask);

    mask->Invert();
    XYPointSet& rfiSet = plot.StartLine("RFI");
    RFIPlots::MakeDistPlot(rfiSet, image, mask);

    _plotManager->Update();
  }
}

void RFIGuiController::PlotLogLogDist() {
  if (IsImageLoaded()) {
    const TimeFrequencyData activeData = ActiveData();
    HistogramCollection histograms(activeData.PolarizationCount());
    for (unsigned p = 0; p != activeData.PolarizationCount(); ++p) {
      const TimeFrequencyData polData(activeData.MakeFromPolarizationIndex(p));
      const Image2DCPtr image = polData.GetSingleImage();
      const Mask2DCPtr mask = Mask2D::MakePtr(*polData.GetSingleMask());
      histograms.Add(0, 1, p, image, mask);
    }
    _rfiGuiWindow->ShowHistogram(histograms);
  }
}

void RFIGuiController::PlotPowerSpectrum() {
  if (IsImageLoaded()) {
    XYPlot& plot = _plotManager->NewPlot2D("Power spectrum");
    plot.YAxis().SetLogarithmic(true);

    const TimeFrequencyData data = ActiveData();
    std::array<Image2DCPtr, 2> images;
    if (data.ComplexRepresentation() == TimeFrequencyData::ComplexParts) {
      images = data.GetSingleComplexImage();
    } else {
      images[0] = data.GetSingleImage();
      images[1] = images[0];
    }
    Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(images[0]->Width(),
                                                     images[0]->Height());
    XYPointSet& beforeSet = plot.StartLine("Flags not applied");
    RFIPlots::MakePowerSpectrumPlot(beforeSet, *images[0], *images[1], *mask,
                                    SelectedMetaData().get());

    mask = Mask2D::MakePtr(*data.GetSingleMask());
    if (!mask->AllFalse()) {
      XYPointSet& afterSet = plot.StartLine("Flags applied");
      RFIPlots::MakePowerSpectrumPlot(afterSet, *images[0], *images[1], *mask,
                                      SelectedMetaData().get());
    }

    _plotManager->Update();
  }
}

void RFIGuiController::PlotFrequencyScatter() {
  if (IsImageLoaded()) {
    MultiPlot plot(_plotManager->NewPlot2D("Frequency scatter"), 4);
    RFIPlots::MakeFrequencyScatterPlot(plot, ActiveData(), SelectedMetaData());
    plot.Finish();
    _plotManager->Update();
  }
}

void RFIGuiController::DrawTimeMean(XYPlot& plot) {
  plot.SetTitle("Mean over time");
  plot.YAxis().SetLogarithmic(true);
  plot.XAxis().SetType(AxisType::kTime);

  const TimeFrequencyData activeData = ActiveData();
  const Image2DCPtr image = activeData.GetSingleImage();
  Mask2DPtr mask =
      Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height());
  XYPointSet& totalPlot = plot.StartLine("Without flagging");
  RFIPlots::MakePowerTimePlot(totalPlot, image, mask, SelectedMetaData());

  mask = Mask2D::MakePtr(*activeData.GetSingleMask());
  if (!mask->AllFalse()) {
    XYPointSet& uncontaminatedPlot = plot.StartLine("With flagging");
    RFIPlots::MakePowerTimePlot(uncontaminatedPlot, image, mask,
                                SelectedMetaData());
  }
}

void RFIGuiController::PlotTimeMean() {
  if (IsImageLoaded()) {
    XYPlot& plot = _plotManager->NewPlot2D("Mean over time");
    DrawTimeMean(plot);
    _plotManager->Update();
  }
}

void RFIGuiController::PlotTimeScatter() {
  if (IsImageLoaded()) {
    MultiPlot plot(_plotManager->NewPlot2D("Time scatter"), 4);
    RFIPlots::MakeTimeScatterPlot(plot, ActiveData(), SelectedMetaData());
    plot.Finish();
    _plotManager->Update();
  }
}

void RFIGuiController::PlotSingularValues() {
  if (IsImageLoaded()) {
    XYPlot& plot = _plotManager->NewPlot2D("Singular values");

    SVDMitigater::CreateSingularValueGraph(ActiveData(), plot);
    _plotManager->Update();
  }
}

void RFIGuiController::open(std::unique_ptr<imagesets::ImageSet> imageSet) {
  std::vector<std::string> filenames = imageSet->Files();
  if (filenames.size() == 1)
    Logger::Info << "Opened " << filenames[0] << '\n';
  else
    Logger::Info << "Opening multiple files.\n";

  SetImageSet(std::move(imageSet));

  if (filenames.size() == 1) setRecentFile(filenames[0]);
}

void RFIGuiController::Open(const std::vector<std::string>& filenames) {
  std::unique_lock<std::mutex> lock(_ioMutex);
  MSOptions options;
  options.ioMode = DirectReadMode;
  options.baselineIntegration.enable = false;
  std::unique_ptr<imagesets::ImageSet> imageSet(
      imagesets::ImageSet::Create(filenames, options));
  imageSet->Initialize();
  lock.unlock();

  open(std::move(imageSet));
}

void RFIGuiController::OpenMsConcatenated(
    const std::vector<std::string>& filenames, const MSOptions& options) {
  constexpr size_t kMaxIoThreads = 16;
  const size_t n_io_threads = std::min<size_t>(
      {kMaxIoThreads, aocommon::system::ProcessorCount(), filenames.size()});

  std::unique_ptr<imagesets::ImageSet> image_set =
      std::make_unique<imagesets::MultiBandMsImageSet>(
          filenames, options.ioMode, options.intervalStart,
          options.intervalStart, n_io_threads);

  image_set->Initialize();

  open(std::move(image_set));
}

void RFIGuiController::OpenMS(const std::vector<std::string>& filenames,
                              const MSOptions& options) {
  if (options.concatenateFrequency) {
    OpenMsConcatenated(filenames, options);
    return;
  }

  std::unique_ptr<imagesets::ImageSet> imageSet(
      imagesets::ImageSet::Create(filenames, options));

  if (imagesets::H5ImageSet* h5ImageSet =
          dynamic_cast<imagesets::H5ImageSet*>(imageSet.get());
      h5ImageSet != nullptr) {
    h5ImageSet->SetInterval(options.intervalStart, options.intervalEnd);
  } else if (imagesets::MSImageSet* msImageSet =
                 dynamic_cast<imagesets::MSImageSet*>(imageSet.get());
             msImageSet != nullptr) {
    msImageSet->SetDataColumnName(options.dataColumnName);
    msImageSet->SetInterval(options.intervalStart, options.intervalEnd);

    msImageSet->SetReadUVW(true);

    if (options.combineSPWs) {
      msImageSet->Initialize();
      imageSet.release();
      std::unique_ptr<imagesets::MSImageSet> msImageSetPtr(msImageSet);
      imageSet.reset(new imagesets::JoinedSPWSet(std::move(msImageSetPtr)));
    }
  }
  imageSet->Initialize();

  open(std::move(imageSet));
}

void RFIGuiController::SaveBaselineFlags() {
  _imageSet->AddWriteFlagsTask(_imageSetIndex,
                               _tfController.GetActiveDataFullSize());
  _imageSet->PerformWriteFlagsTask();
}

void RFIGuiController::setRecentFile(const std::string& filename) {
  std::string absFilename = std::filesystem::absolute(filename).string();
  std::vector<std::string> files = _settings.RecentFiles();
  files.emplace(files.begin(), absFilename);
  if (files.size() > 10) files.resize(10);
  for (size_t i = 1; i != files.size(); ++i) {
    std::error_code ec;
    if (files[i] == absFilename ||
        std::filesystem::equivalent(files[i], absFilename, ec)) {
      files.erase(files.begin() + i);
      break;
    }
  }
  _settings.SetRecentFiles(files);
  _signalRecentFilesChanged.emit();
}

std::vector<std::string> RFIGuiController::RecentFiles() const {
  std::vector<std::string> files = _settings.RecentFiles();
  std::vector<std::string> shownFiles;
  shownFiles.reserve(files.size());
  for (size_t i = 0; i != files.size(); ++i) {
    if (!files[i].empty()) {
      // Don't return the current open file as a recent file
      std::error_code ec;
      const bool isOpen =
          (HasImageSet() &&
           (files[i] == _imageSet->Files()[0] ||
            std::filesystem::equivalent(files[i], _imageSet->Files()[0], ec)));
      if (!isOpen) shownFiles.emplace_back(files[i]);
    }
  }
  return shownFiles;
}

void RFIGuiController::OpenTestSet(
    algorithms::RFITestSet rfiSet,
    algorithms::BackgroundTestSet backgroundSet) {
  size_t width = 1024, height = 512;
  TimeFrequencyMetaDataCPtr metaData;
  if (IsImageLoaded()) {
    const TimeFrequencyData activeData = ActiveData();
    width = activeData.ImageWidth();
    height = activeData.ImageHeight();
    metaData = SelectedMetaData();
  } else {
    metaData.reset(new TimeFrequencyMetaData());
  }
  CloseImageSet();

  const TimeFrequencyData data =
      TestSetGenerator::MakeTestSet(rfiSet, backgroundSet, width, height);
  _tfController.SetNewData(data, metaData);
  const char* name = "Simulated test set";
  _tfController.Plot().SetTitleText(name);

  if (_rfiGuiWindow != nullptr) {
    _rfiGuiWindow->SetBaselineInfo(true, false, name, name);
  }
}

void RFIGuiController::ExecutePythonStrategy() {
  PythonStrategy pythonStrategy;
  _tfController.ClearAllButOriginal();
  TimeFrequencyData data = OriginalData();

  ScriptData scriptData;
  scriptData.SetCanVisualize(true);
  pythonStrategy.Execute(data, SelectedMetaData(), scriptData);
  scriptData.SortVisualizations();
  for (size_t i = 0; i != scriptData.VisualizationCount(); ++i) {
    auto v = scriptData.GetVisualization(i);
    _tfController.AddVisualization(std::get<0>(v), std::get<1>(v));
  }

  _tfController.AddVisualization("Script result", data);
  _rfiGuiWindow->GetTimeFrequencyWidget().Update();
}

void RFIGuiController::ExecuteLuaStrategy(class ProgressListener& listener) {
  _tfController.ClearAllButOriginal();
  _processingThread = std::thread([&]() {
    try {
      _scriptTFData = OriginalData();
      const TimeFrequencyMetaDataCPtr metaData =
          _tfController.Plot().GetFullMetaData();

      _scriptData.reset(new ScriptData());
      _scriptData->SetCanVisualize(true);
      _scriptData->SetProgressListener(listener);
      LuaStrategy luaStrategy;
      luaStrategy.Initialize();
      luaStrategy.LoadFile(_settings.GetStrategyFilename().c_str());
      luaStrategy.Execute(_scriptTFData, metaData, *_scriptData, "execute");
      listener.OnFinish();
    } catch (std::exception& e) {
      listener.OnException(e);
    }
  });
}

void RFIGuiController::JoinLuaThread() {
  _processingThread.join();
  _scriptData->SortVisualizations();
  for (size_t i = 0; i != _scriptData->VisualizationCount(); ++i) {
    auto v = _scriptData->GetVisualization(i);
    _tfController.AddVisualization(std::get<0>(v), std::get<1>(v));
  }
  _scriptData.reset();

  _tfController.AddVisualization("Script result", std::move(_scriptTFData));
  _rfiGuiWindow->GetTimeFrequencyWidget().Update();
  _scriptTFData = TimeFrequencyData();
}

void RFIGuiController::SetImageSet(
    std::unique_ptr<imagesets::ImageSet> newImageSet) {
  _imageSetIndex = newImageSet->StartIndex();
  _imageSet = std::move(newImageSet);
}

void RFIGuiController::SetImageSetIndex(
    const imagesets::ImageSetIndex& newImageSetIndex) {
  _imageSetIndex = newImageSetIndex;
}

void RFIGuiController::CloseImageSet() {
  _imageSet.reset();
  _tfController.Clear();
  if (_rfiGuiWindow != nullptr)
    _rfiGuiWindow->SetBaselineInfo(true, false, "", "No data loaded");
  // Closing a file causes the recent files to change, since
  // the closed file now becomes a recent file.
  _signalRecentFilesChanged.emit();
}

void RFIGuiController::LoadCurrentTFDataAsync(ProgressListener& progress) {
  try {
    if (HasImageSet()) {
      const std::lock_guard<std::mutex> lock(_ioMutex);
      _imageSet->AddReadRequest(_imageSetIndex);
      _imageSet->PerformReadRequests(progress);
      _tempLoadedBaseline = _imageSet->GetNextRequested();
    }
  } catch (std::exception& exception) {
    _tempLoadedBaseline.reset();
    progress.OnException(exception);
  }
}

void RFIGuiController::LoadCurrentTFDataFinish(bool success) {
  if (success) {
    _tfController.SetNewData(std::move(_tempLoadedBaseline->Data()),
                             _tempLoadedBaseline->MetaData());
    _tempLoadedBaseline.reset();

    // We store these seperate, as they might access the measurement set. This
    // is not only faster (the names are used in the onMouse.. events) but also
    // less dangerous, since the set can be simultaneously accessed by another
    // thread. (thus the io mutex should be locked before calling below
    // statements).
    std::string name, description;
    std::unique_lock<std::mutex> lock(_ioMutex);
    name = GetImageSet().Name();
    description = GetImageSet().Description(GetImageSetIndex());
    lock.unlock();

    _tfController.Plot().SetTitleText(description);

    if (_rfiGuiWindow != nullptr) {
      // Disable forward/back buttons when only one baseline is available
      imagesets::ImageSetIndex firstIndex = _imageSet->StartIndex();
      firstIndex.Next();
      const bool multipleBaselines = !firstIndex.HasWrapped();

      _rfiGuiWindow->SetBaselineInfo(false, multipleBaselines, name,
                                     description);
    }
  } else {
    CloseImageSet();
  }
}

void RFIGuiController::CheckPolarizations(bool forceSignal) {
  const bool pp = _showPP, pq = _showPQ, qp = _showQP, qq = _showQQ;
  if (!pp && !pq && !qp && !qq) {
    _showPP = true;
    _showQQ = true;
  }
  _tfController.TryVisualizePolarizations(_showPP, _showPQ, _showQP, _showQQ);
  if (forceSignal || _showPP != pp || _showPQ != pq || _showQP != qp ||
      _showQQ != qq)
    _signalStateChange();
}

void RFIGuiController::GetAvailablePolarizations(bool& pp, bool& pq, bool& qp,
                                                 bool& qq) const {
  bool b[4];
  for (size_t i = 0; i != 4; ++i) {
    // Set b to false except for b[i]
    for (size_t j = 0; j != 4; ++j) b[j] = (j == i);
    _tfController.TryVisualizePolarizations(b[0], b[1], b[2], b[3]);
    switch (i) {
      case 0:
        pp = b[0];
        break;
      case 1:
        pq = b[1];
        break;
      case 2:
        qp = b[2];
        break;
      case 3:
        qq = b[3];
        break;
    }
  }
}

void RFIGuiController::SaveBaselineAsRfibl(const std::string& filename) {
  SingleBaselineFile file;
  file.data = _tfController.OriginalData();
  file.metaData = *_tfController.Plot().GetFullMetaData();
  file.telescopeName = _imageSet->TelescopeName();
  std::ofstream stream(filename);
  file.Write(stream);
}

void RFIGuiController::SaveBaselineAsNpy(const std::string& filename) {
  const TimeFrequencyData& tf_data = _tfController.OriginalData();
  std::vector<std::complex<num_t>> data = ToComplexVector(tf_data);
  const std::array<unsigned long, 3> shape = {
      tf_data.PolarizationCount(), tf_data.ImageHeight(), tf_data.ImageWidth()};
  npy::SaveArrayAsNumpy(filename, false, shape.size(), shape.data(), data);
}

void RFIGuiController::NewDefaultStrategy() {
  _settings.InitializeWorkStrategy();
  _openStrategyFilename.clear();
}

void RFIGuiController::NewEmptyStrategy() {
  const std::ofstream file(_settings.GetStrategyFilename());
  if (!file)
    throw std::runtime_error("Error writing new strategy to work file");
  _openStrategyFilename.clear();
}

void RFIGuiController::NewTemplateStrategy() {
  std::ofstream file(_settings.GetStrategyFilename());
  file << LuaStrategy::GetTemplateScript();
  if (!file)
    throw std::runtime_error("Error writing new strategy to work file");
  _openStrategyFilename.clear();
}

void RFIGuiController::OpenStrategy(const std::string& filename) {
  std::error_code ec;
  if (std::filesystem::equivalent(_settings.GetStrategyFilename(), filename,
                                  ec))
    throw std::runtime_error("Can't open working Lua strategy");
  std::filesystem::copy_file(filename, _settings.GetStrategyFilename(),
                             std::filesystem::copy_options::overwrite_existing);
  _openStrategyFilename = filename;
}

void RFIGuiController::SaveStrategy() {
  std::filesystem::copy_file(_settings.GetStrategyFilename(),
                             _openStrategyFilename,
                             std::filesystem::copy_options::overwrite_existing);
}

void RFIGuiController::SaveStrategyAs(const std::string& filename) {
  std::filesystem::copy_file(_settings.GetStrategyFilename(), filename,
                             std::filesystem::copy_options::overwrite_existing);
  _openStrategyFilename = filename;
}

std::string RFIGuiController::GetWorkStrategyText() const {
  const std::ifstream file(_settings.GetStrategyFilename());
  if (!file) throw std::runtime_error("Error reading work strategy from file");
  std::ostringstream text;
  text << file.rdbuf();
  return text.str();
}

void RFIGuiController::SetWorkStrategyText(const std::string& text) {
  std::ofstream file(_settings.GetStrategyFilename());
  if (!file) throw std::runtime_error("Error writing work strategy to file");
  file << text;
}