WMDetTractClusteringGP.cpp 8.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include <set>
#include <string>
#include <utility>
#include <vector>

30 31
#include <omp.h>

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "core/common/datastructures/WDendrogram.h"
#include "core/common/datastructures/WUnionFind.h"
#include "core/common/WLimits.h"
#include "core/kernel/WKernel.h"
#include "WMDetTractClusteringGP.h"

WMDetTractClusteringGP::WMDetTractClusteringGP():
    WModule()
{
}

WMDetTractClusteringGP::~WMDetTractClusteringGP()
{
}

boost::shared_ptr< WModule > WMDetTractClusteringGP::factory() const
{
    return boost::shared_ptr< WModule >( new WMDetTractClusteringGP() );
}

const std::string WMDetTractClusteringGP::getName() const
{
    return "Deterministic Tract Clustering using Gaussian Proccesses";
}

const std::string WMDetTractClusteringGP::getDescription() const
{
    return "Clusters Gaussian Processes which represents deterministic tracts with the Wassermann "
           "approach described in the paper: http://dx.doi.org/10.1016/j.neuroimage.2010.01.004";
}

void WMDetTractClusteringGP::connectors()
{
    m_gpIC = WModuleInputData< WDataSetGP >::createAndAdd( shared_from_this(), "gpInput", "WDataSetGP providing the Gaussian processes" );
    m_dendOC = WModuleOutputData< WDendrogram >::createAndAdd( shared_from_this(), "dendrogramOutput", "WDendrogram as a result of this clustering" );
67 68
    m_matrixOC = WModuleOutputData< WDataSetMatrixSymFLT >::createAndAdd( shared_from_this(), "similaritiesOutput",
        "WDataSetMatrixSym containing the similarities" );
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    WModule::connectors();
}

void WMDetTractClusteringGP::properties()
{
    WModule::properties();
}

void WMDetTractClusteringGP::moduleMain()
{
    m_moduleState.setResetable( true, true ); // remember actions when actually not waiting for actions
    m_moduleState.add( m_gpIC->getDataChangedCondition() );

    ready();

    while( !m_shutdownFlag() ) // loop until the module container requests the module to quit
    {
        debugLog() << "Waiting..";
        m_moduleState.wait();
        if( !m_gpIC->getData().get() ) // ok, the output has not yet sent data
        {
            continue;
        }

        boost::shared_ptr< WDataSetGP > dataSet = m_gpIC->getData();
        if( !dataSet || dataSet->size() == 0 )
        {
            debugLog() << "Invalid data--> continue";
            continue;
        }
        if( m_gpIC->handledUpdate() )
        {
            debugLog() << "Input has been updated...";
        }

        infoLog() << "Generating similarity matrix...";

        computeDistanceMatrix( dataSet );
        if( m_shutdownFlag() ) // since computeDistanceMatrix may quit earlier due to the m_shutdownFlag()
        {
            debugLog() << "Abort all computations since shutdown requested";
            break;
        }

        infoLog() << "Building dendrogram...";
        boost::shared_ptr< WDendrogram > result( computeDendrogram( dataSet->size() ) );
        if( m_shutdownFlag() ) // since computeDendrogram may quit earlier due to the m_shutdownFlag()
        {
            debugLog() << "Abort all computations since shutdown requested";
            break;
        }
        m_dendOC->updateData( result );

        infoLog() << "Done.";
    }
}

void WMDetTractClusteringGP::computeDistanceMatrix( boost::shared_ptr< const WDataSetGP > dataSet )
{
    const size_t steps = dataSet->size() * ( dataSet->size() - 2 ) / 2; // n(n-2)/2
    boost::shared_ptr< WProgress > progress( new WProgress( "Similarity matrix computation", steps ) );
    m_progress->addSubProgress( progress );

133
    m_similarities = WMatrixSymFLT::SPtr( new WMatrixSymFLT( dataSet->size() ) );
134

Mathias Goldau's avatar
Mathias Goldau committed
135
    #pragma omp parallel for schedule(guided)
136
    for( int i = 0; i < static_cast< int >( dataSet->size() ); ++i )
137 138 139 140 141
    {
        for( size_t j = i + 1; j < dataSet->size() && !m_shutdownFlag(); ++j )
        {
            const WGaussProcess& p1 = ( *dataSet )[i];
            const WGaussProcess& p2 = ( *dataSet )[j];
142 143 144
            bool computeInnerProduct = p1.getBB().minDistance( p2.getBB() ) < ( p1.getRadius() + p2.getRadius() );
            float innerProduct = 0.0;
            if( computeInnerProduct )
145
            {
146
                innerProduct = gauss::innerProduct( p1, p2 ); // As written in the paper, we don't use the normalized inner product
147
            }
148
            #pragma omp critical
149
            {
150
                (*m_similarities)( i, j ) = innerProduct;
151 152
            }
        }
153 154 155 156
        #pragma omp critical
        {
            *progress = *progress + ( dataSet->size() - i - 1 );
        }
157
    }
158 159

    m_matrixOC->updateData( WDataSetMatrixSymFLT::SPtr( new WDataSetMatrixSymFLT( m_similarities ) ) );
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    progress->finish();
    m_progress->removeSubProgress( progress );
}

boost::shared_ptr< WDendrogram > WMDetTractClusteringGP::computeDendrogram( size_t n )
{
    boost::shared_ptr< WDendrogram > dend( new WDendrogram( n ) );
    boost::shared_ptr< WProgress > progress( new WProgress( "Matrix => Dendrogram", n - 1 ) );
    m_progress->addSubProgress( progress );

    WUnionFind uf( n );
    std::vector< size_t > innerNode( n ); // The refernces from the canonical Elements (cE) to the inner nodes.
    std::set< size_t > idx; // valid indexes, to keep trac of already erased columns
    std::vector< size_t > clusterSize( n, 1 ); // to keep trac how many elements a cluster has.

    for( size_t i = 0; i < n; ++i )
    {
        innerNode[i] = i; // initialize them with their corresponding leafs.
        idx.insert( i );
    }

Mathias Goldau's avatar
Mathias Goldau committed
181 182
    SimSortArray sims;
    IdxSimMap map;
183
    for( size_t i = 0; i < n ; ++i ) {
Mathias Goldau's avatar
Mathias Goldau committed
184 185 186 187 188 189 190 191
        for( size_t j = i + 1; j < n; ++j ) {
            IdxSimSPtr current( new IndexSimilarity( (*m_similarities)( i, j ), i, j ) );
            sims.push( current );
            map[ ( i * n + j - ( i + 1 ) * ( i + 2 ) / 2 ) ] = current;
        }
    }


192 193
    for( size_t i = 0; i < n - 1 && !m_shutdownFlag(); ++i )
    {
Mathias Goldau's avatar
Mathias Goldau committed
194 195
        while( !sims.empty() && sims.top() && ( idx.find( sims.top()->_i ) == idx.end()  || idx.find( sims.top()->_j ) == idx.end() ) ) {
            sims.pop();
196
        }
Mathias Goldau's avatar
Mathias Goldau committed
197 198

        if( sims.empty() || m_shutdownFlag() ) {
199 200 201
            break;
        }

Mathias Goldau's avatar
Mathias Goldau committed
202 203 204 205 206
        float maxSim = sims.top()->_sim;
        size_t p = sims.top()->_i;
        size_t q = sims.top()->_j;
        sims.pop();

207 208 209 210
        uf.merge( p, q );
        size_t newCE = uf.find( p );
        innerNode[ newCE ] = dend->merge( innerNode[ p ], innerNode[ q ], maxSim );

Mathias Goldau's avatar
Mathias Goldau committed
211
        idx.erase( newCE == p ? q : p );
212 213 214 215 216 217 218 219 220 221 222

        // update the column where now the new cluster pq resides
        for( std::set< size_t >::const_iterator it = idx.begin(); it != idx.end(); ++it )
        {
            if( *it != newCE )
            {
                // we have two Gauss processes p and q. We have merged p and q into pq. Hence for all valid indexes we must
                // recompute < pq, k > where k is a GP identified through an valid index, where:
                // < pq, k > = |p| / ( |p| + |q| ) < p, k > + |q| / (|p| + |q|) < q, k >
                double firstFactor = static_cast< double >( clusterSize[ p ] ) / ( clusterSize[ p ] + clusterSize[ q ] );
                double secondFactor = static_cast< double >( clusterSize[ q ] ) / ( clusterSize[ p ] + clusterSize[ q ] );
Mathias Goldau's avatar
Mathias Goldau committed
223 224
                size_t r = newCE > *it ? *it : newCE;
                size_t s = newCE > *it ? newCE : *it;
225
                (*m_similarities)( newCE, *it ) = firstFactor * (*m_similarities)( p, *it ) + secondFactor * (*m_similarities)( q, *it );
Mathias Goldau's avatar
Mathias Goldau committed
226 227 228 229 230 231 232
                IdxSimSPtr current( new IndexSimilarity( (*m_similarities)( r, s ), r, s ) );
                IdxSimMap::iterator old = map.find( ( r * n + s - ( r + 1 ) * ( r + 2 ) / 2 ) );
                if( old != map.end() && old->second ) {
                    old->second->_i = std::numeric_limits< size_t >::max();
                    old->second = current;
                }
                sims.push( current );
233 234 235 236 237 238 239 240 241 242 243
            }
        }
        clusterSize[ newCE ] = clusterSize[ p ] + clusterSize[ q ];
        ++*progress;
    }

    progress->finish();
    m_progress->removeSubProgress( progress );

    return dend;
}