Developer documentation
Version 3.0.3-105-gd3941f44
tensor_prob.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 __dwi_tractography_algorithms_tensor_prob_h__
18#define __dwi_tractography_algorithms_tensor_prob_h__
19
20#include "dwi/bootstrap.h"
23
24
25namespace MR
26{
27 namespace DWI
28 {
29 namespace Tractography
30 {
31 namespace Algorithms
32 {
33
34 using namespace MR::DWI::Tractography::Tracking;
35
37 public:
38 class Shared : public Tensor_Det::Shared { MEMALIGN(Shared)
39 public:
40 Shared (const std::string& diff_path, DWI::Tractography::Properties& property_set) :
41 Tensor_Det::Shared (diff_path, property_set) {
42
43 if (is_act() && act().backtrack())
44 throw Exception ("Sorry, backtracking not currently enabled for TensorProb algorithm");
45
46 properties["method"] = "TensorProb";
47
48 Hat = bmat * binv;
49 }
50
51 Eigen::MatrixXf Hat;
52 };
53
54
55
56
57
58
59 Tensor_Prob (const Shared& shared) :
60 Tensor_Det (shared),
61 S (shared),
63
64 Tensor_Prob (const Tensor_Prob& F) :
65 Tensor_Det (F.S),
66 S (F.S),
68
69
70
71 bool init() override {
72 source.clear();
73 if (!source.get (pos, values))
74 return false;
75 return Tensor_Det::do_init();
76 }
77
78
79
80 term_t next () override {
81 if (!source.get (pos, values))
82 return EXIT_IMAGE;
83 return Tensor_Det::do_next();
84 }
85
86
87 void truncate_track (GeneratedTrack& tck, const size_t length_to_revert_from, const size_t revert_step) override { assert (0); }
88
89
90 protected:
91
92 class WildBootstrap { MEMALIGN(WildBootstrap)
93 public:
94 WildBootstrap (const Eigen::MatrixXf& hat_matrix) :
95 H (hat_matrix),
96 uniform_int (0, 1),
97 residuals (H.rows()),
98 log_signal (H.rows()) { }
99
100 void operator() (float* data) {
101 for (ssize_t i = 0; i < residuals.size(); ++i)
102 log_signal[i] = data[i] > float (0.0) ? -std::log (data[i]) : float (0.0);
103
104 residuals = H * log_signal;
105
106 for (ssize_t i = 0; i < residuals.size(); ++i) {
107 residuals[i] = residuals[i] ? (data[i] - std::exp (-residuals[i])) : float(0.0);
108 data[i] += uniform_int (rng) ? residuals[i] : -residuals[i];
109 }
110 }
111
112 private:
113 const Eigen::MatrixXf& H;
114 std::uniform_int_distribution<> uniform_int;
115 Eigen::VectorXf residuals, log_signal;
116 };
117
118
119 class Interp : public Interpolator<Bootstrap<Image<float>,WildBootstrap>>::type { MEMALIGN(Interp)
120 public:
121 Interp (const Bootstrap<Image<float>,WildBootstrap>& bootstrap_vox) :
123 {
124 for (size_t i = 0; i < 8; ++i)
125 raw_signals.push_back (Eigen::VectorXf (size(3)));
126 }
127
128 vector<Eigen::VectorXf> raw_signals;
129
130 bool get (const Eigen::Vector3f& pos, Eigen::VectorXf& data) {
131 if (!scanner (pos)) {
132 data.fill (NaN);
133 return false;
134 }
135
136 data.setZero();
137
138 size_t i = 0;
139 for (ssize_t z = 0; z < 2; ++z) {
140 index(2) = clamp (P[2]+z, size(2));
141 for (ssize_t y = 0; y < 2; ++y) {
142 index(1) = clamp (P[1]+y, size(1));
143 for (ssize_t x = 0; x < 2; ++x) {
144 index(0) = clamp (P[0]+x, size(0));
145 if (factors[i]) {
146 get_values (raw_signals[i]);
147 data += factors[i] * raw_signals[i];
148 }
149 ++i;
150 }
151 }
152 }
153
154 return !std::isnan (data[0]);
155 }
156 };
157
158 const Shared& S;
160
161
162 };
163
164 }
165 }
166 }
167}
168
169#endif
170
171
172
thread_local Math::RNG rng
thread-local, but globally accessible RNG to vastly simplify multi-threading
Definition: base.h:24
constexpr default_type NaN
Definition: types.h:230
size_t index
#define MEMALIGN(...)
Definition: types.h:185