v2.0.0
Loading...
Searching...
No Matches
selectionio.cpp
Go to the documentation of this file.
1//=============================================================================================================
23
24//=============================================================================================================
25// INCLUDES
26//=============================================================================================================
27
28#include "selectionio.h"
29
30#include <iostream>
31#include <fstream>
32#include <sstream>
33
34//=============================================================================================================
35// QT INCLUDES
36//=============================================================================================================
37
38#include <QFileInfo>
39#include <QTextStream>
40#include <QFile>
41#include <QDebug>
42#include <QScreen>
43
44//=============================================================================================================
45// USED NAMESPACES
46//=============================================================================================================
47
48using namespace UTILSLIB;
49
50//=============================================================================================================
51// DEFINE MEMBER METHODS
52//=============================================================================================================
53
57
58//=============================================================================================================
59
60bool SelectionIO::readMNESelFile(QString path, QMultiMap<QString,QStringList> &selectionMap)
61{
62 //Open .sel file
63 if(!path.contains(".sel"))
64 return false;
65
66 //clear the map first
67 selectionMap.clear();
68
69 QFile file(path);
70 if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
71 qDebug()<<"Error opening selection file";
72 return false;
73 }
74
75 //Start reading from file
76 QTextStream in(&file);
77
78 while(!in.atEnd()) {
79 QString line = in.readLine();
80
81 if(line.contains("%") == false && line.contains(":") == true) //Skip commented areas in file
82 {
83 QStringList firstSplit = line.split(":");
84
85 //Create new key
86 QString key = firstSplit.at(0);
87
88 QStringList secondSplit = firstSplit.at(1).split("|");
89
90 //Delete last element if it is a blank character
91 if(secondSplit.at(secondSplit.size()-1) == "")
92 secondSplit.removeLast();
93
94 //Add to map
95 selectionMap.insert(key, secondSplit);
96 }
97 }
98
99 file.close();
100
101 return true;
102}
103
104//=============================================================================================================
105
106bool SelectionIO::readMNESelFile(const std::string& path, std::multimap<std::string,std::vector<std::string>> &selectionMap)
107{
108 //Open .sel file
109 if(path.find(".sel") == std::string::npos)
110 return false;
111
112 //clear the map first
113 selectionMap.clear();
114
115 std::ifstream inFile(path);
116 if(!inFile.is_open()){
117 qDebug()<<"Error opening selection file";
118 return false;
119 }
120
121 std::string line;
122
123 while(std::getline(inFile, line)){
124 if(line.find('%') == std::string::npos && line.find(':') != std::string::npos){
125 // Split on ':' → key : channels
126 auto colonPos = line.find(':');
127 std::string key = line.substr(0, colonPos);
128 std::string channelsPart = line.substr(colonPos + 1);
129
130 // Split channels on '|'
131 std::vector<std::string> channels;
132 std::stringstream stream{channelsPart};
133 std::string token;
134 while(std::getline(stream, token, '|')){
135 channels.push_back(token);
136 }
137
138 // Remove trailing empty element
139 if(!channels.empty() && channels.back().empty()){
140 channels.pop_back();
141 }
142
143 selectionMap.insert({key, channels});
144 }
145 }
146
147 return true;
148}
149
150//=============================================================================================================
151
152bool SelectionIO::readBrainstormMonFile(QString path, QMultiMap<QString,QStringList> &selectionMap)
153{
154 //Open .sel file
155 if(!path.contains(".mon"))
156 return false;
157
158 //clear the map first
159 selectionMap.clear();
160
161 QFile file(path);
162 if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
163 qDebug()<<"Error opening montage file";
164 return false;
165 }
166
167 //Start reading from file
168 QTextStream in(&file);
169 QString groupName = in.readLine();
170 QStringList channels;
171
172 while(!in.atEnd()) {
173 QString line = in.readLine();
174
175 if(line.contains(":") == true) {
176 QStringList secondSplit = line.split(":");
177 QString key = secondSplit.at(0);
178 channels.append(key);
179 }
180 }
181
182 //Add to map
183 selectionMap.insert(groupName, channels);
184
185 file.close();
186
187 return true;
188}
189
190//=============================================================================================================
191
192bool SelectionIO::readBrainstormMonFile(const std::string& path, std::multimap<std::string,std::vector<std::string>>& selectionMap)
193{
194 //Open file
195 if(path.find(".mon") == std::string::npos)
196 return false;
197
198 //clear the map first
199 selectionMap.clear();
200
201 std::ifstream inFile(path);
202 if(!inFile.is_open()){
203 qDebug()<<"Error opening montage file";
204 return false;
205 }
206 std::vector<std::string> channels;
207
208 std::string groupName;
209 std::getline(inFile, groupName);
210
211 std::string line;
212 while(std::getline(inFile, line)){
213 if(line.find(':') != std::string::npos){
214 auto colonPos = line.find(':');
215 std::string channelName = line.substr(0, colonPos);
216 channels.push_back(channelName);
217 }
218 }
219
220 selectionMap.insert({groupName, channels});
221
222 return true;
223}
224
225//=============================================================================================================
226
227bool SelectionIO::writeMNESelFile(QString path, const QMultiMap<QString,QStringList> &selectionMap)
228{
229 //Open .sel file
230 if(!path.contains(".sel"))
231 return false;
232
233 QFile file(path);
234 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
235 qDebug()<<"Error opening sel file for writing";
236 return false;
237 }
238
239 //Write selections to file
240 QTextStream out(&file);
241
242 QMultiMap<QString, QStringList>::const_iterator i = selectionMap.constBegin();
243 while (i != selectionMap.constEnd()) {
244 out << i.key() << ":";
245
246 for(int u=0; u<i.value().size() ; u++)
247 out << i.value().at(u) << "|";
248
249 out << "\n" << "\n";
250
251 ++i;
252 }
253
254 file.close();
255
256 return true;
257}
258
259//=============================================================================================================
260
261bool SelectionIO::writeMNESelFile(const std::string& path, const std::map<std::string,std::vector<std::string>>& selectionMap)
262{
263 //Open .sel file
264 if(path.find(".sel") == std::string::npos)
265 return false;
266
267 std::ofstream outFile(path);
268 if (!outFile.is_open()){
269 qDebug()<<"Error opening sel file for writing";
270 return false;
271 }
272
273 for(auto& mapElement : selectionMap){
274 outFile << mapElement.first << ":";
275 for(auto& vectorElement : mapElement.second){
276 outFile << vectorElement << "|";
277 }
278 outFile << "\n" << "\n";
279 }
280
281 return true;
282}
283
284//=============================================================================================================
285
286bool SelectionIO::writeBrainstormMonFiles(QString path, const QMultiMap<QString,QStringList> &selectionMap)
287{
288 for(auto i = selectionMap.constBegin(); i != selectionMap.constEnd(); i++) {
289 QFileInfo fileInfo(path);
290
291 QString newPath = QString("%1/%2.mon").arg(fileInfo.absolutePath()).arg(i.key());
292
293 //std::cout<<newPath.toStdString()<<std::endl;
294
295 QFile file(newPath);
296 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
297 qDebug()<<"Error opening mon file for writing";
298 return false;
299 }
300
301 //Write selections to file
302 QTextStream out(&file);
303
304 out<<i.key()<<"\n";
305
306 for(int u=0; u<i.value().size() ; u++)
307 out << i.value().at(u) << " : " << i.value().at(u) << "\n";
308
309 file.close();
310 }
311
312 return true;
313}
314
315//=============================================================================================================
316
317bool SelectionIO::writeBrainstormMonFiles(const std::string& path, const std::map<std::string,std::vector<std::string>>& selectionMap)
318{
319 for(auto& mapElement : selectionMap){
320 std::string newPath{path.substr(0, path.find_last_of("/") + 1) + mapElement.first + ".mon"};
321 std::ofstream outFile(newPath);
322 if(!outFile.is_open()){
323 qDebug()<<"Error opening mon file for writing";
324 return false;
325 }
326
327 outFile << mapElement.first << "\n";
328 for(auto& vectorElement : mapElement.second){
329 outFile << vectorElement << " : " << vectorElement << "\n";
330 }
331 }
332
333 return true;
334}
Reader / writer for MNE .sel channel-selection files and Brainstorm .mon montage files.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
static bool readBrainstormMonFile(QString path, QMultiMap< QString, QStringList > &selectionMap)
static bool writeBrainstormMonFiles(QString path, const QMultiMap< QString, QStringList > &selectionMap)
static bool readMNESelFile(QString path, QMultiMap< QString, QStringList > &selectionMap)
static bool writeMNESelFile(QString path, const QMultiMap< QString, QStringList > &selectionMap)