diff --git a/src/backend/generalTest/CorrelationCoefficient.cpp b/src/backend/generalTest/CorrelationCoefficient.cpp index fb9143bf1..dfe13ae4d 100644 --- a/src/backend/generalTest/CorrelationCoefficient.cpp +++ b/src/backend/generalTest/CorrelationCoefficient.cpp @@ -1,197 +1,327 @@ /*************************************************************************** File : CorrelationCoefficient.cpp Project : LabPlot Description : Finding Correlation Coefficient on data provided -------------------------------------------------------------------- Copyright : (C) 2019 Devanshu Agarwal(agarwaldevanshu8@gmail.com) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "CorrelationCoefficient.h" #include "GeneralTest.h" #include "kdefrontend/generalTest/CorrelationCoefficientView.h" #include "backend/spreadsheet/Spreadsheet.h" #include "backend/core/column/Column.h" #include "backend/lib/macros.h" #include #include #include #include #include #include #include #include #include #include #include +#include extern "C" { #include "backend/nsl/nsl_stats.h" } CorrelationCoefficient::CorrelationCoefficient(const QString &name) : GeneralTest (name, AspectType::CorrelationCoefficient) { } CorrelationCoefficient::~CorrelationCoefficient() { } void CorrelationCoefficient::performTest(Test test, bool categoricalVariable) { m_statsTable = ""; m_tooltips.clear(); - for (int i = 0; i < 10; i++) + for (int i = 0; i < RESULTLINESCOUNT; i++) m_resultLine[i]->clear(); switch (test) { case CorrelationCoefficient::Test::Pearson: { m_currTestName = "

" + i18n("Pearson's r Correlation Test") + "

"; performPearson(categoricalVariable); break; } case CorrelationCoefficient::Test::Kendall: - m_currTestName = "

" + i18n("Kendall's Correlation Test") + "

"; + m_currTestName = "

" + i18n("Kendall's Rank Correlation Test") + "

"; performKendall(); break; case CorrelationCoefficient::Test::Spearman: { m_currTestName = "

" + i18n("Spearman Correlation Test") + "

"; performSpearman(); break; } } emit changed(); } double CorrelationCoefficient::correlationValue() { return m_correlationValue; } /*************************************************************************************************************************** * Private Implementations * ************************************************************************************************************************/ /*********************************************Pearson r ******************************************************************/ +//Formulaes are taken from https://www.statisticssolutions.com/correlation-pearson-kendall-spearman/ + // variables: // N = total number of observations // sumColx = sum of values in colx // sumSqColx = sum of square of values in colx // sumColxColy = sum of product of values in colx and coly //TODO: support for col1 is categorical. //TODO: add symbols in stats table header. +//TODO: add automatic test +//TODO: add tooltip for correlation value result +//TODO: find p value void CorrelationCoefficient::performPearson(bool categoricalVariable) { if (m_columns.count() != 2) { printError("Select only 2 columns "); return; } if (categoricalVariable) { printLine(1, "currently categorical variable not supported", "blue"); return; } QString col1Name = m_columns[0]->name(); QString col2Name = m_columns[1]->name(); if (!isNumericOrInteger(m_columns[1])) { printError("Column " + col2Name + " should contain only numeric or interger values"); } int N = findCount(m_columns[0]); if (N != findCount(m_columns[1])) { printError("Number of data values in Column: " + col1Name + "and Column: " + col2Name + "are not equal"); return; } double sumCol1 = findSum(m_columns[0], N); double sumCol2 = findSum(m_columns[1], N); double sumSqCol1 = findSumSq(m_columns[0], N); double sumSqCol2 = findSumSq(m_columns[1], N); double sumCol12 = 0; for (int i = 0; i < N; i++) sumCol12 += m_columns[0]->valueAt(i) * m_columns[1]->valueAt(i); // printing table; // cell constructor structure; data, level, rowSpanCount, m_columnspanCount, isHeader; QList rowMajor; int level = 0; // horizontal header rowMajor.append(new Cell("", level, true)); rowMajor.append(new Cell("N", level, true, "Total Number of Observations")); rowMajor.append(new Cell("Sigma", level, true, "Sum of Scores in each column")); rowMajor.append(new Cell("Sigma x2", level, true, "Sum of Squares of scores in each column")); rowMajor.append(new Cell("Sigma xy", level, true, "Sum of Squares of scores in each column")); //data with vertical header. level++; rowMajor.append(new Cell(col1Name, level, true)); rowMajor.append(new Cell(N, level)); rowMajor.append(new Cell(sumCol1, level)); rowMajor.append(new Cell(sumSqCol1, level)); rowMajor.append(new Cell(sumCol12, level, false, "", 2, 1)); level++; rowMajor.append(new Cell(col2Name, level, true)); rowMajor.append(new Cell(N, level)); rowMajor.append(new Cell(sumCol2, level)); rowMajor.append(new Cell(sumSqCol2, level)); m_statsTable += getHtmlTable3(rowMajor); m_correlationValue = (N * sumCol12 - sumCol1*sumCol2) / sqrt((N * sumSqCol1 - gsl_pow_2(sumCol1)) * (N * sumSqCol2 - gsl_pow_2(sumCol2))); - printLine(0, QString("Correlation Value is %1").arg(m_correlationValue), "green"); + printLine(0, QString("Correlation Value is %1").arg(round(m_correlationValue)), "green"); } /***********************************************Kendall ******************************************************************/ +// used knight algorithm for fast performance O(nlogn) rather than O(n2) +// http://adereth.github.io/blog/2013/10/30/efficiently-computing-kendalls-tau/ + +// TODO: Change date format type to original for numeric type; +// TODO: add tooltips. +// TODO: Compute tauB for ties. +// TODO: find P Value from Z Value void CorrelationCoefficient::performKendall() { + if (m_columns.count() != 2) { + printError("Select only 2 columns "); + return; + } + + QString col1Name = m_columns[0]->name(); + QString col2Name = m_columns[1]->name(); + + int N = findCount(m_columns[0]); + if (N != findCount(m_columns[1])) { + printError("Number of data values in Column: " + col1Name + "and Column: " + col2Name + "are not equal"); + return; + } + + int col2Ranks[N]; + if (isNumericOrInteger(m_columns[0]) || isNumericOrInteger(m_columns[1])) { + if (isNumericOrInteger(m_columns[0]) && isNumericOrInteger(m_columns[1])) { + for (int i = 0; i < N; i++) + col2Ranks[int(m_columns[0]->valueAt(i)) - 1] = int(m_columns[1]->valueAt(i)); + } else { + printError(QString("Ranking System should be same for both Column: %1 and Column: %2
" + "Hint: Check for data types of columns").arg(col1Name).arg(col2Name)); + return; + } + } else { + AbstractColumn::ColumnMode origCol1Mode = m_columns[0]->columnMode(); + AbstractColumn::ColumnMode origCol2Mode = m_columns[1]->columnMode(); + + m_columns[0]->setColumnMode(AbstractColumn::Text); + m_columns[1]->setColumnMode(AbstractColumn::Text); + + QMap ValueToRank; + + for (int i = 0; i < N; i++) { + if (ValueToRank[m_columns[0]->textAt(i)] != 0) { + printError("Currently ties are not supported"); + m_columns[0]->setColumnMode(origCol1Mode); + m_columns[1]->setColumnMode(origCol2Mode); + return; + } + ValueToRank[m_columns[0]->textAt(i)] = i + 1; + } + + for (int i = 0; i < N; i++) + col2Ranks[i] = ValueToRank[m_columns[1]->textAt(i)]; + + m_columns[0]->setColumnMode(origCol1Mode); + m_columns[1]->setColumnMode(origCol2Mode); + } + + int nPossiblePairs = (N * (N - 1)) / 2; + + int nDiscordant = findDiscordants(col2Ranks, 0, N - 1); + int nCorcordant = nPossiblePairs - nDiscordant; + + double tauA = double(nCorcordant - nDiscordant) / nPossiblePairs; + + double zA = (3 * (nCorcordant - nDiscordant)) / + sqrt(N * (N- 1) * (2 * N + 5) / 2); + + printLine(0 , QString("Number of Discordants are %1").arg(nDiscordant), "green"); + printLine(1 , QString("Number of Concordant are %1").arg(nCorcordant), "green"); + + printLine(2 , QString("Tau a is %1").arg(round(tauA)), "green"); + printLine(3 , QString("Z Value is %1").arg(round(zA)), "green"); + + m_correlationValue = tauA; + return; } /***********************************************Spearman ******************************************************************/ void CorrelationCoefficient::performSpearman() { } -// Virtual functions +/***********************************************Helper Functions******************************************************************/ + +int CorrelationCoefficient::findDiscordants(int *ranks, int start, int end) { + if (start >= end) + return 0; + + int mid = (start + end) / 2; + + int leftDiscordants = findDiscordants(ranks, start, mid); + int rightDiscordants = findDiscordants(ranks, mid + 1, end); + + int len = end - start + 1; + int leftLen = mid - start + 1; + int rightLen = end - mid; + int leftLenRemain = leftLen; + + int leftRanks[leftLen]; + int rightRanks[rightLen]; + + for (int i = 0; i < leftLen; i++) + leftRanks[i] = ranks[start + i]; + + for (int i = leftLen; i < leftLen + rightLen; i++) + rightRanks[i - leftLen] = ranks[start + i]; + + int mergeDiscordants = 0; + int i = 0, j = 0, k =0; + while (i < len) { + if (j >= leftLen) { + ranks[start + i] = rightRanks[k]; + k++; + } else if (k >= rightLen) { + ranks[start + i] = leftRanks[j]; + j++; + } else if (leftRanks[j] < rightRanks[k]) { + ranks[start + i] = leftRanks[j]; + j++; + leftLenRemain--; + } else if (leftRanks[j] > rightRanks[k]) { + ranks[start + i] = rightRanks[k]; + mergeDiscordants += leftLenRemain; + k++; + } + i++; + } + return leftDiscordants + rightDiscordants + mergeDiscordants; +} + +/***********************************************Virtual Functions******************************************************************/ + QWidget* CorrelationCoefficient::view() const { if (!m_partView) { m_view = new CorrelationCoefficientView(const_cast(this)); m_partView = m_view; } return m_partView; } diff --git a/src/backend/generalTest/CorrelationCoefficient.h b/src/backend/generalTest/CorrelationCoefficient.h index 1b75acaa7..aa86f97e6 100644 --- a/src/backend/generalTest/CorrelationCoefficient.h +++ b/src/backend/generalTest/CorrelationCoefficient.h @@ -1,67 +1,69 @@ /*************************************************************************** File : CorrelationCoefficient.h Project : LabPlot Description : Finding Correlation Coefficient on data provided -------------------------------------------------------------------- Copyright : (C) 2019 Devanshu Agarwal(agarwaldevanshu8@gmail.com) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef CORRELATIONCOEFFICIENT_H #define CORRELATIONCOEFFICIENT_H #include "backend/core/AbstractPart.h" #include "GeneralTest.h" #include "backend/lib/macros.h" class CorrelationCoefficientView; class Spreadsheet; class QString; class Column; class QVBoxLayout; class QLabel; class CorrelationCoefficient : public GeneralTest { Q_OBJECT public: explicit CorrelationCoefficient(const QString& name); ~CorrelationCoefficient() override; enum Test{ Pearson, Kendall, Spearman }; double correlationValue(); QWidget* view() const override; void performTest(Test m_test, bool categoricalVariable = true); private: void performPearson(bool categoricalVariable); void performKendall(); void performSpearman(); + int findDiscordants(int* ranks, int start, int end); + double m_correlationValue; }; #endif // CORRELATIONCOEFFICIENT_H diff --git a/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.cpp b/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.cpp index 391fba7f4..85ac2deb5 100644 --- a/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.cpp +++ b/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.cpp @@ -1,520 +1,512 @@ /*************************************************************************** File : CorrelationCoefficientDock.cpp Project : LabPlot Description : widget for correlation test properties -------------------------------------------------------------------- Copyright : (C) 2019 Devanshu Agarwal(agarwaldevanshu8@gmail.com) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "CorrelationCoefficientDock.h" #include "backend/core/AspectTreeModel.h" #include "backend/core/AbstractAspect.h" #include "backend/core/Project.h" #include "backend/spreadsheet/Spreadsheet.h" #include "commonfrontend/widgets/TreeViewComboBox.h" #include "kdefrontend/datasources/DatabaseManagerDialog.h" #include "kdefrontend/datasources/DatabaseManagerWidget.h" #include "kdefrontend/TemplateHandler.h" #include #include #include #include #include #include #include #include /*! \class CorrelationCoefficientDock \brief Provides a dock (widget) for correlation testing: \ingroup kdefrontend */ //TODO: To add tooltips in docks for non obvious widgets. //TODO: Add functionality for database along with spreadsheet. CorrelationCoefficientDock::CorrelationCoefficientDock(QWidget* parent) : QWidget(parent) { ui.setupUi(this); ui.cbDataSourceType->addItem(i18n("Spreadsheet")); ui.cbDataSourceType->addItem(i18n("Database")); cbSpreadsheet = new TreeViewComboBox; ui.gridLayout->addWidget(cbSpreadsheet, 5, 4, 1, 3); ui.bDatabaseManager->setIcon(QIcon::fromTheme("network-server-database")); ui.bDatabaseManager->setToolTip(i18n("Manage connections")); m_configPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).constFirst() + "sql_connections"; ui.cbTest->addItem( i18n("Pearson r"), CorrelationCoefficient::Test::Pearson); ui.cbTest->addItem( i18n("Kendall"), CorrelationCoefficient::Test::Kendall); ui.cbTest->addItem( i18n("Spearman"), CorrelationCoefficient::Test::Spearman); // adding item to tests and testtype combo box; // making all test blocks invisible at starting. ui.lCategorical->hide(); ui.chbCategorical->hide(); ui.lCol1->hide(); ui.cbCol1->hide(); ui.lCol2->hide(); ui.cbCol2->hide(); ui.pbPerformTest->setEnabled(false); ui.pbPerformTest->setIcon(QIcon::fromTheme("run-build")); // readConnections(); connect(ui.cbDataSourceType, static_cast(&QComboBox::currentIndexChanged), this, &CorrelationCoefficientDock::dataSourceTypeChanged); connect(cbSpreadsheet, &TreeViewComboBox::currentModelIndexChanged, this, &CorrelationCoefficientDock::spreadsheetChanged); // connect(ui.cbConnection, static_cast(&QComboBox::currentIndexChanged), // this, &CorrelationCoefficientDock::connectionChanged); // connect(ui.cbTable, static_cast(&QComboBox::currentIndexChanged), // this, &CorrelationCoefficientDock::tableChanged); // connect(ui.bDatabaseManager, &QPushButton::clicked, this, &CorrelationCoefficientDock::showDatabaseManager); // connect(ui.bAddRow, &QPushButton::clicked, this, &CorrelationCoefficientDock::addRow); // connect(ui.bRemoveRow, &QPushButton::clicked, this,&CorrelationCoefficientDock::removeRow); // connect(ui.bAddColumn, &QPushButton::clicked, this, &CorrelationCoefficientDock::addColumn); // connect(ui.bRemoveColumn, &QPushButton::clicked, this,&CorrelationCoefficientDock::removeColumn); // connect(ui.cbCol1, static_cast(&QComboBox::currentIndexChanged), this, &CorrelationCoefficientDock::doTTest); // connect(ui.cbCol2, static_cast(&QComboBox::currentIndexChanged), this, &CorrelationCoefficientDock::doTTest); // connect(ui.lwFields, &QListWidget::itemSelectionChanged, this, [=]() { // bool enabled = !ui.lwFields->selectedItems().isEmpty(); // ui.bAddRow->setEnabled(enabled); // ui.bAddColumn->setEnabled(enabled); // }); // connect(ui.lwRows, &QListWidget::doubleClicked, this,&CorrelationCoefficientDock::removeRow); // connect(ui.lwRows, &QListWidget::itemSelectionChanged, this, [=]() { // ui.bRemoveRow->setEnabled(!ui.lwRows->selectedItems().isEmpty()); // }); // connect(ui.lwColumns, &QListWidget::doubleClicked, this,&CorrelationCoefficientDock::removeColumn); // connect(ui.lwColumns, &QListWidget::itemSelectionChanged, this, [=]() { // ui.bRemoveColumn->setEnabled(!ui.lwColumns->selectedItems().isEmpty()); // }); connect(ui.cbTest, static_cast(&QComboBox::currentIndexChanged), this, &CorrelationCoefficientDock::showCorrelationCoefficient); connect(ui.chbCategorical, &QCheckBox::stateChanged, this, &CorrelationCoefficientDock::changeCbCol2Label); - connect(ui.pbPerformTest, &QPushButton::clicked, this, &CorrelationCoefficientDock::doCorrelationCoefficient); + connect(ui.pbPerformTest, &QPushButton::clicked, this, &CorrelationCoefficientDock::findCorrelationCoefficient); connect(ui.cbCol1, static_cast(&QComboBox::currentIndexChanged), this, &CorrelationCoefficientDock::col1IndexChanged); ui.cbTest->setCurrentIndex(0); emit ui.cbTest->currentIndexChanged(0); } void CorrelationCoefficientDock::setCorrelationCoefficient(CorrelationCoefficient* CorrelationCoefficient) { m_initializing = true; - m_CorrelationCoefficient = CorrelationCoefficient; + m_correlationCoefficient = CorrelationCoefficient; - m_aspectTreeModel = new AspectTreeModel(m_CorrelationCoefficient->project()); + m_aspectTreeModel = new AspectTreeModel(m_correlationCoefficient->project()); QList list{AspectType::Folder, AspectType::Workbook, AspectType::Spreadsheet, AspectType::LiveDataSource}; cbSpreadsheet->setTopLevelClasses(list); list = {AspectType::Spreadsheet, AspectType::LiveDataSource}; m_aspectTreeModel->setSelectableAspects(list); cbSpreadsheet->setModel(m_aspectTreeModel); //show the properties - ui.leName->setText(m_CorrelationCoefficient->name()); - ui.leComment->setText(m_CorrelationCoefficient->comment()); - ui.cbDataSourceType->setCurrentIndex(m_CorrelationCoefficient->dataSourceType()); - if (m_CorrelationCoefficient->dataSourceType() == CorrelationCoefficient::DataSourceType::DataSourceSpreadsheet) - setModelIndexFromAspect(cbSpreadsheet, m_CorrelationCoefficient->dataSourceSpreadsheet()); + ui.leName->setText(m_correlationCoefficient->name()); + ui.leComment->setText(m_correlationCoefficient->comment()); + ui.cbDataSourceType->setCurrentIndex(m_correlationCoefficient->dataSourceType()); + if (m_correlationCoefficient->dataSourceType() == CorrelationCoefficient::DataSourceType::DataSourceSpreadsheet) + setModelIndexFromAspect(cbSpreadsheet, m_correlationCoefficient->dataSourceSpreadsheet()); // else - // ui.cbConnection->setCurrentIndex(ui.cbConnection->findText(m_CorrelationCoefficient->dataSourceConnection())); + // ui.cbConnection->setCurrentIndex(ui.cbConnection->findText(m_correlationCoefficient->dataSourceConnection())); - setColumnsComboBoxModel(m_CorrelationCoefficient->dataSourceSpreadsheet()); + setColumnsComboBoxModel(m_correlationCoefficient->dataSourceSpreadsheet()); this->dataSourceTypeChanged(ui.cbDataSourceType->currentIndex()); //setting rows and columns in combo box; //undo functions -// connect(m_CorrelationCoefficient, SIGNAL(aspectDescriptionChanged(const AbstractAspect*)), this, SLOT(CorrelationCoefficientDescriptionChanged(const AbstractAspect*))); +// connect(m_correlationCoefficient, SIGNAL(aspectDescriptionChanged(const AbstractAspect*)), this, SLOT(CorrelationCoefficientDescriptionChanged(const AbstractAspect*))); m_initializing = false; } void CorrelationCoefficientDock::showCorrelationCoefficient() { if (ui.cbTest->count() == 0) return; m_test = CorrelationCoefficient::Test(ui.cbTest->currentData().toInt()); ui.lCol1->show(); ui.cbCol1->show(); ui.lCol2->show(); ui.cbCol2->show(); ui.lCategorical->show(); ui.chbCategorical->show(); setColumnsComboBoxView(); ui.pbPerformTest->setEnabled(nonEmptySelectedColumns()); } -void CorrelationCoefficientDock::doCorrelationCoefficient() { +void CorrelationCoefficientDock::findCorrelationCoefficient() { QVector cols; if (ui.cbCol1->count() == 0) return; cols << reinterpret_cast(ui.cbCol1->currentData().toLongLong()); cols << reinterpret_cast(ui.cbCol2->currentData().toLongLong()); - m_CorrelationCoefficient->setColumns(cols); - switch(m_test) { - case (CorrelationCoefficient::Test::Pearson): { - m_CorrelationCoefficient->performTest(m_test, ui.chbCategorical->isChecked()); - break; - } - case (CorrelationCoefficient::Test::Kendall): { - break; - } - case (CorrelationCoefficient::Test::Spearman): { - break; - } - } - - m_CorrelationCoefficient->setColumns(cols); - m_CorrelationCoefficient->performTest(m_test, ui.chbCategorical->isChecked()); + m_correlationCoefficient->setColumns(cols); + m_correlationCoefficient->performTest(m_test, ui.chbCategorical->isChecked()); } void CorrelationCoefficientDock::setModelIndexFromAspect(TreeViewComboBox* cb, const AbstractAspect* aspect) { if (aspect) cb->setCurrentModelIndex(m_aspectTreeModel->modelIndexOfAspect(aspect)); else cb->setCurrentModelIndex(QModelIndex()); } ////************************************************************* ////****** SLOTs for changes triggered in CorrelationCoefficientDock ******* ////************************************************************* //void CorrelationCoefficientDock::nameChanged() { // if (m_initializing) // return; -// m_CorrelationCoefficient->setName(ui.leName->text()); +// m_correlationCoefficient->setName(ui.leName->text()); //} //void CorrelationCoefficientDock::commentChanged() { // if (m_initializing) // return; -// m_CorrelationCoefficient->setComment(ui.leComment->text()); +// m_correlationCoefficient->setComment(ui.leComment->text()); //} void CorrelationCoefficientDock::dataSourceTypeChanged(int index) { //QDEBUG("in dataSourceTypeChanged"); CorrelationCoefficient::DataSourceType type = static_cast(index); bool showDatabase = (type == CorrelationCoefficient::DataSourceType::DataSourceDatabase); ui.lSpreadsheet->setVisible(!showDatabase); cbSpreadsheet->setVisible(!showDatabase); ui.lConnection->setVisible(showDatabase); ui.cbConnection->setVisible(showDatabase); ui.bDatabaseManager->setVisible(showDatabase); ui.lTable->setVisible(showDatabase); ui.cbTable->setVisible(showDatabase); if (m_initializing) return; - m_CorrelationCoefficient->setComment(ui.leComment->text()); + m_correlationCoefficient->setComment(ui.leComment->text()); } void CorrelationCoefficientDock::spreadsheetChanged(const QModelIndex& index) { //QDEBUG("in spreadsheetChanged"); auto* aspect = static_cast(index.internalPointer()); Spreadsheet* spreadsheet = dynamic_cast(aspect); setColumnsComboBoxModel(spreadsheet); - m_CorrelationCoefficient->setDataSourceSpreadsheet(spreadsheet); + m_correlationCoefficient->setDataSourceSpreadsheet(spreadsheet); } void CorrelationCoefficientDock::col1IndexChanged(int index) { if (index < 0) return; changeCbCol2Label(); } //void CorrelationCoefficientDock::connectionChanged() { // if (ui.cbConnection->currentIndex() == -1) { // ui.lTable->hide(); // ui.cbTable->hide(); // return; // } // //clear the previously shown tables // ui.cbTable->clear(); // ui.lTable->show(); // ui.cbTable->show(); // const QString& connection = ui.cbConnection->currentText(); // //connection name was changed, determine the current connections settings // KConfig config(m_configPath, KConfig::SimpleConfig); // KConfigGroup group = config.group(connection); // //close and remove the previos connection, if available // if (m_db.isOpen()) { // m_db.close(); // QSqlDatabase::removeDatabase(m_db.driverName()); // } // //open the selected connection // //QDEBUG("CorrelationCoefficientDock: connecting to " + connection); // const QString& driver = group.readEntry("Driver"); // m_db = QSqlDatabase::addDatabase(driver); // const QString& dbName = group.readEntry("DatabaseName"); // if (DatabaseManagerWidget::isFileDB(driver)) { // if (!QFile::exists(dbName)) { // KMessageBox::error(this, i18n("Couldn't find the database file '%1'. Please check the connection settings.", dbName), // appendRow i18n("Connection Failed")); // return; // } else // m_db.setDatabaseName(dbName); // } else if (DatabaseManagerWidget::isODBC(driver)) { // if (group.readEntry("CustomConnectionEnabled", false)) // m_db.setDatabaseName(group.readEntry("CustomConnectionString")); // else // m_db.setDatabaseName(dbName); // } else { // m_db.setDatabaseName(dbName); // m_db.setHostName( group.readEntry("HostName") ); // m_db.setPort( group.readEntry("Port", 0) ); // m_db.setUserName( group.readEntry("UserName") ); // m_db.setPassword( group.readEntry("Password") ); // } // WAIT_CURSOR; // if (!m_db.open()) { // RESET_CURSOR; // KMessageBox::error(this, i18n("Failed to connect to the database '%1'. Please check the connection settings.", ui.cbConnection->currentText()) + // QLatin1String("\n\n") + m_db.lastError().databaseText(), // i18n("Connection Failed")); // return; // } // //show all available database tables // if (m_db.tables().size()) { // for (auto table : m_db.tables()) // ui.cbTable->addItem(QIcon::fromTheme("view-form-table"), table); // ui.cbTable->setCurrentIndex(0); // } // RESET_CURSOR; // if (m_initializing) // return; -//// m_CorrelationCoefficient->setDataSourceConnection(connection); +//// m_correlationCoefficient->setDataSourceConnection(connection); //} //void CorrelationCoefficientDock::tableChanged() { // const QString& table = ui.cbTable->currentText(); // //show all attributes of the selected table //// for (const auto* col : spreadsheet->children()) { //// QListWidgetItem* item = new QListWidgetItem(col->icon(), col->name()); //// ui.lwFields->addItem(item); //// } // if (m_initializing) // return; -//// m_CorrelationCoefficient->setDataSourceTable(table); +//// m_correlationCoefficient->setDataSourceTable(table); //} ////************************************************************* ////******** SLOTs for changes triggered in Spreadsheet ********* ////************************************************************* void CorrelationCoefficientDock::CorrelationCoefficientDescriptionChanged(const AbstractAspect* aspect) { - if (m_CorrelationCoefficient != aspect) + if (m_correlationCoefficient != aspect) return; m_initializing = true; if (aspect->name() != ui.leName->text()) ui.leName->setText(aspect->name()); else if (aspect->comment() != ui.leComment->text()) ui.leComment->setText(aspect->comment()); m_initializing = false; } void CorrelationCoefficientDock::changeCbCol2Label() { if (ui.cbCol1->count() == 0) return; QString selected_text = ui.cbCol1->currentText(); - Column* col1 = m_CorrelationCoefficient->dataSourceSpreadsheet()->column(selected_text); + Column* col1 = m_correlationCoefficient->dataSourceSpreadsheet()->column(selected_text); if (!ui.chbCategorical->isChecked() && (col1->columnMode() == AbstractColumn::Integer || col1->columnMode() == AbstractColumn::Numeric)) { ui.lCol2->setText( i18n("Independent Var. 2")); ui.chbCategorical->setChecked(false); ui.chbCategorical->setEnabled(true); } else { ui.lCol2->setText( i18n("Dependent Var. 1")); if (!ui.chbCategorical->isChecked()) ui.chbCategorical->setEnabled(false); else ui.chbCategorical->setEnabled(true); ui.chbCategorical->setChecked(true); } } ////************************************************************* ////******************** SETTINGS ******************************* ////************************************************************* //void CorrelationCoefficientDock::load() { //} //void CorrelationCoefficientDock::loadConfigFromTemplate(KConfig& config) { // Q_UNUSED(config); //} ///*! // loads saved matrix properties from \c config. // */ //void CorrelationCoefficientDock::loadConfig(KConfig& config) { // Q_UNUSED(config); //} ///*! // saves matrix properties to \c config. // */ //void CorrelationCoefficientDock::saveConfigAsTemplate(KConfig& config) { // Q_UNUSED(config); //} void CorrelationCoefficientDock::setColumnsComboBoxModel(Spreadsheet* spreadsheet) { m_onlyValuesCols.clear(); m_twoCategoricalCols.clear(); m_multiCategoricalCols.clear(); for (auto* col : spreadsheet->children()) { if (col->columnMode() == AbstractColumn::Integer || col->columnMode() == AbstractColumn::Numeric) m_onlyValuesCols.append(col); else { int np = 0, n_rows = 0; countPartitions(col, np, n_rows); if (np <= 1) continue; else if (np == 2) m_twoCategoricalCols.append(col); else m_multiCategoricalCols.append(col); } } setColumnsComboBoxView(); showCorrelationCoefficient(); } //TODO: change from if else to switch case: void CorrelationCoefficientDock::setColumnsComboBoxView() { ui.cbCol1->clear(); ui.cbCol2->clear(); QList::iterator i; switch (m_test) { case (CorrelationCoefficient::Test::Pearson): { for (i = m_onlyValuesCols.begin(); i != m_onlyValuesCols.end(); i++) { ui.cbCol1->addItem( (*i)->name(), qint64(*i)); ui.cbCol2->addItem( (*i)->name(), qint64(*i)); } for (i = m_twoCategoricalCols.begin(); i != m_twoCategoricalCols.end(); i++) ui.cbCol1->addItem( (*i)->name(), qint64(*i)); break; } case CorrelationCoefficient::Test::Kendall: { for (i = m_onlyValuesCols.begin(); i != m_onlyValuesCols.end(); i++) { ui.cbCol1->addItem( (*i)->name(), qint64(*i)); ui.cbCol2->addItem( (*i)->name(), qint64(*i)); } - for (i = m_twoCategoricalCols.begin(); i != m_twoCategoricalCols.end(); i++) + for (i = m_twoCategoricalCols.begin(); i != m_twoCategoricalCols.end(); i++) { ui.cbCol1->addItem( (*i)->name(), qint64(*i)); + ui.cbCol2->addItem( (*i)->name(), qint64(*i)); + } + for (i = m_multiCategoricalCols.begin(); i != m_multiCategoricalCols.end(); i++) { + ui.cbCol1->addItem( (*i)->name(), qint64(*i)); + ui.cbCol2->addItem( (*i)->name(), qint64(*i)); + } break; } case CorrelationCoefficient::Test::Spearman: { for (i = m_onlyValuesCols.begin(); i != m_onlyValuesCols.end(); i++) { ui.cbCol1->addItem( (*i)->name(), qint64(*i)); ui.cbCol2->addItem( (*i)->name(), qint64(*i)); } for (i = m_twoCategoricalCols.begin(); i != m_twoCategoricalCols.end(); i++) ui.cbCol1->addItem( (*i)->name(), qint64(*i)); break; } } } bool CorrelationCoefficientDock::nonEmptySelectedColumns() { if (ui.cbCol1->isVisible() && ui.cbCol1->count() < 1) return false; if (ui.cbCol2->isVisible() && ui.cbCol2->count() < 1) return false; return true; } void CorrelationCoefficientDock::countPartitions(Column *column, int &np, int &total_rows) { total_rows = column->rowCount(); np = 0; QString cell_value; QMap discovered_categorical_var; AbstractColumn::ColumnMode original_col_mode = column->columnMode(); column->setColumnMode(AbstractColumn::Text); for (int i = 0; i < total_rows; i++) { cell_value = column->textAt(i); if (cell_value.isEmpty()) { total_rows = i; break; } if (discovered_categorical_var[cell_value]) continue; discovered_categorical_var[cell_value] = true; np++; } column->setColumnMode(original_col_mode); } diff --git a/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.h b/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.h index a4395be44..c396e70bd 100644 --- a/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.h +++ b/src/kdefrontend/dockwidgets/CorrelationCoefficientDock.h @@ -1,105 +1,105 @@ /*************************************************************************** File : CorrelationCoefficientDock.h Project : LabPlot Description : widget for hypothesis testing properties -------------------------------------------------------------------- Copyright : (C) 2019 Devanshu Agarwal(agarwaldevanshu8@gmail.com) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef CORRELATIONCOEFFICIENTDOCK_H #define CORRELATIONCOEFFICIENTDOCK_H #include "backend/generalTest/CorrelationCoefficient.h" #include "ui_correlationcoefficientdock.h" #include //class Column; //class Spreadsheet; //class AbstractAspect; class AspectTreeModel; //class CorrelationCoefficient; class TreeViewComboBox; //class KConfig; //class QStandardItemModel; //class QStandardItem; //class QComboBox; class CorrelationCoefficientDock : public QWidget { Q_OBJECT public: explicit CorrelationCoefficientDock(QWidget*); void setCorrelationCoefficient(CorrelationCoefficient*); private: Ui::CorrelationCoefficientDock ui; bool m_initializing{false}; TreeViewComboBox* cbSpreadsheet{nullptr}; - CorrelationCoefficient* m_CorrelationCoefficient{nullptr}; + CorrelationCoefficient* m_correlationCoefficient{nullptr}; AspectTreeModel* m_aspectTreeModel{nullptr}; QSqlDatabase m_db; QString m_configPath; // void load(); // void loadConfig(KConfig&); void setModelIndexFromAspect(TreeViewComboBox*, const AbstractAspect*); // void readConnections(); // void updateFields(); // bool fieldSelected(const QString&); CorrelationCoefficient::Test m_test; void countPartitions(Column *column, int &np, int &total_rows); void setColumnsComboBoxModel(Spreadsheet* spreadsheet); void setColumnsComboBoxView(); bool nonEmptySelectedColumns(); QList m_onlyValuesCols; QList m_twoCategoricalCols; QList m_multiCategoricalCols; private slots: //SLOTs for changes triggered in PivotTableDock // void nameChanged(); // void commentChanged(); void dataSourceTypeChanged(int); void showCorrelationCoefficient(); - void doCorrelationCoefficient(); + void findCorrelationCoefficient(); void spreadsheetChanged(const QModelIndex&); void col1IndexChanged(int index); void changeCbCol2Label(); // void connectionChanged(); // void tableChanged(); // void showDatabaseManager(); // //SLOTs for changes triggered in PivotTable void CorrelationCoefficientDescriptionChanged(const AbstractAspect*); // //save/load template // void loadConfigFromTemplate(KConfig&); // void saveConfigAsTemplate(KConfig&); signals: // void info(const QString&); }; #endif // CORRELATIONCOEFFICIENTDOCK_H