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"
58 return WMReadDipoles_xpm;
62 return "Read Dipoles";
67 return "Reading \".dip\" files containing position and additional information on dipoles reconstructed from EEG.";
73 shared_from_this(),
"Dipoles",
"The loaded dipoles reconstructed from EEG." ) );
85 m_metaFile =
m_properties->addProperty(
"Use meta file",
"File containing multiple filenames to load.",
true );
108 std::shared_ptr< WProgress > progress(
new WProgress(
"Read Dipoles", 2 ) );
127 std::vector< std::string > names;
129 ifs.open( filename.c_str(), std::ifstream::in );
130 if( !ifs || ifs.bad() )
132 throw WDHIOFailure( std::string(
"Internal error while opening file" ) );
136 while( std::getline( ifs, line ) )
138 names.push_back( line );
146 size_t* firstTimeStep,
size_t* lastTimeStep )
149 ifs.open( filename.c_str(), std::ifstream::in );
150 if( !ifs || ifs.bad() )
152 throw WDHIOFailure( std::string(
"Internal error while opening file" ) );
156 std::vector< std::string > tokens;
158 std::getline( ifs, line,
'\n' );
160 while( line.find(
"NumberTimeSteps" ) )
162 std::getline( ifs, line,
'\n' );
165 size_t nbTimeSteps = string_utils::fromString< size_t >( tokens[1].c_str() );
167 std::getline( ifs, line,
'\n' );
169 while( line.find(
"TimeSteps" ) )
171 std::getline( ifs, line,
'\n' );
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 )
181 (*times)[timeStep] = timeFirst + timeStep * timeDistance;
183 WAssert( std::abs( (*times)[nbTimeSteps-1] - timeLast ) < 1e-4,
"Error during filling times vector." );
185 while( line.find(
"FirstTimeStep" ) )
187 std::getline( ifs, line,
'\n' );
190 *firstTimeStep = std::max( string_utils::fromString< size_t >( tokens[1].c_str() ) - 1u,
size_t( 0u ) );
192 while( line.find(
"LastTimeStep" ) )
194 std::getline( ifs, line,
'\n' );
197 *lastTimeStep = std::min( string_utils::fromString< size_t >( tokens[1].c_str() ) + 1u, nbTimeSteps - 1u );
199 while( line.find(
"PositionsFixed" ) )
201 std::getline( ifs, line,
'\n' );
204 ifs >> (*pos)[0] >> (*pos)[1] >> (*pos)[2];
206 while( line.find(
"Magnitudes" ) )
208 std::getline( ifs, line,
'\n' );
210 std::getline( ifs, line,
'\n' );
214 for(
unsigned int tokenId = 0; tokenId < tokens.size(); ++tokenId )
216 magnitudes->push_back( string_utils::fromString< float >( tokens[tokenId].c_str() ) );
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" );
228 std::vector< float > times;
229 std::vector< float > magnitudes;
230 size_t firstTimeStep;
233 readFile( filenames[0], &pos, ×, &magnitudes, &firstTimeStep, &lastTimeStep );
234 std::shared_ptr< WDataSetDipoles > loadedData(
new WDataSetDipoles( pos, magnitudes, times, firstTimeStep, lastTimeStep ) );
236 for(
size_t fileId = 1; fileId < filenames.size(); ++fileId )
238 readFile( filenames[fileId], &pos, ×, &magnitudes, &firstTimeStep, &lastTimeStep );
239 loadedData->addDipole( pos, magnitudes, times, firstTimeStep, lastTimeStep );
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.
Use this for IO error handling.
Represents a dipole dataset.
Someone should add some documentation here.
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.
virtual void properties()
Initialize properties in this function.
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
std::shared_ptr< WProperties > m_properties
The property object for the module.
void ready()
Call this whenever your module is ready and can react on property changes.
WConditionSet m_moduleState
The internal state of the module.
virtual void connectors()
Initialize connectors in this function.
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
This only is a 3d double vector.
Class managing progress inside of modules.
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).