MyFracWindow::~MyFracWindow() { _presets.savePresets(kapp->config()); writeParameters(); writeDockConfig(); generatorThread.abort(); delete[] _gradientData; }
int Action::save(GFFFile &gff, uint32 top) { uint32 list; if(gff.writeVar(top, NWN_VAR_UINT16, "GroupActionId", groupactionid)) return ERR(gff); if(gff.writeVar(top, NWN_VAR_UINT16, "NumParams", parameters.size())) return ERR(gff); if(gff.writeVar(top, NWN_VAR_UINT32, "ActionId", actionid)) return ERR(gff); if(parameters.empty()) return errcode = 0; if(gff.writeList(list, top, "Paramaters")) return ERR(gff); if(writeParameters(gff, list)) return ERR(gff); return errcode = 0; }
double StartTraining(int&result) { double errors=0.0; vcbList eTrainVcbList, fTrainVcbList; globeTrainVcbList=&eTrainVcbList; globfTrainVcbList=&fTrainVcbList; string repFilename = Prefix + ".gizacfg" ; ofstream of2(repFilename.c_str()); writeParameters(of2,getGlobalParSet(),-1) ; cout << "reading vocabulary files \n"; eTrainVcbList.setName(SourceVocabFilename.c_str()); fTrainVcbList.setName(TargetVocabFilename.c_str()); eTrainVcbList.readVocabList(); fTrainVcbList.readVocabList(); cout << "Source vocabulary list has " << eTrainVcbList.uniqTokens() << " unique tokens \n"; cout << "Target vocabulary list has " << fTrainVcbList.uniqTokens() << " unique tokens \n"; vcbList eTestVcbList(eTrainVcbList) ; vcbList fTestVcbList(fTrainVcbList) ; corpus = new sentenceHandler(CorpusFilename.c_str(), &eTrainVcbList, &fTrainVcbList); if (TestCorpusFilename == "NONE") TestCorpusFilename = ""; if (TestCorpusFilename != ""){ cout << "Test corpus will be read from: " << TestCorpusFilename << '\n'; testCorpus= new sentenceHandler(TestCorpusFilename.c_str(), &eTestVcbList, &fTestVcbList); cout << " Test total # sentence pairs : " <<(*testCorpus).getTotalNoPairs1()<<" weighted:"<<(*testCorpus).getTotalNoPairs2() <<'\n'; cout << "Size of the source portion of test corpus: " << eTestVcbList.totalVocab() << " tokens\n"; cout << "Size of the target portion of test corpus: " << fTestVcbList.totalVocab() << " tokens \n"; cout << "In source portion of the test corpus, only " << eTestVcbList.uniqTokensInCorpus() << " unique tokens appeared\n"; cout << "In target portion of the test corpus, only " << fTestVcbList.uniqTokensInCorpus() << " unique tokens appeared\n"; cout << "ratio (target/source) : " << double(fTestVcbList.totalVocab()) / eTestVcbList.totalVocab() << '\n'; } cout << " Train total # sentence pairs (weighted): " << corpus->getTotalNoPairs2() << '\n'; cout << "Size of source portion of the training corpus: " << eTrainVcbList.totalVocab()-corpus->getTotalNoPairs2() << " tokens\n"; cout << "Size of the target portion of the training corpus: " << fTrainVcbList.totalVocab() << " tokens \n"; cout << "In source portion of the training corpus, only " << eTrainVcbList.uniqTokensInCorpus() << " unique tokens appeared\n"; cout << "In target portion of the training corpus, only " << fTrainVcbList.uniqTokensInCorpus() << " unique tokens appeared\n"; cout << "lambda for PP calculation in IBM-1,IBM-2,HMM:= " << double(fTrainVcbList.totalVocab()) << "/(" << eTrainVcbList.totalVocab() << "-" << corpus->getTotalNoPairs2() << ")="; LAMBDA = double(fTrainVcbList.totalVocab()) / (eTrainVcbList.totalVocab()-corpus->getTotalNoPairs2()); cout << "= " << LAMBDA << '\n'; // load dictionary Dictionary *dictionary; useDict = !dictionary_Filename.empty(); if (useDict) dictionary = new Dictionary(dictionary_Filename.c_str()); else dictionary = new Dictionary(""); int minIter=0; #ifdef BINARY_SEARCH_FOR_TTABLE if( CoocurrenceFile.length()==0 ) { cerr << "ERROR: NO COOCURRENCE FILE GIVEN!\n"; abort(); } //ifstream coocs(CoocurrenceFile.c_str()); tmodel<COUNT, PROB> tTable(CoocurrenceFile); #else tmodel<COUNT, PROB> tTable; #endif model1 m1(CorpusFilename.c_str(), eTrainVcbList, fTrainVcbList,tTable,trainPerp, *corpus,&testPerp, testCorpus, trainViterbiPerp, &testViterbiPerp); amodel<PROB> aTable(false); amodel<COUNT> aCountTable(false); model2 m2(m1,aTable,aCountTable); hmm h(m2); model3 m3(m2); if(ReadTablePrefix.length() ) { string number = "final"; string tfile,afilennfile,dfile,d4file,p0file,afile,nfile; //d5file tfile = ReadTablePrefix + ".t3." + number ; afile = ReadTablePrefix + ".a3." + number ; nfile = ReadTablePrefix + ".n3." + number ; dfile = ReadTablePrefix + ".d3." + number ; d4file = ReadTablePrefix + ".d4." + number ; //d5file = ReadTablePrefix + ".d5." + number ; p0file = ReadTablePrefix + ".p0_3." + number ; tTable.readProbTable(tfile.c_str()); aTable.readTable(afile.c_str()); m3.dTable.readTable(dfile.c_str()); m3.nTable.readNTable(nfile.c_str()); sentPair sent ; double p0; ifstream p0f(p0file.c_str()); p0f >> p0; d4model d4m(MAX_SENTENCE_LENGTH); d4m.makeWordClasses(m1.Elist,m1.Flist,SourceVocabFilename+".classes",TargetVocabFilename+".classes"); d4m.readProbTable(d4file.c_str()); //d5model d5m(d4m); //d5m.makeWordClasses(m1.Elist,m1.Flist,SourceVocabFilename+".classes",TargetVocabFilename+".classes"); //d5m.readProbTable(d5file.c_str()); makeSetCommand("model4smoothfactor","0.0",getGlobalParSet(),2); //makeSetCommand("model5smoothfactor","0.0",getGlobalParSet(),2); if( corpus||testCorpus ) { sentenceHandler *x=corpus; if(x==0) x=testCorpus; cout << "Text corpus exists.\n"; x->rewind(); while(x&&x->getNextSentence(sent)){ Vector<WordIndex>& es = sent.eSent; Vector<WordIndex>& fs = sent.fSent; int l=es.size()-1; int m=fs.size()-1; transpair_model4 tm4(es,fs,m1.tTable,m2.aTable,m3.dTable,m3.nTable,1-p0,p0,&d4m); alignment al(l,m); cout << "I use the alignment " << sent.sentenceNo-1 << '\n'; //convert(ReferenceAlignment[sent.sentenceNo-1],al); transpair_model3 tm3(es,fs,m1.tTable,m2.aTable,m3.dTable,m3.nTable,1-p0,p0,0); double p=tm3.prob_of_target_and_alignment_given_source(al,1); cout << "Sentence " << sent.sentenceNo << " has IBM-3 prob " << p << '\n'; p=tm4.prob_of_target_and_alignment_given_source(al,3,1); cout << "Sentence " << sent.sentenceNo << " has IBM-4 prob " << p << '\n'; //transpair_model5 tm5(es,fs,m1.tTable,m2.aTable,m3.dTable,m3.nTable,1-p0,p0,&d5m); //p=tm5.prob_of_target_and_alignment_given_source(al,3,1); //cout << "Sentence " << sent.sentenceNo << " has IBM-5 prob " << p << '\n'; } } else { cout << "No corpus exists.\n"; } }
int main (void) { // HELLO HERE I AM printf("example CAM_SERVER TCP/IP camera server ...\n\nfloat-format=%f\n\n", float(M_PI)); ///////////////////////////////////////////////////////////////////////////// // INITIALIZING the TCPIP SERVER CLASS ///////////////////////////////////////////////////////////////////////////// printf("starting TCP/IP server ...\n"); TCPIPserver* server=new TCPIPserver; server->set_port(PORT); server->open_socket(); printf("%s\n", server->get_version().c_str()); bool running=true; ///////////////////////////////////////////////////////////////////////////// // initializing basic variabls ///////////////////////////////////////////////////////////////////////////// uint16_t* frame=(uint16_t*)malloc(img_width*img_height*sizeof(uint16_t)); int img_byte_size=img_width*img_height*sizeof(uint16_t); double t=0; getNextFrame(t, frame, img_width, img_height); std::string basename="example_image_server_output"; ///////////////////////////////////////////////////////////////////////////// // initializing the acquisition thread structures + mutex ///////////////////////////////////////////////////////////////////////////// pthread_t thread1; pthread_mutex_init(&mutexframesCompleted, NULL); ///////////////////////////////////////////////////////////////////////////// // MAIN LOOP ///////////////////////////////////////////////////////////////////////////// // in this loop, instructions are read from the TCPIP connection // and the camera (in this case only a simulated device) is controlled // accordingly. The function getNextFrame() is used to retrieve frames // with a sinusoidal test pattern. ///////////////////////////////////////////////////////////////////////////// while(running) { // accept any incoming connection int connection=server->accept_connection(); printfMessage(" accepted connection %d from %s\n", connection, server->get_client_name(connection).c_str()); while(server->is_online(connection)) { // if we have an open connection, read instructions (as strings): std::string instruction=server->read_str_until(connection, '\n'); // decode instructions: if (instruction.size()>0) { if (instruction=="CONNECT") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // CONNCET TO CAMERA cam_connected=true; // SEND ANSWER TO CLIENT/QF3 server->write(connection, std::string("ACK_CONNECT\n\n")); // DONE CONNCETING TO CAMERA printfMessage("CAMERA CONNECTED!\n"); } else if (instruction=="DISCONNECT") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // DISCONNCET FROM CAMERA cam_connected=false; // SEND ANSWER TO CLIENT/QF3 server->write(connection, std::string("ACK_DISCONNECT\n\n")); // DONE DISCONNCETING FROM CAMERA printfMessage("CAMERA DISCONNECTED!\n"); } else if (instruction=="LIVE_START") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // START THE LIVE_VIEW MODE // INIT/CONFIG CAMERA for LIVE-VIEW cam_liveview=true; // SEND ANSWER TO CLIENT/QF3 server->write(connection, std::string("ACK_LIVE_START\n\n")); // DONE STARTING THE LIVE_VIEW MODE printfMessage("LIVE VIEW STARTED!\n"); } else if (instruction=="LIVE_STOP") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // STOP THE LIVE-VIEW MODE cam_liveview=false; // SEND ANSWER TO CLIENT/QF3 server->write(connection, std::string("ACK_LIVE_STOP\n\n")); printfMessage("LIVE VIEW STOPED!\n"); } else if (instruction=="SIZE_X_GET") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // RETURNS THE width OF A FRAME IN LIVE_VIEW MODE // SEND ANSWER TO CLIENT/QF3 server->write(connection, format("%d\n\n", img_width)); printfMessage("GET FRAME WIDTH!\n"); } else if (instruction=="SIZE_Y_GET") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // RETURNS THE height OF A FRAME IN LIVE_VIEW MODE // SEND ANSWER TO CLIENT/QF3 server->write(connection, format("%d\n\n", img_height)); printfMessage("GET FRAME HEIGHT!\n"); } else if (instruction=="GET_EXPOSURE") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // RETURNS THE exposure time [seconds] OF A FRAME IN LIVE_VIEW MODE // SEND ANSWER TO CLIENT/QF3 server->write(connection, format("%f\n\n", exposure)); printfMessage("GET EXPOSURE TIME!\n"); } else if (instruction=="PARAMETERS_GET") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // RETURNS A LIST OF THE AVAILABLE CAMERA PARAMETERS // SEND ANSWER TO CLIENT/QF3 // { (PARAM_FLOAT | PARAM_INT | PARAM_BOOL | PARAM_STRING);<parameter_name>;<parameter_value_as_string>;<parameter_description_as_string>;[<param_range_min>];[<param_range_max>];[(RW|RO)}\n }* \n writeParameters(server, connection); server->write(connection, "\n\n"); printfMessage("GET CAMERA PARAMETERS!\n"); } else if (instruction=="PARAMETER_GET") { // RETURNS A SINGLE PARAMETER // SEND ANSWER TO CLIENT/QF3 // { (PARAM_FLOAT | PARAM_INT | PARAM_BOOL | PARAM_STRING);<parameter_name>;<parameter_value_as_string>;<parameter_description_as_string>;[<param_range_min>];[<param_range_max>];[(RW|RO)}\n }{1} \n std::string param_name=server->read_str_until(connection, "\n\n"); writeParameters(server, connection, param_name); server->write(connection, "\n\n"); printfMessage("GET CAMERA PARAMETER '%s'!\n", param_name.c_str()); } else if (instruction=="PARAMETERS_SET") { // SET A SINGLE IMAGE PARAMETER // instruction has the form PARAMETERS_SET\n<name>;<value>\n\n std::string param_name=server->read_str_until(connection, ';'); std::string param_value=server->read_str_until(connection, "\n\n"); setParameter(param_name, param_value); printfMessage("SET CAMERA PARAMETERS ('%s' = '%s')!\n", param_name.c_str(), param_value.c_str()); } else if (instruction=="IMAGE_NEXT_GET") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // GET A NEW FRAME AND SEND IT TO THE CLIENT t++; getNextFrame(t, frame, img_width, img_height); // SEND ANSWER TO CLIENT/QF3: // 1. (IMAGE8 | IMAGE16 | IMAGE32 | IMAGE64) \n // 2. <image_width_in_pixels>\n // 3. <image_height_in_pixels>\n // 5. <image raw data of size image_width_in_pixels*image_height_in_pixels*pixel_data_size> // 6. METADATA RECORDS, DESCRIBING THE IMAGE: (from writeParameters()) // { (PARAM_FLOAT | PARAM_INT | PARAM_BOOL | PARAM_STRING);<parameter_name>;<parameter_value_as_string>[;<parameter_description_as_string>]\n }* \n server->write(connection, format("IMAGE%d\n%d\n%d\n", int(sizeof(frame[0])*8), img_width, img_height)); server->write(connection, (char*)frame, img_byte_size); writeParameters(server, connection); server->write(connection, "\n\n"); printfMessage("GET FRAME! t=%lf\n", t); } else if (instruction=="RECORD") { // START AN ACQUISITION AND SAVE DATA TO A FILE WITH THE BASENAME filename basename=server->read_str_until(connection, "\n\n"); if (!cam_acquisition_running) { printfMessage("RECORD FRAME START! basename=%s\n", basename.c_str()); // SEND ANSWER TO CLIENT/QF3 // 1. the written filenames (format FILE;<TYPE>;<FILENAME>;<DESCRIPTION>\n server->write(connection, format("FILE;TIFF;%s.tif;acquired frames\n", basename.c_str())); server->write(connection, format("FILE;TXT;%s.txt;additional text output\n", basename.c_str())); // 2. the used camera config parameters writeParameters(server, connection); server->write(connection, "\n\n"); cam_acquisition_running=true; int rc = pthread_create( &thread1, NULL, &acquisitionThreadFunc, (void*)(&basename) ); if( rc != 0 ) { cam_acquisition_running=false; printfMessage("COULD NOT START RECORD FRAME!\n"); } else { } } printfMessage("RECORD FRAME STARTED!"); } else if (instruction=="CANCEL_ACQUISITION") { // read remaining \n from "INSTRUCTION\n\n" server->read_str_until(connection, '\n'); // STOP THE LIVE-VIEW MODE pthread_mutex_lock(&mutexframesCompleted); cam_acquisition_running=false; pthread_mutex_unlock(&mutexframesCompleted); // SEND ANSWER TO CLIENT/QF3 server->write(connection, std::string("ACK_CANCEL_ACQUISITION\n\n")); printfMessage("ACQUISITION CANCELED!\n"); } else if (instruction!="\n" || instruction!="\n\n") { // PRINT ERROR MESSAGE AND IGNORE UNKNOWN INSTRUCTION printfMessage("read(%d) unknown instruction %s\n", connection, instruction.c_str()); } } } printfMessage(" connection %d closed\n", connection); } printf("stopping/freeing TCP/IP server ...\n"); free(frame); delete server; pthread_mutex_destroy(&mutexframesCompleted); pthread_exit(NULL); }
int main(int argc, char* argv[]) { // parameters that you can set. string infF = ""; string infR = ""; string infPath = ""; string outI = "oIter.txt"; string outP = "oPar.txt"; string outCorr = "oCorr.txt"; string outCnt = "oCnt.txt"; string outPath = ""; string outStub = ""; //bool useemp = false; double truncLimit = 0.99; int verbose = 3; int lambdaD = 147; int minD = 130; int maxD = 180; int sampleSize = -1; int burnins = 1000; int iterations = 10000; int seed = -1; // Unknowns double lambdaF[2]; double lambdaR[2]; double pF[2]; double pR[2]; /* * Read and check input parameters */ string errorLine = "usage " + string(argv[0]) + " [parameters] \n" + "\t-iF <forward reads binary file> \n" + "\t-iR <reverse reads binary file> \n" + "\t-iPath <path to forward and reverse reads binary files \n" + " (overrides iR and iF if set)> \n" + "\t-oPath <outdirectory, where all output files are put. \n" + "\t NOT created - needs to exists. Defaults to where \n"+ "\t the program is executed from.>\n" + "\t-oStub <outfile names stub, \n" + "\t defaults -oIter to <-oStub_>oIter.txt, \n" + "\t defaults -oPar to <-oStub_>oPar.txt> \n" + "\t defaults -oCorr to <-oStub_>oCorr.txt> \n" + "\t defaults -oCnt to <-oStub_>oCnt.txt> \n" + "\t-oIter <outfile, where to write MCMC parameter\n" + "\t estimates for each iteration,default generated from -oStub>\n" + "\t-oPar <parameter-file, where to write MCMC\n" + "\t final parameter estimates, default generated from -oStub> \n" + /* "\t-oCorr <out-file, lists correlation coefficients between forward\n" + "\t and reverse reads in [mind,maxd] \n" + "\t default generated from -oStub> \n" + */ "\t-oCnt <out-file, lists forward and reverse read count frequencies\n" + "\t default generated from -oStub> \n" + "\t-trunc <truncation limit in (0,1], default 0.99.> \n" + "\t-size <sample size, default all observations.> \n" + "\t-burnin <number of MCMC burnin iterations,\n" + "\t default 1000.> \n" + "\t-iter <number of MCMC iterations\n" + "\t (non-burnin iterations), default 10000.> \n" + "\t-seed <set seed to random number generator.> \n" + "\t-ld <distance-lambda, default 147 bp.> \n" + "\t-mind <min distance, default 130 bp.> \n" + "\t-maxd <max distance, default 180 bp.> \n" + /* "\t-useemp <toggle use of data-driven distance distribution, or poisson\n" + "\t around distance-lambda. default off.>\n" + */ "\t-v <verbose level 0-3. default 2>"; bool fail = false; string failmessage = ""; for (int i = 1; i < argc; i++) { if(strcmp(argv[i],"-iF") == 0) infF.assign(argv[++i]); else if(strcmp(argv[i],"-iR") == 0) infR.assign(argv[++i]); else if(strcmp(argv[i],"-iPath") == 0) infPath.assign(argv[++i]); else if(strcmp(argv[i],"-oPath") == 0) outPath.assign(argv[++i]); else if(strcmp(argv[i],"-oStub") == 0) outStub.assign(argv[++i]); else if(strcmp(argv[i],"-oIter") == 0) outI.assign(argv[++i]); else if(strcmp(argv[i],"-oPar") == 0) outP.assign(argv[++i]); /* else if(strcmp(argv[i],"-oCorr") == 0) outCorr.assign(argv[++i]); */ else if(strcmp(argv[i],"-oCnt") == 0) outCnt.assign(argv[++i]); /* else if (strcmp(argv[i],"-useemp") == 0) useemp = true; */ else if (strcmp(argv[i],"-v") == 0) verbose = atoi(argv[++i]); else if (strcmp(argv[i],"-seed") == 0) seed = atoi(argv[++i]); else if (strcmp(argv[i],"-trunc") == 0) truncLimit = atof(argv[++i]); else if (strcmp(argv[i],"-size") == 0) sampleSize = atoi(argv[++i]); else if (strcmp(argv[i],"-burnin") == 0) burnins = atoi(argv[++i]); else if (strcmp(argv[i],"-iter") == 0) iterations = atoi(argv[++i]); else if (strcmp(argv[i],"-ld") == 0) lambdaD = atoi(argv[++i]); else if (strcmp(argv[i],"-mind") == 0) minD = atoi(argv[++i]); else if (strcmp(argv[i],"-maxd") == 0) maxD = atoi(argv[++i]); else { failmessage.assign("Unknown argument: "); failmessage.append(argv[i]); failmessage.append("\n"); fail = true; } } if (truncLimit <= 0 || truncLimit > 1) { failmessage.append("-trunc value does not make sense.\n"); fail = true; } bool infPathSpec = false; if (strcmp(infPath.c_str(), "") != 0) { infPathSpec = true; DIR *d = opendir(infPath.c_str()); if(d) { closedir(d); } else { failmessage.append("-iPath does not exist.\n"); fail = true; } } if (strcmp(infF.c_str(), "") == 0) { if (!infPathSpec) { failmessage.append("-iF or -iPath must be specified.\n"); fail = true; } } if (strcmp(infR.c_str(), "") == 0) { if (!infPathSpec) { failmessage.append("-iR or -iPath must be specified.\n"); fail = true; } } if (strcmp(outI.c_str(), "") == 0) { failmessage.append("invalid -oIter.\n"); fail = true; } if (strcmp(outP.c_str(), "") == 0) { failmessage.append("invalid -oPar.\n"); fail = true; } if (strcmp(outCorr.c_str(), "") == 0) { failmessage.append("invalid -oCorr.\n"); fail = true; } if (strcmp(outCnt.c_str(), "") == 0) { failmessage.append("invalid -oCnt.\n"); fail = true; } if (strcmp(outPath.c_str(), "") != 0) { DIR* d = opendir(outPath.c_str()); if(d) { closedir(d); } else { failmessage.append("-oPath does not exist.\n"); fail = true; } } int infFCnt = 1; if (infPathSpec) { infFCnt = countFiles(infPath); if (infFCnt < 1) { failmessage.append("ERROR: infile path \""); failmessage.append(infPath.c_str()); failmessage.append("\" does not contain a positive number of F and R binary files, aborting.\n"); fail = true; } } if (fail) { cerr << endl << failmessage.c_str() << endl << errorLine << endl; return(-1); } if(strcmp(outStub.c_str(),"") != 0) { outI = outStub + "_" + outI; outP = outStub + "_" + outP; outCorr = outStub + "_" + outCorr; outCnt = outStub + "_" + outCnt; } if(strcmp(outPath.c_str(),"") != 0) { outI = outPath + outI; outP = outPath + outP; outCorr = outPath + outCorr; outCnt = outPath + outCnt; } if (seed < -1) seed = -1; ifstream iff[infFCnt]; ifstream ifr[infFCnt]; string fileNames[infFCnt]; if (infPathSpec) { if (openFiles(infPath, iff, ifr, fileNames) != infFCnt) { failmessage.append("ERROR: all files in \""); failmessage.append(infPath.c_str()); failmessage.append("\" could not be opened, aborting.\n"); fail = true; } } else { iff[0].open(infF.c_str(),ios::binary); ifr[0].open(infR.c_str(),ios::binary); if (iff[0].fail()) { failmessage.append("ERROR: Forward reads binary file \""); failmessage.append(infF.c_str()); failmessage.append("\" could not be opened, aborting.\n"); fail = true; } if (ifr[0].fail()) { failmessage.append("ERROR: Reverse reads binary file \""); failmessage.append(infR.c_str()); failmessage.append("\" could not be opened, aborting.\n"); fail = true; } } /* iff.open(infF.c_str(),ios::binary); if (iff.fail()) { failmessage.append("ERROR: Forward reads binary file \""); failmessage.append(infF.c_str()); failmessage.append("\" could not be opened, aborting.\n"); fail = true; } ifstream ifr; ifr.open(infR.c_str(),ios::binary); if (ifr.fail()) { failmessage.append("ERROR: Reverse reads binary file \""); failmessage.append(infR.c_str()); failmessage.append("\" could not be opened, aborting.\n"); fail = true; } */ ofstream ofi; ofi.open(outI.c_str(),ios::trunc); if (ofi.fail()) { failmessage.append("ERROR: Output file \""); failmessage.append(outI.c_str()); failmessage.append("\" could not be created, aborting.\n"); fail = true; } ofstream ofc; ofc.open(outCorr.c_str(),ios::trunc); if (ofc.fail()) { failmessage.append("ERROR: Output file \""); failmessage.append(outCorr.c_str()); failmessage.append("\" could not be created, aborting.\n"); fail = true; } ofstream ofcnt; ofcnt.open(outCnt.c_str(),ios::trunc); if (ofcnt.fail()) { failmessage.append("ERROR: Output file \""); failmessage.append(outCnt.c_str()); failmessage.append("\" could not be created, aborting.\n"); fail = true; } ofstream ofp; int truncValF,truncValR; int estMinD = minD, estMaxD = maxD, estLambdaD = lambdaD; double* distDens; vector<string> distDensV; ofp.open(outP.c_str(),ios::trunc); if (ofp.fail()) { failmessage.append("ERROR: Paramater file \""); failmessage.append(outP.c_str()); failmessage.append("\" could not be created, aborting.\n"); fail = true; } if (fail) { cerr << endl << failmessage.c_str() << endl << errorLine << endl; return(-1); } ofi << "! Command:"; for (int i = 0; i < argc; i++) ofi << " " << argv[i]; ofi << endl; vlevel = verbose; /* * Check file lengths */ int minPos[infFCnt], maxPos[infFCnt], minF[infFCnt], maxF[infFCnt], minR[infFCnt], maxR[infFCnt]; checkFileLengths(iff, ifr, minPos, maxPos, minR, minF, maxF, maxR, infFCnt); /* * Estimate parameters */ ofp << "! Command:"; for (int i = 0; i < argc; i++) ofp << " " << argv[i]; ofp << endl; vcerr(1) << "*** Identifying truncation limits and data distributions ***" << endl; calculateTruncVal(iff,ifr,&ofcnt, &truncValF, &truncValR, truncLimit, minPos, maxPos, minR, minF, maxF, maxR, infFCnt); ofcnt.close(); distDens = new double[maxD-minD+1]; if (infFCnt == 1) { estimateDistance(&iff[0],&ifr[0],&ofc, &estMinD, &estMaxD, &estLambdaD, minD, maxD, minPos[0], maxPos[0], minR[0], minF[0], maxF[0], maxR[0], truncValF, truncValR,distDens,0.3); ofc.close(); } /* if (useemp) { vcerr(2) << "\t* Estimating forward-reverse distance" << endl; estimateDistance(&iff,&ifr,&ofc, &estMinD, &estMaxD, &estLambdaD, minD, maxD, minPos, maxPos, minR, minF, maxF, maxR, truncValF, truncValR,distDens,0.3); ofc.close(); } else { */ for (int dist = minD; dist <= maxD; dist++) distDens[dist-minD] = gsl_ran_poisson_pdf(dist, lambdaD); /* } */ vcerr(1) << "*** Parameter estimation ***" << endl; if (estimateParameters(iff,ifr,&ofi, estMinD,estMaxD,estLambdaD, pF, pR, lambdaF, lambdaR, sampleSize,burnins,iterations, seed, minPos, maxPos, minR, minF, maxF, maxR, truncValF, truncValR, infFCnt) < 0) { cerr << "ERROR: estimateParameters failed, aborting." << endl; for (int i=0; i<infFCnt; i++) { iff[i].close(); ifr[i].close(); } ofi.close(); ofp.close(); delete[] distDens; return(-1); } else { if (writeParameters(&ofp, pF, pR, lambdaF, lambdaR, lambdaD, truncValF, truncValR, minD, maxD, estLambdaD, estMinD, estMaxD, distDens) < 0) { cerr << "WARNING: could not write parameters to file, skipping." << endl; } } ofi.close(); ofp.close(); for (int i=0; i<infFCnt; i++) { iff[i].close(); ifr[i].close(); } delete[] distDens; vcerr(3) << setprecision(3); vcerr(3) << endl << "\t\tParameter estimates:" << endl; vcerr(3) << "\t\tpF: S = " << pF[0] << " NotS = " << pF[1] << endl; vcerr(3) << "\t\tpR: E = " << pR[0] << " NotE = " << pR[1] << endl; vcerr(3) << "\t\tlambdaF: S = " << lambdaF[0] << " NotS = " << lambdaF[1] << endl; vcerr(3) << "\t\tlambdaR: E = " << lambdaR[0] << " NotE = " << lambdaR[1] << endl; vcerr(3) << "\t\testMinD: = " << estMinD << " estMaxd = " << estMaxD << endl; }
bool Dymola::setVariablesToDsin(QString fileName, QString modelName,MOVector<Variable> *variables,MOParameters *parameters,QString & errMsg) { //Reading Preamble QFileInfo fileinfo = QFileInfo(fileName); QFile file(fileinfo.filePath()); if(!file.open(QIODevice::ReadOnly)) { errMsg = "Failed to open "+fileName; return false; } else { QTextStream textRead(&file); QString allText = textRead.readAll(); file.close(); // change preamble writeParameters(allText,parameters); // change variable values QRegExp rxLine; int index=0; QString newLine1,newLine2; QString varName; int iCurVar; Variable* curVar; QStringList fields; QString smallText; QStringList capLines; int index2; int prec=MOSettings::value("MaxDigitsDsin").toInt(); //number of decimals QString value; for(int iV=0;iV<variables->size();iV++) { InfoSender::instance()->debug("Setting variable "+ varName+" in "+fileName); curVar = variables->at(iV); varName = curVar->name(Variable::SHORT); //varName = varName.remove(modelName+"."); rxLine.setPattern(sciNumRx()+"\\s+"+sciNumRx()+"\\s+"+sciNumRx()+"\\s+"+sciNumRx()+"\\s+"+sciNumRx()+"\\s+"+sciNumRx()+"\\s*#\\s*("+varName+")\\s*"); // extracting only text around varname // to speed-up regexp research (too long without) index2 = allText.indexOf(varName); smallText.clear(); while(index2>-1) { smallText += allText.mid(index2-300,310+varName.size()); // must capture end of line chars -> 310>300 index2 = allText.indexOf(varName,index2+1); } index = rxLine.indexIn(smallText); if(index>-1) { char format = 'E'; value = QString::number(curVar->getFieldValue(Variable::VALUE).toDouble(),format,prec); fields = rxLine.capturedTexts(); capLines = rxLine.cap(0).split("\n",QString::SkipEmptyParts); newLine1 = fields.at(1)+"\t"+ value +"\t"; newLine1 += fields.at(3)+"\t"+fields.at(4); newLine2 = fields.at(5)+"\t"+fields.at(6)+"\t"+" # "+fields.at(7); // if variable def were on two lines if((capLines.size()>1)&& capLines.at(1).contains(QRegExp("\\S"))) { InfoSender::instance()->debug("found variable. 2 lines. Total text captured: "+rxLine.cap(0)); allText = allText.replace(rxLine.cap(0),newLine1+"\n"+newLine2+"\n"); InfoSender::instance()->debug("New Text : "+newLine1+"\n"+newLine2); } else { InfoSender::instance()->debug("found variable. 1 line. Total text captured: "+rxLine.cap(0)); // if variable def were on only one line allText = allText.replace(rxLine.cap(0),newLine1+"\t"+newLine2+"\n"); InfoSender::instance()->debug("New Text : "+newLine1+"\t"+newLine2); } } else { InfoSender::instance()->send(Info("Unable to set variable value (not found in init file):"+varName,ListInfo::ERROR2)); } } fileinfo.setFile(fileName); file.setFileName(fileinfo.filePath()); bool ok = file.open(QIODevice::WriteOnly); if(!ok) { errMsg = "Unable to open file for writing :" + fileinfo.absoluteFilePath(); return false; } QTextStream textWrite(&file); textWrite<<allText; file.close(); } }