//********************************************************************************************************************** int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& out) { try { //format data vector< map<float, int> > tableX; tableX.resize(numaxes); map<float, int>::iterator itTable; vector< vector<spearmanRank> > scores; scores.resize(numaxes); for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) { vector<float> temp = it->second; for (int i = 0; i < temp.size(); i++) { spearmanRank member(it->first, temp[i]); scores[i].push_back(member); //count number of repeats itTable = tableX[i].find(temp[i]); if (itTable == tableX[i].end()) { tableX[i][temp[i]] = 1; }else { tableX[i][temp[i]]++; } } } //calc LX //for each axis vector<double> Lx; Lx.resize(numaxes, 0.0); for (int i = 0; i < numaxes; i++) { for (itTable = tableX[i].begin(); itTable != tableX[i].end(); itTable++) { double tx = (double) itTable->second; Lx[i] += ((pow(tx, 3.0) - tx) / 12.0); } } //sort each axis for (int i = 0; i < numaxes; i++) { sort(scores[i].begin(), scores[i].end(), compareSpearman); } //find ranks of xi in each axis map<string, vector<float> > rankAxes; for (int i = 0; i < numaxes; i++) { vector<spearmanRank> ties; int rankTotal = 0; for (int j = 0; j < scores[i].size(); j++) { rankTotal += (j+1); ties.push_back(scores[i][j]); if (j != (scores[i].size()-1)) { // you are not the last so you can look ahead if (scores[i][j].score != scores[i][j+1].score) { // you are done with ties, rank them and continue for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); rankAxes[ties[k].name].push_back(thisrank); } ties.clear(); rankTotal = 0; } }else { // you are the last one for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); rankAxes[ties[k].name].push_back(thisrank); } } } } //for each otu for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) { if (metadatafile == "") { out << i+1; } else { out << metadataLabels[i]; } //find the ranks of this otu - Y vector<spearmanRank> otuScores; map<float, int> tableY; for (int j = 0; j < lookupFloat.size(); j++) { spearmanRank member(lookupFloat[j]->getGroup(), lookupFloat[j]->getAbundance(i)); otuScores.push_back(member); itTable = tableY.find(member.score); if (itTable == tableY.end()) { tableY[member.score] = 1; }else { tableY[member.score]++; } } //calc Ly double Ly = 0.0; for (itTable = tableY.begin(); itTable != tableY.end(); itTable++) { double ty = (double) itTable->second; Ly += ((pow(ty, 3.0) - ty) / 12.0); } sort(otuScores.begin(), otuScores.end(), compareSpearman); map<string, float> rankOtus; vector<spearmanRank> ties; int rankTotal = 0; for (int j = 0; j < otuScores.size(); j++) { rankTotal += (j+1); ties.push_back(otuScores[j]); if (j != (otuScores.size()-1)) { // you are not the last so you can look ahead if (otuScores[j].score != otuScores[j+1].score) { // you are done with ties, rank them and continue for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); rankOtus[ties[k].name] = thisrank; } ties.clear(); rankTotal = 0; } }else { // you are the last one for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); rankOtus[ties[k].name] = thisrank; } } } vector<double> pValues(numaxes); //calc spearman ranks for each axis for this otu for (int j = 0; j < numaxes; j++) { double di = 0.0; for (int k = 0; k < lookupFloat.size(); k++) { float xi = rankAxes[lookupFloat[k]->getGroup()][j]; float yi = rankOtus[lookupFloat[k]->getGroup()]; di += ((xi - yi) * (xi - yi)); } double p = 0.0; double n = (double) lookupFloat.size(); double SX2 = ((pow(n, 3.0) - n) / 12.0) - Lx[j]; double SY2 = ((pow(n, 3.0) - n) / 12.0) - Ly; p = (SX2 + SY2 - di) / (2.0 * sqrt((SX2*SY2))); if (isnan(p) || isinf(p)) { p = 0.0; } out << '\t' << p; pValues[j] = p; //signifigance calc - http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient double temp = (lookupFloat.size()-2) / (double) (1- (p*p)); temp = sqrt(temp); double sig = p*temp; if (isnan(sig) || isinf(sig)) { sig = 0.0; } out << '\t' << sig; } double sum = 0; for(int k=0;k<numaxes;k++){ sum += pValues[k] * pValues[k]; } out << '\t' << sqrt(sum) << endl; } return 0; } catch(exception& e) { m->errorOut(e, "CorrAxesCommand", "calcSpearman"); exit(1); } }
std::vector<std::string> ScriptSystem::load() { std::vector<std::string> filenames; static const string types[] = { "config", "global", "extensions", "classes", "states", "ui" }; for (unsigned int i = 0; i < sizeof(types) / sizeof(types[0]); i++) { std::vector<std::string> gathered = chi::System::gatherFiles( System::resourceLocation() + "/script/" + types[i], "(.*\\.js|.*\\.coffee)" ); for (unsigned int j = 0; j < gathered.size(); j++) { filenames.push_back(gathered[j]); } } sort(filenames.begin(), filenames.end()); HandleScope scope; for (unsigned int i = 0; i < filenames.size(); i++) { Script *script = Script::factory->create(); scripts[filenames[i]] = script->compileFile(filenames[i]); } std::vector<std::string> loadedFilenames; while (scripts.size() > 0) { unsigned int size = scripts.size(); vector<string>::iterator i = filenames.begin(); while (i != filenames.end()) { try { scripts[*i]->run(); loadedFilenames.push_back(*i); delete scripts[*i]; scripts.erase(*i); i = filenames.erase(i); } catch (std::exception &e) { ++i; } } if (size == scripts.size()) { std::string text; map<string, Script *>::iterator i = scripts.begin(); while (i != scripts.end()) { try { (i->second)->run(); } catch (std::exception &e) { text += e.what(); } delete i->second; scripts.erase(i++); } throw chi::Exception(text); } } return loadedFilenames; }
void print(map<int, int> & distances) { for (MapIntIterator it = distances.begin(); it != distances.end(); ++it) { cout << it -> first << " => " << it -> second << endl; } }
void update(int value) { // Conta i cicli di presentazione dello stimolo if ( (sumOutside > str2num<int>(parameters.find("StimulusCycles")) ) && (trialMode == STIMULUSMODE) ) { sumOutside=0; trialMode++; trialMode=trialMode%4; } if (conditionInside && (sumOutside*2 > str2num<int>(parameters.find("FixationCycles"))) && (trialMode ==FIXATIONMODE ) ) { sumOutside=0; trialMode++; trialMode=trialMode%4; stimulusDuration.start(); } if ( trialMode == STIMULUSMODE ) stimulusFrames++; if ( trialMode == FIXATIONMODE ) stimulusFrames=0; Screen screenPassive; screenPassive.setWidthHeight(SCREEN_WIDE_SIZE, SCREEN_WIDE_SIZE*SCREEN_HEIGHT/SCREEN_WIDTH); screenPassive.setOffset(alignmentX,alignmentY); screenPassive.setFocalDistance(0); screenPassive.transform(headEyeCoords.getRigidStart().getFullTransformation()*Translation3d(center)); camPassive.init(screenPassive); camPassive.setDrySimulation(true); camPassive.setEye(eyeRight); objectPassiveTransformation = ( camPassive.getModelViewMatrix()*objectActiveTransformation ); // Coordinates picker markers = optotrak.getAllPoints(); if ( isVisible(markers[1]) && isVisible(markers[2]) && isVisible(markers[3]) ) headEyeCoords.update(markers[1],markers[2],markers[3]); Affine3d active = headEyeCoords.getRigidStart().getFullTransformation(); eulerAngles.init( headEyeCoords.getRigidStart().getFullTransformation().rotation() ); eyeLeft = headEyeCoords.getLeftEye(); eyeRight = headEyeCoords.getRightEye(); cyclopeanEye = (eyeLeft+eyeRight)/2.0; // Projection of view normal on the focal plane Vector3d directionOfSight = (active.rotation()*Vector3d(0,0,-1)).normalized(); Eigen::ParametrizedLine<double,3> lineOfSightRight = Eigen::ParametrizedLine<double,3>::Through( eyeRight , eyeRight+directionOfSight ); Eigen::ParametrizedLine<double,3> lineOfSightLeft = Eigen::ParametrizedLine<double,3>::Through( eyeLeft, eyeLeft+directionOfSight ); double lineOfSightRightDistanceToFocalPlane = lineOfSightRight.intersection(focalPlane); double lineOfSightLeftDistanceToFocalPlane = lineOfSightLeft.intersection(focalPlane); //double lenghtOnZ = (active*(center-eyeCalibration )+eyeRight).z(); projPointEyeRight = lineOfSightRightDistanceToFocalPlane *(directionOfSight)+ (eyeRight); projPointEyeLeft= lineOfSightLeftDistanceToFocalPlane * (directionOfSight) + (eyeLeft); // second projection the fixation point computed with z non constant but perfectly parallel to projPointEyeRight lineOfSightRightDistanceToFocalPlane= (( active.rotation()*(center)) - eyeRight).norm(); Vector3d secondProjection = lineOfSightRightDistanceToFocalPlane *(directionOfSight)+ (eyeRight); if ( !zOnFocalPlane ) projPointEyeRight=secondProjection ; // Compute the translation to move the eye in order to avoid shear components Vector3d posAlongLineOfSight = (headEyeCoords.getRigidStart().getFullTransformation().rotation())*(eyeRight -eyeCalibration); switch ( (int)factors["Translation"] ) { case -1: case -2: translationFactor.setZero(); if ( trialMode == STIMULUSMODE ) projPointEyeRight=center; break; case 0: translationFactor.setZero(); break; case 1: translationFactor = factors["TranslationConstant"]*Vector3d(posAlongLineOfSight.z(),0,0); break; case 2: translationFactor = factors["TranslationConstant"]*Vector3d(0,posAlongLineOfSight.z(),0); break; } if ( passiveMode ) initProjectionScreen(0,headEyeCoords.getRigidStart().getFullTransformation()*Translation3d(Vector3d(0,0,focalDistance))); else initProjectionScreen(focalDistance,Affine3d::Identity()); checkBounds(); /**** Save to file part ****/ // Markers file save the used markers and time-depending experimental variable to a file // (Make sure that in passive experiment the list of variables has the same order) markersFile << trialNumber << " " << headCalibrationDone << " " << trialMode << " " ; markersFile <<markers[1].transpose() << " " << markers[2].transpose() << " " << markers[3].transpose() << " " << markers[17].transpose() << " " << markers[18].transpose() << " " ; markersFile << factors["Tilt"] << " " << factors["Slant"] << " " << factors["Translation"] << " " << factors["Onset"] << " " << factors["TranslationConstant"] << endl; ofstream outputfile; outputfile.open("data.dat"); outputfile << "Subject Name: " << parameters.find("SubjectName") << endl; outputfile << "Passive matrix:" << endl << objectPassiveTransformation.matrix() << endl; outputfile << "Yaw: " << toDegrees(eulerAngles.getYaw()) << endl <<"Pitch: " << toDegrees(eulerAngles.getPitch()) << endl; outputfile << "EyeLeft: " << headEyeCoords.getLeftEye().transpose() << endl; outputfile << "EyeRight: " << headEyeCoords.getRightEye().transpose() << endl << endl; outputfile << "Slant: " << instantPlaneSlant << endl; outputfile << "(Width,Height) [px]: " << getPlaneDimensions().transpose() << " " << endl; outputfile << "Factors:" << endl; for (map<string,double>::iterator iter=factors.begin(); iter!=factors.end(); ++iter) { outputfile << "\t\t" << iter->first << "= " << iter->second << endl; } outputfile << "Trial remaining: " << trial.getRemainingTrials()+1 << endl; outputfile << "Last response: " << probeAngle << endl; // Here we save plane projected width and height // now rewind the file outputfile.clear(); outputfile.seekp(0,ios::beg); // Write down frame by frame the trajectories and angles of eyes and head if ( trialMode == STIMULUSMODE && headCalibrationDone > 2 ) { trajFile << setw(6) << left << trialNumber << " " << stimulusFrames << " " << eyeRight.transpose() << endl; anglesFile << setw(6) << left << trialNumber << " " << stimulusFrames << " " << toDegrees(eulerAngles.getPitch()) << " " << toDegrees(eulerAngles.getRoll()) << " " << toDegrees(eulerAngles.getYaw()) << " " << instantPlaneSlant << endl; matrixFile << setw(6) << left << trialNumber << " " << stimulusFrames << " " ; for (int i=0; i<3; i++) matrixFile << objectPassiveTransformation.matrix().row(i) << " " ; matrixFile << endl; // Write the 13 special extremal points on stimFile stimFile << setw(6) << left << trialNumber << " " << stimulusFrames << " " ; double winx=0,winy=0,winz=0; for (PointsRandIterator iRand = redDotsPlane.specialPointsRand.begin(); iRand!=redDotsPlane.specialPointsRand.end(); ++iRand) { Point3D *p=(*iRand); Vector3d v = objectActiveTransformation*Vector3d( p->x, p->y, p->z); gluProject(v.x(),v.y(),v.z(), (&cam)->getModelViewMatrix().data(), (&cam)->getProjectiveMatrix().data(), (&cam)->getViewport().data(), &winx,&winy,&winz); stimFile << winx << " " << winy << " " << winz << " "; } stimFile << endl; } glutPostRedisplay(); glutTimerFunc(TIMER_MS, update, 0); }
void handleRESTQuery( string ns , string action , map<string,string> & params , int & responseCode , stringstream & out ) { Timer t; int skip = _getOption( params["skip"] , 0 ); int num = _getOption( params["limit"] , _getOption( params["count" ] , 1000 ) ); // count is old, limit is new int one = 0; if ( params["one"].size() > 0 && tolower( params["one"][0] ) == 't' ) { num = 1; one = 1; } BSONObjBuilder queryBuilder; for ( map<string,string>::iterator i = params.begin(); i != params.end(); i++ ) { if ( ! i->first.find( "filter_" ) == 0 ) continue; const char * field = i->first.substr( 7 ).c_str(); const char * val = i->second.c_str(); char * temp; // TODO: this is how i guess if something is a number. pretty lame right now double number = strtod( val , &temp ); if ( temp != val ) queryBuilder.append( field , number ); else queryBuilder.append( field , val ); } BSONObj query = queryBuilder.obj(); auto_ptr<DBClientCursor> cursor = db.query( ns.c_str() , query, num , skip ); if ( one ) { if ( cursor->more() ) { BSONObj obj = cursor->next(); out << obj.jsonString() << "\n"; } else { responseCode = 404; } return; } out << "{\n"; out << " \"offset\" : " << skip << ",\n"; out << " \"rows\": [\n"; int howMany = 0; while ( cursor->more() ) { if ( howMany++ ) out << " ,\n"; BSONObj obj = cursor->next(); out << " " << obj.jsonString(); } out << "\n ],\n\n"; out << " \"total_rows\" : " << howMany << " ,\n"; out << " \"query\" : " << query.jsonString() << " ,\n"; out << " \"millis\" : " << t.millis() << "\n"; out << "}\n"; }
void CPeripherals::GetSettingsFromMappingsFile(TiXmlElement *xmlNode, map<CStdString, CSetting *> &m_settings) { TiXmlElement *currentNode = xmlNode->FirstChildElement("setting"); int iMaxOrder(0); while (currentNode) { CSetting *setting = NULL; CStdString strKey(currentNode->Attribute("key")); if (strKey.IsEmpty()) continue; CStdString strSettingsType(currentNode->Attribute("type")); int iLabelId = currentNode->Attribute("label") ? atoi(currentNode->Attribute("label")) : -1; bool bConfigurable = (!currentNode->Attribute("configurable") || strcmp(currentNode->Attribute("configurable"), "") == 0 || (strcmp(currentNode->Attribute("configurable"), "no") != 0 && strcmp(currentNode->Attribute("configurable"), "false") != 0 && strcmp(currentNode->Attribute("configurable"), "0") != 0)); if (strSettingsType.Equals("bool")) { bool bValue = (strcmp(currentNode->Attribute("value"), "no") != 0 && strcmp(currentNode->Attribute("value"), "false") != 0 && strcmp(currentNode->Attribute("value"), "0") != 0); setting = new CSettingBool(0, strKey, iLabelId, bValue, CHECKMARK_CONTROL); } else if (strSettingsType.Equals("int")) { int iValue = currentNode->Attribute("value") ? atoi(currentNode->Attribute("value")) : 0; int iMin = currentNode->Attribute("min") ? atoi(currentNode->Attribute("min")) : 0; int iStep = currentNode->Attribute("step") ? atoi(currentNode->Attribute("step")) : 1; int iMax = currentNode->Attribute("max") ? atoi(currentNode->Attribute("max")) : 255; CStdString strFormat(currentNode->Attribute("format")); setting = new CSettingInt(0, strKey, iLabelId, iValue, iMin, iStep, iMax, SPIN_CONTROL_INT, strFormat); } else if (strSettingsType.Equals("float")) { float fValue = currentNode->Attribute("value") ? (float) atof(currentNode->Attribute("value")) : 0; float fMin = currentNode->Attribute("min") ? (float) atof(currentNode->Attribute("min")) : 0; float fStep = currentNode->Attribute("step") ? (float) atof(currentNode->Attribute("step")) : 0; float fMax = currentNode->Attribute("max") ? (float) atof(currentNode->Attribute("max")) : 0; setting = new CSettingFloat(0, strKey, iLabelId, fValue, fMin, fStep, fMax, SPIN_CONTROL_FLOAT); } else if (strSettingsType.Equals("enum")) { CStdString strEnums(currentNode->Attribute("lvalues")); if (!strEnums.IsEmpty()) { map<int,int> enums; vector<CStdString> valuesVec; CUtil::Tokenize(strEnums, valuesVec, "|"); for (unsigned int i = 0; i < valuesVec.size(); i++) enums.insert(make_pair(atoi(valuesVec[i]), atoi(valuesVec[i]))); int iValue = currentNode->Attribute("value") ? atoi(currentNode->Attribute("value")) : 0; setting = new CSettingInt(0, strKey, iLabelId, iValue, enums, SPIN_CONTROL_TEXT); } } else { CStdString strValue(currentNode->Attribute("value")); setting = new CSettingString(0, strKey, iLabelId, strValue, EDIT_CONTROL_INPUT, !bConfigurable, -1); } if (setting) { //TODO add more types if needed /* set the visibility */ setting->SetVisible(bConfigurable); /* set the order */ int iOrder(0); currentNode->Attribute("order", &iOrder); /* if the order attribute is invalid or 0, then the setting will be added at the end */ if (iOrder < 0) iOrder = 0; setting->SetOrder(iOrder); if (iOrder > iMaxOrder) iMaxOrder = iOrder; /* and add this new setting */ m_settings[strKey] = setting; } currentNode = currentNode->NextSiblingElement("setting"); } /* add the settings without an order attribute or an invalid order attribute set at the end */ for (map<CStdString, CSetting *>::iterator it = m_settings.begin(); it != m_settings.end(); it++) { if (it->second->GetOrder() == 0) it->second->SetOrder(++iMaxOrder); } }
void __stdcall GFGoodBuy(struct SGFGoodBuyInfo const &gbi, unsigned int iClientID) { returncode = DEFAULT_RETURNCODE; const GoodInfo *packageInfo = GoodList::find_by_id(gbi.iGoodID); /* if (packageInfo->iType == 0) { PrintUserCmdText(iClientID, L"This should be a commodity (equipment?) purchase"); } */ if (packageInfo->iType == 1) { //PrintUserCmdText(iClientID, L"This should be an equipment (maybe?) purchase"); uint iBase; pub::Player::GetBase(iClientID, iBase); bool aminiceitem = true; string itemname; int wearecool = 0; //in this case, it's more efficent to check if it's an item under watch first. for (map<uint, string>::iterator iter = mapACItems.begin(); iter!=mapACItems.end(); iter++) { if (iter->first == gbi.iGoodID) { //PrintUserCmdText(iClientID, L"I have found this commodity"); //We iterate through the base names to see if it's a non-POB base list<uint>::iterator i = mapACBases.begin(); while (i != mapACBases.end()) { if (*i == iBase) { if (mapACSales[iClientID].items.find(gbi.iGoodID) != mapACSales[iClientID].items.end()) { --mapACSales[iClientID].items.find(gbi.iGoodID)->second; //PrintUserCmdText(iClientID, L"DEBUG: I have found this sale, letting the purchase go through."); aminiceitem = true; wearecool = 1; wstring wscCharname = (const wchar_t*) Players.GetActiveCharacterName(iClientID); wstring wscBaseName = HkGetBaseNickByID(iBase); //PrintUserCmdText(iClientID, L"DEBUG: %i purchases left.", mapACSales[iClientID].items.find(gbi.iGoodID)->second); wstring wscMsgLog = L"<%sender> has bought back <%item> from base <%basename> (%isale purchases left)"; wscMsgLog = ReplaceStr(wscMsgLog, L"%sender", wscCharname.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%basename", wscBaseName.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%item", stows(iter->second).c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%isale", stows(itos(mapACSales[iClientID].items.find(gbi.iGoodID)->second))).c_str(); string scText = wstos(wscMsgLog); Logging("%s", scText.c_str()); if (mapACSales[iClientID].items.find(gbi.iGoodID)->second == 0) { mapACSales[iClientID].items.erase(gbi.iGoodID); //PrintUserCmdText(iClientID, L"DEBUG: no purchases left"); } break; } if (wearecool == 0) { //PrintUserCmdText(iClientID, L"DEBUG: I have found this base, not good"); aminiceitem = false; itemname = iter->second; } } i++; } } if (aminiceitem == false) { returncode = SKIPPLUGINS_NOFUNCTIONCALL; wstring wscCharname = (const wchar_t*) Players.GetActiveCharacterName(iClientID); wstring wscBaseName = HkGetBaseNickByID(iBase); pub::Player::SendNNMessage(iClientID, pub::GetNicknameId("nnv_anomaly_detected")); wstring wscMsgU = L"MF: %name has been permabanned. (Type 2)"; wscMsgU = ReplaceStr(wscMsgU, L"%name", wscCharname.c_str()); HkMsgU(wscMsgU); wstring wscMsgLog = L"<%sender> was permabanned for attempting to buy an illegal item <%item> from base <%basename> (see DSAM)"; wscMsgLog = ReplaceStr(wscMsgLog, L"%sender", wscCharname.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%basename", wscBaseName.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%item", stows(itemname).c_str()); LogCheater(iClientID, wscMsgLog); } } } else if (packageInfo->iType == 3) { uint iBase; pub::Player::GetBase(iClientID, iBase); //PrintUserCmdText(iClientID, L"This should be a ship purchase"); bool aminiceship = false; for (map<uint, list<uint>>::iterator iter = mapACShips.begin(); iter!=mapACShips.end(); iter++) { if (iter->first == iBase) { //PrintUserCmdText(iClientID, L"This should be a base"); // we check if one of the three packages sold here is the correct one list<uint>::iterator i = iter->second.begin(); while (i != iter->second.end()) { if (*i == gbi.iGoodID) { //PrintUserCmdText(iClientID, L"I have found this ship"); aminiceship = true; break; } i++; } } } if (aminiceship == false) { wstring wscCharname = (const wchar_t*) Players.GetActiveCharacterName(iClientID); wstring wscBaseName = HkGetBaseNickByID(iBase); pub::Player::SendNNMessage(iClientID, pub::GetNicknameId("nnv_anomaly_detected")); wstring wscMsgU = L"MF: %name has been permabanned. (Type 1)"; wscMsgU = ReplaceStr(wscMsgU, L"%name", wscCharname.c_str()); HkMsgU(wscMsgU); wstring wscMsgLog = L"<%sender> was permabanned for attempting to buy an illegal ship from base <%basename> (see DSAM)"; wscMsgLog = ReplaceStr(wscMsgLog, L"%sender", wscCharname.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%basename", wscBaseName.c_str()); LogCheater(iClientID, wscMsgLog); } } }
// recursive function, returns maximum depth of this path int FindBest(int MoneyLeft, int Depth) { vector<int>::iterator it; map<ChangeGiven, int>::iterator iter; map<int,int>::iterator solit; // table of partial solutions, key is the type of coin, value the minimum number of coins possible; best path for this amount of money will be remembered map<int,int> PartialSolutions; pair<int,int> Best; // depth is increased for every level in the recursion and serves as an orientation; numcoins is a buffer variable int NewDepth=Depth+1, MaxDepth, NumCoins, BestCoin; bool check=false; // iterate through the list of coins for (it=CoinList.begin(); it!=CoinList.end(); it++) { // check if coin is still eligible if ((MoneyLeft - (*it)) > 0) { // iterate through the list of visited states for (iter=Table.begin(); iter!=Table.end(); iter++) { // check if state was visited before; set check variable to true if yes if ((MoneyLeft-(*it)) == iter->first.second) { NumCoins = iter->second; check = true; break; } } // if it is a new state, call function again if (check == false) { MaxDepth = FindBest((MoneyLeft-(*it)), NewDepth); NumCoins = MaxDepth - NewDepth; } // make entry for coin Best.first = *it; Best.second = NumCoins; PartialSolutions.insert(Best); } // if this leads to a solution, return depth for determining the number of coins else if ((MoneyLeft - (*it)) == 0) { // type of coin Best.first = *it; // amount of money left Best.second = *it; Table[Best] = 1; return NewDepth; // or Depth? } check = false; } NumCoins = 100000; // iterate over possible paths and remember the best for (solit=PartialSolutions.begin(); solit!=PartialSolutions.end(); solit++) { if (solit->second < NumCoins) { NumCoins = solit->second; BestCoin = solit->first; } } Best.first = BestCoin; Best.second = MoneyLeft; Table[Best] = NumCoins; // prepare return value MaxDepth = NewDepth + NumCoins; return MaxDepth; }
void CUdmApp::UdmMain(Udm::DataNetwork* p_backend, Udm::Object focusObject, std::set<Udm::Object> selectedObjects, long param, map<_bstr_t, _variant_t>& componentParameters, std::string workingDir) { //AllocConsole(); //Py_DebugFlag = 1; //Py_VerboseFlag = 2; // Py_NoSiteFlag = 1; // we import site after setting up sys.path HMODULE python_dll = LoadLibraryA("Python27.dll"); if (python_dll == nullptr) throw udm_exception("Failed to load Python27.dll"); int* Py_NoSiteFlagAddress = reinterpret_cast<int*>(GetProcAddress(python_dll, "Py_NoSiteFlag")); FreeLibrary(python_dll); if (Py_NoSiteFlagAddress == nullptr) throw udm_exception("Failed to find Py_NoSiteFlag in Python27.dll"); *Py_NoSiteFlagAddress = 1; Py_Initialize(); //PY_MAJOR_VERSION //PY_MINOR_VERSION PyGILState_STATE gstate; // n.b. we need this to be reentrant (i.e. in case this interpreter is being called by python (e.g. via win32com.client)) // this is because PyEval_SaveThread() was called gstate = PyGILState_Ensure(); struct UnGil { PyGILState_STATE &state; UnGil(PyGILState_STATE &state) : state(state) {} ~UnGil() { PyGILState_Release(state); } } ungil(gstate); char *path = Py_GetPath(); #ifdef _WIN32 std::string separator = ";"; #else std::string separator = ":"; #endif std::string newpath = path; if (meta_path.length()) { newpath += separator + meta_path + "\\bin"; } //newpath += separator + "C:\\Program Files\\ISIS\\Udm\\bin"; PySys_SetPath(const_cast<char*>(newpath.c_str())); PyObject_RAII main = PyImport_ImportModule("__main__"); PyObject* main_namespace = PyModule_GetDict(main); PyObject_RAII ret = PyRun_StringFlags("import sys\n" "import udm\n", Py_file_input, main_namespace, main_namespace, NULL); if (ret == NULL && PyErr_Occurred()) { throw python_error(GetPythonError()); } HMODULE udm_pyd = LoadLibraryA("udm.pyd"); if (!udm_pyd) { throw python_error("Could not load Udm Python: LoadLibary failed"); } Object_Convert_t Object_Convert = (Object_Convert_t) GetProcAddress(udm_pyd, "Object_Convert"); if (!Object_Convert) { FreeLibrary(udm_pyd); throw python_error("Could not load Udm Python: GetProcAddress failed"); } PyObject_RAII pyFocusObject; if (focusObject) pyFocusObject.p = (*Object_Convert)(focusObject); else { pyFocusObject.p = get_Py_None(); Py_INCREF(pyFocusObject.p); } PyObject_RAII pyRootObject = (*Object_Convert)(p_backend->GetRootObject()); FreeLibrary(udm_pyd); if (meta_path.length()) { newpath += separator + meta_path + "\\bin"; newpath += separator + meta_path + "\\bin\\Python27\\Scripts"; PyObject_RAII prefix = PyString_FromString((meta_path + "\\bin\\Python27").c_str()); PyObject* sys = PyDict_GetItemString(main_namespace, "sys"); PyObject_SetAttrString(sys, "prefix", prefix); PyObject_SetAttrString(sys, "exec_prefix", prefix); } PyObject* CyPhyPython = Py_InitModule("CyPhyPython", CyPhyPython_methods); auto console_messages = componentParameters.find(L"console_messages"); if (console_messages != componentParameters.end() && console_messages->second.vt == VT_BSTR && console_messages->second.bstrVal && wcsicmp(console_messages->second.bstrVal, L"off") == 0) { auto output_dir = componentParameters.find(L"output_dir"); if (output_dir != componentParameters.end() && output_dir->second.vt == VT_BSTR && output_dir->second.bstrVal && *output_dir->second.bstrVal) { CreateDirectoryW(CStringW(output_dir->second.bstrVal) + L"\\log", NULL); PyObject_RAII logfile = PyFile_FromString(const_cast<char *>(static_cast<const char*>(CStringA(output_dir->second.bstrVal) + "\\log\\CyPhyPython.log")), "w"); if (PyErr_Occurred()) { // FIXME where to log this PyErr_Clear(); } else { PyObject_SetAttrString(CyPhyPython, "_logfile", logfile); } } } std::string module_name; auto script_file_it = componentParameters.find(_bstr_t(L"script_file")); if (script_file_it == componentParameters.end()) { std::wstring scriptFilename; scriptFilename = openfilename(L"Python Scripts (*.py)\0*.py\0"); if (scriptFilename.length() == 0) { return; } TCHAR fullpath[MAX_PATH]; TCHAR* filepart; if (!GetFullPathNameW(scriptFilename.c_str(), sizeof(fullpath)/sizeof(fullpath[0]), fullpath, &filepart)) { } else { *(filepart-1) = '\0'; newpath += separator + static_cast<const char*>(CStringA(fullpath)); PySys_SetPath(const_cast<char*>(newpath.c_str())); module_name = static_cast<const char*>(CStringA(filepart)); } } else { if (script_file_it->second.vt != VT_BSTR || SysStringLen(script_file_it->second.bstrVal) == 0) { throw udm_exception("No script_file specified"); } module_name = _bstr_t(script_file_it->second.bstrVal); if (module_name != "" && PathIsRelativeA(module_name.c_str())) { std::replace(module_name.begin(), module_name.end(), '/', '\\'); if (module_name.rfind('\\') != std::string::npos) { std::string path = module_name.substr(0, module_name.rfind('\\')); newpath = workingDir + "\\CyPhyPythonScripts\\" + path + separator + newpath; newpath = workingDir + "\\" + path + separator + newpath; PySys_SetPath(const_cast<char*>(newpath.c_str())); module_name = module_name.substr(module_name.rfind('\\') + 1); } else { newpath = workingDir + "\\CyPhyPythonScripts" + separator + newpath; newpath = workingDir + separator + newpath; PySys_SetPath(const_cast<char*>(newpath.c_str())); } //newpath += separator + fullpath; //PySys_SetPath(const_cast<char*>(newpath.c_str())); } } { PyObject_RAII ret = PyRun_StringFlags("import site\n", Py_file_input, main_namespace, main_namespace, NULL); if (ret == NULL && PyErr_Occurred()) { throw python_error(GetPythonError()); } } if (module_name.rfind(".py") != std::string::npos) { module_name = module_name.substr(0, module_name.rfind(".py")); } if (module_name != "") { PyObject_RAII module = PyImport_ImportModule(module_name.c_str()); if (!module) { throw python_error(GetPythonError()); } // TODO: check if this is necessary PyObject_RAII reloaded_module = PyImport_ReloadModule(module); if (!reloaded_module) { throw python_error(GetPythonError()); } PyObject_RAII invoke = PyObject_GetAttrString(reloaded_module, "invoke"); if (!invoke) { throw python_error("Error: script has no \"invoke\" function"); } if (!PyCallable_Check(invoke)) { throw python_error("Error: script \"invoke\" attribute is not callable"); } PyObject_RAII empty_tuple = PyTuple_New(0); PyObject_RAII args = PyDict_New(); PyDict_SetItemString(args, "focusObject", pyFocusObject); PyDict_SetItemString(args, "rootObject", pyRootObject); PyObject_RAII parameters = PyDict_New(); for (auto it = componentParameters.begin(); it != componentParameters.end(); it++) { if (it->second.vt == VT_BSTR) { PyObject_RAII parameterString = PyUnicode_FromWideChar(it->second.bstrVal, SysStringLen(it->second.bstrVal)); PyDict_SetItemString(parameters, static_cast<const char*>(it->first), parameterString); } } PyDict_SetItemString(args, "componentParameters", parameters); PyObject_RAII ret = PyObject_Call(invoke, empty_tuple, args); if (PyObject_HasAttrString(CyPhyPython, "_logfile")) { PyObject_RAII logfile = PyObject_GetAttrString(CyPhyPython, "_logfile"); if (logfile.p) { PyObject_RAII write = PyObject_GetAttrString(logfile, "close"); PyObject_RAII ret = PyObject_CallObject(write, NULL); ASSERT(ret.p); if (ret == NULL) { throw python_error(GetPythonError()); } } } char* params[] = { "runCommand", "labels", NULL }; for (char** param = params; *param; param++) { PyObject* pyParam = PyDict_GetItemString(parameters, *param); if (pyParam) { if (PyString_Check(pyParam)) { componentParameters[_bstr_t(*param)] = _bstr_t(PyString_AsString(pyParam)); } if (PyUnicode_Check(pyParam)) { componentParameters[_bstr_t(*param)] = _bstr_t(PyUnicode_AsUnicode(pyParam)); } } } } }
int adjustBundle_ssba(vector<CloudPoint> &pointcloud, Mat &camIntrinsic, map<int, vector<KeyPoint> > &imgpts, map<int, Matx34d> &Pmats) { int i, j, k; int N, M, K; N = Pmats.size(); M = pointcloud.size(); K = Count2DMeasurements_ssba(pointcloud); printf("\nadjustBundle_ssba: \n"); printf(" N (cams) = %d, M (points) = %d, K (measurements) = %d\n", N, M, K); /********************************************************************************/ /* Use SSBA-3.0 for sparse bundle adjustment */ /********************************************************************************/ StdDistortionFunction distortion; //conver camera intrinsics to BA datastructs Matrix3x3d KMat; makeIdentityMatrix(KMat); KMat[0][0] = camIntrinsic.at<double>(0,0); //fx KMat[1][1] = camIntrinsic.at<double>(1,1); //fy KMat[0][1] = camIntrinsic.at<double>(0,1); //skew KMat[0][2] = camIntrinsic.at<double>(0,2); //ppx KMat[1][2] = camIntrinsic.at<double>(1,2); //ppy double const f0 = KMat[0][0]; //cout << "intrinsic before bundle = "; displayMatrix(KMat); Matrix3x3d Knorm = KMat; // Normalize the intrinsic to have unit focal length. scaleMatrixIP(1.0/f0, Knorm); Knorm[2][2] = 1.0; //conver 3D point cloud to BA datastructs vector<int> pointIdFwdMap(M); map<int, int> pointIdBwdMap; vector<Vector3d> Xs(M); for (j = 0; j < M; j++) { int pointId = j; Xs[j][0] = pointcloud[j].pt.x; Xs[j][1] = pointcloud[j].pt.y; Xs[j][2] = pointcloud[j].pt.z; pointIdFwdMap[j] = pointId; pointIdBwdMap.insert(make_pair(pointId, j)); } //convert cameras to BA datastructs vector<int> camIdFwdMap(N,-1); map<int, int> camIdBwdMap; vector<CameraMatrix> cams(N); map<int, Matx34d>::iterator itcam; for(i=0,itcam=Pmats.begin(); itcam!=Pmats.end(); i++,itcam++) { int camId = i; Matrix3x3d R; Vector3d T; Matx34d& P = itcam->second; R[0][0] = P(0,0); R[0][1] = P(0,1); R[0][2] = P(0,2); T[0] = P(0,3); R[1][0] = P(1,0); R[1][1] = P(1,1); R[1][2] = P(1,2); T[1] = P(1,3); R[2][0] = P(2,0); R[2][1] = P(2,1); R[2][2] = P(2,2); T[2] = P(2,3); camIdFwdMap[i] = itcam->first; camIdBwdMap.insert(make_pair(itcam->first, camId)); cams[i].setIntrinsic(Knorm); cams[i].setRotation(R); cams[i].setTranslation(T); } //convert 2D measurements to BA datastructs vector<Vector2d > measurements; vector<int> correspondingView; vector<int> correspondingPoint; map<int,int> *m; map<int,int>::iterator itm; measurements.reserve(K); correspondingView.reserve(K); correspondingPoint.reserve(K); for(k = 0; k < pointcloud.size(); k++) { m = &( pointcloud[k].img_kp ); for(itm=m->begin(); itm!=m->end(); itm++) { int vid, pid; Vector3d p; vid = itm->first; pid = itm->second; Point cvp = imgpts[vid][pid].pt; p[0] = cvp.x; p[1] = cvp.y; p[2] = 1.0; // Normalize the measurements to match the unit focal length. scaleVectorIP(1.0/f0, p); measurements.push_back(Vector2d(p[0], p[1])); correspondingView.push_back(camIdBwdMap[vid]); correspondingPoint.push_back(pointIdBwdMap[k]); } } K = measurements.size(); cout << "Read " << K << " valid 2D measurements." << endl; showErrorStatistics_ssba(f0, distortion, cams, Xs, measurements, correspondingView, correspondingPoint); // V3D::optimizerVerbosenessLevel = 1; double const inlierThreshold = 2.0 / fabs(f0); Matrix3x3d K0 = cams[0].getIntrinsic(); //cout << "K0 = "; displayMatrix(K0); bool good_adjustment = false; // perform bundle adjust { ScopedBundleExtrinsicNormalizer extNorm(cams, Xs); ScopedBundleIntrinsicNormalizer intNorm(cams, measurements, correspondingView); CommonInternalsMetricBundleOptimizer opt(V3D::FULL_BUNDLE_METRIC, inlierThreshold, K0, distortion, cams, Xs, measurements, correspondingView, correspondingPoint); //StdMetricBundleOptimizer opt(inlierThreshold, // cams,Xs,measurements, // correspondingView,correspondingPoint); opt.tau = 1e-3; opt.maxIterations = 50; opt.minimize(); cout << "optimizer status = " << opt.status << endl; good_adjustment = (opt.status != 2); } //cout << "refined K = "; displayMatrix(K0); for (int i = 0; i < N; ++i) cams[i].setIntrinsic(K0); Matrix3x3d Knew = K0; scaleMatrixIP(f0, Knew); Knew[2][2] = 1.0; //cout << "Knew = "; displayMatrix(Knew); showErrorStatistics_ssba(f0, distortion, cams, Xs, measurements, correspondingView, correspondingPoint); if( good_adjustment ) { //Vector3d mean(0.0, 0.0, 0.0); //for (unsigned int j = 0; j < Xs.size(); ++j) addVectorsIP(Xs[j], mean); //scaleVectorIP(1.0/Xs.size(), mean); // //vector<float> norms(Xs.size()); //for (unsigned int j = 0; j < Xs.size(); ++j) // norms[j] = distance_L2(Xs[j], mean); // //std::sort(norms.begin(), norms.end()); //float distThr = norms[int(norms.size() * 0.9f)]; //cout << "90% quantile distance: " << distThr << endl; //extract 3D points for (j = 0; j < Xs.size(); j++) { //if (distance_L2(Xs[j], mean) > 3*distThr) makeZeroVector(Xs[j]); pointcloud[j].pt.x = Xs[j][0]; pointcloud[j].pt.y = Xs[j][1]; pointcloud[j].pt.z = Xs[j][2]; } //extract adjusted cameras for (i = 0; i < N; i++) { Matrix3x3d R = cams[i].getRotation(); Vector3d T = cams[i].getTranslation(); Matx34d P; P(0,0) = R[0][0]; P(0,1) = R[0][1]; P(0,2) = R[0][2]; P(0,3) = T[0]; P(1,0) = R[1][0]; P(1,1) = R[1][1]; P(1,2) = R[1][2]; P(1,3) = T[1]; P(2,0) = R[2][0]; P(2,1) = R[2][1]; P(2,2) = R[2][2]; P(2,3) = T[2]; Pmats[camIdFwdMap[i]] = P; } //TODO: extract camera intrinsics /* cam_matrix.at<double>(0,0) = Knew[0][0]; cam_matrix.at<double>(0,1) = Knew[0][1]; cam_matrix.at<double>(0,2) = Knew[0][2]; cam_matrix.at<double>(1,1) = Knew[1][1]; cam_matrix.at<double>(1,2) = Knew[1][2]; */ return 0; } return -1; }
void ClsMultiThread::FindSimilarKmer(map<string, St_KmerIndex>& mpCombine, vector<St_KmerInfo>& vKmerRegular) { //1: Get the number of CPU string strNum = exec("nproc"); if(strNum.substr(strNum.length()-1,1) == "\n") strNum = strNum.substr(0, strNum.length()-1); int iCpuNum = atoi(strNum.c_str()); //2: Divid KmerRegular Table evenly by the number of cpu int iLen = ceil((float)vKmerRegular.size() / iCpuNum); vector<St_SimilarKmer> vSubGroupKmer; vSubGroupKmer.resize(iCpuNum); for(int i=0; i<iCpuNum; i++) { int iStart = i * iLen; int iEnd = (i+1)*iLen; if(iEnd < vKmerRegular.size()) vSubGroupKmer[i].vRegKmer.insert(vSubGroupKmer[i].vRegKmer.end(), vKmerRegular.begin() + iStart, vKmerRegular.begin() + iEnd); else vSubGroupKmer[i].vRegKmer.insert(vSubGroupKmer[i].vRegKmer.end(), vKmerRegular.begin() + iStart, vKmerRegular.end()); vSubGroupKmer[i].mpSuperKmer.insert(mpCombine.begin(), mpCombine.end()); } //Create Threads int start_s = time(NULL); pthread_t tids[iCpuNum]; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for(int i=0; i<iCpuNum; i++) { int ret = pthread_create(&tids[i], &attr, ClsThreadFunc::SimilarDetect, (void*)&(vSubGroupKmer[i])); if(ret != 0) cout << "Thread error: " << ret << endl; } pthread_attr_destroy(&attr); void* status; for(int i=0; i<iCpuNum; i++) { int ret = pthread_join(tids[i], &status); if(ret != 0) cout << "Thread Error: " << ret << endl; else cout << "Thread Status: " << (long)status << endl; } //pthread_exit(NULL); int stop_s = time(NULL); double dRuningTime = difftime(stop_s, start_s); string strTimeFormat = ::GetHMSTimeFormat(dRuningTime); cout << "Running Time: " << strTimeFormat << endl; //Arrange the kmer for(map<string, St_KmerIndex>::iterator itr = mpCombine.begin(); itr != mpCombine.end(); itr++) { int iBase = itr->second.iCount; int iAccumulate = 0; for(vector<St_SimilarKmer>::iterator subItr = vSubGroupKmer.begin(); subItr != vSubGroupKmer.end(); subItr++) { iAccumulate += subItr->mpSuperKmer[itr->first].iCount - iBase; } itr->second.iCount = iBase + iAccumulate; } //好折腾啊!!!!!!!! //2: 将剩下的没能归组的再进行合并,得到总的没有归组的kmer vKmerRegular.clear(); for(vector<St_SimilarKmer>::iterator itr = vSubGroupKmer.begin(); itr != vSubGroupKmer.end(); itr++) { vKmerRegular.insert(vKmerRegular.end(), itr->vRegKmer.begin(), itr->vRegKmer.end()); } }
// infer until fixpoint, which may not be appropriate in the presence of retraction void infer(vector<Rule> &rules) { bool changed = true; map<constint_t, size_t> sizes; map<constint_t, Index>::const_iterator atomit = atoms.begin(); for (; atomit != atoms.end(); ++atomit) { sizes[atomit->first] = atomit->second.size(); } size_t ncycles = 0; while (changed) { cerr << " Cycle " << ++ncycles << "..." << endl; changed = false; #ifndef ANY_ORDER Index assertions (Order(0, 1, 2)); Index retractions (Order(0, 1, 2)); #endif int rulecount = 0; vector<Rule>::iterator rule = rules.begin(); for (; rule != rules.end(); ++rule) { cerr << " Rule " << (rule - rules.begin()) + 1 << "..." << endl; #ifdef ANY_ORDER Index assertions (Order(0, 1, 2)); Index retractions (Order(0, 1, 2)); #endif Relation results; set<varint_t> allvars; query(rule->condition, allvars, results); act(rule->action_block, results, assertions, retractions); #ifndef ANY_ORDER } #endif size_t nretractions = 0; Index::iterator it = retractions.begin(); for (; it != retractions.end(); ++it) { if (idxspo.erase(*it) > 0) { idxpos.erase(*it); idxosp.erase(*it); changed = true; ++nretractions; } } cerr << " " << nretractions << " retractions, "; size_t nassertions = 0; it = assertions.begin(); for (; it != assertions.end(); ++it) { if (idxspo.insert(*it).second) { idxpos.insert(*it); idxosp.insert(*it); changed = true; ++nassertions; } } cerr << nassertions << " assertions." << endl; #ifdef ANY_ORDER } #endif atomit = atoms.begin(); for (; atomit != atoms.end(); ++atomit) { changed = changed || sizes[atomit->first] != atomit->second.size(); sizes[atomit->first] = atomit->second.size(); } }
// 加载训练集数据,在加载评分数据的过程中加载用户和商家数据 void loadTrainingSet(map<string, User> &userMap, map<string, Business> &businessMap, set<Review> &reviewSet, const map<string, Category> &categoryMap) { ifstream trainingReviewFile = ifstream(FolderName + "yelp_training_set/yelp_training_set_review.json"); // 对于training_set_review根据uid和bid进行排序,便于直接生成稀疏矩阵 if (trainingReviewFile.is_open()) { while (!trainingReviewFile.eof()) { string line; getline(trainingReviewFile, line); size_t start = line.find("\"user_id\""); string uid = line.substr(start+12, 22); string bid = line.substr(line.length() - 24, 22); start = line.find("\"stars\"", 124); int stars = atoi(line.substr(start+9, 1).c_str()); start = line.find("\"date\":", 124); int year = atoi(line.substr(start+9, 4).c_str()); int month = atoi(line.substr(start+14, 2).c_str()); int day = atoi(line.substr(start+17, 2).c_str()); int date = year*365+month*30+day; // 判断用户id是否已经出现在userMap中,如果没有则将新出现的用户数据插入到userMap中 // 否则,修改userMap中对应的用户数据(reviewCount和平均分) map<string, User>::iterator userIter = userMap.find(uid); if (userIter == userMap.end()) { userMap.insert(make_pair(uid, User(0, stars, 1, ""))); } else { // 先计算总得打分,最后计算平均分,business类似 ++(userIter->second.reviewCount); userIter->second.starVec.push_back(stars); } // businessMap的更新和userMap类似 map<string, Business>::iterator businessIter = businessMap.find(bid); if (businessIter == businessMap.end()) { businessMap.insert(make_pair(bid, Business(0, stars, 0, 1, ""))); } else { ++(businessIter->second.reviewCount); businessIter->second.starVec.push_back(stars); } reviewSet.insert(Review(uid, bid, stars, date)); } } else { cout << "can't open trainingReviewFile" << endl; } trainingReviewFile.close(); // 根据用户ID的字符串顺序调整用户在矩阵中的位置 // 计算用户打分的平均分以及打分的RMSE int sequence = 0; for (map<string, User>::iterator iter = userMap.begin(); iter != userMap.end(); ++iter) { iter->second.avgStar = calculateVectorAvg(iter->second.starVec); iter->second.RMSE = calculateVectorRMSE(iter->second.starVec, iter->second.avgStar); iter->second.sequence = sequence++; } // 根据商家ID的字符串顺序调整商家在矩阵中的位置 // 计算商家打分的平均分以及打分的RMSE sequence = 0; for (map<string, Business>::iterator iter = businessMap.begin(); iter != businessMap.end(); ++iter) { // 没有review_count小于3的business,等于3的有2531 iter->second.avgStar = calculateVectorAvg(iter->second.starVec); iter->second.RMSE = calculateVectorRMSE(iter->second.starVec, iter->second.avgStar); iter->second.sequence = sequence++; } // load training_set_user to userMap // 用户数据文件中的数据修改上一步得到的用户数据,评分数和平均分以用户文件中的数据为准 ifstream trainingSetUserFile(FolderName + "yelp_training_set/yelp_training_set_user.json"); if (trainingSetUserFile.is_open()) { while (!trainingSetUserFile.eof()) { string line; getline(trainingSetUserFile, line); size_t start, end; start = line.find("\"user_id\":", 48); string uid = line.substr(start+12, 22); start = line.find("\"name\"", 80); end = line.find(",", start); string name = line.substr(start + 9, end - start - 10); start = line.find("\"average_stars\"", 96); end = line.find(",", start+17); float avg_stars = atof(line.substr(start+17, end - start - 17).c_str()); start = line.find("\"review_count\"", end); end = line.find(",", start); int review_count = atoi(line.substr(start+16, end-start-16).c_str()); map<string, User>::iterator userIter = userMap.find(uid); // if (userIter != userMap.end()) 一定为真 // id 为:KQnq1p-PlWUo-qPEtWtmMw 的用户在training_set_user里面的平均分是0,review_count是17; // 从review里计算的平均分是3,review_cnt是1 if (avg_stars > 0) { // TESTK userIter->second.avgStar = avg_stars; userIter->second.reviewCount = review_count; userIter->second.name = name; } } } else { cout << "can't open trainingSetUserFile" << endl; } trainingSetUserFile.close(); // load training_set_business to businessMap int lessBusiCnt = 0; ifstream trainingSetBusinessFile(FolderName + "yelp_training_set/yelp_training_set_business.json"); if (trainingSetBusinessFile.is_open()) { while (!trainingSetBusinessFile.eof()) { vector<string> category; string line; getline(trainingSetBusinessFile, line); stringstream jsonStream(line); ptree pt; read_json(jsonStream, pt); ptree ptCategory = pt.get_child("categories"); float weight = 0; float totalStar = 0; for (ptree::iterator iter = ptCategory.begin(); iter != ptCategory.end(); ++iter) { string cate(iter->second.data()); map<string, Category>::const_iterator cateIter = categoryMap.find(cate); // 部分类别不存在 if (cateIter != categoryMap.end()) { float avgStar = cateIter->second.avgStar; int reviewCnt = cateIter->second.reviewCnt; float RMSE = cateIter->second.RMSE; // TODO: 存在RMSE为零但是review_count比较多的情况 if (RMSE < GlobalRMSE && RMSE != 0) { weight += log10(reviewCnt)/RMSE; totalStar += (avgStar * log10(reviewCnt) / RMSE); } } else { // 打印出不存在的类别信息 // cout << cate << endl; } } if (totalStar == 0) { totalStar = GlobalAvg; } else { totalStar /= weight; } string bid = pt.get<string>("business_id"); float avg_stars = pt.get<float>("stars"); int review_count = pt.get<int>("review_count"); string city = pt.get<string>("city"); map<string, Business>::iterator businessIter = businessMap.find(bid); // if (businessIter != businessMap.end()) 一定为真 // 如果review文件中的review_count小于10而且business文件中的review_count大于review中的review_count, // 则使用business文件中的review_count和avg_star businessIter->second.city = city; businessIter->second.cateAvgStar = totalStar; // 有下面的调整时结果是:1.24577;没有调整的结果是:1.24588 if (businessIter->second.reviewCount < 10 && review_count > businessIter->second.reviewCount*1.3) { businessIter->second.avgStar = avg_stars; businessIter->second.reviewCount = review_count; lessBusiCnt++; } } } else { cout << "can't open trainingSetBusinessFile" << endl; } trainingSetBusinessFile.close(); cout << "less business Count:" << lessBusiCnt << endl; // 根据用户的review_count和avg_star重新计算平均分 float K = 0; int uc1 = 0, uc2 = 0, uc3 = 0, uc4 = 0; for (map<string, User>::iterator iter = userMap.begin(); iter != userMap.end(); ++iter) { if (iter->second.reviewCount == 1) { K = 5; ++uc1; } else if (iter->second.reviewCount == 2) { K = 2.5*(iter->second.RMSE/20 + 1); ++uc2; } else if (iter->second.reviewCount == 3) { K = 1.5*(iter->second.RMSE/30 + 1); ++uc3; } else// if (iter->second.reviewCount == 4) { K = 0; ++uc4; } iter->second.avgStar = (GlobalAvg*K + iter->second.avgStar*iter->second.reviewCount) / (K + iter->second.reviewCount); } cout << uc1 << "\t" << uc2 << "\t" << uc3 << "\t" << uc4 << endl; for (map<string, Business>::iterator iter = businessMap.begin(); iter != businessMap.end(); ++iter) { if (iter->second.reviewCount == 3) { // K = 0.4 1.24580 // K = 0.6 1.24466 K = 0.6; iter->second.avgStar = (GlobalAvg*K + iter->second.avgStar*iter->second.reviewCount) / (K + iter->second.reviewCount); } } }
//********************************************************************************************************************** int CorrAxesCommand::calcKendall(map<string, vector<float> >& axes, ofstream& out) { try { //format data vector< vector<spearmanRank> > scores; scores.resize(numaxes); for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) { vector<float> temp = it->second; for (int i = 0; i < temp.size(); i++) { spearmanRank member(it->first, temp[i]); scores[i].push_back(member); } } //sort each axis for (int i = 0; i < numaxes; i++) { sort(scores[i].begin(), scores[i].end(), compareSpearman); } //convert scores to ranks of xi in each axis for (int i = 0; i < numaxes; i++) { vector<spearmanRank*> ties; int rankTotal = 0; for (int j = 0; j < scores[i].size(); j++) { rankTotal += (j+1); ties.push_back(&(scores[i][j])); if (j != scores[i].size()-1) { // you are not the last so you can look ahead if (scores[i][j].score != scores[i][j+1].score) { // you are done with ties, rank them and continue for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); (*ties[k]).score = thisrank; } ties.clear(); rankTotal = 0; } }else { // you are the last one for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); (*ties[k]).score = thisrank; } } } } //for each otu for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) { if (metadatafile == "") { out << i+1; } else { out << metadataLabels[i]; } //find the ranks of this otu - Y vector<spearmanRank> otuScores; for (int j = 0; j < lookupFloat.size(); j++) { spearmanRank member(lookupFloat[j]->getGroup(), lookupFloat[j]->getAbundance(i)); otuScores.push_back(member); } sort(otuScores.begin(), otuScores.end(), compareSpearman); map<string, float> rankOtus; vector<spearmanRank> ties; int rankTotal = 0; for (int j = 0; j < otuScores.size(); j++) { rankTotal += (j+1); ties.push_back(otuScores[j]); if (j != otuScores.size()-1) { // you are not the last so you can look ahead if (otuScores[j].score != otuScores[j+1].score) { // you are done with ties, rank them and continue for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); rankOtus[ties[k].name] = thisrank; } ties.clear(); rankTotal = 0; } }else { // you are the last one for (int k = 0; k < ties.size(); k++) { float thisrank = rankTotal / (float) ties.size(); rankOtus[ties[k].name] = thisrank; } } } vector<double> pValues(numaxes); //calc spearman ranks for each axis for this otu for (int j = 0; j < numaxes; j++) { int numCoor = 0; int numDisCoor = 0; vector<spearmanRank> otus; vector<spearmanRank> otusTemp; for (int l = 0; l < scores[j].size(); l++) { spearmanRank member(scores[j][l].name, rankOtus[scores[j][l].name]); otus.push_back(member); } int count = 0; for (int l = 0; l < scores[j].size(); l++) { int numWithHigherRank = 0; int numWithLowerRank = 0; float thisrank = otus[l].score; for (int u = l+1; u < scores[j].size(); u++) { if (otus[u].score > thisrank) { numWithHigherRank++; } else if (otus[u].score < thisrank) { numWithLowerRank++; } count++; } numCoor += numWithHigherRank; numDisCoor += numWithLowerRank; } double p = (numCoor - numDisCoor) / (float) count; if (isnan(p) || isinf(p)) { p = 0.0; } out << '\t' << p; pValues[j] = p; //calc signif - zA - http://en.wikipedia.org/wiki/Kendall_tau_rank_correlation_coefficient#Significance_tests double numer = 3.0 * (numCoor - numDisCoor); int n = scores[j].size(); double denom = n * (n-1) * (2*n + 5) / (double) 2.0; denom = sqrt(denom); double sig = numer / denom; if (isnan(sig) || isinf(sig)) { sig = 0.0; } out << '\t' << sig; } double sum = 0; for(int k=0;k<numaxes;k++){ sum += pValues[k] * pValues[k]; } out << '\t' << sqrt(sum) << endl; } return 0; } catch(exception& e) { m->errorOut(e, "CorrAxesCommand", "calcKendall"); exit(1); } }
vector<segment* > load::insert_bedgraph_to_segment_joint(map<string, vector<segment *> > A , string forward, string reverse, int rank ){ map<string, node> NT; typedef map<string, vector<segment *> >::iterator it_type_5; for(it_type_5 c = A.begin(); c != A.end(); c++) { NT[c->first] = node(c->second); } int start, stop, N, j; double coverage; N = 0,j = 0; int strand; int o_st, o_sp; vector<string> lineArray; string chrom, prevchrom, line; vector<segment *> segments; double center; vector<string> FILES(2); FILES[0]=forward, FILES[1]=reverse; string FILE; for (int i =0; i < 2; i++){ if (i==0){ strand=1; }else{ strand=-1; } FILE=FILES[i]; ifstream FH(FILE); if (FH){ prevchrom=""; while (getline(FH, line)){ lineArray = splitter2(line, "\t"); if (lineArray.size()==4){ chrom = lineArray[0]; start=stoi(lineArray[1]),stop=stoi(lineArray[2]), coverage = abs(stod(lineArray[3])); center = (stop + start) /2.; if (NT.find(chrom)!=NT.end()){ vector<double> x(2); x[0]=center, x[1] = coverage; NT[chrom].insert_coverage(x, strand); } } else{ printf("\n***error in line: %s, not bedgraph formatted\n", line.c_str() ); segments.clear(); return segments; } } FH.close(); }else{ cout<<"could not open forward bedgraph file: "<<FILE<<endl; segments.clear(); return segments; } } //now we want to get all the intervals and make a vector<segment *> again... vector<segment *>NS; typedef map<string, node>::iterator it_type_6; for (it_type_6 c = NT.begin(); c!=NT.end(); c++){ c->second.retrieve_nodes(NS); } return NS; }
int test_environment (map<pair<enc_char, enc_char>, int> env_counts, multimap<pair<enc_char, enc_char>, vector<wstring> >& mods, TownWords& first_unigrams, TownBigrams& first_bigrams, TownWords second_unigrams, TownBigrams second_bigrams, enc_char first_char, enc_char second_char) { map<pair<enc_char, enc_char>, int>::iterator it3; int total_changed = 0; float diff = 0; TownWords unigram_counts; TownBigrams bigram_counts; merge_lists (first_unigrams, second_unigrams, unigram_counts); merge_lists (first_bigrams, second_bigrams, bigram_counts); float prev_prob = LM_POWER*find_prob (bigram_counts, orig_uni_cts, orig_bi_cts, unigram_probs, bigram_probs); for (it3=env_counts.begin(); it3 != env_counts.end(); it3++) { TownWords unigram_copy = first_unigrams; TownBigrams bigram_copy = first_bigrams; wcout << decoding[it3->first.first] << "~" << decoding[it3->first.second] << endl; TownWords::iterator it4; TownBigrams::iterator it5; TownWords::iterator it6; vector<wstring> curr_mods; for (it4=first_unigrams.begin(); it4 != first_unigrams.end(); it4++) { Word curr_word = split_word(it4->first); //wcout << it4->first << endl; if (/*second_unigrams.count(it4->first) == 0 &&*/ modify_word(it3->first, curr_word, first_char, second_char) && curr_word.size() > 0) { wstring combined = combine_word (curr_word); curr_mods.push_back (it4->first); //wcout << combined << endl; unigram_copy[combined] += it4->second; unigram_copy.erase (it4->first); for (it5=bigram_copy.begin(); it5 != bigram_copy.end(); it5++) { if (it5->second.count(it4->first) == 1) { it5->second[combined] += it5->second[it4->first]; it5->second.erase (it4->first); } } for (it6=bigram_copy[it4->first].begin(); it6 != bigram_copy[it4->first].end(); it6++) bigram_copy[combined][it6->first] += it6->second; bigram_copy.erase (it4->first); } } unigram_counts.clear(); bigram_counts.clear(); merge_lists (unigram_copy, second_unigrams, unigram_counts); merge_lists (bigram_copy, second_bigrams, bigram_counts); float curr_prob = LM_POWER*find_prob (bigram_counts, orig_uni_cts, orig_bi_cts, unigram_probs, bigram_probs) + CHANGE_BASE; /*int c = 0; TownWords::iterator it; TownBigrams::iterator it2; for (it2=first_bigrams.begin(); it2 != first_bigrams.end(); it2++) { for (it=it2->second.begin(); it != it2->second.end(); it++) c += it->second; } wcout << c << endl; c = 0; for (it2=bigram_copy.begin(); it2 != bigram_copy.end(); it2++) { for (it=it2->second.begin(); it != it2->second.end(); it++) c += it->second; } wcout << c << endl;*/ wcout << prev_prob << endl; wcout << curr_prob << endl; diff = curr_prob - prev_prob; wcout << diff << endl; TownWords* first_replace; wcout << curr_mods.size() << endl; if (diff >= -100) { first_unigrams = unigram_copy; first_bigrams = bigram_copy; mods.insert (pair<pair<enc_char, enc_char>, vector<wstring> > (it3->first, curr_mods)); total_changed += curr_mods.size(); } wcout << total_changed << endl; } return total_changed; }
void load::write_out_models_from_free_mode(map<int, map<int, vector<simple_c_free_mode> > > G, params * P, int job_ID,map<int, string> IDS, int noise, string & file_name){ //======================================================================================== //write out each model parameter estimates double scale = stof(P->p["-ns"]); double penality = stof(P->p["-ms_pen"]) ; string out_dir = P->p["-o"]; ofstream FHW; if (noise==0){ file_name = out_dir+ P->p["-N"] + "-" + to_string(job_ID)+ "_K_models_MLE.tsv"; FHW.open(file_name); }else{ file_name = out_dir+ P->p["-N"] + "-" + to_string(job_ID)+ "_non_div_txn_K_models_MLE.tsv"; FHW.open(file_name); } FHW<<P->get_header(0); typedef map<int, map<int, vector<simple_c_free_mode> > >::iterator it_type_1; typedef map<int, vector<simple_c_free_mode> > ::iterator it_type_2; typedef vector<simple_c_free_mode>::iterator it_type_3; typedef map<int, string>::iterator it_type_IDS; int IN=0; string mus="", sis="", ls="", wEMs="", wPIs="",forward_bs="", forward_ws="",forward_PIs="",reverse_as="", reverse_ws="",reverse_PIs=""; double w_thresh = 0.; double ALPHA_2 = stof(P->p["-ALPHA_2"]); string mu, sigma, lambda, pos, neg, ll, pi, w,ra,fb,rw,fw,fp; int start; string chrom; string INFO = ""; FHW<<"#ID|chromosome:start-stop|forward strand coverage, reverse strand coverage"<<endl; FHW<<"#model complexity,log-likelihood"<<endl; FHW<<"#mu_k\tsigma_k\tlambda_k\tpi_k\tfp_k\tw_[p,k],w_[f,k],w_[r,k]\tb_[f,k]\ta_[r,k]"<<endl; for (it_type_1 s = G.begin(); s!=G.end(); s++){ //iterate over each segment FHW<<">" + IDS[s->first]+ "|"; for (it_type_2 k = s->second.begin(); k != s->second.end(); k++){//iterate over each model_complexity for (it_type_3 c = k->second.begin(); c!=k->second.end(); c++){ chrom = (*c).chrom; INFO = chrom + ":" + to_string((*c).ID[1])+"-"+to_string((*c).ID[2]); pos = to_string((*c).SS[1]); neg = to_string((*c).SS[2]); } } FHW<<INFO<<"|"<<pos+","+neg<<endl; for (it_type_2 k = s->second.begin(); k != s->second.end(); k++){//iterate over each model_complexity string mus = "", sigmas="", lambdas="",pis="", ws= "" ,fbs="",ras="", fws="", rws="", fps=""; string pos = "", neg=""; string k_header = "~"+ to_string(k->first)+","; int NN = k->second.size(); int ii = 0; for (it_type_3 c = k->second.begin(); c!=k->second.end(); c++){ chrom = (*c).chrom; start = (*c).ID[1]; mu = to_string((*c).ps[0]*scale + (*c).ID[1] ); sigma = to_string((*c).ps[1]*scale); lambda = to_string(scale/(*c).ps[2]); pi = to_string( (*c).ps[4]); w = to_string( (*c).ps[3]); fw = to_string( (*c).ps[6]); rw = to_string( (*c).ps[9]); ra = to_string( scale*(*c).ps[8] + (*c).ID[1] ); fb = to_string( scale*(*c).ps[5] + (*c).ID[1]); fp = to_string( scale*(*c).ps[11] ); ll = to_string((*c).SS[0]); if (ii +1 < NN){ mus+=mu+","; sigmas+=sigma+","; lambdas+=lambda+","; pis+=pi+","; ws+=w+ "," + fw+ "," + rw + "|"; ras+=ra+","; fbs+=fb+","; fps+=fp+","; }else{ mus+=mu ; sigmas+=sigma ; lambdas+=lambda ; pis+=pi ; ws+=w+ "," + fw+ "," + rw ; fbs+=fb; ras+=ra; fps+=fp ; } ii++; } k_header +=ll+ "\t"; FHW<<k_header; if (k->first>0){ FHW<<mus+"\t"+sigmas+"\t"+lambdas+"\t" + pis+"\t" + fps+ "\t" + ws + "\t" + fbs+"\t" +ras ; } FHW<<endl; } } FHW.flush(); }
int main() { long long A, B; int index=1; cin>>A>>B; while(A*B>1) { int space=0; int dist=0; for(int i=2;i<=sqrt(A);i++) { int cnt1=0; while((A%i==0)&&A>1) { cnt1++; A=A/i; } if(cnt1!=0) { mymap[i]=cnt1; } } if(A>1) { mymap[A]=1; } for(int i=2;i<=sqrt(B);i++) { int cnt2=0; while((B%i==0)&&B>1) { cnt2++; B=B/i; } if(cnt2!=0) { if(mymap.count(i)){ mymap[i]=absl(mymap[i],cnt2); } else { mymap[i]=cnt2; } } } if(B>1) { if(mymap.count(B)) { mymap[B]=mymap[B]-1; } else { mymap[B]=1; } } for(it=mymap.begin();it!=mymap.end();++it) { dist=dist+it->second; space++; } cout<<index++<<". "<<space<<":"<<dist<<endl; mymap.clear(); cin>>A>>B; } }
void __stdcall GFGoodSell(struct SGFGoodSellInfo const &gsi, unsigned int iClientID) { returncode = DEFAULT_RETURNCODE; const GoodInfo *packageInfo = GoodList::find_by_id(gsi.iArchID); // check for equipments only if (packageInfo->iType == 1) { uint iBase; pub::Player::GetBase(iClientID, iBase); //in this case, it's more efficent to check if it's an item under watch first. for (map<uint, string>::iterator iter = mapACItems.begin(); iter!=mapACItems.end(); iter++) { if (iter->first == gsi.iArchID) { //PrintUserCmdText(iClientID, L"I have found commodity %s.", stows(iter->second).c_str()); //We iterate through the base names to see if it's a non-POB base list<uint>::iterator i = mapACBases.begin(); while (i != mapACBases.end()) { if (*i == iBase) { wstring wscCharname = (const wchar_t*) Players.GetActiveCharacterName(iClientID); wstring wscBaseName = HkGetBaseNickByID(iBase); //PrintUserCmdText(iClientID, L"I have found this base, logging the purchase."); // check if this item is already under watch, if so increase amount by 1 if (mapACSales[iClientID].items.find(gsi.iArchID) != mapACSales[iClientID].items.end()) { ++mapACSales[iClientID].items.find(gsi.iArchID)->second; //PrintUserCmdText(iClientID, L"DEBUG: I have logged %i sales.", mapACSales[iClientID].items.find(gsi.iArchID)->second); wstring wscMsgLog = L"<%sender> has sold <%item> to base <%basename> (Already recorded %isale sales of this item)"; wscMsgLog = ReplaceStr(wscMsgLog, L"%sender", wscCharname.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%basename", wscBaseName.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%item", stows(iter->second).c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%isale", stows(itos(mapACSales[iClientID].items.find(gsi.iArchID)->second))).c_str(); string scText = wstos(wscMsgLog); Logging("%s", scText.c_str()); } else { mapACSales[iClientID].items[gsi.iArchID] = 1; wstring wscMsgLog = L"<%sender> has sold <%item> to base <%basename> (First sale)"; wscMsgLog = ReplaceStr(wscMsgLog, L"%sender", wscCharname.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%basename", wscBaseName.c_str()); wscMsgLog = ReplaceStr(wscMsgLog, L"%item", stows(iter->second).c_str()); string scText = wstos(wscMsgLog); Logging("%s", scText.c_str()); } break; } i++; } break; } } } }
void FIRST(){ bool flag=true; while(flag){ flag=false; for(map<char,vector<string> >::iterator it=CFG.begin();it!=CFG.end();it++){ char V=it->first; string s; for(int i=0;i< it->second.size();i++){ s=it->second[i]; //null production if(s=="@"){ if(first[V].find('@') == first[V].end()){ first[V].insert('@'); flag=true; } continue; } int j=0; bool epsilon; while(j<s.size()){ if(nonTerminal[s[j]]){ //if non terminal epsilon=false; set<char>::iterator it1=first[s[j]].begin(); while(it1 != first[s[j]].end()){ //has null production if(*it1=='@'){ epsilon=true; it1++; continue; } if(first[V].find(*it1) == first[V].end()){ flag=true; first[V].insert(*it1); } it1++; } if(epsilon) j++; else break; } else{ //if s[j] terminal then insert s[j] if(first[V].find(s[j]) == first[V].end()){ first[V].insert(s[j]); flag=true; } epsilon=false; break; } } if(epsilon){ if(first[V].find('@') == first[V].end()){ first[V].insert('@'); flag=true; } } } } } cout<<endl<<"First set"<<endl; for(map<char,set<char> >::iterator it=first.begin();it!=first.end();it++){ cout<<it->first<<" = "; for(set<char>::iterator it2=it->second.begin();it2!=it->second.end();it2++) cout<<*it2<<" "; cout<<endl; } }
int main(int argc, char **argv) { int ret = 0; if ((ret = parse_parameters(argc, argv)) <= 0) { return ret; } #ifdef WIN32 WSADATA wsaData; int err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData ); if ( err != 0 ) { fprintf(stderr, "couldn't find a usable winsock.dll.\n" ); return -1; } #else signal(SIGPIPE, SIG_IGN); #endif signal(SIGTERM, exit_handler); signal(SIGINT, exit_handler); evutil_socket_t listen_fd; if (init_listen_socket(listen_fd) < 0) { #ifdef WIN32 WSACleanup(); #endif return -1; } printf("listening on port %d.\n", g_port); #ifndef WIN32 int i = 0; for(i = 0; i < g_process_number; i++) { pid_t pid = fork(); if (pid == 0) { break; } else if (pid < 0) { fprintf(stderr, "fork failed!, errno:%d, info:%s\n", errno, get_error_string(errno)); } else { g_child_pid[i] = pid; printf("fork child process. pid:%u\n", pid); } } if (g_process_number != 0 && i == g_process_number) { for(int i = 0; i < g_process_number; i++) { if (g_child_pid[i] != 0) { waitpid(g_child_pid[i], NULL, 0); } } EVUTIL_CLOSESOCKET(listen_fd); return 0; } #endif if (fill_send_buffer() < 0) { EVUTIL_CLOSESOCKET(listen_fd); #ifdef WIN32 WSACleanup(); #endif return -1; } /* Initialize libevent. */ g_evbase = event_init(); printf("event method:%s.\n", event_base_get_method(g_evbase)); struct event *ev_accept = new event; /* We now have a listening socket, we create a read event to * be notified when a client connects. */ event_set(ev_accept, listen_fd, EV_READ | EV_PERSIST, on_accept, NULL); event_add(ev_accept, NULL); if (g_limit_flow > 0.0) { struct event timeout; struct timeval tv; int flags = 0; /* Initalize one event */ event_assign(&timeout, g_evbase, -1, flags, timeout_cb, (void*) &timeout); //evtimer_set(&timeout, timeout_cb, &timeout); evutil_timerclear(&tv); tv.tv_sec = 0; tv.tv_usec = 10000; event_add(&timeout, &tv); evutil_gettimeofday(&g_last_time, NULL); } if (g_http_port > 0 && g_process_number == 0) { const char *http_addr = "0.0.0.0"; struct evhttp *http_server = NULL; http_server = evhttp_start(http_addr, g_http_port); if (http_server == NULL) { fprintf(stderr, "Http server cann't start!\n"); } else { evhttp_set_gencb(http_server, generic_request_handler, NULL); } struct event timeout; struct timeval tv; int flags = 0; /* Initalize one event */ event_assign(&timeout, g_evbase, -1, flags, timeout_show_cb, (void*) &timeout); //evtimer_set(&timeout, timeout_cb, &timeout); evutil_timerclear(&tv); tv.tv_sec = 1; event_add(&timeout, &tv); evutil_gettimeofday(&g_last_show_time, NULL); } /* Start the libevent event loop. */ event_dispatch(); delete []g_send_buf; while (!g_conns.empty()) { map <int, server_conn *>::iterator it = g_conns.begin(); it->second->set_error_no(NL_CONN_ERROR_SEVER_STOP); close_client(it->first); } EVUTIL_CLOSESOCKET(listen_fd); #ifdef WIN32 WSACleanup(); #endif return 0; }
void FOLLOW(){ follow[start].insert('$'); bool flag=true; while(flag){ flag=false; for(map<char,vector<string> >::iterator it=CFG.begin();it!=CFG.end();it++){ char V=it->first; string s; for(int i=0;i<it->second.size();i++){ s=it->second[i]; if(s=="@") continue; int j=0; while(j<s.size()){ if(nonTerminal[s[j]]){ //s[j] is non terminal bool epsilon=false; if(j+1<s.size()){ //s[j+1] is terminal follow of s[j] is not calculated or if calculated s[j+1] is not in it if(!nonTerminal[s[j+1]] && (follow.find(s[j]) == follow.end() || follow[s[j]].find(s[j+1]) == follow[s[j]].end())){ flag=true; follow[s[j]].insert(s[j+1]); j++; continue; } //s[j+1] is non terminal for(set<char>::iterator it2=first[s[j+1]].begin();it2!=first[s[j+1]].end();it2++){ // s[j+1] ka first set s[j] mei add karna hai except epsilon if(*it2=='@'){ epsilon=true; continue; } if(follow.find(s[j]) == follow.end() || follow[s[j]].find(*it2) == follow[s[j]].end()){ flag=true; follow[s[j]].insert(*it2); } } } if(j == s.length()-1 || epsilon){ for(set<char>::iterator it3=follow[V].begin();it3!=follow[V].end();it3++){ //s[j] ka calculate nahi kiya hai or agar kiya hai toh *it3 nahi hai if(follow.find(s[j]) == follow.end() || follow[s[j]].find(*it3) == follow[s[j]].end()){ flag=true; follow[s[j]].insert(*it3); } } } j++; } else{ j++; continue; } } } } } cout<<endl<<"Follow set"<<endl; for(map<char,set<char> >::iterator it=follow.begin();it!=follow.end();it++){ cout<<it->first<<" = "; for(set<char>::iterator it2=it->second.begin();it2!=it->second.end();it2++) cout<<*it2<<" "; cout<<endl; } }
bool CAlert::ProcessAlert() { if (!CheckSignature()) return false; if (!IsInEffect()) return false; // alert.nID=max is reserved for if the alert key is // compromised. It must have a pre-defined message, // must never expire, must apply to all versions, // and must cancel all previous // alerts or it will be ignored (so an attacker can't // send an "everything is OK, don't panic" version that // cannot be overridden): int maxInt = std::numeric_limits<int>::max(); if (nID == maxInt) { if (!( nExpiration == maxInt && nCancel == (maxInt-1) && nMinVer == 0 && nMaxVer == maxInt && setSubVer.empty() && nPriority == maxInt && strStatusBar == "URGENT: Alert key compromised, upgrade required" )) return false; } { LOCK(cs_mapAlerts); // Cancel previous alerts for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();) { const CAlert& alert = (*mi).second; if (Cancels(alert)) { printf("cancelling alert %d\n", alert.nID); uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED); mapAlerts.erase(mi++); } else if (!alert.IsInEffect()) { printf("expiring alert %d\n", alert.nID); uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED); mapAlerts.erase(mi++); } else mi++; } // Check if this alert has been cancelled BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) { const CAlert& alert = item.second; if (alert.Cancels(*this)) { printf("alert already cancelled by %d\n", alert.nID); return false; } } // Add to mapAlerts mapAlerts.insert(make_pair(GetHash(), *this)); // Notify UI if it applies to me if(AppliesToMe()) uiInterface.NotifyAlertChanged(GetHash(), CT_NEW); } printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe()); return true; }
int main() { #ifdef HOME freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int i, j, k; //scanf("%d", &ntest); for(itest = 1; itest <= ntest; ++itest) { scanf("%d", &n); for(i = 0; i < n; ++i) { scanf("%s", s[i]); } int np = 0; for(i = 0; i <= n; ++i) { for(j = i; j <= n; ++j) { if(__gcd(i, j) == 1) { //printf("%d %d\n", i, j); p[np].fi = i; p[np].se = j; ++np; } } } for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) { if(s[i][j] != '.') { for(k = 0; k < np; ++k) { int a = p[k].fi; int b = p[k].se; int c = -(a * i + b * j); mp[Line(a, b, -(a * i + b * j))]++; if(a != 0 && b != 0) mp[Line(a, -b, -(a * i + -b * j))]++; if(b != a) { mp[Line(b, a, -(b * i + a * j))]++; if(a != 0) mp[Line(b, -a, -(b * i + -a * j))]++; } } } } } ll res = 0; for(map<Line, int>::iterator it = mp.begin(); it != mp.end(); ++it) { k = (*it).se; if(k >= 3) { //(*it).fi.display(); //printf("k = %d\n", k); res += 1LL * k * (k-2) * (k-1) / 6; } } printf("%lld\n", res); } return 0; }
bool CAlert::ProcessAlert(bool fThread) { if (!CheckSignature()) return false; if (!IsInEffect()) return false; // alert.nID=max is reserved for if the alert key is // compromised. It must have a pre-defined message, // must never expire, must apply to all versions, // and must cancel all previous // alerts or it will be ignored (so an attacker can't // send an "everything is OK, don't panic" version that // cannot be overridden): int maxInt = std::numeric_limits<int>::max(); if (nID == maxInt) { if (!( nExpiration == maxInt && nCancel == (maxInt-1) && nMinVer == 0 && nMaxVer == maxInt && setSubVer.empty() && nPriority == maxInt && strStatusBar == "URGENT: Alert key compromised, upgrade required" )) return false; } { LOCK(cs_mapAlerts); // Cancel previous alerts for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();) { const CAlert& alert = (*mi).second; if (Cancels(alert)) { printf("cancelling alert %d\n", alert.nID); uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED); mapAlerts.erase(mi++); } else if (!alert.IsInEffect()) { printf("expiring alert %d\n", alert.nID); uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED); mapAlerts.erase(mi++); } else mi++; } // Check if this alert has been cancelled BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) { const CAlert& alert = item.second; if (alert.Cancels(*this)) { printf("alert already cancelled by %d\n", alert.nID); return false; } } // Add to mapAlerts mapAlerts.insert(make_pair(GetHash(), *this)); // Notify UI and -alertnotify if it applies to me if(AppliesToMe()) { uiInterface.NotifyAlertChanged(GetHash(), CT_NEW); std::string strCmd = GetArg("-alertnotify", ""); if (!strCmd.empty()) { // Alert text should be plain ascii coming from a trusted source, but to // be safe we first strip anything not in safeChars, then add single quotes around // the whole string before passing it to the shell: std::string singleQuote("'"); // safeChars chosen to allow simple messages/URLs/email addresses, but avoid anything // even possibly remotely dangerous like & or > std::string safeChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@"); std::string safeStatus; for (std::string::size_type i = 0; i < strStatusBar.size(); i++) { if (safeChars.find(strStatusBar[i]) != std::string::npos) safeStatus.push_back(strStatusBar[i]); } safeStatus = singleQuote+safeStatus+singleQuote; boost::replace_all(strCmd, "%s", safeStatus); if (fThread) boost::thread t(runCommand, strCmd); // thread runs free else runCommand(strCmd); } } } printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe()); return true; }
double AmovaCommand::runAMOVA(ofstream& AMOVAFile, map<string, vector<int> > groupSampleMap, double alpha) { try { map<string, vector<int> >::iterator it; int numGroups = groupSampleMap.size(); int totalNumSamples = 0; for(it = groupSampleMap.begin();it!=groupSampleMap.end();it++){ totalNumSamples += it->second.size(); } double ssTotalOrig = calcSSTotal(groupSampleMap); double ssWithinOrig = calcSSWithin(groupSampleMap); double ssAmongOrig = ssTotalOrig - ssWithinOrig; double counter = 0; for(int i=0;i<iters;i++){ map<string, vector<int> > randomizedGroup = getRandomizedGroups(groupSampleMap); double ssWithinRand = calcSSWithin(randomizedGroup); if(ssWithinRand < ssWithinOrig){ counter++; } } double pValue = (double)counter / (double) iters; string pString = ""; if(pValue < 1/(double)iters){ pString = '<' + toString(1/(double)iters); } else { pString = toString(pValue); } //print anova table it = groupSampleMap.begin(); AMOVAFile << it->first; m->mothurOut(it->first); it++; for(it;it!=groupSampleMap.end();it++){ AMOVAFile << '-' << it->first; m->mothurOut('-' + it->first); } AMOVAFile << "\tAmong\tWithin\tTotal" << endl; m->mothurOut("\tAmong\tWithin\tTotal\n"); AMOVAFile << "SS\t" << ssAmongOrig << '\t' << ssWithinOrig << '\t' << ssTotalOrig << endl; m->mothurOut("SS\t" + toString(ssAmongOrig) + '\t' + toString(ssWithinOrig) + '\t' + toString(ssTotalOrig) + '\n'); int dfAmong = numGroups - 1; double MSAmong = ssAmongOrig / (double) dfAmong; int dfWithin = totalNumSamples - numGroups; double MSWithin = ssWithinOrig / (double) dfWithin; int dfTotal = totalNumSamples - 1; double Fs = MSAmong / MSWithin; AMOVAFile << "df\t" << dfAmong << '\t' << dfWithin << '\t' << dfTotal << endl; m->mothurOut("df\t" + toString(dfAmong) + '\t' + toString(dfWithin) + '\t' + toString(dfTotal) + '\n'); AMOVAFile << "MS\t" << MSAmong << '\t' << MSWithin << endl << endl; m->mothurOut("MS\t" + toString(MSAmong) + '\t' + toString(MSWithin) + "\n\n"); AMOVAFile << "Fs:\t" << Fs << endl; m->mothurOut("Fs:\t" + toString(Fs) + '\n'); AMOVAFile << "p-value: " << pString; m->mothurOut("p-value: " + pString); if(pValue < alpha){ AMOVAFile << "*"; m->mothurOut("*"); } AMOVAFile << endl << endl; m->mothurOutEndLine();m->mothurOutEndLine(); return pValue; } catch(exception& e) { m->errorOut(e, "AmovaCommand", "runAMOVA"); exit(1); } }
LabelUpdateThread::LabelUpdateThread(const set<string> &labelsToDelete, const map<string, string> &labelsToRename) { copy(labelsToDelete.begin(), labelsToDelete.end(), inserter(m_labelsToDelete, m_labelsToDelete.begin())); copy(labelsToRename.begin(), labelsToRename.end(), inserter(m_labelsToRename, m_labelsToRename.begin())); }
//! 保存个人房屋内的物品信息 bool CRsDupRgn::SaveDupRgnObj(const CGUID& ownerID, map<CGUID,CDBRgnGoods*>& goodsGroup, _ConnectionPtr& cn) { if(cn == NULL) { AddLogText(CStringReading::LoadString(IDS_DBS_RSDUPRGN,STR_DBS_DUPRGN_CNPTRNULL)); return false; } _RecordsetPtr rs; // 比物品个数多分配2048字节 char *sql = new char[1024 * (goodsGroup.size()+2)]; string iniName = "phrgnobj"; string tableName = "CSL_DUPRGN_GOODS"; try { TESTHR(CreateRs(rs)); char szGUID[128]; ownerID.tostring(szGUID); sprintf(sql, "DELETE FROM CSL_DUPRGN_GOODS WHERE DupRgnGUID='%s'", szGUID); // 先删除该个人房屋的成员 if(!ExecuteCn(sql, cn)) { PrintErr(CStringReading::LoadString(IDS_DBS_RSDUPRGN,STR_DBS_DUPRGN_DELRGNOBJFAIL)); return false; } // 清空sql语句 memset(sql, 0, 1024 * (goodsGroup.size()+2)); // 生成语句头 strcpy(sql, "INSERT INTO CSL_DUPRGN_GOODS("); CDataEntityManager* pObjAttrDef = NULL; std::map<string, CDataEntityManager*>::iterator itr = GetGame()->GetDBEntityManager()->GetObjAttrDef().find(iniName); if(itr != GetGame()->GetDBEntityManager()->GetObjAttrDef().end()) pObjAttrDef = itr->second; if(!pObjAttrDef) return false; //! 生成语句身 CDataEntityManager::EntityPropertyMapItr attrItr = pObjAttrDef->GetEntityPropertyMap().begin(); for (; attrItr != pObjAttrDef->GetEntityPropertyMap().end(); attrItr++) { if(attrItr->second->GetDBTableName() == tableName) { CWrapDataObject* wdo = attrItr->second->GetDataObject(0); if(wdo) { // 添加属性字段名 strcat(sql, attrItr->second->GetEPName().c_str()); strcat(sql, ","); } } } long sqlLen = strlen(sql); sql[sqlLen-1] = '\0'; strcat(sql, ") select "); map<CGUID,CDBRgnGoods*>::iterator goodsItr = goodsGroup.begin(); for(; goodsItr != goodsGroup.end(); goodsItr++) { //! 生成语句身 attrItr = pObjAttrDef->GetEntityPropertyMap().begin(); for (; attrItr != pObjAttrDef->GetEntityPropertyMap().end(); attrItr++) { if(attrItr->second) { if(attrItr->second->GetDBTableName() == tableName) { // 添加属性字段值 CEntityProperty* ep = (goodsItr->second)->GetDataEntityManager().GetEntityProperty(attrItr->second->GetEPName()); if(ep) { if(ep->GetDBTableName() == tableName) { switch(ep->GetDataType()) { case DATA_OBJECT_TIME: { char szTimeValue[2048]; DWORD timeValue[6] = {0}; ep->GetBufAttr(0, (void*)&timeValue[0], sizeof(DWORD)*6); sprintf(szTimeValue, "%d-%d-%d %d:%d:%d", timeValue[0], timeValue[1], timeValue[2], timeValue[3], timeValue[4], timeValue[5]); //rs->PutCollect((*epItr).c_str(), szTimeValue); strcat(sql, "'"); strcat(sql, szTimeValue); strcat(sql, "',"); } break; case DATA_OBJECT_STRING: { //rs->PutCollect((*epItr).c_str(), ); strcat(sql, "'"); const char* pStr = ep->GetStringAttr(0); if(pStr) strcat(sql, pStr); else strcat(sql, ""); strcat(sql, "',"); } break; case DATA_OBJECT_BUFFER: { //SaveBufferField((*goodsItr), (*epItr).c_str(), rs); } break; case DATA_OBJECT_GUID: { CGUID tGUID; ep->GetGuidAttr(0, tGUID); char szGuid[128]; tGUID.tostring(szGuid); //rs->PutCollect((*epItr).c_str(), szGuid); strcat(sql, "'"); strcat(sql, szGuid); strcat(sql, "',"); } break; case DATA_OBJECT_BOOL: case DATA_OBJECT_CHAR: case DATA_OBJECT_BYTE: { //rs->PutCollect((*epItr).c_str(), (BYTE)ep->GetLongAttr(0)); char value[32]; memset(value, 0, sizeof(value)); itoa((BYTE)ep->GetLongAttr(0), value, 10); strcat(sql, value); strcat(sql, ","); } break; case DATA_OBJECT_SHORT: case DATA_OBJECT_WORD: case DATA_OBJECT_USHORT: { //rs->PutCollect((*epItr).c_str(), (WORD)ep->GetLongAttr(0)); char value[32]; memset(value, 0, sizeof(value)); itoa((WORD)ep->GetLongAttr(0), value, 10); strcat(sql, value); strcat(sql, ","); } break; case DATA_OBJECT_FLOAT: case DATA_OBJECT_LONG: case DATA_OBJECT_ULONG: case DATA_OBJECT_DWORD: { //rs->PutCollect((*epItr).c_str(), (DWORD)ep->GetLongAttr(0)); char value[32]; memset(value, 0, sizeof(value)); itoa((DWORD)ep->GetLongAttr(0), value, 10); strcat(sql, value); strcat(sql, ","); } break; } } } } } } sqlLen = strlen(sql); sql[sqlLen-1] = '\0'; strcat(sql, " union all select "); } sqlLen = strlen(sql); sql[sqlLen-17] = '\0'; TESTHR(ExecuteCn(sql, cn)); SAFE_DELETE_ARRAY(sql); return true; } catch (_com_error &e) { SAFE_DELETE_ARRAY(sql); ReleaseRs(rs); PrintErr(CStringReading::LoadString(IDS_DBS_RSDUPRGN,STR_DBS_DUPRGN_SAVERGNGOODSFAIL), e); return false; } SAFE_DELETE_ARRAY(sql); return false; }
bool VBufStorage_buffer_t::replaceSubtrees(map<VBufStorage_fieldNode_t*,VBufStorage_buffer_t*>& m) { VBufStorage_controlFieldNode_t* parent=NULL; VBufStorage_fieldNode_t* previous=NULL; //Using the current selection start, record a list of ancestor fields by their identifier, //and a relative offset of the selection start to those fields, so that the selection can be corrected after the replacement. list<pair<VBufStorage_controlFieldNodeIdentifier_t,int>> identifierList; if(this->getTextLength()>0) { int controlDocHandle, controlID, controlNodeStart, controlNodeEnd; parent=this->locateControlFieldNodeAtOffset(this->selectionStart,&controlNodeStart,&controlNodeEnd,&controlDocHandle,&controlID); int relativeSelectionStart=this->selectionStart-controlNodeStart; for(;parent!=NULL;parent=parent->parent) { identifierList.push_front(pair<VBufStorage_controlFieldNodeIdentifier_t,int>(parent->identifier,relativeSelectionStart)); for(previous=parent->previous;previous!=NULL;relativeSelectionStart+=previous->length,previous=previous->previous); } } //For each node in the map, //Replace the node on this buffer, with the content of the buffer in the map for that node //Note that controlField info will automatically be removed, but not added again bool failedBuffers=false; for(map<VBufStorage_fieldNode_t*,VBufStorage_buffer_t*>::iterator i=m.begin();i!=m.end();) { VBufStorage_fieldNode_t* node=i->first; VBufStorage_buffer_t* buffer=i->second; if(buffer==this) { LOG_DEBUGWARNING(L"Cannot replace a subtree on a buffer with the same buffer. Skipping"); failedBuffers=true; m.erase(i++); continue; } parent=node->parent; previous=node->previous; if(!this->removeFieldNode(node)) { LOG_DEBUGWARNING(L"Error removing node. Skipping"); failedBuffers=true; buffer->clearBuffer(); delete buffer; m.erase(i++); continue; } if(!this->insertNode(parent,previous,buffer->rootNode)) { LOG_DEBUGWARNING(L"Error inserting node. Skipping"); failedBuffers=true; buffer->clearBuffer(); delete buffer; m.erase(i++); continue; } buffer->nodes.erase(buffer->rootNode); this->nodes.insert(buffer->nodes.begin(),buffer->nodes.end()); buffer->nodes.clear(); buffer->rootNode=NULL; ++i; } //Update the controlField info on this buffer using all the buffers in the map //We do this all in one go instead of for each replacement in case there are issues with ordering //e.g. an identifier appears in one place before its removed in another for(map<VBufStorage_fieldNode_t*,VBufStorage_buffer_t*>::iterator i=m.begin();i!=m.end();++i) { VBufStorage_buffer_t* buffer=i->second; int failedIDs=0; for(map<VBufStorage_controlFieldNodeIdentifier_t,VBufStorage_controlFieldNode_t*>::iterator j=buffer->controlFieldNodesByIdentifier.begin();j!=buffer->controlFieldNodesByIdentifier.end();++j) { map<VBufStorage_controlFieldNodeIdentifier_t,VBufStorage_controlFieldNode_t*>::iterator existing=this->controlFieldNodesByIdentifier.find(j->first); if(existing!=this->controlFieldNodesByIdentifier.end()) { ++failedIDs; if(!removeFieldNode(existing->second,false)) { LOG_DEBUGWARNING(L"Error removing old node to make when handling ID clash"); continue; } nhAssert(this->controlFieldNodesByIdentifier.count(j->first)==0); } this->controlFieldNodesByIdentifier.insert(make_pair(j->first,j->second)); } buffer->controlFieldNodesByIdentifier.clear(); delete buffer; if(failedIDs>0) { LOG_DEBUGWARNING(L"Duplicate IDs when replacing subtree. Duplicate count "<<failedIDs); failedBuffers=true; } } m.clear(); //Find the deepest field the selection started in that still exists, //and correct the selection so its still positioned accurately relative to that field. if(!identifierList.empty()) { VBufStorage_controlFieldNode_t* lastAncestorNode=NULL; int lastRelativeSelectionStart=0; for(list<pair<VBufStorage_controlFieldNodeIdentifier_t,int> >::iterator i=identifierList.begin();i!=identifierList.end();++i) { VBufStorage_controlFieldNode_t* currentAncestorNode=this->getControlFieldNodeWithIdentifier(i->first.docHandle,i->first.ID); if(currentAncestorNode==NULL) break; if(currentAncestorNode->parent!=lastAncestorNode) break; lastAncestorNode=currentAncestorNode; lastRelativeSelectionStart=i->second; } if(lastAncestorNode!=NULL) { int lastAncestorStartOffset, lastAncestorEndOffset; if(!this->getFieldNodeOffsets(lastAncestorNode,&lastAncestorStartOffset,&lastAncestorEndOffset)) { LOG_DEBUGWARNING(L"Error getting offsets for last ancestor node. Returnning false"); return false; } this->selectionStart=lastAncestorStartOffset+min(lastRelativeSelectionStart,max(lastAncestorNode->length-1,0)); } } return !failedBuffers; }
//********************************************************************************************************************** int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& out) { try { //find average of each axis - X vector<float> averageAxes; averageAxes.resize(numaxes, 0.0); for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) { vector<float> temp = it->second; for (int i = 0; i < temp.size(); i++) { averageAxes[i] += temp[i]; } } for (int i = 0; i < averageAxes.size(); i++) { averageAxes[i] = averageAxes[i] / (float) axes.size(); } //for each otu for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) { if (metadatafile == "") { out << i+1; } else { out << metadataLabels[i]; } //find the averages this otu - Y float sumOtu = 0.0; for (int j = 0; j < lookupFloat.size(); j++) { sumOtu += lookupFloat[j]->getAbundance(i); } float Ybar = sumOtu / (float) lookupFloat.size(); vector<float> rValues(averageAxes.size()); //find r value for each axis for (int k = 0; k < averageAxes.size(); k++) { double r = 0.0; double numerator = 0.0; double denomTerm1 = 0.0; double denomTerm2 = 0.0; for (int j = 0; j < lookupFloat.size(); j++) { float Yi = lookupFloat[j]->getAbundance(i); float Xi = axes[lookupFloat[j]->getGroup()][k]; numerator += ((Xi - averageAxes[k]) * (Yi - Ybar)); denomTerm1 += ((Xi - averageAxes[k]) * (Xi - averageAxes[k])); denomTerm2 += ((Yi - Ybar) * (Yi - Ybar)); } double denom = (sqrt(denomTerm1) * sqrt(denomTerm2)); r = numerator / denom; if (isnan(r) || isinf(r)) { r = 0.0; } rValues[k] = r; out << '\t' << r; //signifigance calc - http://faculty.vassar.edu/lowry/ch4apx.html double temp = (1- (r*r)) / (double) (lookupFloat.size()-2); temp = sqrt(temp); double sig = r / temp; if (isnan(sig) || isinf(sig)) { sig = 0.0; } out << '\t' << sig; } double sum = 0; for(int k=0;k<rValues.size();k++){ sum += rValues[k] * rValues[k]; } out << '\t' << sqrt(sum) << endl; } return 0; } catch(exception& e) { m->errorOut(e, "CorrAxesCommand", "calcPearson"); exit(1); } }