/** * The function main marks the entry point of the program. * By default, main has the storage class extern. * * @param [in] argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. * @param [in] argv (argument vector) is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started. * @return the value that was set to exit() (which is 0 if exit() is called via quit()). */ int main(int argc, char *argv[]) { QApplication app(argc, argv); // Command Line Parser QCommandLineParser parser; parser.setApplicationDescription("Compute Inverse Powell RAP-MUSIC Example"); parser.addHelpOption(); QCommandLineOption fwdFileOption("fwd", "Path to forward solution <file>.", "file", "./MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif"); QCommandLineOption evokedFileOption("ave", "Path to evoked <file>.", "file", "./MNE-sample-data/MEG/sample/sample_audvis-ave.fif"); QCommandLineOption subjectDirectoryOption("subjDir", "Path to subject <directory>.", "directory", "./MNE-sample-data/subjects"); QCommandLineOption subjectOption("subj", "Selected <subject>.", "subject", "sample"); QCommandLineOption stcFileOption("stcOut", "Path to stc <file>, which is to be written.", "file", "");//"RapMusic.stc"); QCommandLineOption numDipolePairsOption("numDip", "<number> of dipole pairs to localize.", "number", "1"); QCommandLineOption doMovieOption("doMovie", "Create overlapping movie.", "doMovie", "false"); QCommandLineOption annotOption("annotType", "Annotation type <type>.", "type", "aparc.a2009s"); QCommandLineOption surfOption("surfType", "Surface type <type>.", "type", "orig"); parser.addOption(fwdFileOption); parser.addOption(evokedFileOption); parser.addOption(subjectDirectoryOption); parser.addOption(subjectOption); parser.addOption(stcFileOption); parser.addOption(numDipolePairsOption); parser.addOption(doMovieOption); parser.addOption(annotOption); parser.addOption(surfOption); parser.process(app); // Parse command line parameters QFile t_fileFwd(parser.value(fwdFileOption)); QFile t_fileEvoked(parser.value(evokedFileOption)); QString subject(parser.value(subjectOption)); QString subjectDir(parser.value(subjectDirectoryOption)); AnnotationSet t_annotationSet(subject, 2, parser.value(annotOption), subjectDir); SurfaceSet t_surfSet(subject, 2, parser.value(surfOption), subjectDir); QString t_sFileNameStc(parser.value(stcFileOption)); qint32 numDipolePairs = parser.value(numDipolePairsOption).toInt(); bool doMovie = false; if(parser.value(doMovieOption) == "false" || parser.value(doMovieOption) == "0") { doMovie = false; } else if(parser.value(doMovieOption) == "true" || parser.value(doMovieOption) == "1") { doMovie = true; } qDebug() << "Start calculation with stc:" << t_sFileNameStc; // Load data fiff_int_t setno = 0; QPair<QVariant, QVariant> baseline(QVariant(), 0); FiffEvoked evoked(t_fileEvoked, setno, baseline); if(evoked.isEmpty()) return 1; std::cout << "evoked first " << evoked.first << "; last " << evoked.last << std::endl; MNEForwardSolution t_Fwd(t_fileFwd); if(t_Fwd.isEmpty()) return 1; QStringList ch_sel_names = t_Fwd.info.ch_names; FiffEvoked pickedEvoked = evoked.pick_channels(ch_sel_names); // // Cluster forward solution; // MNEForwardSolution t_clusteredFwd = t_Fwd.cluster_forward_solution(t_annotationSet, 20);//40); // std::cout << "Size " << t_clusteredFwd.sol->data.rows() << " x " << t_clusteredFwd.sol->data.cols() << std::endl; // std::cout << "Clustered Fwd:\n" << t_clusteredFwd.sol->data.row(0) << std::endl; PwlRapMusic t_pwlRapMusic(t_clusteredFwd, false, numDipolePairs); int iWinSize = 200; if(doMovie) { t_pwlRapMusic.setStcAttr(iWinSize, 0.6); } MNESourceEstimate sourceEstimate = t_pwlRapMusic.calculateInverse(pickedEvoked); if(doMovie) { //Select only the activations once MatrixXd dataPicked(sourceEstimate.data.rows(), int(std::floor(sourceEstimate.data.cols()/iWinSize))); for(int i = 0; i < dataPicked.cols(); ++i) { dataPicked.col(i) = sourceEstimate.data.col(i*iWinSize); } sourceEstimate.data = dataPicked; } std::cout << "source estimated" << std::endl; if(sourceEstimate.isEmpty()) return 1; View3D::SPtr testWindow = View3D::SPtr(new View3D()); Data3DTreeModel::SPtr p3DDataModel = Data3DTreeModel::SPtr(new Data3DTreeModel()); testWindow->setModel(p3DDataModel); p3DDataModel->addSurfaceSet(parser.value(subjectOption), "HemiLRSet", t_surfSet, t_annotationSet); QList<BrainRTSourceLocDataTreeItem*> rtItemList = p3DDataModel->addSourceData(parser.value(subjectOption), "HemiLRSet", sourceEstimate, t_clusteredFwd); //Init some rt related values for right visual data for(int i = 0; i < rtItemList.size(); ++i) { rtItemList.at(i)->setLoopState(true); rtItemList.at(i)->setTimeInterval(17); rtItemList.at(i)->setNumberAverages(1); rtItemList.at(i)->setStreamingActive(true); rtItemList.at(i)->setNormalization(QVector3D(0.01,0.5,1.0)); rtItemList.at(i)->setVisualizationType("Annotation based"); rtItemList.at(i)->setColortable("Hot"); } testWindow->show(); Control3DWidget::SPtr control3DWidget = Control3DWidget::SPtr(new Control3DWidget()); control3DWidget->init(p3DDataModel, testWindow); control3DWidget->show(); if(!t_sFileNameStc.isEmpty()) { QFile t_fileClusteredStc(t_sFileNameStc); sourceEstimate.write(t_fileClusteredStc); } return app.exec(); }
/** * The function main marks the entry point of the program. * By default, main has the storage class extern. * * @param [in] argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. * @param [in] argv (argument vector) is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started. * @return the value that was set to exit() (which is 0 if exit() is called via quit()). */ int main(int argc, char *argv[]) { QApplication a(argc, argv); // Command Line Parser QCommandLineParser parser; parser.setApplicationDescription("Clustered Inverse Raw Example"); parser.addHelpOption(); QCommandLineOption inputOption("fileIn", "The input file <in>.", "in", "./MNE-sample-data/MEG/sample/sample_audvis_raw.fif"); QCommandLineOption surfOption("surfType", "Surface type <type>.", "type", "inflated"); QCommandLineOption annotOption("annotType", "Annotation type <type>.", "type", "aparc.a2009s"); QCommandLineOption subjectOption("subject", "Selected subject <subject>.", "subject", "sample"); QCommandLineOption subjectPathOption("subjectPath", "Selected subject path <subjectPath>.", "subjectPath", "./MNE-sample-data/subjects"); QCommandLineOption fwdOption("fwd", "Path to forwad solution <file>.", "file", "./MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif"); QCommandLineOption covFileOption("cov", "Path to the covariance <file>.", "file", "./MNE-sample-data/MEG/sample/sample_audvis-cov.fif"); QCommandLineOption evokedIndexOption("aveIdx", "The average <index> to choose from the average file.", "index", "1"); QCommandLineOption eventsFileOption("eve", "Path to the event <file>.", "file", "./MNE-sample-data/MEG/sample/sample_audvis_raw-eve.fif"); QCommandLineOption hemiOption("hemi", "Selected hemisphere <hemi>.", "hemi", "2"); QCommandLineOption methodOption("method", "Inverse estimation <method>, i.e., 'MNE', 'dSPM' or 'sLORETA'.", "method", "dSPM");//"MNE" | "dSPM" | "sLORETA" QCommandLineOption snrOption("snr", "The SNR value used for computation <snr>.", "snr", "1.0");//3.0;//0.1;//3.0; QCommandLineOption invFileOutOption("invOut", "Path to inverse <file>, which is to be written.", "file", ""); QCommandLineOption stcFileOutOption("stcOut", "Path to stc <file>, which is to be written.", "file", ""); QCommandLineOption keepCompOption("keepComp", "Keep compensators.", "keepComp", "false"); QCommandLineOption pickAllOption("pickAll", "Pick all channels.", "pickAll", "true"); QCommandLineOption destCompsOption("destComps", "<Destination> of the compensator which is to be calculated.", "destination", "0"); parser.addOption(inputOption); parser.addOption(surfOption); parser.addOption(annotOption); parser.addOption(subjectOption); parser.addOption(subjectPathOption); parser.addOption(fwdOption); parser.addOption(covFileOption); parser.addOption(evokedIndexOption); parser.addOption(eventsFileOption); parser.addOption(hemiOption); parser.addOption(methodOption); parser.addOption(snrOption); parser.addOption(invFileOutOption); parser.addOption(stcFileOutOption); parser.addOption(keepCompOption); parser.addOption(pickAllOption); parser.addOption(destCompsOption); parser.process(a); // Load files QFile t_fileFwd(parser.value(fwdOption)); QFile t_fileCov(parser.value(covFileOption)); QFile t_fileRaw(parser.value(inputOption)); QString t_sEventName = parser.value(eventsFileOption); SurfaceSet t_surfSet (parser.value(subjectOption), parser.value(hemiOption).toInt(), parser.value(surfOption), parser.value(subjectPathOption)); AnnotationSet t_annotationSet (parser.value(subjectOption), parser.value(hemiOption).toInt(), parser.value(annotOption), parser.value(subjectPathOption)); qint32 event = parser.value(evokedIndexOption).toInt(); float tmin = -0.2f; float tmax = 0.4f; bool keep_comp = false; if(parser.value(keepCompOption) == "false" || parser.value(keepCompOption) == "0") { keep_comp = false; } else if(parser.value(keepCompOption) == "true" || parser.value(keepCompOption) == "1") { keep_comp = true; } fiff_int_t dest_comp = parser.value(destCompsOption).toInt(); bool pick_all = false; if(parser.value(pickAllOption) == "false" || parser.value(pickAllOption) == "0") { pick_all = false; } else if(parser.value(pickAllOption) == "true" || parser.value(pickAllOption) == "1") { pick_all = true; } qint32 k, p; // // Setup for reading the raw data // FiffRawData raw(t_fileRaw); RowVectorXi picks; if (pick_all) { // // Pick all // picks.resize(raw.info.nchan); for(k = 0; k < raw.info.nchan; ++k) picks(k) = k; // } else { QStringList include; include << "STI 014"; bool want_meg = true; bool want_eeg = false; bool want_stim = false; // picks = Fiff::pick_types(raw.info, want_meg, want_eeg, want_stim, include, raw.info.bads); picks = raw.info.pick_types(want_meg, want_eeg, want_stim, include, raw.info.bads);//prefer member function } QStringList ch_names; for(k = 0; k < picks.cols(); ++k) ch_names << raw.info.ch_names[picks(0,k)]; // // Set up projection // if (raw.info.projs.size() == 0) printf("No projector specified for these data\n"); else { // // Activate the projection items // for (k = 0; k < raw.info.projs.size(); ++k) raw.info.projs[k].active = true; printf("%d projection items activated\n",raw.info.projs.size()); // // Create the projector // // fiff_int_t nproj = MNE::make_projector_info(raw.info, raw.proj); Using the member function instead fiff_int_t nproj = raw.info.make_projector(raw.proj); if (nproj == 0) { printf("The projection vectors do not apply to these channels\n"); } else { printf("Created an SSP operator (subspace dimension = %d)\n",nproj); } } // // Set up the CTF compensator // // qint32 current_comp = MNE::get_current_comp(raw.info); qint32 current_comp = raw.info.get_current_comp(); if (current_comp > 0) printf("Current compensation grade : %d\n",current_comp); if (keep_comp) dest_comp = current_comp; if (current_comp != dest_comp) { qDebug() << "This part needs to be debugged"; if(MNE::make_compensator(raw.info, current_comp, dest_comp, raw.comp)) { // raw.info.chs = MNE::set_current_comp(raw.info.chs,dest_comp); raw.info.set_current_comp(dest_comp); printf("Appropriate compensator added to change to grade %d.\n",dest_comp); } else { printf("Could not make the compensator\n"); return 0; } } // // Read the events // QFile t_EventFile; MatrixXi events; if (t_sEventName.size() == 0) { p = t_fileRaw.fileName().indexOf(".fif"); if (p > 0) { t_sEventName = t_fileRaw.fileName().replace(p, 4, "-eve.fif"); } else { printf("Raw file name does not end properly\n"); return 0; } // events = mne_read_events(t_sEventName); t_EventFile.setFileName(t_sEventName); MNE::read_events(t_EventFile, events); printf("Events read from %s\n",t_sEventName.toUtf8().constData()); } else { // // Binary file // p = t_fileRaw.fileName().indexOf(".fif"); if (p > 0) { t_EventFile.setFileName(t_sEventName); if(!MNE::read_events(t_EventFile, events)) { printf("Error while read events.\n"); return 0; } printf("Binary event file %s read\n",t_sEventName.toUtf8().constData()); } else { // // Text file // printf("Text file %s is not supported jet.\n",t_sEventName.toUtf8().constData()); // try // events = load(eventname); // catch // error(me,mne_omit_first_line(lasterr)); // end // if size(events,1) < 1 // error(me,'No data in the event file'); // end // // // // Convert time to samples if sample number is negative // // // for p = 1:size(events,1) // if events(p,1) < 0 // events(p,1) = events(p,2)*raw.info.sfreq; // end // end // // // // Select the columns of interest (convert to integers) // // // events = int32(events(:,[1 3 4])); // // // // New format? // // // if events(1,2) == 0 && events(1,3) == 0 // fprintf(1,'The text event file %s is in the new format\n',eventname); // if events(1,1) ~= raw.first_samp // error(me,'This new format event file is not compatible with the raw data'); // end // else // fprintf(1,'The text event file %s is in the old format\n',eventname); // // // // Offset with first sample // // // events(:,1) = events(:,1) + raw.first_samp; // end } } // // Select the desired events // qint32 count = 0; MatrixXi selected = MatrixXi::Zero(1, events.rows()); for (p = 0; p < events.rows(); ++p) { if (events(p,1) == 0 && events(p,2) == event) { selected(0,count) = p; ++count; } } selected.conservativeResize(1, count); if (count > 0) printf("%d matching events found\n",count); else { printf("No desired events found.\n"); return 0; } fiff_int_t event_samp, from, to; MatrixXd timesDummy; MNEEpochDataList data; MNEEpochData* epoch = NULL; MatrixXd times; for (p = 0; p < count; ++p) { // // Read a data segment // event_samp = events(selected(p),0); from = event_samp + tmin*raw.info.sfreq; to = event_samp + floor(tmax*raw.info.sfreq + 0.5); epoch = new MNEEpochData(); if(raw.read_raw_segment(epoch->epoch, timesDummy, from, to, picks)) { if (p == 0) { times.resize(1, to-from+1); for (qint32 i = 0; i < times.cols(); ++i) times(0, i) = ((float)(from-event_samp+i)) / raw.info.sfreq; } epoch->event = event; epoch->tmin = ((float)(from)-(float)(raw.first_samp))/raw.info.sfreq; epoch->tmax = ((float)(to)-(float)(raw.first_samp))/raw.info.sfreq; data.append(MNEEpochData::SPtr(epoch));//List takes ownwership of the pointer - no delete need } else { printf("Can't read the event data segments"); return 0; } } if(data.size() > 0) { printf("Read %d epochs, %d samples each.\n",data.size(),(qint32)data[0]->epoch.cols()); //DEBUG std::cout << data[0]->epoch.block(0,0,10,10) << std::endl; qDebug() << data[0]->epoch.rows() << " x " << data[0]->epoch.cols(); std::cout << times.block(0,0,1,10) << std::endl; qDebug() << times.rows() << " x " << times.cols(); } // Calculate the average // Option 1 - Random selection VectorXi vecSel(50); srand (time(NULL)); // initialize random seed for(qint32 i = 0; i < vecSel.size(); ++i) { qint32 val = rand() % count; vecSel(i) = val; } // //Option 3 - Take all epochs // VectorXi vecSel(data.size()); // for(qint32 i = 0; i < vecSel.size(); ++i) // { // vecSel(i) = i; // } // //Option 3 - Manual selection // VectorXi vecSel(20); // vecSel << 76, 74, 13, 61, 97, 94, 75, 71, 60, 56, 26, 57, 56, 0, 52, 72, 33, 86, 96, 67; std::cout << "Select following epochs to average:\n" << vecSel << std::endl; FiffEvoked evoked = data.average(raw.info, tmin*raw.info.sfreq, floor(tmax*raw.info.sfreq + 0.5), vecSel); //######################################################################################## // Source Estimate // // Settings // double snr = parser.value(snrOption).toDouble(); QString method(parser.value(methodOption)); QString t_sFileNameClusteredInv(parser.value(invFileOutOption)); QString t_sFileNameStc(parser.value(stcFileOutOption)); double lambda2 = 1.0 / pow(snr, 2); qDebug() << "Start calculation with: SNR" << snr << "; Lambda" << lambda2 << "; Method" << method << "; stc:" << t_sFileNameStc; // // Load data // MNEForwardSolution t_Fwd(t_fileFwd); if(t_Fwd.isEmpty()) return 1; FiffCov noise_cov(t_fileCov); // // regularize noise covariance // noise_cov = noise_cov.regularize(evoked.info, 0.05, 0.05, 0.1, true); // // Cluster forward solution; // MatrixXd D; MNEForwardSolution t_clusteredFwd = t_Fwd.cluster_forward_solution(t_annotationSet, 20, D, noise_cov, evoked.info); // // make an inverse operators // FiffInfo info = evoked.info; MNEInverseOperator inverse_operator(info, t_clusteredFwd, noise_cov, 0.2f, 0.8f); // // save clustered inverse // if(!t_sFileNameClusteredInv.isEmpty()) { QFile t_fileClusteredInverse(t_sFileNameClusteredInv); inverse_operator.write(t_fileClusteredInverse); } // // Compute inverse solution // MinimumNorm minimumNorm(inverse_operator, lambda2, method); #ifdef BENCHMARK // // Set up the inverse according to the parameters // minimumNorm.doInverseSetup(vecSel.size(),false); MNESourceEstimate sourceEstimate; QList<qint64> qVecElapsedTime; for(qint32 i = 0; i < 100; ++i) { //Benchmark time QElapsedTimer timer; timer.start(); sourceEstimate = minimumNorm.calculateInverse(evoked.data, evoked.times(0), evoked.times(1)-evoked.times(0)); qVecElapsedTime.append(timer.elapsed()); } double meanTime = 0.0; qint32 offset = 19; qint32 c = 0; for(qint32 i = offset; i < qVecElapsedTime.size(); ++i) { meanTime += qVecElapsedTime[i]; ++c; } meanTime /= (double)c; double varTime = 0; for(qint32 i = offset; i < qVecElapsedTime.size(); ++i) varTime += pow(qVecElapsedTime[i] - meanTime,2); varTime /= (double)c - 1.0f; varTime = sqrt(varTime); qDebug() << "MNE calculation took" << meanTime << "+-" << varTime << "ms in average"; #else MNESourceEstimate sourceEstimate = minimumNorm.calculateInverse(evoked); #endif if(sourceEstimate.isEmpty()) return 1; // View activation time-series std::cout << "\nsourceEstimate:\n" << sourceEstimate.data.block(0,0,10,10) << std::endl; std::cout << "time\n" << sourceEstimate.times.block(0,0,1,10) << std::endl; std::cout << "timeMin\n" << sourceEstimate.times[0] << std::endl; std::cout << "timeMax\n" << sourceEstimate.times[sourceEstimate.times.size()-1] << std::endl; std::cout << "time step\n" << sourceEstimate.tstep << std::endl; //Condition Numbers // MatrixXd mags(102, t_Fwd.sol->data.cols()); // qint32 count = 0; // for(qint32 i = 2; i < 306; i += 3) // { // mags.row(count) = t_Fwd.sol->data.row(i); // ++count; // } // MatrixXd magsClustered(102, t_clusteredFwd.sol->data.cols()); // count = 0; // for(qint32 i = 2; i < 306; i += 3) // { // magsClustered.row(count) = t_clusteredFwd.sol->data.row(i); // ++count; // } // MatrixXd grads(204, t_Fwd.sol->data.cols()); // count = 0; // for(qint32 i = 0; i < 306; i += 3) // { // grads.row(count) = t_Fwd.sol->data.row(i); // ++count; // grads.row(count) = t_Fwd.sol->data.row(i+1); // ++count; // } // MatrixXd gradsClustered(204, t_clusteredFwd.sol->data.cols()); // count = 0; // for(qint32 i = 0; i < 306; i += 3) // { // gradsClustered.row(count) = t_clusteredFwd.sol->data.row(i); // ++count; // gradsClustered.row(count) = t_clusteredFwd.sol->data.row(i+1); // ++count; // } VectorXd s; double t_dConditionNumber = MNEMath::getConditionNumber(t_Fwd.sol->data, s); double t_dConditionNumberClustered = MNEMath::getConditionNumber(t_clusteredFwd.sol->data, s); std::cout << "Condition Number:\n" << t_dConditionNumber << std::endl; std::cout << "Clustered Condition Number:\n" << t_dConditionNumberClustered << std::endl; std::cout << "ForwardSolution" << t_Fwd.sol->data.block(0,0,10,10) << std::endl; std::cout << "Clustered ForwardSolution" << t_clusteredFwd.sol->data.block(0,0,10,10) << std::endl; // double t_dConditionNumberMags = MNEMath::getConditionNumber(mags, s); // double t_dConditionNumberMagsClustered = MNEMath::getConditionNumber(magsClustered, s); // std::cout << "Condition Number Magnetometers:\n" << t_dConditionNumberMags << std::endl; // std::cout << "Clustered Condition Number Magnetometers:\n" << t_dConditionNumberMagsClustered << std::endl; // double t_dConditionNumberGrads = MNEMath::getConditionNumber(grads, s); // double t_dConditionNumberGradsClustered = MNEMath::getConditionNumber(gradsClustered, s); // std::cout << "Condition Number Gradiometers:\n" << t_dConditionNumberGrads << std::endl; // std::cout << "Clustered Condition Number Gradiometers:\n" << t_dConditionNumberGradsClustered << std::endl; //Source Estimate end //######################################################################################## // //only one time point - P100 // qint32 sample = 0; // for(qint32 i = 0; i < sourceEstimate.times.size(); ++i) // { // if(sourceEstimate.times(i) >= 0) // { // sample = i; // break; // } // } // sample += (qint32)ceil(0.106/sourceEstimate.tstep); //100ms // sourceEstimate = sourceEstimate.reduce(sample, 1); View3D::SPtr testWindow = View3D::SPtr(new View3D()); Data3DTreeModel::SPtr p3DDataModel = Data3DTreeModel::SPtr(new Data3DTreeModel()); testWindow->setModel(p3DDataModel); p3DDataModel->addSurfaceSet(parser.value(subjectOption), "MRI", t_surfSet, t_annotationSet); if(MneEstimateTreeItem* pRTDataItem = p3DDataModel->addSourceData(parser.value(subjectOption), evoked.comment, sourceEstimate, t_clusteredFwd)) { pRTDataItem->setLoopState(true); pRTDataItem->setTimeInterval(17); pRTDataItem->setNumberAverages(1); pRTDataItem->setStreamingActive(true); pRTDataItem->setNormalization(QVector3D(0.0,0.5,20.0)); pRTDataItem->setVisualizationType("Smoothing based"); pRTDataItem->setColortable("Hot"); } testWindow->show(); Control3DWidget::SPtr control3DWidget = Control3DWidget::SPtr(new Control3DWidget()); control3DWidget->init(p3DDataModel, testWindow); control3DWidget->show(); if(!t_sFileNameStc.isEmpty()) { QFile t_fileClusteredStc(t_sFileNameStc); sourceEstimate.write(t_fileClusteredStc); } return a.exec(); }