Skip to main content

Read a segment of raw data

Read a segment of a raw FIFF recording with channel picking, SSP projectors and CTF compensation.

Libraries: Visualization, MNE Core, FIFF I/O, Utilities

Results​

The first six gradiometers of the segment with the SSP projectors applied.
The first six gradiometers of the segment with the SSP projectors applied.
Console output
Opening raw data resources/data/mne-cpp-test-data/MEG/sample/sample_audvis_trunc_raw.fif...
Creating tag directory for resources/data/mne-cpp-test-data/MEG/sample/sample_audvis_trunc_raw.fif...
Read a total of 4 projection items:
PCA-v1 (1 x 102) idle
PCA-v2 (1 x 102) idle
PCA-v3 (1 x 102) idle
Average EEG reference (1 x 60) idle
Range : 12900 ... 18906 = 42.956 ... 62.955 secs
Ready.
4 projection items activated
Created an SSP operator (subspace dimension = 4)
Read 3005 samples.
-7.8953e-16 -7.5617e-16 -9.86807e-16 -1.70061e-15 -2.37518e-15 -2.77201e-15 -3.25573e-15 -3.90999e-15 -4.25906e-15 -4.06957e-15
4.00059e-16 1.02682e-15 1.2776e-15 1.16745e-15 8.57991e-16 2.94696e-16 -2.24068e-16 -1.59346e-17 8.28144e-16 1.30618e-15
-1.3041e-18 -9.38135e-18 -1.94505e-17 -2.43232e-17 -2.39295e-17 -2.55488e-17 -2.95863e-17 -2.25088e-17 6.86707e-18 4.69476e-17
-8.46389e-16 -1.75192e-15 -1.85765e-15 -1.74133e-15 -2.35965e-15 -3.63241e-15 -4.6049e-15 -4.56361e-15 -3.6339e-15 -2.62643e-15
1.53422e-16 -1.07616e-16 -8.72947e-16 -1.50142e-15 -1.19122e-15 7.98294e-17 1.22424e-15 1.08909e-15 -2.31929e-17 -6.32946e-16
-5.79271e-18 -1.45846e-17 1.14204e-18 4.0179e-17 6.85695e-17 6.49531e-17 5.39264e-17 7.33624e-17 1.22104e-16 1.62621e-16
2.76808e-16 1.10569e-15 1.29554e-15 3.72085e-16 -6.5355e-16 -7.03596e-16 -8.83464e-17 2.50385e-16 1.5791e-16 1.39514e-16
5.64595e-16 -2.12245e-16 -1.13634e-15 -7.81882e-16 2.89788e-16 4.12537e-16 -8.44747e-16 -2.55205e-15 -3.66692e-15 -3.5697e-15
-1.05077e-17 -4.81108e-17 -4.46854e-17 2.35428e-18 3.60151e-17 3.13664e-17 2.64341e-17 5.42459e-17 1.01874e-16 1.40898e-16
6.84132e-16 1.3094e-15 1.81239e-15 2.09358e-15 1.97817e-15 1.49776e-15 1.07955e-15 1.09768e-15 1.33806e-15 1.21785e-15
Line frequency is not defined

Background​

Build and run​

Needs the mne-cpp-test-data repository cloned to resources/data/mne-cpp-test-data.

cmake --build build/developer-dynamic --target ex_read_raw
./out/Release/examples/ex_read_raw --help

Options​

OptionDescription
--fileInThe input file <in>.
--fromRead data from <from> (in seconds).
--toRead data from <to> (in seconds).
--inSamplesTiming is set in samples.
--keepCompKeep compensators.

Key API​

The documented classes this example uses most:

ClassLibrary
FiffRawDataFIFF I/O
FigureExporterVisualization
LinePlotVisualization
MNEMNE Core

Used in the API reference​

Source​

src/examples/ex_read_raw on GitHub.

src/examples/ex_read_raw/main.cpp
//=============================================================================================================
/**
* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2012-2026 MNE-CPP Authors
*
* @file main.cpp
* @author Gabriel Motta <gabrielbenmotta@gmail.com>;
* Christoph Dinh <christoph.dinh@mne-cpp.org>;
* Lorenz Esch <lorenz.esch@tu-ilmenau.de>
* @since 0.1.0
* @date July, 2012
* @brief Read a segment of a raw FIFF recording with channel picking, SSP projectors and CTF compensation.
*/

//=============================================================================================================
// INCLUDES
//=============================================================================================================

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#include <fiff/fiff.h>
#include <mne/mne.h>
#include <utils/generics/mne_logger.h>

#include <disp/plots/helpers/figureexporter.h>
#include <disp/plots/lineplot.h>

//=============================================================================================================
// QT INCLUDES
//=============================================================================================================

#include <QApplication>
#include <QCommandLineParser>
#include <QFile>

//=============================================================================================================
// USED NAMESPACES
//=============================================================================================================

using namespace DISPLIB;
using namespace FIFFLIB;
using namespace MNELIB;
using namespace UTILSLIB;
using namespace Eigen;

//=============================================================================================================
// MAIN
//=============================================================================================================

//=============================================================================================================

/**
* 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[])
{
qInstallMessageHandler(MNELogger::customLogWriter);
QApplication app(argc, argv);

// Command Line Parser
QCommandLineParser parser;
parser.setApplicationDescription("Read Raw Example");
parser.addHelpOption();

QCommandLineOption inputOption("fileIn", "The input file <in>.", "in", QCoreApplication::applicationDirPath() + "/../resources/data/mne-cpp-test-data/MEG/sample/sample_audvis_trunc_raw.fif");
QCommandLineOption fromOption("from", "Read data from <from> (in seconds).", "from", "42.956");
QCommandLineOption toOption("to", "Read data from <to> (in seconds).", "to", "52.956");
QCommandLineOption inSamplesOption("inSamples", "Timing is set in samples.", "inSamples", "false");
QCommandLineOption keepCompOption("keepComp", "Keep compensators.", "keepComp", "false");

parser.addOption(inputOption);
parser.addOption(fromOption);
parser.addOption(toOption);
parser.addOption(inSamplesOption);
parser.addOption(keepCompOption);

parser.process(app);

QFile t_fileRaw(parser.value(inputOption));

float from = parser.value(fromOption).toFloat();
float to = parser.value(toOption).toFloat();

bool in_samples = false;
if (parser.value(inSamplesOption) == "false" || parser.value(inSamplesOption) == "0") {
in_samples = false;
} else if (parser.value(inSamplesOption) == "true" || parser.value(inSamplesOption) == "1") {
in_samples = true;
}

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_raw_data_open]
//
// Setup for reading the raw data
//
FiffRawData raw(t_fileRaw);

//
// Set up pick list: MEG + STI 014 - bad channels
//
QStringList include;
include << "STI 014";
bool want_meg = true;
bool want_eeg = false;
bool want_stim = false;

RowVectorXi picks = raw.info.pick_types(want_meg, want_eeg, want_stim, include, raw.info.bads);
//! [fiff_raw_data_open]

//
// Set up projection
//
qint32 k = 0;
if (raw.info.projs.size() == 0)
qInfo("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;

qInfo("%lld projection items activated\n",
static_cast<long long>(raw.info.projs.size()));
//
// Create the projector
//
fiff_int_t nproj = raw.info.make_projector(raw.proj);

if (nproj == 0)
qWarning("The projection vectors do not apply to these channels\n");
else
qInfo("Created an SSP operator (subspace dimension = %d)\n", nproj);
}

//
// Set up the CTF compensator
//
qint32 current_comp = raw.info.get_current_comp();
qint32 dest_comp = 0;

if (current_comp > 0)
qInfo("Current compensation grade : %d\n", current_comp);

if (keep_comp)
dest_comp = current_comp;

if (current_comp != dest_comp) {
if (MNE::make_compensator(raw.info, current_comp, dest_comp, raw.comp)) {
raw.info.set_current_comp(dest_comp);
qInfo("Appropriate compensator added to change to grade %d.\n", dest_comp);
} else {
qWarning("Could not make the compensator\n");
return -1;
}
}
//! [fiff_raw_data_read_segment]
//
// Read a data segment
// times output argument is optional
//
bool readSuccessful = false;
MatrixXd data;
MatrixXd times;
if (in_samples)
readSuccessful = raw.read_raw_segment(data, times, (qint32)from, (qint32)to, picks);
else
readSuccessful = raw.read_raw_segment_times(data, times, from, to, picks);

if (!readSuccessful) {
qWarning("Could not read raw segment.\n");
return -1;
}
//! [fiff_raw_data_read_segment]

qInfo("Read %d samples.\n", (qint32)data.cols());

std::cout << data.block(0, 0, 10, 10) << std::endl;

// Six gradiometers in fT/cm, stacked six standard deviations apart
QVector<int> grads;
for (int c = 0; c < data.rows() && grads.size() < 6; ++c) {
if (raw.info.chs[picks(c)].unit == FIFF_UNIT_T_M) {
grads.append(c);
}
}
double spread = 0.0;
for (int c : grads) {
const RowVectorXd row = 1e13 * data.row(c);
spread = std::max(spread, std::sqrt((row.array() - row.mean()).square().mean()));
}
LinePlot traces;
traces.setTitle(QString("Raw MEG from %1 s to %2 s").arg(from).arg(to));
traces.setXLabel("Time [s]");
traces.setYLabel("Gradiometers [fT/cm], offset");
const QVector<double> seconds(times.data(), times.data() + times.size());
for (int i = 0; i < grads.size(); ++i) {
const int c = grads[i];
const double mean = 1e13 * data.row(c).mean();
QVector<double> values(data.cols());
for (int s = 0; s < data.cols(); ++s) {
values[s] = 1e13 * data(c, s) - mean - 6.0 * spread * i;
}
traces.addSeries(seconds, values, raw.info.ch_names[picks(c)]);
}
FigureExporter::save(traces, "raw", "The first six gradiometers of the segment with the SSP projectors applied.", QSize(800, 520));

return 0;
}

//=============================================================================================================
// STATIC DEFINITIONS
//=============================================================================================================