MNE-CPP  beta 1.0
rapmusictoolbox.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "rapmusictoolbox.h"
42 
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // QT INCLUDES
49 //=============================================================================================================
50 
51 #include <QtCore/QtPlugin>
52 #include <QtConcurrent>
53 #include <QDebug>
54 
55 
56 //*************************************************************************************************************
57 //=============================================================================================================
58 // USED NAMESPACES
59 //=============================================================================================================
60 
61 using namespace RapMusicToolboxPlugin;
62 using namespace FIFFLIB;
63 using namespace XMEASLIB;
64 
65 
66 //*************************************************************************************************************
67 //=============================================================================================================
68 // DEFINE MEMBER METHODS
69 //=============================================================================================================
70 
72 : m_bIsRunning(false)
73 , m_bReceiveData(false)
74 , m_bProcessData(false)
75 , m_bFinishedClustering(false)
76 , m_qFileFwdSolution("./MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif")
77 , m_sAtlasDir("./MNE-sample-data/subjects/sample/label")
78 , m_sSurfaceDir("./MNE-sample-data/subjects/sample/surf")
79 , m_iNumAverages(10)
80 , m_iDownSample(4)
81 {
82 
83 }
84 
85 
86 //*************************************************************************************************************
87 
89 {
90  if(this->isRunning())
91  stop();
92 }
93 
94 
95 //*************************************************************************************************************
96 
97 QSharedPointer<IPlugin> RapMusicToolbox::clone() const
98 {
99  QSharedPointer<RapMusicToolbox> pRapMusicToolboxClone(new RapMusicToolbox());
100  return pRapMusicToolboxClone;
101 }
102 
103 
104 //*************************************************************************************************************
105 //=============================================================================================================
106 // Creating required display instances and set configurations
107 //=============================================================================================================
108 
110 {
111  // Inits
112  m_pFwd = MNEForwardSolution::SPtr(new MNEForwardSolution(m_qFileFwdSolution));
113  m_pAnnotationSet = AnnotationSet::SPtr(new AnnotationSet(m_sAtlasDir+"/lh.aparc.a2009s.annot", m_sAtlasDir+"/rh.aparc.a2009s.annot"));
114  m_pSurfaceSet = SurfaceSet::SPtr(new SurfaceSet(m_sSurfaceDir+"/lh.white", m_sSurfaceDir+"/rh.white"));
115 
116  // Input
117  m_pRTEInput = PluginInputData<RealTimeEvoked>::create(this, "RapMusic Toolbox RTE In", "RapMusic Toolbox real-time evoked input data");
118  connect(m_pRTEInput.data(), &PluginInputConnector::notify, this, &RapMusicToolbox::updateRTE, Qt::DirectConnection);
119  m_inputConnectors.append(m_pRTEInput);
120 
121  // Output
122  m_pRTSEOutput = PluginOutputData<RealTimeSourceEstimate>::create(this, "MNEOut", "RapMusic Toolbox output data");
123  m_outputConnectors.append(m_pRTSEOutput);
124  m_pRTSEOutput->data()->setName("Real-Time Source Estimate");
125  m_pRTSEOutput->data()->setAnnotSet(m_pAnnotationSet);
126  m_pRTSEOutput->data()->setSurfSet(m_pSurfaceSet);
127 
128  // start clustering
129  QFuture<void> future = QtConcurrent::run(this, &RapMusicToolbox::doClustering);
130 
131 }
132 
133 
134 //*************************************************************************************************************
135 
137 {
138 
139 }
140 
141 
142 //*************************************************************************************************************
143 
144 void RapMusicToolbox::calcFiffInfo()
145 {
146  QMutexLocker locker(&m_qMutex);
147  if(m_pFiffInfoEvoked && m_pFiffInfoForward)
148  {
149  qDebug() << "Fiff Infos available";
150 
151  m_qListPickChannels.clear();
152  foreach (const QString &ch, m_pFiffInfoForward->ch_names)
153  {
154  if(m_pFiffInfoEvoked->ch_names.contains(ch))
155  m_qListPickChannels << ch;
156  }
157 
158  RowVectorXi sel = m_pFiffInfoEvoked->pick_channels(m_qListPickChannels);
159 
160  m_pFiffInfo = QSharedPointer<FiffInfo>(new FiffInfo(m_pFiffInfoEvoked->pick_info(sel)));
161  }
162 
163 }
164 
165 
166 //*************************************************************************************************************
167 
168 void RapMusicToolbox::doClustering()
169 {
170  emit clusteringStarted();
171 
172  m_qMutex.lock();
173  m_bFinishedClustering = false;
174  m_pClusteredFwd = MNEForwardSolution::SPtr(new MNEForwardSolution(m_pFwd->cluster_forward_solution(*m_pAnnotationSet.data(), 40)));
175  m_qMutex.unlock();
176 
177  finishedClustering();
178 }
179 
180 
181 //*************************************************************************************************************
182 
183 void RapMusicToolbox::finishedClustering()
184 {
185  m_qMutex.lock();
186  m_bFinishedClustering = true;
187  m_pFiffInfoForward = QSharedPointer<FiffInfoBase>(new FiffInfoBase(m_pClusteredFwd->info));
188  m_qMutex.unlock();
189 
190  emit clusteringFinished();
191 }
192 
193 
194 //*************************************************************************************************************
195 
197 {
198  //Check if the thread is already or still running. This can happen if the start button is pressed immediately after the stop button was pressed. In this case the stopping process is not finished yet but the start process is initiated.
199  if(this->isRunning())
200  QThread::wait();
201 
202  if(m_bFinishedClustering)
203  {
204  m_bIsRunning = true;
205  QThread::start();
206  return true;
207  }
208  else
209  return false;
210 }
211 
212 
213 //*************************************************************************************************************
214 
216 {
217  //Check if the thread is already or still running. This can happen if the start button is pressed immediately after the stop button was pressed. In this case the stopping process is not finished yet but the start process is initiated.
218  if(this->isRunning())
219  QThread::wait();
220 
221  m_qMutex.lock();
222  m_bIsRunning = false;
223 
224  if(m_bProcessData) // Only clear if buffers have been initialised
225  m_qVecFiffEvoked.clear();
226 
227  // Stop filling buffers with data from the inputs
228  m_bProcessData = false;
229 
230  m_bReceiveData = false;
231 
232  m_qMutex.unlock();
233 
234  return true;
235 }
236 
237 
238 //*************************************************************************************************************
239 
241 {
242  return _IAlgorithm;
243 }
244 
245 
246 //*************************************************************************************************************
247 
249 {
250  return "RAP MUSIC Toolbox";
251 }
252 
253 
254 //*************************************************************************************************************
255 
257 {
258  RapMusicToolboxSetupWidget* setupWidget = new RapMusicToolboxSetupWidget(this);//widget is later distroyed by CentralWidget - so it has to be created everytime new
259 
260  if(!m_bFinishedClustering)
261  setupWidget->setClusteringState();
262 
265 
266  return setupWidget;
267 }
268 
269 
270 //*************************************************************************************************************
271 
273 {
274  QSharedPointer<RealTimeEvoked> pRTE = pMeasurement.dynamicCast<RealTimeEvoked>();
275 
276  QMutexLocker locker(&m_qMutex);
277  //MEG
278  if(pRTE && m_bReceiveData)
279  {
280  //Fiff Information of the evoked
281  if(!m_pFiffInfoEvoked)
282  m_pFiffInfoEvoked = QSharedPointer<FiffInfo>(new FiffInfo(pRTE->getValue()->info));
283 
284  if(m_bProcessData)
285  m_qVecFiffEvoked.push_back(pRTE->getValue()->pick_channels(m_qListPickChannels));
286  }
287 }
288 
289 
290 //*************************************************************************************************************
291 
293 {
294  qint32 numDipolePairs = 1;
295 
296  //
297  // start receiving data
298  //
299  m_qMutex.lock();
300  m_bReceiveData = true;
301  m_qMutex.unlock();
302 
303  //
304  // Read Fiff Info
305  //
306  while(true)
307  {
308  {
309  QMutexLocker locker(&m_qMutex);
310  if(m_pFiffInfo)
311  break;
312  }
313  calcFiffInfo();
314  msleep(10);// Wait for fiff Info
315  }
316 
317  m_pPwlRapMusic.reset();
318 
319  m_pPwlRapMusic = RapMusic::SPtr(new RapMusic(*m_pClusteredFwd, false, numDipolePairs));
320 
321  //
322  // start processing data
323  //
324  m_qMutex.lock();
325  m_bProcessData = true;
326  m_qMutex.unlock();
327 
328  qint32 skip_count = 0;
329  while(true)
330  {
331  {
332  QMutexLocker locker(&m_qMutex);
333  if(!m_bIsRunning)
334  break;
335  }
336 
337  m_qMutex.lock();
338  qint32 t_evokedSize = m_qVecFiffEvoked.size();
339  m_qMutex.unlock();
340 
341  if(t_evokedSize > 0)
342  {
343  if(m_pPwlRapMusic && ((skip_count % 10) == 0))
344  {
345  m_qMutex.lock();
346  FiffEvoked t_fiffEvoked = m_qVecFiffEvoked[0];
347  m_pPwlRapMusic->setStcAttr(t_fiffEvoked.data.cols()/4.0,0.0);
348  m_qVecFiffEvoked.pop_front();
349  m_qMutex.unlock();
350 
351  qDebug() << "m_pRapMusic->calculateInverse";
352 
353  MNESourceEstimate sourceEstimate = m_pPwlRapMusic->calculateInverse(t_fiffEvoked);
354  m_pRTSEOutput->data()->setValue(sourceEstimate);
355  }
356  else
357  {
358  m_qMutex.lock();
359  m_qVecFiffEvoked.pop_front();
360  m_qMutex.unlock();
361  }
362  ++skip_count;
363  }
364  }
365 }
FIFF measurement file information.
Definition: fiff_info.h:96
OutputConnectorList m_outputConnectors
Definition: IPlugin.h:222
QSharedPointer< RapMusic > SPtr
Definition: rapmusic.h:112
void updateRTE(XMEASLIB::NewMeasurement::SPtr pMeasurement)
QSharedPointer< MNEForwardSolution > SPtr
evoked data
Definition: fiff_evoked.h:91
virtual IPlugin::PluginType getType() const
Annotation set.
Definition: annotationset.h:96
QSharedPointer< SurfaceSet > SPtr
Definition: surfaceset.h:86
Contains the declaration of the RapMusicToolbox class.
Definition: fiff.h:98
QSharedPointer< NewMeasurement > SPtr
virtual QSharedPointer< IPlugin > clone() const
QSharedPointer< AnnotationSet > SPtr
Definition: annotationset.h:99
The RealTimeMultiSampleArrayNew class is the base class of every RealTimeMultiSampleArrayNew Measurem...
static QSharedPointer< PluginOutputData< T > > create(IPlugin *parent, const QString &name, const QString &descr)
The RapMusic class provides the RAP MUSIC Algorithm CPU implementation. ToDo: Paper references...
Definition: rapmusic.h:109
The DummySetupWidget class provides the DummyToolbox configuration window.
InputConnectorList m_inputConnectors
Definition: IPlugin.h:221
light measurement info
Contains the declaration of the RapMusicToolboxSetupWidget class.
A hemisphere set of surfaces.
Definition: surfaceset.h:83
static QSharedPointer< PluginInputData< T > > create(IPlugin *parent, const QString &name, const QString &descr)