Commit 4d28de00 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[MERGE]

parents f5534a1b b2f727f8
......@@ -22,8 +22,15 @@
//
//---------------------------------------------------------------------------
#include <algorithm>
#include <iterator>
#include <fstream>
#include <map>
#include <set>
#include <sstream>
#include <vector>
#include "../WAssert.h"
#include "WDendrogram.h"
WDendrogram::WDendrogram( size_t n )
......@@ -55,3 +62,52 @@ size_t WDendrogram::merge( size_t i, size_t j, double height )
return m_parents.size() - 1;
}
std::string WDendrogram::toTXTString() const
{
std::stringstream ss;
std::map< size_t, std::set< size_t > > childsOfInnerNodes;
std::map< size_t, std::set< size_t > > preds;
std::vector< size_t > level( 2 * m_heights.size() + 1, 0 );
// first write out all fibers
for( size_t i = 0; i < m_heights.size() + 1; ++i )
{
ss << "(0, (" << i << ",))" << std::endl;
childsOfInnerNodes[ m_parents[ i ] ].insert( i );
preds[ m_parents[ i ] ] = childsOfInnerNodes[ m_parents[ i ] ];
}
for( size_t i = m_heights.size() + 1; i < 2 * m_heights.size() + 1; ++i )
{
WAssert( preds[ i ].size() == 2, "There are more or less than 2 predecessors for an inner node" );
size_t left = *( preds[ i ].begin() );
size_t right = *( preds[ i ].rbegin() );
level[ i ] = std::max( level[ left ], level[ right ] ) + 1;
preds[ m_parents[ i ] ].insert( i );
std::set< size_t > join;
std::set_union( childsOfInnerNodes[ m_parents[ i ] ].begin(), childsOfInnerNodes[ m_parents[ i ] ].end(), childsOfInnerNodes[ i ].begin(), childsOfInnerNodes[ i ].end(),
std::inserter( join, join.begin() ) );
childsOfInnerNodes[ m_parents[ i ] ] = join;
ss << "(" << level[i] << ", (";
size_t numElements = childsOfInnerNodes[i].size();
for( std::set< size_t >::const_iterator cit = childsOfInnerNodes[i].begin(); cit != childsOfInnerNodes[i].end(); ++cit )
{
if( numElements == 1 )
{
ss << *cit << "), ";
}
else
{
ss << *cit << ", ";
}
numElements -= 1;
}
ss << " (" << left << ", " << right << "), " << m_heights[ i - m_heights.size() + 1 ] << ")" << std::endl;
// TODO(math): the needs to be made with a writer instead
std::ofstream file( "/home/math/pansen.txt" );
file << ss.str();
file.close();
}
return ss.str();
}
......@@ -74,6 +74,14 @@ public:
*/
size_t merge( size_t i, size_t j, double height );
/**
* Transform this dendrogram into a string, where each leaf or inner node is mapped to a special string.
* <dfn>"(level, (all leafs incorporated by this node), (the two direct predecessors), height if available )"</dfn>
*
* \return The special string as constructed from the scheme above.
*/
std::string toTXTString() const;
protected:
private:
/**
......
......@@ -103,7 +103,7 @@ void WMDetTractClusteringGP::moduleMain()
m_maxSegmentLength = searchGlobalMaxSegementLength( dataSet );
// computeDistanceMatrix( dataSet );
computeDendrogram( computeEMST( dataSet ) );
std::cout << computeDendrogram( computeEMST( dataSet ) )->toTXTString();
debugLog() << "done";
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment