v2.0.0
Loading...
Searching...
No Matches
fiff_annotation_event_utils.cpp
Go to the documentation of this file.
1//=============================================================================================================
15
16//=============================================================================================================
17// INCLUDES
18//=============================================================================================================
19
21
22//=============================================================================================================
23// QT INCLUDES
24//=============================================================================================================
25
26#include <QDebug>
27
28//=============================================================================================================
29// STL INCLUDES
30//=============================================================================================================
31
32#include <cmath>
33
34//=============================================================================================================
35// USED NAMESPACES
36//=============================================================================================================
37
38using namespace FIFFLIB;
39using namespace Eigen;
40
41//=============================================================================================================
42// DEFINE FUNCTIONS
43//=============================================================================================================
44
46 const MatrixXi& events,
47 double sfreq,
48 const QMap<int, QString>& eventDescriptions,
49 int firstSample)
50{
51 FiffAnnotations annotations;
52
53 if (events.rows() == 0 || events.cols() < 3) {
54 return annotations;
55 }
56
57 if (sfreq <= 0.0) {
58 qWarning("annotationsFromEvents: sfreq must be positive, got %f", sfreq);
59 return annotations;
60 }
61
62 for (int i = 0; i < static_cast<int>(events.rows()); ++i) {
63 const int sample = events(i, 0);
64 const int eventId = events(i, 2);
65
66 const double onset = static_cast<double>(sample - firstSample) / sfreq;
67
68 QString description;
69 if (eventDescriptions.contains(eventId)) {
70 description = eventDescriptions.value(eventId);
71 } else {
72 description = QString::number(eventId);
73 }
74
75 annotations.append(onset, 0.0, description);
76 }
77
78 return annotations;
79}
80
81//=============================================================================================================
82
84 const FiffAnnotations& annotations,
85 double sfreq,
86 const QMap<QString, int>& eventIds,
87 int firstSample)
88{
89 if (annotations.isEmpty()) {
90 return MatrixXi(0, 3);
91 }
92
93 if (sfreq <= 0.0) {
94 qWarning("eventsFromAnnotations: sfreq must be positive, got %f", sfreq);
95 return MatrixXi(0, 3);
96 }
97
98 const int n = annotations.size();
99 MatrixXi result(n, 3);
100
101 for (int i = 0; i < n; ++i) {
102 const FiffAnnotation& a = annotations[i];
103
104 const int sample = static_cast<int>(std::round(a.onset * sfreq)) + firstSample;
105
106 int eventId = 0;
107 if (eventIds.contains(a.description)) {
108 eventId = eventIds.value(a.description);
109 } else {
110 bool ok = false;
111 const int parsed = a.description.toInt(&ok);
112 if (ok) {
113 eventId = parsed;
114 }
115 }
116
117 result(i, 0) = sample;
118 result(i, 1) = 0;
119 result(i, 2) = eventId;
120 }
121
122 return result;
123}
124
125//=============================================================================================================
126
127QMap<QString, int> FIFFLIB::countAnnotations(const FiffAnnotations& annotations)
128{
129 QMap<QString, int> counts;
130
131 for (int i = 0; i < annotations.size(); ++i) {
132 counts[annotations[i].description] += 1;
133 }
134
135 return counts;
136}
Conversion helpers between FIFF annotations and the integer stim-channel event list used by epoching ...
FIFF file I/O, in-memory data structures and high-level readers/writers.
Eigen::MatrixXi eventsFromAnnotations(const FiffAnnotations &annotations, double sfreq, const QMap< QString, int > &eventIds=QMap< QString, int >(), int firstSample=0)
QMap< QString, int > countAnnotations(const FiffAnnotations &annotations)
FiffAnnotations annotationsFromEvents(const Eigen::MatrixXi &events, double sfreq, const QMap< int, QString > &eventDescriptions=QMap< int, QString >(), int firstSample=0)
One FIFF / MNE annotation: onset (s), duration (s) and description string.
Container for FiffAnnotation entries with FIFF, JSON and CSV serializers.
void append(const FiffAnnotation &annotation)