OpenWalnut  1.5.0dev
WReaderClustering.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 <cstdlib>
26 #include <fstream>
27 #include <list>
28 #include <memory>
29 #include <stdint.h>
30 #include <string>
31 #include <vector>
32 
33 #include <boost/regex.hpp>
34 
35 #include "WReaderClustering.h"
36 #include "core/common/WAssert.h"
37 #include "core/common/WIOTools.h"
38 #include "core/common/WLimits.h"
39 #include "core/common/WLogger.h"
40 #include "core/common/WStringUtils.h"
41 #include "core/dataHandler/WDataSetHierarchicalClustering.h"
42 #include "core/dataHandler/datastructures/WFiberCluster.h"
43 #include "core/dataHandler/exceptions/WDHIOFailure.h"
44 #include "core/dataHandler/exceptions/WDHNoSuchFile.h"
45 #include "core/dataHandler/exceptions/WDHParseError.h"
46 
48  : WReader( fname )
49 {
50 }
51 
53 {
54 }
55 
56 
57 std::shared_ptr< WDataSetHierarchicalClustering > WReaderClustering::read()
58 {
59  //Read file to line list
60  std::shared_ptr< std::ifstream > ifs( new std::ifstream() );
61  ifs->open( m_fname.c_str(), std::ifstream::in | std::ifstream::binary );
62  if( !ifs || ifs->bad() )
63  {
64  throw WDHIOFailure( std::string( "internal error while opening file" ) );
65  }
66 
67  std::vector< std::string > lines;
68  std::string line;
69 
70 
71  while( !ifs->eof() )
72  {
73  getline( *ifs, line );
74  lines.push_back( std::string( line ) );
75  }
76 
77  ifs->close();
78 
79  for( size_t i = 0; i < lines.size() - 1; ++i )
80  {
81  std::string &ls = lines[i];
82 
83  boost::regex reg1( "\\(" );
84  ls = boost::regex_replace( ls, reg1, "(," );
85 
86  boost::regex reg2( "\\)" );
87  ls = boost::regex_replace( ls, reg2, ",)" );
88 
89  boost::regex reg3( "\\ " );
90  ls = boost::regex_replace( ls, reg3, "" );
91  }
92 
93  std::vector<std::string>svec;
94  WTreeNode::SPtr rootNode;
95 
96  for( size_t i = 0; i < lines.size() - 1; ++i )
97  {
98  svec.clear();
99  boost::regex reg( "," );
100  boost::sregex_token_iterator it( lines[i].begin(), lines[i].end(), reg, -1 );
101  boost::sregex_token_iterator end;
102  while( it != end )
103  {
104  svec.push_back( *it++ );
105  }
106 
107  size_t level = string_utils::fromString< size_t >( svec[1] );
108 
109  if( level == 0 )
110  {
111  std::list< size_t > indices;
112  // start after ( level (
113  size_t k = 3;
114  while( svec[k] != ")" && svec[k] != "" )
115  {
116  indices.push_back( string_utils::fromString< size_t >( svec[k] ) );
117  ++k;
118  }
119  WColor color( 1.0, 1.0, 0.0, 1.0 );
120  WFiberCluster::SPtr cluster( new WFiberCluster( indices, color ) );
121  m_clustering[ i ] = cluster;
122  m_nodes[ i ] = WTreeNode::SPtr( new WTreeNode( i, level ) );
123  rootNode = m_nodes[ i ];
124  }
125  else
126  {
127  std::list< size_t > indices;
128  // start after ( level (
129  size_t k = 3;
130  while( svec[k] != ")" )
131  {
132  indices.push_back( string_utils::fromString< size_t >( svec[k] ) );
133  ++k;
134  }
135 
136  WColor color( 0.1 * ( i % 10 ), 0.1 * ( ( i + 5 ) % 10 ), 0.0, 1.0 );
137  WFiberCluster::SPtr cluster( new WFiberCluster( indices, color ) );
138  m_clustering[ i ] = cluster;
139  m_nodes[ i ] = WTreeNode::SPtr( new WTreeNode( i, level ) );
140  rootNode = m_nodes[ i ];
141 
142  //add Child cluster
143  k += 2;
144  std::vector< size_t > childIndices;
145  while( svec[k] != ")" )
146  {
147  childIndices.push_back( string_utils::fromString< size_t >( svec[k] ) );
148  ++k;
149  }
150 
151  for( size_t j = 0; j < childIndices.size(); j++ )
152  {
153  WTreeNode::SPtr childNode = m_nodes[ childIndices[ j ] ];
154  m_nodes[ i ]->addChild( childNode );
155  }
156  }
157  }
158 
159  //Return the WDataSetHierarchicalClustering
161  return clustering;
162 }
163 
164 
165 
Use this for IO error handling.
Definition: WDHIOFailure.h:38
Represents a hierarchy of clusters.
std::shared_ptr< WDataSetHierarchicalClustering > SPtr
Pointer to dataset.
Represents a cluster of indices of a WDataSetFiberVector.
Definition: WFiberCluster.h:45
std::shared_ptr< WFiberCluster > SPtr
Shared pointer abbreviation.
Definition: WFiberCluster.h:51
std::map< size_t, WFiberCluster::SPtr > m_clustering
Stores the hierarchical clustering map.
virtual std::shared_ptr< WDataSetHierarchicalClustering > read()
Reads the clustering file and creates a dataset out of it.
virtual ~WReaderClustering()
Destroys this instance and closes the file.
std::map< size_t, WTreeNode::SPtr > m_nodes
Stores the hierarchical clustering map.
WReaderClustering(std::string fname)
Constructs and makes a new FDG reader for separate thread start.
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
A node in a tree, holding an index, a level in the tree and pointers to its child nodes.
Definition: WTreeNode.h:39
std::shared_ptr< WTreeNode > SPtr
Shared pointer abbreviation.
Definition: WTreeNode.h:44