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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
/* Copyright (c) 2008-2025 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/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include "apply.h"
#include "command.h"
#include "progressbar.h"
#include "image.h"
#include "algo/loop.h"
#include "algo/threaded_loop.h"
#include "formats/mrtrix_utils.h"
#include "dwi/tractography/file.h"
#include "dwi/tractography/scalar_file.h"
#include "math/SH.h"
#include "fixel/helpers.h"
#include "fixel/keys.h"
#include "fixel/loop.h"
#include "fixel/types.h"
using namespace MR;
using namespace App;
using Fixel::index_type;
const char* operations[] = {
"mean",
"sum",
"product",
"min",
"max",
"absmax",
"magmax",
"count",
"complexity",
"sf",
"dec_unit",
"dec_scaled",
"none",
nullptr
};
void usage ()
{
AUTHOR = "Robert E. Smith (robert.smith@florey.edu.au) & David Raffelt (david.raffelt@florey.edu.au)";
SYNOPSIS = "Convert a fixel-based sparse-data image into some form of scalar image";
DESCRIPTION
+ "Fixel data can be reduced to voxel data in a number of ways:"
+ "- Some statistic computed across all fixel values within a voxel: mean, sum, product, min, max, absmax, magmax"
+ "- The number of fixels in each voxel: count"
+ "- Some measure of crossing-fibre organisation: complexity, sf ('single-fibre')"
+ "- A 4D directionally-encoded colour image: dec_unit, dec_scaled"
+ "- A 4D image containing all fixel data values in each voxel unmodified: none"
+ "The -weighted option deals with the case where there is some per-fixel metric of interest "
"that you wish to collapse into a single scalar measure per voxel, but each fixel possesses "
"a different volume, and you wish for those fixels with greater volume to have a greater "
"influence on the calculation than fixels with lesser volume. For instance, when estimating "
"a voxel-based measure of mean axon diameter from per-fixel mean axon diameters, a fixel's "
"mean axon diameter should be weigthed by its relative volume within the voxel in the "
"calculation of that voxel mean.";
REFERENCES
+ "* Reference for 'complexity' operation:\n"
"Riffert, T. W.; Schreiber, J.; Anwander, A. & Knosche, T. R. "
"Beyond Fractional Anisotropy: Extraction of bundle-specific structural metrics from crossing fibre models. "
"NeuroImage, 2014, 100, 176-191";
ARGUMENTS
+ Argument ("fixel_in", "the input fixel data file").type_image_in ()
+ Argument ("operation", "the operation to apply, one of: " + join(operations, ", ") + ".").type_choice (operations)
+ Argument ("image_out", "the output scalar image.").type_image_out ();
OPTIONS
+ Option ("number", "use only the largest N fixels in calculation of the voxel-wise statistic; "
"in the case of operation \"none\", output only the largest N fixels in each voxel.")
+ Argument ("N").type_integer(1)
+ Option ("fill", "for \"none\" operation, specify the value to fill when number of fixels is fewer than the maximum (default: 0.0)")
+ Argument ("value").type_float()
+ Option ("weighted", "weight the contribution of each fixel to the per-voxel result according to its volume.")
+ Argument ("fixel_in").type_image_in ();
}
using FixelIndexType = Image<index_type>;
using FixelDataType = Image<float>;
struct set_offset { NOMEMALIGN
FORCE_INLINE set_offset (index_type offset) : offset (offset) { }
template <class DataType>
FORCE_INLINE void operator() (DataType& data) { data.index(0) = offset; }
index_type offset;
};
struct inc_fixel { NOMEMALIGN
template <class DataType>
FORCE_INLINE void operator() (DataType& data) { ++data.index(0); }
};
struct LoopFixelsInVoxelWithMax { NOMEMALIGN
const index_type num_fixels;
const index_type max_fixels;
const index_type offset;
template <class... DataType>
struct Run { NOMEMALIGN
const index_type num_fixels;
const index_type max_fixels;
const index_type offset;
index_type fixel_index;
const std::tuple<DataType&...> data;
FORCE_INLINE Run (const index_type num_fixels, const index_type max_fixels, const index_type offset, const std::tuple<DataType&...>& data) :
num_fixels (num_fixels), max_fixels (max_fixels), offset (offset), fixel_index (0), data (data) {
apply (set_offset (offset), data);
}
FORCE_INLINE operator bool() const { return max_fixels ? (fixel_index < max_fixels) : (fixel_index < num_fixels); }
FORCE_INLINE void operator++() { if (!padding()) apply (inc_fixel (), data); ++fixel_index; }
FORCE_INLINE void operator++(int) { operator++(); }
FORCE_INLINE bool padding() const { return (max_fixels && fixel_index >= num_fixels); }
FORCE_INLINE index_type count() const { return max_fixels ? max_fixels : num_fixels; }
};
template <class... DataType>
FORCE_INLINE Run<DataType...> operator() (DataType&... data) const { return { num_fixels, max_fixels, offset, std::tie (data...) }; }
};
class Base
{ NOMEMALIGN
public:
Base (FixelDataType& data, const index_type max_fixels, const bool pad = false, const float pad_value = 0.0) :
data (data),
max_fixels (max_fixels),
pad (pad),
pad_value (pad_value) { }
protected:
FORCE_INLINE LoopFixelsInVoxelWithMax
Loop (FixelIndexType& index) {
index.index(3) = 0;
const index_type num_fixels = index.value();
index.index(3) = 1;
const index_type offset = index.value();
return { num_fixels, max_fixels, offset };
}
FixelDataType data;
const index_type max_fixels;
const bool pad;
const float pad_value;
};
class Mean : protected Base
{ MEMALIGN (Mean)
public:
Mean (FixelDataType& data, const index_type max_fixels, FixelDataType& vol) :
Base (data, max_fixels),
vol (vol) {}
void operator() (FixelIndexType& index, Image<float>& out)
{
default_type sum = 0.0;
default_type sum_volumes = 0.0;
if (vol.valid()) {
for (auto f = Base::Loop (index) (data, vol); f; ++f) {
if (!f.padding()) {
sum += data.value() * vol.value();
sum_volumes += vol.value();
}
}
out.value() = sum_volumes ? (sum / sum_volumes) : 0.0;
} else {
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding()) {
sum += data.value();
sum_volumes += 1.0;
}
}
out.value() = sum_volumes ? (sum / sum_volumes) : 0.0;
}
}
protected:
FixelDataType vol;
};
class Sum : protected Base
{ MEMALIGN (Sum)
public:
Sum (FixelDataType& data, const index_type max_fixels, FixelDataType& vol) :
Base (data, max_fixels),
vol (vol) {}
void operator() (FixelIndexType& index, Image<float>& out)
{
if (vol.valid()) {
for (auto f = Base::Loop (index) (data, vol); f; ++f) {
if (!f.padding())
out.value() += data.value() * vol.value();
}
} else {
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding())
out.value() += data.value();
}
}
}
protected:
FixelDataType vol;
};
class Product : protected Base
{ MEMALIGN (Product)
public:
Product (FixelDataType& data, const index_type max_fixels) :
Base (data, max_fixels) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
index.index(3) = 0;
index_type num_fixels = index.value();
if (!num_fixels) {
out.value() = 0.0;
return;
}
index.index(3) = 1;
index_type offset = index.value();
data.index(0) = offset;
out.value() = data.value();
num_fixels = max_fixels ? std::min (max_fixels, num_fixels) : num_fixels;
for (index_type f = 1; f != num_fixels; ++f) {
data.index(0)++;
out.value() *= data.value();
}
}
};
class Min : protected Base
{ MEMALIGN (Min)
public:
Min (FixelDataType& data, const index_type max_fixels) :
Base (data, max_fixels) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
default_type min = std::numeric_limits<default_type>::infinity();
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding() && data.value() < min)
min = data.value();
}
out.value() = std::isfinite (min) ? min : NAN;
}
};
class Max : protected Base
{ MEMALIGN (Max)
public:
Max (FixelDataType& data, const index_type max_fixels) :
Base (data, max_fixels) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
default_type max = -std::numeric_limits<default_type>::infinity();
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding() && data.value() > max)
max = data.value();
}
out.value() = std::isfinite (max) ? max : NAN;
}
};
class AbsMax : protected Base
{ MEMALIGN (AbsMax)
public:
AbsMax (FixelDataType& data, const index_type max_fixels) :
Base (data, max_fixels) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
default_type absmax = -std::numeric_limits<default_type>::infinity();
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding() && abs (float(data.value())) > absmax)
absmax = abs (float(data.value()));
}
out.value() = std::isfinite (absmax) ? absmax : 0.0;
}
};
class MagMax : protected Base
{ MEMALIGN (MagMax)
public:
MagMax (FixelDataType& data, const index_type num_fixels) :
Base (data, num_fixels) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
default_type magmax = 0.0;
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding() && abs (float(data.value())) > abs (magmax))
magmax = data.value();
}
out.value() = std::isfinite (magmax) ? magmax : 0.0;
}
};
class Complexity : protected Base
{ MEMALIGN (Complexity)
public:
Complexity (FixelDataType& data, const index_type max_fixels) :
Base (data, max_fixels) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
index.index(3) = 0;
index_type num_fixels = index.value();
num_fixels = max_fixels ? std::min (num_fixels, max_fixels) : num_fixels;
if (num_fixels <= 1) {
out.value() = 0.0;
return;
}
default_type max = 0.0;
default_type sum = 0.0;
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding()) {
max = std::max (max, default_type(data.value()));
sum += data.value();
}
}
out.value() = (default_type(num_fixels) / default_type(num_fixels-1.0)) * (1.0 - (max / sum));
}
};
class SF : protected Base
{ MEMALIGN (SF)
public:
SF (FixelDataType& data, const index_type max_fixels) :
Base (data, max_fixels) { }
void operator() (Image<index_type>& index, FixelDataType& out)
{
default_type max = 0.0;
default_type sum = 0.0;
for (auto f = Base::Loop (index) (data); f; ++f) {
if (!f.padding()) {
max = std::max (max, default_type(data.value()));
sum += data.value();
}
}
out.value() = sum ? (max / sum) : 0.0;
}
};
class DEC_unit : protected Base
{ MEMALIGN (DEC_unit)
public:
DEC_unit (FixelDataType& data, const index_type max_fixels, FixelDataType& vol, Image<float>& dir) :
Base (data, max_fixels),
vol (vol), dir (dir) {}
void operator() (Image<index_type>& index, Image<float>& out)
{
Eigen::Vector3d sum_dec = {0.0, 0.0, 0.0};
if (vol.valid()) {
for (auto f = Base::Loop (index) (data, vol, dir); f; ++f) {
if (!f.padding())
sum_dec += Eigen::Vector3d (abs (dir.row(1)[0]), abs (dir.row(1)[1]), abs (dir.row(1)[2])) * data.value() * vol.value();
}
} else {
for (auto f = Base::Loop (index) (data, dir); f; ++f) {
if (!f.padding())
sum_dec += Eigen::Vector3d (abs (dir.row(1)[0]), abs (dir.row(1)[1]), abs (dir.row(1)[2])) * data.value();
}
}
if ((sum_dec.array() != 0.0).any())
sum_dec.normalize();
for (out.index(3) = 0; out.index(3) != 3; ++out.index(3))
out.value() = sum_dec[size_t(out.index(3))];
}
protected:
FixelDataType vol;
Image<float> dir;
};
class DEC_scaled : protected Base
{ MEMALIGN (DEC_scaled)
public:
DEC_scaled (FixelDataType& data, const index_type max_fixels, FixelDataType& vol, Image<float>& dir) :
Base (data, max_fixels),
vol (vol), dir (dir) {}
void operator() (FixelIndexType& index, Image<float>& out)
{
Eigen::Vector3d sum_dec = {0.0, 0.0, 0.0};
default_type sum_value = 0.0;
if (vol.valid()) {
default_type sum_volume = 0.0;
for (auto f = Base::Loop (index) (data, vol, dir); f; ++f) {
if (!f.padding()) {
sum_dec += Eigen::Vector3d (abs (dir.row(1)[0]), abs (dir.row(1)[1]), abs (dir.row(1)[2])) * data.value() * vol.value();
sum_volume += vol.value();
sum_value += vol.value() * data.value();
}
}
if ((sum_dec.array() != 0.0).any())
sum_dec.normalize();
sum_dec *= (sum_value / sum_volume);
} else {
for (auto f = Base::Loop (index) (data, dir); f; ++f) {
if (!f.padding()) {
sum_dec += Eigen::Vector3d (abs (dir.row(1)[0]), abs (dir.row(1)[1]), abs (dir.row(1)[2])) * data.value();
sum_value += data.value();
}
}
if ((sum_dec.array() != 0.0).any())
sum_dec.normalize();
sum_dec *= sum_value;
}
for (out.index(3) = 0; out.index(3) != 3; ++out.index(3))
out.value() = sum_dec[size_t(out.index(3))];
}
protected:
FixelDataType vol;
Image<float> dir;
};
class None : protected Base
{ MEMALIGN (None)
public:
None (FixelDataType& data, const index_type max_fixels, const float fill_value) :
Base (data, max_fixels, true, fill_value) { }
void operator() (FixelIndexType& index, Image<float>& out)
{
for (auto f = Base::Loop (index) (data); f; ++f) {
out.index(3) = f.fixel_index;
out.value() = f.padding() ? pad_value : data.value();
}
}
};
void run ()
{
auto in_data = Fixel::open_fixel_data_file<typename FixelDataType::value_type> (argument[0]);
if (in_data.size(2) != 1)
throw Exception ("Input fixel data file must have a single scalar value per fixel (i.e. have dimensions Nx1x1)");
Header in_index_header = Fixel::find_index_header (Fixel::get_fixel_directory (argument[0]));
auto in_index_image = in_index_header.get_image<typename FixelIndexType::value_type>();
Image<float> in_directions;
const int op = argument[1];
const index_type max_fixels = get_option_value ("number", 0);
if (max_fixels && op == 7)
throw Exception ("\"count\" statistic is meaningless if constraining the number of fixels per voxel using the -number option");
Header H_out (in_index_header);
H_out.datatype() = DataType::Float32;
H_out.datatype().set_byte_order_native();
H_out.keyval().erase (Fixel::n_fixels_key);
if (op == 7) { // count
H_out.ndim() = 3;
H_out.datatype() = DataType::UInt8;
} else if (op == 10 || op == 11) { // dec
H_out.ndim() = 4;
H_out.size (3) = 3;
} else if (op == 12) { // none
H_out.ndim() = 4;
if (max_fixels) {
H_out.size(3) = max_fixels;
} else {
index_type max_count = 0;
for (auto l = Loop ("determining largest fixel count", in_index_image, 0, 3) (in_index_image); l; ++l)
max_count = std::max (max_count, (index_type)in_index_image.value());
if (max_count == 0)
throw Exception ("fixel image is empty");
// 3 volumes per fixel if performing split_dir
H_out.size(3) = max_count;
}
} else {
H_out.ndim() = 3;
}
if (op == 10 || op == 11) // dec
in_directions = Fixel::find_directions_header (
Fixel::get_fixel_directory (in_data.name())).get_image<float>().with_direct_io();
FixelDataType in_vol;
auto opt = get_options ("weighted");
if (opt.size()) {
in_vol = FixelDataType::open (opt[0][0]);
check_dimensions (in_data, in_vol);
}
if (op == 2 || op == 3 || op == 4 || op == 5 || op == 6 ||
op == 7 || op == 8 || op == 9 || op == 12) {
if (in_vol.valid())
WARN ("Option -weighted has no meaningful interpretation for the operation specified; ignoring");
}
opt = get_options ("fill");
float fill_value = 0.0;
if (opt.size()) {
if (op == 12) {
fill_value = opt[0][0];
} else {
WARN ("Option -fill ignored; only applicable to \"none\" operation");
}
}
auto out = Image<float>::create (argument[2], H_out);
auto loop = ThreadedLoop ("converting sparse fixel data to scalar image", in_index_image, 0, 3);
switch (op) {
case 0: loop.run (Mean (in_data, max_fixels, in_vol), in_index_image, out); break;
case 1: loop.run (Sum (in_data, max_fixels, in_vol), in_index_image, out); break;
case 2: loop.run (Product (in_data, max_fixels), in_index_image, out); break;
case 3: loop.run (Min (in_data, max_fixels), in_index_image, out); break;
case 4: loop.run (Max (in_data, max_fixels), in_index_image, out); break;
case 5: loop.run (AbsMax (in_data, max_fixels), in_index_image, out); break;
case 6: loop.run (MagMax (in_data, max_fixels), in_index_image, out); break;
case 7: loop.run ([](Image<index_type>& index, Image<float>& out) { // count
out.value() = index.value();
}, in_index_image, out); break;
case 8: loop.run (Complexity (in_data, max_fixels), in_index_image, out); break;
case 9: loop.run (SF (in_data, max_fixels), in_index_image, out); break;
case 10: loop.run (DEC_unit (in_data, max_fixels, in_vol, in_directions), in_index_image, out); break;
case 11: loop.run (DEC_scaled (in_data, max_fixels, in_vol, in_directions), in_index_image, out); break;
case 12: loop.run (::None (in_data, max_fixels, fill_value), in_index_image, out); break;
}
}
|