Skip to content

Commit

Permalink
ENH: Revised connectivity examples
Browse files Browse the repository at this point in the history
  • Loading branch information
johaenns authored and juangpc committed Nov 8, 2023
1 parent 588923a commit 0d92e66
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 28 deletions.
20 changes: 19 additions & 1 deletion src/examples/ex_connectivity/connectivitysettingsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,23 @@ class ConnectivitySettingsManager : public QObject
// Default frequency range
m_fFreqBandLow = 7.0f;
m_fFreqBandHigh = 13.0f;

//set coloring
mColor.insert("COR",Vector4i(90, 26, 100, 1));
mColor.insert("XCOR",Vector4i(255, 20, 80, 1));
mColor.insert("PLI",Vector4i(255, 255, 0, 1));
mColor.insert("COH",Vector4i(2, 89, 100, 1));
mColor.insert("IMAGCOH",Vector4i(50, 255, 48, 1));
mColor.insert("PLV",Vector4i(0, 255, 255, 1));
mColor.insert("WPLI",Vector4i(255, 0, 100, 1));
mColor.insert("USPLI",Vector4i(255, 89, 200, 1));
mColor.insert("DSWPLI",Vector4i(25, 10, 255, 1));
}

ConnectivitySettings m_settings;
RtConnectivity::SPtr m_pRtConnectivity;
QList<Network> m_networkData;
QMap<QString,Vector4i> mColor;

float m_fFreqBandLow;
float m_fFreqBandHigh;
Expand Down Expand Up @@ -203,7 +215,13 @@ class ConnectivitySettingsManager : public QObject

for(int i = 0; i < connectivityResults.size(); ++i) {
m_networkData[i].setFrequencyRange(m_fFreqBandLow, m_fFreqBandHigh);
//m_networkData[i].normalize();
m_networkData[i].normalize();

VisualizationInfo visInfo = m_networkData[i].getVisualizationInfo();
visInfo.sMethod = "Color";
visInfo.colNodes = mColor[m_networkData[i].getConnectivityMethod()];
visInfo.colEdges = mColor[m_networkData[i].getConnectivityMethod()];
m_networkData[i].setVisualizationInfo(visInfo);

// if(!m_networkData.isEmpty()) {
// Network network = m_networkData.first();
Expand Down
49 changes: 31 additions & 18 deletions src/examples/ex_connectivity/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,14 @@ int main(int argc, char *argv[])
mapReject,
exludeChs);
data.dropRejected();

QPair<float, float> pair(fTMin, 0.0f);
data.applyBaselineCorrection(pair);

// Average epochs. Do not use SSPs.
FiffEvoked evoked = data.average(raw.info,
0,
data.first()->epoch.cols()-1);
fTMin,
fTMax);

MNESourceEstimate sourceEstimateEvoked;
VectorXi vDataIndices;
Expand All @@ -285,7 +286,7 @@ int main(int argc, char *argv[])
picks.conservativeResize(picks.cols()+1);
picks(picks.cols()-1) = picksTmp(i);
}
} else if (sCoilType.contains("medg", Qt::CaseInsensitive)) {
} else if (sCoilType.contains("mag", Qt::CaseInsensitive)) {
picks = raw.info.pick_types(QString("mag"),false,false,QStringList(),exclude);
}

Expand Down Expand Up @@ -331,7 +332,7 @@ int main(int argc, char *argv[])
t_clusteredFwd = t_Fwd.cluster_forward_solution(tAnnotSet, 40);
} else {
t_clusteredFwd = t_Fwd;
}
}

MNEInverseOperator inverse_operator(raw.info,
t_clusteredFwd,
Expand All @@ -340,14 +341,15 @@ int main(int argc, char *argv[])
0.8f);

// Compute inverse solution
MinimumNorm minimumNorm(inverse_operator, 1.0 / pow(1.0, 2), method);
MinimumNorm minimumNorm(inverse_operator, lambda2, method);
minimumNorm.doInverseSetup(1, true);

picks = raw.info.pick_types(QString("all"),true,false,QStringList(),exclude);
data.pick_channels(picks);
for(int i = 0; i < data.size(); i++) {
sourceEstimate = minimumNorm.calculateInverse(data.at(i)->epoch,
evoked.times[0],
//0.0f,
1.0/raw.info.sfreq,
true);

Expand All @@ -364,7 +366,7 @@ int main(int argc, char *argv[])
pConnectivitySettingsManager->m_matEvokedSource = sourceEstimateEvoked.data;

//Generate network nodes and define ROIs
QList<Label> lLabels;
/*QList<Label> lLabels;
QList<RowVector4i> qListLabelRGBAs;
QStringList lWantedLabels;
Expand All @@ -388,7 +390,8 @@ int main(int argc, char *argv[])
//Generate node vertices based on picked labels
MatrixX3f matNodePositions = t_clusteredFwd.getSourcePositionsByLabel(lLabels, tSurfSetInflated);
pConnectivitySettingsManager->m_settings.setNodePositions(matNodePositions);
pConnectivitySettingsManager->m_settings.setNodePositions(matNodePositions);*/
pConnectivitySettingsManager->m_settings.setNodePositions(t_clusteredFwd, tSurfSetInflated);
}

pConnectivitySettingsManager->epochs = data;
Expand All @@ -399,13 +402,18 @@ int main(int argc, char *argv[])
pConnectivitySettingsManager->m_settings.setWindowType("hanning");

ConnectivitySettings::IntermediateTrialData connectivityData;
for(int i = 0; i < matDataList.size(); i++) {
connectivityData.matData.resize(vDataIndices.rows(),matDataList.at(i).cols()-samplesToCutOut);
int iNumTrials = matDataList.size();
for(int i = 0; i < iNumTrials; i++) {
/*connectivityData.matData.resize(vDataIndices.rows(),matDataList.at(i).cols()-samplesToCutOut);
for(int j = 0; j < vDataIndices.rows(); j++) {
// Only calculate connectivity for post stim
connectivityData.matData.row(j) = matDataList.at(i).row(vDataIndices(j)).segment(samplesToCutOut, matDataList.at(i).cols()-samplesToCutOut);
}
}*/
connectivityData.matData = matDataList.at(i).block(0,
samplesToCutOut,
matDataList.at(i).rows(),
matDataList.at(i).cols()-samplesToCutOut);

pConnectivitySettingsManager->m_settings.append(connectivityData);
pConnectivitySettingsManager->m_dataListOriginal.append(connectivityData);
Expand All @@ -419,22 +427,26 @@ int main(int argc, char *argv[])
pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::onConnectivityMetricChanged);

QObject::connect(tNetworkView.getConnectivitySettingsView().data(), &ConnectivitySettingsView::numberTrialsChanged,
pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::onNumberTrialsChanged);
//pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::onNumberTrialsChanged);
[&pConnectivitySettingsManager,&tNetworkView,iNumTrials] (int a) {
tNetworkView.getConnectivitySettingsView()->setNumberTrials(qMin(iNumTrials,a));
pConnectivitySettingsManager.data()->onNumberTrialsChanged(qMin(iNumTrials,a));});


QObject::connect(tNetworkView.getConnectivitySettingsView().data(), &ConnectivitySettingsView::freqBandChanged,
pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::onFreqBandChanged);

QObject::connect(pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::newConnectivityResultAvailable,
[&](const QString& a, const QString& b, const Network& c) {if(NetworkTreeItem* pNetworkTreeItem = tNetworkView.addData(a,b,c)) {
//pNetworkTreeItem->setThresholds(QVector3D(0.9,0.95,1.0));
pNetworkTreeItem->setThresholds(QVector3D(0.9,0.95,1.0));
}});

QPointer<TfSettingsView> pTfSettingsView = new TfSettingsView();
//QPointer<TfSettingsView> pTfSettingsView = new TfSettingsView();
QList<QWidget*> lWidgets;
lWidgets << pTfSettingsView;
/*lWidgets << pTfSettingsView;
QObject::connect(pTfSettingsView.data(), &TfSettingsView::numberTrialRowChanged,
pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::plotTimeCourses);
pConnectivitySettingsManager.data(), &ConnectivitySettingsManager::plotTimeCourses);*/

//Read and show sensor helmets
if(!bDoSourceLoc && sChType.contains("meg", Qt::CaseInsensitive)) {
Expand Down Expand Up @@ -463,7 +475,7 @@ int main(int argc, char *argv[])
t_sensorSurfaceVV,
raw.info.bads);
} else {
QPointer<MinimumNormSettingsView> pMinimumNormSettingsView = new MinimumNormSettingsView("EX_CONNECTIVITY");
QPointer<MinimumNormSettingsView> pMinimumNormSettingsView = new MinimumNormSettingsView("EX_CONNECTIVITY", sSourceLocMethod);
lWidgets << pMinimumNormSettingsView;

QObject::connect(pMinimumNormSettingsView.data(), &MinimumNormSettingsView::timePointChanged,
Expand Down Expand Up @@ -512,8 +524,9 @@ int main(int argc, char *argv[])
}

tNetworkView.setQuickControlWidgets(lWidgets);
tNetworkView.getConnectivitySettingsView()->setNumberTrials(200);
pConnectivitySettingsManager->onNumberTrialsChanged(200);
tNetworkView.getConnectivitySettingsView()->setNumberTrials(iNumTrials);
pConnectivitySettingsManager->onNumberTrialsChanged(iNumTrials);
pConnectivitySettingsManager->onFreqBandChanged(tNetworkView.getConnectivitySettingsView()->getLowerFreq(),tNetworkView.getConnectivitySettingsView()->getUpperFreq());

return a.exec();
}
35 changes: 28 additions & 7 deletions src/examples/ex_connectivity_comparison/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,14 @@ int main(int argc, char *argv[])
QCommandLineOption tMinOption("tmin", "The time minimum value for averaging in seconds relativ to the trigger onset.", "value", "-0.1");
QCommandLineOption tMaxOption("tmax", "The time maximum value for averaging in seconds relativ to the trigger onset.", "value", "0.4");
QCommandLineOption eventsFileOption("eve", "Path to the event <file>.", "file", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis_raw-eve.fif");
QCommandLineOption rawFileOption("raw", "Path to the raw <file>.", "file", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif");
QCommandLineOption rawFileOption("fileIn", "Path to the raw <file>.", "file", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif");
QCommandLineOption subjectOption("subj", "Selected <subject> (for source level usage only).", "subject", "sample");
QCommandLineOption subjectPathOption("subjDir", "Selected <subjectPath> (for source level usage only).", "subjectPath", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/subjects");
QCommandLineOption fwdOption("fwd", "Path to forwad solution <file> (for source level usage only).", "file", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif");
QCommandLineOption covFileOption("cov", "Path to the covariance <file> (for source level usage only).", "file", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis-cov.fif");
QCommandLineOption trialOption("ntrials", "The number of averaged trials.", "value", "200");
QCommandLineOption freqMinOption("freqmin", "The lower frequency bound for connectivity computation.", "value", "7.0");
QCommandLineOption freqMaxOption("freqmax", "The upper frequency bound for connectivity computation.", "value", "13.0");
parser.addOption(annotOption);
parser.addOption(subjectOption);
parser.addOption(subjectPathOption);
Expand All @@ -155,6 +158,9 @@ int main(int argc, char *argv[])
parser.addOption(rawFileOption);
parser.addOption(tMinOption);
parser.addOption(tMaxOption);
parser.addOption(trialOption);
parser.addOption(freqMinOption);
parser.addOption(freqMaxOption);

parser.process(a);

Expand All @@ -173,6 +179,9 @@ int main(int argc, char *argv[])
float fTMax = parser.value(tMaxOption).toFloat();
double dSnr = parser.value(snrOption).toDouble();
int iEvent = parser.value(evokedIndexOption).toInt();
int iNumTrials = parser.value(trialOption).toInt();
double dFreqMin = parser.value(freqMinOption).toDouble();
double dFreqMax = parser.value(freqMaxOption).toDouble();

bool bDoSourceLoc = false;
bool bDoClust = false;
Expand Down Expand Up @@ -226,7 +235,9 @@ int main(int argc, char *argv[])

// Read the epochs and reject bad epochs. Note, that SSPs are automatically applied to the data if MNE::setup_compensators was called beforehand.
QMap<QString,double> mapReject;
mapReject.insert("eog", 300e-06);
mapReject.insert("eog", 100e-06);
mapReject.insert("grad", 3000e-13);
mapReject.insert("mag", 3.5e-12);

MNEEpochDataList data = MNEEpochDataList::readEpochs(raw,
events,
Expand All @@ -239,8 +250,8 @@ int main(int argc, char *argv[])
data.applyBaselineCorrection(pair);

FiffEvoked evoked = data.average(raw.info,
0,
data.first()->epoch.cols()-1);
fTMin,
fTMax);
MNESourceEstimate sourceEstimateEvoked;

QStringList exclude;
Expand Down Expand Up @@ -300,7 +311,7 @@ int main(int argc, char *argv[])

// Cluster forward solution;
if(bDoClust) {
t_clusteredFwd = t_Fwd.cluster_forward_solution(tAnnotSet, 200);
t_clusteredFwd = t_Fwd.cluster_forward_solution(tAnnotSet, 40);
} else {
t_clusteredFwd = t_Fwd;
}
Expand Down Expand Up @@ -340,7 +351,8 @@ int main(int argc, char *argv[])
// Compute the connectivity estimates for the methods to be compared
conSettings.setConnectivityMethods(QStringList() << "COH" << "COR" << "XCOR" << "PLI" << "IMAGCOH" << "PLV" << "WPLI" << "USPLI" << "DSWPLI");

for(int i = 0; i < matDataList.size(); i++) {
iNumTrials = qMin(iNumTrials, matDataList.size());
for(int i = 0; i < iNumTrials; i++) {
// Only calculate connectivity for post stim
int samplesToCutOut = abs(fTMin*raw.info.sfreq);
conSettings.append(matDataList.at(i).block(0,
Expand All @@ -366,7 +378,7 @@ int main(int argc, char *argv[])
mColor.insert("DSWPLI",Vector4i(25, 10, 255, 1));

for(int j = 0; j < lNetworks.size(); ++j) {
lNetworks[j].setFrequencyRange(7.0f, 13.0f);
lNetworks[j].setFrequencyRange(dFreqMin, dFreqMax);
lNetworks[j].normalize();
VisualizationInfo visInfo = lNetworks.at(j).getVisualizationInfo();
visInfo.sMethod = "Color";
Expand All @@ -386,6 +398,9 @@ int main(int argc, char *argv[])
lNetworkTreeItems.at(j)->setThresholds(QVector3D(0.9,0.95,1.0));
}

tNetworkView.getConnectivitySettingsView()->setNumberTrials(iNumTrials);
tNetworkView.getConnectivitySettingsView().data()->setFrequencyBand(dFreqMin,dFreqMax);

//Read and show sensor helmets
if(!bDoSourceLoc && sChType.contains("meg", Qt::CaseInsensitive)) {
QFile t_filesensorSurfaceVV(QCoreApplication::applicationDirPath() + "/../resources/general/sensorSurfaces/306m_rt.fif");
Expand Down Expand Up @@ -414,5 +429,11 @@ int main(int argc, char *argv[])
}
}

QObject::connect(tNetworkView.getConnectivitySettingsView().data(), &ConnectivitySettingsView::numberTrialsChanged,
[&tNetworkView,iNumTrials] () {tNetworkView.getConnectivitySettingsView().data()->setNumberTrials(iNumTrials);});

QObject::connect(tNetworkView.getConnectivitySettingsView().data(), &ConnectivitySettingsView::freqBandChanged,
[&tNetworkView,dFreqMin,dFreqMax] () {tNetworkView.getConnectivitySettingsView().data()->setFrequencyBand(dFreqMin,dFreqMax);});

return a.exec();
}
7 changes: 5 additions & 2 deletions src/examples/ex_connectivity_performance/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ int main(int argc, char *argv[])
#endif

qInstallMessageHandler(customMessageHandler);
QCoreApplication a(argc, argv);

m_sCurrentDir = QCoreApplication::applicationDirPath();

// printf("globalInstance()->maxThreadCount(): %d\n",QThreadPool::globalInstance()->maxThreadCount());
// QThreadPool::globalInstance()->setMaxThreadCount(24);
Expand All @@ -163,7 +166,7 @@ int main(int argc, char *argv[])
qint64 iTime = 0;
timer.start();

QString sRaw = QCoreApplication::applicationDirPath() + "../resources/data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif";
QString sRaw = QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif";
MatrixXd matDataOrig, matData;
MatrixXd times;
QFile t_fileRaw(sRaw);
Expand Down Expand Up @@ -229,7 +232,7 @@ int main(int argc, char *argv[])
m_iNumberSamples = lNumberSamples.at(j);

//Create new folder
m_sCurrentDir = QCoreApplication::applicationDirPath() + QString("./connectivity_performance_%1_%2_%3/%4/%5_%6_%7").arg(QHostInfo::localHostName()).arg(AbstractMetric::m_iNumberBinAmount).arg(iStorageModeActive).arg(sConnectivityMethodList.at(i)).arg(QString::number(lNumberChannels.at(k))).arg(QString::number(lNumberSamples.at(j))).arg(QString::number(lNumberTrials.at(l)));
m_sCurrentDir = QCoreApplication::applicationDirPath() + QString("/connectivity_performance_%1_%2_%3/%4/%5_%6_%7").arg(QHostInfo::localHostName()).arg(AbstractMetric::m_iNumberBinAmount).arg(iStorageModeActive).arg(sConnectivityMethodList.at(i)).arg(QString::number(lNumberChannels.at(k))).arg(QString::number(lNumberSamples.at(j))).arg(QString::number(lNumberTrials.at(l)));
QDir().mkpath(m_sCurrentDir);

//Write basic information to file
Expand Down
8 changes: 8 additions & 0 deletions src/libraries/disp/viewers/connectivitysettingsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ void ConnectivitySettingsView::setNumberTrials(int iNumberTrials)

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

void ConnectivitySettingsView::setFrequencyBand(double dFreqLow, double dFreqHigh)
{
m_pUi->m_spinBox_freqLow->setValue(dFreqLow);
m_pUi->m_spinBox_freqHigh->setValue(dFreqHigh);
}

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

QString ConnectivitySettingsView::getConnectivityMetric()
{
return m_pUi->m_comboBox_method->currentText();
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/disp/viewers/connectivitysettingsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ class DISPSHARED_EXPORT ConnectivitySettingsView : public AbstractView
*/
void setNumberTrials(int iNumberTrials);


//=========================================================================================================
/**
* Sets the frequency band.
*
* @param[in] dFreqLow The lower frequency.
* @param[in] dFreqHigh The upper frequency.
*/
void setFrequencyBand(double dFreqLow, double dFreqHigh);

//=========================================================================================================
/**
* Get the current connectivity metric.
Expand Down
Loading

0 comments on commit 0d92e66

Please sign in to comment.