Vespucci  1.0.0
statsdialog.cpp
Go to the documentation of this file.
2 #include "ui_statsdialog.h"
4 #include <Math/Stats/histogram.h>
5 #include "Global/global.h"
6 
7 StatsDialog::StatsDialog(MainWindow *parent, QSharedPointer<VespucciWorkspace> ws) :
8  QDialog(parent),
9  ui(new Ui::StatsDialog)
10 {
11  workspace_ = ws;
12  ui->setupUi(this);
13 }
14 
16 {
17  delete ui;
18 }
19 
20 void StatsDialog::showEvent(QShowEvent *ev)
21 {
22  QDialog::showEvent(ev);
23  UpdateDisplayData();
24 }
25 
26 void StatsDialog::closeEvent(QCloseEvent *ev)
27 {
28  QDialog::closeEvent(ev);
29  emit SetActionChecked(false);
30 }
31 
32 void StatsDialog::MatrixSelectionChanged(QStringList matrix_keys)
33 {
34  data_keys_ = matrix_keys;
35  if (isVisible()) UpdateDisplayData();
36 }
37 
38 void StatsDialog::MatrixToBeRemoved(QStringList matrix_keys)
39 {
40  if (Vespucci::KeysAreEqual(data_keys_, matrix_keys)){
41  data_keys_ = QStringList();
42  ClearFields();
43  }
44 
45 }
46 
48 {
49  if (!data_keys_.size()) return;
50  if (data_keys_.first() == name){
51  data_keys_ = QStringList();
52  ClearFields();
53  }
54 }
55 
56 double StatsDialog::CalculateMedian()
57 {
58  const mat& data = workspace_->GetMatrix(data_keys_);
59  if (!data.n_elem) return 0;
60  if (data.n_cols == 1) return as_scalar(median(data));
61  if (data.n_cols > 1){
62  mat data_copy = data;
63  data_copy.reshape(data.n_elem, 1);
64  return as_scalar(median(data_copy));
65  }
66  return 0;
67 
68 }
69 
70 double StatsDialog::CalculateStdDev()
71 {
72  const mat& data = workspace_->GetMatrix(data_keys_);
73  if (!data.n_elem) return 0;
74  if (data.n_cols == 1) return as_scalar(stddev(data));
75  if (data.n_cols > 1){
76  mat data_copy = data;
77  data_copy.reshape(data.n_elem, 1);
78  return as_scalar(stddev(data_copy));
79  }
80  return 0;
81 }
82 
83 double StatsDialog::CalculateMean()
84 {
85  const mat& data = workspace_->GetMatrix(data_keys_);
86  if (!data.n_elem) return 0;
87  if (data.n_cols == 1) return as_scalar(mean(data));
88  if (data.n_cols > 1){
89  mat data_copy = data;
90  data_copy.reshape(data.n_elem, 1);
91  return as_scalar(mean(data_copy));
92  }
93  return 0;
94 }
95 
96 void StatsDialog::GenerateHistogram()
97 {
98  ui->histogramCustomPlot->clearPlottables();
99  const mat& data = workspace_->GetMatrix(data_keys_);
100  if (!data.n_elem) return;
101  vec edges;
102  uvec hist = Vespucci::Math::Stats::GenerateHistogram(data, edges);
103  edges.shed_row(edges.n_rows - 1);
104 
105  qvec histq = qvec::fromStdVector(conv_to<stdvec>::from(hist));
106  qvec edgesq = qvec::fromStdVector(conv_to<stdvec>::from(edges));
107 
108  QCPBars *hist_plot = new QCPBars(ui->histogramCustomPlot->xAxis,
109  ui->histogramCustomPlot->yAxis);
110  hist_plot->addData(edgesq, histq);
111  hist_plot->setWidth(edges(1) - edges(0));
112  edgesq.append(data.max()); //add maximum back in for plotting purposes
113  QVector<QString> labels;
114  for (auto value: edgesq) labels << QString::number(value, 'g', 3);
115  ui->histogramCustomPlot->addPlottable(hist_plot);
116  ui->histogramCustomPlot->xAxis->setTickVector(edgesq);
117  ui->histogramCustomPlot->xAxis->setTickVectorLabels(labels);
118  ui->histogramCustomPlot->rescaleAxes();
119  ui->histogramCustomPlot->replot();
120 }
121 
122 void StatsDialog::UpdateDisplayData()
123 {
124  if (data_keys_.size() < 2) ClearFields();
125  GenerateHistogram();
126  CalculateCI();
127  const mat& data = workspace_->GetMatrix(data_keys_);
128  if (!data.n_elem){
129  ClearFields();
130  return;
131  }
132  ui->minLineEdit->setText(QString::number(data.min()));
133  ui->maxLineEdit->setText(QString::number(data.max()));
134  ui->medLineEdit->setText(QString::number(CalculateMedian()));
135  ui->stddevLineEdit->setText(QString::number(CalculateStdDev()));
136  ui->meanLineEdit->setText(QString::number(CalculateMean()));
137  ui->nameLabel->setText(data_keys_.last());
138  QString dimensions = QString::number(data.n_rows) + "×" + QString::number(data.n_cols);
139  ui->dimensionLabel->setText(dimensions);
140  ui->plottableLabel->setText(workspace_->Plottable(data_keys_) ? "True" : "False");
141  ui->mappableLabel->setText(workspace_->Mappable(data_keys_) ? "True" : "False");
142 }
143 
144 void StatsDialog::CalculateCI()
145 {
146  const mat& data = workspace_->GetMatrix(data_keys_);
147  if (!data.n_elem) return;
148  double alpha = ui->alphaDoubleSpinBox->value();
149  double stddev = CalculateStdDev();
150  unsigned int n = data.n_elem;
151  double w = Vespucci::Math::Stats::TInterval(alpha, stddev, n);
152  ui->confidenceLineEdit->setText(QString::number(w));
153 }
154 
155 
156 void StatsDialog::on_calculatePushButton_clicked()
157 {
158  CalculateCI();
159 }
160 
161 void StatsDialog::ClearFields()
162 {
163  ui->minLineEdit->setText("NA");
164  ui->maxLineEdit->setText("NA");
165  ui->medLineEdit->setText("NA");
166  ui->stddevLineEdit->setText("NA");
167  ui->meanLineEdit->setText("NA");
168  ui->nameLabel->setText("No matrix selected");
169  ui->dimensionLabel->setText("NA");
170  ui->plottableLabel->setText("NA");
171  ui->mappableLabel->setText("NA");
172  ui->confidenceLineEdit->setText("NA");
173  ui->histogramCustomPlot->clearPlottables();
174  ui->histogramCustomPlot->replot();
175 }
VESPUCCI_EXPORT arma::uvec GenerateHistogram(const arma::mat &data, arma::vec &edges, uint bins=0)
Definition: histogram.cpp:21
Definition: ahcadialog.h:26
void closeEvent(QCloseEvent *ev)
Definition: statsdialog.cpp:26
A plottable representing a bar chart in a plot.
Definition: qcustomplot.h:2873
StatsDialog(MainWindow *parent, QSharedPointer< VespucciWorkspace > ws)
Definition: statsdialog.cpp:7
void setWidth(double width)
VESPUCCI_EXPORT double TInterval(double alpha, double stddev, unsigned int n)
Vespucci::Math::Stats::TInterval.
QVector< double > qvec
Definition: mapplot.h:28
void MatrixToBeRemoved(QStringList matrix_keys)
Definition: statsdialog.cpp:38
void showEvent(QShowEvent *ev)
Definition: statsdialog.cpp:20
void MatrixSelectionChanged(QStringList matrix_keys)
Definition: statsdialog.cpp:32
cmplx FADDEEVA() w(cmplx z, double relerr)
Definition: Faddeeva.cpp:680
bool KeysAreEqual(QStringList &keys1, QStringList &keys2)
Definition: global.cpp:168
void addData(const QCPBarDataMap &dataMap)
void SetActionChecked(bool checked)
The MainWindow class The main window of the program, this is where the user performs most operations...
Definition: mainwindow.h:58
void DatasetToBeRemoved(QString name)
Definition: statsdialog.cpp:47