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