Developer documentation
Version 3.0.3-105-gd3941f44
mhsampler.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_mhsampler_h__
18#define __gt_mhsampler_h__
19
20#include "image.h"
21#include "transform.h"
22
23#include "math/rng.h"
24
30
31
32namespace MR {
33 namespace DWI {
34 namespace Tractography {
35 namespace GT {
36
41 { MEMALIGN(MHSampler)
42 public:
43 MHSampler(const Image<float>& dwi, Properties &p, Stats &s, ParticleGrid &pgrid,
45 : props(p), stats(s), pGrid(pgrid), E(e), T(dwi),
46 dims{size_t(dwi.size(0)), size_t(dwi.size(1)), size_t(dwi.size(2))},
47 mask(m), lock(make_shared<SpatialLock<float>>(5*Particle::L)),
48 sigpos(Particle::L / 8.), sigdir(0.2)
49 {
50 DEBUG("Initialise Metropolis Hastings sampler.");
51 }
52
53 MHSampler(const MHSampler& other)
54 : props(other.props), stats(other.stats), pGrid(other.pGrid), E(other.E->clone()),
55 T(other.T), dims(other.dims), mask(other.mask), lock(other.lock), rng_uniform(), rng_normal(), sigpos(other.sigpos), sigdir(other.sigdir)
56 {
57 DEBUG("Copy Metropolis Hastings sampler.");
58 }
59
60 ~MHSampler() { delete E; }
61
62 void execute();
63
64 void next();
65
66 void birth();
67 void death();
68 void randshift();
69 void optshift();
70 void connect();
71
72
73 protected:
74
78 EnergyComputer* E; // Polymorphic copy requires call to EnergyComputer::clone(), hence references or smart pointers won't do.
79
83
84 std::shared_ptr< SpatialLock<float> > lock;
87 float sigpos, sigdir;
88
89
91
92 bool inMask(const Point_t p);
93
95
96 void moveRandom(const Particle* par, Point_t& pos, Point_t& dir);
97
98 bool moveOptimal(const Particle* par, Point_t& pos, Point_t& dir) const;
99
100 inline double calcShiftProb(const Particle* par, const Point_t& pos, const Point_t& dir) const
101 {
102 Point_t Dpos = par->getPosition() - pos;
103 Point_t Ddir = par->getDirection() - dir;
104 return gaussian_pdf(Dpos, sigpos) * gaussian_pdf(Ddir, sigdir);
105 }
106
107 inline double gaussian_pdf(const Point_t& x, double sigma) const {
108 return std::exp( -x.squaredNorm() / (2*sigma) ) / std::sqrt( 2*Math::pi * sigma*sigma);
109 }
110
111
112 };
113
114
115 }
116 }
117 }
118}
119
120
121#endif // __gt_mhsampler_h__
bool moveOptimal(const Particle *par, Point_t &pos, Point_t &dir) const
Math::RNG::Uniform< float > rng_uniform
Definition: mhsampler.h:85
void moveRandom(const Particle *par, Point_t &pos, Point_t &dir)
double calcShiftProb(const Particle *par, const Point_t &pos, const Point_t &dir) const
Definition: mhsampler.h:100
Math::RNG::Normal< float > rng_normal
Definition: mhsampler.h:86
std::shared_ptr< SpatialLock< float > > lock
Definition: mhsampler.h:84
double gaussian_pdf(const Point_t &x, double sigma) const
Definition: mhsampler.h:107
SpatialLock manages a mutex lock on n positions in 3D space.
Definition: spatiallock.h:36
#define DEBUG(msg)
Definition: exception.h:75
constexpr double e
Definition: math.h:39
constexpr double pi
Definition: math.h:40
Eigen::Vector3f Point_t
Definition: particle.h:29
Definition: base.h:24
std::shared_ptr< X > make_shared(Args &&... args)
Definition: types.h:283
InputImageType dwi