|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "MetaRecognition.h" |
|
|
#include <string.h> |
|
|
|
|
|
#include <stdlib.h> |
|
|
|
|
|
#ifdef __cplusplus |
|
|
extern "C" { |
|
|
#endif |
|
|
extern int weibull_fit_verbose_debug; |
|
|
#ifdef __cplusplus |
|
|
} |
|
|
#endif |
|
|
MetaRecognition::MetaRecognition(int scores_to_dropx, int fitting_sizex, bool verb, double alphax, int translate_amountx): |
|
|
scores_to_drop(scores_to_dropx),verbose(verb),fitting_size(fitting_sizex),alpha(alphax),translate_amount(translate_amountx) |
|
|
{ |
|
|
memset(parmhat,0,sizeof(parmhat)); |
|
|
memset(parmci,0,sizeof(parmci)); |
|
|
sign = 1; |
|
|
ftype = complement_reject; |
|
|
small_score=0; |
|
|
isvalid=false; |
|
|
if(verb) weibull_fit_verbose_debug=1; |
|
|
else weibull_fit_verbose_debug=0; |
|
|
} |
|
|
|
|
|
MetaRecognition::~MetaRecognition() |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
bool MetaRecognition::is_valid(){ |
|
|
return isvalid; |
|
|
} |
|
|
|
|
|
void MetaRecognition::set_translate(double t){ |
|
|
translate_amount = t; |
|
|
isvalid=false; |
|
|
}; |
|
|
|
|
|
|
|
|
void MetaRecognition::Reset(){ |
|
|
memset(parmhat,0,sizeof(parmhat)); |
|
|
memset(parmci,0,sizeof(parmci)); |
|
|
sign = 1; |
|
|
scores_to_drop = 0; |
|
|
small_score=0; |
|
|
isvalid=false; |
|
|
} |
|
|
|
|
|
|
|
|
int compare_sort_decending (const void * a, const void * b) |
|
|
{ |
|
|
const double *da = (const double *) a; |
|
|
const double *db = (const double *) b; |
|
|
return (*da < *db) - (*da > *db); |
|
|
} |
|
|
|
|
|
int compare_sort_assending (const void * a, const void * b) |
|
|
{ |
|
|
const double *da = (const double *) a; |
|
|
const double *db = (const double *) b; |
|
|
return (*da > *db) - (*da < *db); |
|
|
} |
|
|
|
|
|
inline const char * const BoolToString(bool b) |
|
|
{ |
|
|
return b ? "true" : "false"; |
|
|
} |
|
|
|
|
|
inline int const BoolToInt(bool b) |
|
|
{ |
|
|
return b ? 1 : 0; |
|
|
} |
|
|
|
|
|
inline const bool IntToBool(const char * s) |
|
|
{ |
|
|
int val= atoi(s); |
|
|
if(val) return true; |
|
|
else return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double MetaRecognition::Inv(double x) |
|
|
{ |
|
|
if(!isvalid) return -9999.0; |
|
|
double score = weibull_inv(x, parmhat[0], parmhat[1]); |
|
|
return (score - translate_amount + small_score)*sign; |
|
|
} |
|
|
|
|
|
double MetaRecognition::CDF(double x) |
|
|
{ |
|
|
if(!isvalid) return -9999.0; |
|
|
double translated_x = x*sign + translate_amount - small_score; |
|
|
double wscore=weibull_cdf(translated_x, parmhat[0], parmhat[1]); |
|
|
if(ftype==complement_model || ftype==positive_model) return 1-wscore; |
|
|
return wscore; |
|
|
}; |
|
|
|
|
|
double MetaRecognition::W_score(double x){ |
|
|
return CDF(x); |
|
|
}; |
|
|
|
|
|
bool MetaRecognition::Predict_Match(double x, double threshold){ |
|
|
double score = Inv(threshold); |
|
|
if(sign <0) return (x < score); |
|
|
return (x > score); |
|
|
}; |
|
|
|
|
|
int MetaRecognition::ReNormalize(double *invec, double *outvec, int length) |
|
|
{ |
|
|
if(!isvalid) return -9997.0; |
|
|
int rval=1; |
|
|
for(int i=0; i< length; i++){ |
|
|
outvec[i] = W_score(invec[i]); |
|
|
} |
|
|
return rval; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int MetaRecognition::EvtGeneric(double* inputData, int inputDataSize, int inward, double x) |
|
|
{ |
|
|
double * inputDataCopy = (double *) malloc(sizeof(double) * inputDataSize); |
|
|
|
|
|
double * dataPtr = NULL; |
|
|
int icnt=0; |
|
|
if(!inward && (sign > 0) ) { |
|
|
icnt = inputDataSize; |
|
|
memcpy(inputDataCopy,inputData, inputDataSize*sizeof(double)); |
|
|
} |
|
|
if(!inward && (sign < 0) ){ |
|
|
for(int i=0; i < inputDataSize; i++) inputDataCopy[i] = (inputData[i]*sign); |
|
|
icnt = inputDataSize; |
|
|
} |
|
|
else if(inward && (sign < 0)) { |
|
|
for(int i=0; i < inputDataSize; i++) { |
|
|
if(inputData[i] > x) { |
|
|
inputDataCopy[icnt++] = (inputData[i]*sign); |
|
|
} |
|
|
} |
|
|
} else if(inward && (sign > 0)) { |
|
|
for(int i=0; i < inputDataSize; i++) { |
|
|
if(inputData[i] < x) { |
|
|
inputDataCopy[icnt++] = (inputData[i]); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
qsort(inputDataCopy, icnt , sizeof(double), compare_sort_decending); |
|
|
|
|
|
|
|
|
if(scores_to_drop>0){ |
|
|
dataPtr=inputDataCopy+scores_to_drop; |
|
|
} else { |
|
|
dataPtr=inputDataCopy; |
|
|
} |
|
|
|
|
|
small_score = dataPtr[fitting_size-1]; |
|
|
|
|
|
for(int i=0; i < fitting_size; i++) |
|
|
{ |
|
|
|
|
|
dataPtr[i] = dataPtr[i] + translate_amount - small_score; |
|
|
} |
|
|
|
|
|
|
|
|
int rval = weibull_fit(parmhat, parmci, dataPtr, alpha, fitting_size); |
|
|
isvalid= true; |
|
|
if(rval != 1) Reset(); |
|
|
free(inputDataCopy); |
|
|
return rval; |
|
|
} |
|
|
|
|
|
|
|
|
int MetaRecognition::FitLow(double* inputData, int inputDataSize, int fsize) |
|
|
{ |
|
|
if(fsize>0) fitting_size=fsize; |
|
|
sign = -1; |
|
|
return EvtGeneric(inputData, inputDataSize); |
|
|
} |
|
|
|
|
|
int MetaRecognition::FitHigh(double* inputData, int inputDataSize, int fsize) |
|
|
{ |
|
|
if(fsize>0) fitting_size=fsize; |
|
|
sign = 1; |
|
|
return EvtGeneric(inputData, inputDataSize); |
|
|
} |
|
|
|
|
|
int MetaRecognition::FitSVM(svm_node_libsvm* SVMdata, int inputDataSize, int label_of_interest, bool label_has_positive_score, int fit_type, int fit_size ) |
|
|
{ |
|
|
|
|
|
Reset(); |
|
|
ftype = (MR_fitting_type)fit_type; |
|
|
fitting_size = fit_size; |
|
|
double * inputDataCopy = (double *) malloc(sizeof(double) * inputDataSize); |
|
|
int sign_of_label_of_interest=0; |
|
|
double * dataPtr = NULL; |
|
|
int sign_of_expected_score=-1; |
|
|
if(label_has_positive_score) sign_of_expected_score=1; |
|
|
|
|
|
int icnt=0; |
|
|
bool rejection=(ftype==complement_reject || ftype == positive_reject); |
|
|
if(rejection) { |
|
|
for(int i=0; i < inputDataSize; i++) { |
|
|
if(SVMdata[i].index != label_of_interest) inputDataCopy[icnt++] = (SVMdata[i].value); |
|
|
else { |
|
|
if(SVMdata[i].value >0) sign_of_label_of_interest++; |
|
|
else sign_of_label_of_interest--; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
for(int i=0; i < inputDataSize; i++) { |
|
|
if(SVMdata[i].index == label_of_interest) inputDataCopy[icnt++] = (SVMdata[i].value); |
|
|
else { |
|
|
if(SVMdata[i].value >0) sign_of_label_of_interest++; |
|
|
else sign_of_label_of_interest--; |
|
|
} |
|
|
} |
|
|
} |
|
|
if (verbose && sign_of_label_of_interest * sign_of_expected_score > 0){ |
|
|
printf("In MetaRecognition::FitSVM, warning: possible inconsistency average of the non-matching data has score %d, but expected sign is %d\n", |
|
|
sign_of_label_of_interest, -sign_of_expected_score); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((!label_has_positive_score && rejection) |
|
|
|| (label_has_positive_score && !rejection)) { |
|
|
sign = -1; |
|
|
for(int i=0; i < icnt; i++) { |
|
|
inputDataCopy[i] *= -1; |
|
|
} |
|
|
} else sign=1; |
|
|
|
|
|
|
|
|
qsort(inputDataCopy, icnt , sizeof(double), compare_sort_decending); |
|
|
|
|
|
|
|
|
if(scores_to_drop){ |
|
|
dataPtr=inputDataCopy+scores_to_drop; |
|
|
} else { |
|
|
dataPtr=inputDataCopy; |
|
|
} |
|
|
|
|
|
small_score = dataPtr[fitting_size - 1]; |
|
|
|
|
|
for(int i=0; i < fitting_size; i++) |
|
|
{ |
|
|
|
|
|
dataPtr[i] = dataPtr[i] + translate_amount - small_score; |
|
|
} |
|
|
|
|
|
int rval = weibull_fit(parmhat, parmci, dataPtr, alpha, fitting_size); |
|
|
|
|
|
isvalid= true; |
|
|
if(rval != 1) Reset(); |
|
|
free(inputDataCopy); |
|
|
printf("Completed weibull fitting\n"); |
|
|
return rval; |
|
|
}; |
|
|
|
|
|
void MetaRecognition::Save(std::ostream &outputStream) const |
|
|
{ |
|
|
if(outputStream.good() && isvalid) |
|
|
{ |
|
|
try { |
|
|
outputStream.precision(21); |
|
|
outputStream.setf(std::ios::scientific,std::ios::floatfield); |
|
|
outputStream << parmhat[0] << " " << parmhat[1] << " " |
|
|
<< parmci[0] << " " << parmci[1] << " " |
|
|
<< parmci[2] << " " << parmci[3] << " " |
|
|
<< sign << " " |
|
|
<< alpha << " " |
|
|
<< (int) ftype << " " |
|
|
<< fitting_size << " " |
|
|
<< translate_amount << " " |
|
|
<< small_score<< " " |
|
|
<< scores_to_drop |
|
|
<< std::endl; |
|
|
} catch(std::bad_alloc& e) { |
|
|
std::cout << "Could not allocate the required memory, failed with error: '" << e.what() << "'" << std::endl; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
std::ostream& operator<< ( std::ostream& os, const MetaRecognition& mr ) |
|
|
{ |
|
|
mr.Save(os); |
|
|
return os; |
|
|
} |
|
|
|
|
|
std::istream& operator>> ( std::istream& is, MetaRecognition& mr ) |
|
|
{ |
|
|
mr.Load(is); |
|
|
return is; |
|
|
} |
|
|
|
|
|
|
|
|
void MetaRecognition::Load(std::istream &inputStream) |
|
|
{ |
|
|
isvalid=false; |
|
|
int temp; |
|
|
if(inputStream.good()) |
|
|
{ |
|
|
int iftype; |
|
|
inputStream >> parmhat[0] >> parmhat[1] |
|
|
>> parmci[0] >> parmci[1] |
|
|
>> parmci[2] >> parmci[3] |
|
|
>> sign |
|
|
>> alpha |
|
|
>> iftype |
|
|
>> fitting_size |
|
|
>> translate_amount |
|
|
>> small_score |
|
|
>> scores_to_drop; |
|
|
isvalid=true; |
|
|
ftype = (MR_fitting_type) iftype; |
|
|
} |
|
|
} |
|
|
|
|
|
void MetaRecognition::Save(FILE *outputFile) const |
|
|
{ |
|
|
if((outputFile != NULL) && !feof(outputFile)) |
|
|
{ |
|
|
fprintf(outputFile, |
|
|
"%21.18g %21.18g " |
|
|
"%21.18g %21.18g " |
|
|
"%21.18g %21.18g " |
|
|
"%d %f %d %d " |
|
|
"%d %21.18g %d\n", |
|
|
parmhat[0], parmhat[1], |
|
|
parmci[0],parmci[1], |
|
|
parmci[2],parmci[3], |
|
|
sign, alpha, (int) ftype,fitting_size, |
|
|
translate_amount, small_score, scores_to_drop); |
|
|
} |
|
|
} |
|
|
|
|
|
void MetaRecognition::Load(FILE *inputFile) |
|
|
{ |
|
|
int temp, iftype; |
|
|
int retcode=0; |
|
|
isvalid=false; |
|
|
if((inputFile != NULL) && !feof(inputFile)) |
|
|
{ |
|
|
|
|
|
retcode = fscanf(inputFile, |
|
|
"%lf %lf " |
|
|
"%lf %lf " |
|
|
"%lf %lf " |
|
|
"%d %lf %d %d " |
|
|
"%d %lf %d ", |
|
|
parmhat, parmhat+1, |
|
|
parmci,parmci+1, |
|
|
parmci+2,parmci+3, |
|
|
&sign, &alpha, &iftype, &fitting_size, |
|
|
&translate_amount, &small_score, &scores_to_drop); |
|
|
isvalid=true; |
|
|
ftype = (MR_fitting_type) iftype; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void MetaRecognition::Save(char* filename) const |
|
|
{ |
|
|
FILE* fp = fopen(filename,"w"); |
|
|
if(fp) { |
|
|
Save(fp); |
|
|
fclose(fp); |
|
|
} else if(strlen(filename)>0) |
|
|
fprintf(stderr,"SaveWeibull could not open file |%s|\n",filename); |
|
|
else fprintf(stderr,"SaveWeibull called with null filename\n"); |
|
|
} |
|
|
|
|
|
void MetaRecognition::Load(char* filename){ |
|
|
FILE* fp = fopen(filename,"r"); |
|
|
isvalid=false; |
|
|
if(fp) { |
|
|
Load(fp); |
|
|
isvalid=true; |
|
|
fclose(fp); |
|
|
} else if(strlen(filename)>0) |
|
|
fprintf(stderr,"LoadWeibull could not open file |%s|\n",filename); |
|
|
else fprintf(stderr,"LoadWeibull called with null filename\n"); |
|
|
|
|
|
} |
|
|
|
|
|
std::string MetaRecognition::to_string() { |
|
|
std::stringstream oss; |
|
|
this->Save(oss); |
|
|
return oss.str(); |
|
|
} |
|
|
void MetaRecognition::from_string(std::string input) { |
|
|
std::stringstream iss(input); |
|
|
this->Load(iss); |
|
|
} |
|
|
|
|
|
|
|
|
int MetaRecognition::set_fitting_size(int nsize){ isvalid=false; return fitting_size=nsize;} |
|
|
int MetaRecognition::get_fitting_size(){ return fitting_size;} |
|
|
int MetaRecognition::get_translate_amount(){ return translate_amount;} |
|
|
int MetaRecognition::set_translate_amount(int ntrans) {isvalid=false; return translate_amount=ntrans;} |
|
|
double MetaRecognition::get_small_score(){return small_score;} |
|
|
double MetaRecognition::set_small_score(double nscore){isvalid=false; return small_score=nscore;} |
|
|
int MetaRecognition::get_sign(){return sign;} |
|
|
int MetaRecognition::set_sign(int nsign){return sign=nsign;} |
|
|
|