MNE-CPP  beta 1.0
mne_forwardsolution.h
Go to the documentation of this file.
1 //=============================================================================================================
37 #ifndef MNE_FORWARDSOLUTION_H
38 #define MNE_FORWARDSOLUTION_H
39 
40 //*************************************************************************************************************
41 //=============================================================================================================
42 // INCLUDES
43 //=============================================================================================================
44 
45 #include "mne_global.h"
46 #include "mne_sourcespace.h"
47 
48 
49 //*************************************************************************************************************
50 //=============================================================================================================
51 // FIFF INCLUDES
52 //=============================================================================================================
53 
54 #include <utils/mnemath.h>
55 #include <utils/kmeans.h>
56 
57 #include <fs/annotationset.h>
58 
59 #include <fiff/fiff_constants.h>
60 #include <fiff/fiff_coord_trans.h>
61 #include <fiff/fiff_types.h>
62 #include <fiff/fiff_info_base.h>
63 
64 
65 //*************************************************************************************************************
66 //=============================================================================================================
67 // Eigen INCLUDES
68 //=============================================================================================================
69 
70 #include <Eigen/Core>
71 #include <Eigen/SVD>
72 #include <Eigen/Sparse>
73 #include <unsupported/Eigen/KroneckerProduct>
74 
75 
76 //*************************************************************************************************************
77 //=============================================================================================================
78 // STL INCLUDES
79 //=============================================================================================================
80 
81 #include <math.h>
82 
83 
84 //*************************************************************************************************************
85 //=============================================================================================================
86 // QT INCLUDES
87 //=============================================================================================================
88 
89 #include <QFile>
90 #include <QSharedPointer>
91 #include <QDataStream>
92 
93 
94 
95 //*************************************************************************************************************
96 //=============================================================================================================
97 // DEFINE NAMESPACE MNELIB
98 //=============================================================================================================
99 
100 namespace MNELIB
101 {
102 
103 
104 //*************************************************************************************************************
105 //=============================================================================================================
106 // USED NAMESPACES
107 //=============================================================================================================
108 
109 using namespace Eigen;
110 using namespace FSLIB;
111 using namespace UTILSLIB;
112 using namespace FIFFLIB;
113 
114 //=========================================================================================================
119 {
120  VectorXi roiIdx;
121  MatrixXd ctrs;
122  VectorXd sumd;
123  MatrixXd D;
125  qint32 iLabelIdxOut;
126 };
127 
128 
129 //=========================================================================================================
134 {
135  MatrixXd matRoiG;
136  MatrixXd matRoiGWhitened;
139  MatrixXd matRoiGOrig;
140 // MatrixXd matRoiGOrigWhitened; /**< Whitened region gain matrix sensors x sources(x,y,z)*/
141 
142  qint32 nClusters;
144  VectorXi idcs;
145  qint32 iLabelIdxIn;
146  QString sDistMeasure;
148  RegionDataOut cluster() const
149  {
150  QString t_sDistMeasure;
151  if(sDistMeasure.isEmpty())
152  t_sDistMeasure = QString("cityblock");
153  else
154  t_sDistMeasure = sDistMeasure;
155 
156  // Kmeans Reduction
157  RegionDataOut p_RegionDataOut;
158 
159  KMeans t_kMeans(t_sDistMeasure, QString("sample"), 5);
160 
161  if(bUseWhitened)
162  {
163  t_kMeans.calculate(this->matRoiGWhitened, this->nClusters, p_RegionDataOut.roiIdx, p_RegionDataOut.ctrs, p_RegionDataOut.sumd, p_RegionDataOut.D);
164 
165  MatrixXd newCtrs = MatrixXd::Zero(p_RegionDataOut.ctrs.rows(), p_RegionDataOut.ctrs.cols());
166  for(qint32 c = 0; c < p_RegionDataOut.ctrs.rows(); ++c)
167  {
168  qint32 num = 0;
169 
170  for(qint32 idx = 0; idx < p_RegionDataOut.roiIdx.size(); ++idx)
171  {
172  if(c == p_RegionDataOut.roiIdx[idx])
173  {
174  newCtrs.row(c) += this->matRoiG.row(idx); //just take whitened to get indeces calculate centroids using the original matrix
175  ++num;
176  }
177  }
178 
179  if(num > 0)
180  newCtrs.row(c) /= num;
181  }
182  p_RegionDataOut.ctrs = newCtrs; //Replace whitened with original
183  }
184  else
185  t_kMeans.calculate(this->matRoiG, this->nClusters, p_RegionDataOut.roiIdx, p_RegionDataOut.ctrs, p_RegionDataOut.sumd, p_RegionDataOut.D);
186 
187  p_RegionDataOut.iLabelIdxOut = this->iLabelIdxIn;
188 
189  return p_RegionDataOut;
190  }
191 
192 };
193 
194 
195 const static FiffCov defaultCov;
196 const static FiffInfo defaultInfo;
197 static MatrixXd defaultD;
198 
199 
200 //=============================================================================================================
206 class MNESHARED_EXPORT MNEForwardSolution
207 {
208 public:
209  typedef QSharedPointer<MNEForwardSolution> SPtr;
210  typedef QSharedPointer<const MNEForwardSolution> ConstSPtr;
212  //=========================================================================================================
217 
218  //=========================================================================================================
230  MNEForwardSolution(QIODevice &p_IODevice, bool force_fixed = false, bool surf_ori = false, const QStringList& include = defaultQStringList, const QStringList& exclude = defaultQStringList, bool bExcludeBads = false);
231 
232  //=========================================================================================================
238  MNEForwardSolution(const MNEForwardSolution &p_MNEForwardSolution);
239 
240  //=========================================================================================================
245 
246  //=========================================================================================================
250  void clear();
251 
252  //=========================================================================================================
266  MNEForwardSolution cluster_forward_solution(const AnnotationSet &p_AnnotationSet, qint32 p_iClusterSize, MatrixXd& p_D = defaultD, const FiffCov &p_pNoise_cov = defaultCov, const FiffInfo &p_pInfo = defaultInfo, QString p_sMethod = "cityblock") const;
267 
268  //=========================================================================================================
276  FiffCov compute_orient_prior(float loose = 0.2);
277 
278  //=========================================================================================================
292  static FiffCov compute_depth_prior(const MatrixXd &Gain, const FiffInfo &gain_info, bool is_fixed_ori, double exp = 0.8, double limit = 10.0, const MatrixXd &patch_areas = defaultConstMatrixXd, bool limit_depth_chs = false);
293 
294  //=========================================================================================================
300  inline bool isClustered() const;
301 
302  //=========================================================================================================
308  inline bool isEmpty() const;
309 
310  //=========================================================================================================
316  inline bool isFixedOrient() const;
317 
318  //=========================================================================================================
329  MNEForwardSolution pick_channels(const QStringList& include = defaultQStringList, const QStringList& exclude = defaultQStringList) const;
330 
331  //=========================================================================================================
339  MNEForwardSolution pick_regions(const QList<Label> &p_qListLabels) const;
340 
341  //=========================================================================================================
354  MNEForwardSolution pick_types(bool meg, bool eeg, const QStringList& include = defaultQStringList, const QStringList& exclude = defaultQStringList) const;
355 
356  //=========================================================================================================
369  void prepare_forward(const FiffInfo &p_info, const FiffCov &p_noise_cov, bool p_pca, FiffInfo &p_outFwdInfo, MatrixXd &gain, FiffCov &p_outNoiseCov, MatrixXd &p_outWhitener, qint32 &p_outNumNonZero) const;
370 
371 // //=========================================================================================================
372 // /**
373 // * Prepares a forward solution, Bad channels, after clustering etc ToDo...
374 // *
375 // * @param [in] p_FiffInfo Fif measurement info
376 // *
377 // */
378 // void prepare_forward(const FiffInfo& p_FiffInfo)
379 // {
380 // QStringList fwdChNames = this->sol->row_names;
381 // QStringList chNames;
382 // for(qint32 i = 0; i < p_FiffInfo.ch_names.size(); ++i)
383 // {
384 // bool inBads = false;
385 // bool inFwd = false;
386 
387 // for(qint32 j = 0; j < p_FiffInfo.bads.size(); ++j)
388 // {
389 // if(QString::compare(p_FiffInfo.bads[j], p_FiffInfo.ch_names[i]) == 0)
390 // {
391 // inBads = true;
392 // break;
393 // }
394 // }
395 
396 // for(qint32 j = 0; j < fwdChNames.size(); ++j)
397 // {
398 // if(QString::compare(fwdChNames[j], p_FiffInfo.ch_names[i]) == 0)
399 // {
400 // inFwd = true;
401 // break;
402 // }
403 // }
404 
405 // if(!inBads && inFwd)
406 // chNames.append(p_FiffInfo.ch_names[i]);
407 // }
408 
409 // qint32 nchan = chNames.size();
410 // }
411 
412  //=========================================================================================================
416  VectorXi tripletSelection(const VectorXi& p_vecIdxSelection) const
417  {
418  MatrixXi triSelect = p_vecIdxSelection.transpose().replicate(3,1).array() * 3;//repmat((p_vecIdxSelection - 1) * 3 + 1, 3, 1);
419  triSelect.row(1).array() += 1;
420  triSelect.row(2).array() += 2;
421  VectorXi retTriSelect(triSelect.cols()*3);
422  for(int i = 0; i < triSelect.cols(); ++i)
423  retTriSelect.block(i*3,0,3,1) = triSelect.col(i);
424  return retTriSelect;
425  } // tripletSelection
426 
427 
428  //=========================================================================================================
444  static bool read(QIODevice& p_IODevice, MNEForwardSolution& fwd, bool force_fixed = false, bool surf_ori = false, const QStringList& include = defaultQStringList, const QStringList& exclude = defaultQStringList, bool bExcludeBads = true);
445 
446  //ToDo readFromStream
447 
448  //=========================================================================================================
457  MNEForwardSolution reduce_forward_solution(qint32 p_iNumDipoles, MatrixXd& p_D) const;
458 
459  //=========================================================================================================
466  static void restrict_gain_matrix(MatrixXd &G, const FiffInfo &info);
467 
468  //=========================================================================================================
472  void to_fixed_ori();
473 
474  //=========================================================================================================
483  friend std::ostream& operator<<(std::ostream& out, const MNELIB::MNEForwardSolution &p_MNEForwardSolution);
484 
485 
486 
487 private:
488 
489  //=========================================================================================================
501  static bool read_one(FiffStream* p_pStream, const FiffDirTree& p_Node, MNEForwardSolution& one);
502 
503 public:
505  fiff_int_t source_ori;
506  bool surf_ori;
507  fiff_int_t coord_frame;
508  fiff_int_t nsource;
509  fiff_int_t nchan;
514  MatrixX3f source_rr;
515  MatrixX3f source_nn;
516 };
517 
518 //*************************************************************************************************************
519 //=============================================================================================================
520 // INLINE DEFINITIONS
521 //=============================================================================================================
522 
524 {
525  return src[0].isClustered();
526 }
527 
528 
529 //*************************************************************************************************************
530 
531 inline bool MNEForwardSolution::isEmpty() const
532 {
533  return this->nchan <= 0;
534 }
535 
536 
537 //*************************************************************************************************************
538 
540 {
541  return this->source_ori == FIFFV_MNE_FIXED_ORI;
542 }
543 
544 
545 //*************************************************************************************************************
546 
547 inline std::ostream& operator<<(std::ostream& out, const MNELIB::MNEForwardSolution &p_MNEForwardSolution)
548 {
549  out << "#### MNE Forward Solution ####\n";
550 
551  out << "\n source_ori: " << p_MNEForwardSolution.source_ori << std::endl;
552  out << "\n coord_frame: " << p_MNEForwardSolution.coord_frame << std::endl;
553  out << "\n nsource: " << p_MNEForwardSolution.nsource << std::endl;
554  out << "\n nchan: " << p_MNEForwardSolution.nchan << std::endl;
555  out << "\n sol:\n\t" << *p_MNEForwardSolution.sol.data() << std::endl;
556  out << "\n sol_grad:\n\t" << *p_MNEForwardSolution.sol_grad.data() << std::endl;
557 
558  return out;
559 }
560 
561 } // NAMESPACE
562 
563 #endif // MNE_FORWARDSOLUTION_H
QSharedPointer< const MNEForwardSolution > ConstSPtr
FIFF measurement file information.
Definition: fiff_info.h:96
bool calculate(MatrixXd X, qint32 kClusters, VectorXi &idx, MatrixXd &C, VectorXd &sumD, MatrixXd &D)
Definition: kmeans.cpp:93
Source Space descritpion.
Old fiff_type declarations - replace them.
QSharedDataPointer< FiffNamedMatrix > SDPtr
QSharedPointer< MNEForwardSolution > SPtr
Fiff constants.
VectorXi tripletSelection(const VectorXi &p_vecIdxSelection) const
FiffNamedMatrix::SDPtr sol_grad
FiffInfoBase class declaration.
Annotation set.
Definition: annotationset.h:96
Directory tree structure.
Definition: fiff_dir_tree.h:80
covariance data
Definition: fiff_cov.h:94
FiffNamedMatrix::SDPtr sol
Definition: fiff.h:98
KMeans class declaration.
Coordinate transformation description.
MNESourceSpace class declaration.
K-Means Clustering.
Definition: kmeans.h:86
AnnotationSet class declaration.
MNEMath class declaration.
light measurement info
FIFF File I/O routines.
Definition: fiff_stream.h:129
FiffCoordTrans class declaration.