OpenWalnut  1.5.0dev
WReaderEEG.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <fstream>
26 #include <string>
27 #include <vector>
28 
29 #include "core/common/WStringUtils.h"
30 #include "core/common/WAssert.h"
31 #include "core/common/WLogger.h"
32 
33 #include "WReaderEEG.h"
34 
35 WReaderEEG::WReaderEEG( std::string fileName )
36  : WReader( fileName )
37 {
38 }
39 
41 {
42  namespace su = string_utils;
43 
44  std::string elcFileName = m_fname;
45  elcFileName.resize( elcFileName.size() - 3 ); // drop suffix
46  elcFileName += "elc"; // add new suffix
47 
48  std::ifstream ifs;
49  ifs.open( elcFileName.c_str(), std::ifstream::in );
50  if( !ifs || ifs.bad() )
51  {
52  WLogger::getLogger()->addLogMessage( "Try load broken file '" + elcFileName + "'", "EEG Reader", LL_ERROR );
53  throw std::runtime_error( "Problem during reading file. Probably file not found." );
54  }
55 
56  std::string line = "";
57 
58  while( ifs.good() && line.substr( 0, 16 ) != "NumberPositions=" ) // go to number of positions
59  {
60  std::getline( ifs, line );
61  if( !ifs.good() )
62  {
63  WLogger::getLogger()->addLogMessage( "Unexpected end of file: " + elcFileName, "EEG Reader", LL_ERROR );
64  }
65  }
66  std::vector< std::string > tokens = su::tokenize( line );
67  size_t numPositions = string_utils::fromString< size_t >( tokens.at( 1 ) );
68 
69  while( ifs.good() && line.substr( 0, 9 ) != "Positions" ) // go to line before start of positions
70  {
71  std::getline( ifs, line );
72  if( !ifs.good() )
73  {
74  WLogger::getLogger()->addLogMessage( "Unexpected end of file: " + elcFileName, "EEG Reader", LL_ERROR );
75  }
76  }
77 
78  size_t posCounter = 0;
79  WEEGElectrodeLibrary elecPos;
80  while( posCounter != numPositions && ifs.good() && line.substr( 0, 9 ) != "Labels" ) // run through all positions
81  {
82  std::getline( ifs, line );
83  if( !ifs.good() )
84  {
85  WLogger::getLogger()->addLogMessage( "Unexpected end of file: " + elcFileName, "EEG Reader", LL_ERROR );
86  }
87  else
88  {
89  ++posCounter;
90  std::vector< std::string > lineTokens = su::tokenize( line, ":" );
91  std::string label = lineTokens.at( 0 );
92  label = su::rTrim( label );
93 // std::cout << "Loading positions: " << label << std::endl;
94  std::vector< std::string > posTokens = su::tokenize( lineTokens.at( 1 ) );
95  double posX = string_utils::fromString< double >( posTokens.at( 1 ) );
96  double posY = string_utils::fromString< double >( posTokens.at( 2 ) );
97  double posZ = string_utils::fromString< double >( posTokens.at( 3 ) );
98  WPosition pos( posX, posY, posZ );
99  elecPos.push_back( WEEGElectrodeObject( pos ) );
100 // std::cout << "Loading positions: " << pos << std::endl;
101  }
102  }
103 
104  std::getline( ifs, line );
105  WAssert( elecPos.size() == numPositions, "Incompatible number of positions and electrodes found." );
106  WAssert( line.substr( 0, 6 ) == "Labels", "Wrong string in file header found." );
107 
108  return elecPos;
109 }
An incomplete implementation to store information about electrodes of EEG data.
Definition: WEEG.h:42
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
This only is a 3d double vector.
WReaderEEG(std::string fileName)
Constructs basic eeg Reader with a file name.
Definition: WReaderEEG.cpp:35
WEEGElectrodeLibrary extractElectrodePositions()
Load electrode positions from ELC file with same name.
Definition: WReaderEEG.cpp:40
Read some data from a given file.
Definition: WReader.h:40
std::string m_fname
Absolute path of the file to read from.
Definition: WReader.h:68
Some utilities for string manipulation and output operations.
Definition: WStringUtils.h:59