Skip to content

Commit

Permalink
Fix crash upon recomputing the embedding with a lower scale than prev…
Browse files Browse the repository at this point in the history
…iously

This fixes the crash when the hsne embedding is loaded in a scatterplot and we recompute it with a lower scale. There is still something that breaks when you go to only 1 scale, but that's not a pressing issue, as that shouldn't really be done.
  • Loading branch information
JulianThijssen committed Nov 21, 2024
1 parent 21775f6 commit 2dfcabd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/HSNE/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(HSNE_PLUGIN_SOURCES
${DIR}/HsneHierarchy.cpp
${DIR}/HsneParameters.h
${DIR}/HsneRecomputeWarningDialog.h
${DIR}/Globals.h
PARENT_SCOPE
)

Expand Down
12 changes: 12 additions & 0 deletions src/HSNE/Globals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <QUuid>

/** Property label for DATA_TAG property in datasets created by this plugin */
inline QString DATA_TAG_LABEL = "HSNE_Tag";

/**
/* Global static UUID for tagging data generated by this plugin, used for removal upon recomputation
/* Gets instantiated in HsneAnalysisPlugin::init()
*/
inline QUuid DATA_TAG;
22 changes: 22 additions & 0 deletions src/HSNE/HsneAnalysisPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "HsneParameters.h"
#include "HsneRecomputeWarningDialog.h"
#include "HsneScaleAction.h"
#include "Globals.h"

#include <PointData/DimensionsPickerAction.h>
#include <PointData/InfoAction.h>
Expand Down Expand Up @@ -43,6 +44,8 @@ HsneAnalysisPlugin::~HsneAnalysisPlugin()

void HsneAnalysisPlugin::init()
{
DATA_TAG = QUuid::createUuid();

// Get input/output datasets
auto inputDataset = getInputDataset<Points>();

Expand Down Expand Up @@ -251,6 +254,9 @@ void HsneAnalysisPlugin::computeTopLevelEmbedding()
datasetTask.setName("Compute HSNE top level embedding");
datasetTask.setRunning();

// Remove any previous generated datasets
removePreviouslyCreatedDatasets();

// Get the top scale of the HSNE hierarchy
const int topScaleIndex = _hierarchy->getTopScale();
Hsne::scale_type& topScale = _hierarchy->getScale(topScaleIndex);
Expand Down Expand Up @@ -289,6 +295,7 @@ void HsneAnalysisPlugin::computeTopLevelEmbedding()
auto selectionHelperCount = inputDataset->getProperty("selectionHelperCount").toInt();
inputDataset->setProperty("selectionHelperCount", ++selectionHelperCount);
_selectionHelperData = inputDataset->createSubsetFromSelection(QString("Hsne selection helper %1").arg(selectionHelperCount), inputDataset, /*visible = */ false);
_selectionHelperData->setProperty(DATA_TAG_LABEL, DATA_TAG);

selectionDataset->indices.clear();

Expand Down Expand Up @@ -348,6 +355,21 @@ void HsneAnalysisPlugin::continueComputation()
_tsneAnalysis.continueComputation(_hsneSettingsAction->getTsneParameters().getNumIterations());
}

void HsneAnalysisPlugin::removePreviouslyCreatedDatasets()
{
qDebug() << "Trying to remove datasets previously created by this plugin..";
mv::Datasets datasets = mv::data().getAllDatasets();
for (mv::Dataset<DatasetImpl> dataset : datasets)
{
if (dataset->hasProperty(DATA_TAG_LABEL) && dataset->getProperty(DATA_TAG_LABEL).toUuid() == DATA_TAG)
{
qDebug() << "Found previous dataset" << dataset->getGuiName() << "to be deleted..";
mv::data().removeDataset(dataset);
qDebug() << "Previous dataset deleted.";
}
}
}

void HsneAnalysisPlugin::fromVariantMap(const QVariantMap& variantMap)
{
AnalysisPlugin::fromVariantMap(variantMap);
Expand Down
1 change: 1 addition & 0 deletions src/HSNE/HsneAnalysisPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HsneAnalysisPlugin : public AnalysisPlugin

void computeTopLevelEmbedding();
void continueComputation();
void removePreviouslyCreatedDatasets();

HsneHierarchy& getHierarchy() { return *_hierarchy.get(); }
TsneAnalysis& getTsneAnalysis() { return _tsneAnalysis; }
Expand Down
3 changes: 3 additions & 0 deletions src/HSNE/HsneScaleAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "GradientDescentSettingsAction.h"
#include "HsneHierarchy.h"
#include "TsneParameters.h"
#include "Globals.h"

#include <event/Event.h>

Expand Down Expand Up @@ -296,11 +297,13 @@ void HsneScaleAction::refine()
_input->setProperty("selectionHelperCount", ++selectionHelperCount);
_selectionHelpers.push_back(_input->createSubsetFromSelection(QString("Hsne selection helper %1").arg(selectionHelperCount), _input, /* visible = */ false));
auto& hsneScaleSubset = _selectionHelpers.back();
hsneScaleSubset.setProperty("HSNE_tag", DATA_TAG);

// Create derived data for the embedding
_refineEmbeddings.push_back(mv::data().createDerivedDataset<Points>(QString("Hsne scale %1").arg(refinedScaleLevel), hsneScaleSubset, _embedding, false));
auto& refineEmbedding = _refineEmbeddings.back();
refineEmbedding->setProperty("selectionHelperID", hsneScaleSubset->getId());
refineEmbedding->setProperty("HSNE_tag", DATA_TAG);

//qDebug() << "refineEmbedding " << refineEmbedding->getId() << " with hsneScaleSubset " << hsneScaleSubset->getId();

Expand Down

0 comments on commit 2dfcabd

Please sign in to comment.