StatisticsDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/vp/StatisticsDialog.cpp
22 
23  \brief A dialog for statistics operation
24 */
25 
26 // TerraLib
27 #include "../../common/Exception.h"
28 #include "../../dataaccess/dataset/DataSet.h"
29 #include "../../dataaccess/utils/Utils.h"
30 #include "../../datatype/Enums.h"
31 #include "../../datatype/Property.h"
32 #include "../core/Config.h"
33 #include "../core/NumericStatisticalSummary.h"
34 #include "../core/StringStatisticalSummary.h"
35 #include "../core/SummaryFunctions.h"
36 #include "../core/Utils.h"
37 #include "StatisticsDialog.h"
38 #include "ui_StatisticsDialogForm.h"
39 
40 
41 // Qt
42 #include <QListWidget>
43 #include <QMessageBox>
44 
45 // Boost
46 #include <boost/lexical_cast.hpp>
47 
49  :QDialog(parent, f),
50  m_ui(new Ui::StatisticsDialogForm),
51  m_dset(nullptr)
52 {
53  m_ui->setupUi(this);
54 
55  connect(m_ui->m_savePushButton, SIGNAL(clicked()), this, SLOT(onSavePushButtonClicked()));
56  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
57 
58 }
59 
61 
62 void te::stat::StatisticsDialog::setStatistics(te::da::DataSet* dataSet, const std::string prop)
63 {
64  QApplication::setOverrideCursor(Qt::WaitCursor);
65 
66  m_dset = dataSet;
67  m_prop = prop;
69  int propType = m_dset->getPropertyDataType(index);
70 
71  m_ui->m_datasourceTypeTitleLabel->setText("Statistics: " + QString(prop.c_str()));
72 
73  if(propType == te::dt::STRING_TYPE)
74  {
75  std::vector<std::string> values = te::stat::GetStringData(m_dset, m_prop);
76 
78 
79  if(!values.empty())
80  {
82  m_ui->m_statTableWidget->setRowCount(0);
83 
84 // Minimum value
85  m_ui->m_statTableWidget->insertRow(0);
86  QTableWidgetItem* itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()));
87  QTableWidgetItem* itemValue = new QTableWidgetItem(QString(ss.m_minVal.c_str()));
88  m_ui->m_statTableWidget->setItem(0, 0, itemParameter);
89  m_ui->m_statTableWidget->setItem(0, 1, itemValue);
90 
91 // Maximum value
92  m_ui->m_statTableWidget->insertRow(1);
93  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()));
94  itemValue = new QTableWidgetItem(QString(ss.m_maxVal.c_str()));
95  m_ui->m_statTableWidget->setItem(1, 0, itemParameter);
96  m_ui->m_statTableWidget->setItem(1, 1, itemValue);
97 
98 // Total number of values
99  m_ui->m_statTableWidget->insertRow(2);
100  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::COUNT).c_str()));
101  itemValue = new QTableWidgetItem(QString(QString(boost::lexical_cast<std::string>(m_dset->size()).c_str())));
102  m_ui->m_statTableWidget->setItem(2, 0, itemParameter);
103  m_ui->m_statTableWidget->setItem(2, 1, itemValue);
104 
105 // Total not null values
106  m_ui->m_statTableWidget->insertRow(3);
107  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT).c_str()));
108  itemValue = new QTableWidgetItem(QString(QString(boost::lexical_cast<std::string>(values.size()).c_str())));
109  m_ui->m_statTableWidget->setItem(3, 0, itemParameter);
110  m_ui->m_statTableWidget->setItem(3, 1, itemValue);
111  }
112  else
113  {
114  QApplication::setOverrideCursor(Qt::ArrowCursor);
115  throw te::common::Exception(tr("The selected column is empty.").toUtf8().data());
116  }
117  }
118  else
119  {
120  std::vector<double> values = te::stat::GetNumericData(m_dset, m_prop);
121 
123 
124  if(!values.empty())
125  {
127 
128 // Minimum value
129  m_ui->m_statTableWidget->insertRow(0);
130  QTableWidgetItem* itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()));
131  QTableWidgetItem* itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_minVal).c_str()));
132  m_ui->m_statTableWidget->setItem(0, 0, itemParameter);
133  m_ui->m_statTableWidget->setItem(0, 1, itemValue);
134 
135 // Maximum value
136  m_ui->m_statTableWidget->insertRow(1);
137  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()));
138  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_maxVal).c_str()));
139  m_ui->m_statTableWidget->setItem(1, 0, itemParameter);
140  m_ui->m_statTableWidget->setItem(1, 1, itemValue);
141 
142 // Total number of values
143  m_ui->m_statTableWidget->insertRow(2);
144  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::COUNT).c_str()));
145  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(m_dset->size()).c_str()));
146  m_ui->m_statTableWidget->setItem(2, 0, itemParameter);
147  m_ui->m_statTableWidget->setItem(2, 1, itemValue);
148 
149 // Total not null values
150  m_ui->m_statTableWidget->insertRow(3);
151  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT).c_str()));
152  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(values.size()).c_str()));
153  m_ui->m_statTableWidget->setItem(3, 0, itemParameter);
154  m_ui->m_statTableWidget->setItem(3, 1, itemValue);
155 
156 // Mean of values
157  m_ui->m_statTableWidget->insertRow(4);
158  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEAN).c_str()));
159  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_mean).c_str()));
160  m_ui->m_statTableWidget->setItem(4, 0, itemParameter);
161  m_ui->m_statTableWidget->setItem(4, 1, itemValue);
162 
163 // Sum of values
164  m_ui->m_statTableWidget->insertRow(5);
165  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::SUM).c_str()));
166  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_sum).c_str()));
167  m_ui->m_statTableWidget->setItem(5, 0, itemParameter);
168  m_ui->m_statTableWidget->setItem(5, 1, itemValue);
169 
170 // Standard deviation
171  m_ui->m_statTableWidget->insertRow(6);
172  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION).c_str()));
173  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_stdDeviation).c_str()));
174  m_ui->m_statTableWidget->setItem(6, 0, itemParameter);
175  m_ui->m_statTableWidget->setItem(6, 1, itemValue);
176 
177 // Variance
178  m_ui->m_statTableWidget->insertRow(7);
179  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::VARIANCE).c_str()));
180  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_variance).c_str()));
181  m_ui->m_statTableWidget->setItem(7, 0, itemParameter);
182  m_ui->m_statTableWidget->setItem(7, 1, itemValue);
183 
184 // Skewness
185  m_ui->m_statTableWidget->insertRow(8);
186  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::SKEWNESS).c_str()));
187  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_skewness).c_str()));
188  m_ui->m_statTableWidget->setItem(8, 0, itemParameter);
189  m_ui->m_statTableWidget->setItem(8, 1, itemValue);
190 
191 // Kurtosis
192  m_ui->m_statTableWidget->insertRow(9);
193  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::KURTOSIS).c_str()));
194  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_kurtosis).c_str()));
195  m_ui->m_statTableWidget->setItem(9, 0, itemParameter);
196  m_ui->m_statTableWidget->setItem(9, 1, itemValue);
197 
198 // Amplitude
199  m_ui->m_statTableWidget->insertRow(10);
200  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE).c_str()));
201  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_amplitude).c_str()));
202  m_ui->m_statTableWidget->setItem(10, 0, itemParameter);
203  m_ui->m_statTableWidget->setItem(10, 1, itemValue);
204 
205 // Median
206  m_ui->m_statTableWidget->insertRow(11);
207  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEDIAN).c_str()));
208  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_median).c_str()));
209  m_ui->m_statTableWidget->setItem(11, 0, itemParameter);
210  m_ui->m_statTableWidget->setItem(11, 1, itemValue);
211 
212 // Coefficient variation.
213  m_ui->m_statTableWidget->insertRow(12);
214  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF).c_str()));
215  itemValue = new QTableWidgetItem(QString(boost::lexical_cast<std::string>(ss.m_varCoeff).c_str()));
216  m_ui->m_statTableWidget->setItem(12, 0, itemParameter);
217  m_ui->m_statTableWidget->setItem(12, 1, itemValue);
218 
219 // Mode.
220  te::stat::Mode(values, ss);
221 
222  m_ui->m_statTableWidget->insertRow(13);
223  itemParameter = new QTableWidgetItem(QString(te::stat::GetStatSummaryFullName(te::stat::MODE).c_str()));
224  if(ss.m_mode.size() == 0)
225  itemValue = new QTableWidgetItem(QString(""));
226  else
227  {
228  std::string value;
229  for(std::size_t i = 0; i < ss.m_mode.size(); ++i)
230  value += ", "+ boost::lexical_cast<std::string>(ss.m_mode[i]);
231 
232  value.erase(0,2);
233  itemValue = new QTableWidgetItem(QString(value.c_str()));
234  }
235  m_ui->m_statTableWidget->setItem(13, 0, itemParameter);
236  m_ui->m_statTableWidget->setItem(13, 1, itemValue);
237 
238  }
239  else
240  {
241  QApplication::setOverrideCursor(Qt::ArrowCursor);
242  throw te::common::Exception(tr("The selected column is empty.").toUtf8().data());
243  }
244  }
245 
246  QApplication::setOverrideCursor(Qt::ArrowCursor);
247  m_ui->m_statTableWidget->resizeColumnToContents(0);
248 }
249 
251 {
252  throw te::common::Exception(tr("Under development - It should save the result...").toUtf8().data());
253 }
254 
256 {
257  reject();
258 }
A structure to hold the set of statistics from a set of numerical values.
A dialog statistics.
Total number of values.
void setStatistics(te::da::DataSet *dataSet, const std::string prop)
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
TESTATEXPORT std::vector< double > GetNumericData(te::da::DataSet *dataSet, const std::string propName)
Returns the values of a numeric type property in a vector of values.
virtual std::size_t size() const =0
It returns the collection size, if it is known.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
TESTATEXPORT std::vector< std::string > GetStringData(te::da::DataSet *dataSet, const std::string propName)
Returns the values of a string type property in a vector of values.
std::unique_ptr< Ui::StatisticsDialogForm > m_ui
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullValue)
TESTATEXPORT std::string GetStatSummaryFullName(const int &e)
Get the statistical parameter full name ffrom its enumerator.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
TESTATEXPORT void GetStringStatisticalSummary(std::vector< std::string > &values, te::stat::StringStatisticalSummary &ss, const std::string &nullValue)
A dataset is the unit of information manipulated by the data access module of TerraLib.
A structure to hold the set of statistics from a set of categorical (sample) values.
StatisticsDialog(QWidget *parent=0, Qt::WindowFlags f=0)
TESTATEXPORT void Mode(const std::vector< double > &values, te::stat::NumericStatisticalSummary &ss)