MNE-CPP  0.1.9
A Framework for Electrophysiology
layoutloader.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "layoutloader.h"
42 #include <algorithm>
43 #include <fstream>
44 
45 //=============================================================================================================
46 // QT INCLUDES
47 //=============================================================================================================
48 
49 #include <QIODevice>
50 #include <QTextStream>
51 #include <QFile>
52 #include <QDebug>
53 #include <QRegularExpression>
54 
55 //=============================================================================================================
56 // USED NAMESPACES
57 //=============================================================================================================
58 
59 using namespace UTILSLIB;
60 
61 //=============================================================================================================
62 // DEFINE MEMBER METHODS
63 //=============================================================================================================s
64 
65 bool LayoutLoader::readAsaElcFile(const QString& path,
66  QStringList &channelNames,
67  QList<QVector<float> > &location3D,
68  QList<QVector<float> > &location2D,
69  QString &unit)
70 {
71  //Open .elc file
72  if(!path.contains(".elc"))
73  return false;
74 
75  QFile file(path);
76  if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
77  qDebug()<<"Error opening elc file";
78  return false;
79  }
80 
81  //Start reading from file
82  double numberElectrodes;
83  QTextStream in(&file);
84  bool read2D = false;
85 
86  while(!in.atEnd())
87  {
88  QString line = in.readLine();
89 
90  QStringList fields = line.split(QRegularExpression("\\s+"));
91 
92  //Delete last element if it is a blank character
93  if(fields.at(fields.size()-1) == "")
94  fields.removeLast();
95 
96  if(!line.contains("#")) //Skip commented areas in file
97  {
98  //Read number of electrodes
99  if(line.contains("NumberPositions"))
100  numberElectrodes = fields.at(1).toDouble();
101 
102  //Read the unit of the position values
103  if(line.contains("UnitPosition"))
104  unit = fields.at(1);
105 
106  //Read actual electrode positions
107  if(line.contains("Positions2D"))
108  read2D = true;
109 
110  if(line.contains(":") && !read2D) //Read 3D positions
111  {
112  channelNames.push_back(fields.at(0));
113  QVector<float> posTemp;
114 
115  posTemp.push_back(fields.at(fields.size()-3).toDouble()); //x
116  posTemp.push_back(fields.at(fields.size()-2).toDouble()); //y
117  posTemp.push_back(fields.at(fields.size()-1).toDouble()); //z
118 
119  location3D.append(posTemp);
120  }
121 
122  if(line.contains(":") && read2D) //Read 2D positions
123  {
124  QVector<float> posTemp;
125  posTemp.push_back(fields.at(fields.size()-2).toDouble()); //x
126  posTemp.push_back(fields.at(fields.size()-1).toDouble()); //y
127  location2D.append(posTemp);
128  }
129 
130  //Read channel names
131  if(line.contains("Labels"))
132  {
133  line = in.readLine();
134  fields = line.split(QRegularExpression("\\s+"));
135 
136  //Delete last element if it is a blank character
137  if(fields.at(fields.size()-1) == "")
138  fields.removeLast();
139 
140  channelNames = fields;
141  }
142  }
143  }
144 
145  Q_UNUSED(numberElectrodes);
146 
147  file.close();
148 
149  return true;
150 }
151 
152 //=============================================================================================================
153 
154 bool LayoutLoader::readAsaElcFile(const std::string &path,
155  std::vector<std::string> &channelNames,
156  std::vector<std::vector<float> > &location3D,
157  std::vector<std::vector<float> > &location2D,
158  std::string &unit)
159 {
160 
161  if(path.find(".elc") == std::string::npos){
162  return false;
163  }
164 
165  std::ifstream inFile(path);
166 
167  if(!inFile.is_open()){
168  qDebug()<<"Error opening elc file";
169  return false;
170  }
171 
172  //Start reading from file
173  double numberElectrodes;
174  bool read2D = false;
175 
176  std::string line;
177 
178  while(std::getline(inFile, line)){
179  if(line.find('#') == std::string::npos){
180  std::vector<std::string> elements;
181  std::stringstream stream{line};
182  std::string element;
183 
184  stream >> std::ws;
185  while(stream >> element){
186  elements.push_back(std::move(element));
187  stream >> std::ws;
188  }
189 
190  //Read number of electrodes
191  if(line.find("NumberPositions") != std::string::npos)
192  numberElectrodes = std::stod(elements.at(1));
193 
194  //Read the unit of the position values
195  if(line.find("UnitPosition") != std::string::npos)
196  unit = elements.at(1);
197 
198  //Read actual electrode positions
199  if(line.find("Positions2D") != std::string::npos)
200  read2D = true;
201 
202  if(line.find(':') != std::string::npos && !read2D) //Read 3D positions
203  {
204  channelNames.push_back(elements.at(0));
205  std::vector<float> posTemp;
206 
207  posTemp.push_back(std::stod(elements.at(elements.size()-3))); //x
208  posTemp.push_back(std::stod(elements.at(elements.size()-2))); //y
209  posTemp.push_back(std::stod(elements.at(elements.size()-1))); //z
210 
211  location3D.push_back(std::move(posTemp));
212  }
213 
214  if(line.find(":") != std::string::npos && read2D) //Read 2D positions
215  {
216  std::vector<float> posTemp;
217  posTemp.push_back(std::stod(elements.at(elements.size()-2))); //x
218  posTemp.push_back(std::stod(elements.at(elements.size()-1))); //y
219  location2D.push_back(std::move(posTemp));
220  }
221 
222  //Read channel names
223  if(line.find("Labels") != std::string::npos)
224  {
225  std::getline(inFile, line);
226  std::stringstream channels{line};
227  std::vector<std::string> listOfNames;
228 
229  std::string channelName;
230 
231  channels >> std::ws;
232  while(channels >> channelName){
233  listOfNames.push_back(std::move(channelName));
234  channels >> std::ws;
235  }
236 
237  channelNames = std::move(listOfNames);
238  }
239  }
240  }
241 
242  Q_UNUSED(numberElectrodes);
243 
244  return true;
245 }
246 
247 //=============================================================================================================
248 
249 bool LayoutLoader::readMNELoutFile(const QString &path, QMap<QString, QPointF> &channelData)
250 {
251  //Open .elc file
252  if(!path.contains(".lout"))
253  return false;
254 
255  channelData.clear();
256 
257  QFile file(path);
258  if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
259  qDebug()<<"Error opening mne lout file";
260  return false;
261  }
262 
263  //Start reading from file
264  QTextStream in(&file);
265 
266  //skip first line
267  in.readLine();
268 
269  while(!in.atEnd()) {
270  QString line = in.readLine();
271 
272  QStringList fields = line.split(QRegularExpression("\\s+"));
273 
274  //Delete last element if it is a blank character
275  if(fields.at(fields.size()-1) == "")
276  fields.removeLast();
277 
278  QPointF posTemp;
279  posTemp.setX(fields.at(1).toDouble()); //x
280  posTemp.setY(fields.at(2).toDouble()); //y
281 
282  //Create channel data map entry
283  QString key = QString("%1 %2").arg(fields.at(fields.size()-2)).arg(fields.at(fields.size()-1));
284  channelData.insert(key, posTemp);
285  }
286 
287  file.close();
288 
289  return true;
290 }
291 
292 //=============================================================================================================
293 
294 bool LayoutLoader::readMNELoutFile(const std::string &path, QMap<std::string, QPointF> &channelData)
295 {
296 
297  if(path.find(".lout") == std::string::npos){
298  return false;
299  }
300 
301  channelData.clear();
302  std::ifstream inFile(path);
303 
304  if(!inFile.is_open()){
305  qDebug()<<"Error opening mne lout file";
306  return false;
307  }
308 
309  std::string line;
310 
311  while(std::getline(inFile, line)){
312  if(line.find('#') != std::string::npos){
313  std::vector<std::string> elements;
314  std::stringstream stream{line};
315  std::string element;
316 
317  stream >> std::ws;
318  while(stream >> element){
319  elements.push_back(std::move(element));
320  stream >> std::ws;
321  }
322 
323  QPointF posTemp;
324  posTemp.setX(std::stod(elements.at(1))); //x
325  posTemp.setY(std::stod(elements.at(2))); //y
326 
327  //Create channel data map entry
328  std::string key{elements.at(elements.size() - 2) + " " + elements.at(elements.size() - 1)};
329  channelData.insert(key, posTemp);
330  }
331  }
332 
333  return true;
334 }
layoutloader.h
LayoutLoader class declaration.
UTILSLIB::LayoutLoader::readMNELoutFile
static bool readMNELoutFile(const QString &path, QMap< QString, QPointF > &channelData)
Definition: layoutloader.cpp:249
UTILSLIB::LayoutLoader::readAsaElcFile
static bool readAsaElcFile(const QString &path, QStringList &channelNames, QList< QVector< float > > &location3D, QList< QVector< float > > &location2D, QString &unit)
Definition: layoutloader.cpp:65