| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * Histogram.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Jul 26, 2012
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "Histogram.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <algorithm>
 | 
|---|
| 41 | #include <cmath>
 | 
|---|
| 42 | #include <iostream>
 | 
|---|
| 43 | #include <sstream>
 | 
|---|
| 44 | #include <string>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include <boost/bind.hpp>
 | 
|---|
| 47 | #include <boost/foreach.hpp>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 50 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /** Tiny helper struct to create equally spaced bins with count of zero.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  */
 | 
|---|
| 55 | struct BinCreator_t {
 | 
|---|
| 56 |   BinCreator_t( const double lowerend, const double _width ) :
 | 
|---|
| 57 |     currentstart(lowerend),
 | 
|---|
| 58 |     width(_width)
 | 
|---|
| 59 |   {}
 | 
|---|
| 60 |   Histogram::Bin_t operator()() {
 | 
|---|
| 61 |     Histogram::Bin_t bin( make_pair( currentstart, 0.) );
 | 
|---|
| 62 |     currentstart+=width;
 | 
|---|
| 63 |     return bin;
 | 
|---|
| 64 |   }
 | 
|---|
| 65 | private:
 | 
|---|
| 66 |   double currentstart;
 | 
|---|
| 67 |   const double width;
 | 
|---|
| 68 | };
 | 
|---|
| 69 | 
 | 
|---|
| 70 | 
 | 
|---|
| 71 | // see http://stackoverflow.com/questions/634087/stdcopy-to-stdcout-for-stdpair
 | 
|---|
| 72 | // printing a pair is not easy, especially so with ostream_iterator as it cannot find
 | 
|---|
| 73 | // overload operator<<() as it is not in namespace std.
 | 
|---|
| 74 | template <class T>
 | 
|---|
| 75 | struct PrintPair : public std::unary_function<T, void>
 | 
|---|
| 76 | {
 | 
|---|
| 77 |     std::ostream& os;
 | 
|---|
| 78 |     PrintPair(std::ostream& strm) : os(strm) {}
 | 
|---|
| 79 | 
 | 
|---|
| 80 |     void operator()(const T& elem) const
 | 
|---|
| 81 |     {
 | 
|---|
| 82 |       os << "(" << elem.first << "," << elem.second << ") ";
 | 
|---|
| 83 |     }
 | 
|---|
| 84 | };
 | 
|---|
| 85 | 
 | 
|---|
| 86 | std::ostream & operator<<(std::ostream &ost, const Histogram::Bin_t &elem)
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   ost << "(" << elem.first << "," << elem.second << ") ";
 | 
|---|
| 89 |   return ost;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | std::ostream & operator<<(std::ostream &ost, const Histogram &histogram)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   for (Histogram::Bins_t::const_iterator iter = histogram.bins.begin();
 | 
|---|
| 95 |       iter != histogram.bins.end(); ++iter)
 | 
|---|
| 96 |     ost << *iter;
 | 
|---|
| 97 |   return ost;
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | Histogram::Histogram(const samples_t &samples) :
 | 
|---|
| 101 |   binwidth(0.5),
 | 
|---|
| 102 |   offset(0.)
 | 
|---|
| 103 | {
 | 
|---|
| 104 |   if (!samples.empty()) {
 | 
|---|
| 105 |     // set offset to first value
 | 
|---|
| 106 |     const_cast<BinLowerEnd &>(offset) = *samples.begin();
 | 
|---|
| 107 |     // set binwidth to statistical sensible value
 | 
|---|
| 108 |     MinMax_t MinMax = getMinMaxFromSamples(samples);
 | 
|---|
| 109 |     const_cast<double &>(binwidth) = (*(MinMax.second) - *(MinMax.first))/pow(samples.size(), 1./3.);
 | 
|---|
| 110 |     // and add all samples to these histogram bins
 | 
|---|
| 111 |     addSamples(samples);
 | 
|---|
| 112 |   }
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | Histogram::Histogram(const samples_t &samples, const BinLowerEnd _offset, const double _binwidth) :
 | 
|---|
| 116 |     binwidth(_binwidth),
 | 
|---|
| 117 |     offset(_offset)
 | 
|---|
| 118 | {
 | 
|---|
| 119 |   addSamples(samples);
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | Histogram::MinMax_t Histogram::getMinMaxFromSamples(const samples_t &samples) const
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   std::pair<samples_t::const_iterator, samples_t::const_iterator> returnpair;
 | 
|---|
| 125 |   returnpair.first = min_element(samples.begin(), samples.end());
 | 
|---|
| 126 |   returnpair.second = max_element(samples.begin(), samples.end());
 | 
|---|
| 127 |   ASSERT((returnpair.second != samples.end()) || (returnpair.first != samples.end()),
 | 
|---|
| 128 |       "Histogram::Histogram() - cannot find min/max despite non-empty range.");
 | 
|---|
| 129 |   return returnpair;
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | void Histogram::addSamples(const samples_t &samples)
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   // build histogram from samples
 | 
|---|
| 135 |   if (!samples.empty()) {
 | 
|---|
| 136 |     // 1. get min and max and determine width
 | 
|---|
| 137 |     MinMax_t MinMax = getMinMaxFromSamples(samples);
 | 
|---|
| 138 | //    LOG(1, "DEBUG: min is " << *(MinMax.first) << " and max is " << *(MinMax.second) << ".");
 | 
|---|
| 139 | 
 | 
|---|
| 140 |     // 2. create each bin
 | 
|---|
| 141 |     {
 | 
|---|
| 142 |       std::vector<Bin_t> vectorbins;
 | 
|---|
| 143 |       const BinLowerEnd HistogramStart = getLowerEnd(*(MinMax.first));
 | 
|---|
| 144 |       BinCreator_t BinCreator( HistogramStart, binwidth );
 | 
|---|
| 145 |       // we need one extra bin for get...Bin()'s find to work properly
 | 
|---|
| 146 |       const int CountBins = ceil((*(MinMax.second) - HistogramStart)/binwidth)+1;
 | 
|---|
| 147 |       vectorbins.resize(CountBins+1, Bin_t( make_pair(0., 0.) ) );
 | 
|---|
| 148 |       std::generate( vectorbins.begin(), vectorbins.end(), BinCreator );
 | 
|---|
| 149 |       bins.insert(vectorbins.begin(), vectorbins.end());
 | 
|---|
| 150 |     }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     // 3. place each sample into bin
 | 
|---|
| 153 |     BOOST_FOREACH( double value, samples) {
 | 
|---|
| 154 |       const Bins_t::iterator biniter = getLowerEndBin(value);
 | 
|---|
| 155 |       ASSERT( biniter != bins.end(),
 | 
|---|
| 156 |           "Histogram::Histogram() - cannot find bin for value from given samples.");
 | 
|---|
| 157 |       // (make bin count normalized, i.e. always equal to surface area of 1)
 | 
|---|
| 158 |       biniter->second += 1./binwidth;
 | 
|---|
| 159 |     }
 | 
|---|
| 160 |     LOG(2, "DEBUG: Bins are " << printBins() << ".");
 | 
|---|
| 161 |   } else {
 | 
|---|
| 162 | //    LOG(1, "INFO: Given vector of samples is empty.");
 | 
|---|
| 163 |   }
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | std::string Histogram::printBins() const
 | 
|---|
| 167 | {
 | 
|---|
| 168 |   std::stringstream output;
 | 
|---|
| 169 |   std::for_each( bins.begin(), bins.end(), PrintPair<Bin_t>(output));
 | 
|---|
| 170 |   return output.str();
 | 
|---|
| 171 | }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | void Histogram::extendMissingBins(const BinLowerEnd LowerEnd, const BinLowerEnd NextLowerEnd)
 | 
|---|
| 174 | {
 | 
|---|
| 175 |   Bins_t::const_iterator loweriter = getLowerEndBin(LowerEnd);
 | 
|---|
| 176 |   Bins_t::const_iterator upperiter = getHigherEndBin(NextLowerEnd);
 | 
|---|
| 177 |   if (loweriter == bins.end()) {
 | 
|---|
| 178 |     // we need bins beneath our first, add them
 | 
|---|
| 179 |     for(BinLowerEnd offset = getLowerEnd(LowerEnd);
 | 
|---|
| 180 |         offset < getLowerEnd(NextLowerEnd);
 | 
|---|
| 181 |         offset += binwidth) {
 | 
|---|
| 182 |       LOG(4, "DEBUG: Extending below at offset " << offset << ".");
 | 
|---|
| 183 |       bins.insert( std::make_pair (offset, 0. ) );
 | 
|---|
| 184 |     }
 | 
|---|
| 185 |     LOG(3, "DEBUG: Bins after adding empties before start are " << printBins() << ".");
 | 
|---|
| 186 |     loweriter = getLowerEndBin(LowerEnd);
 | 
|---|
| 187 |     ASSERT( loweriter != bins.end(),
 | 
|---|
| 188 |         "Histogram::extendMissingBins() - still no lower bound after adding empties.");
 | 
|---|
| 189 |   }
 | 
|---|
| 190 |   if (upperiter == bins.end()) {
 | 
|---|
| 191 |     // we need bins after our last, add them
 | 
|---|
| 192 |     for(BinLowerEnd offset = getLowerEnd(LowerEnd);
 | 
|---|
| 193 |         offset <= getLowerEnd(NextLowerEnd)+(1.5*binwidth); /* for safety we go a little further */
 | 
|---|
| 194 |         offset += binwidth) {
 | 
|---|
| 195 |       LOG(4, "DEBUG: Extending above at offset " << offset << ".");
 | 
|---|
| 196 |       bins.insert( std::make_pair (offset, 0. ) );
 | 
|---|
| 197 |     }
 | 
|---|
| 198 |     LOG(3, "DEBUG: Bins after adding empties after end are " << printBins() << ".");
 | 
|---|
| 199 |     upperiter = getHigherEndBin(NextLowerEnd);
 | 
|---|
| 200 |     ASSERT( upperiter != bins.end(),
 | 
|---|
| 201 |         "Histogram::extendMissingBins() - still no upper bound after adding empties.");
 | 
|---|
| 202 |   }
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | void Histogram::superposeOtherHistogram(const Histogram &other, const double prefactor)
 | 
|---|
| 206 | {
 | 
|---|
| 207 |   // go through each of the other histogram's bins
 | 
|---|
| 208 |   Bins_t::const_iterator enditer = --other.bins.end(); // (except internal last one)
 | 
|---|
| 209 |   for (Bins_t::const_iterator biniter = other.bins.begin();
 | 
|---|
| 210 |       biniter != enditer; /* we advance ourselves in loop */) {
 | 
|---|
| 211 |     const Bin_t &bin = *biniter;
 | 
|---|
| 212 |     ++biniter;
 | 
|---|
| 213 |     const Bin_t &nextbin = *biniter;
 | 
|---|
| 214 | 
 | 
|---|
| 215 |     LOG(4, "DEBUG: Current bin is " << bin << ", next bin is " << nextbin << ".");
 | 
|---|
| 216 | 
 | 
|---|
| 217 |     // Check first whether start or end actually fit into our histogram, if not extend.
 | 
|---|
| 218 |     extendMissingBins(bin.first, nextbin.first);
 | 
|---|
| 219 | 
 | 
|---|
| 220 |     // The bin will in general not fit into one bin in this histogram, but overlap.
 | 
|---|
| 221 |     // Hence, we determine the contribution of the bin to each bin in this histogram
 | 
|---|
| 222 |     // its overlaps into and add this weight to the bin.
 | 
|---|
| 223 |     Bins_t::const_iterator loweriter = getLowerEndBin(bin.first);
 | 
|---|
| 224 |     Bins_t::const_iterator upperiter = getHigherEndBin(nextbin.first);
 | 
|---|
| 225 | 
 | 
|---|
| 226 |     ASSERT( loweriter->first < upperiter->first,
 | 
|---|
| 227 |         "Histogram::superposeOtherHistogram() - the bin range is invalid.");
 | 
|---|
| 228 |     LOG(4, "DEBUG: bin range here is ["
 | 
|---|
| 229 |         << loweriter->first << ","  << upperiter->first << ").");
 | 
|---|
| 230 | 
 | 
|---|
| 231 |     // Next, we create a vector of offsets
 | 
|---|
| 232 |     typedef std::vector< BinLowerEnd > offsets_t;
 | 
|---|
| 233 |     offsets_t offsets;
 | 
|---|
| 234 |     {
 | 
|---|
| 235 |       offsets.push_back(bin.first);
 | 
|---|
| 236 |       Bins_t::const_iterator iter = loweriter;
 | 
|---|
| 237 |       for (++iter; iter != upperiter; ++iter)
 | 
|---|
| 238 |         if (offsets.back() != iter->first)
 | 
|---|
| 239 |           offsets.push_back(iter->first);
 | 
|---|
| 240 |       if (offsets.back() != nextbin.first)
 | 
|---|
| 241 |         offsets.push_back(nextbin.first);
 | 
|---|
| 242 |       LOG(3, "DEBUG: Offsets are " << offsets << ".");
 | 
|---|
| 243 |     }
 | 
|---|
| 244 | 
 | 
|---|
| 245 |     // then, we go through the offsets but the last one and add the respective area
 | 
|---|
| 246 |     {
 | 
|---|
| 247 |       offsets_t::const_iterator iter = offsets.begin();
 | 
|---|
| 248 |       offsets_t::const_iterator nextiter = ++offsets.begin();
 | 
|---|
| 249 |       for (; iter != --offsets.end(); ++iter, ++nextiter) {
 | 
|---|
| 250 |         const double length = *nextiter - *iter;
 | 
|---|
| 251 |         const double weight = bin.second * (length/binwidth);
 | 
|---|
| 252 |         Bins_t::iterator filliter = getLowerEndBin(*iter);
 | 
|---|
| 253 |         filliter->second += prefactor * weight;
 | 
|---|
| 254 |       }
 | 
|---|
| 255 |     }
 | 
|---|
| 256 | 
 | 
|---|
| 257 |     {
 | 
|---|
| 258 |       std::stringstream output;
 | 
|---|
| 259 |       std::for_each( bins.begin(), bins.end(), PrintPair<Bin_t>(output));
 | 
|---|
| 260 |       LOG(2, "DEBUG: Bins are after summation " << output.str() << ".");
 | 
|---|
| 261 |     }
 | 
|---|
| 262 |   }
 | 
|---|
| 263 | }
 | 
|---|
| 264 | 
 | 
|---|
| 265 | Histogram& Histogram::operator=(const Histogram &other)
 | 
|---|
| 266 | {
 | 
|---|
| 267 |   // check for self-assigment
 | 
|---|
| 268 |   if (&other != this) {
 | 
|---|
| 269 |     bins.clear();
 | 
|---|
| 270 |     const_cast<BinLowerEnd &>(offset) = other.offset;
 | 
|---|
| 271 |     const_cast<double &>(binwidth) = other.binwidth;
 | 
|---|
| 272 |     bins.insert(other.bins.begin(), other.bins.end());
 | 
|---|
| 273 |   }
 | 
|---|
| 274 |   return *this;
 | 
|---|
| 275 | }
 | 
|---|
| 276 | 
 | 
|---|
| 277 | Histogram& Histogram::operator+=(const Histogram &other)
 | 
|---|
| 278 | {
 | 
|---|
| 279 |   superposeOtherHistogram(other, +1.);
 | 
|---|
| 280 |   LOG(2, "DEBUG: Bins after addition are " << printBins() << ".");
 | 
|---|
| 281 |   return *this;
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | Histogram& Histogram::operator-=(const Histogram &other)
 | 
|---|
| 285 | {
 | 
|---|
| 286 |   superposeOtherHistogram(other, -1.);
 | 
|---|
| 287 |   LOG(2, "DEBUG: Bins after subtraction are " << printBins() << ".");
 | 
|---|
| 288 |   return *this;
 | 
|---|
| 289 | }
 | 
|---|
| 290 | 
 | 
|---|
| 291 | bool Histogram::isEmpty() const
 | 
|---|
| 292 | {
 | 
|---|
| 293 |   bool status = true;
 | 
|---|
| 294 |   for (Bins_t::const_iterator iter = bins.begin(); iter != bins.end(); ++iter)
 | 
|---|
| 295 |     status &= iter->second == 0;
 | 
|---|
| 296 |   return status;
 | 
|---|
| 297 | }
 | 
|---|
| 298 | 
 | 
|---|
| 299 | Histogram::Bins_t::iterator Histogram::getLowerEndBin(const double _value)
 | 
|---|
| 300 | {
 | 
|---|
| 301 |   // lower bound returns key that is equal or greater
 | 
|---|
| 302 |   Bins_t::iterator iter = bins.lower_bound(_value);
 | 
|---|
| 303 |   if (iter != bins.end()) {
 | 
|---|
| 304 |     // if we are not on the boundary we always have to step back by one
 | 
|---|
| 305 |     if (_value != iter->first) {
 | 
|---|
| 306 |       if (iter != bins.begin()) {
 | 
|---|
| 307 |           --iter;
 | 
|---|
| 308 |       } else {
 | 
|---|
| 309 |         iter = bins.end();
 | 
|---|
| 310 |       }
 | 
|---|
| 311 |     } else if (iter == --bins.end()) {
 | 
|---|
| 312 |       // if we actually are on boundary of "last bin", set to end
 | 
|---|
| 313 |       iter = bins.end();
 | 
|---|
| 314 |     }
 | 
|---|
| 315 |   }
 | 
|---|
| 316 |   return iter;
 | 
|---|
| 317 | }
 | 
|---|
| 318 | 
 | 
|---|
| 319 | Histogram::Bins_t::iterator Histogram::getHigherEndBin(const double _value)
 | 
|---|
| 320 | {
 | 
|---|
| 321 |   // upper bound return key that is strictly greater
 | 
|---|
| 322 |   Bins_t::iterator iter = bins.upper_bound(_value);
 | 
|---|
| 323 |   // if we are on the boundary we have to step back by one
 | 
|---|
| 324 |   if (iter != bins.end())
 | 
|---|
| 325 |     if (_value == iter->first)
 | 
|---|
| 326 |       if (iter != bins.begin())
 | 
|---|
| 327 |           --iter;
 | 
|---|
| 328 | 
 | 
|---|
| 329 |   return iter;
 | 
|---|
| 330 | }
 | 
|---|
| 331 | 
 | 
|---|
| 332 | Histogram::BinLowerEnd Histogram::getLowerEnd(const double _value) const
 | 
|---|
| 333 | {
 | 
|---|
| 334 |   BinLowerEnd start = _value - offset;
 | 
|---|
| 335 |   // then divide by binwidth
 | 
|---|
| 336 |   const int integral = floor(start/binwidth);
 | 
|---|
| 337 |   //const double fraction = fmod(start,binwidth);
 | 
|---|
| 338 |   start = offset + binwidth*integral;
 | 
|---|
| 339 |   return start;
 | 
|---|
| 340 | }
 | 
|---|
| 341 | 
 | 
|---|
| 342 | double Histogram::area() const
 | 
|---|
| 343 | {
 | 
|---|
| 344 |   double area = 0.;
 | 
|---|
| 345 |   for(Bins_t::const_iterator iter = bins.begin(); iter != bins.end(); ++iter)
 | 
|---|
| 346 |     area += iter->second*binwidth;
 | 
|---|
| 347 |   return area;
 | 
|---|
| 348 | }
 | 
|---|
| 349 | 
 | 
|---|
| 350 | template<> Histogram ZeroInstance<Histogram>()
 | 
|---|
| 351 | {
 | 
|---|
| 352 |   Histogram returnvalue;
 | 
|---|
| 353 |   return returnvalue;
 | 
|---|
| 354 | }
 | 
|---|