Skip to content

Commit

Permalink
MAINT: convert other algorithm plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbmotta committed Jan 26, 2022
1 parent fd77c90 commit 70b0677
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ NeuronalConnectivity::NeuronalConnectivity()

NeuronalConnectivity::~NeuronalConnectivity()
{
if(this->isRunning()) {
if(m_bProcessOutput) {
stop();
}
}
Expand Down Expand Up @@ -195,7 +195,8 @@ void NeuronalConnectivity::unload()
bool NeuronalConnectivity::start()
{
//Start thread
QThread::start();
m_bProcessOutput = true;
m_OutputProcessingThread = std::thread(&NeuronalConnectivity::run, this);

return true;
}
Expand All @@ -206,8 +207,11 @@ bool NeuronalConnectivity::stop()
{
m_pRtConnectivity->restart();

requestInterruption();
wait(500);
m_bProcessOutput = false;

if(m_OutputProcessingThread.joinable()){
m_OutputProcessingThread.join();
}

m_bPluginControlWidgetsInit = false;

Expand Down Expand Up @@ -510,13 +514,13 @@ void NeuronalConnectivity::run()
break;
}
m_mutex.unlock();
msleep(100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

int skip_count = 0;
Network network;

while(!isInterruptionRequested()) {
while(m_bProcessOutput) {
//Do processing after skip count has reached limit
if((skip_count % m_iDownSample) == 0) {
if(m_pCircularBuffer->pop(network)) {
Expand Down Expand Up @@ -562,7 +566,7 @@ void NeuronalConnectivity::onMetricChanged(const QString& sMetric)

m_sConnectivityMethods = QStringList() << sMetric;
m_connectivitySettings.setConnectivityMethods(m_sConnectivityMethods);
if(m_pRtConnectivity && this->isRunning()) {
if(m_pRtConnectivity && m_bProcessOutput) {
m_pRtConnectivity->restart();
m_pRtConnectivity->append(m_connectivitySettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#include <connectivity/connectivitysettings.h>
#include <connectivity/network/network.h>

#include <thread>

//=============================================================================================================
// QT INCLUDES
//=============================================================================================================
Expand Down Expand Up @@ -272,6 +274,9 @@ class NEURONALCONNECTIVITYSHARED_EXPORT NeuronalConnectivity : public SCSHAREDLI
Eigen::RowVectorXi m_vecPicks; /**< The picked data channels. */

CONNECTIVITYLIB::Network m_currentConnectivityResult; /**< The current connectivity result.*/

std::thread m_OutputProcessingThread;
std::atomic_bool m_bProcessOutput;
};
} // NAMESPACE

Expand Down
16 changes: 10 additions & 6 deletions applications/mne_scan/plugins/noisereduction/noisereduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ NoiseReduction::NoiseReduction()

NoiseReduction::~NoiseReduction()
{
if(this->isRunning()) {
if(m_bProcessOutput) {
stop();
}
}
Expand Down Expand Up @@ -159,8 +159,11 @@ bool NoiseReduction::start()

bool NoiseReduction::stop()
{
requestInterruption();
wait(500);
m_bProcessOutput = false;

if(m_OutputProcessingThread.joinable()){
m_OutputProcessingThread.join();
}

m_iMaxFilterTapSize = -1;

Expand Down Expand Up @@ -224,7 +227,8 @@ void NoiseReduction::update(SCMEASLIB::Measurement::SPtr pMeasurement)
if(m_iMaxFilterTapSize == -1) {
m_iMaxFilterTapSize = pRTMSA->getMultiSampleArray().first().cols();
initPluginControlWidgets();
QThread::start();
m_bProcessOutput = true;
m_OutputProcessingThread = std::thread(&NoiseReduction::run, this);
}

for(unsigned char i = 0; i < pRTMSA->getMultiSampleArray().size(); ++i) {
Expand Down Expand Up @@ -345,7 +349,7 @@ void NoiseReduction::run()
MatrixXd matData;
QScopedPointer<RTPROCESSINGLIB::FilterOverlapAdd> pRtFilter(new RTPROCESSINGLIB::FilterOverlapAdd());

while(!isInterruptionRequested()) {
while(m_bProcessOutput) {
// Get the current data
if(m_pCircularBuffer->pop(matData)) {
m_mutex.lock();
Expand Down Expand Up @@ -409,7 +413,7 @@ void NoiseReduction::run()
m_mutex.unlock();

//Send the data to the connected plugins and the display
if(!isInterruptionRequested()) {
if(m_bProcessOutput) {
m_pNoiseReductionOutput->measurementData()->setValue(matData);
}
}
Expand Down
4 changes: 4 additions & 0 deletions applications/mne_scan/plugins/noisereduction/noisereduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

#include <scShared/Plugins/abstractalgorithm.h>

#include <thread>

//=============================================================================================================
// QT INCLUDES
//=============================================================================================================
Expand Down Expand Up @@ -263,6 +265,8 @@ class NOISEREDUCTIONSHARED_EXPORT NoiseReduction : public SCSHAREDLIB::AbstractA
SCSHAREDLIB::PluginInputData<SCMEASLIB::RealTimeMultiSampleArray>::SPtr m_pNoiseReductionInput; /**< The RealTimeMultiSampleArray of the NoiseReduction input.*/
SCSHAREDLIB::PluginOutputData<SCMEASLIB::RealTimeMultiSampleArray>::SPtr m_pNoiseReductionOutput; /**< The RealTimeMultiSampleArray of the NoiseReduction output.*/

std::thread m_OutputProcessingThread;
std::atomic_bool m_bProcessOutput;
signals:
};
} // NAMESPACE
Expand Down
32 changes: 18 additions & 14 deletions applications/mne_scan/plugins/rtcmne/rtcmne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ RtcMne::~RtcMne()
{
m_future.waitForFinished();

if(this->isRunning()) {
if(m_bProcessOutput) {
stop();
}
}
Expand Down Expand Up @@ -313,16 +313,20 @@ bool RtcMne::calcFiffInfo()

bool RtcMne::start()
{
QThread::start();
m_bProcessOutput = true;
m_OutputProcessingThread = std::thread(&RtcMne::run, this);
return true;
}

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

bool RtcMne::stop()
{
requestInterruption();
wait(500);
m_bProcessOutput = false;

if(m_OutputProcessingThread.joinable()){
m_OutputProcessingThread.join();
}

m_qListCovChNames.clear();
m_bEvokedInput = false;
Expand Down Expand Up @@ -369,7 +373,7 @@ void RtcMne::updateRTFS(SCMEASLIB::Measurement::SPtr pMeasurement)
m_qMutex.unlock();

// update inverse operator
if(this->isRunning() && m_pRtInvOp) {
if(m_bProcessOutput && m_pRtInvOp) {
m_pRtInvOp->setFwdSolution(m_pFwd);
m_pRtInvOp->append(*m_pNoiseCov);
}
Expand All @@ -386,7 +390,7 @@ void RtcMne::updateRTMSA(SCMEASLIB::Measurement::SPtr pMeasurement)
if(m_pFwd) {
QSharedPointer<RealTimeMultiSampleArray> pRTMSA = pMeasurement.dynamicCast<RealTimeMultiSampleArray>();

if(pRTMSA && this->isRunning()) {
if(pRTMSA && m_bProcessOutput) {
//Fiff Information of the RTMSA
m_qMutex.lock();
if(!m_pFiffInfoInput) {
Expand All @@ -400,7 +404,7 @@ void RtcMne::updateRTMSA(SCMEASLIB::Measurement::SPtr pMeasurement)
initPluginControlWidgets();
}

if(this->isRunning()) {
if(m_bProcessOutput) {
// Check for artifacts
QMap<QString,double> mapReject;
mapReject.insert("eog", 150e-06);
Expand Down Expand Up @@ -433,7 +437,7 @@ void RtcMne::updateRTC(SCMEASLIB::Measurement::SPtr pMeasurement)
if(m_pFwd) {
QSharedPointer<RealTimeCov> pRTC = pMeasurement.dynamicCast<RealTimeCov>();

if(pRTC && this->isRunning()) {
if(pRTC && m_bProcessOutput) {
// Init Real-Time inverse estimator
if(!m_pRtInvOp && m_pFiffInfo && m_pFwd) {
m_pRtInvOp = RtInvOp::SPtr(new RtInvOp(m_pFiffInfo, m_pFwd));
Expand All @@ -446,7 +450,7 @@ void RtcMne::updateRTC(SCMEASLIB::Measurement::SPtr pMeasurement)
m_qListCovChNames = pRTC->getValue()->names;
}

if(this->isRunning() && m_pRtInvOp){
if(m_bProcessOutput && m_pRtInvOp){
m_pNoiseCov = pRTC->getValue();
m_pRtInvOp->append(*m_pNoiseCov);
}
Expand All @@ -467,7 +471,7 @@ void RtcMne::updateRTE(SCMEASLIB::Measurement::SPtr pMeasurement)
initPluginControlWidgets();
}

if(!this->isRunning() || !lResponsibleTriggerTypes.contains(m_sAvrType)) {
if(!m_bProcessOutput || !lResponsibleTriggerTypes.contains(m_sAvrType)) {
return;
}

Expand All @@ -491,7 +495,7 @@ void RtcMne::updateRTE(SCMEASLIB::Measurement::SPtr pMeasurement)
initPluginControlWidgets();
}

if(this->isRunning()) {
if(m_bProcessOutput) {
for(int i = 0; i < pFiffEvokedSet->evoked.size(); ++i) {
if(pFiffEvokedSet->evoked.at(i).comment == m_sAvrType) {
// Store current evoked as member so we can dispatch it if the time pick by the user changed
Expand Down Expand Up @@ -551,7 +555,7 @@ void RtcMne::onTimePointValueChanged(int iTimePointMs)
m_iTimePointSps = m_pFiffInfoInput->sfreq * (float)iTimePointMs * 0.001f;
m_qMutex.unlock();

if(this->isRunning()) {
if(m_bProcessOutput) {
while(!m_pCircularEvokedBuffer->push(m_currentEvoked)) {
//Do nothing until the circular buffer is ready to accept new data again
}
Expand All @@ -565,7 +569,7 @@ void RtcMne::run()
{
// Wait for fiff info to arrive
while(!calcFiffInfo()) {
msleep(200);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}

// Init parameters
Expand All @@ -588,7 +592,7 @@ void RtcMne::run()
QStringList lChNamesInvOp;

// Start processing data
while(!isInterruptionRequested()) {
while(m_bProcessOutput) {
m_qMutex.lock();
iTimePointSps = m_iTimePointSps;
bEvokedInput = m_bEvokedInput;
Expand Down
5 changes: 5 additions & 0 deletions applications/mne_scan/plugins/rtcmne/rtcmne.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

#include <mne/mne_inverse_operator.h>

#include <thread>

//=============================================================================================================
// QT INCLUDES
//=============================================================================================================
Expand Down Expand Up @@ -265,6 +267,9 @@ class RTCMNESHARED_EXPORT RtcMne : public SCSHAREDLIB::AbstractAlgorithm

MNELIB::MNEInverseOperator m_invOp; /**< The inverse operator. */

std::thread m_OutputProcessingThread;
std::atomic_bool m_bProcessOutput;

signals:
void responsibleTriggerTypesChanged(const QStringList& lResponsibleTriggerTypes);

Expand Down
16 changes: 10 additions & 6 deletions applications/mne_scan/plugins/rtfwd/rtfwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ RtFwd::~RtFwd()
{
m_future.waitForFinished();

if(this->isRunning()) {
if(m_bProcessOutput) {
stop();
}
}
Expand Down Expand Up @@ -222,7 +222,8 @@ bool RtFwd::start()
stream->close();

//Start thread
QThread::start();
m_bProcessOutput = true;
m_OutputProcessingThread = std::thread(&RtFwd::run, this);

return true;
}
Expand All @@ -231,8 +232,11 @@ bool RtFwd::start()

bool RtFwd::stop()
{
requestInterruption();
wait(500);
m_bProcessOutput = false;

if(m_OutputProcessingThread.joinable()){
m_OutputProcessingThread.join();
}

m_bPluginControlWidgetsInit = false;

Expand Down Expand Up @@ -400,7 +404,7 @@ void RtFwd::run()
break;
}
m_mutex.unlock();
msleep(200);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}

m_mutex.lock();
Expand Down Expand Up @@ -429,7 +433,7 @@ void RtFwd::run()
bool bDoFwdComputation = false; // compute forward if requested
bool bIsInit = false; // only recompute if initial fwd solulion is calculated

while(!isInterruptionRequested()) {
while(m_bProcessOutput) {
m_mutex.lock();
bDoFwdComputation = m_bDoFwdComputation;
m_mutex.unlock();
Expand Down
5 changes: 5 additions & 0 deletions applications/mne_scan/plugins/rtfwd/rtfwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

#include <fiff/fiff_coord_trans.h>

#include <thread>

//=============================================================================================================
// QT INCLUDES
//=============================================================================================================
Expand Down Expand Up @@ -225,6 +227,9 @@ class RTFWDSHARED_EXPORT RtFwd : public SCSHAREDLIB::AbstractAlgorithm

SCSHAREDLIB::PluginOutputData<SCMEASLIB::RealTimeFwdSolution>::SPtr m_pRTFSOutput; /**< The fwd solution.*/

std::thread m_OutputProcessingThread;
std::atomic_bool m_bProcessOutput;

signals:
//=========================================================================================================
/**
Expand Down
14 changes: 9 additions & 5 deletions applications/mne_scan/plugins/writetofile/writetofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ WriteToFile::WriteToFile()

WriteToFile::~WriteToFile()
{
if(this->isRunning()) {
if(m_bProcessOutput) {
stop();
}
}
Expand Down Expand Up @@ -145,7 +145,8 @@ void WriteToFile::unload()

bool WriteToFile::start()
{
QThread::start();
m_bProcessOutput = true;
m_OutputProcessingThread = std::thread(&WriteToFile::run, this);

return true;
}
Expand All @@ -154,8 +155,11 @@ bool WriteToFile::start()

bool WriteToFile::stop()
{
requestInterruption();
wait();
m_bProcessOutput = false;

if(m_OutputProcessingThread.joinable()){
m_OutputProcessingThread.join();
}

m_bPluginControlWidgetsInit = false;

Expand Down Expand Up @@ -293,7 +297,7 @@ void WriteToFile::run()
MatrixXd matData;
qint32 size = 0;

while(!isInterruptionRequested()) {
while(m_bProcessOutput) {
if(m_pCircularBuffer) {
//pop matrix

Expand Down
Loading

0 comments on commit 70b0677

Please sign in to comment.