v2.0.0
Loading...
Searching...
No Matches
mna_port.cpp
Go to the documentation of this file.
1//=============================================================================================================
22
23//=============================================================================================================
24// INCLUDES
25//=============================================================================================================
26
27#include "mna_port.h"
28
29//=============================================================================================================
30// USED NAMESPACES
31//=============================================================================================================
32
33using namespace MNALIB;
34
35//=============================================================================================================
36// STATIC HELPERS
37//=============================================================================================================
38
39static QString dataKindToString(MnaDataKind kind)
40{
41 switch (kind) {
43 return QStringLiteral("fiff_raw");
45 return QStringLiteral("forward");
47 return QStringLiteral("inverse");
49 return QStringLiteral("covariance");
51 return QStringLiteral("source_estimate");
53 return QStringLiteral("epochs");
55 return QStringLiteral("evoked");
57 return QStringLiteral("matrix");
59 return QStringLiteral("volume");
61 return QStringLiteral("surface");
63 return QStringLiteral("bem");
65 return QStringLiteral("annotation");
67 return QStringLiteral("label");
69 return QStringLiteral("real_time_stream");
71 return QStringLiteral("custom");
72 }
73 return QStringLiteral("custom");
74}
75
76//=============================================================================================================
77
78static MnaDataKind dataKindFromString(const QString& str)
79{
80 if (str == QLatin1String("fiff_raw"))
82 if (str == QLatin1String("forward"))
84 if (str == QLatin1String("inverse"))
86 if (str == QLatin1String("covariance"))
88 if (str == QLatin1String("source_estimate"))
90 if (str == QLatin1String("epochs"))
92 if (str == QLatin1String("evoked"))
94 if (str == QLatin1String("matrix"))
96 if (str == QLatin1String("volume"))
98 if (str == QLatin1String("surface"))
100 if (str == QLatin1String("bem"))
101 return MnaDataKind::Bem;
102 if (str == QLatin1String("annotation"))
104 if (str == QLatin1String("label"))
105 return MnaDataKind::Label;
106 if (str == QLatin1String("real_time_stream"))
108 return MnaDataKind::Custom;
109}
110
111//=============================================================================================================
112
113static QString portDirToString(MnaPortDir dir)
114{
115 switch (dir) {
117 return QStringLiteral("input");
119 return QStringLiteral("output");
120 }
121 return QStringLiteral("input");
122}
123
124//=============================================================================================================
125
126static MnaPortDir portDirFromString(const QString& str)
127{
128 if (str == QLatin1String("output"))
129 return MnaPortDir::Output;
130 return MnaPortDir::Input;
131}
132
133//=============================================================================================================
134// DEFINE MEMBER METHODS
135//=============================================================================================================
136
137QJsonObject MnaPort::toJson() const
138{
139 QJsonObject json = extras;
140 json[QLatin1String("name")] = name;
141 json[QLatin1String("data_kind")] = dataKindToString(dataKind);
142 json[QLatin1String("direction")] = portDirToString(direction);
143
144 if (!sourceNodeId.isEmpty())
145 json[QLatin1String("source_node")] = sourceNodeId;
146 if (!sourcePortName.isEmpty())
147 json[QLatin1String("source_port")] = sourcePortName;
148
149 if (!streamProtocol.isEmpty()) {
150 json[QLatin1String("stream_protocol")] = streamProtocol;
151 json[QLatin1String("stream_endpoint")] = streamEndpoint;
152 json[QLatin1String("stream_buffer_ms")] = streamBufferMs;
153 }
154
155 if (!cachedResultPath.isEmpty())
156 json[QLatin1String("cached_result")] = cachedResultPath;
157 if (!cachedResultHash.isEmpty())
158 json[QLatin1String("cached_result_hash")] = cachedResultHash;
159
160 return json;
161}
162
163//=============================================================================================================
164
165MnaPort MnaPort::fromJson(const QJsonObject& json)
166{
167 MnaPort port;
168 port.name = json[QLatin1String("name")].toString();
169 port.dataKind = dataKindFromString(json[QLatin1String("data_kind")].toString());
170 port.direction = portDirFromString(json[QLatin1String("direction")].toString());
171 port.sourceNodeId = json[QLatin1String("source_node")].toString();
172 port.sourcePortName = json[QLatin1String("source_port")].toString();
173 port.streamProtocol = json[QLatin1String("stream_protocol")].toString();
174 port.streamEndpoint = json[QLatin1String("stream_endpoint")].toString();
175 port.streamBufferMs = json[QLatin1String("stream_buffer_ms")].toInt(0);
176 port.cachedResultPath = json[QLatin1String("cached_result")].toString();
177 port.cachedResultHash = json[QLatin1String("cached_result_hash")].toString();
178
179 static const QSet<QString> knownKeys = {
180 QStringLiteral("name"), QStringLiteral("data_kind"),
181 QStringLiteral("direction"), QStringLiteral("source_node"),
182 QStringLiteral("source_port"), QStringLiteral("stream_protocol"),
183 QStringLiteral("stream_endpoint"), QStringLiteral("stream_buffer_ms"),
184 QStringLiteral("cached_result"), QStringLiteral("cached_result_hash")};
185 for (auto it = json.constBegin(); it != json.constEnd(); ++it) {
186 if (!knownKeys.contains(it.key()))
187 port.extras.insert(it.key(), it.value());
188 }
189
190 return port;
191}
192
193//=============================================================================================================
194
195QCborMap MnaPort::toCbor() const
196{
197 QCborMap cbor = QCborMap::fromJsonObject(extras);
198 cbor[QLatin1String("name")] = name;
199 cbor[QLatin1String("data_kind")] = dataKindToString(dataKind);
200 cbor[QLatin1String("direction")] = portDirToString(direction);
201
202 if (!sourceNodeId.isEmpty())
203 cbor[QLatin1String("source_node")] = sourceNodeId;
204 if (!sourcePortName.isEmpty())
205 cbor[QLatin1String("source_port")] = sourcePortName;
206 if (!streamProtocol.isEmpty()) {
207 cbor[QLatin1String("stream_protocol")] = streamProtocol;
208 cbor[QLatin1String("stream_endpoint")] = streamEndpoint;
209 cbor[QLatin1String("stream_buffer_ms")] = streamBufferMs;
210 }
211 if (!cachedResultPath.isEmpty())
212 cbor[QLatin1String("cached_result")] = cachedResultPath;
213 if (!cachedResultHash.isEmpty())
214 cbor[QLatin1String("cached_result_hash")] = cachedResultHash;
215
216 return cbor;
217}
218
219//=============================================================================================================
220
221MnaPort MnaPort::fromCbor(const QCborMap& cbor)
222{
223 MnaPort port;
224 port.name = cbor[QLatin1String("name")].toString();
225 port.dataKind = dataKindFromString(cbor[QLatin1String("data_kind")].toString());
226 port.direction = portDirFromString(cbor[QLatin1String("direction")].toString());
227 port.sourceNodeId = cbor[QLatin1String("source_node")].toString();
228 port.sourcePortName = cbor[QLatin1String("source_port")].toString();
229 port.streamProtocol = cbor[QLatin1String("stream_protocol")].toString();
230 port.streamEndpoint = cbor[QLatin1String("stream_endpoint")].toString();
231 port.streamBufferMs = cbor[QLatin1String("stream_buffer_ms")].toInteger();
232 port.cachedResultPath = cbor[QLatin1String("cached_result")].toString();
233 port.cachedResultHash = cbor[QLatin1String("cached_result_hash")].toString();
234
235 static const QSet<QString> knownKeys = {
236 QStringLiteral("name"), QStringLiteral("data_kind"),
237 QStringLiteral("direction"), QStringLiteral("source_node"),
238 QStringLiteral("source_port"), QStringLiteral("stream_protocol"),
239 QStringLiteral("stream_endpoint"), QStringLiteral("stream_buffer_ms"),
240 QStringLiteral("cached_result"), QStringLiteral("cached_result_hash")};
241 QJsonObject cborJson = cbor.toJsonObject();
242 for (auto it = cborJson.constBegin(); it != cborJson.constEnd(); ++it) {
243 if (!knownKeys.contains(it.key()))
244 port.extras.insert(it.key(), it.value());
245 }
246
247 return port;
248}
Typed input or output slot on a graph node — carries an MnaDataKind, an upstream link,...
MNE Analysis Container Format (mna/mnx).
MnaDataKind
Definition mna_types.h:93
@ FiffRaw
Raw MEG/EEG data (FIFF format).
Definition mna_types.h:94
@ Annotation
FreeSurfer annotation / parcellation.
Definition mna_types.h:105
@ Forward
Forward solution.
Definition mna_types.h:95
@ SourceEstimate
Source-level time series.
Definition mna_types.h:98
@ Custom
User-defined data kind.
Definition mna_types.h:108
@ Bem
BEM model.
Definition mna_types.h:104
@ Inverse
Inverse operator.
Definition mna_types.h:96
@ Surface
Surface mesh (FreeSurfer).
Definition mna_types.h:103
@ Label
ROI label.
Definition mna_types.h:106
@ Volume
MRI volume data.
Definition mna_types.h:102
@ Covariance
Noise or data covariance matrix.
Definition mna_types.h:97
@ RealTimeStream
Live data channel (MNE Scan / LSL / FIFF-RT).
Definition mna_types.h:107
@ Epochs
Epoched data.
Definition mna_types.h:99
@ Evoked
Averaged evoked response.
Definition mna_types.h:100
@ Matrix
Generic Eigen matrix (for intermediate results).
Definition mna_types.h:101
Named, typed port on an MNA graph node with upstream link and optional real-time stream binding.
Definition mna_port.h:65
QString cachedResultHash
SHA-256 for invalidation.
Definition mna_port.h:89
QString name
Port name (unique within a node).
Definition mna_port.h:66
QJsonObject toJson() const
Definition mna_port.cpp:137
QString sourcePortName
Which output port on that node?
Definition mna_port.h:72
MnaDataKind dataKind
Data kind flowing through this port.
Definition mna_port.h:67
MnaPortDir direction
Input or Output.
Definition mna_port.h:68
QCborMap toCbor() const
Definition mna_port.cpp:195
QString cachedResultPath
Relative path to cached result.
Definition mna_port.h:88
int streamBufferMs
Ring-buffer length in ms (0 = unbounded).
Definition mna_port.h:85
QString streamEndpoint
Protocol-specific address (e.g. "localhost:4218" for fiff-rt).
Definition mna_port.h:84
QJsonObject extras
Unknown keys preserved for lossless round-trip.
Definition mna_port.h:90
static MnaPort fromJson(const QJsonObject &json)
Definition mna_port.cpp:165
QString sourceNodeId
Which node produces this input? (empty → graph-level input).
Definition mna_port.h:71
static MnaPort fromCbor(const QCborMap &cbor)
Definition mna_port.cpp:221
QString streamProtocol
"fiff-rt", "lsl", "ftbuffer", "shm", "" = internal signal/slot
Definition mna_port.h:83