#ifndef _itkNMFBase_txx #define _itkNMFBase_txx #include "itkNMFBase.h" namespace itk { //------------------------------------------------------------------------ //NMFBase //------------------------------------------------------------------------ template NMFBase ::NMFBase():m_NumberOfClasses(2) { m_H.set_size(0,0); m_W.set_size(0,0); } //------------------------------------------------------------------------ //~NMFBase //------------------------------------------------------------------------ template NMFBase ::~NMFBase() throw() { } //-------------------------------------------------------------------------- // Initialize //------------------------------------------------------------------------- template void NMFBase::Initialize() { //Check if the input matrix has valid dimensions if( (m_V->rows() == 0) || (m_V->columns() == 0) ) { itk::ExceptionObject exception(__FILE__, __LINE__); exception.SetDescription("Invalid input file."); throw exception; } //Populate the H & W matrix with a random initialization if( (m_H.rows() == 0) || (m_H.columns() == 0) || (m_W.rows()== 0) || (m_W.columns() == 0) ) { m_W.set_size(m_V->rows(), m_NumberOfClasses); m_H.set_size(m_NumberOfClasses, m_V->columns()); vnl_sample_uniform( m_W.begin(), m_W.end(), 0,1); vnl_sample_uniform( m_H.begin(), m_H.end(), 0,1); } }//end Initialize //-------------------------------------------------------------------------- // Initialize with user specified inputs //------------------------------------------------------------------------- template void NMFBase::Initialize(OutMatrixType *w, OutMatrixType *h) { //Check if the input matrix has valid dimensions if( (m_V->rows() == 0) || (m_V->columns() == 0) ) { itk::ExceptionObject exception(__FILE__, __LINE__); exception.SetDescription("Invalid input file."); throw exception; } //Check the number of rows in the input matrix matches with W matrix if( m_V->rows() != w->rows() ) { itk::ExceptionObject exception(__FILE__, __LINE__); exception.SetDescription("Invalid initialization of W matrix."); throw exception; } //Check the number of columns in the input matrix matches with H matrix if( m_V->columns() != h->columns() ) { itk::ExceptionObject exception(__FILE__, __LINE__); exception.SetDescription("Invalid initialization of H matrix."); throw exception; } m_W.set_size(m_V->rows(), m_NumberOfClasses); m_H.set_size(m_NumberOfClasses, m_V->columns()); //Copy the input w matrix to the internal W matrix container OutMatrixType::iterator inWIt = (*w).begin(); for(OutMatrixType::iterator it = m_W.begin(); it < m_W.end(); it++, inWIt++) { *it = *inWIt; } //Copy the input h matrix to the internal H matrix container OutMatrixType::iterator inHIt = (*h).begin(); for(OutMatrixType::iterator it = m_H.begin(); it < m_H.end(); it++, inHIt++) { *it = *inHIt; } }//end Initialize //------------------------------------------------------------------------ // Write a binary stream to a file //------------------------------------------------------------------------ template int NMFBase::Write(OutMatrixType *m, std::string &outFile) { //Write the output matrix m to a binary file specified by the outFile try { std::ofstream output(outFile.c_str(), std::ios::out | std::ios::binary); output.write(reinterpret_cast (m->data_block()), (m->rows() * m->columns()) *sizeof(double)); if (output.fail()) throw std::runtime_error("Error writing the matrix"); } catch (const std::exception &e) { std::cout << e.what() << std::endl; exit(-1); } catch (...) { std::cout << "Error writing the matrix." << std::endl; return -1; } return 0; }//end Write } // namespace itk #endif