| 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 | 
 | 
|---|
| 101 | Histogram::Histogram(const samples_t &samples, const BinLowerEnd _offset, const double _binwidth) :
 | 
|---|
| 102 |     binwidth(_binwidth),
 | 
|---|
| 103 |     offset(_offset)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   addSamples(samples);
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void Histogram::addSamples(const samples_t &samples)
 | 
|---|
| 109 | {
 | 
|---|
| 110 |   // build histogram from samples
 | 
|---|
| 111 |   if (!samples.empty()) {
 | 
|---|
| 112 |     // 1. get min and max and determine width
 | 
|---|
| 113 |     samples_t::const_iterator maxiter = max_element(samples.begin(), samples.end());
 | 
|---|
| 114 |     samples_t::const_iterator miniter = min_element(samples.begin(), samples.end());
 | 
|---|
| 115 |     ASSERT((maxiter != samples.end()) || (miniter != samples.end()),
 | 
|---|
| 116 |         "Histogram::Histogram() - cannot find min/max despite non-empty range.");
 | 
|---|
| 117 | //    LOG(1, "DEBUG: min is " << *miniter << " and max is " << *maxiter << ".");
 | 
|---|
| 118 | 
 | 
|---|
| 119 |     // 2. create each bin
 | 
|---|
| 120 |     {
 | 
|---|
| 121 |       std::vector<Bin_t> vectorbins;
 | 
|---|
| 122 |       BinCreator_t BinCreator( getLowerEnd(*miniter), binwidth );
 | 
|---|
| 123 |       // we need one extra bin for get...Bin()'s find to work properly
 | 
|---|
| 124 |       const int CountBins = ceil((*maxiter - getLowerEnd(*miniter))/binwidth)+1;
 | 
|---|
| 125 |       vectorbins.resize(CountBins+1, Bin_t( make_pair(0., 0.) ) );
 | 
|---|
| 126 |       std::generate( vectorbins.begin(), vectorbins.end(), BinCreator );
 | 
|---|
| 127 |       bins.insert(vectorbins.begin(), vectorbins.end());
 | 
|---|
| 128 |     }
 | 
|---|
| 129 | 
 | 
|---|
| 130 |     // 3. place each sample into bin
 | 
|---|
| 131 |     BOOST_FOREACH( double value, samples) {
 | 
|---|
| 132 |       const Bins_t::iterator biniter = getLowerEndBin(value);
 | 
|---|
| 133 |       ASSERT( biniter != bins.end(),
 | 
|---|
| 134 |           "Histogram::Histogram() - cannot find bin for value from given samples.");
 | 
|---|
| 135 |       // (make bin count normalized, i.e. always equal to surface area of 1)
 | 
|---|
| 136 |       biniter->second += 1./binwidth;
 | 
|---|
| 137 |     }
 | 
|---|
| 138 |     LOG(2, "DEBUG: Bins are " << printBins() << ".");
 | 
|---|
| 139 |   } else {
 | 
|---|
| 140 | //    LOG(1, "INFO: Given vector of samples is empty.");
 | 
|---|
| 141 |   }
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | std::string Histogram::printBins() const
 | 
|---|
| 145 | {
 | 
|---|
| 146 |   std::stringstream output;
 | 
|---|
| 147 |   std::for_each( bins.begin(), bins.end(), PrintPair<Bin_t>(output));
 | 
|---|
| 148 |   return output.str();
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | void Histogram::extendMissingBins(const BinLowerEnd LowerEnd, const BinLowerEnd NextLowerEnd)
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   Bins_t::const_iterator loweriter = getLowerEndBin(LowerEnd);
 | 
|---|
| 154 |   Bins_t::const_iterator upperiter = getHigherEndBin(NextLowerEnd);
 | 
|---|
| 155 |   if (loweriter == bins.end()) {
 | 
|---|
| 156 |     // we need bins beneath our first, add them
 | 
|---|
| 157 |     for(BinLowerEnd offset = getLowerEnd(LowerEnd);
 | 
|---|
| 158 |         offset < getLowerEnd(NextLowerEnd);
 | 
|---|
| 159 |         offset += binwidth)
 | 
|---|
| 160 |       bins.insert( std::make_pair (offset, 0. ) );
 | 
|---|
| 161 | //    LOG(2, "DEBUG: Bins after adding empties before start are " << printBins() << ".");
 | 
|---|
| 162 |     loweriter = getLowerEndBin(LowerEnd);
 | 
|---|
| 163 |     ASSERT( loweriter != bins.end(),
 | 
|---|
| 164 |         "Histogram::extendMissingBins() - still no lower bound after adding empties.");
 | 
|---|
| 165 |   }
 | 
|---|
| 166 |   if (upperiter == bins.end()) {
 | 
|---|
| 167 |     // we need bins after our last, add them
 | 
|---|
| 168 |     for(BinLowerEnd offset = getLowerEnd(LowerEnd);
 | 
|---|
| 169 |         offset <= getLowerEnd(NextLowerEnd)+binwidth;
 | 
|---|
| 170 |         offset += binwidth)
 | 
|---|
| 171 |       bins.insert( std::make_pair (offset, 0. ) );
 | 
|---|
| 172 | //    LOG(2, "DEBUG: Bins after adding empties after end are " << printBins() << ".");
 | 
|---|
| 173 |     upperiter = getHigherEndBin(NextLowerEnd);
 | 
|---|
| 174 |     ASSERT( upperiter != bins.end(),
 | 
|---|
| 175 |         "Histogram::extendMissingBins() - still no upper bound after adding empties.");
 | 
|---|
| 176 |   }
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | void Histogram::superposeOtherHistogram(const Histogram &other, const double prefactor)
 | 
|---|
| 180 | {
 | 
|---|
| 181 |   // go through each of the other histogram's bins
 | 
|---|
| 182 |   Bins_t::const_iterator enditer = --other.bins.end(); // (except internal last one)
 | 
|---|
| 183 |   for (Bins_t::const_iterator biniter = other.bins.begin();
 | 
|---|
| 184 |       biniter != enditer; /* we advance ourselves in loop */) {
 | 
|---|
| 185 |     const Bin_t &bin = *biniter;
 | 
|---|
| 186 |     ++biniter;
 | 
|---|
| 187 |     const Bin_t &nextbin = *biniter;
 | 
|---|
| 188 | 
 | 
|---|
| 189 | //    LOG(2, "DEBUG: Current bin is " << bin << ", next bin is " << nextbin << ".");
 | 
|---|
| 190 | 
 | 
|---|
| 191 |     // Check first whether start or end actually fit into our histogram, if not extend.
 | 
|---|
| 192 |     extendMissingBins(bin.first, nextbin.first);
 | 
|---|
| 193 | 
 | 
|---|
| 194 |     // The bin will in general not fit into one bin in this histogram, but overlap.
 | 
|---|
| 195 |     // Hence, we determine the contribution of the bin to each bin in this histogram
 | 
|---|
| 196 |     // its overlaps into and add this weight to the bin.
 | 
|---|
| 197 |     Bins_t::const_iterator loweriter = getLowerEndBin(bin.first);
 | 
|---|
| 198 |     Bins_t::const_iterator upperiter = getHigherEndBin(nextbin.first);
 | 
|---|
| 199 | 
 | 
|---|
| 200 |     ASSERT( loweriter->first < upperiter->first,
 | 
|---|
| 201 |         "Histogram::superposeOtherHistogram() - the bin range is invalid.");
 | 
|---|
| 202 | //    LOG(2, "DEBUG: bin range here is ["
 | 
|---|
| 203 | //        << loweriter->first << ","  << upperiter->first << ").");
 | 
|---|
| 204 | 
 | 
|---|
| 205 |     // Next, we create a vector of offsets
 | 
|---|
| 206 |     typedef std::vector< BinLowerEnd > offsets_t;
 | 
|---|
| 207 |     offsets_t offsets;
 | 
|---|
| 208 |     {
 | 
|---|
| 209 |       offsets.push_back(bin.first);
 | 
|---|
| 210 |       Bins_t::const_iterator iter = loweriter;
 | 
|---|
| 211 |       for (++iter; iter != upperiter; ++iter)
 | 
|---|
| 212 |         if (offsets.back() != iter->first)
 | 
|---|
| 213 |           offsets.push_back(iter->first);
 | 
|---|
| 214 |       if (offsets.back() != nextbin.first)
 | 
|---|
| 215 |         offsets.push_back(nextbin.first);
 | 
|---|
| 216 | //      LOG(2, "DEBUG: Offsets are " << offsets << ".");
 | 
|---|
| 217 |     }
 | 
|---|
| 218 | 
 | 
|---|
| 219 |     // then, we go through the offsets but the last one and add the respective area
 | 
|---|
| 220 |     {
 | 
|---|
| 221 |       offsets_t::const_iterator iter = offsets.begin();
 | 
|---|
| 222 |       offsets_t::const_iterator nextiter = ++offsets.begin();
 | 
|---|
| 223 |       for (; iter != --offsets.end(); ++iter, ++nextiter) {
 | 
|---|
| 224 |         const double length = *nextiter - *iter;
 | 
|---|
| 225 |         const double weight = bin.second * (length/binwidth);
 | 
|---|
| 226 |         Bins_t::iterator filliter = getLowerEndBin(*iter);
 | 
|---|
| 227 |         filliter->second += prefactor * weight;
 | 
|---|
| 228 |       }
 | 
|---|
| 229 |     }
 | 
|---|
| 230 | 
 | 
|---|
| 231 |     {
 | 
|---|
| 232 |       std::stringstream output;
 | 
|---|
| 233 |       std::for_each( bins.begin(), bins.end(), PrintPair<Bin_t>(output));
 | 
|---|
| 234 |       LOG(2, "DEBUG: Bins are after summation " << output.str() << ".");
 | 
|---|
| 235 |     }
 | 
|---|
| 236 |   }
 | 
|---|
| 237 | }
 | 
|---|
| 238 | 
 | 
|---|
| 239 | Histogram& Histogram::operator+=(const Histogram &other)
 | 
|---|
| 240 | {
 | 
|---|
| 241 |   superposeOtherHistogram(other, +1.);
 | 
|---|
| 242 |   LOG(2, "DEBUG: Bins after addition are " << printBins() << ".");
 | 
|---|
| 243 |   return *this;
 | 
|---|
| 244 | }
 | 
|---|
| 245 | 
 | 
|---|
| 246 | Histogram& Histogram::operator-=(const Histogram &other)
 | 
|---|
| 247 | {
 | 
|---|
| 248 |   superposeOtherHistogram(other, -1.);
 | 
|---|
| 249 |   LOG(2, "DEBUG: Bins after subtraction are " << printBins() << ".");
 | 
|---|
| 250 |   return *this;
 | 
|---|
| 251 | }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | bool Histogram::isEmpty() const
 | 
|---|
| 254 | {
 | 
|---|
| 255 |   bool status = true;
 | 
|---|
| 256 |   for (Bins_t::const_iterator iter = bins.begin(); iter != bins.end(); ++iter)
 | 
|---|
| 257 |     status &= iter->second == 0;
 | 
|---|
| 258 |   return status;
 | 
|---|
| 259 | }
 | 
|---|
| 260 | 
 | 
|---|
| 261 | Histogram::Bins_t::iterator Histogram::getLowerEndBin(const double _value)
 | 
|---|
| 262 | {
 | 
|---|
| 263 |   // lower bound returns key that is equal or greater
 | 
|---|
| 264 |   Bins_t::iterator iter = bins.lower_bound(_value);
 | 
|---|
| 265 |   if (iter != bins.end()) {
 | 
|---|
| 266 |     // if we are not on the boundary we always have to step back by one
 | 
|---|
| 267 |     if (_value != iter->first) {
 | 
|---|
| 268 |       if (iter != bins.begin()) {
 | 
|---|
| 269 |           --iter;
 | 
|---|
| 270 |       } else {
 | 
|---|
| 271 |         iter = bins.end();
 | 
|---|
| 272 |       }
 | 
|---|
| 273 |     } else if (iter == --bins.end()) {
 | 
|---|
| 274 |       // if we actually are on boundary of "last bin", set to end
 | 
|---|
| 275 |       iter = bins.end();
 | 
|---|
| 276 |     }
 | 
|---|
| 277 |   }
 | 
|---|
| 278 |   return iter;
 | 
|---|
| 279 | }
 | 
|---|
| 280 | 
 | 
|---|
| 281 | Histogram::Bins_t::iterator Histogram::getHigherEndBin(const double _value)
 | 
|---|
| 282 | {
 | 
|---|
| 283 |   // upper bound return key that is strictly greater
 | 
|---|
| 284 |   Bins_t::iterator iter = bins.upper_bound(_value);
 | 
|---|
| 285 |   // if we are on the boundary we have to step back by one
 | 
|---|
| 286 |   if (iter != bins.end())
 | 
|---|
| 287 |     if (_value == iter->first)
 | 
|---|
| 288 |       if (iter != bins.begin())
 | 
|---|
| 289 |           --iter;
 | 
|---|
| 290 | 
 | 
|---|
| 291 |   return iter;
 | 
|---|
| 292 | }
 | 
|---|
| 293 | 
 | 
|---|
| 294 | Histogram::BinLowerEnd Histogram::getLowerEnd(const double _value) const
 | 
|---|
| 295 | {
 | 
|---|
| 296 |   BinLowerEnd start = _value - offset;
 | 
|---|
| 297 |   // then divide by binwidth
 | 
|---|
| 298 |   const int integral = floor(start/binwidth);
 | 
|---|
| 299 |   //const double fraction = fmod(start,binwidth);
 | 
|---|
| 300 |   start = offset + binwidth*integral;
 | 
|---|
| 301 |   return start;
 | 
|---|
| 302 | }
 | 
|---|
| 303 | 
 | 
|---|
| 304 | double Histogram::area() const
 | 
|---|
| 305 | {
 | 
|---|
| 306 |   double area = 0.;
 | 
|---|
| 307 |   for(Bins_t::const_iterator iter = bins.begin(); iter != bins.end(); ++iter)
 | 
|---|
| 308 |     area += iter->second*binwidth;
 | 
|---|
| 309 |   return area;
 | 
|---|
| 310 | }
 | 
|---|