barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
statscounter-meat.hpp
Go to the documentation of this file.
1 #ifndef BARRY_STATSCOUNTER_MEAT_HPP
2 #define BARRY_STATSCOUNTER_MEAT_HPP 1
3 
4 #define STATSCOUNTER_TYPE() StatsCounter<Array_Type,Data_Type>
5 
6 #define STATSCOUNTER_TEMPLATE_ARGS() <typename Array_Type, typename Data_Type>
7 
8 #define STATSCOUNTER_TEMPLATE(a,b) \
9  template STATSCOUNTER_TEMPLATE_ARGS() inline a STATSCOUNTER_TYPE()::b
10 
13 )
14 {
15 
16  Array = counter.Array;
17  EmptyArray = *Array;
18  EmptyArray.clear();
19  current_stats = counter.current_stats;
20 
21  // We will save the data here
23  counter_deleted = false;
24 
25 }
26 
28 {
29  if (!counter_deleted)
30  delete counters;
31  return;
32 }
33 
34 STATSCOUNTER_TEMPLATE(void, reset_array)(const Array_Type * Array_)
35 {
36 
37  Array = Array_;
38  EmptyArray = *Array_;
39  EmptyArray.clear();
40 
41  return;
42 }
43 
45 {
46 
47  counters->add_counter(f_);
48 
49  return;
50 
51 }
52 
54 {
55 
56  // Cleaning up before replacing the memory
57  if (!counter_deleted)
58  delete counters;
59  counter_deleted = true;
61 
62  return;
63 
64 }
65 
66 STATSCOUNTER_TEMPLATE(void, count_init)(size_t i,size_t j)
67 {
68 
69  // Do we have any counter?
70  if (counters->size() == 0u)
71  throw std::logic_error("No counters added: Cannot count without knowning what to count!");
72 
73  // Iterating through the functions, and updating the set of
74  // statistics.
75  current_stats.resize(counters->size(), 0.0);
76  // change_stats.resize(counters->size(), 0.0);
77  for (size_t n = 0u; n < counters->size(); ++n)
78  current_stats[n] = counters->operator[](n).init(EmptyArray, i, j);
79 
80  return;
81 }
82 
83 STATSCOUNTER_TEMPLATE(void, count_current)(size_t i, size_t j)
84 {
85 
86  // Iterating through the functions, and updating the set of
87  // statistics.
88  for (size_t n = 0u; n < counters->size(); ++n) {
89  // change_stats[n] = counters->operator[](n).count(EmptyArray, i, j);
90  // current_stats[n] += change_stats[n];
91  current_stats[n] += counters->operator[](n).count(EmptyArray, i, j);
92  }
93 
94  return;
95 
96 }
97 
98 template<typename Array_Type, typename Data_Type>
99 inline std::vector< double > StatsCounter<Array_Type,Data_Type>::count_all()
100 {
101 
102  if (Array->is_dense())
103  {
104  return count_all_dense();
105  }
106  else
107  {
108  return count_all_sparse();
109  }
110 
111 }
112 
113 template<typename Array_Type, typename Data_Type>
114 inline std::vector< double > StatsCounter<Array_Type,Data_Type>::count_all_sparse()
115 {
116 
117  // Initializing the counter on the empty array
118  count_init(0u, 0u);
119 
120  // Setting it to zero.
121  EmptyArray.clear(false);
122 
123  #ifdef BARRY_DEBUG_LEVEL
124  #if BARRY_DEBUG_LEVEL > 0
125  BARRY_DEBUG_MSG("Initializing -count_all- debug. get_names():")
126  BARRY_DEBUG_VEC_PRINT<std::string>(this->get_names());
127  #endif
128  #endif
129 
130  // Start iterating through the data
131  for (size_t i = 0; i < Array->nrow(); ++i)
132  {
133 
134  const auto & row = Array->row(i, false);
135 
136  // Any element?
137  if (row.size() == 0u)
138  continue;
139 
140  // If there's one, then update the statistic, by iterating
141  for (const auto& col: row)
142  {
143 
144  // We only insert if it is different from zero
145  if (static_cast<int>(col.second.value) == 0)
146  continue;
147 
148  // Adding a cell
149  EmptyArray.insert_cell(i, col.first, col.second, false, false);
150 
151  #ifdef BARRY_DEBUG_LEVEL
152  #if (BARRY_DEBUG_LEVEL >= 1)
153  BARRY_DEBUG_MSG("================================================================================")
154  BARRY_DEBUG_MSG("Debugging Stats counter: current_stats (before)")
155  std::string tmpmgs = "Inserting cell (" +
156  std::to_string(i) + ", " + std::to_string(col.first) + ")";
157  BARRY_DEBUG_MSG(tmpmgs.c_str());
158  BARRY_DEBUG_VEC_PRINT(current_stats);
159  #if (BARRY_DEBUG_LEVEL >= 2)
160  BARRY_DEBUG_MSG("Debugging Stats counter: EmptyArray")
161  EmptyArray.print();
162  #endif
163  #endif
164  #endif
165 
166  // Computing the change statistics
167  count_current(i, col.first);
168  #ifdef BARRY_DEBUG_LEVEL
169  #if (BARRY_DEBUG_LEVEL >= 1)
170  BARRY_DEBUG_MSG("Debugging Stats counter: current_stats (after)")
171  BARRY_DEBUG_VEC_PRINT(current_stats);
172  #endif
173  #endif
174 
175  }
176 
177  }
178 
179  // Adding to the sufficient statistics
180  return current_stats;
181 
182 }
183 
184 template<typename Array_Type, typename Data_Type>
185 inline std::vector< double > StatsCounter<Array_Type,Data_Type>::count_all_dense()
186 {
187 
188  // Initializing the counter on the empty array
189  count_init(0u, 0u);
190 
191  // Setting it to zero.
192  EmptyArray.clear(false);
193 
194  #ifdef BARRY_DEBUG_LEVEL
195  #if BARRY_DEBUG_LEVEL > 0
196  BARRY_DEBUG_MSG("Initializing -count_all- debug. get_names():")
197  BARRY_DEBUG_VEC_PRINT<std::string>(this->get_names());
198  #endif
199  #endif
200 
201  // Start iterating through the data
202  for (size_t i = 0u; i < Array->nrow(); ++i)
203  {
204 
205  for (size_t j = 0u; j < Array->ncol(); ++j)
206  {
207  // We only insert if it is different from zero
208  if (Array->is_empty(i,j))
209  continue;
210 
211  // Adding a cell
212  EmptyArray.insert_cell(i, j, 1, false, false);
213 
214  #ifdef BARRY_DEBUG_LEVEL
215  #if (BARRY_DEBUG_LEVEL >= 1)
216  BARRY_DEBUG_MSG("================================================================================")
217  BARRY_DEBUG_MSG("Debugging Stats counter: current_stats (before)")
218  std::string tmpmgs = "Inserting cell (" +
219  std::to_string(i) + ", " + std::to_string(col.first) + ")";
220  BARRY_DEBUG_MSG(tmpmgs.c_str());
221  BARRY_DEBUG_VEC_PRINT(current_stats);
222  #if (BARRY_DEBUG_LEVEL >= 2)
223  BARRY_DEBUG_MSG("Debugging Stats counter: EmptyArray")
224  EmptyArray.print();
225  #endif
226  #endif
227  #endif
228 
229  // Computing the change statistics
230  count_current(i, j);
231  #ifdef BARRY_DEBUG_LEVEL
232  #if (BARRY_DEBUG_LEVEL >= 1)
233  BARRY_DEBUG_MSG("Debugging Stats counter: current_stats (after)")
234  BARRY_DEBUG_VEC_PRINT(current_stats);
235  #endif
236  #endif
237  }
238 
239  }
240 
241  // Adding to the sufficient statistics
242  return current_stats;
243 
244 }
245 
247 inline Counters<Array_Type,Data_Type> * STATSCOUNTER_TYPE()::get_counters() {
248  return this->counters;
249 }
250 
251 STATSCOUNTER_TEMPLATE(std::vector< std::string >, get_names)() const
252 {
253  return this->counters->get_names();
254 }
255 
256 STATSCOUNTER_TEMPLATE(std::vector< std::string >, get_descriptions)() const
257 {
258  return this->counters->get_descriptions();
259 }
260 
261 #undef STATSCOUNTER_TYPE
262 #undef STATSCOUNTER_TEMPLATE_ARGS
263 #undef STATSCOUNTER_TEMPLATE
264 
265 #endif
return * this
A counter function based on change statistics.
Count stats for a single Array.
std::vector< double > count_all()
size_t i
Data_Type * counters_
#define STATSCOUNTER_TEMPLATE(a, b)
counter_deleted
#define STATSCOUNTER_TEMPLATE_ARGS()
size_t j
#define STATSCOUNTER_TYPE()
current_stats
Data_Type & counter
Data_Type f_