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