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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
* Copyright (c) 2008-2018 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/
*
* MRtrix3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* For more details, see http://www.mrtrix.org/
*/
#include "command.h"
#include "progressbar.h"
#include "image.h"
#include "algo/threaded_copy.h"
#include "dwi/gradient.h"
#include "dwi/tensor.h"
#include <Eigen/Eigenvalues>
using namespace MR;
using namespace App;
using value_type = float;
const char* modulate_choices[] = { "none", "fa", "eigval", NULL };
void usage ()
{
ARGUMENTS
+ Argument ("tensor", "the input tensor image.").type_image_in ();
OPTIONS
+ Option ("adc",
"compute the mean apparent diffusion coefficient (ADC) of the diffusion tensor. "
"(sometimes also referred to as the mean diffusivity (MD))")
+ Argument ("image").type_image_out()
+ Option ("fa",
"compute the fractional anisotropy (FA) of the diffusion tensor.")
+ Argument ("image").type_image_out()
+ Option ("ad",
"compute the axial diffusivity (AD) of the diffusion tensor. "
"(equivalent to the principal eigenvalue)")
+ Argument ("image").type_image_out()
+ Option ("rd",
"compute the radial diffusivity (RD) of the diffusion tensor. "
"(equivalent to the mean of the two non-principal eigenvalues)")
+ Argument ("image").type_image_out()
+ Option ("cl",
"compute the linearity metric of the diffusion tensor. "
"(one of the three Westin shape metrics)")
+ Argument ("image").type_image_out()
+ Option ("cp",
"compute the planarity metric of the diffusion tensor. "
"(one of the three Westin shape metrics)")
+ Argument ("image").type_image_out()
+ Option ("cs",
"compute the sphericity metric of the diffusion tensor. "
"(one of the three Westin shape metrics)")
+ Argument ("image").type_image_out()
+ Option ("value",
"compute the selected eigenvalue(s) of the diffusion tensor.")
+ Argument ("image").type_image_out()
+ Option ("vector",
"compute the selected eigenvector(s) of the diffusion tensor.")
+ Argument ("image").type_image_out()
+ Option ("num",
"specify the desired eigenvalue/eigenvector(s). Note that several eigenvalues "
"can be specified as a number sequence. For example, '1,3' specifies the "
"principal (1) and minor (3) eigenvalues/eigenvectors (default = 1).")
+ Argument ("sequence").type_sequence_int()
+ Option ("modulate",
"specify how to modulate the magnitude of the eigenvectors. Valid choices "
"are: none, FA, eigval (default = FA).")
+ Argument ("choice").type_choice (modulate_choices)
+ Option ("mask",
"only perform computation within the specified binary brain mask image.")
+ Argument ("image").type_image_in();
AUTHOR = "Thijs Dhollander (thijs.dhollander@gmail.com) & Ben Jeurissen (ben.jeurissen@uantwerpen.be) & J-Donald Tournier (jdtournier@gmail.com)";
SYNOPSIS = "Generate maps of tensor-derived parameters";
REFERENCES
+ "Basser, P. J.; Mattiello, J. & Lebihan, D. "
"MR diffusion tensor spectroscopy and imaging. "
"Biophysical Journal, 1994, 66, 259-267"
+ "Westin, C. F.; Peled, S.; Gudbjartsson, H.; Kikinis, R. & Jolesz, F. A. "
"Geometrical diffusion measures for MRI from tensor basis analysis. "
"Proc Intl Soc Mag Reson Med, 1997, 5, 1742";
}
class Processor { MEMALIGN(Processor)
public:
Processor (Image<bool>& mask_img,
Image<value_type>& adc_img,
Image<value_type>& fa_img,
Image<value_type>& ad_img,
Image<value_type>& rd_img,
Image<value_type>& cl_img,
Image<value_type>& cp_img,
Image<value_type>& cs_img,
Image<value_type>& value_img,
Image<value_type>& vector_img,
vector<int>& vals,
int modulate) :
mask_img (mask_img),
adc_img (adc_img),
fa_img (fa_img),
ad_img (ad_img),
rd_img (rd_img),
cl_img (cl_img),
cp_img (cp_img),
cs_img (cs_img),
value_img (value_img),
vector_img (vector_img),
vals (vals),
modulate (modulate) {
for (auto& n : this->vals)
--n;
}
void operator() (Image<value_type>& dt_img)
{
/* check mask */
if (mask_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (mask_img);
if (!mask_img.value())
return;
}
/* input dt */
Eigen::Matrix<double, 6, 1> dt;
for (auto l = Loop (3) (dt_img); l; ++l)
dt[dt_img.index(3)] = dt_img.value();
/* output adc */
if (adc_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (adc_img);
adc_img.value() = DWI::tensor2ADC(dt);
}
double fa = 0.0;
if (fa_img.valid() || (vector_img.valid() && (modulate == 1)))
fa = DWI::tensor2FA(dt);
/* output fa */
if (fa_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (fa_img);
fa_img.value() = fa;
}
bool need_eigenvalues = value_img.valid() || vector_img.valid() || ad_img.valid() || rd_img.valid() || cl_img.valid() || cp_img.valid() || cs_img.valid();
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es;
if (need_eigenvalues || vector_img.valid()) {
Eigen::Matrix3d M;
M (0,0) = dt[0];
M (1,1) = dt[1];
M (2,2) = dt[2];
M (0,1) = M (1,0) = dt[3];
M (0,2) = M (2,0) = dt[4];
M (1,2) = M (2,1) = dt[5];
es = Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d>(M, vector_img.valid() ? Eigen::ComputeEigenvectors : Eigen::EigenvaluesOnly);
}
Eigen::Vector3d eigval;
ssize_t ith_eig[3] = { 2, 1, 0 };
if (need_eigenvalues) {
eigval = es.eigenvalues();
ith_eig[0] = 0; ith_eig[1] = 1; ith_eig[2] = 2;
std::sort (std::begin (ith_eig), std::end (ith_eig),
[&eigval](size_t a, size_t b) { return abs(eigval[a]) > abs(eigval[b]); });
}
/* output value */
if (value_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (value_img);
if (vals.size() > 1) {
auto l = Loop(3)(value_img);
for (size_t i = 0; i < vals.size(); i++) {
value_img.value() = eigval(ith_eig[vals[i]]); l++;
}
} else {
value_img.value() = eigval(ith_eig[vals[0]]);
}
}
/* output ad */
if (ad_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (ad_img);
ad_img.value() = eigval(2);
}
/* output rd */
if (rd_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (rd_img);
rd_img.value() = (eigval(1) + eigval(0)) / 2;
}
/* output shape measures */
if (cl_img.valid() || cp_img.valid() || cs_img.valid()) {
double eigsum = eigval.sum();
if (eigsum != 0.0) {
if (cl_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (cl_img);
cl_img.value() = (eigval(2) - eigval(1)) / eigsum;
}
if (cp_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (cp_img);
cp_img.value() = 2.0 * (eigval(1) - eigval(0)) / eigsum;
}
if (cs_img.valid()) {
assign_pos_of (dt_img, 0, 3).to (cs_img);
cs_img.value() = 3.0 * eigval(0) / eigsum;
}
}
}
/* output vector */
if (vector_img.valid()) {
Eigen::Matrix3d eigvec = es.eigenvectors();
assign_pos_of (dt_img, 0, 3).to (vector_img);
auto l = Loop(3)(vector_img);
for (size_t i = 0; i < vals.size(); i++) {
double fact = 1.0;
if (modulate == 1)
fact = fa;
else if (modulate == 2)
fact = eigval(ith_eig[vals[i]]);
vector_img.value() = eigvec(0,ith_eig[vals[i]])*fact; l++;
vector_img.value() = eigvec(1,ith_eig[vals[i]])*fact; l++;
vector_img.value() = eigvec(2,ith_eig[vals[i]])*fact; l++;
}
}
}
private:
Image<bool> mask_img;
Image<value_type> adc_img;
Image<value_type> fa_img;
Image<value_type> ad_img;
Image<value_type> rd_img;
Image<value_type> cl_img;
Image<value_type> cp_img;
Image<value_type> cs_img;
Image<value_type> value_img;
Image<value_type> vector_img;
vector<int> vals;
int modulate;
};
void run ()
{
auto dt_img = Image<value_type>::open (argument[0]);
Header header (dt_img);
auto mask_img = Image<bool>();
auto opt = get_options ("mask");
if (opt.size()) {
mask_img = Image<bool>::open (opt[0][0]);
check_dimensions (dt_img, mask_img, 0, 3);
}
size_t metric_count = 0;
auto adc_img = Image<value_type>();
opt = get_options ("adc");
if (opt.size()) {
header.ndim() = 3;
adc_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto fa_img = Image<value_type>();
opt = get_options ("fa");
if (opt.size()) {
header.ndim() = 3;
fa_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto ad_img = Image<value_type>();
opt = get_options ("ad");
if (opt.size()) {
header.ndim() = 3;
ad_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto rd_img = Image<value_type>();
opt = get_options ("rd");
if (opt.size()) {
header.ndim() = 3;
rd_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto cl_img = Image<value_type>();
opt = get_options ("cl");
if (opt.size()) {
header.ndim() = 3;
cl_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto cp_img = Image<value_type>();
opt = get_options ("cp");
if (opt.size()) {
header.ndim() = 3;
cp_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto cs_img = Image<value_type>();
opt = get_options ("cs");
if (opt.size()) {
header.ndim() = 3;
cs_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
vector<int> vals = {1};
opt = get_options ("num");
if (opt.size()) {
vals = opt[0][0];
if (vals.empty())
throw Exception ("invalid eigenvalue/eigenvector number specifier");
for (size_t i = 0; i < vals.size(); ++i)
if (vals[i] < 1 || vals[i] > 3)
throw Exception ("eigenvalue/eigenvector number is out of bounds");
}
float modulate = get_option_value ("modulate", 1);
auto value_img = Image<value_type>();
opt = get_options ("value");
if (opt.size()) {
header.ndim() = 3;
if (vals.size()>1) {
header.ndim() = 4;
header.size (3) = vals.size();
}
value_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
auto vector_img = Image<value_type>();
opt = get_options ("vector");
if (opt.size()) {
header.ndim() = 4;
header.size (3) = vals.size()*3;
vector_img = Image<value_type>::create (opt[0][0], header);
metric_count++;
}
if (!metric_count)
throw Exception ("No output specified; must request at least one metric of interest using the available command-line options");
ThreadedLoop (std::string("computing metric") + (metric_count > 1 ? "s" : ""), dt_img, 0, 3)
.run (Processor (mask_img, adc_img, fa_img, ad_img, rd_img, cl_img, cp_img, cs_img, value_img, vector_img, vals, modulate), dt_img);
}
|