Developer documentation
Version 3.0.3-105-gd3941f44
shells.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_shells_h__
18#define __dwi_shells_h__
19
20
21#include <fstream>
22#include <limits>
23
24#include "app.h"
25#include "types.h"
26#include "file/config.h"
27#include "misc/bitset.h"
28
29
30// Don't expect these values to change depending on the particular command that is initialising the Shells class;
31// method should be robust to all incoming data
32
33// Maximum absolute difference in b-value for two volumes to be considered to be in the same shell
34#define DWI_SHELLS_EPSILON 80
35// Minimum number of volumes within DWI_SHELL_EPSILON necessary to continue expansion of the cluster selection
36#define DWI_SHELLS_MIN_LINKAGE 3
37// Default number of volumes necessary for a shell to be retained
38// (note: only applies if function reject_small_shells() is called explicitly)
39#define DWI_SHELLS_MIN_DIRECTIONS 6
40// Default b-value threshold for a shell to be classified as "b=0"
41#define DWI_SHELLS_BZERO_THREHSOLD 10.0
42
43
44
45//CONF option: BZeroThreshold
46//CONF default: 10.0
47//CONF Specifies the b-value threshold for determining those image
48//CONF volumes that correspond to b=0.
49
50//CONF option: BValueEpsilon
51//CONF default: 80.0
52//CONF Specifies the difference between b-values necessary for image
53//CONF volumes to be classified as belonging to different shells.
54
55
56
57namespace MR
58{
59
60 namespace App { class OptionGroup; }
61
62 namespace DWI
63 {
64
65 extern const App::OptionGroup ShellsOption;
66
69 return value;
70 }
71
72
73
74 class Shell
76
77 public:
78
79 Shell() : mean (0.0), stdev (0.0), min (0.0), max (0.0) { }
80 Shell (const Eigen::MatrixXd& grad, const vector<size_t>& indices);
81
82 const vector<size_t>& get_volumes() const { return volumes; }
83 size_t count() const { return volumes.size(); }
84
85 default_type get_mean() const { return mean; }
86 default_type get_stdev() const { return stdev; }
87 default_type get_min() const { return min; }
88 default_type get_max() const { return max; }
89
90 bool is_bzero() const { return (mean < bzero_threshold()); }
91
92
93 bool operator< (const Shell& rhs) const { return (mean < rhs.mean); }
94
95 friend std::ostream& operator<< (std::ostream& stream, const Shell& S)
96 {
97 stream << "Shell: " << S.volumes.size() << " volumes, b-value "
98 << S.mean << " +- " << S.stdev << " (range [" << S.min << " - " << S.max << "])";
99 return stream;
100 }
101
102
103 protected:
106
107 };
108
109
110
111
112
113 class Shells
114 { NOMEMALIGN
115 public:
116 Shells (const Eigen::MatrixXd& grad);
117
118 const Shell& operator[] (const size_t i) const { return shells[i]; }
119 const Shell& smallest() const { return shells.front(); }
120 const Shell& largest() const { return shells.back(); }
121 size_t count() const { return shells.size(); }
122 size_t volumecount() const {
123 size_t count = 0;
124 for (const auto& it : shells)
125 count += it.count();
126 return count;
127 }
128
130 vector<size_t> c (count());
131 for (size_t n = 0; n < count(); ++n)
132 c[n] = shells[n].count();
133 return c;
134 }
135
137 vector<size_t> b (count());
138 for (size_t n = 0; n < count(); ++n)
139 b[n] = shells[n].get_mean();
140 return b;
141 }
142
143 Shells& select_shells (const bool force_singleshell, const bool force_with_bzero, const bool force_without_bzero);
144
146
147 bool is_single_shell() const {
148 // only if exactly 1 non-bzero shell
149 return ((count() == 1 && !has_bzero()) || (count() == 2 && has_bzero()));
150 }
151
152 bool has_bzero() const {
153 return smallest().is_bzero();
154 }
155
156 friend std::ostream& operator<< (std::ostream& stream, const Shells& S)
157 {
158 stream << "Total of " << S.count() << " DWI shells:" << std::endl;
159 for (const auto& it : S.shells)
160 stream << it << std::endl;
161 return stream;
162 }
163
164
165 protected:
167
168
169 private:
170
171 using BValueList = decltype(std::declval<const Eigen::MatrixXd>().col(0));
172
173 // Functions for current b-value clustering implementation
174 size_t clusterBvalues (const BValueList&, vector<size_t>&) const;
175 void regionQuery (const BValueList&, const default_type, vector<size_t>&) const;
176
177
178 };
179
180
181
182
183 }
184}
185
186#endif
187
bool is_bzero() const
Definition: shells.h:90
default_type mean
Definition: shells.h:105
friend std::ostream & operator<<(std::ostream &stream, const Shell &S)
Definition: shells.h:95
default_type get_mean() const
Definition: shells.h:85
Shell(const Eigen::MatrixXd &grad, const vector< size_t > &indices)
default_type get_max() const
Definition: shells.h:88
default_type get_stdev() const
Definition: shells.h:86
default_type stdev
Definition: shells.h:105
size_t count() const
Definition: shells.h:83
vector< size_t > volumes
Definition: shells.h:104
default_type get_min() const
Definition: shells.h:87
default_type min
Definition: shells.h:105
default_type max
Definition: shells.h:105
const vector< size_t > & get_volumes() const
Definition: shells.h:82
bool operator<(const Shell &rhs) const
Definition: shells.h:93
bool is_single_shell() const
Definition: shells.h:147
const Shell & largest() const
Definition: shells.h:120
vector< size_t > get_counts() const
Definition: shells.h:129
friend std::ostream & operator<<(std::ostream &stream, const Shells &S)
Definition: shells.h:156
size_t volumecount() const
Definition: shells.h:122
bool has_bzero() const
Definition: shells.h:152
Shells & reject_small_shells(const size_t min_volumes=6)
Shells & select_shells(const bool force_singleshell, const bool force_with_bzero, const bool force_without_bzero)
const Shell & smallest() const
Definition: shells.h:119
size_t count() const
Definition: shells.h:121
Shells(const Eigen::MatrixXd &grad)
vector< Shell > shells
Definition: shells.h:166
const Shell & operator[](const size_t i) const
Definition: shells.h:118
vector< size_t > get_bvalues() const
Definition: shells.h:136
static float get_float(const std::string &key, float default_value)
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
Definition: SH.h:233
#define NOMEMALIGN
Definition: memory.h:22
FORCE_INLINE default_type bzero_threshold()
Definition: shells.h:67
const App::OptionGroup ShellsOption
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
Eigen::MatrixXd S
#define DWI_SHELLS_MIN_DIRECTIONS
Definition: shells.h:39
#define DWI_SHELLS_BZERO_THREHSOLD
Definition: shells.h:41
#define FORCE_INLINE
Definition: types.h:156