bool MNEForwardSolution::read_one(FiffStream* p_pStream, const FiffDirTree& p_Node, MNEForwardSolution& one) { // // Read all interesting stuff for one forward solution // if(p_Node.isEmpty()) return false; one.clear(); FIFFLIB::FiffTag* t_pTag = NULL; if(!p_Node.find_tag(p_pStream, FIFF_MNE_SOURCE_ORIENTATION, t_pTag)) { p_pStream->device()->close(); std::cout << "Source orientation tag not found."; //ToDo: throw error. return false; } one.source_ori = *t_pTag->toInt(); if(!p_Node.find_tag(p_pStream, FIFF_MNE_COORD_FRAME, t_pTag)) { p_pStream->device()->close(); std::cout << "Coordinate frame tag not found."; //ToDo: throw error. return false; } one.coord_frame = *t_pTag->toInt(); if(!p_Node.find_tag(p_pStream, FIFF_MNE_SOURCE_SPACE_NPOINTS, t_pTag)) { p_pStream->device()->close(); std::cout << "Number of sources not found."; //ToDo: throw error. return false; } one.nsource = *t_pTag->toInt(); if(!p_Node.find_tag(p_pStream, FIFF_NCHAN, t_pTag)) { p_pStream->device()->close(); printf("Number of channels not found."); //ToDo: throw error. return false; } one.nchan = *t_pTag->toInt(); if(p_pStream->read_named_matrix(p_Node, FIFF_MNE_FORWARD_SOLUTION, *one.sol.data())) one.sol->transpose_named_matrix(); else { p_pStream->device()->close(); printf("Forward solution data not found ."); //ToDo: throw error. //error(me,'Forward solution data not found (%s)',mne_omit_first_line(lasterr)); return false; } if(p_pStream->read_named_matrix(p_Node, FIFF_MNE_FORWARD_SOLUTION_GRAD, *one.sol_grad.data())) one.sol_grad->transpose_named_matrix(); else one.sol_grad->clear(); if (one.sol->data.rows() != one.nchan || (one.sol->data.cols() != one.nsource && one.sol->data.cols() != 3*one.nsource)) { p_pStream->device()->close(); printf("Forward solution matrix has wrong dimensions.\n"); //ToDo: throw error. //error(me,'Forward solution matrix has wrong dimensions'); return false; } if (!one.sol_grad->isEmpty()) { if (one.sol_grad->data.rows() != one.nchan || (one.sol_grad->data.cols() != 3*one.nsource && one.sol_grad->data.cols() != 3*3*one.nsource)) { p_pStream->device()->close(); printf("Forward solution gradient matrix has wrong dimensions.\n"); //ToDo: throw error. //error(me,'Forward solution gradient matrix has wrong dimensions'); } } if (t_pTag) delete t_pTag; return true; }
bool MNEInverseOperator::read_inverse_operator(QIODevice& p_IODevice, MNEInverseOperator& inv) { // // Open the file, create directory // FiffStream::SPtr t_pStream(new FiffStream(&p_IODevice)); printf("Reading inverse operator decomposition from %s...\n",t_pStream->streamName().toUtf8().constData()); FiffDirTree t_Tree; QList<FiffDirEntry> t_Dir; if(!t_pStream->open(t_Tree, t_Dir)) return false; // // Find all inverse operators // QList <FiffDirTree> invs_list = t_Tree.dir_tree_find(FIFFB_MNE_INVERSE_SOLUTION); if ( invs_list.size()== 0) { printf("No inverse solutions in %s\n", t_pStream->streamName().toUtf8().constData()); return false; } FiffDirTree* invs = &invs_list[0]; // // Parent MRI data // QList <FiffDirTree> parent_mri = t_Tree.dir_tree_find(FIFFB_MNE_PARENT_MRI_FILE); if (parent_mri.size() == 0) { printf("No parent MRI information in %s", t_pStream->streamName().toUtf8().constData()); return false; } printf("\tReading inverse operator info..."); // // Methods and source orientations // FiffTag::SPtr t_pTag; if (!invs->find_tag(t_pStream.data(), FIFF_MNE_INCLUDED_METHODS, t_pTag)) { printf("Modalities not found\n"); return false; } inv = MNEInverseOperator(); inv.methods = *t_pTag->toInt(); // if (!invs->find_tag(t_pStream.data(), FIFF_MNE_SOURCE_ORIENTATION, t_pTag)) { printf("Source orientation constraints not found\n"); return false; } inv.source_ori = *t_pTag->toInt(); // if (!invs->find_tag(t_pStream.data(), FIFF_MNE_SOURCE_SPACE_NPOINTS, t_pTag)) { printf("Number of sources not found\n"); return false; } inv.nsource = *t_pTag->toInt(); inv.nchan = 0; // // Coordinate frame // if (!invs->find_tag(t_pStream.data(), FIFF_MNE_COORD_FRAME, t_pTag)) { printf("Coordinate frame tag not found\n"); return false; } inv.coord_frame = *t_pTag->toInt(); // // The actual source orientation vectors // if (!invs->find_tag(t_pStream.data(), FIFF_MNE_INVERSE_SOURCE_ORIENTATIONS, t_pTag)) { printf("Source orientation information not found\n"); return false; } // if(inv.source_nn) // delete inv.source_nn; inv.source_nn = t_pTag->toFloatMatrix(); inv.source_nn.transposeInPlace(); printf("[done]\n"); // // The SVD decomposition... // printf("\tReading inverse operator decomposition..."); if (!invs->find_tag(t_pStream.data(), FIFF_MNE_INVERSE_SING, t_pTag)) { printf("Singular values not found\n"); return false; } // if(inv.sing) // delete inv.sing; inv.sing = Map<VectorXf>(t_pTag->toFloat(), t_pTag->size()/4).cast<double>(); inv.nchan = inv.sing.rows(); // // The eigenleads and eigenfields // inv.eigen_leads_weighted = false; if(!t_pStream->read_named_matrix(*invs, FIFF_MNE_INVERSE_LEADS, *inv.eigen_leads.data())) { inv.eigen_leads_weighted = true; if(!t_pStream->read_named_matrix(*invs, FIFF_MNE_INVERSE_LEADS_WEIGHTED, *inv.eigen_leads.data())) { printf("Error reading eigenleads named matrix.\n"); return false; } } // // Having the eigenleads as columns is better for the inverse calculations // inv.eigen_leads->transpose_named_matrix(); if(!t_pStream->read_named_matrix(*invs, FIFF_MNE_INVERSE_FIELDS, *inv.eigen_fields.data())) { printf("Error reading eigenfields named matrix.\n"); return false; } printf("[done]\n"); // // Read the covariance matrices // if(t_pStream->read_cov(*invs, FIFFV_MNE_NOISE_COV, *inv.noise_cov.data())) { printf("\tNoise covariance matrix read.\n"); } else { printf("\tError: Not able to read noise covariance matrix.\n"); return false; } if(t_pStream->read_cov(*invs, FIFFV_MNE_SOURCE_COV, *inv.source_cov.data())) { printf("\tSource covariance matrix read.\n"); } else { printf("\tError: Not able to read source covariance matrix.\n"); return false; } // // Read the various priors // if(t_pStream->read_cov(*invs, FIFFV_MNE_ORIENT_PRIOR_COV, *inv.orient_prior.data())) { printf("\tOrientation priors read.\n"); } else inv.orient_prior->clear(); if(t_pStream->read_cov(*invs, FIFFV_MNE_DEPTH_PRIOR_COV, *inv.depth_prior.data())) { printf("\tDepth priors read.\n"); } else { inv.depth_prior->clear(); } if(t_pStream->read_cov(*invs, FIFFV_MNE_FMRI_PRIOR_COV, *inv.fmri_prior.data())) { printf("\tfMRI priors read.\n"); } else { inv.fmri_prior->clear(); } // // Read the source spaces // if(!MNESourceSpace::readFromStream(t_pStream, false, t_Tree, inv.src)) { printf("\tError: Could not read the source spaces.\n"); return false; } for (qint32 k = 0; k < inv.src.size(); ++k) inv.src[k].id = MNESourceSpace::find_source_space_hemi(inv.src[k]); // // Get the MRI <-> head coordinate transformation // FiffCoordTrans mri_head_t;// = NULL; if (!parent_mri[0].find_tag(t_pStream.data(), FIFF_COORD_TRANS, t_pTag)) { printf("MRI/head coordinate transformation not found\n"); return false; } else { mri_head_t = t_pTag->toCoordTrans(); if (mri_head_t.from != FIFFV_COORD_MRI || mri_head_t.to != FIFFV_COORD_HEAD) { mri_head_t.invert_transform(); if (mri_head_t.from != FIFFV_COORD_MRI || mri_head_t.to != FIFFV_COORD_HEAD) { printf("MRI/head coordinate transformation not found"); // if(mri_head_t) // delete mri_head_t; return false; } } } inv.mri_head_t = mri_head_t; // // get parent MEG info // t_pStream->read_meas_info_base(t_Tree, inv.info); // // Transform the source spaces to the correct coordinate frame // if necessary // if (inv.coord_frame != FIFFV_COORD_MRI && inv.coord_frame != FIFFV_COORD_HEAD) printf("Only inverse solutions computed in MRI or head coordinates are acceptable"); // // Number of averages is initially one // inv.nave = 1; // // We also need the SSP operator // inv.projs = t_pStream->read_proj(t_Tree); // // Some empty fields to be filled in later // // inv.proj = []; % This is the projector to apply to the data // inv.whitener = []; % This whitens the data // inv.reginv = []; % This the diagonal matrix implementing // % regularization and the inverse // inv.noisenorm = []; % These are the noise-normalization factors // if(!inv.src.transform_source_space_to(inv.coord_frame, mri_head_t)) { printf("Could not transform source space.\n"); } printf("\tSource spaces transformed to the inverse solution coordinate frame\n"); // // Done! // return true; }