Developer documentation
Version 3.0.3-105-gd3941f44
gt.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __gt_gt_h__
18#define __gt_gt_h__
19
20#define ITER_BIGSTEP 10000
21#define FRAC_BURNIN 10
22#define FRAC_PHASEOUT 10
23
24#include <iostream>
25#include <mutex>
26
27#include <Eigen/Dense>
28
29#include "progressbar.h"
30#include "types.h"
31
32
33namespace MR {
34 namespace DWI {
35 namespace Tractography {
36 namespace GT {
37
38 const double M_4PI = 4.0 * Math::pi;
39 const double M_sqrt4PI = std::sqrt(M_4PI);
40
41
44 float p_birth;
45 float p_death;
46 float p_shift;
48 float p_connect;
49
50 double density;
51 double weight;
52 int Lmax;
53
54 double lam_ext;
55 double lam_int;
56
57 double beta;
58 double ppot;
59
60 Eigen::MatrixXf resp_WM;
62
63 };
64
65
66
67 class Stats
68 { MEMALIGN(Stats)
69 public:
70
71 Stats(const double T0, const double T1, const uint64_t maxiter)
72 : Text(T1), Tint(T0), EextTot(0.0), EintTot(0.0), n_iter(0), n_max(maxiter),
73 progress("running MH sampler", n_max/ITER_BIGSTEP)
74 {
75 for (int k = 0; k != 5; k++)
76 n_gen[k] = n_acc[k] = 0;
77 alpha = std::pow(T1/T0, double(ITER_BIGSTEP)/double(n_max - n_max/FRAC_BURNIN - n_max/FRAC_PHASEOUT));
78 }
79
80 ~Stats() {
81 out.close();
82 }
83
84
85 void open_stream(const std::string& file) {
86 out.close();
87 out.open(file.c_str(), std::ofstream::out);
88 }
89
90
91 bool next() {
92 std::lock_guard<std::mutex> lock (mutex);
93 ++n_iter;
94 if (n_iter % ITER_BIGSTEP == 0) {
96 Tint *= alpha;
97 progress++;
98 out << *this << std::endl;
99 }
100 return (n_iter < n_max);
101 }
102
103
104 // getters and setters ----------------------------------------------
105
106 double getText() const {
107 return Text;
108 }
109
110 double getTint() const {
111 return Tint;
112 }
113
114 void setTint(double temp) {
115 std::lock_guard<std::mutex> lock (mutex);
116 Tint = temp;
117 }
118
119
120 double getEextTotal() const {
121 return EextTot;
122 }
123
124 double getEintTotal() const {
125 return EintTot;
126 }
127
128 void incEextTotal(double d) {
129 std::lock_guard<std::mutex> lock (mutex);
130 EextTot += d;
131 }
132
133 void incEintTotal(double d) {
134 std::lock_guard<std::mutex> lock (mutex);
135 EintTot += d;
136 }
137
138
139 unsigned int getN(const char p) const {
140 switch (p) {
141 case 'b': return n_gen[0];
142 case 'd': return n_gen[1];
143 case 'r': return n_gen[2];
144 case 'o': return n_gen[3];
145 case 'c': return n_gen[4];
146 default: return 0;
147 }
148 }
149
150 unsigned int getNa(const char p) const {
151 switch (p) {
152 case 'b': return n_acc[0];
153 case 'd': return n_acc[1];
154 case 'r': return n_acc[2];
155 case 'o': return n_acc[3];
156 case 'c': return n_acc[4];
157 default: return 0;
158 }
159 }
160
161 void incN(const char p, unsigned int i = 1) {
162 std::lock_guard<std::mutex> lock (mutex);
163 switch (p) {
164 case 'b': n_gen[0] += i; break;
165 case 'd': n_gen[1] += i; break;
166 case 'r': n_gen[2] += i; break;
167 case 'o': n_gen[3] += i; break;
168 case 'c': n_gen[4] += i; break;
169 default: return;
170 }
171 }
172
173 void incNa(const char p, unsigned int i = 1) {
174 std::lock_guard<std::mutex> lock (mutex);
175 switch (p) {
176 case 'b': n_acc[0] += i; break;
177 case 'd': n_acc[1] += i; break;
178 case 'r': n_acc[2] += i; break;
179 case 'o': n_acc[3] += i; break;
180 case 'c': n_acc[4] += i; break;
181 }
182 }
183
184 double getAcceptanceRate(const char p) const {
185 switch (p) {
186 case 'b': return double(n_acc[0]) / double(n_gen[0]);
187 case 'd': return double(n_acc[1]) / double(n_gen[1]);
188 case 'r': return double(n_acc[2]) / double(n_gen[2]);
189 case 'o': return double(n_acc[3]) / double(n_gen[3]);
190 case 'c': return double(n_acc[4]) / double(n_gen[4]);
191 default: return 0.0;
192 }
193 }
194
195
196 friend std::ostream& operator<< (std::ostream& o, Stats const& stats);
197
198
199 protected:
200 std::mutex mutex;
201 double Text, Tint;
203 double alpha;
204
205 unsigned long n_gen[5];
206 unsigned long n_acc[5];
207 unsigned long n_iter;
208 const uint64_t n_max;
209
211 std::ofstream out;
212
213 };
214
215
216
217
218
219
220 }
221 }
222 }
223}
224
225#endif // __gt_gt_h__
unsigned long n_acc[5]
Definition: gt.h:206
const uint64_t n_max
Definition: gt.h:208
unsigned long n_iter
Definition: gt.h:207
friend std::ostream & operator<<(std::ostream &o, Stats const &stats)
unsigned long n_gen[5]
Definition: gt.h:205
implements a progress meter to provide feedback to the user
Definition: progressbar.h:58
constexpr double pi
Definition: math.h:40
#define FRAC_PHASEOUT
Definition: gt.h:22
#define ITER_BIGSTEP
Definition: gt.h:20
#define FRAC_BURNIN
Definition: gt.h:21
const double M_sqrt4PI
Definition: gt.h:39
const double M_4PI
Definition: gt.h:38
Definition: base.h:24
vector< Eigen::VectorXf > resp_ISO
Definition: gt.h:61
MEMALIGN(Properties) float p_birth