OpenWalnut  1.5.0dev
WMReadDipoles.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 <algorithm>
26 #include <fstream>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include "WMReadDipoles.h"
32 #include "core/common/WPathHelper.h"
33 #include "core/dataHandler/WDataSetDipoles.h"
34 #include "core/dataHandler/exceptions/WDHIOFailure.h"
35 #include "core/kernel/WKernel.h"
36 #include "modules/readDipoles/WMReadDipoles.xpm"
37 
38 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
39 W_LOADABLE_MODULE( WMReadDipoles )
40 
42  WModule()
43 {
44 }
45 
47 {
48  // Cleanup!
49 }
50 
51 std::shared_ptr< WModule > WMReadDipoles::factory() const
52 {
53  return std::shared_ptr< WModule >( new WMReadDipoles() );
54 }
55 
56 const char** WMReadDipoles::getXPMIcon() const
57 {
58  return WMReadDipoles_xpm; // Please put a real icon here.
59 }
60 const std::string WMReadDipoles::getName() const
61 {
62  return "Read Dipoles";
63 }
64 
65 const std::string WMReadDipoles::getDescription() const
66 {
67  return "Reading \".dip\" files containing position and additional information on dipoles reconstructed from EEG.";
68 }
69 
71 {
72  m_dipoles = std::shared_ptr< WModuleOutputData< WDataSetDipoles > >( new WModuleOutputData< WDataSetDipoles >(
73  shared_from_this(), "Dipoles", "The loaded dipoles reconstructed from EEG." ) );
75 
77 }
78 
80 {
81  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
82  m_dataFile = m_properties->addProperty( "File", "", WPathHelper::getAppPath(), m_propCondition );
84 
85  m_metaFile = m_properties->addProperty( "Use meta file", "File containing multiple filenames to load.", true );
86 
88 }
89 
91 {
92  // Put the code for your requirements here. See "src/modules/template/" for an extensively documented example.
93 }
94 
96 {
98  ready();
99  while( !m_shutdownFlag() )
100  {
102 
103  if( m_shutdownFlag() )
104  {
105  break;
106  }
107 
108  std::shared_ptr< WProgress > progress( new WProgress( "Read Dipoles", 2 ) );
109  ++*progress;
110  if( !m_metaFile->get() )
111  {
112  m_dataSet = readFiles( std::vector< std::string >( 1, m_dataFile->get().string() ) );
113  }
114  else
115  {
116  m_dataSet = readMetaData( m_dataFile->get().string() );
117  }
118  ++*progress;
119  m_dipoles->updateData( m_dataSet );
120  progress->finish();
121  }
122 }
123 
124 
125 std::shared_ptr< WDataSetDipoles > WMReadDipoles::readMetaData( std::string filename )
126 {
127  std::vector< std::string > names;
128  std::ifstream ifs;
129  ifs.open( filename.c_str(), std::ifstream::in );
130  if( !ifs || ifs.bad() )
131  {
132  throw WDHIOFailure( std::string( "Internal error while opening file" ) );
133  }
134  std::string line;
135 
136  while( std::getline( ifs, line ) )
137  {
138  names.push_back( line );
139  }
140  ifs.close();
141 
142  return readFiles( names );
143 }
144 
145 void WMReadDipoles::readFile( std::string filename, WPosition* pos, std::vector< float >* times, std::vector< float >* magnitudes,
146  size_t* firstTimeStep, size_t* lastTimeStep )
147 {
148  std::ifstream ifs;
149  ifs.open( filename.c_str(), std::ifstream::in );
150  if( !ifs || ifs.bad() )
151  {
152  throw WDHIOFailure( std::string( "Internal error while opening file" ) );
153  }
154 
155  std::string line;
156  std::vector< std::string > tokens;
157 
158  std::getline( ifs, line, '\n' );
159 
160  while( line.find( "NumberTimeSteps" ) )
161  {
162  std::getline( ifs, line, '\n' );
163  }
164  tokens = string_utils::tokenize( line );
165  size_t nbTimeSteps = string_utils::fromString< size_t >( tokens[1].c_str() );
166 
167  std::getline( ifs, line, '\n' );
168 
169  while( line.find( "TimeSteps" ) )
170  {
171  std::getline( ifs, line, '\n' );
172  }
173  tokens = string_utils::tokenize( line );
174  tokens = string_utils::tokenize( tokens[1], "()" );
175  float timeFirst = string_utils::fromString< float >( tokens[0].c_str() );
176  float timeDistance = string_utils::fromString< float >( tokens[1].c_str() );
177  float timeLast = string_utils::fromString< float >( tokens[2].c_str() );
178  times->resize( nbTimeSteps );
179  for( size_t timeStep = 0; timeStep < nbTimeSteps; ++timeStep )
180  {
181  (*times)[timeStep] = timeFirst + timeStep * timeDistance;
182  }
183  WAssert( std::abs( (*times)[nbTimeSteps-1] - timeLast ) < 1e-4, "Error during filling times vector." );
184 
185  while( line.find( "FirstTimeStep" ) )
186  {
187  std::getline( ifs, line, '\n' );
188  }
189  tokens = string_utils::tokenize( line );
190  *firstTimeStep = std::max( string_utils::fromString< size_t >( tokens[1].c_str() ) - 1u, size_t( 0u ) );
191 
192  while( line.find( "LastTimeStep" ) )
193  {
194  std::getline( ifs, line, '\n' );
195  }
196  tokens = string_utils::tokenize( line );
197  *lastTimeStep = std::min( string_utils::fromString< size_t >( tokens[1].c_str() ) + 1u, nbTimeSteps - 1u );
198 
199  while( line.find( "PositionsFixed" ) )
200  {
201  std::getline( ifs, line, '\n' );
202  }
203 
204  ifs >> (*pos)[0] >> (*pos)[1] >> (*pos)[2];
205 
206  while( line.find( "Magnitudes" ) )
207  {
208  std::getline( ifs, line, '\n' );
209  }
210  std::getline( ifs, line, '\n' );
211 
212  magnitudes->clear();
213  tokens = string_utils::tokenize( line );
214  for( unsigned int tokenId = 0; tokenId < tokens.size(); ++tokenId )
215  {
216  magnitudes->push_back( string_utils::fromString< float >( tokens[tokenId].c_str() ) );
217  }
218 
219  WAssert( magnitudes->size() == nbTimeSteps, "Number of time steps and magnitudes must be equal" );
220  WAssert( times->size() == nbTimeSteps, "Number of time steps and times must be equal" );
221 
222  ifs.close();
223 }
224 
225 std::shared_ptr< WDataSetDipoles > WMReadDipoles::readFiles( std::vector< std::string > filenames )
226 {
227  WPosition pos;
228  std::vector< float > times;
229  std::vector< float > magnitudes;
230  size_t firstTimeStep;
231  size_t lastTimeStep;
232 
233  readFile( filenames[0], &pos, &times, &magnitudes, &firstTimeStep, &lastTimeStep );
234  std::shared_ptr< WDataSetDipoles > loadedData( new WDataSetDipoles( pos, magnitudes, times, firstTimeStep, lastTimeStep ) );
235 
236  for( size_t fileId = 1; fileId < filenames.size(); ++fileId )
237  {
238  readFile( filenames[fileId], &pos, &times, &magnitudes, &firstTimeStep, &lastTimeStep );
239  loadedData->addDipole( pos, magnitudes, times, firstTimeStep, lastTimeStep );
240  }
241 
242  return loadedData;
243 }
virtual void wait() const
Wait for the condition.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
Use this for IO error handling.
Definition: WDHIOFailure.h:38
Represents a dipole dataset.
Someone should add some documentation here.
Definition: WMReadDipoles.h:52
virtual ~WMReadDipoles()
Destructs the reader.
WPropFilename m_dataFile
The data will be read from this file.
void readFile(std::string filename, WPosition *pos, std::vector< float > *times, std::vector< float > *magnitudes, size_t *firstTimeStep, size_t *lastTimeStep)
Function doing the actual reading from one file.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual const std::string getName() const
Gives back the name of this module.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
std::shared_ptr< WModuleOutputData< WDataSetDipoles > > m_dipoles
Output connector for dipoles of EEG data.
std::shared_ptr< WDataSetDipoles > m_dataSet
Pointer to the loaded dataset.
WPropBool m_metaFile
Use meta file containing fileNames.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual void moduleMain()
Entry point after loading the module.
virtual void connectors()
Initialize the connectors this module is using.
virtual void requirements()
Initialize requirements for this module.
virtual void properties()
Initialize the properties for this module.
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
WMReadDipoles()
Simple constructor.
std::shared_ptr< WDataSetDipoles > readFiles(std::vector< std::string > filenames)
Function that composes the data read by readFile() from the different files to one WDataSetDipoles.
std::shared_ptr< WDataSetDipoles > readMetaData(std::string filename)
Function reading meta file with filenames of dipole files.
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WModule.h:640
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:93
This only is a 3d double vector.
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
void addTo(WPropFilename prop)
Add the PC_PATHEXISTS constraint to the property.
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).