MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
selectionio.cpp
Go to the documentation of this file.
1//=============================================================================================================
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 std::stringstream stream{line};
139 std::vector<std::string> firstSplit;
140 for (std::string element; std::getline(stream, line, ':');){
141 firstSplit.push_back(element);
142 }
143 std::string key = firstSplit.at(0);
144
145 std::vector<std::string> secondSplit;
146 for (std::string element; std::getline(stream, line, '|');){
147 secondSplit.push_back(element);
148 }
149 if(secondSplit.back() == ""){
150 secondSplit.pop_back();
151 }
152 selectionMap.insert({key, secondSplit});
153 }
154 }
155
156 return true;
157}
158
159//=============================================================================================================
160
161bool SelectionIO::readBrainstormMonFile(QString path, QMultiMap<QString,QStringList> &selectionMap)
162{
163 //Open .sel file
164 if(!path.contains(".mon"))
165 return false;
166
167 //clear the map first
168 selectionMap.clear();
169
170 QFile file(path);
171 if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
172 qDebug()<<"Error opening montage file";
173 return false;
174 }
175
176 //Start reading from file
177 QTextStream in(&file);
178 QString groupName = in.readLine();
179 QStringList channels;
180
181 while(!in.atEnd()) {
182 QString line = in.readLine();
183
184 if(line.contains(":") == true) {
185 QStringList secondSplit = line.split(":");
186 QString key = secondSplit.at(0);
187 channels.append(key);
188 }
189 }
190
191 //Add to map
192 selectionMap.insert(groupName, channels);
193
194 file.close();
195
196 return true;
197}
198
199//=============================================================================================================
200
201bool SelectionIO::readBrainstormMonFile(const std::string& path, std::multimap<std::string,std::vector<std::string>>& selectionMap)
202{
203 //Open file
204 if(path.find(".mon") != std::string::npos)
205 return false;
206
207 //clear the map first
208 selectionMap.clear();
209
210 std::ifstream inFile(path);
211 if(!inFile.is_open()){
212 qDebug()<<"Error opening montage file";
213 return false;
214 }
215 std::vector<std::string> channels;
216
217 std::string groupName;
218 std::getline(inFile, groupName);
219
220 std::string line;
221 while(std::getline(inFile, line)){
222 if(line.find(':') != std::string::npos){
223 std::stringstream stream{line};
224 std::vector<std::string> split;
225 for (std::string element; std::getline(stream, line, ':');){
226 split.push_back(std::move(element));
227 }
228 channels.push_back(split.at(0));
229 }
230 }
231
232 selectionMap.insert({groupName, channels});
233
234 return true;
235}
236
237//=============================================================================================================
238
239bool SelectionIO::writeMNESelFile(QString path, const QMultiMap<QString,QStringList> &selectionMap)
240{
241 //Open .sel file
242 if(!path.contains(".sel"))
243 return false;
244
245 QFile file(path);
246 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
247 qDebug()<<"Error opening sel file for writing";
248 return false;
249 }
250
251 //Write selections to file
252 QTextStream out(&file);
253
254 QMultiMap<QString, QStringList>::const_iterator i = selectionMap.constBegin();
255 while (i != selectionMap.constEnd()) {
256 out << i.key() << ":";
257
258 for(int u=0; u<i.value().size() ; u++)
259 out << i.value().at(u) << "|";
260
261 out << "\n" << "\n";
262
263 ++i;
264 }
265
266 file.close();
267
268 return true;
269}
270
271//=============================================================================================================
272
273bool SelectionIO::writeMNESelFile(const std::string& path, const std::map<std::string,std::vector<std::string>>& selectionMap)
274{
275 //Open .sel file
276 if(path.find(".sel") == std::string::npos)
277 return false;
278
279 std::ofstream outFile(path);
280 if (outFile.is_open()){
281 qDebug()<<"Error opening sel file for writing";
282 return false;
283 }
284
285 for(auto& mapElement : selectionMap){
286 outFile << mapElement.first << ":";
287 for(auto& vectorElement : mapElement.second){
288 outFile << vectorElement << "|";
289 }
290 outFile << "\n" << "\n";
291 }
292
293 return true;
294}
295
296//=============================================================================================================
297
298bool SelectionIO::writeBrainstormMonFiles(QString path, const QMultiMap<QString,QStringList> &selectionMap)
299{
300 //Open .sel file
301 if(!path.contains(".mon"))
302 return false;
303
304 for(auto i = selectionMap.constBegin(); i != selectionMap.constEnd(); i++) {
305 QFileInfo fileInfo(path);
306
307 QString newPath = QString("%1/%2.mon").arg(fileInfo.absolutePath()).arg(i.key());
308
309 //std::cout<<newPath.toStdString()<<std::endl;
310
311 QFile file(newPath);
312 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
313 qDebug()<<"Error opening mon file for writing";
314 return false;
315 }
316
317 //Write selections to file
318 QTextStream out(&file);
319
320 out<<i.key()<<"\n";
321
322 for(int u=0; u<i.value().size() ; u++)
323 out << i.value().at(u) << " : " << i.value().at(u) << "\n";
324
325 file.close();
326 }
327
328 return true;
329}
330
331//=============================================================================================================
332
333bool SelectionIO::writeBrainstormMonFiles(const std::string& path, const std::map<std::string,std::vector<std::string>>& selectionMap)
334{
335 //Open file
336 if(path.find(".mon") == std::string::npos)
337 return false;
338
339 for(auto& mapElement : selectionMap){
340 //logic of using parent_path here might not be "correct"
341 std::string newPath{path.substr(0, path.find_last_of("/") + 1) + mapElement.first + ".mon"};
342 std::ofstream outFile(newPath);
343 if(!outFile.is_open()){
344 qDebug()<<"Error opening mon file for writing";
345 return false;
346 }
347
348 outFile << mapElement.first << "\n";
349 for(auto& vectorElement : mapElement.second){
350 outFile << vectorElement << " : " << vectorElement << "\n";
351 }
352 }
353
354 return true;
355}
SelectionIO class declaration.
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)