OpenFPM  5.2.0
Project that contain the implementation of distributed structures
Plot_unit_tests.hpp
1 /*
2  * Plot_unit_tests.hpp
3  *
4  * Created on: Jan 9, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_
9 #define OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_
10 
11 #include "GoogleChart.hpp"
12 #include "Plot/util.hpp"
13 
14 BOOST_AUTO_TEST_SUITE( plot_unit_test )
15 
16 
17 BOOST_AUTO_TEST_CASE( google_chart_bar_string )
18 {
19  Vcluster<> & v_cl = create_vcluster();
20 
21  if (v_cl.getProcessUnitID() != 0)
22  return;
23 
24  std::string c2 = std::string("test_data/gc_out_sc_test.html");
25 
27 
31 
32  x.add("colum1");
33  x.add("colum2");
34  x.add("colum3");
35  x.add("colum4");
36  x.add("colum5");
37  x.add("colum6");
38 
39  // Each colum can have multiple data set (in this case 4 dataset)
40  // Each dataset can have a name
41  yn.add("dataset1");
42  yn.add("dataset2");
43  yn.add("dataset3");
44  yn.add("dataset4");
45 
46  // Each colums can have multiple data-set
47  y.add({2,3,5,6});
48  y.add({5,6,1,6});
49  y.add({2,1,6,9});
50  y.add({1,6,3,2});
51  y.add({3,3,0,6});
52  y.add({2,1,4,6});
53 
54  // Google charts options
55  GCoptions options;
56 
57  options.title = std::string("Example");
58  options.yAxis = std::string("Y Axis");
59  options.xAxis = std::string("X Axis");
60  options.stype = std::string("bars");
61  options.barWD = true;
62 
63  // it say that the colum4 must me represented with a line
64  options.stypeext = std::string("{3: {type: 'line'}}");
65 
66  GoogleChart cg;
67  cg.AddHistGraph(x,y,yn,options);
68  cg.write("gc_out_sc.html");
69 
71 
72  bool test = compare("gc_out_sc.html",c2);
73  BOOST_REQUIRE_EQUAL(true,test);
74 }
75 
76 
77 BOOST_AUTO_TEST_CASE( google_chart )
78 {
79  Vcluster<> & v_cl = create_vcluster();
80 
81  if (v_cl.getProcessUnitID() != 0)
82  return;
83 
84  std::string c2 = std::string("test_data/gc_out_test.html");
85 
87 
91 
92  x.add("colum1");
93  x.add("colum2");
94  x.add("colum3");
95  x.add("colum4");
96  x.add("colum5");
97  x.add("colum6");
98 
99  // Each colum can have multiple data set (in this case 4 dataset)
100  // Each dataset can have a name
101  yn.add("dataset1");
102  yn.add("dataset2");
103  yn.add("dataset3");
104  yn.add("dataset4");
105 
106  // Each colums can have multiple data-set
107  y.add({2,3,5,6});
108  y.add({5,6,1,6});
109  y.add({2,1,6,9});
110  y.add({1,6,3,2});
111  y.add({3,3,0,6});
112  y.add({2,1,4,6});
113 
114  // Google charts options
115  GCoptions options;
116 
117  options.title = std::string("Example");
118  options.yAxis = std::string("Y Axis");
119  options.xAxis = std::string("X Axis");
120  options.stype = std::string("bars");
121 
122  // it say that the colum4 must me represented with a line
123  options.stypeext = std::string("{3: {type: 'line'}}");
124 
125  GoogleChart cg;
126  cg.AddHistGraph(x,y,yn,options);
127  cg.write("gc_out.html");
128 
130 
131  bool test = compare("gc_out.html",c2);
132  BOOST_REQUIRE_EQUAL(true,test);
133 }
134 
135 BOOST_AUTO_TEST_CASE( google_chart2 )
136 {
137  Vcluster<> & v_cl = create_vcluster();
138 
139  if (v_cl.getProcessUnitID() != 0)
140  return;
141 
142  std::string c2 = std::string("test_data/gc_out2_test.html");
143 
147 
148  x.add("colum1");
149  x.add("colum2");
150  x.add("colum3");
151  x.add("colum4");
152  x.add("colum5");
153  x.add("colum6");
154 
155  // Each colum can have multiple data set (in this case 4 dataset)
156  // Each dataset can have a name
157  yn.add("dataset1");
158  yn.add("dataset2");
159  yn.add("dataset3");
160  yn.add("dataset4");
161 
162  // Each colums can have multiple data-set
163  y.add({2.2,1.3,4.5,0.6});
164  y.add({5.0,6.1,1.3,2.6});
165  y.add({2.1,1.0,6.1,9.3});
166  y.add({1.1,6.1,3.0,2.0});
167  y.add({3.3,0.3,0.0,6.2});
168  y.add({2.0,1.1,4.0,6.1});
169 
170  // Google charts options
171  GCoptions options;
172 
173  options.title = std::string("Example");
174  options.yAxis = std::string("Y Axis");
175  options.xAxis = std::string("X Axis");
176  options.stype = std::string("bars");
177 
178  GoogleChart cg;
179  cg.AddHistGraph(x,y,yn,options);
180  cg.write("gc_out2.html");
181 
182  bool test = compare("gc_out2.html",c2);
183  BOOST_REQUIRE_EQUAL(true,test);
184 }
185 
186 BOOST_AUTO_TEST_CASE( google_chart3 )
187 {
188  Vcluster<> & v_cl = create_vcluster();
189 
190  if (v_cl.getProcessUnitID() != 0)
191  return;
192 
193  std::string c2 = std::string("test_data/gc_out3_test.html");
194 
198 
199  x.add("colum1");
200  x.add("colum2");
201  x.add("colum3");
202  x.add("colum4");
203  x.add("colum5");
204  x.add("colum6");
205 
206  // Each colum can have multiple data set (in this case 4 dataset)
207  // Each dataset can have a name
208  yn.add("dataset1");
209  yn.add("dataset2");
210  yn.add("dataset3");
211  yn.add("dataset4");
212 
213  // Each colums can have multiple data-set
214  y.add({2.2,1.3,4.5,0.6});
215  y.add({5.0,6.1,1.3,2.6});
216  y.add({2.1,1.0,6.1,9.3});
217  y.add({1.1,6.1,3.0,2.0});
218  y.add({3.3,0.3,0.0,6.2});
219  y.add({2.0,1.1,4.0,6.1});
220 
221  // Google charts options
222  GCoptions options;
223 
224  options.title = std::string("Example");
225  options.yAxis = std::string("Y Axis");
226  options.xAxis = std::string("X Axis");
227 
228  GoogleChart cg;
229  cg.AddHistGraph(x,y,yn,options);
230  cg.write("gc_out3.html");
231 
232  bool test = compare("gc_out3.html",c2);
233  BOOST_REQUIRE_EQUAL(true,test);
234 }
235 
236 BOOST_AUTO_TEST_CASE( google_chart4 )
237 {
238  Vcluster<> & v_cl = create_vcluster();
239 
240  if (v_cl.getProcessUnitID() != 0)
241  return;
242 
243  std::string c2 = std::string("test_data/gc_out4_test.html");
244 
248 
249  x.add("colum1");
250  x.add("colum2");
251  x.add("colum3");
252  x.add("colum4");
253  x.add("colum5");
254  x.add("colum6");
255 
256  // Each colum can have multiple data set (in this case 4 dataset)
257  // Each dataset can have a name
258  yn.add("dataset1");
259  yn.add("dataset2");
260  yn.add("dataset3");
261  yn.add("dataset4");
262 
263  // Each colums can have multiple data-set
264  y.add({2.2,1.3,4.5,0.6});
265  y.add({5.0,6.1,1.3,2.6});
266  y.add({2.1,1.0,6.1,9.3});
267  y.add({1.1,6.1,3.0,2.0});
268  y.add({3.3,0.3,0.0,6.2});
269  y.add({2.0,1.1,4.0,6.1});
270 
271  GoogleChart cg;
272  cg.AddHistGraph(x,y,yn);
273  cg.write("gc_out4.html");
274 
275  bool test = compare("gc_out4.html",c2);
276  BOOST_REQUIRE_EQUAL(true,test);
277 }
278 
279 BOOST_AUTO_TEST_CASE( google_chart5 )
280 {
281  Vcluster<> & v_cl = create_vcluster();
282 
283  if (v_cl.getProcessUnitID() != 0)
284  return;
285 
286  std::string c2 = std::string("test_data/gc_out5_test.html");
287 
290 
291  x.add("colum1");
292  x.add("colum2");
293  x.add("colum3");
294  x.add("colum4");
295  x.add("colum5");
296  x.add("colum6");
297 
298  // Each colums can have multiple data-set
299  y.add({2.2,1.3,4.5,0.6});
300  y.add({5.0,6.1,1.3,2.6});
301  y.add({2.1,1.0,6.1,9.3});
302  y.add({1.1,6.1,3.0,2.0});
303  y.add({3.3,0.3,0.0,6.2});
304  y.add({2.0,1.1,4.0,6.1});
305 
306  GoogleChart cg;
307  cg.AddHistGraph(x,y);
308  cg.write("gc_out5.html");
309 
310  bool test = compare("gc_out5.html",c2);
311  BOOST_REQUIRE_EQUAL(true,test);
312 }
313 
314 BOOST_AUTO_TEST_CASE( google_chart6 )
315 {
316  Vcluster<> & v_cl = create_vcluster();
317 
318  if (v_cl.getProcessUnitID() != 0)
319  return;
320 
321  std::string c2 = std::string("test_data/gc_out6_test.html");
322 
324 
325  // Each colums can have multiple data-set
326  y.add({2.2,1.3,4.5,0.6});
327  y.add({5.0,6.1,1.3,2.6});
328  y.add({2.1,1.0,6.1,9.3});
329  y.add({1.1,6.1,3.0,2.0});
330  y.add({3.3,0.3,0.0,6.2});
331  y.add({2.0,1.1,4.0,6.1});
332 
333  GoogleChart cg;
334  cg.AddHistGraph(y);
335  cg.write("gc_out6.html");
336 
337  bool test = compare("gc_out6.html",c2);
338  BOOST_REQUIRE_EQUAL(true,test);
339 }
340 
341 BOOST_AUTO_TEST_CASE( google_chart_with_inject_HTML )
342 {
343  Vcluster<> & v_cl = create_vcluster();
344 
345  if (v_cl.getProcessUnitID() != 0)
346  return;
347 
348  std::string c2 = std::string("test_data/gc_out7_test.html");
349 
351 
355 
356  x.add("colum1");
357  x.add("colum2");
358  x.add("colum3");
359  x.add("colum4");
360  x.add("colum5");
361  x.add("colum6");
362 
363  // Each colum can have multiple data set (in this case 4 dataset)
364  // Each dataset can have a name
365  yn.add("dataset1");
366  yn.add("dataset2");
367  yn.add("dataset3");
368  yn.add("dataset4");
369 
370  // Each colums can have multiple data-set
371  y.add({2,3,5,6});
372  y.add({5,6,1,6});
373  y.add({2,1,6,9});
374  y.add({1,6,3,2});
375  y.add({3,3,0,6});
376  y.add({2,1,4,6});
377 
378  // Google charts options
379  GCoptions options;
380 
381  options.title = std::string("Example");
382  options.yAxis = std::string("Y Axis");
383  options.xAxis = std::string("X Axis");
384  options.stype = std::string("bars");
385 
386  // it say that the colum4 must me represented with a line
387  options.stypeext = std::string("{3: {type: 'line'}}");
388 
389  GoogleChart cg;
390  //
391  cg.addHTML("<h2>Before first graph</h2>");
392  cg.AddHistGraph(x,y,yn,options);
393  cg.addHTML("<h2>Before second graph</h2>");
394  cg.AddHistGraph(x,y,yn,options);
395  cg.addHTML("<h2>Before third graph</h2>");
396  cg.AddHistGraph(x,y,yn,options);
397  cg.addHTML("<h2>At the end</h2>");
398  cg.write("gc_out7.html");
399 
401 
402  bool test = compare("gc_out7.html",c2);
403  BOOST_REQUIRE_EQUAL(true,test);
404 }
405 
406 BOOST_AUTO_TEST_CASE( google_chart_number )
407 {
408  Vcluster<> & v_cl = create_vcluster();
409 
410  if (v_cl.getProcessUnitID() != 0)
411  return;
412 
413  std::string c2 = std::string("test_data/gc_num_plot_test.html");
414 
416 
420 
421  x.add(0.1);
422  x.add(0.2);
423  x.add(0.3);
424  x.add(0.4);
425  x.add(0.5);
426  x.add(0.6);
427 
428  // Each colum can have multiple data set (in this case 4 dataset)
429  // Each dataset can have a name
430  yn.add("dataset1");
431  yn.add("dataset2");
432  yn.add("dataset3");
433  yn.add("dataset4");
434 
435  // Each colums can have multiple data-set
436  y.add({2,3,5,6});
437  y.add({5,6,1,6});
438  y.add({2,1,6,9});
439  y.add({1,6,3,2});
440  y.add({3,3,0,6});
441  y.add({2,1,4,6});
442 
443  // Google charts options
444  GCoptions options;
445 
446  options.title = std::string("Example");
447  options.yAxis = std::string("Y Axis");
448  options.xAxis = std::string("X Axis");
449  options.stype = std::string("line");
450 
451  GoogleChart cg;
452  //
453  cg.AddLinesGraph(x,y,yn,options);
454  cg.write("gc_num_plot.html");
455 
457 
458  bool test = compare("gc_num_plot.html",c2);
459  BOOST_REQUIRE_EQUAL(true,test);
460 }
461 
462 BOOST_AUTO_TEST_CASE( google_chart_number_lines_different_x )
463 {
464  Vcluster<> & v_cl = create_vcluster();
465 
466  if (v_cl.getProcessUnitID() != 0)
467  return;
468 
469  std::string c2 = std::string("test_data/gc_num_ydif_plot_test.html");
470 
472 
480 
481  x1.add(0.1); y1.add(4.5);
482  x1.add(0.2); y1.add(3.0);
483  x1.add(0.3); y1.add(5.5);
484  x1.add(0.4); y1.add(3.3);
485  x1.add(0.5); y1.add(1.0);
486  x1.add(0.6); y1.add(7.0);
487 
488  x2.add(0.15); y2.add(1.5);
489  x2.add(0.2); y2.add(4.5);
490  x2.add(0.35); y2.add(2.5);
491  x2.add(0.45); y2.add(6.5);
492 
493  x3.add(0.1); y3.add(3.5);
494  x3.add(0.2); y3.add(6.5);
495  x3.add(0.63); y3.add(1.5);
496  x3.add(0.37); y3.add(1.5);
497  x3.add(0.7); y3.add(3.5);
498  x3.add(0.82); y3.add(2.5);
499  x3.add(0.4); y3.add(2.5);
500  x3.add(1.0); y3.add(1.5);
501  x3.add(0.5); y3.add(7.5);
502  x3.add(0.91); y3.add(5.5);
503 
504  // Each colum can have multiple data set (in this case 4 dataset)
505  // Each dataset can have a name
506  yn.add("dataset1");
507  yn.add("dataset2");
508  yn.add("dataset3");
509 
510 
511  // Google charts options
512  GCoptions options;
513 
514  options.title = std::string("Example");
515  options.yAxis = std::string("Y Axis");
516  options.xAxis = std::string("X Axis");
517  options.stype = std::string("line");
518 
519  GoogleChart cg;
520 
521  cg.AddLines(yn,options,x1,y1,x2,y2,x3,y3);
522  cg.write("gc_num_ydif_plot.html");
523 
525 
526  bool test = compare("gc_num_ydif_plot.html",c2);
527  BOOST_REQUIRE_EQUAL(true,test);
528 }
529 
530 BOOST_AUTO_TEST_CASE( google_chart_linear_plot )
531 {
532  Vcluster<> & v_cl = create_vcluster();
533 
534  if (v_cl.getProcessUnitID() != 0)
535  return;
536 
537  std::string c2 = std::string("test_data/gc_plot_out_test.html");
538 
540 
544 
545  x.add("colum1");
546  x.add("colum2");
547  x.add("colum3");
548  x.add("colum4");
549  x.add("colum5");
550  x.add("colum6");
551 
552  // Here we specify how many lines we have
553  // first Line
554  yn.add("line1");
555 
556  // second line + 2 intervals (Error bands)
557  yn.add("line2");
558  yn.add("interval");
559  yn.add("interval");
560  yn.add("interval");
561  yn.add("interval");
562 
563  // third line + 1 interval (Error bands)
564  yn.add("line3");
565  yn.add("interval");
566  yn.add("interval");
567 
568  // Each line can have multiple intervals or error bars
569  // The first number specify the bottom line
570  // The last three numbers specify the top line + error band (min, max)
571  // The middle 5 line specify the middle lines + one external error band + one internal error band
572 
573  y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
574  y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
575  y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
576  y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
577  y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
578  y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});
579 
580  // Google charts options
581  GCoptions options;
582 
583  options.title = std::string("Example");
584  options.yAxis = std::string("Y Axis");
585  options.xAxis = std::string("X Axis");
586  options.lineWidth = 1.0;
587  options.intervalext = std::string("{'i2': { 'color': '#4374E0', 'style':'bars', 'lineWidth':4, 'fillOpacity':1 } }");
588 
589  GoogleChart cg;
590  cg.AddLinesGraph(x,y,yn,options);
591  cg.write("gc_plot_out.html");
592 
594 
595  bool test = compare("gc_plot_out.html",c2);
596  BOOST_REQUIRE_EQUAL(true,test);
597 }
598 
599 BOOST_AUTO_TEST_CASE( google_chart_linear_plot2 )
600 {
601  Vcluster<> & v_cl = create_vcluster();
602 
603  if (v_cl.getProcessUnitID() != 0)
604  return;
605 
606  std::string c2 = std::string("test_data/gc_plot2_out_test.html");
607 
609 
612 
613  x.add("colum1");
614  x.add("colum2");
615  x.add("colum3");
616  x.add("colum4");
617  x.add("colum5");
618  x.add("colum6");
619 
620  // Each line can have multiple intervals or error bars
621  // The first number specify the bottom line
622  // The last three numbers specify the top line + error band (min, max)
623  // The middle 5 line specify the middle lines + one external error band + one internal error band
624 
625  y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
626  y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
627  y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
628  y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
629  y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
630  y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});
631 
632  // Google charts options
633  GCoptions options;
634 
635  options.title = std::string("Example");
636  options.yAxis = std::string("Y Axis");
637  options.xAxis = std::string("X Axis");
638  options.lineWidth = 1.0;
639 
640  GoogleChart cg;
641  cg.AddLinesGraph(x,y,options);
642  cg.write("gc_plot2_out.html");
643 
645 
646  bool test = compare("gc_plot2_out.html",c2);
647  BOOST_REQUIRE_EQUAL(true,test);
648 }
649 
651 
652 double f(double x)
653 {
654  return x*x;
655 }
656 
658 
659 BOOST_AUTO_TEST_CASE( plot_util )
660 {
661  Vcluster<> & v_cl = create_vcluster();
662 
663  if (v_cl.getProcessUnitID() != 0)
664  return;
665 
667 
669 
670  Fill1D(0.0,2.0,5,x);
671 
672  BOOST_REQUIRE_EQUAL(x.get(0),0.0);
673  BOOST_REQUIRE_EQUAL(x.get(1),0.5);
674  BOOST_REQUIRE_EQUAL(x.get(2),1.0);
675  BOOST_REQUIRE_EQUAL(x.get(3),1.5);
676  BOOST_REQUIRE_EQUAL(x.get(4),2.0);
677 
679 
680  x.clear();
681 
683 
684  Fill1D(0.0,2.0,5,x,f);
685 
686  BOOST_REQUIRE_EQUAL(x.get(0),0.0);
687  BOOST_REQUIRE_EQUAL(x.get(1),0.25);
688  BOOST_REQUIRE_EQUAL(x.get(2),1.0);
689  BOOST_REQUIRE_EQUAL(x.get(3),2.25);
690  BOOST_REQUIRE_EQUAL(x.get(4),4.0);
691 
693 }
694 
695 BOOST_AUTO_TEST_SUITE_END()
696 
697 #endif /* OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_ */
Small class to produce graph with Google chart in HTML.
void write(std::string file)
It write the graphs on file in html format using Google charts.
void AddLines(const openfpm::vector< std::string > &yn, const GCoptions &opt, X ... xy)
Add lines graph.
void AddLinesGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, const GCoptions &opt)
Add a simple lines graph.
void AddHistGraph(openfpm::vector< Y > &y)
Add an histogram graph.
void addHTML(const std::string &html)
Add HTML text.
size_t getProcessUnitID()
Get the process unit id.
Implementation of VCluster class.
Definition: VCluster.hpp:59
Google chart options.
Definition: GoogleChart.hpp:26
std::string stypeext
Definition: GoogleChart.hpp:43
std::string intervalext
Definition: GoogleChart.hpp:64
std::string xAxis
X axis name.
Definition: GoogleChart.hpp:32
size_t lineWidth
Width of the line.
Definition: GoogleChart.hpp:56
std::string title
Title of the chart.
Definition: GoogleChart.hpp:28
bool barWD
barWD
Definition: GoogleChart.hpp:73
std::string stype
Definition: GoogleChart.hpp:38
std::string yAxis
Y axis name.
Definition: GoogleChart.hpp:30