Calibrator  0.1
Command line tool for 14C calibration
cal_date.cpp
Go to the documentation of this file.
1 #include "../include/cal_date.h"
2 
3 CalDate::CalDate(string name, vector<double> probabilities, vector<int> bp, int uncal_bp, int uncal_error, vector<int> full_bp, vector<double> full_probabilities):
4  _name(name),
5  _probabilities(probabilities),
6  _bp(bp),
7  _uncal_bp(uncal_bp),
8  _uncal_error(uncal_error),
9  _full_probabilities(full_probabilities),
10  _full_bp(full_bp)
11  {}
12 
13 CalDate::CalDate(string name, vector<double> probabilities, vector<int> bp, int uncal_bp, int uncal_error):
14  _name(name),
15  _probabilities(probabilities),
16  _bp(bp),
17  _uncal_bp(uncal_bp),
18  _uncal_error(uncal_error)
19  {}
20 
22  _probabilities()
23  {}
24 
25 void CalDate::info()const{
26  std::cout << "calibrated_date";
27  std::cout << "bp: " << _uncal_bp;
28  std::cout << "std: " << _uncal_error;
29  std::cout << std::endl;
30 };
31 
33  json return_value;
34  return_value["uncal_bp"] = _uncal_bp;
35  return_value["uncal_error"] = _uncal_error;
36  return_value["probabilities"] = prob_to_json();
37  return_value["bp"] = bp_to_json();
38  return_value["sigma_ranges"] = sigma_ranges_to_json();
39  return return_value;
40 }
41 
42 json CalDate::prob_to_json(){
43  json return_value(_probabilities);
44  return return_value;
45 }
46 
47 json CalDate::bp_to_json(){
48  json return_value(_bp);
49  return return_value;
50 }
51 
53  return _name;
54 }
55 
56 vector<int> CalDate::get_full_bp() {
57  return _full_bp;
58 }
59 
61  return _full_probabilities;
62 }
63 
65  static const double arr[] = {1 - 0.954,1 - 0.682};
66  vector<int> my_sigma_ranges;
67  for (unsigned t = 0; t < sizeof(arr)/sizeof(arr[0]); t++) {
68  double this_sigma = arr[t];
69  my_sigma_ranges.clear();
70  while (my_sigma_ranges.empty() || my_sigma_ranges.size() % 2) {
71  my_sigma_ranges = sigma_range_helper(this_sigma);
72  }
73  for (unsigned i=0;i < my_sigma_ranges.size(); i += 2) {
74  SigmaRange this_sigma_range = SigmaRange(my_sigma_ranges[i], my_sigma_ranges[i+1], this_sigma);
75  _sigma_ranges.push_back(this_sigma_range);
76  }
77  }
78 }
79 
80 vector<int> CalDate::sigma_range_helper(double &prob){
81  int nrun = 5000;
82  vector<double> prob_vector;
83  vector<double> cum_sum(_probabilities.size());
84 
85  std::partial_sum(_probabilities.begin(), _probabilities.end(), cum_sum.begin(), plus<double>());
86 
87  double max=cum_sum.back();
88  for(int i = 0;
89  i < nrun;
90  ++i)
91  {
92  double linear = rand()*max/RAND_MAX;
93  int up = upper_bound(cum_sum.begin(), cum_sum.end(), linear) - cum_sum.begin();
94  prob_vector.push_back(_probabilities[up]);
95  }
96 
97  std::sort (prob_vector.begin(), prob_vector.end());
98 
99  int n = prob_vector.size();
100  int thres_index = floor( (n - 1) * prob);
101  double threshold = prob_vector[thres_index];
102  vector<int> index;
103 
104  vector<double>::iterator lower;
105  vector<double>::iterator upper;
106  unsigned i = 0;
107  while ( i < _probabilities.size()) {
108  lower = find_if (_probabilities.begin()+i, _probabilities.end(), // range
109  bind2nd(greater<double>(),threshold)); // criterion
110  upper = find_if (lower, _probabilities.end(), // range
111  bind2nd(less<double>(),threshold)); // criterion
112 
113  if (upper == _probabilities.end()) break;
114 
115  index.push_back(lower - _probabilities.begin());
116  index.push_back(upper - _probabilities.begin());
117  i = upper - _probabilities.begin();
118  i++;
119  }
120 
121  int ni = index.size();
122  vector<int> bp_collector;
123  if (ni > 0)
124  {
125  for (int j = 0; j < ni; j++)
126  {
127  int a = index[j];
128  int b = index[j]-1;
129  double y1 = _bp[a];
130  double y2 = _bp[b];
131  double mu = (float)(threshold-_probabilities[a]) / (float)(_probabilities[b] - _probabilities[a]);
132  int this_sigma_end = round(LinearInterpolate(y1, y2, mu));
133  bp_collector.push_back(this_sigma_end);
134  }
135  }
136  std::unique (bp_collector.begin(), bp_collector.end());
137  bp_collector.erase( unique( bp_collector.begin(), bp_collector.end() ), bp_collector.end() );
138  return bp_collector;
139 }
140 
141 double CalDate::LinearInterpolate(double y1, double y2, double mu){
142  return(y1*(1-mu)+y2*mu);
143 }
144 
145 vector<SigmaRange> CalDate::get_sigma_ranges() {
146  return _sigma_ranges;
147 }
148 
149 json CalDate::sigma_ranges_to_json() {
150  json sigma_ranges_json;
151  for(auto &this_sigma_range : _sigma_ranges)
152  sigma_ranges_json.push_back(this_sigma_range.to_json());
153  return sigma_ranges_json;
154 }
155 
156 string CalDate::to_csv() {
157  std::stringstream ss;
158 
159  for(unsigned i = 0; i < _bp.size(); ++i)
160  {
161  ss << _name << "," << _bp[i] << "," << _probabilities[i] << "\n";
162  }
163  std::string return_value = ss.str();
164 
165  return return_value;
166 }
string to_csv()
Definition: cal_date.cpp:156
nlohmann::json json
Definition: cal_date.h:29
CalDate()
Definition: cal_date.cpp:21
void info() const
Definition: cal_date.cpp:25
string get_name()
Definition: cal_date.cpp:52
vector< double > get_full_probabilities()
Definition: cal_date.cpp:60
Represents a sigma range for a calibrated date.
Definition: sigma_range.h:26
json to_json()
Definition: cal_date.cpp:32
vector< SigmaRange > get_sigma_ranges()
Definition: cal_date.cpp:145
vector< int > get_full_bp()
Definition: cal_date.cpp:56
void calculate_sigma_ranges()
Definition: cal_date.cpp:64