OpenWalnut  1.5.0dev
WReaderELC.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 <cstddef>
26 #include <fstream>
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 
33 #include "WReaderELC.h"
34 #include "core/common/WLogger.h"
35 #include "core/common/WStringUtils.h"
36 #include "core/common/math/linearAlgebra/WPosition.h"
37 #include "core/dataHandler/WEEGPositionsLibrary.h"
38 #include "core/dataHandler/exceptions/WDHIOFailure.h"
39 #include "core/dataHandler/exceptions/WDHNoSuchFile.h"
40 #include "core/dataHandler/exceptions/WDHParseError.h"
41 
42 WReaderELC::WReaderELC( std::string fname )
43  : WReader( fname )
44 {
45 }
46 
47 std::shared_ptr< WEEGPositionsLibrary > WReaderELC::read()
48 {
49  std::ifstream ifs;
50  ifs.open( m_fname.c_str(), std::ifstream::in );
51  if( !ifs || ifs.bad() )
52  {
53  throw WDHNoSuchFile( std::string( "Problem loading file " + m_fname + ". Probably file not found." ) );
54  }
55 
56  std::string line;
57  while( ifs.good() && line.substr( 0, 16 ) != "NumberPositions=" ) // go to number of positions
58  {
59  std::getline( ifs, line );
60  if( !ifs.good() )
61  {
62  throw WDHIOFailure( std::string( "Unexpected end of file " + m_fname ) );
63  }
64  }
65 
66  std::vector< std::string > tokens = string_utils::tokenize( line );
67  std::size_t numPositions = string_utils::fromString< std::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  throw WDHIOFailure( std::string( "Unexpected end of file " + m_fname ) );
75  }
76  }
77 
78  std::size_t posCounter = 0;
79  std::vector< WPosition > positions;
80  positions.reserve( numPositions );
81  while( posCounter != numPositions && ifs.good() && line.substr( 0, 6 ) != "Labels" ) // run through all positions
82  {
83  std::getline( ifs, line );
84  if( !ifs.good() )
85  {
86  throw WDHIOFailure( std::string( "Unexpected end of file " + m_fname ) );
87  }
88  else
89  {
90  ++posCounter;
91  std::vector< std::string > lineTokens = string_utils::tokenize( line, ":" );
92 
93  std::vector< std::string > posTokens = string_utils::tokenize( lineTokens.back() );
94  double posX = string_utils::fromString< double >( posTokens.at( posTokens.size() - 3 ) );
95  double posY = string_utils::fromString< double >( posTokens.at( posTokens.size() - 2 ) );
96  double posZ = string_utils::fromString< double >( posTokens.at( posTokens.size() - 1 ) );
97  positions.push_back( WPosition( posX, posY, posZ ) );
98  }
99  }
100 
101  if( positions.size() != numPositions )
102  {
103  throw WDHParseError( std::string( "Could not find correct number of Positions regarding to NumberPositions statement in file " + m_fname ) );
104  }
105 
106  while( ifs.good() && line.substr( 0, 6 ) != "Labels" ) // go to line before start of labels
107  {
108  std::getline( ifs, line );
109  if( !ifs.good() )
110  {
111  throw WDHIOFailure( std::string( "Unexpected end of file " + m_fname ) );
112  }
113  }
114 
115  std::size_t labelCounter = 0;
116  std::map< std::string, WPosition > positionsMap;
117  while( labelCounter != numPositions && ifs.good() ) // run through all labels
118  {
119  std::getline( ifs, line );
120  if( ifs.fail() )
121  {
122  throw WDHIOFailure( std::string( "Unexpected end of file " + m_fname ) );
123  }
124  else
125  {
126  std::vector< std::string > labelTokens = string_utils::tokenize( line );
127  for( std::size_t i = 0; i < labelTokens.size(); ++i )
128  {
129  positionsMap[labelTokens[i]] = positions[labelCounter];
130  ++labelCounter;
131  }
132  }
133  }
134 
135  if( positionsMap.size() != numPositions )
136  {
137  throw WDHParseError( std::string( "Could not find correct number of Labels regarding to NumberPositions statement in file " + m_fname ) );
138  }
139 
140  ifs.close();
141 
142  for( std::map< std::string, WPosition >::const_iterator iter = positionsMap.begin(); iter != positionsMap.end(); ++iter )
143  {
144  wlog::debug( "WReaderELC" ) << iter->first << ": " << iter->second;
145  }
146 
147  return std::shared_ptr< WEEGPositionsLibrary >( new WEEGPositionsLibrary( positionsMap ) );
148 }
Use this for IO error handling.
Definition: WDHIOFailure.h:38
File not found exception.
Definition: WDHNoSuchFile.h:38
Use this for IO error handling.
Definition: WDHParseError.h:38
Class which contains the positions of EEG electrodes by label.
This only is a 3d double vector.
WReaderELC(std::string fname)
Constructs a reader object.
Definition: WReaderELC.cpp:42
std::shared_ptr< WEEGPositionsLibrary > read()
Read the file and create a dataset out of it.
Definition: WReaderELC.cpp:47
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
std::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331