v2.0.0
Loading...
Searching...
No Matches
layoutloader.cpp
Go to the documentation of this file.
1//=============================================================================================================
22
23//=============================================================================================================
24// INCLUDES
25//=============================================================================================================
26
27#include "layoutloader.h"
28#include <algorithm>
29#include <fstream>
30
31//=============================================================================================================
32// QT INCLUDES
33//=============================================================================================================
34
35#include <QIODevice>
36#include <QTextStream>
37#include <QFile>
38#include <QDebug>
39#include <QRegularExpression>
40
41//=============================================================================================================
42// USED NAMESPACES
43//=============================================================================================================
44
45using namespace UTILSLIB;
46
47//=============================================================================================================
48// DEFINE MEMBER METHODS
49//=============================================================================================================s
50
51bool LayoutLoader::readAsaElcFile(const QString& path,
52 QStringList& channelNames,
53 QList<QVector<float>>& location3D,
54 QList<QVector<float>>& location2D,
55 QString& unit)
56{
57 //Open .elc file
58 if (!path.contains(".elc"))
59 return false;
60
61 QFile file(path);
62 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
63 qDebug() << "Error opening elc file";
64 return false;
65 }
66
67 //Start reading from file
68 double numberElectrodes;
69 QTextStream in(&file);
70 bool read2D = false;
71
72 while (!in.atEnd()) {
73 QString line = in.readLine();
74
75 QStringList fields = line.split(QRegularExpression("\\s+"));
76
77 //Delete last element if it is a blank character
78 if (fields.at(fields.size() - 1) == "")
79 fields.removeLast();
80
81 if (!line.contains("#")) //Skip commented areas in file
82 {
83 //Read number of electrodes
84 if (line.contains("NumberPositions"))
85 numberElectrodes = fields.at(1).toDouble();
86
87 //Read the unit of the position values
88 if (line.contains("UnitPosition"))
89 unit = fields.at(1);
90
91 //Read actual electrode positions
92 if (line.contains("Positions2D"))
93 read2D = true;
94
95 if (line.contains(":") && !read2D) //Read 3D positions
96 {
97 channelNames.push_back(fields.at(0));
98 QVector<float> posTemp;
99
100 posTemp.push_back(fields.at(fields.size() - 3).toDouble()); //x
101 posTemp.push_back(fields.at(fields.size() - 2).toDouble()); //y
102 posTemp.push_back(fields.at(fields.size() - 1).toDouble()); //z
103
104 location3D.append(posTemp);
105 }
106
107 if (line.contains(":") && read2D) //Read 2D positions
108 {
109 QVector<float> posTemp;
110 posTemp.push_back(fields.at(fields.size() - 2).toDouble()); //x
111 posTemp.push_back(fields.at(fields.size() - 1).toDouble()); //y
112 location2D.append(posTemp);
113 }
114
115 //Read channel names
116 if (line.contains("Labels")) {
117 line = in.readLine();
118 fields = line.split(QRegularExpression("\\s+"));
119
120 //Delete last element if it is a blank character
121 if (fields.at(fields.size() - 1) == "")
122 fields.removeLast();
123
124 channelNames = fields;
125 }
126 }
127 }
128
129 Q_UNUSED(numberElectrodes);
130
131 file.close();
132
133 return true;
134}
135
136//=============================================================================================================
137
138bool LayoutLoader::readAsaElcFile(const std::string& path,
139 std::vector<std::string>& channelNames,
140 std::vector<std::vector<float>>& location3D,
141 std::vector<std::vector<float>>& location2D,
142 std::string& unit)
143{
144 if (path.find(".elc") == std::string::npos) {
145 return false;
146 }
147
148 std::ifstream inFile(path);
149
150 if (!inFile.is_open()) {
151 qDebug() << "Error opening elc file";
152 return false;
153 }
154
155 //Start reading from file
156 double numberElectrodes;
157 bool read2D = false;
158
159 std::string line;
160
161 while (std::getline(inFile, line)) {
162 if (line.find('#') == std::string::npos) {
163 std::vector<std::string> elements;
164 std::stringstream stream{line};
165 std::string element;
166
167 stream >> std::ws;
168 while (stream >> element) {
169 elements.push_back(std::move(element));
170 stream >> std::ws;
171 }
172
173 //Read number of electrodes
174 if (line.find("NumberPositions") != std::string::npos)
175 numberElectrodes = std::stod(elements.at(1));
176
177 //Read the unit of the position values
178 if (line.find("UnitPosition") != std::string::npos)
179 unit = elements.at(1);
180
181 //Read actual electrode positions
182 if (line.find("Positions2D") != std::string::npos)
183 read2D = true;
184
185 if (line.find(':') != std::string::npos && !read2D) //Read 3D positions
186 {
187 channelNames.push_back(elements.at(0));
188 std::vector<float> posTemp;
189
190 posTemp.push_back(std::stod(elements.at(elements.size() - 3))); //x
191 posTemp.push_back(std::stod(elements.at(elements.size() - 2))); //y
192 posTemp.push_back(std::stod(elements.at(elements.size() - 1))); //z
193
194 location3D.push_back(std::move(posTemp));
195 }
196
197 if (line.find(":") != std::string::npos && read2D) //Read 2D positions
198 {
199 std::vector<float> posTemp;
200 posTemp.push_back(std::stod(elements.at(elements.size() - 2))); //x
201 posTemp.push_back(std::stod(elements.at(elements.size() - 1))); //y
202 location2D.push_back(std::move(posTemp));
203 }
204
205 //Read channel names
206 if (line.find("Labels") != std::string::npos) {
207 std::getline(inFile, line);
208 std::stringstream channels{line};
209 std::vector<std::string> listOfNames;
210
211 std::string channelName;
212
213 channels >> std::ws;
214 while (channels >> channelName) {
215 listOfNames.push_back(std::move(channelName));
216 channels >> std::ws;
217 }
218
219 channelNames = std::move(listOfNames);
220 }
221 }
222 }
223
224 Q_UNUSED(numberElectrodes);
225
226 return true;
227}
Reader for ANT .elc electrode files used by the topographic plotting widgets.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
static bool readAsaElcFile(const QString &path, QStringList &channelNames, QList< QVector< float > > &location3D, QList< QVector< float > > &location2D, QString &unit)