コード例 #1
0
ファイル: predict_ptron.cpp プロジェクト: EISALab/DBN
int Ptron_clean( int argc, char** argv, ostream& errMsg){
  errMsg << "Clean file\n";
  //errMsg << "[mode]: training(0), testing(1), both(2), Anomaly Detection(3), Error-D(4), Error-DaM(5)\n";
  //errMsg << "[norm]: unnormalized(0)/normalized(1) data\n";
  errMsg << "[datafile]: Name of file containing data. \n";
  errMsg << "[tilt_time] :  (0) Do NOT use tilted time (1) use tilted time.\n";
  errMsg << "[nvar]: Number of variables in datafile. \n";
  errMsg << "[tgtIdx]: Column of target variable (first column = 0).\n";
  errMsg << "[delay1]: delay before starting time-series of first variable\n";
  errMsg << "[nlag1]: number of lags to compose the time-series of the first variable from\n";
  errMsg << "[delay2]: delay before starting time-series of secon variable\n";
  errMsg << "[nlag2]: number of lags to compose the time-series of the second variable from\n";
  errMsg << "\n\n\n";
  if ( argc != 9+2*(atoi(argv[7])) ) return(0);
  //
  string prefix = argv[2];
  cout << "# I/O prefix: " << prefix << endl;
  //
  int norm = atoi(argv[4]);
  cout << "# Using normalized params: " << norm << endl;
  if( norm!=0 && norm!=1 ){
    errMsg << "Error: Invalid variable [norm]\n";
    return(0);
  }
  //
  cout << "# Inputfile is: " << argv[5] << endl;
  ifstream ifile( argv[5] );
  if( !ifile ) {
    cerr << "ERROR: cannot open \"" << argv[5] << "\"... aborting\n";
    return( 0 );
  }
  //
  int mitigation;
  if( atoi( argv[3] ) == 6 ) mitigation=0;
  if( atoi( argv[3] ) == 7 ) mitigation=1;
  if( mitigation == 0 ) cout << "# Excising anomalies.\n";
  if( mitigation == 1 ) cout << "# Correcting anomalies.\n";
  int tilt_time = atoi( argv[6] );
  if( tilt_time == 0 ) cout << "# Using linear time window.\n";
  if( tilt_time == 1 ) cout << "# Using tilted time window\n";
    //
  int nvar = atoi(argv[7]);
  cout << "# nvar is: " << nvar << endl;
  //
  int tgtIdx = atoi(argv[8]);
  cout << "# Target index is: " << tgtIdx << endl;
  if( tgtIdx > nvar-1 ){
    errMsg << "ERROR: Invalid target index\n";
    return( 0 );
  }
  //
  vector<int> delay(nvar);
  vector<int> nlags(nvar);
  cout << "# ( delay , lag )\n";
  for(int i=0; i< nvar; i++){
    int argvIdx = 9+2*i;
    delay[i] = atoi(argv[argvIdx]);
    nlags[i] = atoi(argv[argvIdx+1]);
    if( i==tgtIdx && delay[i] <= 0){
      delay[i]==1;
    }
    cout << "# ( " << delay[i] << "," << nlags[i] << " )" << endl;
  }
  int nAtt = 0;
  for( int i=0; i< nvar; i++){
    nAtt += nlags[i];
  }
  //
  cout << "# Reading Data" << endl;
  vector< ts_record > Records;
  if( GetRecords( ifile, Records ) != 1 ){
  errMsg << "ERROR: could not read records from " << argv[5] << "... aborting.\n";
    return( 0 );
  }
  ifile.close();
  cout << "# Read " << Records.size() << " records\n";
  //
  vector< vector< float > > normParam;
  string npfile = prefix + "-norm_param.dat";
  GetNormParam( npfile, normParam );
  //
  string pfile_name = prefix + "-ptron_predictor.out";
  adet_ptron model( pfile_name );
  //
  int last_written_idx = 0;
  bool write = false;
  int inc = 30;
  int timer=-2;
  int ErrCnt = 0;
  int ExampCnt = 0;
  timestamp lastOutput;
  for( int i=0; i < Records.size(); i++ ){
    ts_record newEx;
    int secDiff;
    if( i==0) lastOutput = Records[i].TS();
    // if record can be cleaned -- do so
    if( MakeLinearTimeExample( Records, i, tgtIdx-1, delay, nlags, newEx) == 1 ){
      //
      //vector< float > Example = newEx.Data();
      if( norm == 1 ) NormalizeExample( newEx.Data(), normParam );
      //
      vector< float > result = model.TestHelper( newEx.Data() );
      //
      if( norm == 1 ) UnnormalizeResult( result, normParam );
      //
      ExampCnt++;
      ts_record outputVal( Records[i] );
      if( result[4] == 0 ){
        ErrCnt++;
        Records[i].Data()[tgtIdx-1] = result[2];
        if( mitigation==1 ){
          outputVal.Data()[tgtIdx-1] = result[2];
        }
        else{
          outputVal.Data()[tgtIdx-1] = outputVal.NAFlag();
        }
      }
      outputVal.PrintCSV(1, cout );
    }
    //  otherwise just leave record uncleaned
    else{
      Records[i].PrintCSV(1, cout );
    }
  }
  cout << "# Number of Examples: " << ExampCnt << endl;
  cout << "# Number of Errors: " << ErrCnt << endl;
  return( 1 );
}
コード例 #2
0
//セルデータの出力
void outputdata(void)
{
	FILE *fp;
	wchar_t buf[1024];
	// ファイルのオープン
	fopen_s(&fp, output_filename.c_str(), "w,ccs=UTF-8");
	if (fp == NULL)
	{
		//エラー
		printf("file open error!!\n");
		err = true;
	}
	if (err == false)
	{
		//ロケール指定
		setlocale(LC_ALL, "japanese");

		//ヘッダ出力
		fputws(_T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"), fp);
		fputws(_T("<ssbx version=\"1.0\">\n"), fp);


		{
			//アニメーション情報
			wchar_t	name[256];
			size_t wLen = 0;
			mbstowcs_s(&wLen, name, 256, ssbp_name.c_str(), _TRUNCATE);
			swprintf_s(buf, 1024, _T("  <name>%s</name>\n"), name);
			fputws(buf, fp);

			{
				//セル情報
				fputws(_T("  <textures>\n"), fp);
				int i = 0;
				for (i = 0; i < TEXDATA_MAX; i++)
				{
					if (texdata[i].id == -1)
					{
						break;	//データ終わり
					}
					wchar_t	name[256];
					size_t wLen = 0;
					mbstowcs_s(&wLen, name, 256, texdata[i].name.c_str(), _TRUNCATE);

					swprintf_s(buf, 1024, _T("      <texture id=\"%d\" name=\"%s\" />\n"), texdata[i].id, name);
					fputws(buf, fp);
				}
				fputws(_T("  </textures>\n"), fp);
			}
		}

		ss::ResourceSet* rs = resman->getData(ssbp_name);

		for (std::map<std::string, ss::AnimeRef*>::iterator itpairstri = rs->animeCache->_dic.begin(); itpairstri != rs->animeCache->_dic.end(); itpairstri++)
		{
			// イテレータは pair<const string, int> 型なので、
			std::string strKey = itpairstri->first;     // イテレータからキーが得られる。


			if (strKey.find("/") == std::string::npos)	//ssae名が含まれていない場合はスキップ
			{
				continue;
			}


			//再生するモーションを設定
			ssplayer->play(strKey);				 // アニメーション名を指定(ssae名/アニメーション名も可能、詳しくは後述)
			ssplayer->pause();

			fputws(_T("  <motion>\n"), fp);
			{
				//モーション情報
				wchar_t	name[256];
				size_t wLen = 0;
				mbstowcs_s(&wLen, name, 256, strKey.c_str(), _TRUNCATE);
				swprintf_s(buf, 1024, _T("    <name>%s</name>\n"), name);
				fputws(buf, fp);

				swprintf_s(buf, 1024, _T("    <fps>%d</fps>\n"), ssplayer->getFPS());
				fputws(buf, fp);
				swprintf_s(buf, 1024, _T("    <maxframe>%d</maxframe>\n"), ssplayer->getMaxFrame());
				fputws(buf, fp);

			}

			{
				//フレームデータ
				fputws(_T("    <framedata>\n"), fp);
				int maxframe = ssplayer->getMaxFrame();
				int i; 
				for (i = 0; i < maxframe; i++)
				{
					swprintf_s(buf, 1024, _T("      <frame time=\"%d\">\n"), i);
					fputws(buf, fp);

					ssplayer->setFrameNo(i);
					ssplayer->update(0);


					partdata_index = 0;	//パーツインデックスの初期化
					int idx = 0;
					for (idx = 0; idx < PARTDATA_MAX; idx++ )
					{
						partdata[idx].texture = -2;	//データなし
					}


					ssplayer->draw();	//パーツステータスの取得

					//データ書き出し
					idx = 0;
					while (1)
					{
						if ( partdata[idx].texture == -2 )
						{
							//データ終了
							break;
						}
						//パーツステートを書き出す
						fputws(_T("        <partdata"), fp);
						
						{
							wchar_t	name[256];
							size_t wLen = 0;
							mbstowcs_s(&wLen, name, 256, partdata[idx].name.c_str(), _TRUNCATE);
							swprintf_s(buf, 1024, _T(" NAME=\"%s\""), name);
							fputws(buf, fp);
						}

	//					if (partdata[idx].x != 0.0f )
						{
							fputws(_T(" X="), fp);
							outputVal(fp, partdata[idx].x);
						}

	//					if (partdata[idx].y != 0.0f)
						{
							fputws(_T(" Y="), fp);
							outputVal(fp, partdata[idx].y);
						}

	//					if (partdata[idx].rotationZ != 0.0f)
						{
							fputws(_T(" ROTZ="), fp);
							outputVal(fp, partdata[idx].rotationZ);
						}

	//					if (partdata[idx].scaleX != 0.0f)
						{
							fputws(_T(" SCLX="), fp);
							outputVal(fp, partdata[idx].scaleX);
						}

	//					if (partdata[idx].scaleY != 0.0f)
						{
							fputws(_T(" SCLY="), fp);
							outputVal(fp, partdata[idx].scaleY);
						}

	//					if (partdata[idx].opacity != 0)
						{
							fputws(_T(" ALPH="), fp);
							outputVal(fp, partdata[idx].opacity);
						}

	//					if (partdata[idx].size_X != 0.0f)
						{
							fputws(_T(" SIZX="), fp);
							outputVal(fp, partdata[idx].size_X);
						}

	//					if (partdata[idx].size_Y != 0.0f)
						{
							fputws(_T(" SIZY="), fp);
							outputVal(fp, partdata[idx].size_Y);
						}

	//					if (partdata[idx].boundingRadius != 0.0f)
						{
							fputws(_T(" BORA="), fp);
							outputVal(fp, partdata[idx].boundingRadius);
						}
	/*
						swprintf_s(buf, 1024, _T(" colorBlendFunc=\"%d\""), partdata[idx].colorBlendFunc);
						fputws(buf, fp);

						swprintf_s(buf, 1024, _T(" colorBlendType=\"%d\""), partdata[idx].colorBlendType);
						fputws(buf, fp);
	*/
	//					if (partdata[idx].flipX != 0)
						{
							fputws(_T(" FLPX="), fp);
							outputVal(fp, partdata[idx].flipX);
						}

	//					if (partdata[idx].flipY != 0)
						{
							fputws(_T(" FLPY="), fp);
							outputVal(fp, partdata[idx].flipY);
						}

						fputws(_T(" TXID="), fp);
						outputVal(fp, partdata[idx].texture);

						fputws(_T(" RECX="), fp);
						outputVal(fp, partdata[idx].rect.x);
						fputws(_T(" RECY="), fp);
						outputVal(fp, partdata[idx].rect.y);
						fputws(_T(" RECW="), fp);
						outputVal(fp, partdata[idx].rect.w);
						fputws(_T(" RECH="), fp);
						outputVal(fp, partdata[idx].rect.h);

	//					if (partdata[idx].blendfunc != 0)
						{
							fputws(_T(" BLED="), fp);
							outputVal(fp, partdata[idx].blendfunc);
						}


						fputws(_T(" />\n"), fp);

						idx++;
					}
					fputws(_T("      </frame>\n"), fp);
				}
				fputws(_T("    </framedata>\n"), fp);
			}
			fputws(_T("  </motion>\n"), fp);
		}
		//フッダ出力
		fputws(_T("</ssbx>\n"), fp);
		fputws(_T("\n"), fp);

	}
	fclose(fp);	//ファイルのクローズ
}