forked from MRtrix3/mrtrix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwi2fod.cpp
184 lines (133 loc) · 4.84 KB
/
dwi2fod.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "command.h"
#include "memory.h"
#include "progressbar.h"
#include "image/threaded_loop.h"
#include "image/buffer.h"
#include "image/buffer_preload.h"
#include "image/voxel.h"
#include "dwi/gradient.h"
#include "dwi/shells.h"
#include "dwi/sdeconv/constrained.h"
using namespace MR;
using namespace App;
void usage ()
{
DESCRIPTION
+ "perform non-negativity constrained spherical deconvolution."
+ "Note that this program makes use of implied symmetries in the diffusion "
"profile. First, the fact the signal attenuation profile is real implies "
"that it has conjugate symmetry, i.e. Y(l,-m) = Y(l,m)* (where * denotes "
"the complex conjugate). Second, the diffusion profile should be "
"antipodally symmetric (i.e. S(x) = S(-x)), implying that all odd l "
"components should be zero. Therefore, this program only computes the even "
"elements."
+ "Note that the spherical harmonics equations used here differ slightly "
"from those conventionally used, in that the (-1)^m factor has been "
"omitted. This should be taken into account in all subsequent calculations."
+ Math::SH::encoding_description;
REFERENCES
+ "Tournier, J.-D.; Calamante, F. & Connelly, A. "
"Robust determination of the fibre orientation distribution in diffusion MRI: "
"Non-negativity constrained super-resolved spherical deconvolution. "
"NeuroImage, 2007, 35, 1459-1472";
ARGUMENTS
+ Argument ("dwi",
"the input diffusion-weighted image.").type_image_in()
+ Argument ("response",
"a text file containing the diffusion-weighted signal response function "
"coefficients for a single fibre population, ").type_file_in()
+ Argument ("SH",
"the output spherical harmonics coefficients image.").type_image_out();
OPTIONS
+ DWI::GradImportOptions()
+ DWI::ShellOption
+ DWI::CSD_options
+ Image::Stride::StrideOption;
}
typedef float value_type;
typedef double cost_value_type;
typedef Image::BufferPreload<value_type> InputBufferType;
typedef Image::Buffer<value_type> OutputBufferType;
typedef Image::Buffer<bool> MaskBufferType;
class Processor
{
public:
Processor (InputBufferType::voxel_type& DWI_vox,
OutputBufferType::voxel_type& FOD_vox,
copy_ptr<MaskBufferType::voxel_type>& mask_vox,
const DWI::CSDeconv<value_type>::Shared& shared) :
dwi (DWI_vox),
fod (FOD_vox),
mask (mask_vox),
sdeconv (shared),
data (shared.dwis.size()) {
if (mask)
Image::check_dimensions (*mask, dwi, 0, 3);
}
void operator () (const Image::Iterator& pos) {
if (!load_data (pos))
return;
sdeconv.set (data);
size_t n;
for (n = 0; n < sdeconv.shared.niter; n++)
if (sdeconv.iterate())
break;
if (n >= sdeconv.shared.niter)
INFO ("voxel [ " + str (pos[0]) + " " + str (pos[1]) + " " + str (pos[2]) +
" ] did not reach full convergence");
write_back (pos);
}
private:
InputBufferType::voxel_type dwi;
OutputBufferType::voxel_type fod;
copy_ptr<MaskBufferType::voxel_type> mask;
DWI::CSDeconv<value_type> sdeconv;
Math::Vector<value_type> data;
bool load_data (const Image::Iterator& pos) {
if (mask) {
Image::voxel_assign (*mask, pos);
if (!mask->value())
return false;
}
Image::voxel_assign (dwi, pos);
for (size_t n = 0; n < sdeconv.shared.dwis.size(); n++) {
dwi[3] = sdeconv.shared.dwis[n];
data[n] = dwi.value();
if (!std::isfinite (data[n]))
return false;
if (data[n] < 0.0)
data[n] = 0.0;
}
return true;
}
void write_back (const Image::Iterator& pos) {
Image::voxel_assign (fod, pos);
for (fod[3] = 0; fod[3] < fod.dim (3); ++fod[3])
fod.value() = sdeconv.FOD() [fod[3]];
}
};
void run ()
{
InputBufferType dwi_buffer (argument[0], Image::Stride::contiguous_along_axis(3));
copy_ptr<MaskBufferType> mask_data;
copy_ptr<MaskBufferType::voxel_type> mask_vox;
Options opt = get_options ("mask");
if (opt.size()) {
mask_data.reset (new MaskBufferType (opt[0][0]));
mask_vox.reset (new MaskBufferType::voxel_type (*mask_data));
}
DWI::CSDeconv<value_type>::Shared shared (dwi_buffer);
shared.parse_cmdline_options();
shared.set_response (argument[1]);
shared.init();
Image::Header header (dwi_buffer);
header.dim(3) = shared.nSH();
header.datatype() = DataType::Float32;
Image::Stride::set_from_command_line (header);
OutputBufferType FOD_buffer (argument[2], header);
auto dwi_vox = dwi_buffer.voxel();
auto FOD_vox = FOD_buffer.voxel();
Processor processor (dwi_vox, FOD_vox, mask_vox, shared);
Image::ThreadedLoop loop ("performing constrained spherical deconvolution...", dwi_vox, 0, 3);
loop.run (processor);
}