Commit 83104584 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] tuned histogram. does not work completely now.

parent a50ca2d5
...@@ -146,8 +146,7 @@ boost::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram() ...@@ -146,8 +146,7 @@ boost::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram()
} }
boost::lock_guard<boost::mutex> lock( m_histogramLock ); boost::lock_guard<boost::mutex> lock( m_histogramLock );
std::cout << " -------------------------------------------------- " << std::endl; m_histogram = boost::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet ) );
//m_histogram = boost::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet ) );
return m_histogram; return m_histogram;
} }
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "../../common/WAssert.h" #include "../../common/WAssert.h"
#include "../../common/WLimits.h" #include "../../common/WLimits.h"
#include "../WDataHandlerEnums.h"
#include "WValueSetHistogram.h" #include "WValueSetHistogram.h"
...@@ -58,65 +59,80 @@ WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > value ...@@ -58,65 +59,80 @@ WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > value
insert( tmp ); insert( tmp );
} }
} }
/*
WValueSetHistogram::WValueSetHistogram( const WValueSetBase& valueSet )
{
// calculate min max
m_minimum = wlimits::MAX_DOUBLE;
m_maximum = wlimits::MIN_DOUBLE;
double minDistance = wlimits::MAX_DOUBLE;
for( size_t i = 0; i != valueSet.size(); ++i )
{
double tmp = valueSet.getScalarDouble( i );
m_maximum = m_maximum < tmp ? tmp : m_maximum;
m_minimum = m_minimum > tmp ? tmp : m_minimum;
if( m_minimum != tmp && m_minimum != wlimits::MAX_DOUBLE )
{
minDistance = tmp - m_minimum < minDistance ? tmp - m_minimum : minDistance;
}
if( m_maximum != tmp && m_maximum != wlimits::MIN_DOUBLE )
{
minDistance = m_maximum - tmp < minDistance ? m_maximum - tmp : minDistance;
}
}
WValueSetHistogram::WValueSetHistogram( const WValueSetBase& valueSet ):
m_minimum( valueSet.getMinimumValue() ),
m_maximum( valueSet.getMaximumValue() )
{
// create base histogram // create base histogram
m_nInitialBuckets = ( ( m_maximum - m_minimum ) / minDistance ) + 1; m_nInitialBuckets = 100;
m_bucketSize = minDistance; m_initialBucketSize = ( m_maximum - m_minimum ) / static_cast< double >( m_nInitialBuckets );
size_t* initialBuckets = new size_t[m_nInitialBuckets]; WAssert( m_initialBucketSize > 0.0, "WValueSetHistogram::WValueSetHistogram() : m_initialBucketSize to small." );
// initialize array to zero
// create and initialize array to zero which finally contains the counts
size_t* initialBuckets = new size_t[ m_nInitialBuckets ];
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( size_t ) ); memset( initialBuckets, 0, m_nInitialBuckets * sizeof( size_t ) );
//*initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT! // *initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT!
// the array can be shared among several instances of WValueSetHistogram.
m_initialBuckets = boost::shared_array< size_t >( initialBuckets ); m_initialBuckets = boost::shared_array< size_t >( initialBuckets );
m_nMappedBuckets = 0; // no mapping applied yet. Initial and mapped are equal
m_nMappedBuckets = m_nInitialBuckets;
m_mappedBuckets = m_initialBuckets;
m_mappedBucketSize = m_initialBucketSize;
// and finally create the histogram
for( size_t i = 0; i < valueSet.size(); ++i ) for( size_t i = 0; i < valueSet.size(); ++i )
{ {
double tmp = valueSet.getScalarDouble( i ); double tmp = valueSet.getScalarDouble( i );
increment( tmp ); insert( tmp );
} }
} }
WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, double intervalSize ) WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, size_t buckets ):
m_minimum( histogram.m_minimum ),
m_maximum( histogram.m_maximum ),
m_initialBucketSize( histogram.m_initialBucketSize ),
m_initialBuckets( histogram.m_initialBuckets ),
m_nInitialBuckets( histogram.m_nInitialBuckets ),
m_mappedBuckets( histogram.m_mappedBuckets ),
m_nMappedBuckets( histogram.m_nMappedBuckets ),
m_mappedBucketSize( histogram.m_mappedBucketSize )
{ {
// copy constructor // apply modification of the histogram bucket size?
m_nInitialBuckets = histogram.getNInitialBuckets(); if( buckets == 0 )
m_initialBuckets = boost::shared_array< size_t >( histogram.getInitialBuckets() ); {
m_bucketSize = histogram.getBucketSize(); return;
}
WAssert( buckets > 0, "WValueSetHistogram::WValueSetHistogram : number of buckets needs to be larger than zero." );
WAssert( buckets < m_nInitialBuckets, "WValueSetHistogram::WValueSetHistogram : number of buckets needs to be smaller than the initial bucket count." );
// number of elements in the new mapped histogram = division + (round up)
m_nMappedBuckets = buckets;
m_mappedBucketSize = ( m_maximum - m_minimum ) / static_cast< double >( m_nMappedBuckets );
m_nMappedBuckets = 0; size_t ratio = static_cast<size_t>( buckets / m_nInitialBuckets );
m_minimum = histogram.getMin(); // map it
m_maximum = histogram.getMax(); m_mappedBuckets.reset();
if( intervalSize != 0.0 ) size_t* mappedBuckets = new size_t[ m_nMappedBuckets ];
memset( mappedBuckets, 0, m_nMappedBuckets * sizeof( size_t ) );
// *mappedBuckets = { 0 }; // works with C++0x
m_mappedBuckets = boost::shared_array< size_t >( mappedBuckets );
size_t index = 0;
for( size_t i = 0; i != m_nInitialBuckets; ++i )
{ {
WAssert( intervalSize > 0.0, "WValueSetHistogram::WValueSetHistogram : intervalSize has to be greater then zero." ); if( i % ratio == 0 && i != 0 )
calculateMapping( intervalSize ); {
index++;
}
m_mappedBuckets[index] += m_initialBuckets[i];
} }
}*/ }
WValueSetHistogram::~WValueSetHistogram() WValueSetHistogram::~WValueSetHistogram()
{ {
...@@ -148,42 +164,13 @@ void WValueSetHistogram::insert( double value ) ...@@ -148,42 +164,13 @@ void WValueSetHistogram::insert( double value )
m_mappedBuckets[ index ]++; m_mappedBuckets[ index ]++;
} }
void WValueSetHistogram::calculateMapping( double intervalSize ) size_t WValueSetHistogram::operator[]( size_t index ) const
{
/*
size_t ratio = static_cast<size_t>( intervalSize / m_bucketSize );
WAssert( ratio > 1, "WValueSetHistogram::calculateMapping() : intervalSize has to be greater then the original size." );
// number of elements in the new mapped histogram = division + (round up)
m_nMappedBuckets = m_nInitialBuckets / ratio + ( m_nInitialBuckets % ratio > 0 ? 1 : 0 );
if( m_mappedBuckets )
{
m_mappedBuckets.reset();
}
size_t* mappedBuckets = new size_t[m_nMappedBuckets];
memset( mappedBuckets, 0, m_nMappedBuckets * sizeof( size_t ) );
// *mappedBuckets = { 0 }; // works with C++0x
boost::scoped_array< size_t > scoped( mappedBuckets );
m_mappedBuckets.swap( scoped );
size_t index = 0;
for( size_t i = 0; i != m_nInitialBuckets; ++i )
{
if( i % ratio == 0 && i != 0 )
{
index++;
}
m_mappedBuckets[index] += m_initialBuckets[i];
}*/
}
size_t WValueSetHistogram::operator[]( size_t index )
{ {
// if no mapping has been applied, mappedBuckets points to the initial bucket // if no mapping has been applied, mappedBuckets points to the initial bucket
return m_mappedBuckets[ index ]; return m_mappedBuckets[ index ];
} }
size_t WValueSetHistogram::at( size_t index ) size_t WValueSetHistogram::at( size_t index ) const
{ {
WAssert( index < m_nMappedBuckets, "WValueSetHistogram::at() : index out of range." ); WAssert( index < m_nMappedBuckets, "WValueSetHistogram::at() : index out of range." );
...@@ -206,3 +193,21 @@ double WValueSetHistogram::getMaximum() const ...@@ -206,3 +193,21 @@ double WValueSetHistogram::getMaximum() const
return m_maximum; return m_maximum;
} }
std::pair< double, double > WValueSetHistogram::getIntervalForIndex( size_t index ) const
{
double first = m_minimum + m_mappedBucketSize * index;
double second = m_minimum + m_mappedBucketSize * ( index + 1 );
return std::make_pair( first, second );
}
std::ostream& operator<<( std::ostream& out, const WValueSetHistogram& h )
{
for ( size_t i = 0; i < h.size(); ++i )
{
std::pair< double, double > interval = h.getIntervalForIndex( i );
out << i << " = [" << interval.first << ", " << interval.second << "] = " << h[ i ] << std::endl;
}
return out;
}
...@@ -57,15 +57,15 @@ public: ...@@ -57,15 +57,15 @@ public:
* *
* \param valueSet source of the data for the histogram * \param valueSet source of the data for the histogram
*/ */
//explicit WValueSetHistogram( const WValueSetBase& valueSet ); explicit WValueSetHistogram( const WValueSetBase& valueSet );
/** /**
* Copy constructor. If another interval size is given the histogram gets matched to it using the initial bucket data. * Copy constructor. If another interval size is given the histogram gets matched to it using the initial bucket data.
* *
* \param histogram another WValueSetHistogram * \param histogram another WValueSetHistogram
* \param intervalSize the size of one bucket in the mapped histogram * \param buckets the new number of buckets.
*/ */
//explicit WValueSetHistogram( const WValueSetHistogram& histogram, double intervalSize = 0.0 ); explicit WValueSetHistogram( const WValueSetHistogram& histogram, size_t buckets = 0 );
/** /**
* Destructor. * Destructor.
...@@ -80,7 +80,7 @@ public: ...@@ -80,7 +80,7 @@ public:
* *
* \return elements in the bucket. * \return elements in the bucket.
*/ */
size_t operator[]( size_t index ); size_t operator[]( size_t index ) const;
/** /**
* Get the size of the bucket. Testing if the position is valid. * Get the size of the bucket. Testing if the position is valid.
...@@ -90,7 +90,7 @@ public: ...@@ -90,7 +90,7 @@ public:
* *
* \return elements in the bucket * \return elements in the bucket
*/ */
size_t at( size_t index ); size_t at( size_t index ) const;
/** /**
* Returns the number of buckets in the histogram with the actual mapping. * Returns the number of buckets in the histogram with the actual mapping.
...@@ -120,6 +120,16 @@ public: ...@@ -120,6 +120,16 @@ public:
*/ */
double getBucketSize() const; double getBucketSize() const;
/**
* Returns the actual interval associated with the given index. The interval is
* getIntervalForIndex( i ).second == getIntervalForIndex( i + 1 ).first
*
* \param index the intex
*
* \return the open interval in [getMinimum(),getMaximum].
*/
std::pair< double, double > getIntervalForIndex( size_t index ) const;
protected: protected:
/** /**
* Return the initial buckets. * Return the initial buckets.
...@@ -183,13 +193,6 @@ private: ...@@ -183,13 +193,6 @@ private:
*/ */
double m_mappedBucketSize; double m_mappedBucketSize;
/**
* To calculate the new buckets
*
* \param intervalSize the size of one bucket
*/
void calculateMapping( double intervalSize );
/** /**
* increment the value by one, contains the logic to find the element place in the array. * increment the value by one, contains the logic to find the element place in the array.
* Should only be used in the constructor i.e. while iterating over WValueSet. * Should only be used in the constructor i.e. while iterating over WValueSet.
...@@ -199,5 +202,10 @@ private: ...@@ -199,5 +202,10 @@ private:
void insert( double value ); void insert( double value );
}; };
/**
* Write a histogram in string representation to the given output stream.
*/
std::ostream& operator<<( std::ostream& out, const WValueSetHistogram& h );
#endif // WVALUESETHISTOGRAM_H #endif // WVALUESETHISTOGRAM_H
...@@ -134,7 +134,8 @@ void WMHistogramEqualization::moduleMain() ...@@ -134,7 +134,8 @@ void WMHistogramEqualization::moduleMain()
debugLog() << "Recalculating"; debugLog() << "Recalculating";
// Grab the histogram // Grab the histogram
boost::shared_ptr< const WValueSetHistogram > hist = dataSet->getHistogram();
debugLog() << *hist;
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment