StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes
StochHMM::weight Class Reference

weight class contains scaling factors for external scores More...

#include <weight.h>

List of all members.

Public Member Functions

 weight ()
 ~weight ()
bool isAbsolute ()
double getAbsolute ()
std::string & getName ()
double getWeightedScore (double)
size_t size ()
 Given a score (x) it will map the score to F(x)
void print ()
std::string stringify ()
void setAbsolute (double val)
void setWeights (std::vector< double > &, std::vector< double > &, std::pair< double, double > *, std::pair< double, double > *)
void setWeights (std::vector< double > &, std::vector< double > &)
void setMaxWeight (double &, double &)
void setMinWeight (double &, double &)
void setName (std::string &nm)
bool parse (const std::string &)
 Parse weight from string.

Private Attributes

bool absolute
double absoluteValue
 Stores whether Weight is a constant value.
std::string name
 Stores the constant value.
std::pair< double, double > * maxValue
std::pair< double, double > * minValue
 Stores the MAX value of x and the corresponding F(x)
std::vector< std::pair< double,
double > > * 
vals
 Stores the MIN value of x and the corresponding F(x)

Detailed Description

weight class contains scaling factors for external scores

weight class contains either an *1. Absolute scaling value: meaning the returned value multiplied by constant *2. Score Mapping: x and y values describing the transformation from x=returned score to y the scaled score. If the returned value isn't equal to a specific y the score is interpolated or extrapolated. If the return score x<MIN then="" y="scaled" min="" if="" the="" return="" score="" x>="">MAX then y=scaled MAX Scores provided to weight should be log(Prob) It will return a log(Prob) scaled score

Definition at line 56 of file weight.h.


Constructor & Destructor Documentation

StochHMM::weight::weight ( )

Definition at line 32 of file weight.cpp.

References absolute, absoluteValue, maxValue, minValue, and vals.

{
absolute=false;
absoluteValue=-INFINITY;
maxValue=NULL;
minValue=NULL;
vals=NULL;
}
StochHMM::weight::~weight ( )

Definition at line 40 of file weight.cpp.

References maxValue, minValue, and vals.

{
delete maxValue;
delete minValue;
delete vals;
maxValue=NULL;
minValue=NULL;
vals=NULL;
return;
}

Member Function Documentation

double StochHMM::weight::getAbsolute ( )
inline

Definition at line 69 of file weight.h.

References absoluteValue.

Referenced by StochHMM::transitionFuncParam::stringify(), and StochHMM::emissionFuncParam::stringify().

{ return absoluteValue;};
std::string& StochHMM::weight::getName ( )
inline
double StochHMM::weight::getWeightedScore ( double  score)

Returns F(x) given some score x

Parameters:
scoreThe value returned by some function
Returns:
double F(x) which interpolated or extrapolated as necessary

Definition at line 127 of file weight.cpp.

References absolute, absoluteValue, StochHMM::compXval(), StochHMM::extrapolate(), StochHMM::interpolate(), maxValue, minValue, and vals.

Referenced by StochHMM::emissionFuncParam::evaluate().

{
//Find score that flanks or is equal to the score we supply
//If there is one equal to then we return it, otherwise
//we get the two values and interpolate the score linearly
//unless we are on the ends then we extrapolate
if (absolute){
return score + absoluteValue;
}
else if (maxValue!=NULL && score > maxValue->first){
return maxValue->second;
}
else if (maxValue!=NULL && score < minValue->first){
return minValue->second;
}
else{
std::vector<std::pair<double,double> >::iterator it;
std::pair<double,double> temp (score,0);
it=lower_bound(vals->begin(),vals->end(),temp ,compXval);
//std::cout << (*it).first << "\t" << exp((*it).first) <<std::endl;
if ((*it).first==score){
return (*it).second;
}
else if (it==vals->begin()){ //extrapolate based on beginning
return extrapolate((*it),(*(it+1)),score);
}
else if (it==vals->end()){ //extrapolate based on end
return extrapolate((*(it-2)), (*(it-1)),score);
}
else{
return interpolate((*(it-1)),(*it),score);
}
}
}
bool StochHMM::weight::isAbsolute ( )
inline

Checks to see if weight is defined as a constant

Returns:
bool TRUE if weight is defined as a constant, else FALSE

Definition at line 67 of file weight.h.

References absolute.

Referenced by StochHMM::transitionFuncParam::stringify(), and StochHMM::emissionFuncParam::stringify().

{return absolute;};
bool StochHMM::weight::parse ( const std::string &  txt)

Parse weight from string.

Definition at line 201 of file weight.cpp.

References StochHMM::clear_whitespace(), StochHMM::max(), StochHMM::min(), name, StochHMM::stringList::push_back(), setAbsolute(), setMaxWeight(), setMinWeight(), setWeights(), StochHMM::stringList::size(), StochHMM::stringList::splitString(), and StochHMM::stringToDouble().

Referenced by StochHMM::model::_parseScaling().

{
stringList lst;
lst.splitString(txt,"\n");
bool table=false;
std::vector<double> xVal;
std::vector<double> yVal;
bool min=false;
double minXVal(0);
double minYVal(0);
bool max=false;
double maxXVal(0);
double maxYVal(0);
for(size_t i=0;i<lst.size();i++){
stringList line;
clear_whitespace(lst[i],":[]\n ");
line.splitString(lst[i],"\t,");
if (line.size()==0){
std::cerr << "Unable to parse weight line: " << lst[i] << std::endl;
return false;
}
if (line[0].compare("SCALE")==0){
if (line.size()==2){
name=line[1];
}
else{
std::cerr << "Weight Scale is missing a name" << std::endl;
return false;
}
}
else if (line[0].compare("ABSOLUTE")==0){
if (line.size()<2){
std::cerr << "Weight Absolute value is missing " << std::endl;
return false;
}
double val;
if (!stringToDouble(line[1], val)){
std::cerr << "Weighted Absoolute value not numeric: " << line[1] << std::endl;
return false;
}
return true;
}
else if (line[0].compare("VALUE")==0){
if (line.size()<3){
std::cerr << "Weight VALUE line is missing values" << std::endl;
return false;
}
bool px=line[1].compare("P(X)")==0;
for(size_t i=2;i<line.size();i++){
double tempValue;
if (!stringToDouble(line[i], tempValue)){
std::cerr << "Weighted VALUE not numeric: " << line[i] << std::endl;
return false;
}
if (px){
xVal.push_back(log(tempValue));
}
else{
xVal.push_back(tempValue);
}
}
table=true;
}
else if (line[0].compare("SCALED")==0){
if (line.size()<3){
std::cerr << "Weight SCALED line is missing values" << std::endl;
return false;
}
bool px = line[1].compare("P(X)")==0;
for(size_t i=2;i<line.size();i++){
double tempValue;
if (!stringToDouble(line[i], tempValue)){
std::cerr << "Weighted SCALED value not numeric: " << line[i] << std::endl;
return false;
}
if (px){
yVal.push_back(log(tempValue));
}
else{
yVal.push_back(tempValue);
}
}
table=true;
}
else if (line[0].compare("MIN_VALUE")==0){
if (line.size()<3){
std::cerr << "Weight MIN_VALUE line is missing values" << std::endl;
return false;
}
double tempValue;
if (!stringToDouble(line[2], tempValue)){
std::cerr << "Weighted VALUE not numeric: " << line[i] << std::endl;
return false;
}
if (line[1].compare("P(X)")==0){
minXVal=log(tempValue);
}
else{
minXVal=tempValue;
}
min=true;
}
else if (line[0].compare("MIN_SCALED")==0){
if (line.size()<3){
std::cerr << "Weight MIN_SCALED line is missing values" << std::endl;
return false;
}
double tempValue;
if (!stringToDouble(line[2], tempValue)){
std::cerr << "Weighted MIN_SCALED value not numeric: " << line[i] << std::endl;
return false;
}
if (line[1].compare("P(X)")==0){
minYVal=log(tempValue);
}
else{
minYVal=tempValue;
}
min=true;
}
else if (line[0].compare("MAX_VALUE")==0){
if (line.size()<3){
std::cerr << "Weight MAX_VALUE line is missing values" << std::endl;
return false;
}
double tempValue;
if (!stringToDouble(line[2], tempValue)){
std::cerr << "Weighted MAX_VALUE value not numeric: " << line[i] << std::endl;
return false;
}
if (line[1].compare("P(X)")==0){
maxXVal=log(tempValue);
}
else{
maxXVal=tempValue;
}
max=true;
}
else if (line[0].compare("MAX_SCALED")==0){
if (line.size()<3){
std::cerr << "Weight MAX_SCALED line is missing values" << std::endl;
return false;
}
double tempValue;
if (!stringToDouble(line[2], tempValue)){
std::cerr << "Weighted MAX_SCALED value not numeric: " << line[i] << std::endl;
return false;
}
if (line[1].compare("P(X)")==0){
maxYVal=log(tempValue);
}
else{
maxYVal=tempValue;
}
max=true;
}
}
if (table && (yVal.size()==xVal.size())){
setWeights(xVal, yVal);
}
else{
std::cerr << "Scaling Values entries don't match up. They are of different sizes" <<std::endl;
return false;
}
if (min){
setMinWeight(minXVal, minYVal);
}
if (max){
setMaxWeight(maxXVal, maxYVal);
}
return true;
}
void StochHMM::weight::print ( )
inline

Prints out the weight type to text. Includes the x and F(x) vectors and MIN and MAX

Definition at line 89 of file weight.h.

References stringify().

{std::cout << stringify() << std::endl;};
void StochHMM::weight::setAbsolute ( double  val)
inline

Sets the scaling factor to a constant value

Parameters:
valdouble that is returned x->Constant

Definition at line 103 of file weight.h.

References absolute, and absoluteValue.

Referenced by StochHMM::transitionFuncParam::parse(), StochHMM::emissionFuncParam::parse(), and parse().

{absoluteValue=val; absolute=true; return;};
void StochHMM::weight::setMaxWeight ( double &  x,
double &  y 
)

Sets the MAX value for x to MAX F(MAX(x));

Parameters:
xThe MAX x value to allow
yThe value to return for x>=MAX

Definition at line 50 of file weight.cpp.

References maxValue.

Referenced by parse().

{
if (maxValue!=NULL){
delete maxValue;
}
maxValue=new(std::nothrow) std::pair<double,double>;
if (maxValue==NULL){
std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
exit(1);
}
maxValue->first=x;
maxValue->second=y;
return;
}
void StochHMM::weight::setMinWeight ( double &  x,
double &  y 
)

Sets the MIN value for x to F(MIN(x))

Parameters:
xThe MIN x value to allow
yThe value to return for x<=MIN

Definition at line 65 of file weight.cpp.

References minValue.

Referenced by parse().

{
if (minValue!=NULL){
delete minValue;
}
minValue=new(std::nothrow) std::pair<double,double>;
if (minValue==NULL){
std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
exit(1);
}
minValue->first=x;
minValue->second=y;
return;
}
void StochHMM::weight::setName ( std::string &  nm)
inline

Definition at line 139 of file weight.h.

References name.

{name=nm;};
void StochHMM::weight::setWeights ( std::vector< double > &  xVals,
std::vector< double > &  yVals,
std::pair< double, double > *  xMin,
std::pair< double, double > *  xMax 
)

Setup the score mapping from x->F(x) 2 vectors of equal length

Parameters:
xValsvector of doubles that contains the scores for x
yValsvector of doubles that contains the corresponding scores F(x)
xMINpair of doubles defines MIN x->F(x)
xMAXpair of doubles defines MAX x->F(x)

Definition at line 96 of file weight.cpp.

References vals.

Referenced by parse(), and setWeights().

{
if (vals!=NULL){
delete vals;
}
vals=new(std::nothrow) std::vector<std::pair<double,double> >;
if (vals==NULL){
std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
exit(1);
}
if (xVals.size()!=yVals.size()){
std::cerr << "X-Y values are not the same size." <<std::endl;
return;
}
for(size_t i=0;i<xVals.size();i++){
std::pair<double,double> xy (xVals[i],yVals[i]);
vals->push_back(xy);
}
return;
}
void StochHMM::weight::setWeights ( std::vector< double > &  xVals,
std::vector< double > &  yVals 
)

Setup the score mapping from x->F(x) 2 vectors of equal length

Parameters:
xValsvector of doubles that contains the scores for x
yValsvector of doubles that contains the corresponding scores F(x)

Definition at line 80 of file weight.cpp.

References maxValue, minValue, and setWeights().

{
if (maxValue==NULL && minValue==NULL){
setWeights(xVals, yVals, NULL, NULL);
}
else if (maxValue==NULL){
setWeights(xVals, yVals, minValue, NULL);
}
else if (minValue==NULL){
setWeights(xVals, yVals, NULL, maxValue);
}
else{
}
}
size_t StochHMM::weight::size ( void  )
inline

Given a score (x) it will map the score to F(x)

Returns the number of mapped values = equal to size of xVals handed to setWeights(...)

Returns:
size_t value of vector size;

Definition at line 84 of file weight.h.

References vals.

{return vals->size();};
std::string StochHMM::weight::stringify ( )

Converts weights to string

Definition at line 166 of file weight.cpp.

References StochHMM::double_to_string(), maxValue, minValue, and vals.

Referenced by print().

{
std::string scaleString;
//Process Values and scaled values;
scaleString+= "\tVALUE:\tLOG\t[";
for(size_t i=0;i<vals->size();i++){
if (i>0){
scaleString+= ",";
}
scaleString+= double_to_string((*vals)[i].first);
}
scaleString+= "]\n";
scaleString+= "\tSCALED:\tLOG\t[";
for(size_t i=0;i<vals->size();i++){
if (i>0){
scaleString+= ",";
}
scaleString+= double_to_string((*vals)[i].second);
}
scaleString+="]\n";
if (minValue!=NULL){
scaleString+= "\tMIN_VALUE:\tLOG\t" + double_to_string(minValue->first) + "\n";
scaleString+= "\tMIN_SCALED:\tLOG\t" + double_to_string(minValue->second) + "\n";
}
if (maxValue!=NULL){
scaleString+= "\tMAX_VALUE:\tLOG\t" + double_to_string(maxValue->first) + "\n";
scaleString+= "\tMAX_SCALED:\tLOG\t" + double_to_string(maxValue->second) + "\n";
}
return scaleString;
}

Member Data Documentation

bool StochHMM::weight::absolute
private

Definition at line 146 of file weight.h.

Referenced by getWeightedScore(), isAbsolute(), setAbsolute(), and weight().

double StochHMM::weight::absoluteValue
private

Stores whether Weight is a constant value.

Definition at line 147 of file weight.h.

Referenced by getAbsolute(), getWeightedScore(), setAbsolute(), and weight().

std::pair<double,double>* StochHMM::weight::maxValue
private

Definition at line 150 of file weight.h.

Referenced by getWeightedScore(), setMaxWeight(), setWeights(), stringify(), weight(), and ~weight().

std::pair<double,double>* StochHMM::weight::minValue
private

Stores the MAX value of x and the corresponding F(x)

Definition at line 151 of file weight.h.

Referenced by getWeightedScore(), setMinWeight(), setWeights(), stringify(), weight(), and ~weight().

std::string StochHMM::weight::name
private

Stores the constant value.

Definition at line 148 of file weight.h.

Referenced by getName(), parse(), and setName().

std::vector<std::pair<double,double> >* StochHMM::weight::vals
private

Stores the MIN value of x and the corresponding F(x)

Definition at line 153 of file weight.h.

Referenced by getWeightedScore(), setWeights(), size(), stringify(), weight(), and ~weight().


The documentation for this class was generated from the following files: