コード例 #1
0
ファイル: constant.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_PMathObj _Constant::IGamma (_PMathObj arg)
{
    if (arg->ObjectClass()!=NUMBER) {
        _String errMsg ("A non-numerical argument passed to IGamma(a,x)");
        WarnError (errMsg);
        return new _Constant (0.0);
    }
    _Parameter x = ((_Constant*)arg)->theValue, sum=0.0;
    if (x>1e25) {
        x=1e25;
    } else if (x<0) {
        _String errMsg ("The domain of x is {x>0} for IGamma (a,x)");
        WarnError (errMsg);
        return new _Constant (0.0);
    } else if (x==0.0) {
        return new _Constant (0.0);
    }


    if (x<=theValue+1) // use the series representation
        // IGamma (a,x)=exp(-x) x^a \sum_{n=0}^{\infty} \frac{\Gamma((a)}{\Gamma(a+1+n)} x^n
    {
        _Parameter term = 1.0/theValue, den = theValue+1;
        long count = 0;
        while ((fabs(term)>=fabs(sum)*machineEps)&&(count<500)) {
            sum+=term;
            term*=x/den;
            den += 1.0;
            count++;
        }
    } else // use the continue fraction representation
        // IGamma (a,x)=exp(-x) x^a 1/x+/1-a/1+/1/x+/2-a/1+/2/x+...
    {
        _Parameter lastTerm = 0, a0 = 1.0, a1 = x, b0 = 0.0, b1 = 1.0, factor = 1.0, an, ana, anf;
        for (long count = 1; count<500; count++) {
            an = count;
            ana = an - theValue;
            a0 = (a1+a0*ana)*factor;
            b0 = (b1+b0*ana)*factor;
            anf = an*factor;
            a1  = x*a0+anf*a1;
            b1  = x*b0+anf*b1;
            if (a1!=0.0) {
                factor=1.0/a1;
                sum = b1*factor;
                if (fabs(sum-lastTerm)/sum<machineEps) {
                    break;
                }
                lastTerm = sum;
            }

        }
    }
    _Constant *result = (_Constant*)Gamma();
    result->SetValue(sum*exp(-x+theValue*log(x))/result->theValue);
    if (x>theValue+1) {
        result->SetValue (1.0-result->theValue);
    }
    return result;
}
コード例 #2
0
 bool    RegisterChangeListenerForDataFilter (long const index, BaseRef listener) {
     
   //StringToConsole(_String("RegisterChangeListenerForDataFilter ") & index & " " & (long)listener & "\n");
     
   if (_data_filters.IsValidIndex (index)) {
     
     if (dynamic_cast <_LikelihoodFunction*> (listener)) {
       _List * current_listeners = (_List *)_data_filter_listeners.GetDataByKey ((BaseRef)index);
       if (!current_listeners) {
         current_listeners = new _List;
         _data_filter_listeners.Insert ((BaseRef)index, (long)current_listeners, false, false);
       }
       
       if (current_listeners->_SimpleList::Find((long)listener) < 0L) {
         (*current_listeners) << listener;
       }
       return true;
       
     }
     
     
     WarnError (_String("Not a supported listener type in call to ") & _String (__PRETTY_FUNCTION__));
     
     
   }
   
   return false;
 }
コード例 #3
0
 void    _NotifyDataFilterListeners (const long index, kNotificationType event_type) {
   _List * listeners = (_List*)_data_filter_listeners.GetDataByKey( (BaseRef) index);
   if (listeners) {
     
    _List buffered_updates;
       
     for (unsigned long k = 0UL; k < listeners->lLength; k++) {
       BaseRef this_listener = listeners->GetItem(k);
       
       if (_LikelihoodFunction* lf = dynamic_cast<_LikelihoodFunction*> (this_listener)) {
           //StringToConsole(_String("_NotifyDataFilterListeners ") & index & " " & (long)lf & "\n");
      
         if (event_type == kNotificationTypeChange) {
           buffered_updates << lf;
           //lf->Rebuild();
              /* 20170328 SLKP: this COULD MODIFY the 'listeners' object, hence the buffering */
         } else if (event_type == kNotificationTypeDelete) {
           WarnError ("Attempted to delete a data set filter which is still being referenced by a likelihood function");
         }
       }
     }
       
       for (unsigned long k = 0UL; k < buffered_updates.lLength; k++) {
           ((_LikelihoodFunction*)buffered_updates.GetItem (k)) -> Rebuild();
       }
   }
 }
コード例 #4
0
ファイル: parser.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_PMathObj   FetchObjectFromVariableByTypeIndex (long idx, const unsigned long objectClass, long command_id, _String *errMsg)
{
    _Variable * v = FetchVar (idx);
    if (v && (objectClass == HY_ANY_OBJECT || v->ObjectClass () == objectClass)) {
        return v->GetValue();
    }
    if (command_id >= 0 || errMsg) {
        if (command_id >= 0) {
            WarnError (_String ("'") & *v->GetName() & ("' must refer to a ") & FetchObjectNameFromType (objectClass) & " in call to " 
                                     &_HY_ValidHBLExpressions.RetrieveKeyByPayload(command_id) & '.');
        } else {
            WarnError (errMsg->Replace ("_VAR_NAME_ID_", *v->GetName(), true));
        }
    }    
    return nil;
}
コード例 #5
0
ファイル: error.cpp プロジェクト: mdsmith/OCLHYPHY
void	warnError (long errCode)
{
	if (errCode == -108)
		warnError (DecodeError (errCode)&_String(" Exiting..."));
	else
		WarnError (DecodeError (errCode)&_String(" Exiting..."));
}
コード例 #6
0
void _hyExecutionContext::ReportError (_String errText) {
    if (errMsg) {
        *errMsg = *errMsg & errText & ".\n";
    } else {
        WarnError (errText);
    }
    
}
コード例 #7
0
 unsigned long  CountObjectsByType (const long type) {
   switch (type) {
     case HY_BL_DATASET_FILTER:
       return _data_filters.countitems();
       break;
   }
   WarnError (_String("Called ") & __PRETTY_FUNCTION__ & " with an unsupported type");
   return 0UL;
 }
コード例 #8
0
 AVLListXLIterator  ObjectIndexer (const long type) {
   switch (type) {
     case HY_BL_DATASET_FILTER:
       return AVLListXLIterator (&_data_filters);
       break;
   }
   WarnError (_String("Called ") & __PRETTY_FUNCTION__ & " with an unsupported type");
   return AVLListXLIterator (nil);
 }
コード例 #9
0
 long    StoreDataFilter (_String const& name, _DataSetFilter* object, bool handle_errors) {
   
   if (name.IsValidIdentifier(true)) {
     long exists_already = FindDataFilter(name);
     
     /*printf ("[StoreDataFilter] %s %d\n", name.sData, exists_already);
      
      _SimpleList history;
      long locked_index = _data_filter_locks.Next (-1, history);
      while (locked_index >= 0) {
      printf ("\tLOCKED %s\n", GetFilterName((long)_data_filter_locks.Retrieve(locked_index))->sData);
      locked_index = _data_filter_locks.Next (locked_index, history);
      } */
     
     if (exists_already >= 0L) {
       if (_IsObjectLocked(exists_already, HY_BL_DATASET_FILTER)) {
         if (handle_errors) {
           WarnError (_String ("DataSetFilter ") & name.Enquote() & " could not be created because an existing filter of the same name is locked");
         }
         return -1;
       }
       
       //DeleteObject ((_DataSetFilter*)_data_filters.GetXtra (exists_already));
       _data_filters.SetXtra(exists_already, object, false); // this will delete the existing object
       _NotifyDataFilterListeners (exists_already, kNotificationTypeChange);
       
     } else {
       exists_already = _data_filters.Insert (new _String(name), (long)object, false, false);
     }
     
     
     
     _SetDataFilterParameters (name, *object);
     return exists_already;
   } else {
     if (handle_errors) {
       WarnError (_String ("The name ") & name.Enquote() & " is not a valid HyPhy id in call to " & __PRETTY_FUNCTION__);
     }
   }
   return -1;
 }
コード例 #10
0
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;
}
コード例 #11
0
ファイル: hyphyunixutils.cpp プロジェクト: mdsmith/OCLHYPHY
_String*	StringFromConsole	(bool)
{
	_String * returnme = new _String (32L, true);
	#ifndef __HEADLESS__
		int		  readAChar;
		while 	 ((readAChar = getc(stdin)) != '\n')
			*returnme << readAChar;
	#else
		WarnError ("Unhandled standard input interaction in StringFromConsolel for headless HyPhy");
		return;
	#endif
	returnme->Finalize ();
	return returnme;
}
コード例 #12
0
ファイル: parser.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_Variable* CheckReceptacleCommandID (_String* name, const long id, bool checkValid, bool isGlobal)
{
    if (checkValid && (!name->IsValidIdentifier())) {
        WarnError (_String ("'") & *name & "' is not a variable identifier in call to " & _HY_ValidHBLExpressions.RetrieveKeyByPayload(id) & '.');
        return nil;
    }
    
    long    f = LocateVarByName (*name);
    if (f<0) {
        _Variable dummy (*name, isGlobal);
        f = LocateVarByName (*name);
    }
    
    return FetchVar(f);
}
コード例 #13
0
ファイル: parser.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_Variable* CheckReceptacle (_String* name, _String fID, bool checkValid, bool isGlobal)
{
    if (checkValid && (!name->IsValidIdentifier())) {
        _String errMsg = *name & " is not a variable identifier in call to " & fID;
        WarnError (errMsg);
        return nil;
    }

    long    f = LocateVarByName (*name);
    if (f<0) {
        _Variable dummy (*name, isGlobal);
        f = LocateVarByName (*name);
    }

    return FetchVar(f);
}
コード例 #14
0
ファイル: parser.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
void  InsertVar (_Variable* theV)
{
    long pos = variableNames.Insert (theV->theName);

    /*if (theV->GetName()->Equal (&_String("PS_2")))
    {
        printf ("Making...\n");
    }*/


    if (pos < 0 && isDefiningATree > 1)
        // automatically fix duplicate autogenerated tree node name
    {
        long trySuffix  = 1;
        _String * tryName = new _String;
        do {
            *tryName = *theV->theName & "_" & trySuffix;
            pos      = variableNames.Insert (tryName);
            trySuffix ++;
        } while (pos < 0);
        DeleteObject(theV->theName);
        theV->theName = tryName;
    }

    if (pos < 0) {
        if (isDefiningATree == 1) {
            _String errMsg (*theV->GetName());
            errMsg = errMsg& " is already being used - please rename one of the two variables.";
            WarnError(errMsg);
        }

        theV->theIndex = variableNames.GetXtra(-pos-1);
        return;
    } else {
        theV->theName->nInstances++;
    }

    if (freeSlots.lLength) {
        theV->theIndex = freeSlots.lData[freeSlots.lLength-1];
        variablePtrs[theV->theIndex]=theV->makeDynamic();
        freeSlots.Delete(freeSlots.lLength-1);
    } else {
        theV->theIndex = variablePtrs.lLength;
        variablePtrs&&theV;
    }
    variableNames.SetXtra (pos, theV->theIndex);
}
コード例 #15
0
_String WriteFileDialogInput(void) {
    if (currentExecutionList && currentExecutionList->stdinRedirect) {
        _String outS (currentExecutionList->FetchFromStdinRedirect());
        if (outS.sLength) {
            return outS;
        }
    }
    
    defFileNameValue = ProcessLiteralArgument (&defFileString,nil);
    _String resolvedFilePath;
    
#ifdef __HEADLESS__
    WarnError ("Unhandled standard input call in headless HYPHY. Only redirected standard input (via ExecuteAFile) is allowed");
    return empty;
#else
  #ifdef __MAC__
      resolvedFilePath =  MacSimpleFileSave();
  #endif

  #ifdef __WINDOZE__

      resolvedFilePath = ReturnFileDialogSelectionWin(true);
  #endif

  #ifdef __HYPHY_GTK__
      if (PopUpFileDialog (dialogPrompt)) {
          resolvedFilePath = *argFileName;
      } 
  #endif
    #ifdef __HYPHYQT__  
        resolvedFilePath = _hyQTFileDialog (dialogPrompt,defFileNameValue, true);
    #endif
            
    #if defined __UNIX__ && ! defined __HYPHYQT__ && ! defined __HYPHY_GTK__
        resolvedFilePath = ReturnDialogInput(true);
    #endif
#endif
    
    if (resolvedFilePath.sLength == 0) {
        terminateExecution = true;
    }
    defFileNameValue = empty;
    return resolvedFilePath;

}
コード例 #16
0
//________________________________________________________
Ptr     ProcureIconResource (long id)
{
    long cid = loadedXPMs.Find ((BaseRef)id);
    if (cid < 0) {
        _String picFileName = libDirectory & _String("GTKResources/") & id & ".png";
        GError * pixBufError = nil;
        GdkPixbuf*  thePixMap = gdk_pixbuf_new_from_file  (picFileName.sData, &pixBufError);
        if (!thePixMap) {
            picFileName = _String("Failed to load a pixbuf from file. GTK Error ") & pixBufError -> message;
            WarnError (picFileName);
            g_error_free (pixBufError);
            return nil;
        }
        cid = loadedXPMs.Insert ((BaseRef)id, (long)thePixMap);
    }

    return (Ptr)loadedXPMs.GetXtra(cid);
}
コード例 #17
0
 bool    UnregisterChangeListenerForDataFilter (long const index, BaseRef listener) {
   if (_data_filters.IsValidIndex (index)) {
     
     if (dynamic_cast <_LikelihoodFunction*> (listener)) {
       _List * current_listeners = (_List *)_data_filter_listeners.GetDataByKey ((BaseRef)index);
       if (current_listeners) {
         long listener_index = current_listeners->_SimpleList::Find((long)listener);
         if (listener_index >= 0) {
           //StringToConsole(_String("UnregisterChangeListenerForDataFilter ") & index & " " & (long)listener & "\n");
           current_listeners->Delete (listener_index);
           return true;
         }
       }
     }
     WarnError (_String("Not a supported listener type in call to ") & _String (__PRETTY_FUNCTION__));
   }
   
   return false;
 }
コード例 #18
0
ファイル: likefunc2.cpp プロジェクト: mdsmith/OCLHYPHY
bool	_LikelihoodFunction::ProcessPartitionList (_SimpleList& partsToDo, _Matrix* partitionList, _String caller)
{	
	long	partCount = CountObjects(0);
	partsToDo.Populate (partCount, 0, 1);
	if (partitionList)
	{
		partitionList->ConvertToSimpleList (partsToDo);
		DeleteObject (partitionList);
		partsToDo.Sort();
		partsToDo.FilterRange (-1, partCount);
		if (partsToDo.lLength == 0)
		{
			WarnError (_String("An invalid partition specification in call to ") & caller);
			return nil;
		}
	}

	return true;
}
コード例 #19
0
ファイル: constant.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_PMathObj _Constant::Beta (_PMathObj arg)
{
    if (arg->ObjectClass()!=NUMBER) {
        WarnError ("A non-numerical argument passed to Beta(x,y)");
        return    nil;
    }
    
    _Constant xy         = _Constant (theValue + ((_Constant*)arg)->theValue);
    
    _Constant * lnGammaX    = (_Constant *)LnGamma(),
              * lnGammaY    = (_Constant *)arg->LnGamma(),
              * lnGammaXY   = (_Constant *)xy.LnGamma(),
              * result      = new _Constant (exp (lnGammaX->theValue + lnGammaY->theValue - lnGammaXY->theValue));
         
    DeleteObject (lnGammaX);
    DeleteObject (lnGammaY);
    DeleteObject (lnGammaXY);
    
    return result;
}
コード例 #20
0
ファイル: parser.cpp プロジェクト: Dietermielke/hyphy
//__________________________________________________________________________________
_Variable* CheckReceptacleCommandID (_String* name, const long id, bool checkValid, bool isGlobal, _ExecutionList* context)
{
    if (checkValid && (!name->IsValidIdentifier())) {
        _String errMsg = _String ("'") & *name & "' is not a valid variable identifier in call to " & _HY_ValidHBLExpressions.RetrieveKeyByPayload(id) & '.';
        if (context) {
            context->ReportAnExecutionError(errMsg);
        } else {
            WarnError (errMsg);
        }
        return nil;
    }
    
    long    f = LocateVarByName (*name);
    if (f<0) {
        _Variable dummy (*name, isGlobal);
        f = LocateVarByName (*name);
    }
    
    return FetchVar(f);
}
コード例 #21
0
_String*    StringFromConsole   (bool)
{
    fflush(stdout);
    _String * returnme = new _String (32L, true);
#if not defined __HEADLESS__ && not defined _MINGW32_MEGA_
    int       readAChar;
    while    ((readAChar = getc(stdin)) != '\n') {
        if (readAChar == EOF) {
            CheckReceptacleAndStore (&hasEndBeenReached,empty,false,new _Constant (1.), false);
            break;
        }
        *returnme << readAChar;
    }
#else
    WarnError ("Unhandled standard input interaction in StringFromConsole for headless HyPhy");
    return NULL;
#endif
    returnme->Finalize ();
    return returnme;
}
コード例 #22
0
ファイル: likefunc2.cpp プロジェクト: mdsmith/OCLHYPHY
	void MPISwitchNodesToMPIMode (long totalNodeCount)
	{
		_String message = mpiLoopSwitchToOptimize & hyphyMPIOptimizerMode;
		
			// send a context switch signal
		for (long ni = 1; ni <= totalNodeCount; ni++)
			MPISendString (message, ni);
		// receive confirmation of successful switch
		for (long ni = 1; ni <= totalNodeCount; ni++)
		{
			long fromNode = ni;
			_String t (MPIRecvString (ni,fromNode));
			if (!t.Equal (&mpiLoopSwitchToOptimize))
			{
				WarnError (_String("[MPI] Failed to confirm MPI mode switch at node ") & ni);
				return;
			}
			else
				ReportWarning (_String("[MPI] Successful mode switch to mode ") & hyphyMPIOptimizerMode & " confirmed from node " & ni);	
		}
	}
コード例 #23
0
ファイル: constant.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_PMathObj _Constant::Raise (_PMathObj theObj)
{
    if (!theObj) {
        return nil;
    }

    _Parameter    base  = Value(),
                  expon = theObj->Value();

    if (base>0.0) {
        return    new  _Constant (exp (log(base)*(expon)));;
    } else {
        if (base<0.0)
            if (CheckEqual (expon, (long)expon)) {
                return new _Constant (((((long)expon)%2)?-1:1)*exp (log(-base)*(expon)));
            } else {
                _String errMsg ("An invalid base/exponent pair passed to ^");
                WarnError (errMsg.sData);
            }

        return     new _Constant (0.0);
    }
}
コード例 #24
0
ファイル: HYNetInterface.cpp プロジェクト: Dietermielke/hyphy
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);
    }
}
コード例 #25
0
ファイル: likefunc2.cpp プロジェクト: mdsmith/OCLHYPHY
_Parameter _LikelihoodFunction::SumUpSiteLikelihoods (long index, const _Parameter * patternLikelihoods, const _SimpleList& patternScalers) 
/* 
 compute the likelihood of a partition (index), corrected for scaling, 
 by summing pattern likelihoods from patternLikelihoods, weighted by pattern frequencies
 and corrected for scaling factors from patternScalers
*/
{
	
	_Parameter		 logL			  = 0.;
	_SimpleList		 *catVarType	  = (_SimpleList*)((*(_List*)categoryTraversalTemplate(index))(4));
	long			 cumulativeScaler = 0,
					 categoryType     = catVarType->Element (-1);
	
	_SimpleList		* patternFrequencies = &((_DataSetFilter*)dataSetFilterList (theDataFilters(index)))->theFrequencies;
					 
	// check to see if we need to handle HMM or COP variables
	if (categoryType & _hyphyCategoryHMM)
	{
		_CategoryVariable*hmmVar = (_CategoryVariable*)((*(_List*)(*(_List*)categoryTraversalTemplate(index))(0))(0));
		_Matrix			 *hmm    = hmmVar->ComputeHiddenMarkov(),
						 *hmf    = hmmVar->ComputeHiddenMarkovFreqs();	
		
		_SimpleList		 *dmap	 = &((_DataSetFilter*)dataSetFilterList (theDataFilters(index)))->duplicateMap;
		
		return			 SumUpHiddenMarkov (patternLikelihoods, 
											*hmm, 
											*hmf, 
											dmap,
											&patternScalers,
											patternFrequencies->lLength
											);
	}
	else 	
	{
		if (categoryType & _hyphyCategoryCOP)
		{
			WarnError ("Constant-on-partition categories are currently not supported by the evaluation engine");
		}
		else // simple sum clause

		{
			for				 (long patternID = 0; patternID < patternFrequencies->lLength; patternID++)
			{
				long patternFrequency = patternFrequencies->lData[patternID];
				if (patternFrequency > 1)
				{
					logL			 += myLog(patternLikelihoods[patternID])*patternFrequency;
					cumulativeScaler += patternScalers.lData[patternID]*patternFrequency;
				}
				else
				// all this to avoid a double*long multiplication
				{
					logL			 += myLog(patternLikelihoods[patternID]);
					cumulativeScaler += patternScalers.lData[patternID];			
				}
			}
		}
	}
	
	return logL - cumulativeScaler * _logLFScaler;
		
}
コード例 #26
0
ファイル: likefunc2.cpp プロジェクト: mdsmith/OCLHYPHY
//_______________________________________________________________________________________
void			_LikelihoodFunction::SetupCategoryCaches	  (void)
{
	categoryTraversalTemplate.Clear();
	for (long partIndex = 0; partIndex < theDataFilters.lLength; partIndex++)
		if (blockDependancies.lData[partIndex] == 0)
		{
			_List * noCatVarList = new _List;
			noCatVarList->AppendNewInstance (new _List);
			noCatVarList->AppendNewInstance (new _SimpleList((long)1L));
			noCatVarList->AppendNewInstance (new _SimpleList((long)1L));
			noCatVarList->AppendNewInstance (new _SimpleList());
			noCatVarList->AppendNewInstance (new _SimpleList((long)0L));
			categoryTraversalTemplate.AppendNewInstance (noCatVarList);
		}
		else
		{
			_SimpleList		  myCats;
			PartitionCatVars  (myCats, partIndex);
			_List*			  catVarReferences = new _List,
				 *			  container		   = new _List;
			
			_SimpleList *	  catVarCounts	   = new _SimpleList,
						*	  catVarOffsets	   = new _SimpleList (myCats.lLength,1,0),
						*	  hmmAndCOP		   = new _SimpleList (),
						*     varType		   = new _SimpleList (myCats.lLength,1,0);
			
			long			  totalCatCount	   = 1L,
							  hmmCatCount      = 1L,
							  catVarFlags	   = 0L,
							  varIndex;
			
			for ( varIndex = 0; varIndex < myCats.lLength; varIndex++)
			{
				_CategoryVariable * aCV = (_CategoryVariable *)LocateVar (myCats.lData[varIndex]);
				(*catVarReferences) << aCV;
				long				intervalCount = aCV->GetNumberOfIntervals();
				(*catVarCounts)		<< intervalCount;
                
				if (aCV->IsHiddenMarkov() || aCV->IsConstantOnPartition())
				{
					if (aCV->IsConstantOnPartition())
					{
						if (catVarFlags & (_hyphyCategoryCOP|_hyphyCategoryHMM))
							break;
						varType->lData[varIndex] = _hyphyCategoryCOP;
					}
					else
					{
						if (catVarFlags & (_hyphyCategoryCOP|_hyphyCategoryHMM))
							break;
						varType->lData[varIndex] = _hyphyCategoryHMM;
					}
					
					(*hmmAndCOP) << intervalCount;
					hmmCatCount *= intervalCount;
				}
				else
				    varType->lData[varIndex] = _hyphyCategoryNormal;
				
				catVarFlags |= varType->lData[varIndex];
				totalCatCount		*= intervalCount;
			}
			
			if (varIndex <  myCats.lLength)
			{
				WarnError ("Currently, HyPhy can support at most one HMM or Constant on Partition variable per partition");
				return;
			}
			
			(*catVarCounts) << totalCatCount;
			(*varType)		<< catVarFlags;
			
			for (long varIndex = myCats.lLength-2; varIndex >= 0; varIndex--)
				catVarOffsets->lData[varIndex] = catVarOffsets->lData[varIndex+1]*catVarCounts->lData[varIndex+1];
            
 			for (long varIndex = hmmAndCOP->lLength-2; varIndex >= 0; varIndex--)
				hmmAndCOP->lData[varIndex] *= hmmAndCOP->lData[varIndex+1];

			if (hmmAndCOP->lLength)
				(*hmmAndCOP) << hmmCatCount;
			
			container->AppendNewInstance (catVarReferences);
			container->AppendNewInstance (catVarCounts);
			container->AppendNewInstance (catVarOffsets);
			container->AppendNewInstance (hmmAndCOP);
			container->AppendNewInstance (varType);
			
			((_TheTree*)LocateVar(theTrees(partIndex)))->SetupCategoryMapsForNodes(*catVarReferences,*catVarCounts,*catVarOffsets);
			
			categoryTraversalTemplate.AppendNewInstance(container);
		}
	
	if (indexCat.lLength)
	{
		if (siteResults)
			DeleteObject (siteResults);
		AllocateSiteResults();
	}
}
コード例 #27
0
ファイル: variable.cpp プロジェクト: jjkee/hyphy
void  _Variable::SetFormula (_Formula& theF) // set the value of the var to a formula
{
    bool changeMe    = false,
         isAConstant = theF.IsAConstant();

    _Formula* myF = &theF;

    if (isAConstant) {
        _PMathObj theP = theF.Compute();
        if (theP) {
            myF = new _Formula ((_PMathObj)theP->makeDynamic(),false);
            checkPointer (myF);
        } else {
            return;
        }
    }

    _SimpleList vars;
    {
        _AVLList vA (&vars);
        theF.ScanFForVariables (vA,true);
        vA.ReorderList();
    }

    if (vars.BinaryFind(theIndex)>=0) {
        _String * sf = (_String*)theF.toStr();
        WarnError ((_String("Can't set variable ")&*GetName()&" to "&*sf&" because it would create a circular dependance."));
        DeleteObject(sf);
        if (&theF!=myF) {
            delete myF;
        }
        return;
    }

    varFlags &= HY_VARIABLE_SET;

    if (varFlags & HY_VARIABLE_CHANGED) {
        varFlags -= HY_VARIABLE_CHANGED;
    }


    if (varFormula) {
        delete (varFormula);
        varFormula = nil;
    } else {
        changeMe = true;
    }

    if (varValue) {
        DeleteObject (varValue);
        varValue=nil;
    }

    //_Formula::Duplicate ((BaseRef)myF);
    varFormula = new _Formula;
    varFormula->Duplicate ((BaseRef)myF);

    // mod 20060125 added a call to simplify constants
    varFormula->SimplifyConstants ();

    // also update the fact that this variable is no longer independent in all declared
    // variable containers which contain references to this variable
    if (changeMe)
        if (deferSetFormula) {
            *deferSetFormula << theIndex;
            deferIsConstant  << isAConstant;
        } else {
            long i;
            _SimpleList tcache;
            long        iv;

            i = variableNames.Traverser (tcache,iv,variableNames.GetRoot());

            for (; i >= 0; i = variableNames.Traverser (tcache,iv)) {
                _Variable* theV = FetchVar(i);
                if (theV->IsContainer()) {
                    _VariableContainer* theVC = (_VariableContainer*)theV;
                    if (theVC->SetDependance(theIndex) == -2) {
                        ReportWarning ((_String("Can't make variable ")&*GetName()&" dependent in the context of "&*theVC->GetName()&" because its template variable is bound by another relation in the global context."));
                        continue;
                    }
                }
            }
            {
                for (long i = 0; i<likeFuncList.lLength; i++)
                    if (((_String*)likeFuncNamesList(i))->sLength) {
                        ((_LikelihoodFunction*)likeFuncList(i))->UpdateIndependent(theIndex,isAConstant);
                    }
            }
        }

    if (&theF!=myF) {
        delete myF;
    }
}
コード例 #28
0
ファイル: error.cpp プロジェクト: mdsmith/OCLHYPHY
void	acknError (const char* theError)
{
	WarnError (theError);
}
コード例 #29
0
ファイル: constant.cpp プロジェクト: HyperionRiaz/hyphy
//__________________________________________________________________________________
_PMathObj _Constant::IBeta (_PMathObj arg1, _PMathObj arg2)
{
    if (theValue<=0.0) {
        if (theValue < 0.0) {
            _String     errMsg;
            errMsg = _String ("IBeta is defined for x betweeen 0 and 1. Had: ") & theValue;
            ReportWarning   (errMsg);
        }
        return new _Constant (0.0);
    }

    if (theValue>=1.0) {
        if (theValue>1.0) {
            _String     errMsg;
            errMsg = _String ("IBeta is defined for x betweeen 0 and 1. Had: ") & theValue;
            ReportWarning   (errMsg);
        }
        return new _Constant (1.0);
    }


    if ((arg1->ObjectClass()!=NUMBER)||(arg2->ObjectClass()!=NUMBER)) {
        _String     errMsg ("IBeta called with a non-scalar argument.");
        WarnError   (errMsg);
        return      nil;
    }

    _Constant       *ga = (_Constant*)arg1->Gamma(),
                     *gb = (_Constant*)arg2->Gamma();

    if (ga&&gb) {
        _Constant   *ac = (_Constant*)arg1,
                     *bc = (_Constant*)arg2;

        _Parameter  a = ac->Value(),
                    b = bc->Value(),
                    x = theValue,
                    aa,
                    c,
                    d,
                    del,
                    h,
                    qab,
                    qam,
                    qap,
                    FPMIN = 1e-100;

        bool        swap = false;

        long        m,
                    m2;

        if (x >= (a+1.)/(a+b+2.)) {
            swap = true;
            c = b;
            b = a;
            a = c;
            x = 1. - x;
        }

        qab = a+b;
        qap = a+1.;
        qam = a-1.;
        c   = 1.;
        d   = 1. - qab*x/qap;
        if  ((d<FPMIN)&&(d>-FPMIN)) {
            d = FPMIN;
        }
        d   = 1./d;
        h   = d;

        for (m=1; m<100; m++) {
            m2 = 2*m;
            aa = m*(b-m)*x / ((qam+m2)*(a+m2));
            d = 1.+aa*d;
            if  ((d<FPMIN)&&(d>-FPMIN)) {
                d = FPMIN;
            }
            c = 1.+aa/c;
            if  ((c<FPMIN)&&(c>-FPMIN)) {
                c = FPMIN;
            }
            d = 1./d;
            h*= d*c;
            aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
            d = 1.+aa*d;
            if  ((d<FPMIN)&&(d>-FPMIN)) {
                d = FPMIN;
            }
            c = 1.+aa/c;
            if  ((c<FPMIN)&&(c>-FPMIN)) {
                c = FPMIN;
            }
            d = 1./d;
            del = d*c;
            h*= del;
            del -= 1.;
            if  ((del<1.e-14)&&(del>-1.e-14))   {
                break;
            }
        }

        _Constant   * res = new _Constant (a+b);
        ac  = (_Constant*)res->Gamma();
        c   = ac->Value()/(ga->Value()*gb->Value()) *
              exp (a*log(x)+b*log(1-x));

        if (swap) {
            res->theValue = 1.-c*h/a;
        } else {
            res->theValue = c*h/a;
        }

        DeleteObject (ac);
        DeleteObject (ga);
        DeleteObject (gb);
        return  res;
    }
    DeleteObject (ga);
    DeleteObject (gb);
    return nil;
}