Skip to content

Commit

Permalink
Add ImportAnnotationsKeyDetection
Browse files Browse the repository at this point in the history
  • Loading branch information
timangus committed Aug 31, 2023
1 parent 7eee124 commit da673b3
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/plugins/correlation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ list(APPEND HEADERS
${CMAKE_CURRENT_LIST_DIR}/graphsizeestimateplotitem.h
${CMAKE_CURRENT_LIST_DIR}/hierarchicalclusteringcommand.h
${CMAKE_CURRENT_LIST_DIR}/importannotationscommand.h
${CMAKE_CURRENT_LIST_DIR}/importannotationskeydetection.h
${CMAKE_CURRENT_LIST_DIR}/knnprotograph.h
${CMAKE_CURRENT_LIST_DIR}/loading/correlationfileparser.h
${CMAKE_CURRENT_LIST_DIR}/normaliser.h
Expand All @@ -49,6 +50,7 @@ list(APPEND SOURCES
${CMAKE_CURRENT_LIST_DIR}/graphsizeestimateplotitem.cpp
${CMAKE_CURRENT_LIST_DIR}/hierarchicalclusteringcommand.cpp
${CMAKE_CURRENT_LIST_DIR}/importannotationscommand.cpp
${CMAKE_CURRENT_LIST_DIR}/importannotationskeydetection.cpp
${CMAKE_CURRENT_LIST_DIR}/loading/correlationfileparser.cpp
${CMAKE_CURRENT_LIST_DIR}/qcpcolumnannotations.cpp
${CMAKE_CURRENT_LIST_DIR}/quantilenormaliser.cpp
Expand Down
2 changes: 2 additions & 0 deletions source/plugins/correlation/correlationplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "attributevaluecorrelationheatmapworker.h"

#include "importannotationscommand.h"
#include "importannotationskeydetection.h"

#include "hierarchicalclusteringcommand.h"

Expand Down Expand Up @@ -1432,6 +1433,7 @@ CorrelationPlugin::CorrelationPlugin()
qmlRegisterType<CorrelationTabularDataParser>("app.graphia", 1, 0, "CorrelationTabularDataParser");
qmlRegisterType<AttributeValueCorrelationHeatmapItem>("app.graphia", 1, 0, "AttributeValueCorrelationHeatmapItem");
qmlRegisterType<AttributeValueCorrelationHeatmapWorker>("app.graphia", 1, 0, "AttributeValueCorrelationHeatmapWorker");
qmlRegisterType<ImportAnnotationsKeyDetection>("app.graphia", 1, 0, "ImportAnnotationsKeyDetection");
}

QVariantMap CorrelationPlugin::correlationInfoFor(int correlationType) const
Expand Down
98 changes: 98 additions & 0 deletions source/plugins/correlation/importannotationskeydetection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright © 2013-2023 Graphia Technologies Ltd.
*
* This file is part of Graphia.
*
* Graphia 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 3 of the License, or
* (at your option) any later version.
*
* Graphia 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 Graphia. If not, see <http://www.gnu.org/licenses/>.
*/

#include "importannotationskeydetection.h"

#include "correlationplugin.h"

#include <QFuture>
#include <QtConcurrentRun>
#include <QQmlEngine>

using namespace Qt::Literals::StringLiterals;

ImportAnnotationsKeyDetection::ImportAnnotationsKeyDetection() // NOLINT modernize-use-equals-default
{
connect(&_watcher, &QFutureWatcher<void>::started, this, &ImportAnnotationsKeyDetection::busyChanged);
connect(&_watcher, &QFutureWatcher<void>::finished, this, &ImportAnnotationsKeyDetection::busyChanged);
}

ImportAnnotationsKeyDetection::~ImportAnnotationsKeyDetection() // NOLINT modernize-use-equals-default
{
_watcher.waitForFinished();
}

void ImportAnnotationsKeyDetection::start()
{
uncancel();

const QFuture<void> future = QtConcurrent::run([this]
{
size_t bestRowIndex = 0;
int bestPercent = 0;

QStringList columnNames;
columnNames.reserve(static_cast<int>(_plugin->numColumns()));

for(size_t i = 0; i < _plugin->numColumns(); i++)
columnNames.append(_plugin->columnName(i));

auto typeIdentities = _tabularData->rowTypeIdentities();

for(size_t rowIndex = 0; rowIndex < _tabularData->numRows(); rowIndex++)
{
auto type = typeIdentities.at(rowIndex).type();
if(type != TypeIdentity::Type::String && type != TypeIdentity::Type::Int)
continue;

auto percent = _tabularData->rowMatchPercentage(rowIndex, columnNames);

// If we already have an equivalent match, prefer the earlier column
if(percent == bestPercent && rowIndex > bestRowIndex)
continue;

if(percent >= bestPercent)
{
bestRowIndex = rowIndex;
bestPercent = percent;
}

// Can't improve on 100%!
if(bestPercent >= 100 || cancelled())
break;
}

_result.clear();

if(!cancelled())
{
_result.insert(u"row"_s, static_cast<int>(bestRowIndex));
_result.insert(u"percent"_s, bestPercent);
}

emit resultChanged();
});

_watcher.setFuture(future);
}

void ImportAnnotationsKeyDetection::reset()
{
_result = {};
emit resultChanged();
}
61 changes: 61 additions & 0 deletions source/plugins/correlation/importannotationskeydetection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright © 2013-2023 Graphia Technologies Ltd.
*
* This file is part of Graphia.
*
* Graphia 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 3 of the License, or
* (at your option) any later version.
*
* Graphia 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 Graphia. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef IMPORTANNOTATIONSKEYDETECTION_H
#define IMPORTANNOTATIONSKEYDETECTION_H

#include "shared/loading/tabulardata.h"
#include "shared/utils/cancellable.h"

#include <QObject>
#include <QFutureWatcher>
#include <QVariantMap>

class CorrelationPluginInstance;

class ImportAnnotationsKeyDetection : public QObject, public Cancellable
{
Q_OBJECT

Q_PROPERTY(CorrelationPluginInstance* plugin MEMBER _plugin)
Q_PROPERTY(std::shared_ptr<TabularData> tabularData MEMBER _tabularData)
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
Q_PROPERTY(QVariantMap result MEMBER _result NOTIFY resultChanged)

private:
CorrelationPluginInstance* _plugin = nullptr;
std::shared_ptr<TabularData> _tabularData = nullptr;
QFutureWatcher<void> _watcher;
QVariantMap _result;

public:
ImportAnnotationsKeyDetection();
~ImportAnnotationsKeyDetection() override;

Q_INVOKABLE void start();
Q_INVOKABLE void reset();
Q_INVOKABLE void cancel() override { Cancellable::cancel(); }

bool busy() const { return _watcher.isRunning(); }

signals:
void busyChanged();
void resultChanged();
};

#endif // IMPORTANNOTATIONSKEYDETECTION_H

0 comments on commit da673b3

Please sign in to comment.