Debian Codegraph @

src/non_api/Histogram.h

raw symbols SCIP
1 #ifndef SEQLIB_HISTOGRAM_H__
2 #define SEQLIB_HISTOGRAM_H__
3
4 #include <iostream>
5 #include <cassert>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 #include <fstream>
10
11 #include "IntervalTree.h"
12
13 class Bin;
14
15 typedef TInterval<Bin> BinInterval;
16 typedef TIntervalTree<Bin> BinIntervalTree;
17 typedef std::vector<BinInterval> BinIntervalVector;
18
19 #define INTERCHR 250000000
20
21 namespace SeqLib {
22
23 class Histogram;
24
25 /** Stores one bin in a Histogram
26 */
27 class Bin {
28
29 friend class Histogram;
30
31 public:
32
33 /** Construct a new object with 0 count and range [0,1]
34 */
35 Bin() : m_count(0)
36 {
37 bounds = {0,1};
38 }
39
40 /** Output the bin in format "start range, end range, count"
41 */
42 friend std::ostream& operator<<(std::ostream &out, const Bin &b) {
43 out << b.bounds.first << "," << b.bounds.second << "," << b.m_count;
44 return out;
45 }
46
47 /** Return the number of counts in this histogram bin
48 */
49 int32_t getCount() const { return m_count; }
50
51 /** Check if a value fits within the range of this bin
52 * @param dist Distance value to check if its in this range
53 * @return true if the value is within the range
54 */
55
56 /** Check if this bin contains a value
57 * @param elem Value to check if its in this range
58 * @return true if the value is within the range
59 */
60 bool contains(const int32_t& elem) const;
61
62 /** Define bin comparison operator by location of left bound, then right */
63 bool operator < (const Bin& b) const;
64
65 /** Decrement the histogram bin by one.
66 * Note that this is the prefix version only
67 */
68 Bin& operator--();
69
70 /** Increment the histogram bin by one.
71 * Note that this is the prefix version only
72 */
73 Bin& operator++();
74
75 private:
76 int32_t m_count;
77 std::pair<int32_t, int32_t> bounds; //@! was"bin";
78 };
79
80 /** Class to store histogram of numeric values.
81 *
82 * The bins of the Histogram are not uniformly spaced, and their ranges determined
83 * by partitioning the spans it tablulates into uniform quantiles when initialized
84 * by Histogram::initialSpans(). As elements are added and removed this initial bin
85 * definition remains constant.
86 */
87 class Histogram {
88
89 private:
90
91 std::vector<int32_t> m_ind;
92
93 public:
94
95 std::vector<Bin> m_bins;
96 /** Construct an empty histogram
97 */
98 Histogram() {}
99
100 /** Construct a new histogram with bins spaced evenly
101 * @param start Min value covered
102 * @param end Max value covered
103 * @param width Fixed bin width
104 * @exception Throws an invalid_argument if end <= start
105 */
106 Histogram(const int32_t& start, const int32_t& end, const uint32_t& width);
107
108 std::string toFileString() const;
109
110 friend std::ostream& operator<<(std::ostream &out, const Histogram &h) {
111 for (auto& i : h.m_bins)
112 out << i << std::endl;
113 return out;
114 }
115
116 /** Return iterator to the fist bin
117 */
118 std::vector<int32_t>::iterator begin() { return m_ind.begin(); }
119
120 /** Return iterator to the last bin
121 */
122 std::vector<int32_t>::iterator end() { return m_ind.end(); }
123
124 /** Initialize histogram from a vector of numeric values
125 */
126 void Initialize(size_t num_bins, std::vector<int32_t>* pspanv, size_t min_bin_width = 0);
127
128 /** Add an element to the histogram
129 * @param elem Length of event to add
130 */
131 void addElem(const int32_t &elem);
132
133 /** Remove a span from the histogram
134 * @param span Length of event to remove
135 */
136 void removeElem(const int32_t &elem);
137
138 /** Output to CSV file like: bin_start,bin_end,count
139 */
140 void toCSV(std::ofstream &fs);
141
142 /** Return the total number of elements in the Histogram
143 */
144 int totalCount() const {
145 int tot = 0;
146 for (auto& i : m_bins)
147 tot += i.getCount();
148 return tot;
149 }
150
151 /** Get count for a histogram bin
152 * @param i Bin index
153 * @return number of events in histogram bin
154 */
155 int32_t binCount(size_t i) { return m_bins[i].getCount(); }
156
157 /** Get number of bins in histogram
158 * @return Number of bins in histogram
159 */
160 size_t numBins() { return m_bins.size(); }
161
162 /** Find bin corresponding to a span
163 * @param elem Event length
164 * @return Bin containing event length
165 */
166 size_t retrieveBinID(const int32_t& elem) const;
167
168 };
169
170 }
171 #endif