33#include <QSharedPointer>
104 if (!
read(p_IODevice, *
this)) {
105 throw std::runtime_error(
"Source estimation not found");
115 times = RowVectorXf();
134 qint32 rows = this->
data.rows();
136 p_sourceEstimateReduced.
data = MatrixXd::Zero(rows, n);
137 p_sourceEstimateReduced.
data = this->
data.block(0, start, rows, n);
139 p_sourceEstimateReduced.
times = RowVectorXf::Zero(n);
140 p_sourceEstimateReduced.
times = this->
times.block(0, start, 1, n);
141 p_sourceEstimateReduced.
tmin = p_sourceEstimateReduced.
times(0);
151 return p_sourceEstimateReduced;
158 QSharedPointer<QDataStream> t_pStream(
new QDataStream(&p_IODevice));
160 t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
161 t_pStream->setByteOrder(QDataStream::BigEndian);
162 t_pStream->setVersion(QDataStream::Qt_5_0);
164 if (!t_pStream->device()->open(QIODevice::ReadOnly))
167 QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
169 qInfo(
"Reading source estimate from %s...", t_pFile->fileName().toUtf8().constData());
171 qInfo(
"Reading source estimate...");
174 *t_pStream >> p_stc.
tmin;
177 *t_pStream >> p_stc.
tstep;
181 *t_pStream >> t_nVertices;
182 p_stc.
vertices = VectorXi(t_nVertices);
184 for (quint32 i = 0; i < t_nVertices; ++i)
188 *t_pStream >> t_nTimePts;
192 p_stc.
data = MatrixXd(t_nVertices, t_nTimePts);
193 for (qint32 i = 0; i < p_stc.
data.array().size(); ++i) {
196 p_stc.
data.array()(i) = value;
200 p_stc.update_times();
203 t_pStream->device()->close();
215 QSharedPointer<QDataStream> t_pStream(
new QDataStream(&p_IODevice));
217 t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
218 t_pStream->setByteOrder(QDataStream::BigEndian);
219 t_pStream->setVersion(QDataStream::Qt_5_0);
221 if (!t_pStream->device()->open(QIODevice::WriteOnly)) {
222 qWarning(
"Failed to write source estimate!");
226 QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
228 qInfo(
"Write source estimate to %s...", t_pFile->fileName().toUtf8().constData());
230 qInfo(
"Write source estimate...");
233 *t_pStream << static_cast<float>(1000 * this->
tmin);
235 *t_pStream << static_cast<float>(1000 * this->
tstep);
237 *t_pStream << static_cast<quint32>(this->
vertices.size());
239 for (qint32 i = 0; i < this->
vertices.size(); ++i)
240 *t_pStream <<
static_cast<quint32
>(this->
vertices[i]);
242 *t_pStream << static_cast<quint32>(this->
data.cols());
246 for (qint32 i = 0; i < this->
data.array().size(); ++i)
247 *t_pStream <<
static_cast<float>(this->
data.array()(i));
250 t_pStream->device()->close();
261 qWarning(
"InvSourceEstimate::writeHemispherePair - nVerticesLh is %d for %lld vertices."
262 " The hemisphere split point is unknown, cannot write an MNE compatible pair.",
268 qWarning(
"InvSourceEstimate::writeHemispherePair - data has %lld rows but there are"
270 static_cast<long long>(
data.rows()),
static_cast<long long>(
vertices.size()));
276 QString sBase = sBasePath;
277 for (
const QString& sSuffix : {QStringLiteral(
"-lh.stc"),
278 QStringLiteral(
"-rh.stc"),
279 QStringLiteral(
".stc")}) {
280 if (sBase.endsWith(sSuffix, Qt::CaseInsensitive)) {
281 sBase.chop(sSuffix.size());
295 const Hemisphere hemispheres[2] = {
296 {sBase + QStringLiteral(
"-lh.stc"), 0,
nVerticesLh},
297 {sBase + QStringLiteral(
"-rh.stc"),
nVerticesLh, iNumRh}};
299 for (
const Hemisphere& hemi : hemispheres) {
305 stcHemi.
data = this->
data.block(hemi.iOffset, 0, hemi.iCount, this->data.cols());
307 QFile file(hemi.sPath);
308 if (!stcHemi.
write(file)) {
309 qWarning(
"InvSourceEstimate::writeHemispherePair - Failed to write %s",
310 hemi.sPath.toUtf8().constData());
323 if (!file.open(QIODevice::ReadOnly)) {
324 qWarning(
"InvSourceEstimate::read_w - Cannot open file %s", path.toUtf8().constData());
328 QDataStream stream(&file);
329 stream.setByteOrder(QDataStream::BigEndian);
330 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
338 stream >> b0 >> b1 >> b2;
339 qint32 nVertices = (
static_cast<qint32
>(b0) << 16) | (
static_cast<qint32
>(b1) << 8) |
static_cast<qint32
>(b2);
342 MatrixXd
data(nVertices, 1);
344 for (qint32 i = 0; i < nVertices; ++i) {
346 stream >> b0 >> b1 >> b2;
347 vertices[i] = (
static_cast<qint32
>(b0) << 16) | (
static_cast<qint32
>(b1) << 8) |
static_cast<qint32
>(b2);
352 data(i, 0) =
static_cast<double>(val);
366 qWarning(
"InvSourceEstimate::write_w - Source estimate is empty");
371 if (!file.open(QIODevice::WriteOnly)) {
372 qWarning(
"InvSourceEstimate::write_w - Cannot open file %s for writing", path.toUtf8().constData());
376 QDataStream stream(&file);
377 stream.setByteOrder(QDataStream::BigEndian);
378 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
381 stream << static_cast<quint8>(0) <<
static_cast<quint8
>(0);
384 qint32 nVertices =
static_cast<qint32
>(
vertices.size());
385 stream << static_cast<quint8>((nVertices >> 16) & 0xFF)
386 <<
static_cast<quint8
>((nVertices >> 8) & 0xFF)
387 <<
static_cast<quint8
>(nVertices & 0xFF);
390 for (qint32 i = 0; i < nVertices; ++i) {
392 stream << static_cast<quint8>((idx >> 16) & 0xFF)
393 <<
static_cast<quint8
>((idx >> 8) & 0xFF)
394 <<
static_cast<quint8
>(idx & 0xFF);
396 stream << static_cast<float>(
data(i, 0));
404void InvSourceEstimate::update_times()
406 if (
data.cols() > 0) {
409 for (
float i = 1; i < this->
times.size(); ++i)
412 this->
times = RowVectorXf();
450 VectorXi vIndexSourceLabels;
452 if (lPickedLabels.isEmpty()) {
453 qWarning() <<
"InvSourceEstimate::getIndicesByLabel - picked label list is empty. Returning.";
454 return vIndexSourceLabels;
458 for (
int i = 0; i < this->
vertices.rows(); i++) {
459 for (
int k = 0; k < lPickedLabels.size(); k++) {
460 if (this->
vertices(i) == lPickedLabels.at(k).label_id) {
461 vIndexSourceLabels.conservativeResize(vIndexSourceLabels.rows() + 1, 1);
462 vIndexSourceLabels(vIndexSourceLabels.rows() - 1) = i;
470 for (
int i = 0; i < this->
vertices.rows(); i++) {
478 for (
int k = 0; k < lPickedLabels.size(); k++) {
479 for (
int l = 0; l < lPickedLabels.at(k).
vertices.rows(); l++) {
480 if (this->
vertices(i) == lPickedLabels.at(k).vertices(l) && lPickedLabels.at(k).hemi == hemi) {
481 vIndexSourceLabels.conservativeResize(vIndexSourceLabels.rows() + 1, 1);
482 vIndexSourceLabels(vIndexSourceLabels.rows() - 1) = i;
490 return vIndexSourceLabels;
InvSourceEstimate value type — central source-space data container produced by every INVLIB inverse s...
FreeSurfer surface, annotation and parcellation I/O for mne-cpp.
Inverse source estimation (MNE, dSPM, sLORETA, dipole fitting).
std::vector< InvSourceCoupling > couplings
std::vector< InvFocalDipole > focalDipoles
Eigen::MatrixX3f positions
Eigen::VectorXi getIndicesByLabel(const QList< FSLIB::FsLabel > &lPickedLabels, bool bIsClustered) const
static bool read(QIODevice &p_IODevice, InvSourceEstimate &p_stc)
InvSourceEstimate & operator=(const InvSourceEstimate &rhs)
static InvSourceEstimate read_w(const QString &path)
bool write(QIODevice &p_IODevice)
InvSourceSpaceType sourceSpaceType
InvOrientationType orientationType
bool writeHemispherePair(const QString &sBasePath)
std::vector< InvConnectivity > connectivity
InvSourceEstimate reduce(qint32 start, qint32 n)
void write_w(const QString &path) const