OpenWalnut  1.5.0dev
WMWriteCSV.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 <list>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include "WMWriteCSV.h"
31 #include "core/common/WPathHelper.h"
32 #include "core/kernel/WKernel.h"
33 
34 W_LOADABLE_MODULE( WMWriteCSV )
35 
37  WModule()
38 {
39 }
40 
42 {
44 }
45 
46 std::shared_ptr< WModule > WMWriteCSV::factory() const
47 {
48  return std::shared_ptr< WModule >( new WMWriteCSV() );
49 }
50 
51 const char** WMWriteCSV::getXPMIcon() const
52 {
53  static const char * disc_xpm[] =
54  {
55  "16 16 7 1", " c None", ". c #000080", "+ c #000000", "@ c #FFFF00", "# c #E0E0E0",
56  "$ c #FFFFFF", "% c #C0C0C0", "..+++++++++++..+", "..@@@@@@@@@@@..+", "..###########..+",
57  "..$$$$$$$$$$$..+", "..###########..+", "..$$$$$$$$$$$..+", "..###########..+", "..$$$$$$$$$$$..+",
58  "...............+", "....%%%%%%%....+", "....%..%%%%....+", "....%..%%%%....+", "....%..%%%%....+",
59  "....%..%%%%....+", "+...%%%%%%%....+", "++++++++++++++++"
60  };
61  return disc_xpm;
62 }
63 
64 const std::string WMWriteCSV::getName() const
65 {
66  return "Write CSV";
67 }
68 
69 const std::string WMWriteCSV::getDescription() const
70 {
71  return "Writes a connected data set to a CSV file.";
72 }
73 
75 {
76  m_moduleState.setResetable( true, true );
77  m_moduleState.add( m_CSVInput->getDataChangedCondition() );
78  m_moduleState.add( m_fibersInput->getDataChangedCondition() );
79 
80  ready();
81 
82  while( !m_shutdownFlag() )
83  {
84  debugLog() << "Waiting for data ...";
85 
87  }
88 }
89 
91 {
93  shared_from_this(),
94  "Fibers_in",
95  "The dataset of the connected points" );
97  shared_from_this(),
98  "CSV_in",
99  "The dataset to write in" );
100 
102 }
103 
105 {
106  WPropertyBase::PropertyChangeNotifierType notifier = boost::bind(
108 
109  m_filename = m_properties->addProperty( "Filename", "Filename where to write the NIfTI file to.", WPathHelper::getHomePath(), notifier );
111 
113 }
114 
116 {
117  std::shared_ptr< WDataSetCSV > csvdataSet = m_CSVInput->getData();
118  std::shared_ptr< WDataSetFibers > fibersdataSet = m_fibersInput->getData();
119 
120  if( !csvdataSet )
121  {
122  throw WException( "The Data-Modul-CSV-connection is missing." );
123  }
124 
125  if( !fibersdataSet )
126  {
127  throw WException( "The Point-Connector-connection is missing." );
128  }
129 
130  writeToFile();
131 }
132 
133 std::list< std::tuple < float, float, float, size_t > > WMWriteCSV::getListOfInternalVertex( WDataSetFibers::SPtr fibers )
134 {
135  std::list< std::tuple < float, float, float, size_t > > listOfInternalVertex;
136 
137  WDataSetFibers::VertexArray vertices = fibers->getVertices();
138  WDataSetFibers::IndexArray verticesReverse = fibers->getVerticesReverse();
139 
140  size_t vertexCounter = 0;
141  size_t reverseCounter = 0;
142 
143  float vertexX = 0; // initialized to quiet avoid warning
144  float vertexY = 0; // initialized to quiet avoid warning
145  float vertexZ = 0; // initialized to quiet avoid warning
146 
147  for( size_t idx = 0; idx <= vertices->size(); idx++ )
148  {
149  switch( vertexCounter )
150  {
151  case 0: vertexX = vertices->at( idx ); break;
152  case 1: vertexY = vertices->at( idx ); break;
153  case 2: vertexZ = vertices->at( idx ); break;
154  case 3:
155  {
156  osg::Vec3 vecVertex( vertexX, vertexY, vertexZ );
157  listOfInternalVertex.push_back(
158  std::make_tuple(
159  vertexX,
160  vertexY,
161  vertexZ,
162  verticesReverse->at( reverseCounter++ ) ) );
163 
164  if( idx < vertices->size() )
165  {
166  vertexX = vertices->at( idx );
167  }
168 
169  vertexCounter = 0;
170  break;
171  }
172  }
173  vertexCounter++;
174  }
175 
176  return listOfInternalVertex;
177 }
178 
180 {
181  std::string sourceFilename = m_filename->get().string();
182  sourceFilename = sourceFilename.substr( 0, sourceFilename.find( ".csv" ) );
183  return sourceFilename + ".csv";
184 }
185 
187 {
188  std::vector< std::string > csvHeader = m_CSVInput->getData()->getHeader()->at( 0 );
189  std::string newColumnName = "SelectedEventID";
190  size_t counter = 1;
191  while( std::find( csvHeader.begin(), csvHeader.end(), newColumnName ) != csvHeader.end() )
192  {
193  newColumnName = "SelectedEventID_" + boost::lexical_cast< std::string >( counter );
194  counter++;
195  }
196 
197  return newColumnName;
198 }
199 
200 bool WMWriteCSV::contains( std::string sourceString, float num )
201 {
202  std::stringstream ss;
203  ss << num;
204  return sourceString.find( ss.str() ) != std::string::npos;
205 }
206 
207 size_t WMWriteCSV::createStartCounter( std::list< std::tuple < float, float, float, size_t > > listOfInternalVertex )
208 {
209  size_t eventIDcounter = 0;
210 
211  for( auto element = listOfInternalVertex.begin(); element != listOfInternalVertex.end(); element++ )
212  {
213  size_t selectedVertexIndex = std::get< 3 >( *element );
214 
215  if( eventIDcounter < selectedVertexIndex )
216  {
217  eventIDcounter = selectedVertexIndex;
218  }
219  }
220  return eventIDcounter + 1;
221 }
222 
224 {
225  WDataSetCSV::SeperatedRowSPtr csvContent = m_CSVInput->getData()->getRawDataSet();
226  std::list< std::tuple < float, float, float, size_t > > listOfInternalVertex = getListOfInternalVertex( m_fibersInput->getData() );
227  std::ofstream newCSVFile( getPathToSave() );
228 
229  if( !newCSVFile.is_open() )
230  {
231  throw WException( "Could not create new CSV in the selected source folder" );
232  }
233 
234  bool isMatch = false;
235  size_t eventIDcounter = createStartCounter( listOfInternalVertex );
236 
237  //set new csv-header ( SelectedEventID )
238  newCSVFile << csvContent->at( 0 ) << "," << getNewCSVHeader() << std::endl;
239 
240  //set new csv-content ( content of SelectedEventID )
241  for( size_t row = 1; row < csvContent->size(); row++ )
242  {
243  std::string rowAsString = csvContent->at( row );
244 
245  for( auto element = listOfInternalVertex.begin(); element != listOfInternalVertex.end(); element++ )
246  {
247  float posX = std::get< 0 >( *element );
248  if( !contains( rowAsString, posX ) )
249  {
250  continue;
251  }
252 
253  float posY = std::get< 1 >( *element );
254  if( !contains( rowAsString, posY ) )
255  {
256  continue;
257  }
258 
259  float posZ = std::get< 2 >( *element );
260  if( !contains( rowAsString, posZ ) )
261  {
262  continue;
263  }
264 
265  size_t selectedVertexIndex = std::get< 3 >( *element );
266  newCSVFile << rowAsString << "," << selectedVertexIndex << std::endl;
267 
268  isMatch = true;
269  listOfInternalVertex.erase( element );
270  break;
271  }
272 
273  if( isMatch )
274  {
275  isMatch = false;
276  }
277  else
278  {
279  newCSVFile << rowAsString << "," << std::to_string( eventIDcounter++ ) << std::endl;
280  }
281  }
282 
283  newCSVFile.close();
284 }
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
std::shared_ptr< std::vector< std::string > > SeperatedRowSPtr
represents a pointer to a vector of csv-raw-row as string
Definition: WDataSetCSV.h:57
std::shared_ptr< std::vector< size_t > > IndexArray
Index list indexing fibers in VertexArray in terms of vertex numbers.
std::shared_ptr< WDataSetFibers > SPtr
Pointer to dataset.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
Basic exception handler.
Definition: WException.h:39
Class for safe the point-connector data.
Definition: WMWriteCSV.h:43
void writeToFile()
This performs all work necessary to actually write the data to the file.
Definition: WMWriteCSV.cpp:223
std::list< std::tuple< float, float, float, size_t > > getListOfInternalVertex(WDataSetFibers::SPtr fibers)
Helpermethod to create a List of internal vertex with id.
Definition: WMWriteCSV.cpp:133
WPropFilename m_filename
The filename property -> where to write the csv file.
Definition: WMWriteCSV.h:153
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...
Definition: WMWriteCSV.cpp:46
std::string getNewCSVHeader()
The Helpermethod goes through the header and searches for -SelectedEventID-, if one was found,...
Definition: WMWriteCSV.cpp:186
void propertyCallback()
Trigger Event for Dialogfile.
Definition: WMWriteCSV.cpp:115
std::shared_ptr< WModuleInputData< WDataSetCSV > > m_CSVInput
Input connector required by this module.
Definition: WMWriteCSV.h:155
virtual const std::string getName() const
Gives back the name of this module.
Definition: WMWriteCSV.cpp:64
size_t createStartCounter(std::list< std::tuple< float, float, float, size_t > > listOfInternalVertex)
Helpermethod goes through the existing EvenIDs and returns the next one.
Definition: WMWriteCSV.cpp:207
std::shared_ptr< WModuleInputData< WDataSetFibers > > m_fibersInput
Input connector required by this module.
Definition: WMWriteCSV.h:156
virtual const std::string getDescription() const
Gives back a description of this module.
Definition: WMWriteCSV.cpp:69
bool contains(std::string sourceString, float num)
Helpermethod: checks whether the source domain contains the specified float.
Definition: WMWriteCSV.cpp:200
virtual void properties()
Initialize the properties for this module.
Definition: WMWriteCSV.cpp:104
virtual void connectors()
Initialize the connectors this module is using.
Definition: WMWriteCSV.cpp:90
virtual void moduleMain()
Entry point after loading the module.
Definition: WMWriteCSV.cpp:74
WMWriteCSV()
Standard constructor.
Definition: WMWriteCSV.cpp:36
std::string getPathToSave()
Helpermethod: Returns the path from Filedialog.
Definition: WMWriteCSV.cpp:179
~WMWriteCSV()
Destructor.
Definition: WMWriteCSV.cpp:41
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
Definition: WMWriteCSV.cpp:51
static PtrType createAndAdd(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this in data connector with proper type and add it to ...
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
Definition: WModule.cpp:575
void removeConnectors()
Removes all connectors properly.
Definition: WModule.cpp:194
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 getHomePath()
The path to the OW dir in the user's home.
boost::function< void(std::shared_ptr< WPropertyBase >)> PropertyChangeNotifierType
Signal signature emitted during set operations.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
void addTo(WPropFilename prop)
Add the PC_CONFIRMOVERWRITE constraint to the property.