forked from MRtrix3/mrtrix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phase_encoding.cpp
184 lines (141 loc) · 6.3 KB
/
phase_encoding.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
/* Copyright (c) 2008-2020 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 "phase_encoding.h"
#include "math/math.h"
namespace MR
{
namespace PhaseEncoding
{
using namespace App;
const OptionGroup ImportOptions = OptionGroup ("Options for importing phase-encode tables")
+ Option ("import_pe_table", "import a phase-encoding table from file")
+ Argument ("file").type_file_in()
+ Option ("import_pe_eddy", "import phase-encoding information from an EDDY-style config / index file pair")
+ Argument ("config").type_file_in()
+ Argument ("indices").type_file_in();
const OptionGroup SelectOptions = OptionGroup ("Options for selecting volumes based on phase-encoding")
+ Option ("pe", "select volumes with a particular phase encoding; "
"this can be three comma-separated values (for i,j,k components of vector direction) or four (direction & total readout time)")
+ Argument ("desc").type_sequence_float();
const OptionGroup ExportOptions = OptionGroup ("Options for exporting phase-encode tables")
+ Option ("export_pe_table", "export phase-encoding table to file")
+ Argument ("file").type_file_out()
+ Option ("export_pe_eddy", "export phase-encoding information to an EDDY-style config / index file pair")
+ Argument ("config").type_file_out()
+ Argument ("indices").type_file_out();
void clear_scheme (Header& header)
{
auto erase = [&] (const std::string& s) { auto it = header.keyval().find (s); if (it != header.keyval().end()) header.keyval().erase (it); };
erase ("pe_scheme");
erase ("PhaseEncodingDirection");
erase ("TotalReadoutTime");
}
Eigen::MatrixXd parse_scheme (const Header& header)
{
Eigen::MatrixXd PE;
const auto it = header.keyval().find ("pe_scheme");
if (it != header.keyval().end()) {
try {
PE = parse_matrix (it->second);
} catch (Exception& e) {
throw Exception (e, "malformed PE scheme in image \"" + header.name() + "\"");
}
if (ssize_t(PE.rows()) != ((header.ndim() > 3) ? header.size(3) : 1))
throw Exception ("malformed PE scheme in image \"" + header.name() + "\" - number of rows does not equal number of volumes");
} else {
const auto it_dir = header.keyval().find ("PhaseEncodingDirection");
if (it_dir != header.keyval().end()) {
const auto it_time = header.keyval().find ("TotalReadoutTime");
const size_t cols = it_time == header.keyval().end() ? 3 : 4;
Eigen::Matrix<default_type, Eigen::Dynamic, 1> row (cols);
row.head(3) = Axes::id2dir (it_dir->second);
if (it_time != header.keyval().end())
row[3] = to<default_type>(it_time->second);
PE.resize ((header.ndim() > 3) ? header.size(3) : 1, cols);
PE.rowwise() = row.transpose();
}
}
return PE;
}
Eigen::MatrixXd get_scheme (const Header& header)
{
DEBUG ("searching for suitable phase encoding data...");
using namespace App;
Eigen::MatrixXd result;
try {
const auto opt_table = get_options ("import_pe_table");
if (opt_table.size())
result = load (opt_table[0][0]);
const auto opt_eddy = get_options ("import_pe_eddy");
if (opt_eddy.size()) {
if (opt_table.size())
throw Exception ("Phase encoding table can be provided using either -import_pe_table or -import_pe_eddy option, but NOT both");
result = load_eddy (opt_eddy[0][0], opt_eddy[0][1]);
}
if (!opt_table.size() && !opt_eddy.size())
result = parse_scheme (header);
}
catch (Exception& e) {
throw Exception (e, "error importing phase encoding table for image \"" + header.name() + "\"");
}
if (!result.rows())
return result;
if (result.cols() < 3)
throw Exception ("unexpected phase encoding table matrix dimensions");
INFO ("found " + str (result.rows()) + "x" + str (result.cols()) + " phase encoding table");
return result;
}
Eigen::MatrixXd eddy2scheme (const Eigen::MatrixXd& config, const Eigen::Array<int, Eigen::Dynamic, 1>& indices)
{
if (config.cols() != 4)
throw Exception ("Expected 4 columns in EDDY-format phase-encoding config file");
Eigen::MatrixXd result (indices.size(), 4);
for (ssize_t row = 0; row != indices.size(); ++row) {
if (indices[row] > config.rows())
throw Exception ("Malformed EDDY-style phase-encoding information: Index exceeds number of config entries");
result.row(row) = config.row(indices[row]-1);
}
return result;
}
void export_commandline (const Header& header)
{
auto check = [&](const Eigen::MatrixXd& m) -> const Eigen::MatrixXd& {
if (!m.rows())
throw Exception ("no phase-encoding information found within image \"" + header.name() + "\"");
return m;
};
auto scheme = parse_scheme (header);
auto opt = get_options ("export_pe_table");
if (opt.size())
save (check (scheme), opt[0][0]);
opt = get_options ("export_pe_eddy");
if (opt.size())
save_eddy (check (scheme), opt[0][0], opt[0][1]);
}
Eigen::MatrixXd load (const std::string& path)
{
const Eigen::MatrixXd result = load_matrix (path);
check (result);
return result;
}
Eigen::MatrixXd load_eddy (const std::string& config_path, const std::string& index_path)
{
const Eigen::MatrixXd config = load_matrix (config_path);
const Eigen::Array<int, Eigen::Dynamic, 1> indices = load_vector<int> (index_path);
return eddy2scheme (config, indices);
}
}
}