status.hh 5.85 KB
Newer Older
1
2
3
4
5
6
7
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program is free software: you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
Dennis Gläser's avatar
Dennis Gläser committed
8
 *   the Free Software Foundation, either version 3 of the License, or       *
9
10
11
12
13
14
15
16
17
18
19
20
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
/*!
 * \file
21
 * \ingroup Sampling
22
23
24
25
26
27
 * \brief Class that can be used to define target counters in
 *        sampling procedures and which outputs the current status.
 */
#ifndef FRACKIT_SAMPLING_STATUS_HH
#define FRACKIT_SAMPLING_STATUS_HH

28
29
#include <iomanip>
#include <iostream>
30
#include <algorithm>
31
#include <numeric>
32
33
34
35
36
37
38
39
#include <unordered_map>
#include <initializer_list>

#include <frackit/common/id.hh>

namespace Frackit {

/*!
40
 * \ingroup Sampling
41
42
43
44
45
46
47
48
49
 * \brief Class that can be used to define target counters in
 *        sampling procedures and which outputs the current status.
 */
class SamplingStatus
{

public:

    /*!
50
     * \brief Set the target sample count for a specific id.
51
52
53
54
55
56
57
58
     */
    void setTargetCount(const Id& id, std::size_t targetCount)
    {
        targetCount_[id.get()] = targetCount;

        auto it = count_.find(id.get());
        if (it == count_.end())
            count_[id.get()] = 0;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        else if (it->second > targetCount)
            std::cout << "Warning: given target count is below current count" << std::endl;
    }

    /*!
     * \brief Reset all counters.
     */
    void resetCounters()
    {
        for (auto& count : count_) count.second = 0;
    }

    /*!
     * \brief Reset counter for the given id.
     */
    void resetCounter(const Id& id)
    {
        count_[id.get()] = 0;
77
78
79
    }

    /*!
80
81
     * \brief Returns true when the target
     *        sample count is reached.
82
83
84
85
86
87
88
89
90
91
     */
    bool finished()
    {
        for (const auto& pair : count_)
            if (pair.second != targetCount_.at(pair.first))
                return false;
        return true;
    }

    /*!
92
93
     * \brief Returns true when the target count
     *        for a specific id is reached.
94
95
96
97
98
99
100
101
102
103
     */
    bool finished(const Id& id)
    {
        auto it = count_.find(id.get());
        if (it == count_.end())
            throw std::runtime_error("Target count not set for given id");
        return it->second == targetCount_.at(it->first);
    }

    /*!
104
     * \brief Increase counter for a specific id.
105
106
107
108
109
110
111
     */
    void increaseCounter(const Id& id)
    {
        count_[id.get()]++;
    }

    /*!
112
     * \brief Increase counter of rejected samples.
113
114
115
116
117
118
     */
    void increaseRejectedCounter()
    {
        rejectedCount_++;
    }

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    /*!
     * \brief Returns the entity count for the given id.
     */
    std::size_t getCount(const Id& id)
    { return count_[id.get()]; }

    /*!
     * \brief Returns the overall entity count.
     */
    std::size_t getCount()
    {
        return std::accumulate(count_.begin(),
                               count_.end(),
                               0,
                               [] (const auto& curCount, const auto& idCountPair)
                               { return curCount + idCountPair.second; });
    }

137
    /*!
138
     * \brief Print current status to terminal.
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
     */
    void print(bool forceHeaderPrint = false)
    {
        if (!headerPrinted_ || forceHeaderPrint)
        {
            std::cout << "#################################################################################\n"
                      << "# Accepted count   |   Rejected count   |   Acceptance Ratio   |   Progress [%] #\n"
                      << "#-------------------------------------------------------------------------------#\n";
            headerPrinted_ = true;
        }

        std::size_t curCount = 0;
        std::size_t curTargetCount = 0;
        for (const auto& pair : count_) curCount += pair.second;
        for (const auto& pair : targetCount_) curTargetCount += pair.second;

        const auto ratio = double(double(curCount)/double(curCount+rejectedCount_));
        const auto progress = 100.0*double(curCount)/double(curTargetCount);

        std::cout << "  "   << std::setw(17) << std::setfill(' ')
                            << std::to_string(curCount) + std::string(9, ' ')
                  << "|   " << std::setprecision(6) << std::setw(17) << std::setfill(' ')
                            << std::to_string(rejectedCount_) + std::string(9, ' ')
                  << "|   " << std::setprecision(6) << std::setw(19) << std::setfill(' ')
                            << std::to_string( ratio ) + std::string(7, ' ');
        std::cout << "|   " << std::setprecision(2) << std::string(2, ' ') + std::to_string( progress ) << std::endl;
    }

private:
    bool headerPrinted_ = false;
    std::size_t rejectedCount_ = 0;
    std::unordered_map<std::size_t, std::size_t> count_;
    std::unordered_map<std::size_t, std::size_t> targetCount_;
};

} // end namespace Frackit

#endif // FRACKIT_SAMPLING_STATUS_HH