File: dirgen.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 (276 lines) | stat: -rw-r--r-- 8,617 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
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
/* 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 "progressbar.h"
#include "math/rng.h"
#include "thread.h"
#include "math/gradient_descent.h"
#include "math/check_gradient.h"
#include "dwi/directions/file.h"

#define DEFAULT_POWER 1
#define DEFAULT_NITER 10000
#define DEFAULT_RESTARTS 10


using namespace MR;
using namespace App;

void usage ()
{

  AUTHOR = "J-Donald Tournier (jdtournier@gmail.com)";

  SYNOPSIS = "Generate a set of uniformly distributed directions using a bipolar electrostatic repulsion model";

  DESCRIPTION
    + "Directions are distributed by analogy to an electrostatic repulsion system, with each direction "
    "corresponding to a single electrostatic charge (for -unipolar), or a pair of diametrically opposed charges "
    "(for the default bipolar case). The energy of the system is determined based on the Coulomb repulsion, "
    "which assumes the form 1/r^power, where r is the distance between any pair of charges, and p is the power "
    "assumed for the repulsion law (default: 1). The minimum energy state is obtained by gradient descent.";


  REFERENCES
    + "Jones, D.; Horsfield, M. & Simmons, A. "
    "Optimal strategies for measuring diffusion in anisotropic systems by magnetic resonance imaging. "
    "Magnetic Resonance in Medicine, 1999, 42: 515-525"

    + "Papadakis, N. G.; Murrills, C. D.; Hall, L. D.; Huang, C. L.-H. & Adrian Carpenter, T. "
    "Minimal gradient encoding for robust estimation of diffusion anisotropy. "
    "Magnetic Resonance Imaging, 2000, 18: 671-679";

  ARGUMENTS
    + Argument ("ndir", "the number of directions to generate.").type_integer (6, std::numeric_limits<int>::max())
    + Argument ("dirs", "the text file to write the directions to, as [ az el ] pairs.").type_file_out();

  OPTIONS
    + Option ("power", "specify exponent to use for repulsion power law (default: " + str(DEFAULT_POWER) + "). This must be a power of 2 (i.e. 1, 2, 4, 8, 16, ...).")
    +   Argument ("exp").type_integer (1, std::numeric_limits<int>::max())

    + Option ("niter", "specify the maximum number of iterations to perform (default: " + str(DEFAULT_NITER) + ").")
    +   Argument ("num").type_integer (1, std::numeric_limits<int>::max())

    + Option ("restarts", "specify the number of restarts to perform (default: " + str(DEFAULT_RESTARTS) + ").")
    +   Argument ("num").type_integer (1, std::numeric_limits<int>::max())

    + Option ("unipolar", "optimise assuming a unipolar electrostatic repulsion model rather than the bipolar model normally assumed in DWI")

    + Option ("cartesian", "Output the directions in Cartesian coordinates [x y z] instead of [az el].");

}




// constrain directions to remain unit length:
class ProjectedUpdate { MEMALIGN(ProjectedUpdate)
  public:
    bool operator() (
        Eigen::VectorXd& newx,
        const Eigen::VectorXd& x,
        const Eigen::VectorXd& g,
        double step_size) {
      newx.noalias() = x - step_size * g;
      for (ssize_t n = 0; n < newx.size(); n += 3)
        newx.segment (n,3).normalize();
      return newx != x;
    }
};







class Energy { MEMALIGN(Energy)
  public:
    Energy (ProgressBar& progress) :
      progress (progress),
      ndirs (to<int> (argument[0])),
      bipolar (!(get_options ("unipolar").size())),
      power (0),
      directions (3 * ndirs) { }

// Non-optimised compilation can't handle recursive inline functions
#ifdef __OPTIMIZE__
FORCE_INLINE
#endif
    double fast_pow (double x, int p) {
      return p == 1 ? x : fast_pow (x*x, p/2);
    }

    using value_type = double;

    size_t size () const { return 3 * ndirs; }

    // set x to original directions provided in constructor.
    // The idea is to save the directions from one run to initialise next run
    // at higher power.
    double init (Eigen::VectorXd& x)
    {
      Math::RNG::Normal<double> rng;
      for (size_t n = 0; n < ndirs; ++n) {
        auto d = x.segment (3*n,3);
        d[0] = rng();
        d[1] = rng();
        d[2] = rng();
        d.normalize();
      }
      return 0.01;
    }



    // function executed by optimiser at each iteration:
    double operator() (const Eigen::VectorXd& x, Eigen::VectorXd& g) {
      double E = 0.0;
      g.setZero();

      for (size_t i = 0; i < ndirs-1; ++i) {
        auto d1 = x.segment (3*i, 3);
        auto g1 = g.segment (3*i, 3);
        for (size_t j = i+1; j < ndirs; ++j) {
          auto d2 = x.segment (3*j, 3);
          auto g2 = g.segment (3*j, 3);

          Eigen::Vector3d r = d1-d2;
          double _1_r2 = 1.0 / r.squaredNorm();
          double _1_r = std::sqrt (_1_r2);
          double e = fast_pow (_1_r, power);
          E += e;
          g1 -= (power * e * _1_r2) * r;
          g2 += (power * e * _1_r2) * r;

          if (bipolar) {
            r = d1+d2;
            _1_r2 = 1.0 / r.squaredNorm();
            _1_r = std::sqrt (_1_r2);
            e = fast_pow (_1_r, power);
            E += e;
            g1 -= (power * e * _1_r2) * r;
            g2 -= (power * e * _1_r2) * r;
          }

        }
      }

      // constrain gradients to lie tangent to unit sphere:
      for (size_t n = 0; n < ndirs; ++n)
        g.segment(3*n,3) -= x.segment(3*n,3).dot (g.segment(3*n,3)) * x.segment(3*n,3);

      return E;
    }



    // function executed per thread:
    void execute ()
    {
      size_t this_start = 0;
      while ((this_start = current_start++) < restarts) {
        INFO ("launching start " + str (this_start));
        double E = 0.0;

        for (power = 1; power <= target_power; power *= 2) {
          Math::GradientDescent<Energy,ProjectedUpdate> optim (*this, ProjectedUpdate());

          INFO ("start " + str(this_start) + ": setting power = " + str (power));
          optim.init();

          size_t iter = 0;
          for (; iter < niter; iter++) {
            if (!optim.iterate())
              break;

            DEBUG ("start " + str(this_start) + ": [ " + str (iter) + " ] (pow = " + str (power) + ") E = " + str (optim.value(), 8)
                + ", grad = " + str (optim.gradient_norm(), 8));

            std::lock_guard<std::mutex> lock (mutex);
            ++progress;
          }

          directions = optim.state();
          E = optim.value();
        }



        std::lock_guard<std::mutex> lock (mutex);
        if (E < best_E) {
          best_E = E;
          best_directions = directions;
        }
      }
    }


    static size_t restarts;
    static int target_power;
    static size_t niter;
    static double best_E;
    static Eigen::VectorXd best_directions;

  protected:
    ProgressBar& progress;
    size_t ndirs;
    bool bipolar;
    int power;
    Eigen::VectorXd directions;
    double E;

    static std::mutex mutex;
    static std::atomic<size_t> current_start;
};


size_t Energy::restarts (DEFAULT_RESTARTS);
int Energy::target_power (DEFAULT_POWER);
size_t Energy::niter (DEFAULT_NITER);
std::mutex Energy::mutex;
std::atomic<size_t> Energy::current_start (0);
double Energy::best_E = std::numeric_limits<double>::infinity();
Eigen::VectorXd Energy::best_directions;




void run ()
{
  Energy::restarts = get_option_value ("restarts", DEFAULT_RESTARTS);
  Energy::target_power = get_option_value ("power", DEFAULT_POWER);
  Energy::niter = get_option_value ("niter", DEFAULT_NITER);

  {
    ProgressBar progress ("Optimising directions up to power " + str(Energy::target_power) + " (" + str(Energy::restarts) + " restarts)");
    Energy energy_functor (progress);
    auto threads = Thread::run (Thread::multi (energy_functor), "energy function");
  }

  CONSOLE ("final energy = " + str(Energy::best_E));
  size_t ndirs = Energy::best_directions.size()/3;
  Eigen::MatrixXd directions_matrix (ndirs, 3);
  for (size_t n = 0; n < ndirs; ++n)
    directions_matrix.row (n) = Energy::best_directions.segment (3*n, 3);

  DWI::Directions::save (directions_matrix, argument[1], get_options ("cartesian").size());
}