forked from MRtrix3/mrtrix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5ttcheck.cpp
117 lines (89 loc) · 3.26 KB
/
5ttcheck.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
/* Copyright (c) 2008-2017 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/.
*
* MRtrix 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 "app.h"
#include "command.h"
#include "datatype.h"
#include "image.h"
#include "image_helpers.h"
#include "algo/copy.h"
#include "algo/loop.h"
#include "dwi/tractography/ACT/act.h"
using namespace MR;
using namespace App;
#define MAX_ERROR 0.001
void usage ()
{
AUTHOR = "Robert E. Smith ([email protected])";
SYNOPSIS = "Thoroughly check that one or more images conform to the expected ACT five-tissue-type (5TT) format";
ARGUMENTS
+ Argument ("input", "the 5TT image(s) to be tested").type_image_in().allow_multiple();
OPTIONS
+ Option ("masks", "output mask images highlighting voxels where the input does not conform to 5TT requirements")
+ Argument ("prefix").type_text();
}
void run ()
{
const std::string mask_prefix = get_option_value<std::string> ("masks", "");
size_t error_count = 0;
for (size_t i = 0; i != argument.size(); ++i) {
auto in = Image<float>::open (argument[i]);
Image<bool> voxels;
Header H_out (in);
H_out.ndim() = 3;
H_out.datatype() = DataType::Bit;
if (mask_prefix.size())
voxels = Image<bool>::scratch (H_out, "Scratch image for " + argument[i]);
try {
// This checks:
// - Floating-point image
// - 4-dimensional
// - 5 volumes
DWI::Tractography::ACT::verify_5TT_image (in);
// Manually loop through all voxels, make sure that for every voxel the
// sum of tissue partial volumes is either 0 or 1 (or close enough)
size_t voxel_error_sum = 0;
for (auto outer = Loop(in, 0, 3) (in); outer; ++outer) {
default_type sum = 0.0;
for (auto inner = Loop(3) (in); inner; ++inner)
sum += in.value();
if (!sum) continue;
if (std::abs (sum-1.0) > MAX_ERROR) {
++voxel_error_sum;
if (voxels.valid()) {
assign_pos_of (in, 0, 3).to (voxels);
voxels.value() = true;
}
}
}
if (voxel_error_sum) {
WARN ("Image \"" + argument[i] + "\" contains " + str(voxel_error_sum) + " brain voxels with non-unity sum of partial volume fractions");
++error_count;
if (voxels.valid()) {
auto out = Image<bool>::create (mask_prefix + Path::basename (argument[i]), H_out);
copy (voxels, out);
}
} else {
INFO ("Image \"" + argument[i] + "\" conforms to 5TT format");
}
} catch (...) {
WARN ("Image \"" + argument[i] + "\" does not conform to fundamental 5TT format requirements");
++error_count;
}
}
if (error_count) {
if (argument.size() > 1)
throw Exception (str(error_count) + " input image" + (error_count > 1 ? "s do" : " does") + " not conform to 5TT format");
else
throw Exception ("Input image does not conform to 5TT format");
}
}