Sleipnir
src/bayesnetmixed.cpp
00001 /*****************************************************************************
00002 * This file is provided under the Creative Commons Attribution 3.0 license.
00003 *
00004 * You are free to share, copy, distribute, transmit, or adapt this work
00005 * PROVIDED THAT you attribute the work to the authors listed below.
00006 * For more information, please see the following web page:
00007 * http://creativecommons.org/licenses/by/3.0/
00008 *
00009 * This file is a component of the Sleipnir library for functional genomics,
00010 * authored by:
00011 * Curtis Huttenhower (chuttenh@princeton.edu)
00012 * Mark Schroeder
00013 * Maria D. Chikina
00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
00015 *
00016 * If you use this library, the included executable tools, or any related
00017 * code in your work, please cite the following publication:
00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
00019 * Olga G. Troyanskaya.
00020 * "The Sleipnir library for computational functional genomics"
00021 *****************************************************************************/
00022 #include "stdafx.h"
00023 #ifdef PNL_ENABLED
00024 #pragma warning (disable: 4244 4267)
00025 #include <pnl_dll.hpp>
00026 #pragma warning (default: 4244 4267)
00027 #include "bayesnet.h"
00028 
00029 namespace Sleipnir {
00030 
00031 bool CBayesNetSmileImpl::ConvertGraph( CBayesNetPNL& BNPNL ) const {
00032     CNodeType*                      aNodeTypes;
00033     DSL_intArray                    IntArrayNodes, IntArrayAdj;
00034     int                             i, j, iSize;
00035     int**                           aaiNeighborCounts;
00036     int*                            aiNeighborCounts;
00037     int*                            aiNodeAssociation;
00038     map<int,int>                    mapSmilePNL, mapNodeTypes;
00039     ENeighborType**                 aaeNeighborTypes;
00040     vector<int>                     veciNeighborCounts;
00041     vector<ENeighborType>           veceNeighborTypes;
00042     map<int,int>::const_iterator    iterNodeTypes;
00043     DSL_node*                       pNode;
00044     CGraph*                         pGraph;
00045 
00046     m_SmileNet.GetAllNodes( IntArrayNodes );
00047     for( i = 0; i < IntArrayNodes.NumItems( ); ++i )
00048         mapSmilePNL[ IntArrayNodes[ i ] ] = i;
00049 
00050     aiNodeAssociation = new int[ IntArrayNodes.NumItems( ) ];
00051     for( i = 0; i < IntArrayNodes.NumItems( ); ++i ) {
00052         pNode = m_SmileNet.GetNode( IntArrayNodes[ i ] );
00053         if( IsGaussian( m_SmileNet ) )
00054             iSize = -1;
00055         else
00056             iSize = pNode->Definition( )->GetNumberOfOutcomes( );
00057         if( ( iterNodeTypes = mapNodeTypes.find( iSize ) ) == mapNodeTypes.end( ) ) {
00058             j = (int)mapNodeTypes.size( );
00059             mapNodeTypes[ iSize ] = j;
00060             iterNodeTypes = mapNodeTypes.find( iSize ); }
00061         aiNodeAssociation[ i ] = iterNodeTypes->second; }
00062     aNodeTypes = new CNodeType[ mapNodeTypes.size( ) ];
00063     for( iterNodeTypes = mapNodeTypes.begin( ); iterNodeTypes != mapNodeTypes.end( );
00064         ++iterNodeTypes )
00065         aNodeTypes[ iterNodeTypes->second ].SetType( ( iterNodeTypes->first > 0 ),
00066             abs( iterNodeTypes->first ) );
00067 
00068     aiNeighborCounts = new int[ IntArrayNodes.NumItems( ) ];
00069     aaiNeighborCounts = new int*[ IntArrayNodes.NumItems( ) ];
00070     aaeNeighborTypes = new ENeighborType*[ IntArrayNodes.NumItems( ) ];
00071     for( i = 0; i < IntArrayNodes.NumItems( ); ++i ) {
00072         IntArrayAdj = m_SmileNet.GetParents( IntArrayNodes[ i ] );
00073         veciNeighborCounts.resize( iSize = aiNeighborCounts[ i ] = IntArrayAdj.NumItems( ) );
00074         veceNeighborTypes.resize( iSize );
00075         for( j = 0; j < IntArrayAdj.NumItems( ); ++j ) {
00076             veciNeighborCounts[ j ] = mapSmilePNL[ IntArrayAdj[ j ] ];
00077             veceNeighborTypes[ j ] = ntParent; }
00078 
00079         IntArrayAdj = m_SmileNet.GetChildren( IntArrayNodes[ i ] );
00080         veciNeighborCounts.resize( aiNeighborCounts[ i ] += IntArrayAdj.NumItems( ) );
00081         veceNeighborTypes.resize( aiNeighborCounts[ i ] );
00082         for( j = 0; j < IntArrayAdj.NumItems( ); ++j ) {
00083             veciNeighborCounts[ iSize + j ] = mapSmilePNL[ IntArrayAdj[ j ] ];
00084             veceNeighborTypes[ iSize + j ] = ntChild; }
00085 
00086         aaiNeighborCounts[ i ] = new int[ veciNeighborCounts.size( ) ];
00087         aaeNeighborTypes[ i ] = new ENeighborType[ veceNeighborTypes.size( ) ];
00088         for( j = 0; j < veciNeighborCounts.size( ); ++j ) {
00089             aaiNeighborCounts[ i ][ j ] = veciNeighborCounts[ j ];
00090             aaeNeighborTypes[ i ][ j ] = veceNeighborTypes[ j ]; } }
00091 
00092     if( BNPNL.m_pPNLNet )
00093         delete BNPNL.m_pPNLNet;
00094     pGraph = CGraph::Create( IntArrayNodes.NumItems( ), aiNeighborCounts,
00095         aaiNeighborCounts, aaeNeighborTypes );
00096     BNPNL.m_pPNLNet = CBNet::Create( IntArrayNodes.NumItems( ), (int)mapNodeTypes.size( ),
00097         aNodeTypes, aiNodeAssociation, pGraph );
00098 
00099     for( i = 0; i < IntArrayNodes.NumItems( ); ++i ) {
00100         delete[] aaiNeighborCounts[ i ];
00101         delete[] aaeNeighborTypes[ i ]; }
00102     delete[] aaiNeighborCounts;
00103     delete[] aaeNeighborTypes;
00104     delete[] aiNeighborCounts;
00105     delete[] aiNodeAssociation;
00106     delete[] aNodeTypes;
00107 
00108     return true; }
00109 
00110 bool CBayesNetSmileImpl::ConvertCPTs( CBayesNetPNL& BNPNL ) const {
00111     int                         i, j, k, iCPT;
00112     floatVector                 vecdCPT;
00113     DSL_Dmatrix*                pCPT;
00114     DSL_intArray                IntArrayNodes, IntArrayCoords;
00115     DSL_node*                   pNode;
00116     CNumericDenseMatrix<float>* pMatrix;
00117     int                         aiDims[ 2 ] = { 1, 1 };
00118     float                       dCPT;
00119 
00120     BNPNL.m_pPNLNet->AllocFactors( );
00121     m_SmileNet.GetAllNodes( IntArrayNodes );
00122     for( i = 0; i < IntArrayNodes.NumItems( ); ++i ) {
00123         pNode = m_SmileNet.GetNode( IntArrayNodes[ i ] );
00124         pCPT = pNode->Definition( )->GetMatrix( );
00125         if( IsGaussian( m_SmileNet ) ) {
00126             BNPNL.m_pPNLNet->AllocFactor( i );
00127 
00128             dCPT = (float)(*pCPT)[ iCPT = 0 ];
00129             pMatrix = CNumericDenseMatrix<float>::Create( 2, aiDims, &dCPT );
00130             BNPNL.m_pPNLNet->GetFactor( i )->AttachMatrix( pMatrix, matMean );
00131 
00132             dCPT = (float)(*pCPT)[ ++iCPT ];
00133             pMatrix = CNumericDenseMatrix<float>::Create( 2, aiDims, &dCPT );
00134             BNPNL.m_pPNLNet->GetFactor( i )->AttachMatrix( pMatrix, matCovariance );
00135 
00136             j = m_SmileNet.GetParents( IntArrayNodes[ i ] ).NumItems( );
00137             for( k = 0; k < j; ++k ) {
00138                 dCPT = (float)(*pCPT)[ ++iCPT ];
00139                 pMatrix = CNumericDenseMatrix<float>::Create( 2, aiDims, &dCPT );
00140                 BNPNL.m_pPNLNet->GetFactor( i )->AttachMatrix( pMatrix, matWeights, k ); } }
00141         else {
00142             vecdCPT.resize( pCPT->GetSize( ) );
00143             for( j = 0; j < pCPT->GetSize( ); ++j )
00144                 vecdCPT[ j ] = (float)(*pCPT)[ j ];
00145             BNPNL.m_pPNLNet->CreateTabularCPD( i, vecdCPT ); } }
00146 
00147     return true; }
00148 
00149 }
00150 
00151 #endif // PNL_ENABLED