File: random_loop.h

package info (click to toggle)
mrtrix3 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,712 kB
  • sloc: cpp: 129,776; python: 9,494; sh: 593; makefile: 234; xml: 47
file content (220 lines) | stat: -rw-r--r-- 5,735 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
/* Copyright (c) 2008-2022 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/.
 */

#ifndef __algo_random_loop_h__
#define __algo_random_loop_h__

#include "apply.h"
#include "progressbar.h"
#include "stride.h"
#include "image_helpers.h"
#include <unordered_set>
#include <algorithm>  // std::shuffle
#include <iterator>

namespace MR
{

 template <class ImageType, class RandomEngine>
  class Random_loop { NOMEMALIGN
    public:
      Random_loop (ImageType& in,
        RandomEngine& random_engine,
        const size_t& axis = 0,
        const size_t& number_iterations = std::numeric_limits<ssize_t>::max()):
          image (in),
          engine (random_engine),
          ax (axis),
          idx(image.size(axis)),
          max_cnt (number_iterations),
          status (true),
          cnt (0)
      {
        init();
        set_next_index();
      }
      void init() {
        std::iota (std::begin(idx), std::end(idx), 0);
        std::shuffle(std::begin(idx), std::end(idx), engine);
        cnt = 0;
        it = std::begin(idx);
        stop = std::end(idx);
      }

      void set_next_index() {
        ++cnt;
        image.index(ax) = *it;
        it++;
      }

      void operator++ ()
      {
        set_next_index();
        if (cnt > max_cnt or it == stop) {
          status = false;
        }
      }
      operator bool() const
      {
        return status && image.index(ax) >= 0 && image.index(ax) < image.size(ax);
      }

    private:
      ImageType& image;
      RandomEngine& engine;
      size_t ax;
      vector<size_t> idx;
      vector<size_t>::iterator it;
      vector<size_t>::iterator stop;
      size_t max_cnt;
      bool status;
      size_t cnt;
 };

  // Random_sparse_loop: ok for VERY sparse loops, slows down significantly at higher density (>5%)
  template <class ImageType>
    class Random_sparse_loop { NOMEMALIGN
      public:
        Random_sparse_loop (ImageType& in,
          const size_t& axis = 0,
          const size_t& number_iterations = std::numeric_limits<ssize_t>::max(),
          const bool repeat = false,
          const ssize_t& min_index = 0,
          const ssize_t& max_index = std::numeric_limits<ssize_t>::max()):
            image (in),
            repeat_ (repeat),
            status (true),
            ax (axis),
            cnt (0),
            min_idx (min_index)
        {
          if (max_index < image.size(ax))
            range = max_index - min_idx + 1;
          else
            range = image.size(ax) - min_idx;
          if (number_iterations < range)
            max_cnt = number_iterations;
          else
            max_cnt = range;
          assert (range >= 1);
          assert (max_cnt >= 1);
          if (repeat_)
            set_next_index_with_repeat();
          else
            set_next_index_no_repeat();
        }

        void set_next_index_no_repeat() {
          while (cnt < max_cnt){
            index = rand() % range + min_idx;
            if (!idx_done.count(index)){
              idx_done.insert(index);
              break;
            }
          }
          ++cnt;
          image.index(ax) = index;
          assert(idx_done.size() < range);
        }

        void set_next_index_with_repeat() {
          index = rand() % range + min_idx;
          ++cnt;
          image.index(ax) = index;
        }

        void operator++ ()
        {
          if (repeat_)
            set_next_index_with_repeat();
          else
            set_next_index_no_repeat();
          if (cnt > max_cnt) {
            status = false;
          }
        }
        operator bool() const
        {
          return status && image.index(ax) >= 0 && image.index(ax) < image.size(ax);
        }

      private:
        ImageType& image;
        bool repeat_;
        bool status;
        size_t ax;
        size_t cnt;
        ssize_t min_idx;
        size_t range;
        size_t max_cnt;
        ssize_t index;
        std::unordered_set<ssize_t> idx_done;
   };

  template <class ImageType, class IterType>
    class Iterator_loop { NOMEMALIGN
      public:
        Iterator_loop (ImageType& in,
          IterType first,
          IterType last,
          const size_t& axis = 0,
          const size_t& number_iterations = std::numeric_limits<ssize_t>::max()):
            image (in),
            ax (axis),
            start (first),
            stop (last),
            max_cnt (number_iterations),
            status (true),
            cnt (0)
        {
          set_next_index();
        }

        void set_next_index() {
          ++cnt;
          image.index(ax) = *start;
          start++;
        }

        void operator++ ()
        {
          set_next_index();
          if (cnt > max_cnt or start == stop) {
            status = false;
          }
        }
        operator bool() const
        {
          return status && image.index(ax) >= 0 && image.index(ax) < image.size(ax);
        }

      private:
        ImageType& image;
        size_t ax;
        IterType& start;
        IterType& stop;
        size_t max_cnt;
        bool status;
        size_t cnt;
   };


  //! @}
}

#endif