ST::string cdUp(ST::string path) { // Check for root paths, we can't go up from there! #ifdef _WIN32 if (path.substr(1) == ":\\") return path; #else if (path == "/") return path; #endif // Not very robust, but it works for one level of parent scanning if (path.is_empty()) return ".." PATHSEPSTR; // Strip the ending slash, if necessary, and then go up one dir if (path.char_at(path.size()-1) == PATHSEP) path = path.left(path.size() - 1); ST::string up = path.before_last(PATHSEP); if (path.char_at(0) == PATHSEP) { // Absolute path specified -- make sure we keep it that way return up + PATHSEPSTR; } else { // Relative path specified return up.is_empty() ? "" : up + PATHSEPSTR; } }
ST::string filenameConvert(const ST::string& filename, eDirection dir) { if (dir == kRepack) { fputs("Zrax broke me!\n", stderr); abort(); } ST::string newName = filename; ST_ssize_t dotLoc = newName.find_last('.'); if (dotLoc < 0) { newName += (dir == kCreate) ? ".prp" : ".prd"; } else if (dir == kCreate) { ST::string ext = newName.substr(dotLoc); if (ext == ".prd") newName.replace(".prd", ".prp"); else if (ext != ".prp") newName += ".prp"; } else { ST::string ext = newName.substr(dotLoc); if (ext == ".prp") newName.replace(".prp", ".prd"); else if (ext != ".prd") newName += ".prd"; } return newName; }
void FC_nonp_variance_varselection::outresults(ofstream & out_stata,ofstream & out_R, const ST::string & pathresults) { if (pathresults.isvalidfile() != 1) { ST::string pathresults_delta = pathresults.substr(0,pathresults.length()-4) + "_delta.res"; ST::string pathresults_omega = pathresults.substr(0,pathresults.length()-4) + "_omega.res"; FC_nonp_variance::outresults(out_stata,out_R,pathresults); FC_delta.outresults(out_stata,out_R,""); FC_omega.outresults(out_stata,out_R,pathresults_omega); optionsp->out(" Inclusion probability: " + ST::doubletostring(FC_delta.betamean(0,0),6) + "\n"); optionsp->out("\n"); optionsp->out(" Results for the inclusion probabilities are also stored in file\n"); optionsp->out(" " + pathresults_delta + "\n"); optionsp->out("\n"); optionsp->out("\n"); optionsp->out(" Inclusion probability parameter omega:\n"); optionsp->out("\n"); FC_omega.outresults_singleparam(out_stata,out_R,""); optionsp->out(" Results for the inclusion probability parameter omega are also stored in file\n"); optionsp->out(" " + pathresults_omega + "\n"); optionsp->out("\n"); optionsp->out("\n"); // deltas ofstream ou(pathresults_delta.strtochar()); ou << "pmean" << endl; ou << FC_delta.betamean(0,0) << endl; } // FC_nonp_variance::outresults(out_stata,out_R,pathresults); }
void FC_predict_mult::outresults_DIC(ofstream & out_stata, ofstream & out_R, const ST::string & pathresults) { ST::string pathresultsdic = pathresults.substr(0,pathresults.length()-4) + "_DIC.res"; ofstream out(pathresultsdic.strtochar()); out_R << "DIC=" << pathresultsdic << ";" << endl; optionsp->out(" Results for the DIC are stored in file\n"); optionsp->out(" " + pathresultsdic + "\n"); optionsp->out("\n"); double deviance2=0; double devhelp; vector<double *> worklinp; vector<double *> workresponse; vector<double *> workweight; vector<datamatrix *> aux; unsigned j; for (j=0;j<likep.size();j++) { worklinp.push_back(betamean.getV()+j); workresponse.push_back(likep[j]->response.getV()); workweight.push_back(likep[j]->weight.getV()); aux.push_back(likep[j]->get_auxiliary_parameter(auxpostmean)); } unsigned i; for (i=0;i<likep[0]->nrobs;i++) { likep[likep.size()-1]->compute_deviance_mult(workresponse, workweight,worklinp, &devhelp,aux); deviance2 += devhelp; int s = likep.size(); int bs = betamean.cols(); for (j=0;j<s;j++) { worklinp[j]+=bs; workresponse[j]++; workweight[j]++; } } double devhelpm = FC_deviance.betamean(0,0); unsigned d; if (devhelpm > 1000000000) d = 14; else if (devhelpm > 1000000) d = 11; else d = 8; out << "deviance pd dic" << endl; optionsp->out(" ESTIMATION RESULTS FOR THE DIC: \n",true); optionsp->out("\n"); optionsp->out(" Deviance(bar_mu): " + ST::doubletostring(deviance2,d) + "\n"); out << deviance2 << " "; optionsp->out(" pD: " + ST::doubletostring(devhelpm-deviance2,d) + "\n"); out << (devhelpm-deviance2) << " "; optionsp->out(" DIC: " + ST::doubletostring(2*devhelpm-deviance2,d) + "\n"); optionsp->out("\n"); out << (2*devhelpm-deviance2) << " " << endl; optionsp->out("\n"); }
void FC_nonp_variance::outresults(ofstream & out_stata,ofstream & out_R, const ST::string & pathresults) { FC::outresults(out_stata,out_R,""); ST::string l1 = ST::doubletostring(optionsp->lower1,4); ST::string l2 = ST::doubletostring(optionsp->lower2,4); ST::string u1 = ST::doubletostring(optionsp->upper1,4); ST::string u2 = ST::doubletostring(optionsp->upper2,4); ST::string nl1 = ST::doubletostring(optionsp->lower1,4); ST::string nl2 = ST::doubletostring(optionsp->lower2,4); ST::string nu1 = ST::doubletostring(optionsp->upper1,4); ST::string nu2 = ST::doubletostring(optionsp->upper2,4); nl1 = nl1.replaceallsigns('.','p'); nl2 = nl2.replaceallsigns('.','p'); nu1 = nu1.replaceallsigns('.','p'); nu2 = nu2.replaceallsigns('.','p'); ST::string vstr; if (optionsp->samplesize > 1) { vstr = " Mean: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(betamean(0,0),6) + "\n"); vstr = " Std. dev.: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(sqrt(betavar(0,0)),6) + "\n"); vstr = " " + l1 + "% Quantile: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(betaqu_l1_lower(0,0),6) + "\n"); vstr = " " + l2 + "% Quantile: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(betaqu_l2_lower(0,0),6) + "\n"); vstr = " 50% Quantile: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(betaqu50(0,0),6) + "\n"); vstr = " " + u1 + "% Quantile: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(betaqu_l2_upper(0,0),6) + "\n"); vstr = " " + u2 + "% Quantile: "; optionsp->out(vstr + ST::string(' ',20-vstr.length()) + ST::doubletostring(betaqu_l1_upper(0,0),6) + "\n"); optionsp->out("\n"); } else { optionsp->out(" Smoothing parameter: " + ST::doubletostring(betamean(0,1),6) + "\n"); optionsp->out("\n"); } // out_R << "term=" << title << ";" << endl; if (pathresults.isvalidfile() != 1) { optionsp->out(" Results for the variance component are also stored in file\n"); optionsp->out(" " + pathresults + "\n"); optionsp->out("\n"); ST::string paths = pathresults.substr(0,pathresults.length()-4) + "_sample.raw"; out_R << "pathvarsample=" << paths << endl; // out_R << "filetype=param; path=" << pathresults << ";" << endl; ofstream ou(pathresults.strtochar()); if (optionsp->samplesize > 1) { ou << "pmean pstd pqu" << nl1 << " pqu" << nl2 << " pmed pqu" << nu1 << " pqu" << nu2 << endl; } else { ou << "pmean" << endl; } ou << betamean(0,0) << " "; if (optionsp->samplesize > 1) { if (betavar(0,0) < 0.0000000000001) ou << 0 << " "; else ou << sqrt(betavar(0,0)) << " "; ou << betaqu_l1_lower(0,0) << " "; ou << betaqu_l2_lower(0,0) << " "; ou << betaqu50(0,0) << " "; ou << betaqu_l2_upper(0,0) << " "; ou << betaqu_l1_upper(0,0) << " " << endl; } optionsp->out("\n"); } }
int main(int argc, char* argv[]) { if (argc < 2 || argc > 3) { doHelp(); return 0; } eDirection direction = kRepack; ST::string filename = argv[1]; ST_ssize_t dotLoc = filename.find_last('.'); if (argc == 3) { if (strcmp(argv[1], "-c") == 0) direction = kCreate; else if (strcmp(argv[1], "-x") == 0) direction = kExtract; else if (strcmp(argv[1], "-r") == 0) direction = kRepack; else { doHelp(); return 1; } filename = argv[2]; } else if (dotLoc >= 0 && filename.substr(dotLoc) == ".prd") { direction = kCreate; } plResManager rm; hsFileStream S, OS; if (!S.open(filename, fmRead)) { ST::printf(stderr, "Error opening {} for reading!", filename); return 1; } plPageInfo* page = new plPageInfo(); //int len; short maj = 63, min = 11; unsigned int i, j; if (direction == kExtract || direction == kRepack) { S.close(); delete page; page = rm.ReadPage(filename, true); OS.open(filenameConvert(filename, kExtract), fmCreate); OS.write(4, "PRD"); OS.writeShort(page->getAge().size()); OS.writeStr(page->getAge()); OS.writeShort(page->getPage().size()); OS.writeStr(page->getPage()); if (rm.getVer().isUniversal()) { maj = 0x7FFF; min = 0x7FFF; } else if (rm.getVer().isEoa()) { maj = -1; min = 1; } else if (rm.getVer().isHexIsle()) { maj = -1; min = 2; } else { maj = rm.getVer().revMajor(); min = rm.getVer().revMinor(); } OS.writeShort(maj); OS.writeShort(min); plLocation loc = page->getLocation(); loc.write(&OS); OS.close(); std::vector<short> types = rm.getTypes(loc); #ifdef _WIN32 CreateDirectoryW(getOutputDir(filename, page).to_wchar().data(), NULL); #else mkdir(getOutputDir(filename, page).c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); #endif for (i=0; i<types.size(); i++) { std::vector<plKey> objs = rm.getKeys(loc, types[i]); for (j=0; j<objs.size(); j++) { ST::string po_file = ST::format("{}[{04X}]{}.po", getOutputDir(filename, page), types[i], CleanFileName(objs[j]->getName())); OS.open(po_file, fmCreate); OS.setVer(rm.getVer()); rm.WriteCreatable(&OS, objs[j]->getObj()); OS.close(); } } } if (direction == kRepack) { filename = filenameConvert(filename, kExtract); S.open(filename, fmRead); } if (direction == kCreate || direction == kRepack) { OS.open(filenameConvert(filename, kCreate), fmCreate); char sig[4]; S.read(4, sig); if (strncmp(sig, "PRD", sizeof(sig)) != 0) { fputs("Error: Invalid input file!\n", stderr); OS.close(); S.close(); return 1; } ST::string ageName = S.readStr(S.readShort()); ST::string pageName = S.readStr(S.readShort()); page->setAge(ageName); page->setPage(pageName); maj = S.readShort(); min = S.readShort(); if (maj == 0x7FFF) { OS.setVer(PlasmaVer::pvUniversal); } else if (maj == -1) { if (min == 1) OS.setVer(PlasmaVer::pvEoa); else if (min == 2) OS.setVer(PlasmaVer::pvHex); } else if (maj == 70) { OS.setVer(PlasmaVer::pvMoul); } else if (maj == 63) { if (min == 11) OS.setVer(PlasmaVer::pvPrime); if (min == 12) OS.setVer(PlasmaVer::pvPots); } else { ST::printf(stderr, "Error: Invalid Plasma version: {}.{}\n", maj, min); OS.close(); S.close(); return 1; } S.setVer(OS.getVer()); plLocation loc; loc.read(&S); page->setLocation(loc); page->setReleaseVersion(0); page->setFlags(plPageInfo::kBasicChecksum); S.close(); std::vector<ST::string> inFiles; std::vector<short> inClasses; hsFileStream PS; PS.setVer(OS.getVer()); #ifdef _WIN32 ST::string pattern = ST::format("{}*.po", getOutputDir(filename, page)); WIN32_FIND_DATAW fd; HANDLE fr = FindFirstFileW(pattern.to_wchar().data(), &fd); if (fr != NULL) { do { ST::string po_file = getOutputDir(filename, page) + fd.cFileName; inFiles.push_back(po_file); PS.open(po_file, fmRead); short classType = PS.readShort(); PS.close(); bool haveClass = false; for (j=0; j<inClasses.size(); j++) if (inClasses[j] == classType) { haveClass = true; } if (!haveClass) inClasses.push_back(classType); } while (FindNextFileW(fr, &fd)); FindClose(fr); } #else dirent** des; unsigned int nEntries = scandir(getOutputDir(filename, page).c_str(), &des, &selPO, &alphasort); for (i=0; i<nEntries; i++) { ST::string po_file = getOutputDir(filename, page) + des[i]->d_name; inFiles.push_back(po_file); PS.open(po_file, fmRead); short classType = PS.readShort(); PS.close(); bool haveClass = false; for (j=0; j<inClasses.size(); j++) if (inClasses[j] == classType) { haveClass = true; } if (!haveClass) inClasses.push_back(classType); } #endif page->setClassList(inClasses); page->write(&OS); page->setDataStart(OS.pos()); plKeyCollector keys; for (i=0; i<inFiles.size(); i++) { plKey key = new plKeyData(); PS.open(inFiles[i], fmRead); PS.setVer(S.getVer()); unsigned int poLen = PS.size(); uint8_t* objBuf = new uint8_t[poLen]; key->setFileOff(OS.pos()); key->setObjSize(poLen); PS.read(poLen, objBuf); OS.write(poLen, objBuf); delete[] objBuf; PS.seek(2); key->readUoid(&PS); PS.close(); keys.add(key); } page->setIndexStart(OS.pos()); keys.sortKeys(page->getLocation()); std::vector<short> types = keys.getTypes(page->getLocation()); //if (types != inClasses) // throw "Wtf, mate?"; OS.writeInt(types.size()); for (i=0; i<types.size(); i++) { std::vector<plKey> kList = keys.getKeys(page->getLocation(), types[i]); OS.writeShort(pdUnifiedTypeMap::MappedToPlasma(types[i], OS.getVer())); unsigned int lenPos = OS.pos(); if (!OS.getVer().isUruSP() && !OS.getVer().isUniversal()) { OS.writeInt(0); OS.writeByte(0); } OS.writeInt(kList.size()); for (j=0; j<kList.size(); j++) kList[j]->write(&OS); if (!OS.getVer().isUruSP() && !OS.getVer().isUniversal()) { unsigned int nextPos = OS.pos(); OS.seek(lenPos); OS.writeInt(nextPos - lenPos - 4); OS.seek(nextPos); } } if (OS.getVer().isNewPlasma()) page->setChecksum(OS.pos()); else page->setChecksum(OS.pos() - page->getDataStart()); page->writeSums(&OS); OS.close(); } // Delete temp files with the repack option if (direction == kRepack) { #ifdef _WIN32 ST::string pattern = ST::format("{}*.po", getOutputDir(filename, page)); WIN32_FIND_DATAW rfd; HANDLE rfr = FindFirstFileW(pattern.to_wchar().data(), &rfd); if (rfr != NULL) { do { ST::string po_file = getOutputDir(filename, page) + rfd.cFileName; DeleteFileW(po_file.to_wchar().data()); } while (FindNextFileW(rfr, &rfd)); FindClose(rfr); } RemoveDirectoryW(getOutputDir(filename, page).to_wchar().data()); DeleteFileW(filename.to_wchar().data()); #else dirent** rdes; unsigned int nEntries = scandir(getOutputDir(filename, page).c_str(), &rdes, &selAll, &alphasort); for (i=0; i<nEntries; i++) { ST::string po_file = getOutputDir(filename, page) + rdes[i]->d_name; unlink(po_file.c_str()); } rmdir(getOutputDir(filename, page).c_str()); unlink(filename.c_str()); #endif } return 0; }
bool administrator::parse(ST::string & in) { errormessages.clear(); ST::string objectname; ST::string firsttoken = in.getFirstToken(" ."); int pointpos = in.checksign('.'); if (firsttoken.length() > 0) { if ( (firsttoken == "quit") || (firsttoken == "exit") ) return true; else if (firsttoken == "delimeter") { vector<ST::string> token = in.strtoken(" "); if (token.size() != 3) errormessages.push_back("ERROR: invalid syntax\n"); else if (token[1] != "=") errormessages.push_back("ERROR: \"=\" expected\n"); else { if (token[2] == "newline") delim = '\n'; else if (token[2].length() > 1) errormessages.push_back("ERROR: invalid delimeter symbol\n"); else delim = token[2][0]; } return false; } // end: delimeter else if (firsttoken == "usefile") { vector<ST::string> token = in.strtoken(" "); if (token.size() < 2) errormessages.push_back("ERROR: filename expected\n"); else if (token.size() > 2) errormessages.push_back("ERROR: invalid syntax\n"); if (errormessages.empty()) { ST::string path = token[1]; if (path.isexistingfile() == 1) errormessages.push_back("ERROR: file " + path + " could not be opened\n"); else { ST::string in; ifstream infile; input = &infile; ST::open(infile,path); while (! infile.eof()) { ST::getline(infile,10000,in,delim); if (delim != '\n') in = in.replaceallsigns('\n',' '); in = in.eatwhitespace(); out("> " + in + "\n"); parse(in); } } } input = &cin; out(errormessages); return false; } else if (firsttoken == "logopen") { model m; simpleoption replace("replace",false); optionlist logoptions; logoptions.push_back(&replace); usePathWrite uw; command logopen("logopen",&m,&logoptions,&uw,notallowed,notallowed, notallowed,notallowed,optional,required); logopen.parse(in); errormessages = logopen.geterrormessages(); if (logfileopen == true) errormessages.push_back("ERROR: logfile is already open\n"); if (errormessages.empty()) { logfileopen = true; logfilepath = uw.getPath(); if ((replace.getvalue() == false) && (uw.isexisting() == true)) { ST::open(logout,logfilepath,ios::app); } else { ST::open(logout,logfilepath); } } else out(errormessages); return false; } // end: logopen else if (firsttoken == "logclose") { if (logfileopen == false) { errormessages.push_back("ERROR: currently no logfile open\n"); out(errormessages); } else { logfileopen = false; logout.close(); out("NOTE: logfile " + logfilepath + " closed\n"); } return false; } // end: logclose else if (firsttoken == "drop") { modelStandard m; optionlist dropoptions; usePathWrite uw; command drop("drop",&m,&dropoptions,&uw,required,notallowed, notallowed,notallowed,notallowed,notallowed); drop.parse(in); errormessages = drop.geterrormessages(); vector<ST::string> objectnames = m.getModelVarnamesAsVector(); if (objectnames.size() == 0) errormessages.push_back("ERROR: objectlist required\n"); if (errormessages.empty()) { int j; for (j=0;j<objectnames.size();j++) { int recognized = 0; int i=0; while ( (i < objects.size()) && (recognized == 0) ) { if ( objectnames[j] == objects[i]->getname()) { ST::string type = objects[i]->gettype(); dropobjects(objectnames[j],type); recognized = 1; } i++; } if (recognized == 0) errormessages.push_back( "ERROR: object " + objectnames[j] + " is not existing\n"); } // end: for (j=0;j<objectnames.size();j++) } // end: if (errormessages.empty()) out(errormessages); return false; } // end: drop else if (firsttoken.isinlist(objecttyps) >= 0) // create a new object { if (pointpos == -1) objectname = create(in); else objectname = create(in.substr(0,pointpos)); if ( (errormessages.empty()) && (pointpos > 0) ) { if (in.length()-1-pointpos <= 0) errormessages.push_back("ERROR: invalid syntax\n"); else parseexisting(objectname,in.substr(pointpos+1,in.length()-1-pointpos)); } out(errormessages); return false; } // end: create a new object else // existing object { if (pointpos != firsttoken.length()) errormessages.push_back("ERROR: invalid syntax\n"); else if (in.length() > pointpos+1) parseexisting(firsttoken,in.substr(pointpos+1,in.length()-pointpos-1)); else errormessages.push_back("ERROR: invalid syntax\n"); out(errormessages); return false; } } // end: if (firsttoken.length() > 0) else // empty command return false; }
void FC_cv::outresults(ofstream & out_stata, ofstream & out_R, const ST::string & pathresults) { if (pathresults.isvalidfile() != 1) { FC::outresults(out_stata,out_R,pathresults); optionsp->out(" Marshall-Spiegelhalter Cross Validation: \n",true); optionsp->out("\n"); optionsp->out(" Estimated individual observation samples are stored in\n"); optionsp->out(" " + pathresults + "\n"); optionsp->out("\n"); ST::string pathresults_like = pathresults.substr(0,pathresults.length()-4)+ "_like.res"; FC_sampled_l.outresults(out_stata,out_R,pathresults_like); optionsp->out(" Estimated individual observation likelihoods are stored in\n"); optionsp->out(" " + pathresults_like + "\n"); optionsp->out("\n"); // unsigned nrobs = sampled_etas.rows(); unsigned i; /* ofstream outres(pathresults.strtochar()); for(j=0;j<sampled_etas.cols();j++) outres << "s_eta_" << (j+1) << " "; for(j=0;j<sampled_etas.cols();j++) outres << "s_resp_" << (j+1) << " "; outres << endl; for (i=0;i<nrobs;i++) { for(j=0;j<sampled_etas.cols();j++) outres << sampled_etas(i,j) << " "; for(j=0;j<sampled_responses.cols();j++) outres << sampled_responses(i,j) << " "; outres << endl; } */ // Energy score double es = compute_energyscore(); ST::string pathresults_e = pathresults.substr(0,pathresults.length()-4)+ "_energy.res"; ofstream out2(pathresults_e.strtochar()); out2 << "id score" << endl; for (i=0;i<e_score.rows();i++) out2 << effectvalues[i] << " " << e_score(i,0) << endl; // Log-score double ls = compute_logscore(); ST::string pathresults_l = pathresults.substr(0,pathresults.length()-4)+ "_logscore.res"; ofstream out3(pathresults_l.strtochar()); out3 << "id score" << endl; for (i=0;i<e_score.rows();i++) out3 << effectvalues[i] << " " << log_score(i,0) << endl; optionsp->out(" Estimated energy scores are stored in\n"); optionsp->out(" " + pathresults_e + "\n"); optionsp->out("\n"); optionsp->out(" Estimated log-scores are stored in\n"); optionsp->out(" " + pathresults_l + "\n"); optionsp->out("\n"); optionsp->out(" Mean energy score: " + ST::doubletostring(es,8) + "\n"); optionsp->out(" Mean log score: " + ST::doubletostring(ls,8) + "\n"); } // end if (pathresults.isvalidfile() != 1) }