Pakman
ABCRejectionControllerStatic.cc
1 #include <fstream>
2 #include <string>
3 
4 #include "core/common.h"
5 #include "core/LongOptions.h"
6 #include "core/Arguments.h"
7 #include "interface/input.h"
8 
9 #include "ABCRejectionController.h"
10 
11 // Static help function
13 {
14  return
15 R"(* Help message for 'rejection' controller *
16 
17 Description:
18  The ABC rejection method samples candidate parameters from the stdout of
19  'prior_sampler'. For every candidate parameter, 'simulator' is invoked and
20  given two lines as its input; the first line contains 'epsilon' and the
21  second line contains the candidate parameter.
22 
23  The output of 'simulator' indicates whether the parameter was accepted or
24  rejected; an output of '0', 'reject' or 'rejected' means that the parameter
25  was rejected and an output of '1', 'accept' or 'accepted' means that the
26  parameter was accepted.
27 
28  Upon completion, the controller outputs the parameter names, followed by
29  newline-separated list of accepted parameters.
30 
31 Required arguments:
32  -N, --number-accept=NUM NUM is number of parameters to accept
33  -E, --epsilon=EPS EPS is the tolerance passed to 'simulator'
34  -P, --parameter-names=NAMES NAMES is a comma-separated list of
35  parameter names
36  -S, --simulator=CMD CMD is simulator command
37  -R, --prior-sampler=CMD CMD is prior_sampler command
38 )";
39 }
40 
42  LongOptions& lopts)
43 {
44  lopts.add({"number-accept", required_argument, nullptr, 'N'});
45  lopts.add({"epsilon", required_argument, nullptr, 'E'});
46  lopts.add({"parameter-names", required_argument, nullptr, 'P'});
47  lopts.add({"simulator", required_argument, nullptr, 'S'});
48  lopts.add({"prior-sampler", required_argument, nullptr, 'R'});
49 }
50 
51 // Static function to make from positional arguments
53  const Arguments& args)
54 {
55  Input input_obj;
56 
57  // Parse command-line options
58  input_obj = Input::makeInput(args);
59 
60  // Make ABCRejectionController
61  return new ABCRejectionController(input_obj);
62 }
63 
64 // Construct Input from Arguments object
66  const Arguments& args)
67 {
68  // Initialize input
69  Input input_obj;
70 
71  try
72  {
73  input_obj.number_accept =
74  parse_integer(args.optionalArgument("number-accept"));
75 
76  input_obj.epsilon = parse_epsilon(args.optionalArgument("epsilon"));
77 
78  input_obj.parameter_names =
79  parse_parameter_names(args.optionalArgument("parameter-names"));
80 
81  input_obj.simulator =
82  parse_command(args.optionalArgument("simulator"));
83 
84  input_obj.prior_sampler =
85  parse_command(args.optionalArgument("prior-sampler"));
86  }
87  catch (const std::out_of_range& e)
88  {
89  std::string error_msg;
90  error_msg += "Out of range: ";
91  error_msg += e.what();
92  error_msg += '\n';
93  error_msg += "One or more arguments missing or incorrect, try '";
94  error_msg += g_program_name;
95  error_msg += " rejection --help' for more info";
96  throw std::runtime_error(error_msg);
97  }
98  catch (const std::invalid_argument& e)
99  {
100  std::string error_msg;
101  error_msg += " Invalid argument: ";
102  error_msg += e.what();
103  error_msg += '\n';
104  error_msg += "One or more arguments missing or incorrect, try '";
105  error_msg += g_program_name;
106  error_msg += " rejection --help' for more info";
107  throw std::runtime_error(error_msg);
108  }
109 
110  return input_obj;
111 }
Command parse_command(const std::string &raw_input)
Definition: input.cc:19
const char * g_program_name
Definition: main.cc:22
static ABCRejectionController * makeController(const Arguments &args)
static Input makeInput(const Arguments &args)
static void addLongOptions(LongOptions &lopts)
std::vector< ParameterName > parse_parameter_names(const std::string &raw_input)
Definition: input.cc:42
Epsilon parse_epsilon(const std::string &raw_input)
Definition: input.cc:24
int parse_integer(const std::string &raw_input)
Definition: input.cc:9
std::vector< ParameterName > parameter_names
void add(struct option long_opt)
Definition: LongOptions.cc:20
ABCRejectionController(const Input &input_obj)
std::string optionalArgument(const std::string &option_name) const
Definition: Arguments.cc:45