File: tcksift2.cpp

package info (click to toggle)
mrtrix3 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,300 kB
  • sloc: cpp: 130,470; python: 9,603; sh: 597; makefile: 62; xml: 47
file content (245 lines) | stat: -rw-r--r-- 9,416 bytes parent folder | download
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
/* 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 "command.h"
#include "exception.h"
#include "image.h"
#include "header.h"

#include "file/path.h"

#include "dwi/directions/set.h"

#include "dwi/tractography/mapping/fixel_td_map.h"

#include "dwi/tractography/SIFT/proc_mask.h"
#include "dwi/tractography/SIFT/sift.h"

#include "dwi/tractography/SIFT2/tckfactor.h"





using namespace MR;
using namespace App;

using namespace MR::DWI;
using namespace MR::DWI::Tractography;
using namespace MR::DWI::Tractography::SIFT2;




const OptionGroup SIFT2RegularisationOption = OptionGroup ("Regularisation options for SIFT2")

  + Option ("reg_tikhonov", "provide coefficient for regularising streamline weighting coefficients (Tikhonov regularisation) (default: " + str(SIFT2_REGULARISATION_TIKHONOV_DEFAULT, 2) + ")")
    + Argument ("value").type_float (0.0)

  + Option ("reg_tv", "provide coefficient for regularising variance of streamline weighting coefficient to fixels along its length (Total Variation regularisation) (default: " + str(SIFT2_REGULARISATION_TV_DEFAULT, 2) + ")")
    + Argument ("value").type_float (0.0);



const OptionGroup SIFT2AlgorithmOption = OptionGroup ("Options for controlling the SIFT2 optimisation algorithm")

  + Option ("min_td_frac", "minimum fraction of the FOD integral reconstructed by streamlines; "
                           "if the reconstructed streamline density is below this fraction, the fixel is excluded from optimisation "
                           "(default: " + str(SIFT2_MIN_TD_FRAC_DEFAULT, 2) + ")")
    + Argument ("fraction").type_float (0.0, 1.0)

  + Option ("min_iters", "minimum number of iterations to run before testing for convergence; "
                         "this can prevent premature termination at early iterations if the cost function increases slightly "
                         "(default: " + str(SIFT2_MIN_ITERS_DEFAULT) + ")")
    + Argument ("count").type_integer (0)

  + Option ("max_iters", "maximum number of iterations to run before terminating program")
    + Argument ("count").type_integer (0)

  + Option ("min_factor", "minimum weighting factor for an individual streamline; "
                          "if the factor falls below this number the streamline will be rejected entirely (factor set to zero) "
                          "(default: " + str(std::exp (SIFT2_MIN_COEFF_DEFAULT), 2) + ")")
    + Argument ("factor").type_float (0.0, 1.0)

  + Option ("min_coeff", "minimum weighting coefficient for an individual streamline; "
                         "similar to the '-min_factor' option, but using the exponential coefficient basis of the SIFT2 model; "
                         "these parameters are related as: factor = e^(coeff). "
                         "Note that the -min_factor and -min_coeff options are mutually exclusive - you can only provide one. "
                         "(default: " + str(SIFT2_MIN_COEFF_DEFAULT, 2) + ")")
    + Argument ("coeff").type_float (-std::numeric_limits<default_type>::infinity(), 0.0)

  + Option ("max_factor", "maximum weighting factor that can be assigned to any one streamline "
                          "(default: " + str(std::exp (SIFT2_MAX_COEFF_DEFAULT), 2) + ")")
    + Argument ("factor").type_float (1.0)

  + Option ("max_coeff", "maximum weighting coefficient for an individual streamline; "
                         "similar to the '-max_factor' option, but using the exponential coefficient basis of the SIFT2 model; "
                         "these parameters are related as: factor = e^(coeff). "
                         "Note that the -max_factor and -max_coeff options are mutually exclusive - you can only provide one. "
                         "(default: " + str(SIFT2_MAX_COEFF_DEFAULT, 2) + ")")
    + Argument ("coeff").type_float (1.0)


  + Option ("max_coeff_step", "maximum change to a streamline's weighting coefficient in a single iteration "
                              "(default: " + str(SIFT2_MAX_COEFF_STEP_DEFAULT, 2) + ")")
    + Argument ("step").type_float ()

  + Option ("min_cf_decrease", "minimum decrease in the cost function (as a fraction of the initial value) that must occur each iteration for the algorithm to continue "
                               "(default: " + str(SIFT2_MIN_CF_DECREASE_DEFAULT, 2) + ")")
    + Argument ("frac").type_float (0.0, 1.0)

  + Option ("linear", "perform a linear estimation of streamline weights, rather than the standard non-linear optimisation "
                      "(typically does not provide as accurate a model fit; but only requires a single pass)");





void usage ()
{

  AUTHOR = "Robert E. Smith (robert.smith@florey.edu.au)";

  SYNOPSIS = "Optimise per-streamline cross-section multipliers to match a whole-brain tractogram to fixel-wise fibre densities";

  REFERENCES
    + "Smith, R. E.; Tournier, J.-D.; Calamante, F. & Connelly, A. " // Internal
    "SIFT2: Enabling dense quantitative assessment of brain white matter connectivity using streamlines tractography. "
    "NeuroImage, 2015, 119, 338-351"

    + "* If using the -linear option: \n"
    "Smith, RE; Raffelt, D; Tournier, J-D; Connelly, A. " // Internal
    "Quantitative Streamlines Tractography: Methods and Inter-Subject Normalisation. "
    "OHBM Aperture, 10.52294/ApertureNeuro.2022.2.NEOD9565.";

  ARGUMENTS
  + Argument ("in_tracks",   "the input track file").type_tracks_in()
  + Argument ("in_fod",      "input image containing the spherical harmonics of the fibre orientation distributions").type_image_in()
  + Argument ("out_weights", "output text file containing the weighting factor for each streamline").type_file_out();

  OPTIONS

  + SIFT::SIFTModelProcMaskOption
  + SIFT::SIFTModelOption
  + SIFT::SIFTOutputOption

  + Option ("out_coeffs", "output text file containing the weighting coefficient for each streamline")
    + Argument ("path").type_file_out()

  + SIFT2RegularisationOption
  + SIFT2AlgorithmOption;

}




void run ()
{

  if (get_options("min_factor").size() && get_options("min_coeff").size())
    throw Exception ("Options -min_factor and -min_coeff are mutually exclusive");
  if (get_options("max_factor").size() && get_options("max_coeff").size())
    throw Exception ("Options -max_factor and -max_coeff are mutually exclusive");

  if (Path::has_suffix (argument[2], ".tck"))
    throw Exception ("Output of tcksift2 command should be a text file, not a tracks file");

  auto in_dwi = Image<float>::open (argument[1]);

  DWI::Directions::FastLookupSet dirs (1281);

  TckFactor tckfactor (in_dwi, dirs);

  const bool output_debug = get_options ("output_debug").size();

  if (output_debug)
    tckfactor.output_proc_mask ("proc_mask.mif");

  tckfactor.perform_FOD_segmentation (in_dwi);
  tckfactor.scale_FDs_by_GM();

  tckfactor.map_streamlines (argument[0]);

  tckfactor.store_orig_TDs();

  const float min_td_frac = get_option_value ("min_td_frac", SIFT2_MIN_TD_FRAC_DEFAULT);
  tckfactor.remove_excluded_fixels (min_td_frac);

  if (output_debug)
    tckfactor.output_all_debug_images ("before");

  if (get_options ("linear").size()) {

    tckfactor.calc_afcsa();

  } else {

    auto opt = get_options ("csv");
    if (opt.size())
      tckfactor.set_csv_path (opt[0][0]);

    const float reg_tikhonov = get_option_value ("reg_tikhonov", SIFT2_REGULARISATION_TIKHONOV_DEFAULT);
    const float reg_tv = get_option_value ("reg_tv", SIFT2_REGULARISATION_TV_DEFAULT);
    tckfactor.set_reg_lambdas (reg_tikhonov, reg_tv);

    opt = get_options ("min_iters");
    if (opt.size())
      tckfactor.set_min_iters (int(opt[0][0]));
    opt = get_options ("max_iters");
    if (opt.size())
      tckfactor.set_max_iters (int(opt[0][0]));
    opt = get_options ("min_factor");
    if (opt.size())
      tckfactor.set_min_factor (float(opt[0][0]));
    opt = get_options ("min_coeff");
    if (opt.size())
      tckfactor.set_min_coeff (float(opt[0][0]));
    opt = get_options ("max_factor");
    if (opt.size())
      tckfactor.set_max_factor (float(opt[0][0]));
    opt = get_options ("max_coeff");
    if (opt.size())
      tckfactor.set_max_coeff (float(opt[0][0]));
    opt = get_options ("max_coeff_step");
    if (opt.size())
      tckfactor.set_max_coeff_step (float(opt[0][0]));
    opt = get_options ("min_cf_decrease");
    if (opt.size())
      tckfactor.set_min_cf_decrease (float(opt[0][0]));

    tckfactor.estimate_factors();

  }

  tckfactor.output_factors (argument[2]);

  auto opt = get_options ("out_coeffs");
  if (opt.size())
    tckfactor.output_coefficients (opt[0][0]);

  if (output_debug)
    tckfactor.output_all_debug_images ("after");

  opt = get_options ("out_mu");
  if (opt.size()) {
    File::OFStream out_mu (opt[0][0]);
    out_mu << tckfactor.mu();
  }

}