47 #include <fiff/fiff_evoked.h>
68 #include <QGuiApplication>
79 using namespace FSLIB;
100 int main(
int argc,
char *argv[])
102 QGuiApplication a(argc, argv);
107 QString method(
"MNE");
110 int averageIterator = 1;
112 bool doRawTrialLocalization =
false;
121 QString data_location(
"D:/Dropbox/Masterarbeit DB/Messdaten/EEG/2014_02_07_Lorenz_Esch_007");
131 QFile t_fileFwd(
"D:/Dropbox/Masterarbeit DB/Messdaten/Forward solutions/Lorenz-140128-Duke128-fwd.fif");
135 SurfaceSet t_surfSet(
"D:/Dropbox/Masterarbeit DB/Messdaten/Forward solutions/surface/lh.white",
"D:/Dropbox/Masterarbeit DB/Messdaten/Forward solutions/surface/rh.white");
138 AnnotationSet t_annotationSet(
"D:/Dropbox/Masterarbeit DB/Messdaten/Forward solutions/atlas/lh.aparc.a2009s.annot",
"D:/Dropbox/Masterarbeit DB/Messdaten/Forward solutions/atlas/rh.aparc.a2009s.annot");
356 QString t_sRawFileNameRel (
"/Processed/filtered/EEG_data_001_involuntary_right_opposing_filtered_07_40_raw.fif");
357 QString t_sCovFileNameRel (
"/Processed/covariance/EEG_data_001_base_filtered_07_40_raw-cov.fif");
358 QString t_sEventFileNameRel(
"/Processed/events/EEG_data_001_involuntary_right_opposing_150_raw-eve.fif");
359 QString t_sStcFileNameRel (
"/Processed/stc/SourceLoc_involuntary_right_opposing_filtered_07_40_Averaged_");
360 QString t_sAvrFileNameRel (
"/Processed/averaged/EEG_data_001_involuntary_right_opposing_filtered_07_40_Averaged_");
412 QFile t_fileRaw(t_sRawFileNameRel.prepend(data_location));
413 QFile t_fileCov(t_sCovFileNameRel.prepend(data_location));
414 QString t_sEventName = t_sEventFileNameRel.prepend(data_location);
419 fiff_int_t dest_comp = 0;
420 bool keep_comp =
false;
421 bool pick_all =
false;
434 picks.resize(raw.info.nchan);
436 for(k = 0; k < raw.info.nchan; ++k)
444 bool want_meg =
false;
445 bool want_eeg =
true;
446 bool want_stim =
false;
448 picks = raw.info.pick_types(want_meg, want_eeg, want_stim, include, raw.info.bads);
451 QStringList ch_names;
452 for(k = 0; k < picks.cols(); ++k)
453 ch_names << raw.info.ch_names[picks(0,k)];
458 if (raw.info.projs.size() == 0)
459 printf(
"No projector specified for these data\n");
465 for (k = 0; k < raw.info.projs.size(); ++k)
466 raw.info.projs[k].active =
true;
468 printf(
"%d projection items activated\n",raw.info.projs.size());
473 fiff_int_t nproj = raw.info.make_projector(raw.proj);
477 printf(
"The projection vectors do not apply to these channels\n");
481 printf(
"Created an SSP operator (subspace dimension = %d)\n",nproj);
488 qint32 current_comp = raw.info.get_current_comp();
489 if (current_comp > 0)
490 printf(
"Current compensation grade : %d\n",current_comp);
493 dest_comp = current_comp;
495 if (current_comp != dest_comp)
497 qDebug() <<
"This part needs to be debugged";
500 raw.info.set_current_comp(dest_comp);
501 printf(
"Appropriate compensator added to change to grade %d.\n",dest_comp);
505 printf(
"Could not make the compensator\n");
515 if (t_sEventName.size() == 0)
517 p = t_fileRaw.fileName().indexOf(
".fif");
520 t_sEventName = t_fileRaw.fileName().replace(p, 4,
"-eve.fif");
524 printf(
"Raw file name does not end properly\n");
528 t_EventFile.setFileName(t_sEventName);
530 printf(
"Events read from %s\n",t_sEventName.toUtf8().constData());
537 p = t_fileRaw.fileName().indexOf(
".fif");
540 t_EventFile.setFileName(t_sEventName);
543 printf(
"Error while read events.\n");
546 printf(
"Binary event file %s read\n",t_sEventName.toUtf8().constData());
553 printf(
"Text file %s is not supported jet.\n",t_sEventName.toUtf8().constData());
561 MatrixXi selected = MatrixXi::Zero(1, events.rows());
562 for (p = 0; p < events.rows(); ++p)
564 if (events(p,1) == 0 && events(p,2) == event)
566 selected(0,count) = p;
570 selected.conservativeResize(1, count);
572 printf(
"%d matching events found\n",count);
575 printf(
"No desired events found.\n");
579 fiff_int_t event_samp, from, to;
588 for (p = 0; p < count; ++p)
593 event_samp = events(selected(p),0);
594 from = event_samp + tmin*raw.info.sfreq;
595 to = event_samp + floor(tmax*raw.info.sfreq + 0.5);
599 if(raw.read_raw_segment(epoch->
epoch, timesDummy, from, to, picks))
603 times.resize(1, to-from+1);
604 for (qint32 i = 0; i < times.cols(); ++i)
605 times(0, i) = ((float)(from-event_samp+i)) / raw.info.sfreq;
608 epoch->
event = event;
609 epoch->
tmin = ((float)(from)-(float)(raw.first_samp))/raw.info.sfreq;
610 epoch->
tmax = ((
float)(to)-(
float)(raw.first_samp))/raw.info.sfreq;
616 printf(
"Can't read the event data segments");
623 printf(
"Read %d epochs, %d samples each.\n",data.size(),(qint32)data[0]->epoch.cols());
626 std::cout << data[0]->epoch.block(0,0,10,10) << std::endl;
627 qDebug() << data[0]->epoch.rows() <<
" x " << data[0]->epoch.cols();
629 std::cout << times.block(0,0,1,10) << std::endl;
630 qDebug() << times.rows() <<
" x " << times.cols();
641 if(count<averageIterator)
642 averageIterator = count;
644 VectorXi vecSel(count/averageIterator);
647 for(i=0; i<count/averageIterator; i++)
648 vecSel[i] = i*averageIterator;
651 t_sAvrFileNameRel.append(t_sAvrFileNameRel.number(tmin));
652 t_sAvrFileNameRel.append(
"_");
653 t_sAvrFileNameRel.append(t_sAvrFileNameRel.number(tmax));
654 t_sAvrFileNameRel.append(
"_");
655 t_sAvrFileNameRel.append(t_sAvrFileNameRel.number(i));
656 t_sAvrFileNameRel.append(
".fif");
657 QString t_sAvrFileName = t_sAvrFileNameRel.prepend(data_location);
659 t_sStcFileNameRel.append(t_sStcFileNameRel.number(tmin));
660 t_sStcFileNameRel.append(
"_");
661 t_sStcFileNameRel.append(t_sStcFileNameRel.number(tmax));
662 t_sStcFileNameRel.append(
"_");
663 t_sStcFileNameRel.append(t_sStcFileNameRel.number(i));
664 t_sStcFileNameRel.append(
"_");
665 t_sStcFileNameRel.append(method.append(
".stc"));
666 QString t_sFileNameStc = t_sStcFileNameRel.prepend(data_location);
668 std::cout <<
"Select following epochs to average:\n" << vecSel << std::endl;
669 raw.info.filename = QString(
"");
670 FiffEvoked evoked = data.
average(raw.info, tmin*raw.info.sfreq, floor(tmax*raw.info.sfreq + 0.5), vecSel);
673 QFile m_fileOut(t_sAvrFileName);
677 fiff_int_t first = 0;
678 m_pOutfid->write_int(FIFF_FIRST_SAMPLE, &first);
681 Eigen::MatrixXd eData = evoked.
data;
682 Eigen::MatrixXd matValue = MatrixXd::Zero(eData.rows()+10, eData.cols());
684 matValue.block(0,0,128,matValue.cols()) = evoked.
data;
686 m_pOutfid->write_raw_buffer(matValue, m_cals);
688 m_pOutfid->finish_writing_raw();
694 QString t_sFileNameClusteredInv(
"");
697 for(qint32 i = 0; i < argc; ++i)
699 if(strcmp(argv[i],
"-snr") == 0 || strcmp(argv[i],
"--snr") == 0)
702 snr = atof(argv[i+1]);
704 else if(strcmp(argv[i],
"-method") == 0 || strcmp(argv[i],
"--method") == 0)
707 method = QString::fromUtf8(argv[i+1]);
709 else if(strcmp(argv[i],
"-inv") == 0 || strcmp(argv[i],
"--inv") == 0)
712 t_sFileNameClusteredInv = QString::fromUtf8(argv[i+1]);
714 else if(strcmp(argv[i],
"-stc") == 0 || strcmp(argv[i],
"--stc") == 0)
717 t_sFileNameStc = QString::fromUtf8(argv[i+1]);
721 double lambda2 = 1.0 / pow(snr, 2);
724 qDebug() <<
"Start calculation with: SNR" << snr <<
"; Lambda" << lambda2 <<
"; Method" << method <<
"; stc:" << t_sFileNameStc;
733 noise_cov = noise_cov.regularize(evoked.
info, 0.05, 0.05, 0.1,
true);
747 QFile wrtFWD (
"./mne_x_plugins/resources/tmsi/fwd_clustered.txt");
748 wrtFWD.open(QIODevice::WriteOnly | QIODevice::Text);
749 QTextStream out(&wrtFWD);
752 VectorXi vertno_left = t_clusteredFwd.
src[0].vertno;
753 VectorXi vertno_right = t_clusteredFwd.
src[1].vertno;
755 out<<
"Vertno Left Hemi:"<<endl<<endl;
756 for(
int i=0; i<vertno_left.rows(); i++)
757 out<<vertno_left[i]<<endl;
759 out<<endl<<
"Vertno right Hemi:"<<endl<<endl;
760 for(
int i=0; i<vertno_right.rows(); i++)
761 out<<vertno_right[i]<<endl;
764 VectorXi labelIds_left = t_annotationSet[0].getLabelIds();
765 out<<endl<<endl<<
"labelIds_left:"<<endl<<endl;
766 for(
int i=0; i<labelIds_left.rows(); i++)
767 out<<labelIds_left[i]<<endl;
769 VectorXi labelIds_right = t_annotationSet[1].getLabelIds();
770 out<<endl<<endl<<
"labelIds_right:"<<endl<<endl;
771 for(
int i=0; i<labelIds_right.rows(); i++)
772 out<<labelIds_right[i]<<endl;
776 QFile wrtFWD_28_left (
"./mne_x_plugins/resources/tmsi/fwd_clustered_postcentral_28_Left.txt");
777 wrtFWD_28_left.open(QIODevice::WriteOnly | QIODevice::Text);
778 QTextStream out_28_left(&wrtFWD_28_left);
780 QFile wrtFWD_29_left (
"./mne_x_plugins/resources/tmsi/fwd_clustered_precentral_29_Left.txt");
781 wrtFWD_29_left.open(QIODevice::WriteOnly | QIODevice::Text);
782 QTextStream out_29_left(&wrtFWD_29_left);
784 QFile wrtFWD_45_left (
"./mne_x_plugins/resources/tmsi/fwd_clustered_central_45_Left.txt");
785 wrtFWD_45_left.open(QIODevice::WriteOnly | QIODevice::Text);
786 QTextStream out_45_left(&wrtFWD_45_left);
788 QVector<int> interestingRows_Left;
789 out<<endl<<endl<<
"interestingRows_Left (matlab syntax):"<<endl<<endl;
790 for(
int i=0; i<vertno_left.rows() ; i++)
793 if(labelIds_left[vertno_left[i]] == 9221140)
795 interestingRows_Left.push_back(i);
796 out<<
"Region 28 - G_postcentral: "<<i+1<<endl;
797 out_28_left<<i+1<<endl;
801 if(labelIds_left[vertno_left[i]] == 11832380)
803 interestingRows_Left.push_back(i);
804 out<<
"Region 29 - G_precentral: "<<i+1<<endl;
805 out_29_left<<i+1<<endl;
809 if(labelIds_left[vertno_left[i]] == 660701)
811 interestingRows_Left.push_back(i);
812 out<<
"Region 45 - S_central: "<<i+1<<endl;
813 out_45_left<<i+1<<endl;
818 QFile wrtFWD_28_right (
"./mne_x_plugins/resources/tmsi/fwd_clustered_postcentral_28_Right.txt");
819 wrtFWD_28_right.open(QIODevice::WriteOnly | QIODevice::Text);
820 QTextStream out_28_right(&wrtFWD_28_right);
822 QFile wrtFWD_29_right (
"./mne_x_plugins/resources/tmsi/fwd_clustered_precentral_29_Right.txt");
823 wrtFWD_29_right.open(QIODevice::WriteOnly | QIODevice::Text);
824 QTextStream out_29_right(&wrtFWD_29_right);
826 QFile wrtFWD_45_right (
"./mne_x_plugins/resources/tmsi/fwd_clustered_central_45_Right.txt");
827 wrtFWD_45_right.open(QIODevice::WriteOnly | QIODevice::Text);
828 QTextStream out_45_right(&wrtFWD_45_right);
830 QVector<int> interestingRows_Right;
831 out<<endl<<endl<<
"interestingRows_Right (matlab syntax):"<<endl<<endl;
832 for(
int i=0; i<vertno_right.rows() ; i++)
835 if(labelIds_right[vertno_right[i]] == 9221140)
837 interestingRows_Right.push_back(i);
838 out<<
"Region 28 - G_postcentral: "<<i+1+vertno_left.rows()<<endl;
839 out_28_right<<i+1<<endl;
843 if(labelIds_right[vertno_right[i]] == 11832380)
845 interestingRows_Right.push_back(i);
846 out<<
"Region 29 - G_precentral: "<<i+1+vertno_left.rows()<<endl;
847 out_29_right<<i+1<<endl;
851 if(labelIds_right[vertno_right[i]] == 660701)
853 interestingRows_Right.push_back(i);
854 out<<
"Region 45 - S_central: "<<i+1+vertno_left.rows()<<endl;
855 out_45_right<<i+1<<endl;
859 wrtFWD_28_left.close();
860 wrtFWD_29_left.close();
861 wrtFWD_45_left.close();
862 wrtFWD_28_right.close();
863 wrtFWD_29_right.close();
864 wrtFWD_45_right.close();
880 if(!t_sFileNameClusteredInv.isEmpty())
882 QFile t_fileClusteredInverse(t_sFileNameClusteredInv);
883 inverse_operator.write(t_fileClusteredInverse);
890 MinimumNorm minimumNorm(inverse_operator, lambda2, method);
898 if(doRawTrialLocalization)
902 for(
int i = 0; i<data.size(); i++)
904 QString file_trial_stc_orig = t_sFileNameStc;
906 QString file_trial_stc_tmp = t_sFileNameStc;
907 QString stc_trial_sub_folder = file_trial_stc_tmp.remove(0,t_sFileNameStc.indexOf(
"/So")+1);
909 stc_trial_sub_folder.remove(
".stc");
910 stc_trial_sub_folder.append(
"-raw-trials/");
912 QString dirCheck = data_location;
913 dirCheck.append(
"/Processed/stc/");
914 dirCheck.append(stc_trial_sub_folder);
916 if(QDir(dirCheck).exists() ==
false)
917 QDir().mkdir(dirCheck);
919 stc_trial_sub_folder.prepend(
"stc/");
921 file_trial_stc_orig.replace(
"stc/", stc_trial_sub_folder);
925 temp.append(temp.number(i));
928 file_trial_stc_orig.replace(
".stc",temp);
930 sourceEstimate_trial = minimumNorm.calculateInverse(data[i]->epoch,0,1/info.
sfreq);
932 if(sourceEstimate_trial.
isEmpty())
935 if(!t_sFileNameStc.isEmpty())
937 QFile t_fileTrialStc(file_trial_stc_orig);
938 sourceEstimate_trial.
write(t_fileTrialStc);
944 std::cout <<
"\nsourceEstimate:\n" << sourceEstimate.
data.block(0,0,10,10) << std::endl;
945 std::cout <<
"time\n" << sourceEstimate.
times.block(0,0,1,10) << std::endl;
946 std::cout <<
"timeMin\n" << sourceEstimate.
times[0] << std::endl;
947 std::cout <<
"timeMax\n" << sourceEstimate.
times[sourceEstimate.
times.size()-1] << std::endl;
948 std::cout <<
"time step\n" << sourceEstimate.
tstep << std::endl;
960 std::cout <<
"Clustered ForwardSolution" << t_clusteredFwd.
sol->data.block(0,0,10,10) << std::endl;
963 if(!t_sFileNameStc.isEmpty())
965 QFile t_fileClusteredStc(t_sFileNameStc);
966 sourceEstimate.
write(t_fileClusteredStc);
986 QList<Label> t_qListLabels;
987 QList<RowVector4i> t_qListRGBAs;
990 t_annotationSet.toLabels(t_surfSet, t_qListLabels, t_qListRGBAs);
992 InverseView view(minimumNorm.getSourceSpace(), t_qListLabels, t_qListRGBAs);
994 if (view.stereoType() != QGLView::RedCyanAnaglyph)
995 view.camera()->setEyeSeparation(0.3f);
996 QStringList args = QCoreApplication::arguments();
997 int w_pos = args.indexOf(
"-width");
998 int h_pos = args.indexOf(
"-height");
999 if (w_pos >= 0 && h_pos >= 0)
1002 int w = args.at(w_pos + 1).toInt(&ok);
1005 qWarning() <<
"Could not parse width argument:" << args;
1008 int h = args.at(h_pos + 1).toInt(&ok);
1011 qWarning() <<
"Could not parse height argument:" << args;
1018 view.resize(800, 600);
1023 view.pushSourceEstimate(sourceEstimate);
static bool make_compensator(const FiffInfo &info, fiff_int_t from, fiff_int_t to, FiffCtfComp &ctf_comp, bool exclude_comp_chs=false)
FIFF measurement file information.
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
MNEEpochDataList class declaration.
static bool read_events(QIODevice &p_IODevice, MatrixXi &eventlist)
QSharedPointer< FiffStream > SPtr
FIFF raw measurement data.
Minimum norm class declaration.
bool write(QIODevice &p_IODevice)
FiffNamedMatrix::SDPtr sol
static FiffStream::SPtr start_writing_raw(QIODevice &p_IODevice, const FiffInfo &info, RowVectorXd &cals, MatrixXi sel=defaultMatrixXi)
Surface class declaration.
MNESourceEstimate class declaration.
FiffEvoked average(FiffInfo &p_info, fiff_int_t first, fiff_int_t last, VectorXi sel=defaultVectorXi, bool proj=false)
QSharedPointer< MNEEpochData > SPtr
AnnotationSet class declaration.
InverseView class declaration.
MNEMath class declaration.
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...
A hemisphere set of surfaces.