OpenWalnut  1.5.0dev
WReaderEEGASCII.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 <iostream>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include "WReaderEEGASCII.h"
32 #include "core/common/WAssert.h"
33 #include "core/common/WException.h"
34 #include "core/common/WStringUtils.h"
35 #include "core/dataHandler/WEEG.h"
36 #include "core/dataHandler/WSubject.h"
37 
38 WReaderEEGASCII::WReaderEEGASCII( std::string fileName )
39  : WReaderEEG( fileName )
40 {
41 }
42 
43 std::shared_ptr< WDataSet > WReaderEEGASCII::load()
44 {
45  std::ifstream in( m_fname.c_str() );
46  if( in.fail() )
47  throw WException( std::string( "Could not read file \"" + m_fname + "\"" ) );
48 
49  std::string tmp;
50  getline( in, tmp );
51  std::vector< std::string > tokens = string_utils::tokenize( tmp );
52 
53  const unsigned int nbChannels = tokens.size() / 3;
54  WEEGChannelLabels labels( nbChannels );
55  for( unsigned int i = 0; i < nbChannels; ++i )
56  {
57  labels[i].first = tokens[3*i] + " " + tokens[3*i+1] + " " + tokens[3*i+2];
58  // TODO(wiebel): set second channel
59  }
60 
61  WEEGSegmentArray segments( 1 );
62  segments[0].clear();
63  for( unsigned int i = 0; i < nbChannels; ++i )
64  {
65  segments[0].push_back( WEEGElectrode( 0 ) );
66  }
67 
68  getline( in, tmp );
69  while( !in.eof() )
70  {
71  tokens = string_utils::tokenize( tmp );
72  WAssert( tokens.size() == nbChannels, "Error." );
73  for( unsigned int i = 0; i < nbChannels; ++i )
74  {
75  segments[0][i].push_back( string_utils::fromString< double >( tokens[i].c_str() ) );
76  }
77  getline( in, tmp );
78  }
79 
80  in.close();
81 
82 
83  WEEGElectrodeLibrary lib; // TODO(wiebel): this is a dummy
84 
85  std::shared_ptr< WEEG > eeg( new WEEG( segments, lib, labels ) );
86  eeg->setFilename( m_fname );
87  return eeg;
88 }
Contains EEG recording data.
Definition: WEEG.h:71
Basic exception handler.
Definition: WException.h:39
WReaderEEGASCII(std::string fileName)
Constructs a loader to be executed in its own thread and sets the data needed for the loader when exe...
virtual std::shared_ptr< WDataSet > load()
Loads the dataset.
Abstract base class for all Readers who handle with EEG data.
Definition: WReaderEEG.h:39
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).