Skip to main content

ex_read_raw

Example of reading raw data.

Libraries: MNE Core, FIFF I/O, Utilities

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.

Build and run​

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

Used in the API reference​

Source​

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 Example of reading raw data
*/

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

#include <iostream>
#include <vector>
#include <math.h>

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

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

#include <QtCore/QCoreApplication>
#include <QCommandLineParser>
#include <QFile>

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

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);
QCoreApplication 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;

return 0;
}

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