int main()
{
  itk::RGBPixel<unsigned char> rgbPixel;
  std::cout << Helpers::length(rgbPixel) << std::endl;
  std::cout << Helpers::index(rgbPixel, 2) << std::endl;

  itk::VariableLengthVector<float> varVec(4);
  std::cout << Helpers::length(varVec) << std::endl;
  std::cout << Helpers::index(varVec, 1) << std::endl;

  itk::CovariantVector<float, 5> covVec;
  std::cout << Helpers::length(covVec) << std::endl;
  std::cout << Helpers::index(covVec, 1) << std::endl;

  return 0;
}
예제 #2
0
  void dfgEm(DfgInfo & dfgInfo, string const & varDataFile, string const & facDataFile, string const & subVarDataFile, number_t minDeltaLogLik, unsigned maxIter, 
	     string const & logStateMapsFile, string const & logFactorPotentialsFile, string const & logVariablesFile, string const & logFactorGraphFile, string const & logFile)
  {
    DFG & dfg = dfgInfo.dfg;     // convenient
    AbstractBaseFactorSet & factorSet = dfgInfo.facSet;

    unsigned iter = 0;
    number_t logLik = 0;
    number_t prevLogLik = 0;
    number_t deltaLogLik = 0;

    // setup input data structures
    VarData varData(varDataFile, dfgInfo.varNames);
    FacData * facDataPtr = NULL;
    VarData * subVarDataPtr = NULL;
    if (facDataFile.size() != 0) {
      facDataPtr = new FacData(facDataFile, dfgInfo.facNames);
    }
    if (subVarDataFile.size() != 0) {
      subVarDataPtr = new VarData(subVarDataFile, dfgInfo.subNames);
    }
    else if( dfgInfo.subNames.size() > 0 ){
      errorAbort("DfgDataWrap.cpp::164::main There are subscribed factors but no subscribed variable file were provided");
    }

    // init variables
    // input data variables
    string idVar;
    vector<symbol_t> varVec( varData.count() ); // for input data
    // dfg variables    
    stateMaskVec_t stateMaskVec( dfgInfo.varNames.size() );
    vector<matrix_t> facExpCounts; // Expectation counts
    initAccFactorMarginals(facExpCounts, dfg);
    vector<xmatrix_t> tmpFacMar;  // workspace
    dfg.initFactorMarginals(tmpFacMar);
    xnumber_t normConst;

    // open log file
    ofstream f;
    if ( logFile.size() ) {
      f.open(logFile.c_str(), ios::out);
      if (!f)
	errorAbort("Cannot open file: " + logFile + "\n");
      
      f << "minDeltaLogLik: " << minDeltaLogLik << endl;
      f << "maxIter:        " << maxIter << endl;
      f << endl;
      f << "iter" << "\t" << "logLik" << "\t" << "deltaLogLik" << endl;
    }

    while ( ( (deltaLogLik > minDeltaLogLik) or iter == 0) and iter < maxIter) {
      logLik = 0;
      reset(facExpCounts);

      // reset varData and facData
      varData.reset(varDataFile, dfgInfo.varNames);
      if (facDataFile.size() != 0) {
	facDataPtr->reset(facDataFile, dfgInfo.facNames);
      }
      if (subVarDataFile.size() != 0) {
	subVarDataPtr->reset(subVarDataFile, dfgInfo.subNames);
      }
      unsigned lineCount = 0;
      while ( varData.next(idVar, varVec) ) {
	lineCount++;
	resetFactorPotential(facDataPtr, idVar, lineCount, dfgInfo.dfg);
	updateFactorPotentials(subVarDataPtr, idVar, lineCount, dfgInfo);
	dfgInfo.stateMaskMapSet.symbols2StateMasks( stateMaskVec, varVec, varData.map() );

	//	//debug begin
	//	string facName("CpG_P_1.likelihood");
	//	unsigned facIdx = find(dfgInfo.facNames.begin(), dfgInfo.facNames.end(), facName) - dfgInfo.facNames.begin();
	//	cout << "factor name: " << facName << "; facIdx: " << facIdx << "; potential " << dfg.getFactor(facIdx).potential <<  endl;
	//	//debug end

	calcFacAccMarAndNormConst(facExpCounts, tmpFacMar, normConst, stateMaskVec, dfg);

	logLik += - log(normConst);
      }

      factorSet.submitCounts(facExpCounts);
      factorSet.optimizeParameters();
      dfg.resetFactorPotentials( factorSet.mkFactorVec() );
      factorSet.clearCounts();

      deltaLogLik = abs(prevLogLik - logLik); // set at abs(-logLik) in iter 0

      if ( logFile.size() )
	f << iter << "\t" << logLik << "\t" << deltaLogLik << endl;


      if ( logStateMapsFile.size() )
	writeDfgInfo(dfgInfo, logStateMapsFile, logFactorPotentialsFile, logVariablesFile, logFactorGraphFile);

      //
      //      // debug
      //      cout << "from dfgEm:" << endl;
      //      cout << "iter:        " << iter << endl;
      //      cout << "logLik:      " << logLik << endl;
      //      cout << "prevLogLik:  " << prevLogLik << endl;
      //      cout << "deltaLogLik: " << deltaLogLik << endl;
      //      cout << "minDeltaLogLik: " << minDeltaLogLik << endl;
      //      cout << "maxIter: " << maxIter << endl;
      //      cout << "( ( (deltaLogLik > minDeltaLogLik) or iter == 0) and iter < maxIter): " << ( ( (deltaLogLik > minDeltaLogLik) or iter == 0) and iter < maxIter) << endl;
      //

      //      //debug begin
      //      cout << endl << "iteration #: " << iter << endl << endl;
      //      //debug end

      prevLogLik = logLik;
      iter++;
    }

    // clean up
    if (facDataPtr != NULL)
      delete facDataPtr;
  }