| 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 | Histogram::Histogram(const samples_t &samples, const int _CountBins) :
|
|---|
| 93 | binwidth(0.5),
|
|---|
| 94 | CountBins(_CountBins)
|
|---|
| 95 | {
|
|---|
| 96 | // build histogram from samples
|
|---|
| 97 | if (!samples.empty()) {
|
|---|
| 98 | if (CountBins == -1) {
|
|---|
| 99 | CountBins = ceil(pow(samples.size(), 1./3.));
|
|---|
| 100 | }
|
|---|
| 101 | // 2. get min and max and determine width
|
|---|
| 102 | samples_t::const_iterator maxiter = max_element(samples.begin(), samples.end());
|
|---|
| 103 | samples_t::const_iterator miniter = min_element(samples.begin(), samples.end());
|
|---|
| 104 | ASSERT((maxiter != samples.end()) || (miniter != samples.end()),
|
|---|
| 105 | "Histogram::Histogram() - cannot find min/max despite non-empty range.");
|
|---|
| 106 | LOG(1, "DEBUG: min is " << *miniter << " and max is " << *maxiter << ".");
|
|---|
| 107 | binwidth = (*maxiter - *miniter)/(CountBins-1);
|
|---|
| 108 |
|
|---|
| 109 | // 3. create each bin
|
|---|
| 110 | BinCreator_t BinCreator( *miniter, binwidth );
|
|---|
| 111 | std::vector<Bin_t> vectorbins;
|
|---|
| 112 | // we need one extra bin for get...Bin()'s find to work properly
|
|---|
| 113 | vectorbins.resize(CountBins+1, Bin_t( make_pair(0., 0.) ) );
|
|---|
| 114 | std::generate( vectorbins.begin(), vectorbins.end(), BinCreator );
|
|---|
| 115 | bins.insert(vectorbins.begin(), vectorbins.end());
|
|---|
| 116 |
|
|---|
| 117 | // 4. place each sample into bin
|
|---|
| 118 | BOOST_FOREACH( double value, samples) {
|
|---|
| 119 | const Bins_t::iterator biniter = getLowerEndBin(value);
|
|---|
| 120 | ASSERT( biniter != bins.end(),
|
|---|
| 121 | "Histogram::Histogram() - cannot find bin for value from given samples.");
|
|---|
| 122 | biniter->second += 1.;
|
|---|
| 123 | }
|
|---|
| 124 | std::stringstream output;
|
|---|
| 125 | std::for_each( bins.begin(), bins.end(), PrintPair<Bin_t>(output));
|
|---|
| 126 | LOG(2, "DEBUG: Bins are " << output.str() << ".");
|
|---|
| 127 | } else {
|
|---|
| 128 | LOG(1, "INFO: Given vector of samples is empty.");
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | Histogram& Histogram::operator+=(const Histogram &other)
|
|---|
| 133 | {
|
|---|
| 134 | return *this;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | Histogram& Histogram::operator-=(const Histogram &other)
|
|---|
| 138 | {
|
|---|
| 139 | return *this;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | bool Histogram::isEmpty() const
|
|---|
| 143 | {
|
|---|
| 144 | bool status = true;
|
|---|
| 145 | for (Bins_t::const_iterator iter = bins.begin(); iter != bins.end(); ++iter)
|
|---|
| 146 | status &= iter->second == 0;
|
|---|
| 147 | return status;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | Histogram::Bins_t::iterator Histogram::getLowerEndBin(const double _value)
|
|---|
| 151 | {
|
|---|
| 152 | // lower bound returns key that is equal or greater
|
|---|
| 153 | Bins_t::iterator iter = bins.lower_bound(_value);
|
|---|
| 154 | if (iter != bins.end()) {
|
|---|
| 155 | // if we are not on the boundary we always have to step back by one
|
|---|
| 156 | if (_value != iter->first) {
|
|---|
| 157 | if (iter != bins.begin()) {
|
|---|
| 158 | --iter;
|
|---|
| 159 | } else {
|
|---|
| 160 | iter = bins.end();
|
|---|
| 161 | }
|
|---|
| 162 | } else if (iter == --bins.end()) {
|
|---|
| 163 | // if we actually are on boundary of "last bin", set to end
|
|---|
| 164 | iter = bins.end();
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 | return iter;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | Histogram::Bins_t::iterator Histogram::getHigherEndBin(const double _value)
|
|---|
| 171 | {
|
|---|
| 172 | // upper bound return key that is strictly greater
|
|---|
| 173 | Bins_t::iterator iter = bins.upper_bound(_value);
|
|---|
| 174 | // if we are on the boundary we have to step back by one
|
|---|
| 175 | if (iter != bins.end())
|
|---|
| 176 | if (_value == iter->first)
|
|---|
| 177 | if (iter != bins.begin())
|
|---|
| 178 | --iter;
|
|---|
| 179 |
|
|---|
| 180 | return iter;
|
|---|
| 181 | }
|
|---|