Read an evoked response
Read an evoked data set from FIFF and print its measurement info and coordinate transforms.
Libraries: Visualization, MNE Core, FIFF I/O, Utilities
Results



Console output
Reading resources/data/MNE-sample-data/MEG/sample/sample_audvis-ave.fif ...
Creating tag directory for resources/data/MNE-sample-data/MEG/sample/sample_audvis-ave.fif...
Read a total of 4 projection items:
PCA-v1 (1 x 102) active
PCA-v2 (1 x 102) active
PCA-v3 (1 x 102) active
Average EEG reference (1 x 60) active
Found the data of interest:
t = -199.80 ... 499.49 ms (Left visual)
nave = 67 - aspect type = 100
Preprocessing...
376 channels remain after picking
Created an SSP operator (subspace dimension = 4)
4 projection items activated.
SSP projectors applied to the evoked data
No baseline correction applied
Employing the Neuromag head coordinate system
Condition 'Left visual': 67 epochs, 376 channels, 421 samples from -0.199795 s to 0.499488 s
Applying baseline correction ... (mode: mean)
[Numerics::rescale] Applying baseline correction ... (mode: mean)
Line frequency is not defined
QCommandLineParser: option not defined: "useCTFComp"
Background
Build and run
Needs the MNE sample dataset in resources/data/MNE-sample-data.
cmake --build build/developer-dynamic --target ex_read_evoked
./out/Release/examples/ex_read_evoked --help
Options
| Option | Description |
|---|---|
--ave | Path to the evoked/average <file>. |
--aveIdx | The average <index> to choose from the average file. |
--useCTFComp | Use the CTF compensator, if available. |
Key API
The documented classes this example uses most:
| Class | Library |
|---|---|
FiffCoordTrans | FIFF I/O |
FiffEvoked | FIFF I/O |
FigureExporter | Visualization |
LinePlot | Visualization |
MNE | MNE Core |
Used in the API reference
Source
src/examples/ex_read_evoked on GitHub.
src/examples/ex_read_evoked/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 an evoked data set from FIFF and print its measurement info and coordinate transforms.
*/
//=============================================================================================================
// INCLUDES
//=============================================================================================================
#include <iostream>
#include <vector>
#include <math.h>
#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;
//=============================================================================================================
// DEFINE GLOBAL METHODS
//=============================================================================================================
namespace
{
/** Writes a butterfly plot of the channels of @p evoked of @p kind measured in @p unit, scaled by @p scale. */
void saveButterfly(const FiffEvoked& evoked, int kind, int unit, double scale, const QString& name, const QString& yLabel, const QColor& color)
{
QVector<double> times(evoked.times.size());
for (int s = 0; s < times.size(); ++s) {
times[s] = 1000.0 * evoked.times(s);
}
LinePlot plot;
plot.setTitle(QString("%1 (%2 epochs)").arg(evoked.comment).arg(evoked.nave));
plot.setXLabel("Time [ms]");
plot.setYLabel(yLabel);
for (int c = 0; c < evoked.data.rows(); ++c) {
if (evoked.info.chs[c].kind != kind || evoked.info.chs[c].unit != unit || evoked.info.bads.contains(evoked.info.ch_names[c])) {
continue;
}
QVector<double> values(times.size());
for (int s = 0; s < times.size(); ++s) {
values[s] = scale * evoked.data(c, s);
}
plot.addSeries(times, values, QString(), color);
}
FigureExporter::save(plot, name, QString("%1 of the '%2' response: one line per channel, baseline-corrected, bad channels excluded.").arg(yLabel.section(' ', 0, 0), evoked.comment));
}
} // namespace
//=============================================================================================================
// 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 a(argc, argv);
// Command Line Parser
QCommandLineParser parser;
parser.setApplicationDescription("Read Evoked Example");
parser.addHelpOption();
QCommandLineOption evokedFileOption("ave", "Path to the evoked/average <file>.", "file", QCoreApplication::applicationDirPath() + "/../resources/data/MNE-sample-data/MEG/sample/sample_audvis-ave.fif");
QCommandLineOption evokedIdxOption("aveIdx", "The average <index> to choose from the average file.", "index", "2");
QCommandLineOption useCTFCompOption("useCTFComp", "Use the CTF compensator, if available.");
parser.addOption(evokedFileOption);
parser.addOption(evokedIdxOption);
parser.process(a);
//generate FiffEvoked object
QFile t_sampleFile(parser.value(evokedFileOption));
FiffEvoked p_FiffEvoked(t_sampleFile, QVariant(parser.value(evokedIdxOption)));
//Select the head coordinate system
bool use_ctf_head = parser.isSet(useCTFCompOption);
FiffCoordTrans meg_trans;
if (use_ctf_head) {
if (p_FiffEvoked.info.ctf_head_t.isEmpty())
std::cout << "\nNo CTF head transformation available" << std::endl;
else {
meg_trans = p_FiffEvoked.info.dev_ctf_t;
FiffCoordTrans eeg_trans(meg_trans);
eeg_trans.invert_transform();
std::cout << "Employing the CTF/4D head coordinate system\n"
<< std::endl;
}
} else {
meg_trans = p_FiffEvoked.info.dev_head_t;
FiffCoordTrans eeg_trans;
std::cout << "Employing the Neuromag head coordinate system\n"
<< std::endl;
}
//Transform coil and electrode locations to the desired coordinate frame
//ToDo: MATLAB root fct fiff_transform_meg_chs and fiff_transform_eeg_chs needs to be implemented
//Create the coil definitions
//ToDo: MATLAB root fct mne_add_coil_defs needs to be implemented
//N.B. If a nonstandard (in MNE sense) coil def file is used, do
//ToDo: MATLAB root fct mne_load_coil_def, mne_add_coil_defs needs to be implemented
std::cout << "Condition '" << p_FiffEvoked.comment.toStdString() << "': " << p_FiffEvoked.nave << " epochs, "
<< p_FiffEvoked.info.nchan << " channels, " << p_FiffEvoked.times.size() << " samples from "
<< p_FiffEvoked.times(0) << " s to " << p_FiffEvoked.times(p_FiffEvoked.times.size() - 1) << " s" << std::endl;
// Plot a copy corrected over the prestimulus interval, as mne.Evoked.plot shows it
FiffEvoked corrected(p_FiffEvoked);
QPair<float, float> baseline(p_FiffEvoked.times(0), 0.0f);
corrected.applyBaselineCorrection(baseline);
saveButterfly(corrected, FIFFV_EEG_CH, FIFF_UNIT_V, 1e6, "eeg", "EEG [µV]", QColor(150, 40, 40, 90));
saveButterfly(corrected, FIFFV_MEG_CH, FIFF_UNIT_T_M, 1e13, "gradiometers", "Gradiometers [fT/cm]", QColor(30, 60, 110, 70));
saveButterfly(corrected, FIFFV_MEG_CH, FIFF_UNIT_T, 1e15, "magnetometers", "Magnetometers [fT]", QColor(30, 110, 70, 90));
return 0;
}
//=============================================================================================================
// STATIC DEFINITIONS
//=============================================================================================================
Related examples
- Read a segment of raw data — Read a segment of a raw FIFF recording with channel picking, SSP projectors and CTF compensation.
- Tour of the FIFF data structures — FIFF library: measurement info, channels, digitizer, transforms, projectors, covariance, events, annotations, evoked sets and the low-level file structure.
- Epochs, compensation and the FIFF file directory — FIFF library: fixed-length epochs, CTF compensation, the file directory and raw buffers, and handing recordings to another application.
- Copy a raw recording to a new FIFF file — Copy a raw FIFF recording buffer by buffer into a new file with FiffStream.
- Average epochs into an evoked response — Average stimulus-locked epochs of a raw FIFF recording into an evoked response, with event selection and EOG rejection.