EstimatePlotter.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2003 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/EstimatePlotter.h
10
11#ifndef __EstimatePlotter_h
12#define __EstimatePlotter_h
13
14#include "Estimate.h"
15#include "Error.h"
16#include "true_math.h"
17
18#include <vector>
19#include <iostream>
20#include <cmath>
21
22class EstimatePlotter {
23
24 public:
25
27 static bool report_mean;
28
29 bool report_mean_on_single_line;
30
32 EstimatePlotter ();
33
35 void clear ();
36
38 void set_control_viewport (bool flag);
39
41 void set_border (float fraction_x, float fraction_y);
42
44 void set_xrange (float x_min, float x_max);
45
47 void set_minimum_error (float error);
48
50 void set_maximum_error (float error);
51
53 void set_graph_marker (int symbol);
54
56 void separate_viewports (bool scaled = true, bool vertical = true);
57
59 void set_viewport (unsigned index);
60
62 void set_world (float x1, float x2, float y1, float y2);
63
65 float get_x_min () const { return x_min; }
67 float get_x_max () const { return x_max; }
68
70 float get_y_min () const { return y_min; }
72 float get_y_max () const { return y_max; }
73
75 void restore_viewport ();
76
78 template<class T> void add_plot (const std::vector< Estimate<T> >& data);
79
80 template<class Xt, class Yt>
81 void add_plot (const std::vector<Xt>&, const std::vector< Estimate<Yt> >&);
82
83 template<class Xt, class Yt>
84 void add_plot (const std::vector<Xt>& xdata,
85 const std::vector< std::complex< Estimate<Yt> > >& ydata);
86
88 template<class T> void plot (const std::vector< Estimate<T> >& data);
89
91 unsigned plot (unsigned index);
92
93 unsigned excise (unsigned index);
94
96 unsigned plot ();
97
99 void set_minmax_range (unsigned i_min, unsigned i_max);
100
101 void minmax (bool& xrange_set, float& xmin, float& xmax,
102 bool& yrange_set, float& ymin, float& ymax,
103 const std::vector<float>& x,
104 const std::vector<float>& y,
105 const std::vector<float>& yerr);
106
107 void set_plot_error_bars (bool flag)
108 { plot_error_bars = flag; }
109
110 protected:
111
113 bool control_viewport;
114
116 float x_border, y_border;
117
119 float xrange_min, xrange_max;
120
122 float x_min, x_max;
123
125 float y_min, y_max;
126
128 unsigned i_min, i_max;
129
131 bool xrange_set;
132 bool yrange_set;
133
135 float minimum_error;
136
138 float maximum_error;
139
141 int graph_marker;
142
144 bool plot_error_bars;
145
147 float log_err_madm_threshold;
148 bool log_err_madm_applied;
149
150 std::vector< std::vector<float> > xval;
151 std::vector< std::vector<float> > yval;
152 std::vector< std::vector<float> > yerr;
153
154 private:
155
156 std::vector<float> data_xmin;
157 std::vector<float> data_xmax;
158 std::vector<float> data_ymin;
159 std::vector<float> data_ymax;
160
161 float vp_x1, vp_x2, vp_y1, vp_y2;
162
163 bool viewports_set;
164 bool viewports_vertical;
165 bool viewports_scaled;
166
167};
168
169template<class T>
170void EstimatePlotter::plot (const std::vector< Estimate<T> >& data)
171{
172 clear ();
173 add_plot (data);
174 plot (0);
175}
176
177template<class T>
178void EstimatePlotter::add_plot (const std::vector< Estimate<T> >& data)
179{
180 unsigned ipt = 0, npt = data.size();
181 if (npt == 0)
182 return;
183
184 log_err_madm_applied = false;
185
186 xval.push_back ( std::vector<float>(npt) );
187 yval.push_back ( std::vector<float>(npt) );
188 yerr.push_back ( std::vector<float>(npt) );
189
190 std::vector<float>& x = xval.back();
191 std::vector<float>& y = yval.back();
192 std::vector<float>& ye = yerr.back();
193
194 double xscale = 0.0;
195 if (npt > 1)
196 xscale = double(xrange_max - xrange_min) / double(npt-1);
197
198 MeanEstimate<T> mean;
199
200 for (ipt=0; ipt<npt; ipt++)
201 {
202 x[ipt] = xrange_min + xscale * double(ipt);
203
204 if (!true_math::finite( data[ipt].val ))
205 ye[ipt] = 0;
206 else
207 {
208 y[ipt] = data[ipt].val;
209 ye[ipt] = sqrt (data[ipt].var);
210
211 if (data[ipt].var)
212 mean += data[ipt];
213 }
214
215 }
216
217 // useful at times, excessive at others ...
218 if (report_mean)
219 {
220 std::cerr << "Mean = " << mean << std::endl;
221 }
222 else if (report_mean_on_single_line)
223 {
224 std::cout << mean.get_Estimate().get_value() << " ";
225 std::cout << mean.get_Estimate().get_error() << " ";
226 }
227
228 minmax (xrange_set, x_min, x_max, yrange_set, y_min, y_max, x, y, ye);
229}
230
231template<class Xt, class Yt>
232void EstimatePlotter::add_plot (const std::vector<Xt>& xdata,
233 const std::vector< Estimate<Yt> >& ydata)
234{
235 unsigned ipt = 0, npt = xdata.size();
236 if (npt == 0)
237 return;
238
239 if (ydata.size() != npt)
240 throw Error (InvalidParam, "EstimatePlotter::add_plot (Xt, Yt)",
241 "xdata.size=%d != ydata.size=%d", npt, ydata.size());
242
243 log_err_madm_applied = false;
244
245 xval.push_back ( std::vector<float>(npt) );
246 yval.push_back ( std::vector<float>(npt) );
247 yerr.push_back ( std::vector<float>(npt) );
248
249 std::vector<float>& x = xval.back();
250 std::vector<float>& y = yval.back();
251 std::vector<float>& ye = yerr.back();
252
253 for (ipt=0; ipt<npt; ipt++) {
254 ye[ipt] = sqrt (ydata[ipt].var);
255 x[ipt] = xdata[ipt];
256 y[ipt] = ydata[ipt].val;
257 }
258
259 minmax (xrange_set, x_min, x_max, yrange_set, y_min, y_max, x, y, ye);
260}
261
262template<class Xt, class Yt>
263void EstimatePlotter::add_plot
264(const std::vector<Xt>& xdata,
265 const std::vector< std::complex< Estimate<Yt> > >& ydata)
266{
267 unsigned ipt = 0, npt = xdata.size();
268 if (npt == 0)
269 return;
270
271 if (ydata.size() != npt)
272 throw Error (InvalidParam, "EstimatePlotter::add_plot (Xt, Yt)",
273 "xdata.size=%d != ydata.size=%d", npt, ydata.size());
274
275 log_err_madm_applied = false;
276
277 xval.push_back ( std::vector<float>(npt) );
278 std::vector<float>& x = xval.back();
279
280 std::vector<float> re_y (npt);
281 std::vector<float> re_ye (npt);
282
283 std::vector<float> im_y (npt);
284 std::vector<float> im_ye (npt);
285
286 for (ipt=0; ipt<npt; ipt++)
287 {
288 x[ipt] = xdata[ipt];
289
290 re_y[ipt] = ydata[ipt].real().val;
291 re_ye[ipt] = sqrt (ydata[ipt].real().var);
292
293 im_y[ipt] = ydata[ipt].imag().val;
294 im_ye[ipt] = sqrt (ydata[ipt].imag().var);
295 }
296
297 minmax (xrange_set, x_min, x_max, yrange_set, y_min, y_max, x, re_y, re_ye);
298 minmax (xrange_set, x_min, x_max, yrange_set, y_min, y_max, x, im_y, im_ye);
299
300 yval.push_back ( re_y );
301 yval.push_back ( im_y );
302
303 yerr.push_back ( re_ye );
304 yerr.push_back ( im_ye );
305}
306
307#endif
const ScalarMath sqrt(const ScalarMath &x)

Generated using doxygen 1.14.0