6 #include "system/system_call.h" 15 std::string input_string;
16 input_string += epsilon.
str();
18 input_string += parameter.
str();
28 std::istringstream sstrm(simulator_output);
29 std::getline(sstrm, line);
32 if (sstrm.eof() || (sstrm.peek() != EOF))
34 std::string error_msg;
35 error_msg +=
"Simulator output must contain exactly one " 36 "newline-terminated line, given output: ";
37 error_msg += simulator_output;
38 throw std::runtime_error(error_msg);
42 if (line.compare(
"1") == 0)
44 else if (line.compare(
"accept") == 0)
46 else if (line.compare(
"accepted") == 0)
48 if (line.compare(
"0") == 0)
50 else if (line.compare(
"reject") == 0)
52 else if (line.compare(
"rejected") == 0)
57 std::string error_msg;
58 error_msg +=
"Cannot parse output of simulator: ";
59 error_msg += simulator_output;
60 throw std::runtime_error(error_msg);
69 std::istringstream sstrm(prior_sampler_output);
70 std::getline(sstrm, line);
73 if (sstrm.eof() || (sstrm.peek() != EOF))
75 std::string error_msg;
76 error_msg +=
"Prior_sampler output must contain exactly one " 77 "newline-terminated line, given output: ";
78 error_msg += prior_sampler_output;
79 throw std::runtime_error(error_msg);
87 catch (
const std::runtime_error& e)
89 std::string error_msg;
90 error_msg += e.what();
92 error_msg +=
"Cannot parse output of prior_sampler: ";
93 error_msg += prior_sampler_output;
94 throw std::runtime_error(error_msg);
101 std::string input_string;
102 input_string += std::to_string(t);
103 input_string +=
'\n';
104 input_string += parameter.
str();
105 input_string +=
'\n';
114 return perturber_output;
116 catch (
const std::runtime_error& e)
118 std::string error_msg;
119 error_msg += e.what();
121 error_msg +=
"Cannot parse output of perturber: ";
122 error_msg += perturber_output;
123 throw std::runtime_error(error_msg);
130 std::string input_string;
131 input_string += parameter.
str();
132 input_string +=
'\n';
141 std::istringstream sstrm(prior_pdf_output);
142 std::getline(sstrm, line);
145 if (sstrm.eof() || (sstrm.peek() != EOF))
147 std::string error_msg;
148 error_msg +=
"Prior_pdf output must contain exactly one " 149 "newline-terminated line, given output: ";
150 error_msg += prior_pdf_output;
151 throw std::runtime_error(error_msg);
157 return std::stod(line);
159 catch (
const std::invalid_argument& e)
161 std::string error_msg;
162 error_msg +=
"Invalid argument: ";
163 error_msg += e.what();
165 error_msg +=
"Cannot parse output of prior_pdf: ";
166 error_msg += prior_pdf_output;
167 throw std::runtime_error(error_msg);
169 catch (
const std::out_of_range& e)
171 std::string error_msg;
172 error_msg +=
"Out of range: ";
173 error_msg += e.what();
175 error_msg +=
"Cannot parse output of prior_pdf: ";
176 error_msg += prior_pdf_output;
177 throw std::runtime_error(error_msg);
185 const std::vector<Parameter>& parameter_population)
187 std::string input_string;
188 input_string += std::to_string(t);
189 input_string +=
'\n';
190 input_string += perturbed_parameter.
str();
191 input_string +=
'\n';
193 for (
const Parameter& parameter : parameter_population)
195 input_string += parameter.str();
196 input_string +=
'\n';
203 const std::string& perturbation_pdf_output)
207 if (perturbation_pdf_output.back() !=
'\n')
209 std::string error_msg;
210 error_msg +=
"Perturbation_pdf output must end with newline, " 212 error_msg += perturbation_pdf_output;
213 throw std::runtime_error(error_msg);
217 std::istringstream sstrm(perturbation_pdf_output);
219 std::vector<double> perturbation_pdf_vector;
224 while (std::getline(sstrm, line))
225 perturbation_pdf_vector.push_back(std::stod(line));
227 catch (
const std::invalid_argument& e)
229 std::string error_msg;
230 error_msg +=
"Invalid argument: ";
231 error_msg += e.what();
233 error_msg +=
"Cannot parse output of perturbation_pdf: ";
234 error_msg += perturbation_pdf_output;
235 throw std::runtime_error(error_msg);
237 catch (
const std::out_of_range& e)
239 std::string error_msg;
240 error_msg +=
"Out of range: ";
241 error_msg += e.what();
243 error_msg +=
"Cannot parse output of perturbation_pdf: ";
244 error_msg += perturbation_pdf_output;
245 throw std::runtime_error(error_msg);
249 return perturbation_pdf_vector;
254 const std::string& generator_output)
258 if (generator_output.back() !=
'\n')
260 std::string error_msg;
261 error_msg +=
"Generator output must end with newline, " 263 error_msg += generator_output;
264 throw std::runtime_error(error_msg);
268 std::istringstream sstrm(generator_output);
270 std::vector<Parameter> generator_vector;
275 while (std::getline(sstrm, line))
276 generator_vector.push_back(std::move(line));
278 catch (
const std::runtime_error& e)
280 std::string error_msg;
281 error_msg += e.what();
283 error_msg +=
"Cannot parse output of generator: ";
284 error_msg += generator_output;
285 throw std::runtime_error(error_msg);
289 return generator_vector;
296 std::string prior_sampler_output = system_call(prior_sampler);
306 std::string perturber_output = system_call(perturber, perturber_input);
314 std::string prior_pdf_output = system_call(prior_pdf, prior_pdf_input);
320 int t,
const Parameter& perturbed_parameter,
321 const std::vector<Parameter>& parameter_population)
324 perturbed_parameter, parameter_population);
325 std::string perturbation_pdf_output = system_call(perturbation_pdf,
326 perturbation_pdf_input);
std::string format_perturbation_pdf_input(int t, const Parameter &perturbed_parameter, const std::vector< Parameter > ¶meter_population)
const std::string & str() const
std::string format_simulator_input(const Epsilon &epsilon, const Parameter ¶meter)
std::string format_perturber_input(int t, const Parameter &source_parameter)
std::vector< double > get_perturbation_pdf(const Command &perturbation_pdf, int t, const Parameter &perturbed_parameter, const std::vector< Parameter > ¶meter_population)
Parameter parse_perturber_output(const std::string &perturber_output)
Parameter sample_from_prior(const Command &prior_sampler)
double parse_prior_pdf_output(const std::string &prior_pdf_output)
std::string format_prior_pdf_input(const Parameter ¶meter)
Parameter perturb_parameter(const Command &perturber, int t, Parameter source_parameter)
Parameter parse_prior_sampler_output(const std::string &prior_sampler_output)
double get_prior_pdf(const Command &prior_pdf, Parameter parameter)
std::vector< Parameter > parse_generator_output(const std::string &generator_output)
bool parse_simulator_output(const std::string &simulator_output)
std::vector< double > parse_perturbation_pdf_output(const std::string &perturbation_pdf_output)