PURIFY
Next-generation radio interferometric imaging
Public Member Functions | List of all members
purify::YamlParser Class Reference

#include <yaml-parser.h>

Public Member Functions

 YamlParser (const std::string &filepath)
 
void readFile ()
 
void setParserVariablesFromYaml ()
 
void parseAndSetGeneralConfiguration (const YAML::Node &node)
 
void parseAndSetMeasureOperators (const YAML::Node &node)
 
void parseAndSetSARA (const YAML::Node &node)
 
std::vector< std::string > getWavelets (const std::string &values_str)
 
void parseAndSetAlgorithmOptions (const YAML::Node &node)
 
void writeOutput ()
 
std::string output_path () const
 

Detailed Description

Definition at line 25 of file yaml-parser.h.

Constructor & Destructor Documentation

◆ YamlParser()

purify::YamlParser::YamlParser ( const std::string &  filepath)

YamlParser constructor definition

Parameters
filepathpath to config file

Definition at line 19 of file yaml-parser.cc.

19  : filepath_(filepath) {
20  // Reading the file
21  this->readFile();
22  // Run a batch of methods to parse the YAML and set the
23  // class members accordingly
25  // Setting time stamp
26  // Get timestamp string
27  std::time_t t = std::time(0); // get time now
28  std::tm* now = std::localtime(&t);
29  // Make the datetime human readable
30  std::string datetime = std::to_string(now->tm_year + 1900) + '-' +
31  std::to_string(now->tm_mon + 1) + '-' + std::to_string(now->tm_mday);
32  datetime = datetime + '-' + std::to_string(now->tm_hour) + ':' + std::to_string(now->tm_min) +
33  ':' + std::to_string(now->tm_sec);
34 
35  this->timestamp_ = datetime;
36 }
void setParserVariablesFromYaml()
Definition: yaml-parser.cc:93

References readFile(), and setParserVariablesFromYaml().

Member Function Documentation

◆ getWavelets()

std::vector< std::string > purify::YamlParser::getWavelets ( const std::string &  values_str)

Translate the waveket basis string t_into a vector

Definition at line 281 of file yaml-parser.cc.

281  {
282  // input - values_str
283  // std::string values_str;
284  // values_str = "1, 2, 4..6, 11..18, 24, 31..41";
285  // //config["SARA"]["wavelet_dict"].as<std::string>();
286 
287  // Logic to extract the values as vectors
288  std::vector<std::string> wavelets;
289  std::string value2add;
290  std::string input = values_str;
291  input.erase(std::remove_if(input.begin(), input.end(), [](char x) { return std::isspace(x); }),
292  input.end());
293  // NOTE Maybe a while reststring and using find is better?
294  for (int i = 0; i <= input.size(); i++) {
295  if ((i == input.size()) || (input.at(i) == ',')) {
296  wavelets.push_back((value2add == "0") ? "Dirac" : ("DB" + value2add));
297  value2add = "";
298  } else if (input.at(i) == '.') {
299  // TODO throw exception if open ended: 9..
300  // TODO throw if at the begining
301  // TODO throw if 3 digits on side
302  const int n = ((i + 3) >= input.size()) ? 2 : ((input.at(i + 3) == ',') ? 2 : 3);
303  const std::string final_value = input.substr(i + 2, n);
304  // TODO throw if final_value < start value
305  for (int j = std::stoi(value2add); j <= std::stoi(final_value); j++)
306  wavelets.push_back((j == 0) ? "Dirac" : ("DB" + std::to_string(j)));
307  i += (n + 1);
308  value2add = "";
309  } else {
310  value2add = value2add + input.at(i);
311  }
312  }
313 
314  return wavelets;
315 }

Referenced by parseAndSetSARA().

◆ output_path()

std::string purify::YamlParser::output_path ( ) const
inline

Definition at line 152 of file yaml-parser.h.

152 { return out_path; }

Referenced by genHeaders(), main(), saveDirtyImage(), saveMeasurementEigenVector(), and savePSF().

◆ parseAndSetAlgorithmOptions()

void purify::YamlParser::parseAndSetAlgorithmOptions ( const YAML::Node &  node)

Parse the YAML AlgorithmOptions block and set the class members appropriately

Definition at line 208 of file yaml-parser.cc.

208  {
209  this->algorithm_ = get<std::string>(algorithmOptionsNode, {"algorithm"});
210  if (this->algorithm_ == "padmm") {
211  this->epsilonConvergenceScaling_ =
212  get<t_real>(algorithmOptionsNode, {"padmm", "epsilonConvergenceScaling"});
213  this->mpiAlgorithm_ = factory::algo_distribution_string.at(
214  get<std::string>(algorithmOptionsNode, {"padmm", "mpiAlgorithm"}));
215  this->relVarianceConvergence_ =
216  get<t_real>(algorithmOptionsNode, {"padmm", "relVarianceConvergence"});
217  this->update_iters_ = get<t_int>(algorithmOptionsNode, {"padmm", "stepsize", "update_iters"});
218  this->update_tolerance_ =
219  get<t_real>(algorithmOptionsNode, {"padmm", "stepsize", "update_tolerance"});
220  this->dualFBVarianceConvergence_ =
221  get<t_real>(algorithmOptionsNode, {"padmm", "dualFBVarianceConvergence"});
222  } else if (this->algorithm_ == "fb" or this->algorithm_ == "fb_joint_map") {
223  this->mpiAlgorithm_ = factory::algo_distribution_string.at(
224  get<std::string>(algorithmOptionsNode, {"fb", "mpiAlgorithm"}));
225  this->relVarianceConvergence_ =
226  get<t_real>(algorithmOptionsNode, {"fb", "relVarianceConvergence"});
227  this->stepsize_ = get<t_real>(algorithmOptionsNode, {"fb", "stepsize"});
228  this->regularisation_parameter_ =
229  get<t_real>(algorithmOptionsNode, {"fb", "regularisation_parameter"});
230  this->dualFBVarianceConvergence_ =
231  get<t_real>(algorithmOptionsNode, {"fb", "dualFBVarianceConvergence"});
232 
233  this->nondiffFuncType_ = nondiff_type_string.at(
234  get<std::string>(algorithmOptionsNode, {"fb", "nonDifferentiableFunctionType"}));
235  if (this->nondiffFuncType_ == nondiff_func_type::Denoiser) {
236  this->model_path_ = get<std::string>(algorithmOptionsNode, {"fb", "modelPath"});
237  }
238 
239  this->diffFuncType_ = diff_type_string.at(
240  get<std::string>(algorithmOptionsNode, {"fb", "differentiableFunctionType"}));
241  if (this->diffFuncType_ == diff_func_type::L2Norm_with_CRR) {
242  this->CRR_function_model_path_ =
243  get<std::string>(algorithmOptionsNode, {"fb", "CRR_function_model_path"});
244  this->CRR_gradient_model_path_ =
245  get<std::string>(algorithmOptionsNode, {"fb", "CRR_gradient_model_path"});
246  this->CRR_mu_ = get<t_real>(algorithmOptionsNode, {"fb", "CRR_mu"});
247  this->CRR_lambda_ = get<t_real>(algorithmOptionsNode, {"fb", "CRR_lambda"});
248  }
249 
250  if (this->algorithm_ == "fb_joint_map") {
251  this->jmap_iters_ =
252  get<t_uint>(algorithmOptionsNode, {"fb", "joint_map_estimation", "iters"});
253  this->jmap_relVarianceConvergence_ = get<t_real>(
254  algorithmOptionsNode, {"fb", "joint_map_estimation", "relVarianceConvergence"});
255  this->jmap_objVarianceConvergence_ = get<t_real>(
256  algorithmOptionsNode, {"fb", "joint_map_estimation", "objVarianceConvergence"});
257  this->jmap_alpha_ =
258  get<t_real>(algorithmOptionsNode, {"fb", "joint_map_estimation", "alpha"});
259  this->jmap_beta_ = get<t_real>(algorithmOptionsNode, {"fb", "joint_map_estimation", "beta"});
260  }
261  } else if (this->algorithm_ == "primaldual") {
262  this->epsilonConvergenceScaling_ =
263  get<t_real>(algorithmOptionsNode, {"primaldual", "epsilonConvergenceScaling"});
264  this->mpiAlgorithm_ = factory::algo_distribution_string.at(
265  get<std::string>(algorithmOptionsNode, {"primaldual", "mpiAlgorithm"}));
266  this->relVarianceConvergence_ =
267  get<t_real>(algorithmOptionsNode, {"primaldual", "relVarianceConvergence"});
268  this->update_iters_ =
269  get<t_int>(algorithmOptionsNode, {"primaldual", "stepsize", "update_iters"});
270  this->update_tolerance_ =
271  get<t_real>(algorithmOptionsNode, {"primaldual", "stepsize", "update_tolerance"});
272  this->precondition_iters_ =
273  get<t_int>(algorithmOptionsNode, {"primaldual", "precondition_iters"});
274  } else {
275  throw std::runtime_error(
276  "Only padmm algorithm configured for now. Please fill the appropriate block in the "
277  "configuration file.");
278  }
279 }
const std::map< std::string, algo_distribution > algo_distribution_string
const std::map< std::string, diff_func_type > diff_type_string
Definition: types.h:27
const std::map< std::string, nondiff_func_type > nondiff_type_string
Definition: types.h:32

References purify::factory::algo_distribution_string, purify::Denoiser, purify::diff_type_string, purify::L2Norm_with_CRR, and purify::nondiff_type_string.

Referenced by setParserVariablesFromYaml().

◆ parseAndSetGeneralConfiguration()

void purify::YamlParser::parseAndSetGeneralConfiguration ( const YAML::Node &  node)

Parse the YAML GeneralConfiguration block and set the class members appropriately

Definition at line 102 of file yaml-parser.cc.

102  {
103  this->logging_ = get<std::string>(generalConfigNode, {"logging"});
104  this->iterations_ = get<int>(generalConfigNode, {"iterations"});
105  this->epsilonScaling_ = get<t_real>(generalConfigNode, {"epsilonScaling"});
106  this->output_prefix_ = get<std::string>(generalConfigNode, {"InputOutput", "output_prefix"});
107 
108  const std::string source_str =
109  get<std::string>(generalConfigNode, {"InputOutput", "input", "source"});
110  if (source_str == "measurements") {
111  this->measurements_polarization_ = stokes_string.at(get<std::string>(
112  generalConfigNode, {"InputOutput", "input", "measurements", "measurements_polarization"}));
113  this->warm_start_ =
114  get<std::string>(generalConfigNode, {"InputOutput", "input", "measurements", "warm_start"});
115  if (generalConfigNode["InputOutput"]["input"]["simulation"])
116  throw std::runtime_error(
117  "Expecting only the input measurements block in the configuration file. Please remove "
118  "simulation block!");
120  this->measurements_ = get_vector<std::vector<std::string>>(
121  generalConfigNode, {"InputOutput", "input", "measurements", "measurements_files"});
122  try {
123  this->w_term_ =
124  get<bool>(generalConfigNode, {"InputOutput", "input", "measurements", "w_term"});
125  } catch (...) {
126  PURIFY_LOW_LOG("W-term flag not set for input measurements; defaulting to true.");
127  this->w_term_ = true;
128  }
129  // TODO: use the enum instead of string.
130  const std::string units_measurement_str = get<std::string>(
131  generalConfigNode, {"InputOutput", "input", "measurements", "measurements_units"});
132  if (units_measurement_str == "lambda")
133  this->measurements_units_ = purify::utilities::vis_units::lambda;
134  else if (units_measurement_str == "radians")
135  this->measurements_units_ = purify::utilities::vis_units::radians;
136  else if (units_measurement_str == "pixels")
137  this->measurements_units_ = purify::utilities::vis_units::pixels;
138  else
139  throw std::runtime_error("Visibility units \"" + units_measurement_str +
140  "\" not recognised. Check your config file.");
141  this->measurements_sigma_ = get<t_real>(
142  generalConfigNode, {"InputOutput", "input", "measurements", "measurements_sigma"});
143  } else if (source_str == "simulation") {
144  if (generalConfigNode["InputOutput"]["input"]["measurements"])
145  throw std::runtime_error(
146  "Expecting only the input simulation block in the configuration file. Please remove "
147  "measurements block!");
149  this->skymodel_ =
150  get<std::string>(generalConfigNode, {"InputOutput", "input", "simulation", "skymodel"});
151  this->signal_to_noise_ =
152  get<t_real>(generalConfigNode, {"InputOutput", "input", "simulation", "signal_to_noise"});
153  this->number_of_measurements_ = get<t_int>(
154  generalConfigNode, {"InputOutput", "input", "simulation", "number_of_measurements"});
155  this->sim_J_ = get<t_int>(generalConfigNode, {"InputOutput", "input", "simulation", "sim_J"});
156  this->w_rms_ = get<t_real>(generalConfigNode, {"InputOutput", "input", "simulation", "w_rms"});
157  this->measurements_ = get_vector<std::vector<std::string>>(
158  generalConfigNode, {"InputOutput", "input", "simulation", "coverage_files"});
159  // TODO: use the enum instead of string.
160  const std::string units_measurement_str = get<std::string>(
161  generalConfigNode, {"InputOutput", "input", "simulation", "coverage_units"});
162  if (units_measurement_str == "lambda")
163  this->measurements_units_ = purify::utilities::vis_units::lambda;
164  else if (units_measurement_str == "radians")
165  this->measurements_units_ = purify::utilities::vis_units::radians;
166  else if (units_measurement_str == "pixels")
167  this->measurements_units_ = purify::utilities::vis_units::pixels;
168  else
169  throw std::runtime_error("Visibility units \"" + units_measurement_str +
170  "\" not recognised. Check your config file.");
171  } else
172  throw std::runtime_error("Visibility source \"" + source_str +
173  "\" not recognised. Check your config file.");
174 }
#define PURIFY_LOW_LOG(...)
Low priority message.
Definition: logging.h:207
const std::map< std::string, stokes > stokes_string
Definition: types.h:49

References purify::utilities::lambda, purify::utilities::measurements, purify::utilities::pixels, PURIFY_LOW_LOG, purify::utilities::radians, purify::utilities::simulation, and purify::stokes_string.

Referenced by setParserVariablesFromYaml().

◆ parseAndSetMeasureOperators()

void purify::YamlParser::parseAndSetMeasureOperators ( const YAML::Node &  node)

Parse the YAML MeasureOperator block and set the class members appropriately

Definition at line 176 of file yaml-parser.cc.

176  {
177  this->kernel_ = get<std::string>(measureOperatorsNode, {"kernel"});
178  this->oversampling_ = get<float>(measureOperatorsNode, {"oversampling"});
179  this->powMethod_iter_ = get<int>(measureOperatorsNode, {"powermethod", "iters"});
180  this->powMethod_tolerance_ = get<float>(measureOperatorsNode, {"powermethod", "tolerance"});
181  this->eigenvector_real_ =
182  get<std::string>(measureOperatorsNode, {"powermethod", "eigenvector", "real"});
183  this->eigenvector_imag_ =
184  get<std::string>(measureOperatorsNode, {"powermethod", "eigenvector", "imag"});
185  this->cellsizex_ = get<double>(measureOperatorsNode, {"pixelSize", "cellsizex"});
186  this->cellsizey_ = get<double>(measureOperatorsNode, {"pixelSize", "cellsizey"});
187  this->width_ = get<int>(measureOperatorsNode, {"imageSize", "width"});
188  this->height_ = get<int>(measureOperatorsNode, {"imageSize", "height"});
189  this->Jx_ = get<unsigned int>(measureOperatorsNode, {"J", "Jx"});
190  this->Jy_ = get<unsigned int>(measureOperatorsNode, {"J", "Jy"});
191  this->Jw_ = get<unsigned int>(measureOperatorsNode, {"J", "Jw"});
192  this->gpu_ = get<bool>(measureOperatorsNode, {"gpu"});
193  this->wprojection_ = get<bool>(measureOperatorsNode, {"wide-field", "wprojection"});
194  this->mpi_wstacking_ = get<bool>(measureOperatorsNode, {"wide-field", "mpi_wstacking"});
195  this->mpi_all_to_all_ = get<bool>(measureOperatorsNode, {"wide-field", "mpi_all_to_all"});
196  this->kmeans_iters_ = get<t_int>(measureOperatorsNode, {"wide-field", "kmeans_iterations"});
197  this->conjugate_w_ = get<bool>(measureOperatorsNode, {"wide-field", "conjugate_w"});
198 }

Referenced by setParserVariablesFromYaml().

◆ parseAndSetSARA()

void purify::YamlParser::parseAndSetSARA ( const YAML::Node &  node)

Parse the YAML SARA block and set the class members appropriately

Definition at line 200 of file yaml-parser.cc.

200  {
201  const std::string values_str = get<std::string>(SARANode, {"wavelet_dict"});
202  this->wavelet_basis_ = this->getWavelets(values_str);
203  this->wavelet_levels_ = get<t_int>(SARANode, {"wavelet_levels"});
204  this->realValueConstraint_ = get<bool>(SARANode, {"realValueConstraint"});
205  this->positiveValueConstraint_ = get<bool>(SARANode, {"positiveValueConstraint"});
206 }
std::vector< std::string > getWavelets(const std::string &values_str)
Definition: yaml-parser.cc:281

References getWavelets().

Referenced by setParserVariablesFromYaml().

◆ readFile()

void purify::YamlParser::readFile ( )

Read configuration file in memory

Definition at line 38 of file yaml-parser.cc.

38  {
39  try {
40  YAML::Node config = YAML::LoadFile(this->filepath());
41  // A bit of defensive programming
42  assert(config.Type() == YAML::NodeType::Map);
43  this->config_file = config;
44  } catch (YAML::BadFile& exception) {
45  const std::string current_path = boost::filesystem::current_path().native();
46  throw(
47  std::runtime_error("Runtime error while trying to find config.yaml. The input file path " +
48  this->filepath() + " could not be found from " + current_path));
49  }
50 }

Referenced by YamlParser().

◆ setParserVariablesFromYaml()

void purify::YamlParser::setParserVariablesFromYaml ( )

Set the class members from the YAML inputs

Definition at line 93 of file yaml-parser.cc.

93  {
94  // Each of these methods correspond to blocks in the YAML
95  this->parseAndSetGeneralConfiguration(this->config_file["GeneralConfiguration"]);
96  this->parseAndSetMeasureOperators(this->config_file["MeasureOperators"]);
97  this->parseAndSetSARA(this->config_file["SARA"]);
98  this->parseAndSetAlgorithmOptions(this->config_file["AlgorithmOptions"]);
99  this->version_ = get<std::string>(this->config_file, {"Version"});
100 }
void parseAndSetMeasureOperators(const YAML::Node &node)
Definition: yaml-parser.cc:176
void parseAndSetSARA(const YAML::Node &node)
Definition: yaml-parser.cc:200
void parseAndSetGeneralConfiguration(const YAML::Node &node)
Definition: yaml-parser.cc:102
void parseAndSetAlgorithmOptions(const YAML::Node &node)
Definition: yaml-parser.cc:208

References parseAndSetAlgorithmOptions(), parseAndSetGeneralConfiguration(), parseAndSetMeasureOperators(), and parseAndSetSARA().

Referenced by YamlParser().

◆ writeOutput()

void purify::YamlParser::writeOutput ( )

Write an output YAML file for the current simulation parameters.

Definition at line 317 of file yaml-parser.cc.

317  {
318  // Get base file name (without path or extension)
319  std::size_t file_begin = this->filepath_.find_last_of("/");
320  if (file_begin == std::string::npos) file_begin = 0;
321  std::string file_path = filepath_.substr(0, file_begin);
322  std::string extension = ".yaml";
323  std::string base_file_name = this->filepath_.erase(this->filepath_.size() - extension.size());
324  base_file_name =
325  base_file_name.substr((file_path.size() ? file_path.size() + 1 : 0), base_file_name.size());
326  // Construct output directory structure and file name
327  boost::filesystem::path const path(this->output_prefix_);
328  out_path = output_prefix_ + "/output_" + std::string(this->timestamp());
329  mkdir_recursive(out_path);
330  std::string out_filename = out_path + "/" + base_file_name + "_save.yaml";
331 
332  // Write out the yaml info
333  YAML::Emitter out;
334  out << YAML::BeginMap;
335  out << YAML::Key << "Version";
336  out << this->version_;
337  out << YAML::Key << "GeneralConfiguration";
338  out << this->config_file["GeneralConfiguration"];
339  out << YAML::Key << "MeasureOperators";
340  out << this->config_file["MeasureOperators"];
341  out << YAML::Key << "SARA";
342  out << this->config_file["SARA"];
343  out << YAML::Key << "AlgorithmOptions";
344  out << this->config_file["AlgorithmOptions"];
345  out << YAML::EndMap;
346 
347  std::ofstream output_file;
348  output_file.open(out_filename);
349  output_file << out.c_str();
350  output_file.close();
351 }
void mkdir_recursive(const std::string &path)
recursively create directories when they do not exist

References purify::mkdir_recursive().

Referenced by initOutDirectoryWithConfig(), and TEST_CASE().


The documentation for this class was generated from the following files: