Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Flexible choice of cluster size #970

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/applications/mne_scan/plugins/rtfwd/rtfwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ RtFwd::RtFwd()
, m_bBusy(false)
, m_bDoRecomputation(false)
, m_bDoClustering(true)
, m_bNClusterChanged(true)
, m_bDoFwdComputation(false)
{
// set init values
Expand All @@ -105,6 +106,7 @@ RtFwd::RtFwd()
m_pFwdSettings->include_eeg = true;
m_pFwdSettings->accurate = true;
m_pFwdSettings->mindist = 5.0f/1000.0f;
m_pFwdSettings->ncluster = 200;

m_sAtlasDir = QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/subjects/sample/label";
}
Expand Down Expand Up @@ -320,6 +322,8 @@ void RtFwd::initPluginControlWidgets()
this, &RtFwd::onClusteringStatusChanged);
connect(pFwdSettingsView, &FwdSettingsView::atlasDirChanged,
this, &RtFwd::onAtlasDirChanged);
connect(pFwdSettingsView, &FwdSettingsView::clusterNumberChanged,
this, &RtFwd::onClusterNumberChanged);
connect(pFwdSettingsView, &FwdSettingsView::doForwardComputation,
this, &RtFwd::onDoForwardComputation);

Expand Down Expand Up @@ -390,6 +394,16 @@ void RtFwd::onAtlasDirChanged(const QString& sDirPath, const AnnotationSet::SPtr

//=============================================================================================================

void RtFwd::onClusterNumberChanged(int iNClusterNumber)
{
m_mutex.lock();
m_pFwdSettings->ncluster = iNClusterNumber;
m_bNClusterChanged = true;
m_mutex.unlock();
}

//=============================================================================================================

void RtFwd::run()
{
// Wait for fiff the info to arrive
Expand Down Expand Up @@ -425,6 +439,7 @@ void RtFwd::run()
bool bDoRecomputation = false; // indicate if we want to recompute
bool bDoClustering = false; // indicate if we want to cluster
bool bFwdReady = false; // only cluster if fwd is ready
bool bNClusterChanged = false; // Perform new clustering when cluster size changed
bool bHpiConnectected = false; // only update/recompute if hpi is connected
bool bDoFwdComputation = false; // compute forward if requested
bool bIsInit = false; // only recompute if initial fwd solulion is calculated
Expand Down Expand Up @@ -457,10 +472,9 @@ void RtFwd::run()
m_mutex.lock();
if(!m_bDoClustering) {
m_pRTFSOutput->measurementData()->setValue(pFwdSolution);
bFwdReady = false; // make sure to not cluster
emit statusInformationChanged(5); //finished
}
bFwdReady = true; // enable cluster
bFwdReady = true; // provide fwd for clustering if wanted
m_bDoFwdComputation = false; // don't call this again if not requested
bIsInit = true; // init computation finished -> recomputation possible
m_mutex.unlock();
Expand Down Expand Up @@ -500,7 +514,7 @@ void RtFwd::run()

if(!bDoClustering) {
m_pRTFSOutput->measurementData()->setValue(pFwdSolution);
bFwdReady = false;
//bFwdReady = false; // doesn't seem to be necessary? bDoClustering = false anyway
emit statusInformationChanged(5); //finished
}
}
Expand All @@ -509,18 +523,22 @@ void RtFwd::run()
// do clustering if requested and fwd is ready
m_mutex.lock();
bDoClustering = m_bDoClustering;
bNClusterChanged = m_bNClusterChanged;
m_mutex.unlock();

if(bDoClustering && bFwdReady) {
if(bDoClustering && bFwdReady && bNClusterChanged) {
emit statusInformationChanged(3); // clustering
pClusteredFwd = MNEForwardSolution::SPtr(new MNEForwardSolution(pFwdSolution->cluster_forward_solution(*m_pAnnotationSet.data(), 200)));
pClusteredFwd = MNEForwardSolution::SPtr(new MNEForwardSolution(pFwdSolution->cluster_forward_solution(*m_pAnnotationSet.data(), m_pFwdSettings->ncluster)));
emit clusteringAvailable(pClusteredFwd->nsource);

m_pRTFSOutput->measurementData()->setValue(pClusteredFwd);

bFwdReady = false;
//bFwdReady = false; // fwd remains ready, allows for reclustering.
m_mutex.lock();
m_bNClusterChanged = false;
m_mutex.unlock();

emit statusInformationChanged(5); //finished
emit statusInformationChanged(6); //finished
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/applications/mne_scan/plugins/rtfwd/rtfwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ class RTFWDSHARED_EXPORT RtFwd : public SCSHAREDLIB::AbstractAlgorithm
*/
void onClusteringStatusChanged(bool bDoRecomputation);

//=========================================================================================================
/**
* Call this function whenever the number of dipoles per cluster changed.
*
* @param[in] iNClusterNumber Number of dipoles per cluster.
*/
void onClusterNumberChanged(int iNClusterNumber);

//=========================================================================================================
/**
* Call this function whenever the atlas directory is set.
Expand All @@ -208,6 +216,8 @@ class RTFWDSHARED_EXPORT RtFwd : public SCSHAREDLIB::AbstractAlgorithm
bool m_bBusy; /**< Indicates if we have to update headposition.**/
bool m_bDoRecomputation; /**< If recomputation is activated.**/
bool m_bDoClustering; /**< If clustering is activated.**/
bool m_bNClusterChanged; /**< Perform new clustering when cluster size changed**/

bool m_bDoFwdComputation; /**< Do a forward computation. **/

QString m_sAtlasDir; /**< File to Atlas. */
Expand Down
29 changes: 28 additions & 1 deletion src/libraries/disp/viewers/formfiles/fwdsettingsview.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>333</width>
<width>362</width>
<height>939</height>
</rect>
</property>
Expand Down Expand Up @@ -221,6 +221,33 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Number of sources per cluster</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="m_spinBox_iNDipoleClustered">
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="minimum">
<number>5</number>
</property>
<property name="maximum">
<number>999</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand Down
17 changes: 16 additions & 1 deletion src/libraries/disp/viewers/fwdsettingsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
//=============================================================================================================

#include "fwdsettingsview.h"
#include "QtWidgets/qspinbox.h"
#include "ui_fwdsettingsview.h"

#include <fs/annotationset.h>
Expand Down Expand Up @@ -81,13 +82,15 @@ FwdSettingsView::FwdSettingsView(const QString& sSettingsPath,

// init
m_pUi->m_checkBox_bDoRecomputation->setChecked(false);
m_pUi->m_checkBox_bDoClustering->setChecked(false);
m_pUi->m_checkBox_bDoClustering->setChecked(true);
m_pUi->m_lineEdit_iNChan->setText(QString::number(0));
m_pUi->m_lineEdit_iNSourceSpace->setText(QString::number(0));
m_pUi->m_lineEdit_iNDipole->setText(QString::number(0));
m_pUi->m_lineEdit_sSourceOri->setText("fixed");
m_pUi->m_lineEdit_sCoordFrame->setText("Head Space");
m_pUi->m_lineEdit_iNDipoleClustered->setText("Not Clustered");
m_pUi->m_spinBox_iNDipoleClustered->setValue(200);
m_pUi->m_spinBox_iNDipoleClustered->setKeyboardTracking(false);

// load init annotation set
QString t_sAtlasDir = QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/subjects/sample/label";
Expand Down Expand Up @@ -115,6 +118,8 @@ FwdSettingsView::FwdSettingsView(const QString& sSettingsPath,
this, &FwdSettingsView::onClusteringStatusChanged);
connect(m_pUi->m_qPushButton_ComputeForward, &QPushButton::clicked,
this, &FwdSettingsView::doForwardComputation);
connect(m_pUi->m_spinBox_iNDipoleClustered, QOverload<int>::of(&QSpinBox::valueChanged),
this, &FwdSettingsView::clusterNumberChanged);

// load settings
loadSettings();
Expand Down Expand Up @@ -203,6 +208,9 @@ void FwdSettingsView::setRecomputationStatus(int iStatus)
} else if (iStatus == 4) {
m_pUi->m_label_recomputationFeedback->setText("Not Computed");
m_pUi->m_label_recomputationFeedback->setStyleSheet("QLabel { background-color : red;}");
} else if (iStatus == 5) {
m_pUi->m_label_recomputationFeedback->setText("Not Clustered");
m_pUi->m_label_recomputationFeedback->setStyleSheet("QLabel { background-color : yellow;}");
} else {
m_pUi->m_label_recomputationFeedback->setText("Finished");
m_pUi->m_label_recomputationFeedback->setStyleSheet("QLabel { background-color : green;}");
Expand Down Expand Up @@ -255,6 +263,13 @@ bool FwdSettingsView::getClusteringStatusChanged()

//=============================================================================================================

int FwdSettingsView::getClusterNumber()
{
return m_pUi->m_spinBox_iNDipoleClustered->value();
}

//=============================================================================================================

void FwdSettingsView::onClusteringStatusChanged(bool bChecked)
{
if(!m_bAnnotaionsLoaded) {
Expand Down
16 changes: 16 additions & 0 deletions src/libraries/disp/viewers/fwdsettingsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ class DISPSHARED_EXPORT FwdSettingsView : public AbstractView
*/
bool getClusteringStatusChanged();

//=========================================================================================================
/**
* Get status of cluster size spin box.
*
* @return Desired number of sources in clustered source space.
*/
int getClusterNumber();

//=========================================================================================================
/**
* Shows atlas selection dialog
Expand Down Expand Up @@ -235,6 +243,14 @@ class DISPSHARED_EXPORT FwdSettingsView : public AbstractView
*/
void doForwardComputation();

//=========================================================================================================
/**
* Emit this signal whenever a forward computation is supposed to be triggered.
*
* @param[in] iNCluster Number of desired sources in the clustered source space.
*/
void clusterNumberChanged(int iNCluster);

};

//=============================================================================================================
Expand Down
5 changes: 0 additions & 5 deletions src/libraries/fwd/computeFwd/compute_fwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,11 +1867,6 @@ void ComputeFwd::initFwd()
m_listEegChs = QList<FiffChInfo>();
m_listCompChs = QList<FiffChInfo>();

if(!m_pSettings->compute_grad) {
m_meg_forward_grad = Q_NULLPTR;
m_eeg_forward_grad = Q_NULLPTR;
}

int iNMeg = 0;
int iNEeg = 0;
int iNComp = 0;
Expand Down
1 change: 1 addition & 0 deletions src/libraries/fwd/computeFwd/compute_fwd_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class FWDSHARED_EXPORT ComputeFwdSettings
bool do_all;
QStringList labels; /**< Compute the solution only for these labels. */
int nlabel;
int ncluster; /**< Number of sources desired in clustered solution. */

QString eeg_model_file; /**< File of EEG sphere model specifications. */
QString eeg_model_name; /**< Name of the EEG model to use. */
Expand Down
Loading