view annotationCalculatorSrc/Histogram.cpp @ 49:8df911733fdc

Added new histogram functions - absolute error graphs like Match paper
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 22 May 2012 22:53:44 +0100
parents 5359e2c0b0fb
children
line wrap: on
line source
/*
 *  Histogram.cpp
 *  annotationResultCalculator
 *
 *  Created by Andrew on 08/05/2012.
 *  Copyright 2012 QMUL. All rights reserved.
 *
 */

#include "Histogram.h"

Histogram::Histogram(){
screenHeight = ofGetHeight();
}

void Histogram::createHistogram(const int& binWidthIn, const int& numberofBinsIn){
	//printf("create regular histo\n");
	
	numberofBins = numberofBinsIn;
	binWidth = binWidthIn;
	minimumBinValue = -1.0 * binWidth * ((double)numberofBins/2);
	
	histogram.clear();
	histogram.assign(numberofBins, 0);
	
	maximum = 0;
	numberOutsideRange = 0;
	
	setBinPoints();
	
}


void Histogram::createAbsoluteHistogram(const int& binWidthIn, const int& numberofBinsIn){
	printf("create abs histo\n");
	
	minimumBinValue = 0;
	numberofBins = numberofBinsIn;
	binWidth = binWidthIn;
	
	histogram.clear();
	histogram.assign(numberofBins, 0);
	
	maximum = 0;
	numberOutsideRange = 0;
	
	setBinPoints();
}

void Histogram::createAbsoluteHistogramForMatchData(){
	printf("create abs histo\n");
	
	minimumBinValue = 0;

	numberofBins = 7;
	
	histogram.clear();
	histogram.assign(numberofBins, 0);
	
	maximum = 0;
	numberOutsideRange = 0;
	
	setBinPointsToMatchRange();
}

void Histogram::setBinPointsToMatchRange(){
	binPoints.clear();
	numberofBins = 7;
	binWidth = 0;
	binPoints.push_back(0);
	binPoints.push_back(20);
	binPoints.push_back(40);
	binPoints.push_back(60);
	binPoints.push_back(100);
	binPoints.push_back(200);
	binPoints.push_back(500);
	maximumBinValue = 1000;
}



void Histogram::setBinPoints(){
	binPoints.clear();
	binPoints.push_back(minimumBinValue);
	while (binPoints.size() < numberofBins){
		binPoints.push_back(binPoints[binPoints.size()-1] + binWidth);
	}
	maximumBinValue = binPoints[binPoints.size()-1]+binWidth;
	
//	for (int k = 0; k < binPoints.size();k++){
//		printf("Bin pts [%i] : %f\n", k, binPoints[k]);
//	}
	
}

void Histogram::processDataIntoHistogram(const DoubleVector& data){

	double binPoint;
	int bin = 0;
	numberOutsideRange = 0;
	
	for (int i = 0;i < data.size();i++){
		//find which bin it falls into.
		//5 bins, width is 10
		//then we start around zero for the 5+1/2 th, i.e. 3rd bin
		//zero is the 5/2 th pt
	//	printf("data[%i] %f\n", i, data[i]);
		
		bin = 0;
		binPoint = binPoints[0];//i.e. minimumBinValue;
	//	printf("binpT%f\n", binPoint);
		if (data[i] >= binPoint){//i.e. falls inside
			
			while (bin < numberofBins && data[i] > binPoints[bin+1]) {
				//printf("data pt %f bin %i binPt %.1f\n", data[i], bin, binPoint);
				bin++;
				binPoint = binPoints[bin];
				
//				binPoint += binWidth;
//				bin++;				
			}
			
	//		printf("data pt %f bin %i binPt %.1f\n", data[i], bin, binPoint);
			
			if (data[i] <= maximumBinValue){//in case outside range
				histogram[bin]++;
			} else{
				//then it's greater than maximum
				numberOutsideRange++;
			}
		} else{
			//then it's less than minimum
			numberOutsideRange++;
		}
	}
	
	for (int k = 0; k < histogram.size();k++){
		
		if (histogram[k] > maximum)
			maximum = histogram[k];
		
		printf("HISTO[%i] (%.1f - %.1f) = %i\n", k, binPoints[k], binPoints[k]+binWidth, histogram[k]);
	}
	printf("Number outside %i\n", numberOutsideRange);
	
	
}


void Histogram::printHistogramPercentages(){
	double total = numberOutsideRange;
	
	for (int k = 0; k < histogram.size();k++){
		total += histogram[k];
	}
	
	for (int k = 0;k < histogram.size() - 1;k++){
	printf("HISTO[%i] (%.1f - %.1f) = %.2f\n", k, binPoints[k],  binPoints[k+1],  100.0*(double)histogram[k]/total);
	}
	printf("HISTO[%i] (%.1f - %.1f) = %.2f\n", histogram.size() - 1, binPoints[histogram.size() - 1],  maximumBinValue,  100.0*(double)histogram[histogram.size() - 1]/total);
	printf("OUTSIDE = %.2f\n", (double) 100.0*numberOutsideRange/total);	
	
}

void Histogram::plotHistogram(){
	plotHistogram(maximum);
}

void Histogram::plotHistogram(const double& maxHeight){
	
	double width = ofGetWidth();
	double height = ofGetHeight();
	screenHeight = ofGetHeight();
	if (numberofBins > 0 && maxHeight > 0){
		width /= numberofBins;
		height /= maxHeight;
		
		for (int i = 0;i < numberofBins;i++){
			ofRect(i * width, getY(histogram[i]*height), width, histogram[i]*height);
		}
		
	}
	
}

void Histogram::labelHistogram(){
	
	double width = ofGetWidth();
//	double height = ofGetHeight();
	screenHeight = ofGetHeight();
	if (numberofBins > 0){
		width /= numberofBins;
	//	height /= maxHeight;
		
		
		
		for (int i = 0;i < numberofBins;i++){
			
			int nextBinVal = maximumBinValue;
			if (i < numberofBins - 1){
				nextBinVal = binPoints[i+1];
			}
			
			ofDrawBitmapString(ofToString(binPoints[i], 0)+" - "+ofToString(nextBinVal, 0), (i +0.2) * width, screenHeight - 10);
		}
		
	}
	
}


int Histogram::getY(const int& y){
	return screenHeight - y;
}