NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
plugin_histogram.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2014 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19
20#include "plugins.hpp"
21#include "../kernel.hpp"
22
23/*
24 * Plugin zur Erzeugung von Histogramm-Rubriken
25 */
26
27const std::string PI_HIST = "1.1.2";
28extern mglGraph _fontData;
29
36{
40};
41
42
47struct Ranges
48{
49 double x[2];
50 double y[2];
51 double z[2];
52};
53
54
60{
62
63 double binWidth[2];
64 int nBin;
66 std::string sTable;
67 std::string sBinLabel;
68 std::string sCountLabel;
69 std::string sAxisLabels[3];
70 std::string sSavePath;
71};
72
73
86static std::string getParameterValue(const std::string& sCmd, const std::string& sVersion1, const std::string& sVersion2, const std::string& sDefaultVal)
87{
88 // Try to find one of the two possible
89 // option variants
90 if (findParameter(sCmd, sVersion1, '=') || findParameter(sCmd, sVersion2, '='))
91 {
92 int nPos = 0;
93
94 // Use the detected variant
95 if (findParameter(sCmd, sVersion1, '='))
96 nPos = findParameter(sCmd, sVersion1, '=')+sVersion1.length();
97 else
98 nPos = findParameter(sCmd, sVersion2, '=')+sVersion2.length();
99
100 // Get the value of the option and
101 // strip all surrounding spaces
102 std::string val = getArgAtPos(sCmd, nPos);
103 StripSpaces(val);
104 return val;
105 }
106
107 return sDefaultVal;
108}
109
110
123static void getIntervalDef(const std::string& sCmd, const std::string& sIdentifier, double& dMin, double& dMax)
124{
125 // Does this interval definition exist?
126 if (findParameter(sCmd, sIdentifier, '='))
127 {
128 // Get the interval definition
129 std::string sTemp = getArgAtPos(sCmd, findParameter(sCmd, sIdentifier, '=') + sIdentifier.length());
130
131 // If the interval definition actually contains
132 // a colon, decode it and use it
133 if (sTemp.find(':') != std::string::npos)
134 {
135 if (sTemp.substr(0, sTemp.find(':')).length())
136 dMin = StrToDb(sTemp.substr(0, sTemp.find(':')));
137
138 if (sTemp.substr(sTemp.find(':') + 1).length())
139 dMax = StrToDb(sTemp.substr(sTemp.find(':') + 1));
140 }
141 }
142}
143
144
158static void prepareIntervalsForHist(const std::string& sCmd, double& dMin, double& dMax, double dDataMin, double dDataMax)
159{
160 // Replace the missing interval boundaries
161 // with the minimal and maximal data values
162 if (isnan(dMin) && isnan(dMax))
163 {
164 dMin = dDataMin;
165 dMax = dDataMax;
166 //double dIntervall = dMax - dMin;
167 //dMax += dIntervall / 10.0;
168 //dMin -= dIntervall / 10.0;
169 }
170 else if (isnan(dMin))
171 dMin = dDataMin;
172 else if (isnan(dMax))
173 dMax = dDataMax;
174
175 // Ensure the correct order
176 if (dMax < dMin)
177 {
178 double dTemp = dMax;
179 dMax = dMin;
180 dMin = dTemp;
181 }
182
183 // Ensure that the selected interval is part of
184 // the data interval
185 if (!isnan(dMin) && !isnan(dMax))
186 {
187 if (dMin > dDataMax || dMax < dDataMin)
189 }
190}
191
192
212static std::vector<std::vector<double>> calculateHist1dData(MemoryManager& _data, const Indices& _idx, const HistogramParameters& _histParams, mglData& _histData, mglData& _mAxisVals, int& nMax, std::vector<std::string>& vLegends, bool bGrid, bool isXLog)
213{
214 // Prepare the data table
215 std::vector<std::vector<double>> vHistMatrix(_histParams.nBin, std::vector<double>(bGrid ? 1 : _idx.col.size(), 0.0));
216
217 int nCount = 0;
218
219 if (bGrid)
220 {
221 nMax = 0;
222
223 // Repeat for every bin
224 for (int k = 0; k < _histParams.nBin; k++)
225 {
226 nCount = 0;
227
228 // Detect the number of values, which
229 // are part of the current bin interval
230 for (size_t i = 2; i < _idx.col.size(); i++)
231 {
232 for (size_t l = 0; l < _idx.row.size(); l++)
233 {
234 if (_data.getElement(_idx.row[l], _idx.col[0], _histParams.sTable).real() > _histParams.ranges.x[1]
235 || _data.getElement(_idx.row[l], _idx.col[0], _histParams.sTable).real() < _histParams.ranges.x[0]
236 || _data.getElement(_idx.row[l], _idx.col[1], _histParams.sTable).real() > _histParams.ranges.y[1]
237 || _data.getElement(_idx.row[l], _idx.col[1], _histParams.sTable).real() < _histParams.ranges.y[0]
238 || _data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable).real() > _histParams.ranges.z[1]
239 || _data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable).real() < _histParams.ranges.z[0])
240 continue;
241
242 if (isXLog)
243 {
244 if (_data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable).real() >= pow(10.0, log10(_histParams.ranges.z[0]) + k * _histParams.binWidth[0])
245 && _data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable).real() < pow(10.0, log10(_histParams.ranges.z[0]) + (k + 1) * _histParams.binWidth[0]))
246 nCount++;
247 }
248 else
249 {
250 if (_data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable).real() >= _histParams.ranges.z[0] + k * _histParams.binWidth[0]
251 && _data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable).real() < _histParams.ranges.z[0] + (k + 1) * _histParams.binWidth[0])
252 nCount++;
253 }
254 }
255
256 if (i == 2 && !isXLog)
257 _mAxisVals.a[k] = _histParams.ranges.z[0] + (k + 0.5) * _histParams.binWidth[0];
258 else if (i == 2)
259 _mAxisVals.a[k] = pow(10.0, log10(_histParams.ranges.z[0]) + (k + 0.5) * _histParams.binWidth[0]);
260 }
261
262 // Store the value in the corresponding column
263 vHistMatrix[k][0] = nCount;
264 _histData.a[k] = nCount;
265
266 if (nCount > nMax)
267 nMax = nCount;
268 }
269
270 vLegends.push_back("grid");
271 }
272 else
273 {
274 nMax = 0;
275
276 // Repeat for every data set
277 for (size_t i = 0; i < _idx.col.size(); i++)
278 {
279 // Repeat for every bin
280 for (int k = 0; k < _histParams.nBin; k++)
281 {
282 nCount = 0;
283
284 // Detect the number of values, which
285 // are part of the current bin interval
286 for (size_t l = 0; l < _idx.row.size(); l++)
287 {
288 mu::value_type val = _data.getElement(_idx.row[l], _idx.col[i], _histParams.sTable);
289
290 if (isXLog)
291 {
292 if (std::floor((val.real() - std::pow(10.0, _histParams.ranges.x[0])) / _histParams.binWidth[0]) == k
293 || (val.real() == std::pow(10.0, _histParams.ranges.x[1]) && k+1 == _histParams.nBin))
294 nCount++;
295 }
296 else
297 {
298 if (std::floor((val.real() - _histParams.ranges.x[0]) / _histParams.binWidth[0]) == k
299 || (val.real() == _histParams.ranges.x[1] && k+1 == _histParams.nBin))
300 nCount++;
301 }
302 }
303
304 if (!i && !isXLog)
305 _mAxisVals.a[k] = _histParams.ranges.x[0] + (k + 0.5) * _histParams.binWidth[0];
306 else if (!i)
307 _mAxisVals.a[k] = pow(10.0, log10(_histParams.ranges.x[0]) + (k + 0.5) * _histParams.binWidth[0]);
308
309 // Store the value in the corresponding column
310 vHistMatrix[k][i] = nCount;
311 _histData.a[k + (_histParams.nBin * i)] = nCount;
312
313 if (nCount > nMax)
314 nMax = nCount;
315 }
316
317 // Create the plot legend entry for the current data set
318 vLegends.push_back(replaceToTeX(_data.getTopHeadLineElement(_idx.col[i], _histParams.sTable)));
319 }
320 }
321
322 return vHistMatrix;
323}
324
325
338static std::string prepareTicksForHist1d(const HistogramParameters& _histParams, const mglData& _mAxisVals, std::string& sCommonExponent, bool bGrid)
339{
340 std::string sTicks;
341 double dCommonExponent = 1.0;
342
343 // Try to find the common bin interval exponent to
344 // factorize it out
345 if (bGrid)
346 {
347 if (toString(_histParams.ranges.z[0] + _histParams.binWidth[0] / 2.0, 3).find('e') != std::string::npos || toString(_histParams.ranges.z[0] + _histParams.binWidth[0] / 2.0, 3).find('E') != std::string::npos)
348 {
349 sCommonExponent = toString(_histParams.ranges.z[0] + _histParams.binWidth[0] / 2.0, 3).substr(toString(_histParams.ranges.z[0] + _histParams.binWidth[0] / 2.0, 3).find('e'));
350 dCommonExponent = StrToDb("1.0" + sCommonExponent);
351
352 for (int i = 0; i < _histParams.nBin; i++)
353 {
354 if (toString((_histParams.ranges.z[0] + i * _histParams.binWidth[0] + _histParams.binWidth[0] / 2.0) / dCommonExponent, 3).find('e') != std::string::npos)
355 {
356 sCommonExponent = "";
357 dCommonExponent = 1.0;
358 break;
359 }
360 }
361
362 sTicks = toString((_histParams.ranges.z[0] + _histParams.binWidth[0] / 2.0) / dCommonExponent, 3) + "\\n";
363 }
364 else
365 sTicks = toString(_histParams.ranges.z[0] + _histParams.binWidth[0] / 2.0, 3) + "\\n";
366 }
367 else
368 {
369 if (toString(_histParams.ranges.x[0] + _histParams.binWidth[0] / 2.0, 3).find('e') != std::string::npos || toString(_histParams.ranges.x[0] + _histParams.binWidth[0] / 2.0, 3).find('E') != std::string::npos)
370 {
371 sCommonExponent = toString(_histParams.ranges.x[0] + _histParams.binWidth[0] / 2.0, 3).substr(toString(_histParams.ranges.x[0] + _histParams.binWidth[0] / 2.0, 3).find('e'));
372 dCommonExponent = StrToDb("1.0" + sCommonExponent);
373
374 for (int i = 0; i < _histParams.nBin; i++)
375 {
376 if (toString((_histParams.ranges.x[0] + i * _histParams.binWidth[0] + _histParams.binWidth[0] / 2.0) / dCommonExponent, 3).find('e') != std::string::npos)
377 {
378 sCommonExponent = "";
379 dCommonExponent = 1.0;
380 break;
381 }
382 }
383
384 sTicks = toString((_histParams.ranges.x[0] + _histParams.binWidth[0] / 2.0) / dCommonExponent, 3) + "\\n";
385 }
386 else
387 sTicks = toString(_histParams.ranges.x[0] + _histParams.binWidth[0] / 2.0, 3) + "\\n";
388 }
389
390 // Create the ticks list by factorizing out
391 // the common exponent
392 for (int i = 1; i < _histParams.nBin - 1; i++)
393 {
394 if (_histParams.nBin > 16)
395 {
396 if (!((_histParams.nBin - 1) % 2) && !(i % 2) && _histParams.nBin - 1 < 33)
397 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
398 else if (!((_histParams.nBin - 1) % 2) && (i % 2) && _histParams.nBin - 1 < 33)
399 sTicks += "\\n";
400 else if (!((_histParams.nBin - 1) % 4) && !(i % 4))
401 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
402 else if (!((_histParams.nBin - 1) % 4) && (i % 4))
403 sTicks += "\\n";
404 else if (!((_histParams.nBin - 1) % 3) && !(i % 3))
405 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
406 else if (!((_histParams.nBin - 1) % 3) && (i % 3))
407 sTicks += "\\n";
408 else if (!((_histParams.nBin - 1) % 5) && !(i % 5))
409 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
410 else if (!((_histParams.nBin - 1) % 5) && (i % 5))
411 sTicks += "\\n";
412 else if (!((_histParams.nBin - 1) % 7) && !(i % 7))
413 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
414 else if (!((_histParams.nBin - 1) % 7) && (i % 7))
415 sTicks += "\\n";
416 else if (!((_histParams.nBin - 1) % 11) && !(i % 11))
417 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
418 else if (!((_histParams.nBin - 1) % 11) && (i % 11))
419 sTicks += "\\n";
420 else if (((_histParams.nBin - 1) % 2 && (_histParams.nBin - 1) % 3 && (_histParams.nBin - 1) % 5 && (_histParams.nBin - 1) % 7 && (_histParams.nBin - 1) % 11) && !(i % 3))
421 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
422 else
423 sTicks += "\\n";
424 }
425 else
426 sTicks += toString(_mAxisVals.a[i] / dCommonExponent, 3) + "\\n";
427 }
428
429 sTicks += toString(_mAxisVals.a[_histParams.nBin - 1] / dCommonExponent, 3);
430
431 // Convert the common exponent into a LaTeX string
432 if (sCommonExponent.length())
433 {
434 while (sTicks.find(sCommonExponent) != std::string::npos)
435 sTicks.erase(sTicks.find(sCommonExponent), sCommonExponent.length());
436
437 if (sCommonExponent.find('-') != std::string::npos)
438 sCommonExponent = "\\times 10^{-" + sCommonExponent.substr(sCommonExponent.find_first_not_of('0', 2)) + "}";
439 else
440 sCommonExponent = "\\times 10^{" + sCommonExponent.substr(sCommonExponent.find_first_not_of('0', 2)) + "}";
441 }
442
443 return sTicks;
444}
445
446
462static void createOutputForHist1D(MemoryManager& _data, const Indices& _idx, const std::vector<std::vector<double>>& vHistMatrix, const HistogramParameters& _histParams, const mglData& _mAxisVals, bool bGrid, bool bFormat, bool bSilent)
463{
466 std::string** sOut = new std::string*[vHistMatrix.size() + 1];
467
468 for (size_t i = 0; i < vHistMatrix.size() + 1; i++)
469 {
470 sOut[i] = new std::string[vHistMatrix[0].size() + 1];
471
472 for (size_t j = 0; j < vHistMatrix[0].size() + 1; j++)
473 {
474 sOut[i][j] = "";
475 }
476 }
477
478 // --> Schreibe Tabellenkoepfe <--
479 sOut[0][0] = _histParams.sBinLabel;
480
481 if (bGrid)
482 sOut[0][1] = condenseText(_histParams.sCountLabel) + ": grid";
483 else
484 {
485 for (size_t i = 1; i < vHistMatrix[0].size() + 1; i++)
486 {
487 sOut[0][i] = condenseText(_histParams.sCountLabel) + ": " + _data.getTopHeadLineElement(_idx.col[i-1], _histParams.sTable);
488
489 //size_t nPos;
490
491 //while ((nPos = sOut[0][i].find(' ')) != std::string::npos)
492 // sOut[0][i][nPos] = '_';
493 }
494 }
495
496 // --> Setze die ueblichen Ausgabe-Info-Parameter <--
497 if (bFormat)
498 {
499 _out.setPluginName(_lang.get("HIST_OUT_PLGNINFO", PI_HIST, toString(_idx.col.front() + 1), toString(_idx.col.last()+1), _data.getDataFileName(_histParams.sTable)));
500
501 if (bGrid)
502 _out.setCommentLine(_lang.get("HIST_OUT_COMMENTLINE", toString(_histParams.ranges.z[0], 5), toString(_histParams.ranges.z[1], 5), toString(_histParams.binWidth[0], 5)));
503 else
504 _out.setCommentLine(_lang.get("HIST_OUT_COMMENTLINE", toString(_histParams.ranges.x[0], 5), toString(_histParams.ranges.x[1], 5), toString(_histParams.binWidth[0], 5)));
505
506 _out.setPrefix("hist");
507 }
508
509 if (_out.isFile())
510 {
511 _out.setStatus(true);
512 _out.setCompact(false);
513
514 if (_histParams.sSavePath.length())
515 _out.setFileName(_histParams.sSavePath);
516 else
517 _out.generateFileName();
518 }
519 else
520 _out.setCompact(_option.createCompactTables());
521
522 // --> Fuelle die Ausgabe-Matrix <--
523 for (size_t i = 1; i < vHistMatrix.size() + 1; i++)
524 {
525 // --> Die erste Spalte enthaelt immer die linke Grenze des Bin-Intervalls <--
526 sOut[i][0] = toString(_mAxisVals.a[i - 1], _option);
527
528 for (size_t j = 0; j < vHistMatrix[0].size(); j++)
529 {
530 sOut[i][j + 1] = toString(vHistMatrix[i - 1][j], _option);
531 }
532 }
533
534 // --> Uebergabe an Output::format(string**,int,int,Settings&), das den Rest erledigt
535 if (bFormat)
536 {
537 if (_out.isFile() || (!bSilent && _option.systemPrints()))
538 {
539 if (!_out.isFile())
540 {
542 make_hline();
543 NumeReKernel::print("NUMERE: " + toSystemCodePage(toUpperCase(_lang.get("HIST_HEADLINE"))));
544 make_hline();
545 }
546
547 _out.format(sOut, vHistMatrix[0].size() + 1, vHistMatrix.size() + 1, _option, true);
548
549 if (!_out.isFile())
550 {
552 make_hline();
553 }
554 }
555 }
556
557 // --> WICHTIG: Speicher wieder freigeben! <--
558 for (size_t i = 0; i < vHistMatrix.size() + 1; i++)
559 {
560 delete[] sOut[i];
561 }
562
563 delete[] sOut;
564}
565
566
578static mglGraph* prepareGraphForHist(double dAspect, PlotData& _pData, bool bSilent)
579{
580 // Create a new mglGraph instance on the heap
581 mglGraph* _histGraph = new mglGraph();
582
583 // Apply plot output size using the resolution
584 // and the aspect settings
585 if (_pData.getSettings(PlotData::INT_HIGHRESLEVEL) == 2 && bSilent && _pData.getSettings(PlotData::LOG_SILENTMODE))
586 {
587 double dHeight = sqrt(1920.0 * 1440.0 / dAspect);
588 _histGraph->SetSize((int)lrint(dAspect * dHeight), (int)lrint(dHeight));
589 }
590 else if (_pData.getSettings(PlotData::INT_HIGHRESLEVEL) == 1 && bSilent && _pData.getSettings(PlotData::LOG_SILENTMODE))
591 {
592 double dHeight = sqrt(1280.0 * 960.0 / dAspect);
593 _histGraph->SetSize((int)lrint(dAspect * dHeight), (int)lrint(dHeight));
594 }
595 else
596 {
597 double dHeight = sqrt(800.0 * 600.0 / dAspect);
598 _histGraph->SetSize((int)lrint(dAspect * dHeight), (int)lrint(dHeight));
599 }
600
601 // Get curret plot font, font size
602 // and the width of the bars
603 _histGraph->CopyFont(&_fontData);
604 _histGraph->SetFontSizeCM(0.24 * ((double)(1 + _pData.getSettings(PlotData::FLOAT_TEXTSIZE)) / 6.0), 72);
605 _histGraph->SetBarWidth(_pData.getSettings(PlotData::FLOAT_BARS) ? _pData.getSettings(PlotData::FLOAT_BARS) : 0.9);
606
607 _histGraph->SetRanges(1, 2, 1, 2, 1, 2);
608
609 // Apply logarithmic functions to the axes,
610 // if necessary
611 _histGraph->SetFunc(_pData.getLogscale(XRANGE) ? "lg(x)" : "",
612 _pData.getLogscale(YRANGE) ? "lg(y)" : "",
613 _pData.getLogscale(ZRANGE) ? "lg(z)" : "");
614
615 return _histGraph;
616}
617
618
633static void createPlotForHist1D(HistogramParameters& _histParams, mglData& _mAxisVals, mglData& _histData, const std::vector<std::string>& vLegends, int nMax, bool bSilent, bool bGrid)
634{
638
639 // Define aspect and plotting colors
640 double dAspect = 8.0 / 3.0;
641 int nStyle = 0;
642 const int nStyleMax = 14;
643 std::string sColorStyles[nStyleMax] = {"r", "g", "b", "q", "m", "P", "u", "R", "G", "B", "Q", "M", "p", "U"};
644
645 // Get current color definition
646 for (int i = 0; i < nStyleMax; i++)
647 {
648 sColorStyles[i] = _pData.getColors()[i];
649 }
650
651 // Get a new mglGraph instance located on the heap
652 mglGraph* _histGraph = prepareGraphForHist(dAspect, _pData, bSilent);
653
654 // Add all legends to the graph
655 for (size_t i = 0; i < vLegends.size(); i++)
656 {
657 _histGraph->AddLegend(vLegends[i].c_str(), sColorStyles[nStyle].c_str());
658
659 if (nStyle == nStyleMax - 1)
660 nStyle = 0;
661 else
662 nStyle++;
663 }
664
665 // Get the common exponent and the precalculated ticks
666 std::string sCommonExponent;
667 std::string sTicks = prepareTicksForHist1d(_histParams, _mAxisVals, sCommonExponent, bGrid);
668
669 // If we calculated a single histogram from a data grid,
670 // we need to use the z ranges for the x ranges, because
671 // those are the values we binned for
672 if (bGrid)
673 {
674 _histParams.ranges.x[0] = _histParams.ranges.z[0];
675 _histParams.ranges.x[1] = _histParams.ranges.z[1];
676 }
677
678 // Update the x ranges for a possible logscale
679 if (_pData.getLogscale(XRANGE) && _histParams.ranges.x[0] <= 0.0 && _histParams.ranges.x[1] > 0.0)
680 _histParams.ranges.x[0] = _histParams.ranges.x[1] / 1e3;
681 else if (_pData.getLogscale(XRANGE) && _histParams.ranges.x[0] < 0.0 && _histParams.ranges.x[1] <= 0.0)
682 {
683 _histParams.ranges.x[0] = 1.0;
684 _histParams.ranges.x[1] = 1.0;
685 }
686
687 // Update the y ranges for a possible logscale
688 if (_pData.getLogscale(YRANGE))
689 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], 0.1, 1.4 * (double)nMax);
690 else
691 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], 0.0, 1.05 * (double)nMax);
692
693 // Create the axes
695 {
696 if (!_pData.getLogscale(XRANGE) && _pData.getTimeAxis(XRANGE).use)
697 _histGraph->SetTicksTime('x', 0, _pData.getTimeAxis(XRANGE).sTimeFormat.c_str());
698 else if (!_pData.getLogscale(XRANGE))
699 _histGraph->SetTicksVal('x', _mAxisVals, sTicks.c_str());
700
701 if (!_pData.getSettings(PlotData::LOG_BOX)
702 && _histParams.ranges.x[0] <= 0.0
703 && _histParams.ranges.x[1] >= 0.0
704 && !_pData.getLogscale(YRANGE))
705 {
706 //_histGraph->SetOrigin(0.0,0.0);
707 if (_histParams.nBin > 40)
708 _histGraph->Axis("UAKDTVISO");
709 else
710 _histGraph->Axis("AKDTVISO");
711 }
712 else if (!_pData.getSettings(PlotData::LOG_BOX))
713 {
714 if (_histParams.nBin > 40)
715 _histGraph->Axis("UAKDTVISO");
716 else
717 _histGraph->Axis("AKDTVISO");
718 }
719 else
720 {
721 if (_histParams.nBin > 40)
722 _histGraph->Axis("U");
723 else
724 _histGraph->Axis();
725 }
726 }
727
728 // Create the surrounding box
729 if (_pData.getSettings(PlotData::LOG_BOX))
730 _histGraph->Box();
731
732 // Write the axis labels
734 {
735 _histGraph->Label('x', _histParams.sBinLabel.c_str(), 0.0);
736
737 if (sCommonExponent.length() && !_pData.getLogscale(XRANGE) && !_pData.getTimeAxis(XRANGE).use)
738 {
739 _histGraph->Puts(mglPoint(_histParams.ranges.x[1] + (_histParams.ranges.x[1] - _histParams.ranges.x[0]) / 10.0),
740 mglPoint(_histParams.ranges.x[1] + (_histParams.ranges.x[1] - _histParams.ranges.x[0]) / 10.0 + 1),
741 sCommonExponent.c_str(), ":TL", -1.3);
742 }
743
744 if (_pData.getSettings(PlotData::LOG_BOX))
745 _histGraph->Label('y', _histParams.sCountLabel.c_str(), 0.0);
746 else
747 _histGraph->Label('y', _histParams.sCountLabel.c_str(), 1.1);
748 }
749
750 // Create the grid
752 {
753 if (_pData.getSettings(PlotData::INT_GRID) == 2)
754 {
755 _histGraph->Grid("xy!", _pData.getGridStyle().c_str());
756 _histGraph->Grid("xy", _pData.getFineGridStyle().c_str());
757 }
758 else
759 _histGraph->Grid("xy", _pData.getGridStyle().c_str());
760 }
761
762 // Position the legend
763 if (!_pData.getSettings(PlotData::LOG_BOX))
764 _histGraph->Legend(1.25, 1.0);
765 else
766 _histGraph->Legend(_pData.getSettings(PlotData::INT_LEGENDPOSITION));
767
768 std::string sHistSavePath = _out.getFileName();
769
770 // --> Ausgabe-Info-Parameter loeschen und ggf. bFile = FALSE setzen <--
771
772 if (_option.systemPrints() && !bSilent)
773 NumeReKernel::printPreFmt(toSystemCodePage("|-> " + _lang.get("HIST_GENERATING_PLOT") + " ... "));
774
775 if (_out.isFile())
776 sHistSavePath = sHistSavePath.substr(0, sHistSavePath.length() - 4) + ".png";
777 else
778 sHistSavePath = _option.ValidFileName("<plotpath>/histogramm", ".png");
779
780 std::string sColor = "";
781 nStyle = 0;
782
783 // Create the color definition for the bars
784 for (int i = 0; i < _histData.GetNy(); i++)
785 {
786 sColor += sColorStyles[nStyle];
787
788 if (nStyle == nStyleMax - 1)
789 nStyle = 0;
790 else
791 nStyle++;
792 }
793
794 // Create the actual bars
795 _histGraph->Bars(_mAxisVals, _histData, sColor.c_str());
796
797 // Open the plot in the graph viewer
798 // of write it directly to file
800 {
801 GraphHelper* _graphHelper = new GraphHelper(_histGraph, _pData);
802 _graphHelper->setAspect(dAspect);
804 _histGraph = nullptr;
805 if (_option.systemPrints())
806 NumeReKernel::printPreFmt(_lang.get("COMMON_SUCCESS") + ".\n");
807 }
808 else
809 {
810 _histGraph->WriteFrame(sHistSavePath.c_str());
811 delete _histGraph;
812
813 if (_option.systemPrints() && !bSilent)
814 {
815 NumeReKernel::printPreFmt(_lang.get("COMMON_SUCCESS") + ".\n");
816 NumeReKernel::printPreFmt(LineBreak("| " + _lang.get("HIST_SAVED_AT", sHistSavePath), _option) + "\n");
817 }
818 }
819
820 _out.reset();
821}
822
823
839static void createHist1D(const std::string& sCmd, const std::string& sTargettable, Indices& _idx, Indices& _tIdx, HistogramParameters& _histParams, bool bWriteToCache, bool bSilent, bool bGrid)
840{
843
844 int nMax = 0;
845
846 mglData _histData;
847 mglData _mAxisVals;
848
849 if (bGrid)
850 {
851 // x-Range
852 prepareIntervalsForHist(sCmd, _histParams.ranges.x[0], _histParams.ranges.x[1],
853 _data.min(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1)).real(),
854 _data.max(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1)).real());
855
856 // y-Range
857 prepareIntervalsForHist(sCmd, _histParams.ranges.y[0], _histParams.ranges.y[1],
858 _data.min(_histParams.sTable, _idx.row, _idx.col.subidx(1, 1)).real(),
859 _data.max(_histParams.sTable, _idx.row, _idx.col.subidx(1, 1)).real());
860
861 // z-Range
862 prepareIntervalsForHist(sCmd, _histParams.ranges.z[0], _histParams.ranges.z[1],
863 _data.min(_histParams.sTable, _idx.row, _idx.col.subidx(2)).real(),
864 _data.max(_histParams.sTable, _idx.row, _idx.col.subidx(2)).real());
865 }
866 else
867 {
868 prepareIntervalsForHist(sCmd, _histParams.ranges.x[0], _histParams.ranges.x[1],
869 _data.min(_histParams.sTable, _idx.row, _idx.col).real(),
870 _data.max(_histParams.sTable, _idx.row, _idx.col).real());
871 }
872
873 for (size_t i = 0; i < _idx.row.size(); i++)
874 {
875 for (size_t j = 2 * bGrid; j < _idx.col.size(); j++)
876 {
877 if (_data.isValidElement(_idx.row[i], _idx.col[j], _histParams.sTable) && _data.getElement(_idx.row[i], _idx.col[j], _histParams.sTable).real() <= _histParams.ranges.x[1] && _data.getElement(_idx.row[i], _idx.col[j], _histParams.sTable).real() >= _histParams.ranges.x[0])
878 nMax++;
879 }
880 }
881
882 if (!_histParams.nBin && _histParams.binWidth[0] == 0.0)
883 {
884 if (bGrid)
885 {
886 if (_histParams.nMethod == STURGES)
887 _histParams.nBin = (int)rint(1.0 + 3.3 * log10((double)nMax));
888 else if (_histParams.nMethod == SCOTT)
889 _histParams.binWidth[0] = 3.49 * _data.std(_histParams.sTable, _idx.row, _idx.col.subidx(2)).real() / pow((double)nMax, 1.0 / 3.0);
890 else if (_histParams.nMethod == FREEDMAN_DIACONIS)
891 _histParams.binWidth[0] = 2.0 * (_data.pct(_histParams.sTable, _idx.row, _idx.col.subidx(2), 0.75).real() - _data.pct(_histParams.sTable, _idx.row, _idx.col.subidx(2), 0.25).real()) / pow((double)nMax, 1.0 / 3.0);
892 }
893 else
894 {
895 if (_histParams.nMethod == STURGES)
896 _histParams.nBin = (int)rint(1.0 + 3.3 * log10((double)nMax / (double)(_idx.col.size())));
897 else if (_histParams.nMethod == SCOTT)
898 _histParams.binWidth[0] = 3.49 * _data.std(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1)).real() / pow((double)nMax / (double)(_idx.col.size()), 1.0 / 3.0);
899 else if (_histParams.nMethod == FREEDMAN_DIACONIS)
900 _histParams.binWidth[0] = 2.0 * (_data.pct(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1), 0.75).real() - _data.pct(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1), 0.25).real()) / pow((double)nMax / (double)(_idx.col.size()), 1.0 / 3.0);
901 }
902 }
903
904 // Initialize the mglData objects
905 if (_histParams.nBin)
906 {
907 if (bGrid)
908 _histData.Create(_histParams.nBin);
909 else
910 _histData.Create(_histParams.nBin, _idx.col.size());
911
912 if (_histParams.binWidth[0] == 0.0)
913 {
914 // --> Berechne die Intervall-Laenge (Explizite Typumwandlung von int->double) <--
915 if (bGrid)
916 {
917 if (_pData.getLogscale(XRANGE))
918 _histParams.binWidth[0] = (log10(_histParams.ranges.z[1]) - log10(_histParams.ranges.z[0])) / (double)_histParams.nBin;
919 else
920 _histParams.binWidth[0] = abs(_histParams.ranges.z[1] - _histParams.ranges.z[0]) / (double)_histParams.nBin;
921 }
922 else
923 {
924 if (_pData.getLogscale(XRANGE))
925 _histParams.binWidth[0] = (log10(_histParams.ranges.x[1]) - log10(_histParams.ranges.x[0])) / (double)_histParams.nBin;
926 else
927 _histParams.binWidth[0] = abs(_histParams.ranges.x[1] - _histParams.ranges.x[0]) / (double)_histParams.nBin;
928 }
929 }
930 }
931 else
932 {
933 // --> Gut. Dann berechnen wir daraus die Anzahl der Bins -> Es kann nun aber sein, dass der letzte Bin ueber
934 // das Intervall hinauslaeuft <--
935 if (_histParams.binWidth[0] > _histParams.ranges.x[1] - _histParams.ranges.x[0])
937
938 for (int i = 0; (i * _histParams.binWidth[0]) + _histParams.ranges.x[0] < _histParams.ranges.x[1] + _histParams.binWidth[0]; i++)
939 {
940 _histParams.nBin++;
941 }
942
943 double dDiff = _histParams.nBin * _histParams.binWidth[0] - (double)(_histParams.ranges.x[1] - _histParams.ranges.x[0]);
944 _histParams.ranges.x[0] -= dDiff / 2.0;
945 _histParams.ranges.x[1] += dDiff / 2.0;
946
947 if (_histParams.nBin)
948 {
949 if (bGrid)
950 _histData.Create(_histParams.nBin);
951 else
952 _histData.Create(_histParams.nBin, _idx.col.size());
953 }
954 }
955
956 _mAxisVals.Create(_histParams.nBin);
957
958 // Calculate the data for the histogram
959 std::vector<std::string> vLegends;
960 std::vector<std::vector<double>> vHistMatrix = calculateHist1dData(_data, _idx, _histParams, _histData, _mAxisVals, nMax, vLegends, bGrid, _pData.getLogscale(XRANGE));
961
962 // Create the textual data for the terminal
963 // and the file, if necessary
964 createOutputForHist1D(_data, _idx, vHistMatrix, _histParams, _mAxisVals, bGrid,
965 !bWriteToCache || findParameter(sCmd, "export", '=') || findParameter(sCmd, "save", '='), bSilent);
966
967 // Store the results into the output table,
968 // if desired
969 if (bWriteToCache)
970 {
971 _data.setHeadLineElement(_tIdx.col.front(), sTargettable, "Bins");
972
973 for (size_t i = 0; i < vHistMatrix.size(); i++)
974 {
975 if (_tIdx.row.size() <= i)
976 break;
977
978 _data.writeToTable(_tIdx.row[i], _tIdx.col.front(), sTargettable, _histParams.ranges.x[0] + i * _histParams.binWidth[0] + _histParams.binWidth[0] / 2.0);
979
980 for (size_t j = 0; j < vHistMatrix[0].size(); j++)
981 {
982 if (_tIdx.col.size() <= j)
983 break;
984
985 if (!i)
986 _data.setHeadLineElement(_tIdx.col[j+1], sTargettable, _data.getHeadLineElement(_idx.col[j], _histParams.sTable));
987
988 _data.writeToTable(_tIdx.row[i], _tIdx.col[j+1], sTargettable, vHistMatrix[i][j]);
989 }
990 }
991 }
992
993 // Create the plot using the calculated data
994 createPlotForHist1D(_histParams, _mAxisVals, _histData, vLegends, nMax, bSilent, bGrid);
995}
996
997
1010static void calculateDataForCenterPlot(MemoryManager& _data, const Indices& _idx, const HistogramParameters& _histParams, mglData _hist2DData[3])
1011{
1012 if (_idx.col.size() == 3)
1013 {
1014 for (unsigned int i = 0; i < 3; i++)
1015 {
1016 _hist2DData[i].Create(_idx.row.size());
1017 }
1018
1019 for (size_t i = 0; i < _idx.row.size(); i++)
1020 {
1021 if (_data.isValidElement(_idx.row[i], _idx.col[0], _histParams.sTable)
1022 && _data.isValidElement(_idx.row[i], _idx.col[1], _histParams.sTable)
1023 && _data.isValidElement(_idx.row[i], _idx.col[2], _histParams.sTable)
1024 && _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real() <= _histParams.ranges.x[1]
1025 && _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real() >= _histParams.ranges.x[0]
1026 && _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real() <= _histParams.ranges.y[1]
1027 && _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real() >= _histParams.ranges.y[0])
1028 {
1029 _hist2DData[0].a[i] = _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real();
1030 _hist2DData[1].a[i] = _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real();
1031 _hist2DData[2].a[i] = _data.getElement(_idx.row[i], _idx.col[2], _histParams.sTable).real();
1032 }
1033 else
1034 {
1035 for (unsigned int k = 0; k < 3; k++)
1036 _hist2DData[k].a[i] = NAN;
1037 }
1038 }
1039 }
1040 else
1041 {
1042 _hist2DData[0].Create(_idx.row.size());
1043 _hist2DData[1].Create(_idx.row.size());
1044 _hist2DData[2].Create(_idx.row.size(), _idx.col.size() - 2);
1045
1046 for (size_t i = 0; i < _idx.row.size(); i++)
1047 {
1048 _hist2DData[0].a[i] = _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real();
1049 _hist2DData[1].a[i] = _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real();
1050
1051 for (size_t j = 0; j < _idx.col.size() - 2; j++)
1052 {
1053 if (_data.isValidElement(_idx.row[i], _idx.col[j + 2], _histParams.sTable)
1054 && _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real() <= _histParams.ranges.x[1]
1055 && _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real() >= _histParams.ranges.x[0]
1056 && _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real() <= _histParams.ranges.y[1]
1057 && _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real() >= _histParams.ranges.y[0])
1058 _hist2DData[2].a[i + j * _idx.row.size()] = _data.getElement(_idx.row[i], _idx.col[j + 2], _histParams.sTable).real();
1059 else
1060 _hist2DData[2].a[i + j * _idx.row.size()] = NAN;
1061 }
1062 }
1063
1064 }
1065}
1066
1067
1089static mglData calculateXYHist(MemoryManager& _data, const Indices& _idx, const HistogramParameters& _histParams, mglData* _mAxisVals, double dBinMin, double dMin, double dMax, double dIntLength, int nMax, bool isLogScale, bool isHbar, bool bSum)
1090{
1091 mglData _histData(_histParams.nBin);
1092
1093 for (int k = 0; k < _histParams.nBin; k++)
1094 {
1095 double dSum = 0.0;
1096
1097 for (size_t i = 0; i < _idx.row.size(); i++)
1098 {
1099 if (_data.isValidElement(_idx.row[i], _idx.col[isHbar], _histParams.sTable)
1100 && ((!isLogScale
1101 && _data.getElement(_idx.row[i], _idx.col[isHbar], _histParams.sTable).real() >= dBinMin + k * dIntLength
1102 && _data.getElement(_idx.row[i], _idx.col[isHbar], _histParams.sTable).real() < dBinMin + (k + 1)*dIntLength)
1103 || (isLogScale
1104 && _data.getElement(_idx.row[i], _idx.col[isHbar], _histParams.sTable).real() >= pow(10.0, log10(dBinMin) + k * dIntLength)
1105 && _data.getElement(_idx.row[i], _idx.col[isHbar], _histParams.sTable).real() < pow(10.0, log10(dBinMin) + (k + 1)*dIntLength))
1106 )
1107 )
1108 {
1109 if (_idx.col.size() == 3)
1110 {
1111 if (_data.isValidElement(_idx.row[i], _idx.col[!isHbar], _histParams.sTable)
1112 && _data.isValidElement(_idx.row[i], _idx.col[2], _histParams.sTable)
1113 && _data.getElement(_idx.row[i], _idx.col[2], _histParams.sTable).real() >= _histParams.ranges.z[0]
1114 && _data.getElement(_idx.row[i], _idx.col[2], _histParams.sTable).real() <= _histParams.ranges.z[1]
1115 && _data.getElement(_idx.row[i], _idx.col[!isHbar], _histParams.sTable).real() >= dMin
1116 && _data.getElement(_idx.row[i], _idx.col[!isHbar], _histParams.sTable).real() <= dMax)
1117 {
1118 if (bSum)
1119 dSum += _data.getElement(_idx.row[i], _idx.col[2], _histParams.sTable).real();
1120 else
1121 dSum++;
1122 }
1123 }
1124 else
1125 {
1126 for (size_t l = 0; l < _idx.row.size(); l++)
1127 {
1128 if (_data.isValidElement(_idx.row[l], _idx.col[!isHbar], _histParams.sTable)
1129 && _data.isValidElement(_idx.row[(!isHbar*i+isHbar*l)], _idx.col[(!isHbar*l+isHbar*i)+2], _histParams.sTable)
1130 && _data.getElement(_idx.row[(!isHbar*i+isHbar*l)], _idx.col[(!isHbar*l+isHbar*i)+2], _histParams.sTable).real() >= _histParams.ranges.z[0]
1131 && _data.getElement(_idx.row[(!isHbar*i+isHbar*l)], _idx.col[(!isHbar*l+isHbar*i)+2], _histParams.sTable).real() <= _histParams.ranges.z[1]
1132 && _data.getElement(_idx.row[l], _idx.col[!isHbar], _histParams.sTable).real() >= dMin
1133 && _data.getElement(_idx.row[l], _idx.col[!isHbar], _histParams.sTable).real() <= dMax)
1134 dSum += _data.getElement(_idx.row[(!isHbar*i+isHbar*l)], _idx.col[(!isHbar*l+isHbar*i)+2], _histParams.sTable).real();
1135 }
1136 }
1137 }
1138 }
1139
1140 _histData.a[k] = dSum;
1141
1142 if (!isLogScale)
1143 _mAxisVals->a[k] = dBinMin + (k + 0.5) * dIntLength;
1144 else
1145 _mAxisVals->a[k] = pow(10.0, log10(dBinMin) + (k + 0.5) * dIntLength);
1146 }
1147
1148 return _histData;
1149}
1150
1151
1172static void createOutputForHist2D(MemoryManager& _data, const Indices& _idx, const std::string& sTargettable, const Indices& _tIdx, const HistogramParameters& _histParams, mglData _mAxisVals[2], mglData& _barHistData, mglData& _hBarHistData, bool bSum, bool bWriteToCache, bool shallFormat, bool isSilent)
1173{
1175 const Settings& _option = NumeReKernel::getInstance()->getSettings();
1176
1177 std::string** sOut = new std::string*[_histParams.nBin + 1];
1178
1179 for (int k = 0; k < _histParams.nBin + 1; k++)
1180 sOut[k] = new std::string[4];
1181
1182 // Fill output tables
1183 for (int k = 0; k < _histParams.nBin; k++)
1184 {
1185 sOut[k + 1][0] = toString(_mAxisVals[0].a[k], _option);
1186 sOut[k + 1][1] = toString(_barHistData.a[k], _option);
1187 sOut[k + 1][2] = toString(_mAxisVals[1].a[k], _option);
1188 sOut[k + 1][3] = toString(_hBarHistData.a[k], _option);
1189
1190 if (!k)
1191 {
1192 sOut[k][0] = "Bins_[x]";
1193 sOut[k][1] = (bSum ? "Sum_[x]" : "Counts_[x]");
1194 sOut[k][2] = "Bins_[y]";
1195 sOut[k][3] = (bSum ? "Sum_[y]" : "Counts_[y]");
1196 }
1197
1198 if (bWriteToCache)
1199 {
1200 if (_tIdx.row.size() <= (size_t)k || _tIdx.col.size() < 4)
1201 continue;
1202
1203 if (!k)
1204 {
1205 _data.setHeadLineElement(_tIdx.col[0], sTargettable, "Bins [x]");
1206 _data.setHeadLineElement(_tIdx.col[1], sTargettable, (bSum ? "Sum [x]" : "Counts [x]"));
1207 _data.setHeadLineElement(_tIdx.col[2], sTargettable, "Bins [y]");
1208 _data.setHeadLineElement(_tIdx.col[3], sTargettable, (bSum ? "Sum [y]" : "Counts [y]"));
1209 }
1210
1211 _data.writeToTable(_tIdx.row[k], _tIdx.col[0], sTargettable, _mAxisVals[0].a[k]);
1212 _data.writeToTable(_tIdx.row[k], _tIdx.col[1], sTargettable, _barHistData.a[k]);
1213 _data.writeToTable(_tIdx.row[k], _tIdx.col[2], sTargettable, _mAxisVals[1].a[k]);
1214 _data.writeToTable(_tIdx.row[k], _tIdx.col[3], sTargettable, _hBarHistData.a[k]);
1215 }
1216 }
1217
1218 if (shallFormat)
1219 {
1220 _out.setPluginName("2D-" + _lang.get("HIS_OUT_PLGNINFO", PI_HIST, toString(_idx.col.front()+1), toString(_idx.col.last()+1), _data.getDataFileName(_histParams.sTable)));
1221 _out.setCommentLine(_lang.get("HIST_OUT_COMMENTLINE2D", toString(_histParams.ranges.x[0], 5), toString(_histParams.ranges.x[1], 5), toString(_histParams.binWidth[0], 5), toString(_histParams.ranges.y[0], 5), toString(_histParams.ranges.y[1], 5), toString(_histParams.binWidth[1], 5)));
1222
1223 _out.setPrefix("hist2d");
1224 }
1225
1226 if (_out.isFile())
1227 {
1228 _out.setStatus(true);
1229 _out.setCompact(false);
1230
1231 if (_histParams.sSavePath.length())
1232 _out.setFileName(_histParams.sSavePath);
1233 else
1234 _out.generateFileName();
1235 }
1236 else
1237 _out.setCompact(_option.createCompactTables());
1238
1239 if (shallFormat)
1240 {
1241 if (_out.isFile() || !isSilent)
1242 {
1243 if (!_out.isFile())
1244 {
1246 make_hline();
1247 NumeReKernel::print("NUMERE: 2D-" + toSystemCodePage(toUpperCase(_lang.get("HIST_HEADLINE"))));
1248 make_hline();
1249 }
1250
1251 _out.format(sOut, 4, _histParams.nBin + 1, _option, true);
1252
1253 if (!_out.isFile())
1254 {
1256 make_hline();
1257 }
1258 }
1259 }
1260
1261
1262 for (int k = 0; k < _histParams.nBin + 1; k++)
1263 delete[] sOut[k];
1264 delete[] sOut;
1265}
1266
1267
1284static void createPlotsForHist2D(const std::string& sCmd, HistogramParameters& _histParams, mglData _mAxisVals[2], mglData& _barHistData, mglData& _hBarHistData, mglData _hist2DData[3], bool isScatterPlot, bool bSum, bool bSilent)
1285{
1289
1290 double dAspect = 4.0 / 3.0;
1291
1292 mglGraph* _histGraph = prepareGraphForHist(dAspect, _pData, bSilent);
1293
1295 _histGraph->MultiPlot(3, 3, 0, 2, 1, "<>");
1296 _histGraph->SetBarWidth(0.9);
1297 _histGraph->SetTuneTicks(3, 1.05);
1298 _histParams.binWidth[0] = _barHistData.Maximal() - _barHistData.Minimal();
1299 _histGraph->SetRanges(1, 2, 1, 2, 1, 2);
1300
1301 _histGraph->SetFunc(_pData.getLogscale(XRANGE) ? "lg(x)" : "",
1302 _pData.getLogscale(ZRANGE) ? "lg(y)" : "");
1303
1304 if (_barHistData.Minimal() >= 0)
1305 {
1306 if (_pData.getLogscale(ZRANGE) && _barHistData.Maximal() > 0.0)
1307 {
1308 if (_barHistData.Minimal() - _histParams.binWidth[0] / 20.0 > 0)
1309 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], _barHistData.Minimal() - _histParams.binWidth[0] / 20.0, _barHistData.Maximal() + _histParams.binWidth[0] / 20.0);
1310 else if (_barHistData.Minimal() > 0)
1311 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], _barHistData.Minimal() / 2.0, _barHistData.Maximal() + _histParams.binWidth[0] / 20.0);
1312 else
1313 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], (1e-2 * _barHistData.Maximal() < 1e-2 ? 1e-2 * _barHistData.Maximal() : 1e-2), _barHistData.Maximal() + _histParams.binWidth[0] / 20.0);
1314 }
1315 else if (_barHistData.Maximal() < 0.0 && _pData.getLogscale(ZRANGE))
1316 {
1317 delete _histGraph;
1319 }
1320 else
1321 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], 0.0, _barHistData.Maximal() + _histParams.binWidth[0] / 20.0);
1322 }
1323 else
1324 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], _barHistData.Minimal() - _histParams.binWidth[0] / 20.0, _barHistData.Maximal() + _histParams.binWidth[0] / 20.0);
1325
1326 if (!_pData.getLogscale(XRANGE) && _pData.getTimeAxis(XRANGE).use)
1327 _histGraph->SetTicksTime('x', 0, _pData.getTimeAxis(XRANGE).sTimeFormat.c_str());
1328
1329 _histGraph->Box();
1330 _histGraph->Axis();
1331
1332 if (_pData.getSettings(PlotData::INT_GRID) == 1)
1333 _histGraph->Grid("xy", _pData.getGridStyle().c_str());
1334 else if (_pData.getSettings(PlotData::INT_GRID) == 2)
1335 {
1336 _histGraph->Grid("xy!", _pData.getGridStyle().c_str());
1337 _histGraph->Grid("xy", _pData.getFineGridStyle().c_str());
1338 }
1339
1340 _histGraph->Label('x', _histParams.sAxisLabels[0].c_str(), 0);
1341
1342 if (bSum)
1343 _histGraph->Label('y', _histParams.sAxisLabels[2].c_str(), 0);
1344 else
1345 _histGraph->Label('y', _histParams.sCountLabel.c_str(), 0);
1346
1347 _histGraph->Bars(_mAxisVals[0], _barHistData, _pData.getColors().c_str());
1348
1350 _histGraph->MultiPlot(3, 3, 3, 2, 2, "<_>");
1351 _histGraph->SetTuneTicks(3, 1.05);
1352
1353 _histGraph->SetRanges(1, 2, 1, 2, 1, 2);
1354 _histGraph->SetFunc(_pData.getLogscale(XRANGE) ? "lg(x)" : "",
1355 _pData.getLogscale(YRANGE) ? "lg(y)" : "",
1356 _pData.getLogscale(ZRANGE) ? "lg(z)" : "");
1357
1358 if (_hist2DData[2].Maximal() < 0 && _pData.getLogscale(ZRANGE))
1359 {
1360 delete _histGraph;
1362 }
1363
1364 if (_pData.getLogscale(ZRANGE))
1365 {
1366 if (_hist2DData[2].Minimal() > 0.0)
1367 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], _histParams.ranges.y[0], _histParams.ranges.y[1], _hist2DData[2].Minimal(), _hist2DData[2].Maximal());
1368 else
1369 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], _histParams.ranges.y[0], _histParams.ranges.y[1], (1e-2 * _hist2DData[2].Maximal() < 1e-2 ? 1e-2 * _hist2DData[2].Maximal() : 1e-2), _hist2DData[2].Maximal());
1370 }
1371 else
1372 _histGraph->SetRanges(_histParams.ranges.x[0], _histParams.ranges.x[1], _histParams.ranges.y[0], _histParams.ranges.y[1], _hist2DData[2].Minimal(), _hist2DData[2].Maximal());
1373
1374 if (!_pData.getLogscale(XRANGE) && _pData.getTimeAxis(XRANGE).use)
1375 _histGraph->SetTicksTime('x', 0, _pData.getTimeAxis(XRANGE).sTimeFormat.c_str());
1376
1377 if (!_pData.getLogscale(YRANGE) && _pData.getTimeAxis(YRANGE).use)
1378 _histGraph->SetTicksTime('y', 0, _pData.getTimeAxis(YRANGE).sTimeFormat.c_str());
1379
1380 _histGraph->Box();
1381 _histGraph->Axis("xy");
1382 _histGraph->Colorbar(_pData.getColorScheme("I>").c_str());
1383
1384 if (_pData.getSettings(PlotData::INT_GRID) == 1)
1385 _histGraph->Grid("xy", _pData.getGridStyle().c_str());
1386 else if (_pData.getSettings(PlotData::INT_GRID) == 2)
1387 {
1388 _histGraph->Grid("xy!", _pData.getGridStyle().c_str());
1389 _histGraph->Grid("xy", _pData.getFineGridStyle().c_str());
1390 }
1391
1392 _histGraph->Label('x', _histParams.sAxisLabels[0].c_str(), 0);
1393 _histGraph->Label('y', _histParams.sAxisLabels[1].c_str(), 0);
1394
1395 if (isScatterPlot)
1396 _histGraph->Dots(_hist2DData[0], _hist2DData[1], _hist2DData[2], _pData.getColorScheme().c_str());
1397 else
1398 _histGraph->Dens(_hist2DData[0], _hist2DData[1], _hist2DData[2], _pData.getColorScheme().c_str());
1399
1401 _histGraph->MultiPlot(3, 3, 5, 1, 2, "_");
1402 _histGraph->SetBarWidth(0.9);
1403 _histGraph->SetTuneTicks(3, 1.05);
1404 _histParams.binWidth[0] = _hBarHistData.Maximal() - _hBarHistData.Minimal();
1405
1406 _histGraph->SetRanges(1, 2, 1, 2, 1, 2);
1407 _histGraph->SetFunc(_pData.getLogscale(ZRANGE) ? "lg(x)" : "",
1408 _pData.getLogscale(YRANGE) ? "lg(y)" : "");
1409
1410 if (_hBarHistData.Minimal() >= 0)
1411 {
1412 if (_pData.getLogscale(ZRANGE) && _hBarHistData.Maximal() > 0.0)
1413 {
1414 if (_hBarHistData.Minimal() - _histParams.binWidth[0] / 20.0 > 0)
1415 _histGraph->SetRanges(_hBarHistData.Minimal() - _histParams.binWidth[0] / 20.0, _hBarHistData.Maximal() + _histParams.binWidth[0] / 20.0, _histParams.ranges.y[0], _histParams.ranges.y[1]);
1416 else if (_hBarHistData.Minimal() > 0)
1417 _histGraph->SetRanges(_hBarHistData.Minimal() / 2.0, _hBarHistData.Maximal() + _histParams.binWidth[0] / 20.0, _histParams.ranges.y[0], _histParams.ranges.y[1]);
1418 else
1419 _histGraph->SetRanges((1e-2 * _hBarHistData.Maximal() < 1e-2 ? 1e-2 * _hBarHistData.Maximal() : 1e-2), _hBarHistData.Maximal() + _histParams.binWidth[0] / 10.0, _histParams.ranges.y[0], _histParams.ranges.y[1]);
1420 }
1421 else if (_hBarHistData.Maximal() < 0.0 && _pData.getLogscale(ZRANGE))
1422 {
1423 delete _histGraph;
1425 }
1426 else
1427 _histGraph->SetRanges(0.0, _hBarHistData.Maximal() + _histParams.binWidth[0] / 20.0, _histParams.ranges.y[0], _histParams.ranges.y[1]);
1428 }
1429 else
1430 _histGraph->SetRanges(_hBarHistData.Minimal() - _histParams.binWidth[0] / 20.0, _hBarHistData.Maximal() + _histParams.binWidth[0] / 20.0, _histParams.ranges.y[0], _histParams.ranges.y[1]);
1431
1432 if (!_pData.getLogscale(YRANGE) && _pData.getTimeAxis(YRANGE).use)
1433 _histGraph->SetTicksTime('y', 0, _pData.getTimeAxis(YRANGE).sTimeFormat.c_str());
1434
1435 _histGraph->Box();
1436 _histGraph->Axis("xy");
1437
1438 if (_pData.getSettings(PlotData::INT_GRID) == 1)
1439 _histGraph->Grid("xy", _pData.getGridStyle().c_str());
1440 else if (_pData.getSettings(PlotData::INT_GRID) == 2)
1441 {
1442 _histGraph->Grid("xy!", _pData.getGridStyle().c_str());
1443 _histGraph->Grid("xy", _pData.getFineGridStyle().c_str());
1444 }
1445
1446 if (!bSum)
1447 _histGraph->Label('x', _histParams.sCountLabel.c_str(), 0);
1448 else
1449 _histGraph->Label('x', _histParams.sAxisLabels[2].c_str(), 0);
1450
1451 _histGraph->Label('y', _histParams.sAxisLabels[1].c_str(), 0);
1452 _histGraph->Barh(_mAxisVals[1], _hBarHistData, _pData.getColors().c_str());
1453
1455 std::string sHistSavePath = _out.getFileName();
1456
1457 if (_option.systemPrints() && !bSilent)
1458 NumeReKernel::printPreFmt(toSystemCodePage("|-> " + _lang.get("HIST_GENERATING_PLOT") + " ... "));
1459
1460 if (_out.isFile())
1461 sHistSavePath = sHistSavePath.substr(0, sHistSavePath.length() - 4) + ".png";
1462 else
1463 sHistSavePath = _option.ValidFileName("<plotpath>/histogramm2d", ".png");
1464
1465 if (_pData.getSettings(PlotData::LOG_OPENIMAGE) && !_pData.getSettings(PlotData::LOG_SILENTMODE) && !bSilent)
1466 {
1467 GraphHelper* _graphHelper = new GraphHelper(_histGraph, _pData);
1468 _graphHelper->setAspect(dAspect);
1470 _histGraph = nullptr;
1471
1472 if (_option.systemPrints())
1473 NumeReKernel::printPreFmt(_lang.get("COMMON_SUCCESS") + ".\n");
1474 }
1475 else
1476 {
1477 _histGraph->WriteFrame(sHistSavePath.c_str());
1478 delete _histGraph;
1479
1480 if (_option.systemPrints() && !bSilent)
1481 NumeReKernel::printPreFmt(_lang.get("COMMON_SUCCESS") + ".\n");
1482 if (!_out.isFile() && _option.systemPrints() && !bSilent)
1483 NumeReKernel::printPreFmt(LineBreak("| " + _lang.get("HIST_SAVED_AT", sHistSavePath), _option) + "\n");
1484 }
1485}
1486
1487
1503static void createHist2D(const std::string& sCmd, const std::string& sTargettable, Indices& _idx, Indices& _tIdx, HistogramParameters& _histParams, bool bSum, bool bWriteToCache, bool bSilent)
1504{
1509
1510 int nMax = 0;
1511
1512 mglData _hist2DData[3];
1513 mglData _mAxisVals[2];
1514
1515 if (_idx.col.size() < 3)
1517
1518 if (_idx.col.size() > 3)
1519 bSum = true;
1520
1521 // Update the interval ranges using the minimal and maximal
1522 // data values in the corresponding spatial directions
1523 prepareIntervalsForHist(sCmd, _histParams.ranges.x[0], _histParams.ranges.x[1],
1524 _data.min(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1)).real(),
1525 _data.max(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1)).real());
1526
1527 prepareIntervalsForHist(sCmd, _histParams.ranges.y[0], _histParams.ranges.y[1],
1528 _data.min(_histParams.sTable, _idx.row, _idx.col.subidx(1, 1)).real(),
1529 _data.max(_histParams.sTable, _idx.row, _idx.col.subidx(1, 1)).real());
1530
1531 prepareIntervalsForHist(sCmd, _histParams.ranges.z[0], _histParams.ranges.z[1],
1532 _data.min(_histParams.sTable, _idx.row, _idx.col.subidx(2)).real(),
1533 _data.max(_histParams.sTable, _idx.row, _idx.col.subidx(2)).real());
1534
1535 // Adapt the ranges for logscale
1536 if (_pData.getLogscale(XRANGE) && _histParams.ranges.x[1] < 0.0)
1538 else if (_pData.getLogscale(XRANGE))
1539 {
1540 if (_histParams.ranges.x[0] < 0.0)
1541 _histParams.ranges.x[0] = (1e-2 * _histParams.ranges.x[1] < 1e-2 ? 1e-2 * _histParams.ranges.x[1] : 1e-2);
1542 }
1543
1544 if (_pData.getLogscale(YRANGE) && _histParams.ranges.y[1] < 0.0)
1546 else if (_pData.getLogscale(YRANGE))
1547 {
1548 if (_histParams.ranges.y[0] < 0.0)
1549 _histParams.ranges.y[0] = (1e-2 * _histParams.ranges.y[1] < 1e-2 ? 1e-2 * _histParams.ranges.y[1] : 1e-2);
1550 }
1551
1552 // Count the number of valid entries for determining
1553 // the number of bins based upon the methods automatically
1554 for (size_t i = 0; i < _idx.row.size(); i++)
1555 {
1556 for (size_t j = 2; j < _idx.col.size(); j++)
1557 {
1558 if (_data.isValidElement(_idx.row[i], _idx.col[j], _histParams.sTable)
1559 && _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real() <= _histParams.ranges.x[1]
1560 && _data.getElement(_idx.row[i], _idx.col[0], _histParams.sTable).real() >= _histParams.ranges.x[0]
1561 && _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real() <= _histParams.ranges.y[1]
1562 && _data.getElement(_idx.row[i], _idx.col[1], _histParams.sTable).real() >= _histParams.ranges.y[0])
1563 nMax++;
1564 }
1565 }
1566
1567 // Determine the number of bins based upon the
1568 // selected method
1569 if (!_histParams.nBin && _histParams.binWidth[0] == 0.0)
1570 {
1571 if (_histParams.nMethod == STURGES)
1572 _histParams.nBin = (int)rint(1.0 + 3.3 * log10((double)nMax / (double)(_idx.col.size()-2)));
1573 else if (_histParams.nMethod == SCOTT)
1574 _histParams.binWidth[0] = 3.49 * _data.std(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1)).real() / pow((double)nMax / (double)(_idx.col.size()), 1.0 / 3.0);
1575 else if (_histParams.nMethod == FREEDMAN_DIACONIS)
1576 _histParams.binWidth[0] = 2.0 * (_data.pct(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1), 0.75).real() - _data.pct(_histParams.sTable, _idx.row, _idx.col.subidx(0, 1), 0.25).real()) / pow((double)nMax / (double)(_idx.col.size()), 1.0 / 3.0);
1577 }
1578
1579 // Determine the number of bins based upon the
1580 // selected bin interval width
1581 if (!_histParams.nBin)
1582 {
1583 if (_histParams.binWidth[0] > _histParams.ranges.x[1] - _histParams.ranges.x[0])
1585
1586 for (int i = 0; (i * _histParams.binWidth[0]) + _histParams.ranges.x[0] < _histParams.ranges.x[1] + _histParams.binWidth[0]; i++)
1587 {
1588 _histParams.nBin++;
1589 }
1590
1591 double dDiff = _histParams.nBin * _histParams.binWidth[0] - (double)(_histParams.ranges.x[1] - _histParams.ranges.x[0]);
1592 _histParams.ranges.x[0] -= dDiff / 2.0;
1593 _histParams.ranges.x[1] += dDiff / 2.0;
1594 }
1595
1596 _mAxisVals[0].Create(_histParams.nBin);
1597 _mAxisVals[1].Create(_histParams.nBin);
1598
1599 // Determine the bin interval width
1600 if (_histParams.binWidth[0] == 0.0)
1601 {
1602 // --> Berechne die Intervall-Laenge (Explizite Typumwandlung von int->double) <--
1603 if (_pData.getLogscale(XRANGE))
1604 _histParams.binWidth[0] = (log10(_histParams.ranges.x[1]) - log10(_histParams.ranges.x[0])) / (double)_histParams.nBin;
1605 else
1606 _histParams.binWidth[0] = abs(_histParams.ranges.x[1] - _histParams.ranges.x[0]) / (double)_histParams.nBin;
1607 }
1608
1609 // Determine the bin interval width in
1610 // y direction
1611 if (_pData.getLogscale(YRANGE))
1612 _histParams.binWidth[1] = (log10(_histParams.ranges.y[1]) - log10(_histParams.ranges.y[0])) / (double)_histParams.nBin;
1613 else
1614 _histParams.binWidth[1] = abs(_histParams.ranges.y[1] - _histParams.ranges.y[0]) / (double)_histParams.nBin;
1615
1616 // Calculate the necessary data for the center plot
1617 calculateDataForCenterPlot(_data, _idx, _histParams, _hist2DData);
1618
1619 // Calculate the necessary data for the bar chart
1620 // along the y axis of the central plot
1621 mglData _barHistData = calculateXYHist(_data, _idx, _histParams, &_mAxisVals[0],
1622 _histParams.ranges.x[0], _histParams.ranges.y[0], _histParams.ranges.y[1], _histParams.binWidth[0],
1623 nMax, _pData.getLogscale(XRANGE), false, bSum);
1624
1625 // Calculate the necessary data for the horizontal
1626 // bar chart along the x axis of the central plot
1627 mglData _hBarHistData = calculateXYHist(_data, _idx, _histParams, &_mAxisVals[1],
1628 _histParams.ranges.y[0], _histParams.ranges.x[0], _histParams.ranges.x[1], _histParams.binWidth[1],
1629 nMax, _pData.getLogscale(YRANGE), true, bSum);
1630
1631 // Format the data for the textual output for the
1632 // terminal, the text file and store the result in
1633 // the target table, if desired
1634 createOutputForHist2D(_data, _idx, sTargettable, _tIdx, _histParams, _mAxisVals, _barHistData, _hBarHistData, bSum, bWriteToCache,
1635 !bWriteToCache || findParameter(sCmd, "export", '=') || findParameter(sCmd, "save", '='),
1636 !_option.systemPrints() || bSilent);
1637
1638 // Create the three plots as subplots
1639 createPlotsForHist2D(sCmd, _histParams, _mAxisVals, _barHistData, _hBarHistData, _hist2DData, _idx.col.size() == 3, bSum, bSilent);
1640
1641 _out.reset();
1642}
1643
1644
1653void plugin_histogram(std::string& sCmd)
1654{
1657
1658 if (!_data.isValid()) // Sind ueberhaupt Daten vorhanden?
1660
1661 bool bWriteToCache = false;
1662 bool bMake2DHist = false;
1663 bool bSum = false;
1664 bool bSilent = false;
1665 bool bGrid = false;
1666
1667
1668 HistogramParameters _histParams;
1669
1670 _histParams.sTable = "data";
1671 _histParams.sAxisLabels[0] = "\\i x";
1672 _histParams.sAxisLabels[1] = "\\i y";
1673 _histParams.sAxisLabels[2] = "\\i z";
1674 _histParams.nMethod = STURGES;
1675 _histParams.ranges.x[0] = NAN;
1676 _histParams.ranges.x[1] = NAN;
1677 _histParams.ranges.y[0] = NAN;
1678 _histParams.ranges.y[1] = NAN;
1679 _histParams.ranges.z[0] = NAN;
1680 _histParams.ranges.z[1] = NAN;
1681
1682 Indices _idx;
1683 Indices _tIdx;
1684
1685 if (_data.matchTableAsParameter(sCmd).length())
1686 _histParams.sTable = _data.matchTableAsParameter(sCmd);
1687 else
1688 {
1689 DataAccessParser _accessParser(sCmd);
1690
1691 if (!_accessParser.getDataObject().length())
1693
1694 _accessParser.evalIndices();
1695 _histParams.sTable = _accessParser.getDataObject();
1696 _idx = _accessParser.getIndices();
1697 }
1698
1699 if (_data.isEmpty(_histParams.sTable))
1701
1702 if (findCommand(sCmd).sString == "hist2d")
1703 bMake2DHist = true;
1704
1705 if ((findParameter(sCmd, "cols", '=') || findParameter(sCmd, "c", '=')) && !isValidIndexSet(_idx))
1706 {
1707 long long int nDataRow = VectorIndex::INVALID;
1708 long long int nDataRowFinal = 0;
1709
1710 int nPos = 0;
1711
1712 if (findParameter(sCmd, "cols", '='))
1713 nPos = findParameter(sCmd, "cols", '=') + 4;
1714 else
1715 nPos = findParameter(sCmd, "c", '=') + 1;
1716
1717 std::string sTemp = getArgAtPos(sCmd, nPos);
1718 std::string sTemp_2 = "";
1719 StripSpaces(sTemp);
1720
1721 if (sTemp.find(':') != std::string::npos)
1722 {
1723 int nSep = 0;
1724
1725 for (unsigned int i = 0; i < sTemp.length(); i++)
1726 {
1727 if (sTemp[i] == ':')
1728 {
1729 nSep = i;
1730 break;
1731 }
1732 }
1733
1734 sTemp_2 = sTemp.substr(0, nSep);
1735 sTemp = sTemp.substr(nSep + 1);
1736 StripSpaces(sTemp);
1737 StripSpaces(sTemp_2);
1738
1739 if (sTemp_2.length())
1740 nDataRow = intCast(StrToDb(sTemp_2));
1741 else
1742 nDataRow = 1;
1743
1744 if (sTemp.length())
1745 {
1746 nDataRowFinal = intCast(StrToDb(sTemp));
1747
1748 if (nDataRowFinal > _data.getCols(_histParams.sTable) || !nDataRowFinal)
1749 nDataRowFinal = _data.getCols(_histParams.sTable);
1750 }
1751 else
1752 nDataRowFinal = _data.getCols(_histParams.sTable);
1753 }
1754 else if (sTemp.length())
1755 nDataRow = intCast(StrToDb(sTemp));
1756
1757 _idx.row = VectorIndex(0, _data.getLines(_histParams.sTable)-1);
1758 _idx.col = VectorIndex(nDataRow, nDataRowFinal-1);
1759 }
1760
1761 _histParams.nBin = intCast(StrToDb(getParameterValue(sCmd, "bins", "b", "0")));
1762 _histParams.binWidth[0] = StrToDb(getParameterValue(sCmd, "width", "w", "0"));
1763 std::string sTargettable = getParameterValue(sCmd, "tocache", "totable", "");
1764
1765 if (sTargettable.length())
1766 {
1767 bWriteToCache = true;
1768
1769 if (sTargettable.find('(') == std::string::npos)
1770 sTargettable += "()";
1771
1772 if (!_data.isTable(sTargettable))
1773 _data.addTable(sTargettable, NumeReKernel::getInstance()->getSettings());
1774
1775 sTargettable.erase(sTargettable.find('('));
1777 _tIdx.col = VectorIndex(_data.getCols(sTargettable), VectorIndex::OPEN_END);
1778 }
1779 else
1780 {
1781 sTargettable = evaluateTargetOptionInCommand(sCmd, sTargettable, _tIdx, NumeReKernel::getInstance()->getParser(), _data, NumeReKernel::getInstance()->getSettings());
1782
1783 if (sTargettable.length())
1784 bWriteToCache = true;
1785 }
1786
1787 if (findParameter(sCmd, "tocache") || findParameter(sCmd, "totable"))
1788 {
1789 bWriteToCache = true;
1790
1791 if (!sTargettable.length())
1792 {
1793 sTargettable = "table";
1795 _tIdx.col = VectorIndex(_data.getCols("table"), VectorIndex::OPEN_END);
1796 }
1797 }
1798
1799 _histParams.sSavePath = getParameterValue(sCmd, "save", "export", "");
1800
1801 if (findParameter(sCmd, "save") || findParameter(sCmd, "export"))
1802 _out.setStatus(true);
1803
1804 if (_histParams.sSavePath.length())
1805 _out.setStatus(true);
1806
1807 if (!bMake2DHist)
1808 {
1809 _histParams.sBinLabel = getParameterValue(sCmd, "xlabel", "binlabel", "Bins");
1810 _histParams.sCountLabel = getParameterValue(sCmd, "ylabel", "countlabel", "Counts");
1811 }
1812 else
1813 {
1814 _histParams.sBinLabel = getParameterValue(sCmd, "binlabel", "binlabel", "Bins");
1815 _histParams.sCountLabel = getParameterValue(sCmd, "countlabel", "countlabel", "Counts");
1816 _histParams.sAxisLabels[0] = getParameterValue(sCmd, "xlabel", "xlabel", "\\i x");
1817 _histParams.sAxisLabels[1] = getParameterValue(sCmd, "ylabel", "ylabel", "\\i y");
1818 _histParams.sAxisLabels[2] = getParameterValue(sCmd, "zlabel", "zlabel", "\\i z");
1819 }
1820
1821 std::string sMethod = getParameterValue(sCmd, "method", "m", "");
1822
1823 if (sMethod == "scott")
1824 _histParams.nMethod = SCOTT;
1825 else if (sMethod == "freedman")
1826 _histParams.nMethod = FREEDMAN_DIACONIS;
1827 else
1828 _histParams.nMethod = STURGES;
1829
1830 if (findParameter(sCmd, "sum"))
1831 bSum = true;
1832
1833 getIntervalDef(sCmd, "x", _histParams.ranges.x[0], _histParams.ranges.x[1]);
1834 getIntervalDef(sCmd, "y", _histParams.ranges.y[0], _histParams.ranges.y[1]);
1835 getIntervalDef(sCmd, "z", _histParams.ranges.z[0], _histParams.ranges.z[1]);
1836
1837 if (findParameter(sCmd, "silent"))
1838 bSilent = true;
1839
1840 if (findParameter(sCmd, "grid") && _idx.col.size() > 3)
1841 bGrid = true;
1842
1844 if (bMake2DHist)
1845 createHist2D(sCmd, sTargettable, _idx, _tIdx, _histParams, bSum, bWriteToCache, bSilent);
1846 else
1847 createHist1D(sCmd, sTargettable, _idx, _tIdx, _histParams, bWriteToCache, bSilent, bGrid);
1848}
1849
1850
This class is defined to abstrahize the determination of the correct data object and the calculation ...
Definition: dataaccess.hpp:38
Indices & getIndices()
Returns a reference to the stored indices.
Definition: dataaccess.cpp:196
void evalIndices()
Evaluates open end indices using the identified data object size.
Definition: dataaccess.cpp:141
std::string & getDataObject()
Returns a reference to the data object identifier.
Definition: dataaccess.cpp:170
std::string ValidFileName(std::string _sFileName, const std::string sExtension=".dat", bool checkExtension=true, bool doCleanPath=true) const
This member function evaluates, whether the passed filename is a valid filename. One may supply a pre...
Definition: filesystem.cpp:280
This class encapsulates the mglGraph object during transmission from the kernel to the GUI.
void setAspect(double aspect)
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
Definition: language.cpp:292
This class represents the central memory managing instance. It will handle all tables and clusters,...
std::string matchTableAsParameter(const std::string &sExpression, char cFollowing=' ')
mu::value_type getElement(int _nLine, int _nCol, const std::string &_sTable) const
bool isValidElement(long long int _nLine, long long int _nCol, const std::string &_sTable) const
std::vector< mu::value_type > min(const std::string &sTable, std::string sDir) const
std::string getTopHeadLineElement(int _i, const std::string &_sTable) const
std::vector< mu::value_type > pct(const std::string &sTable, std::string sDir, mu::value_type dPct=0.5) const
int getLines(StringView sTable, bool _bFull=false) const
bool isEmpty(const std::string &sTable) const
bool isTable(const std::string &sTable) const
This member function returns, whether the passed table name corresponds to a known table.
std::string getHeadLineElement(int _i, const std::string &_sTable) const
std::vector< mu::value_type > max(const std::string &sTable, std::string sDir) const
bool isValid() const
Evaluates, whether there's at least a single non-empty table.
std::vector< mu::value_type > std(const std::string &sTable, std::string sDir) const
bool addTable(const std::string &sCache, const Settings &_option)
This member function creates a new table. It is checked, whether its name is valid and not already us...
void writeToTable(int _nLine, int _nCol, const std::string &_sCache, const mu::value_type &_dData)
bool setHeadLineElement(int _i, const std::string &_sTable, std::string _sHead)
int getCols(StringView sTable, bool _bFull=false) const
std::string getDataFileName(const std::string &sTable) const
This member function will return the file name of the selected table. Will default to the table name.
size_t createWindow(GraphHelper *graph)
This public member function will create a window object containing the passed graph.
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
Definition: kernel.hpp:221
PlotData & getPlottingData()
Definition: kernel.hpp:301
Output & getOutput()
Definition: kernel.hpp:306
NumeRe::WindowManager & getWindowManager()
Definition: kernel.hpp:331
static void printPreFmt(const std::string &__sLine, bool printingEnabled=true)
This member function appends the pre- formatted string to the buffer and informs the terminal that we...
Definition: kernel.cpp:2683
MemoryManager & getMemoryManager()
Definition: kernel.hpp:263
static void print(const std::string &__sLine, bool printingEnabled=true)
This member function appends the passed string as a new output line to the buffer and informs the ter...
Definition: kernel.cpp:2636
static void toggleTableStatus()
Toggles the table writing status, which will reduce the number or events send to the terminal.
Definition: kernel.cpp:3671
Settings & getSettings()
Definition: kernel.hpp:296
void format(std::string **_sMatrix, long long int _nCol, long long int _nLine, const Settings &_option, bool bDontAsk=false, int nHeadLineCount=1)
Definition: output.cpp:403
std::string getFileName() const
Definition: output.cpp:184
void setPrefix(std::string _sPrefix)
Definition: output.cpp:897
void setPluginName(std::string _sPluginName)
Definition: output.cpp:204
bool isFile() const
Definition: output.cpp:147
void setStatus(bool bStatus)
Definition: output.cpp:65
void generateFileName()
Definition: output.cpp:336
void reset()
Definition: output.cpp:75
void setCompact(bool _bCompact)
Definition: output.cpp:141
void setCommentLine(std::string _sCommentLine)
Definition: output.cpp:211
void setFileName(std::string sFile)
Definition: output.cpp:153
This class contains all the plot settings usable by the plotting algorithm.
Definition: plotdata.hpp:42
std::string getGridStyle() const
Definition: plotdata.hpp:343
std::string getColors() const
Definition: plotdata.hpp:322
@ INT_LEGENDPOSITION
Definition: plotdata.hpp:105
@ INT_AXIS
Definition: plotdata.hpp:99
@ INT_HIGHRESLEVEL
Definition: plotdata.hpp:104
@ FLOAT_TEXTSIZE
Definition: plotdata.hpp:122
@ FLOAT_BARS
Definition: plotdata.hpp:119
TimeAxis getTimeAxis(unsigned int i=0) const
Definition: plotdata.hpp:393
bool getSettings(LogicalPlotSetting setting) const
Definition: plotdata.hpp:210
std::string getColorScheme(const std::string &_sAddOpt="") const
Definition: plotdata.hpp:307
@ LOG_SILENTMODE
Definition: plotdata.hpp:90
@ LOG_OPENIMAGE
Definition: plotdata.hpp:84
std::string getFineGridStyle() const
Definition: plotdata.hpp:348
bool getLogscale(size_t i) const
Definition: plotdata.hpp:273
This class manages the setting values of the internal (kernel) settings of this application.
Definition: settings.hpp:663
bool createCompactTables() const
Returns, whether tables shall be displayed in a more compact way (does nothing in the external table ...
Definition: settings.hpp:913
bool systemPrints() const
Returns, whether system messages shall be printed to the terminal.
Definition: settings.hpp:1140
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ INVALID_INTERVAL
Definition: error.hpp:132
@ NO_DATA_AVAILABLE
Definition: error.hpp:160
@ TABLE_DOESNT_EXIST
INSERT HERE.
Definition: error.hpp:211
@ NO_CACHED_DATA
Definition: error.hpp:158
@ TOO_LARGE_BINWIDTH
Definition: error.hpp:216
@ TOO_FEW_COLS
Definition: error.hpp:213
@ WRONG_PLOT_INTERVAL_FOR_LOGSCALE
Definition: error.hpp:228
static size_t invalid_position
Definition: error.hpp:235
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
Definition: structures.hpp:42
VectorIndex subidx(size_t pos, size_t nLen=std::string::npos) const
This member function returns a subset of the internal stored index just like the std::string::substr(...
Definition: structures.hpp:238
int last() const
This member function returns the last index value, which can be reached by the values stored internal...
Definition: structures.hpp:693
size_t size() const
This member function returns the size of the indices stored in this class.
Definition: structures.hpp:314
int & front()
This member function returns a reference to the first index value stored internally.
Definition: structures.hpp:640
bool isValidIndexSet(const Indices &_idx)
Definition: dataaccess.hpp:81
Language _lang
Definition: kernel.cpp:39
std::string toSystemCodePage(std::string)
Converts an internal to an external string. Does nothing currently.
if(nReturnType==EIGENVALUES)
Definition: matfuncs.hpp:390
CONSTCD14 std::enable_if< detail::no_overflow< Period, typenameTo::period >::value, To >::type floor(const std::chrono::duration< Rep, Period > &d)
Definition: date.h:1251
CONSTCD11 std::chrono::duration< Rep, Period > abs(std::chrono::duration< Rep, Period > d)
Definition: date.h:1317
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
bool isnan(const value_type &v)
Definition: muParserDef.h:379
std::vector< double > real(const std::vector< value_type > &vVec)
value_type rint(value_type v)
string evaluateTargetOptionInCommand(string &sCmd, const string &sDefaultTarget, Indices &_idx, Parser &_parser, MemoryManager &_data, const Settings &_option)
This function evaluates the "target=TABLE()" expression and creates the target table,...
@ AXIS_NONE
Definition: plotdef.hpp:72
@ XRANGE
Definition: plotdef.hpp:50
@ YRANGE
Definition: plotdef.hpp:51
@ ZRANGE
Definition: plotdef.hpp:52
static void calculateDataForCenterPlot(MemoryManager &_data, const Indices &_idx, const HistogramParameters &_histParams, mglData _hist2DData[3])
This static function calculates the data for the center plot part of the 2D histogram.
static void createOutputForHist1D(MemoryManager &_data, const Indices &_idx, const std::vector< std::vector< double > > &vHistMatrix, const HistogramParameters &_histParams, const mglData &_mAxisVals, bool bGrid, bool bFormat, bool bSilent)
This static function creates the terminal and file output for a 1D histogram.
static void createHist1D(const std::string &sCmd, const std::string &sTargettable, Indices &_idx, Indices &_tIdx, HistogramParameters &_histParams, bool bWriteToCache, bool bSilent, bool bGrid)
This static function is the driver code for creating a 1D histogram.
static std::string prepareTicksForHist1d(const HistogramParameters &_histParams, const mglData &_mAxisVals, std::string &sCommonExponent, bool bGrid)
This static function calculates the custom ticks (one for every bin) and formats them accordingly.
void plugin_histogram(std::string &sCmd)
This function is the interface to both the 1D and the 2D histogram generation.
static void createPlotsForHist2D(const std::string &sCmd, HistogramParameters &_histParams, mglData _mAxisVals[2], mglData &_barHistData, mglData &_hBarHistData, mglData _hist2DData[3], bool isScatterPlot, bool bSum, bool bSilent)
This static function creates the three plots for the 2D histogram.
mglGraph _fontData
Definition: kernel.cpp:40
HistBinMethod
This enumeration defines the available bin determination methods for 1D and 2D histograms.
@ SCOTT
@ FREEDMAN_DIACONIS
@ STURGES
static void getIntervalDef(const std::string &sCmd, const std::string &sIdentifier, double &dMin, double &dMax)
This static function decodes a selected range definition (e.g. x=0:1) into doubles.
static void createHist2D(const std::string &sCmd, const std::string &sTargettable, Indices &_idx, Indices &_tIdx, HistogramParameters &_histParams, bool bSum, bool bWriteToCache, bool bSilent)
This static function is the driver function for creating a 2D histogram.
static mglData calculateXYHist(MemoryManager &_data, const Indices &_idx, const HistogramParameters &_histParams, mglData *_mAxisVals, double dBinMin, double dMin, double dMax, double dIntLength, int nMax, bool isLogScale, bool isHbar, bool bSum)
This static function calculates the data for the both bar plot on top and at right of the center plot...
static void createOutputForHist2D(MemoryManager &_data, const Indices &_idx, const std::string &sTargettable, const Indices &_tIdx, const HistogramParameters &_histParams, mglData _mAxisVals[2], mglData &_barHistData, mglData &_hBarHistData, bool bSum, bool bWriteToCache, bool shallFormat, bool isSilent)
This static function creates the textual output for terminal or file and writes the data also to a ta...
static mglGraph * prepareGraphForHist(double dAspect, PlotData &_pData, bool bSilent)
This static function prepares a mglGraph instance for histogram plotting. It's usable in 1D and 2D ca...
static void prepareIntervalsForHist(const std::string &sCmd, double &dMin, double &dMax, double dDataMin, double dDataMax)
This static function replaces invalid ranges boundaries with the passed minimal and maximal data valu...
static std::string getParameterValue(const std::string &sCmd, const std::string &sVersion1, const std::string &sVersion2, const std::string &sDefaultVal)
This static function returns the value of the selected command line option (passable in two represent...
const std::string PI_HIST
static void createPlotForHist1D(HistogramParameters &_histParams, mglData &_mAxisVals, mglData &_histData, const std::vector< std::string > &vLegends, int nMax, bool bSilent, bool bGrid)
This static function creates the plot for a 1D histogram.
static std::vector< std::vector< double > > calculateHist1dData(MemoryManager &_data, const Indices &_idx, const HistogramParameters &_histParams, mglData &_histData, mglData &_mAxisVals, int &nMax, std::vector< std::string > &vLegends, bool bGrid, bool isXLog)
This static function calculates the data for a 1D histogram. The data is returned as a vector<vector<...
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
int findParameter(const std::string &sCmd, const std::string &sParam, const char cFollowing)
This function searches the passed parameter in the passed command string. If something is found,...
Definition: tools.cpp:113
double StrToDb(const std::string &sString)
Converts a string into a double.
std::string toUpperCase(const std::string &sLowerCase)
Converts lowercase letters to uppercase ones.
std::string condenseText(const std::string &sText)
Removes vowels and umlauts from the passed string.
This structure gathers all necessary parameters for the histograms.
std::string sAxisLabels[3]
This structure is central for managing the indices of a table or cluster read or write data access....
VectorIndex col
VectorIndex row
This structure defines the available ranges for the histograms.
double y[2]
double z[2]
double x[2]
bool use
Definition: interval.hpp:134
std::string sTimeFormat
Definition: interval.hpp:133
long long int intCast(const std::complex< double > &)
Casts the real part of the complex number to an integer and avoids rounding errors.
Definition: tools.cpp:1824
std::string toString(int)
Converts an integer to a string without the Settings bloat.
Match findCommand(StringView sCmd, const std::string &sCommand)
This function is very important for the command handler.
Definition: tools.cpp:1275
string getArgAtPos(const string &sCmd, unsigned int nPos, int extraction)
Extracts a options value at the selected position and applies automatic parsing, if necessary.
Definition: tools.cpp:1598
std::string replaceToTeX(const std::string &sString, bool replaceForTeXFile)
Replace the special tokens with their TeX counterparts, so that they can be handled by a LaTeX interp...
Definition: tools.cpp:753
std::string LineBreak(std::string sOutput, const Settings &_option, bool bAllowDashBreaks, int nFirstIndent, int nIndent)
This function takes a string, splits it into multiple lines if it is too long and returns the result.
Definition: tools.cpp:2205
void make_hline(int nLength=-1)
This function prints a horizontal line to the terminal using either minus or equal signs.
Definition: kernel.cpp:3720