//__________________________________________________________________ void _HYSequencePane::CleanUpSequenceNames (void) { bool doSomething = false; _List namesl; _AVLList names (&namesl); for (long k=0; k<speciesIndex.lLength; k++) { _String * thisString = (_String*)rowHeaders (speciesIndex.lData[k]); if (!thisString->IsValidIdentifier(false)) { BufferToConsole ("Changed "); StringToConsole(*thisString); thisString->ConvertToAnIdent(false); BufferToConsole (" to "); StringToConsole(*thisString); NLToConsole(); doSomething = true; } _String * testString = new _String (*thisString); if (!testString) checkPointer (testString); long tryThisSuffix = 2; while (names.Find (testString)>=0) { *testString = *thisString & '_' & tryThisSuffix; tryThisSuffix++; } if (tryThisSuffix>2) { BufferToConsole ("Changed "); StringToConsole(*thisString); BufferToConsole (" to "); StringToConsole(*testString); BufferToConsole (" to avoid duplicate identifiers\n"); doSomething = true; thisString->CopyDynamicString (testString,true); } else DeleteObject (testString); names.Insert(thisString); thisString->nInstances++; } if (doSomething) { SetHeaders (nil,true); _MarkForUpdate(); } }
//__________________________________________________________________________________ void SetStatusLineUser (_String s) { setvbuf(stdout, NULL, _IONBF, 0); BufferToConsole("\33[2K\r"); StringToConsole(s); needExtraNL = true; }
bool ExpressionCalculator (void) { _String data (StringFromConsole(false)); #ifndef __UNIX__ if (terminateExecution) { return false; } BufferToConsole (">"); StringToConsole (data); BufferToConsole ("\n"); #endif if (data.sLength == 4) { _String checkForExit (data); checkForExit.LoCase(); if (checkForExit == _String ("exit")) { return false; } } _Formula lhs, rhs; _FormulaParsingContext fpc; long retCode = Parse(&lhs, data, fpc, nil); if (!terminateExecution) { if (retCode == HY_FORMULA_EXPRESSION) { _PMathObj formRes = lhs.Compute(); if (!formRes) { BufferToConsole ("NULL\n"); } else { _String * objValue = (_String*)formRes->toStr(); StringToConsole (*objValue); //BufferToConsole ("\n"); DeleteObject (objValue); } } else { BufferToConsole ("NO RETURN VALUE"); } } NLToConsole(); terminateExecution = false; return true; }
_String ReturnDialogInput(bool dispPath) { if (!dispPath) { NLToConsole (); StringToConsole (dialogPrompt); BufferToConsole (":"); } else { NLToConsole (); if (pathNames.lLength) { StringToConsole(*(_String*)pathNames(pathNames.lLength-1)); } else { StringToConsole (baseDirectory); } StringToConsole (dialogPrompt); BufferToConsole (":"); } return StringFromConsole(); }
bool ExpressionCalculator (_String data) { //Checking for exit #ifndef __HYPHYQT__ if (data.sLength == 4) { _String checkForExit (data); checkForExit.LoCase(); if (checkForExit == _String ("exit")) { return false; } } #endif _Formula lhs, rhs; _String errMsg; _FormulaParsingContext fpc (&errMsg, nil); long retCode = Parse(&lhs, data, fpc, nil); if (retCode != HY_FORMULA_FAILED) { if (retCode == HY_FORMULA_EXPRESSION) { _PMathObj formRes = lhs.Compute(0,nil,nil,&errMsg); if (errMsg.sLength) { WarnError(errMsg); } else { _String * objValue = (_String*)formRes->toStr(); StringToConsole(*objValue); DeleteObject(objValue); } } else { BufferToConsole ("NO RETURN VALUE"); } } else { WarnError(errMsg); } return true; }
void TrainModelNN (_String* model, _String* matrix) { _String errMsg; long modelIdx = modelNames.Find(model); _Parameter verbI; checkParameter (VerbosityLevelString, verbI, 0.0); char buffer [128]; if (modelIdx < 0) { errMsg = *model & " did not refer to an existring model"; } else { _Variable* boundsMatrix = FetchVar (LocateVarByName (*matrix)); if (boundsMatrix && (boundsMatrix->ObjectClass() == MATRIX)) { _Matrix * bmatrix = (_Matrix*) boundsMatrix->GetValue (); if (bmatrix->IsAStringMatrix() && (bmatrix->GetVDim () == 3)) { _Variable* modelMatrix = LocateVar (modelMatrixIndices.lData[modelIdx]); _SimpleList modelVariableList; { _AVLList mvla (&modelVariableList); modelMatrix->ScanForVariables (mvla, true); mvla.ReorderList(); } if (bmatrix->GetHDim () == modelVariableList.lLength) { // now map model variables to bounds matrix _SimpleList variableMap; _String *myName; for (long k = 0; k < modelVariableList.lLength; k++) { myName = ((_FString*)bmatrix->GetFormula(k,0)->Compute())->theString; long vID = LocateVarByName (*myName); if (vID < 0) { break; } vID = variableNames.GetXtra (vID); vID = modelVariableList.Find(vID); if (vID < 0) { break; } variableMap << vID; } if (variableMap.lLength == modelVariableList.lLength) { _Matrix vBounds (variableMap.lLength,2, false, true); long k2 = 0; for (; k2 < variableMap.lLength; k2++) { _Parameter lb = ((_FString*)bmatrix->GetFormula(k2,1)->Compute())->theString->toNum(), ub = ((_FString*)bmatrix->GetFormula(k2,2)->Compute())->theString->toNum(); if ( ub>lb || k2) { vBounds.Store (k2,0,lb); vBounds.Store (k2,1,ub); if (ub<=lb && vBounds (k2-1,0) <= vBounds (k2-1,1) && (!CheckEqual(vBounds (k2-1,0),0.0) || !CheckEqual(vBounds (k2-1,1),1.0))) { break; } } } if (k2 == modelVariableList.lLength) { // set up the sampling now _String fName = ProcessLiteralArgument (&ModelNNFile,nil); FILE* nnFile = doFileOpen (fName.getStr(), "w"); if (nnFile) { _Matrix* modelMatrix = (_Matrix*) LocateVar(modelMatrixIndices.lData[modelIdx])->GetValue(); _Parameter mainSteps, checkSteps, errorTerm, loopMax, hiddenNodes, absError, nn1, nn2; long fullDimension = modelMatrix->GetHDim() * modelMatrix->GetVDim(); checkParameter (ModelNNTrainingSteps, mainSteps, 10000.0); checkParameter (ModelNNVerificationSample, checkSteps, 500.0); checkParameter (ModelNNPrecision, errorTerm, 0.01); checkParameter (ModelNNTrainingSteps, loopMax, 10); checkParameter (ModelNNHiddenNodes, hiddenNodes, 5); checkParameter (ModelNNLearningRate, nn1, .3); checkParameter (ModelNNPersistenceRate, nn2, .1); Net** matrixNet = new Net* [fullDimension] ; for (long i = 0; i < fullDimension; i++) { checkPointer (matrixNet [i] = new Net (variableMap.lLength,(long)hiddenNodes,1,errorTerm,nn1,nn2,100,200,true)); //matrixNet[i]->verbose = true; } checkPointer (matrixNet); _List tIn, tOut; FILE* varSamples = doFileOpen ("variableSamples.out", "w"); fprintf (varSamples, "%s" ,LocateVar(modelVariableList.lData[0])->GetName()->getStr()); for (long vc = 1; vc < modelVariableList.lLength; vc++) { fprintf (varSamples, ",%s" ,LocateVar(modelVariableList.lData[variableMap.lData[vc]])->GetName()->getStr()); } fprintf (varSamples, "\n"); for (long itCount = 0; itCount < loopMax; itCount ++) { if (verbI > 5) { snprintf (buffer, sizeof(buffer), "\nNeural Network Pass %ld. Building a training set...\n", itCount); BufferToConsole (buffer); } while (tIn.countitems() < mainSteps) { NNMatrixSampler (0, vBounds, modelVariableList, variableMap, modelMatrix, tIn, tOut); } _Matrix inData (mainSteps, variableMap.lLength, false, true); _Parameter *md = inData.theData; for (long matrixC = 0; matrixC < mainSteps; matrixC++) { _Parameter * ed = ((_Matrix*)tIn (matrixC))->theData; fprintf (varSamples, "\n%g",*ed); *md = *ed; ed++; md++; for (long entryC = 1; entryC < variableMap.lLength; entryC++, ed++, md++) { *md = *ed; fprintf (varSamples, ",%g", *md); } } tIn.Clear(); if (verbI > 5) { BufferToConsole ( "Done Building Training Set. Training...\n"); } long lastDone = 0; for (long cellCount = 0; cellCount < fullDimension; cellCount++) { Net* thisCell = matrixNet[cellCount]; _Matrix outVector (mainSteps, 1, false, true); for (long oc = 0; oc < mainSteps; oc++) { outVector.theData[oc] = ((_Matrix*)tOut(oc))->theData[cellCount]; } thisCell->studyAll (inData.theData, outVector.theData, mainSteps); long nowDone = (cellCount+1)*100./fullDimension; if (nowDone > lastDone) { snprintf (buffer, sizeof(buffer),"%ld%% done\n", lastDone = nowDone); BufferToConsole (buffer); } } tOut.Clear(); if (verbI > 5) { BufferToConsole ( "Done Training. Resampling...\n"); } _PMathObj tObj = _Constant(0).Time(); _Parameter time1 = tObj->Value(), time2; while (tIn.countitems() < checkSteps) { NNMatrixSampler (0, vBounds, modelVariableList, variableMap, modelMatrix, tIn, tOut); } absError = 0.0; DeleteObject (tObj); tObj = _Constant(0).Time(); time2 = tObj->Value(); if (verbI > 5) { snprintf (buffer, sizeof(buffer),"Done Resampling in %g seconds. Computing Error...\n", time2-time1); BufferToConsole (buffer); } _Parameter maxValT, maxValE; for (long verCount = 0; verCount < checkSteps; verCount++) { _Parameter* inData = ((_Matrix*)tIn(verCount))->theData, * outData = ((_Matrix*)tOut(verCount))->theData; for (long cellCount = 0; cellCount < fullDimension; cellCount++) { Net *thisCell = matrixNet[cellCount]; _Parameter estVal = thisCell->eval(inData)[0], trueVal = outData[cellCount], localError; localError = estVal-trueVal; if (localError < 0) { localError = -localError; } if (absError < localError) { maxValT = trueVal; maxValE = estVal; absError = localError; } } } DeleteObject (tObj); tObj = _Constant(0).Time(); time1 = tObj->Value(); DeleteObject (tObj); if (verbI > 5) { snprintf (buffer, sizeof(buffer), "Done Error Checking in %g seconds. Got max abs error %g on the pair %g %g\n", time1-time2, absError, maxValT, maxValE); BufferToConsole (buffer); } if (absError <= errorTerm) { break; } } if (absError > errorTerm) { ReportWarning (_String("Couldn't achive desired precision in TrainModelNN. Achieved error of ") & absError); } fclose (varSamples); fprintf (nnFile,"{{\n\"%s\"", LocateVar(modelVariableList.lData[0])->GetName()->getStr()); _Matrix newBounds (modelVariableList.lLength, 2, false, true); if (vBounds(0,0)>vBounds(0,1)) { newBounds.Store (variableMap.lData[0],0,0.); newBounds.Store (variableMap.lData[0],1,1.); } else { newBounds.Store (variableMap.lData[0],0,vBounds(0,0)); newBounds.Store (variableMap.lData[0],1,vBounds(0,1)); } for (long varCounter = 1; varCounter < modelVariableList.lLength; varCounter ++) { fprintf (nnFile,",\n\"%s\"", LocateVar(modelVariableList.lData[varCounter])->GetName()->getStr()); if (vBounds(varCounter,0)>vBounds(varCounter,1)) { newBounds.Store (variableMap.lData[varCounter],0,0.); newBounds.Store (variableMap.lData[varCounter],1,1.); } else { newBounds.Store (variableMap.lData[varCounter],0,vBounds(varCounter,0)); newBounds.Store (variableMap.lData[varCounter],1,vBounds(varCounter,1)); } } fprintf (nnFile,"\n}}\n"); newBounds.toFileStr (nnFile); for (long i2 = 0; i2 < fullDimension; i2++) { matrixNet[i2]->save(nnFile); delete matrixNet [i2]; } fclose (nnFile); delete matrixNet; } else { errMsg = _String ("Failed to open ") & fName & " for writing"; } } else { errMsg = _String ("Invalid variable bounds in row ") & (k2+1) & " of the bounds matrix"; } } else { errMsg = *myName & " was not one of the model parameters"; } } else { errMsg = *matrix & " must be a have the same number of rows as the number of model parameters"; } } else { errMsg = *matrix & " must be a string matrix with 3 columns"; } } else { errMsg = *matrix & " was not the identifier of a valid matrix variable"; } } if (errMsg.sLength) { errMsg = errMsg & _String(" in call to TrainModelNN."); WarnError (errMsg); } }
void NLToConsole (void) { BufferToConsole ("\n"); }
void StringToConsole (_String & s, _SimpleList *) { BufferToConsole ((const char*)s.sData); }
int main( int argc, char *argv[] ) { #ifdef __HYPHYMPI__ int rank, size; MPI_Init (&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); _hy_mpi_node_rank = rank; setParameter (mpiNodeID, (_Parameter)rank); setParameter (mpiNodeCount, (_Parameter)size); if (rank == 0) #endif gtk_init (&argc, &argv); /* set up globals */ char curWd[4096]; getcwd (curWd,4096); _String baseDir (curWd); baseDir=baseDir&'/'; pathNames&& &baseDir; baseDirectory = baseDir; for (long i=1; i<argc;i++) { _String thisArg (argv[i]); if (thisArg.beginswith ("BASEPATH=")) { baseDirectory = thisArg.Cut(9,-1); if (baseDirectory.sLength) { if (baseDirectory.sData[baseDirectory.sLength-1]!='/') baseDirectory = baseDirectory&"/"; } } else if (thisArg.beginswith ("USEPATH=")) { _String baseArgDir (thisArg,8,-1); errorFileName = baseArgDir & errorFileName; messageFileName = baseArgDir & messageFileName; pathNames.Delete (0); pathNames&& &baseDir; } else if (thisArg.beginswith ("CPU=")) { #ifdef __MP__ _String cpus = thisArg.Cut(4,-1); systemCPUCount = cpus.toNum(); if (systemCPUCount<1) systemCPUCount = 1; #ifdef __MP2__ pthread_setconcurrency (systemCPUCount+1); #endif #endif } #ifdef __HYPHYMPI__ else if (thisArg == _String("MPIOPTIMIZER")) { mpiParallelOptimizer = true; setParameter (mpiNodeCount, 0.0); } else if (thisArg == _String("MPIPARTITIONS")) { mpiPartitionOptimizer = true; setParameter (mpiNodeCount, 0.0); } #endif } #ifdef __HYPHYMPI__ if (rank == 0) #endif { baseDir = baseDirectory & "GTKResources"; _List scanRes; ScanDirectoryForFileNames(baseDir,scanRes,false); if (scanRes.lLength == 0) { GtkWidget * noRez = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "HYPHY_GTK was unable to find a required GTKResources directory in %s. Please use BASEPATH= command line option to specify where the installation directory of HyPhy can be found.", baseDirectory.sData); gtk_dialog_run (GTK_DIALOG (noRez)); gtk_widget_destroy (noRez); return 1; } _String rcPath = baseDir & "/theme/theme.rc"; //printf ("Loading res files from %s\n", rcPath.sData); gtk_rc_parse (rcPath.sData); } GlobalStartup(); #ifdef __HYPHYMPI__ if (rank == 0) { #endif GdkDisplay * defDisplay = gdk_screen_get_display (gdk_screen_get_default()); hSizeCursor = gdk_cursor_new_for_display (defDisplay,GDK_SB_H_DOUBLE_ARROW); pickUpCursor = gdk_cursor_new_for_display (defDisplay,GDK_TARGET); dropOffCursor = gdk_cursor_new_for_display (defDisplay,GDK_TCROSS); screenPContext = gdk_pango_context_get_for_screen (gdk_screen_get_default()); tablePDMenuIcon = (GdkPixbuf*)ProcureIconResource(4020); /*{ GdkScreen * defD = gdk_screen_get_default(); fontConversionFactor = 72.27 / (gdk_screen_get_height (defD) *25.4 / gdk_screen_get_height_mm(defD)); printf ("Pango conversion factor computed at: %g\n", fontConversionFactor); }*/ ReadInTemplateFiles (); hyphyConsoleWindow = new _HYConsoleWindow ("HYPHY Console"); ReadPreferences (); SetStatusLine ("None","Idle","00:00:00"); while (gtk_events_pending()) gtk_main_iteration(); SetPreferences (); ReadGeneticCodes (); ReadModelTemplates (); ReadTreeProcessors (); MoveConsoleWindow (consolePositionRectangle); StringToConsole (hyphyCiteString); hyphyConsoleWindow->BringToFront(); #ifdef __HYPHYMPI__ { char statBuffer[1024]; sprintf (statBuffer,"MPI version of HyPhy running on %d nodes (a master and %d compute nodes) in %s mode\n", size, size-1, mpiPartitionOptimizer?"partition":(mpiParallelOptimizer?"rate heterogeneity":"normal")); BufferToConsole (statBuffer); } #endif g_timeout_add (100,GlobalQueueTimer,nil); g_timeout_add (1000,progressTimerFunction,nil); gtk_main (); WritePreferences(); #ifdef __HYPHYMPI__ } else // slave node { if (mpiParallelOptimizer || mpiPartitionOptimizer) mpiOptimizerLoop (rank, size); else mpiNormalLoop (rank, size, baseDir); } #endif GlobalShutdown(); return 0; }
void StringToConsole (_String & s) { BufferToConsole ((const char*)s.sData); }