EddyGpuUtils.h 10.7 KB
Newer Older
wangkx1's avatar
init  
wangkx1 committed
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
/////////////////////////////////////////////////////////////////////
///
/// \file EddyGpuUtils.h
/// \brief Declarations of static class with collection of GPU routines used in the eddy project
///
/// The routines declared here are "bridges" on to the actual GPU
/// routines. The interface to these routines only display classes
/// that are part of the "regular" FSL libraries. Hence this file
/// can be safely included by files that know nothing of the GPU
/// and that are compiled by gcc.
///
/// \author Jesper Andersson
/// \version 1.0b, Nov., 2012.
/// \Copyright (C) 2012 University of Oxford
///
/////////////////////////////////////////////////////////////////////

#ifndef EddyGpuUtils_h
#define EddyGpuUtils_h

#include <cstdlib>
#include <cstddef>
#include <string>
#include <vector>
#include <cmath>
#include <memory>
#include <hip/hip_runtime.h>
#include "armawrap/newmat.h"
#include "newimage/newimageall.h"
#include "miscmaths/miscmaths.h"
#include "EddyHelperClasses.h"
#include "DiffusionGP.h"
#include "b0Predictor.h"
#include "ECScanClasses.h"
#include "EddyCommandLineOptions.h"

namespace EDDY {

/////////////////////////////////////////////////////////////////////
///
/// \brief This class contains a set of static methods that implement
/// various utility functions for the eddy project implemented on
/// CUDA GPU.
///
/////////////////////////////////////////////////////////////////////
class EddyGpuUtils
{
public:

  /// Loads prediction maker with images unwarped according to current EC estimates
  static std::shared_ptr<DWIPredictionMaker> LoadPredictionMaker(// Input
								 const EddyCommandLineOptions& clo,
							         ScanType                      st,
								 const ECScanManager&          sm,
								 unsigned int                  iter,
								 float                         fwhm,
								 // Output
								 NEWIMAGE::volume<float>&      mask,
								 // Optional input
								 bool                          use_orig=false);

  ///
  static void MakeScatterBrainPredictions(// Input
					  const EddyCommandLineOptions& clo,
					  const ECScanManager&          sm,
					  const std::vector<double>&    hypar,
					  // Output
					  NEWIMAGE::volume4D<float>&    pred,
					  // Optional input
					  bool                          vwbvrot=false);

  /*
  /// Replaces the scans indicated by rm
  static void UpdatePredictionMaker(// Input
				    const EddyCommandLineOptions&        clo,
				    ScanType                             st,
				    const ECScanManager&                 sm,
				    const ReplacementManager&            rm,
				    const NEWIMAGE::volume<float>&       mask,
				    // Input/Output
				    std::shared_ptr<DWIPredictionMaker>  pmp);
  */

  /// Returns a scan corrected for motion and distortions
  static NEWIMAGE::volume<float> GetUnwarpedScan(// Input
						 const EDDY::ECScan&                               scan,
						 std::shared_ptr<const NEWIMAGE::volume<float> >   susc,
						 std::shared_ptr<const NEWIMAGE::volume<float> >   bias,
						 bool                                              use_orig,
						 // Optional output
						 NEWIMAGE::volume<float>                           *omask=NULL);

  /// Returns a scan corrected for motion and distortions, helped by the prediction in pred
  static NEWIMAGE::volume<float> GetUnwarpedScan(// Input
						 const EDDY::ECScan&                               scan,
						 std::shared_ptr<const NEWIMAGE::volume<float> >   susc,
						 std::shared_ptr<const NEWIMAGE::volume<float> >   bias,
						 const NEWIMAGE::volume<float>&                    pred,
						 bool                                              use_orig,
						 // Optional output
						 NEWIMAGE::volume<float>                           *omask=NULL);

  /// Returns a scan corrected for motion and distortions. Will override slice-to-vol
  static NEWIMAGE::volume<float> GetVolumetricUnwarpedScan(// Input
							   const EDDY::ECScan&                               scan,
							   std::shared_ptr<const NEWIMAGE::volume<float> >   susc,
							   std::shared_ptr<const NEWIMAGE::volume<float> >   bias,
							   bool                                              use_orig,
							   // Optional output
							   NEWIMAGE::volume<float>                           *omask=nullptr,
							   NEWIMAGE::volume4D<float>                         *deriv=nullptr);

  /// Calculate the movement RMS for all scans of type st.
  static arma::mat GetMovementRMS(const ECScanManager& sm,
				  ScanType             st,
				  bool                 restricted);

  /// Returns a scan corrected for motion (scanner->model(sort of))
  static void GetMotionCorrectedScan(// Input
				     const EDDY::ECScan&       scan,
				     bool                      use_orig,
				     // Output
				     NEWIMAGE::volume<float>&  ovol,
				     // Optional output
				     NEWIMAGE::volume<float>   *omask=NULL);

  /// Returns a scan (in model space) warped into observation space
  static NEWIMAGE::volume<float> TransformModelToScanSpace(const EDDY::ECScan&                               scan,
							   const NEWIMAGE::volume<float>&                    mima,
							   std::shared_ptr<const NEWIMAGE::volume<float> >   susc,
							   bool                                              jacmod=true);

  static NEWIMAGE::volume4D<float> DerivativesForModelToScanSpaceTransform(const EDDY::ECScan&                               scan,
									   const NEWIMAGE::volume<float>&                    mima,
									   std::shared_ptr<const NEWIMAGE::volume<float> >   susc);

  static NEWIMAGE::volume4D<float> DirectDerivativesForModelToScanSpaceTransform(const EDDY::ECScan&                               scan,
										 const NEWIMAGE::volume<float>&                    mima,
										 std::shared_ptr<const NEWIMAGE::volume<float> >   susc);

  /// Returns a scan convolved with a Gaussian with fwhm in mm
  static NEWIMAGE::volume<float> Smooth(const NEWIMAGE::volume<float>&  ima,
					float                           fwhm);

  /// Detects outlier-slices
  static DiffStatsVector DetectOutliers(// Input
					const EddyCommandLineOptions&             clo,
					ScanType                                  st,
					const std::shared_ptr<DWIPredictionMaker> pmp,
					const NEWIMAGE::volume<float>&            mask,
					const ECScanManager&                      sm,
					// Input/Output
					ReplacementManager&                       rm);

  /// Detects outlier-slices
  static DiffStatsVector DetectOutliers(// Input
					const EddyCommandLineOptions&             clo,
					ScanType                                  st,
					const std::shared_ptr<DWIPredictionMaker> pmp,
					const NEWIMAGE::volume<float>&            mask,
					const ECScanManager&                      sm,
					// These inputs are for debugging purposes only
					unsigned int                              iter,
					unsigned int                              level,
					// Input/Output
					ReplacementManager&                       rm);
  /// Replaces outlier-slices
  static void ReplaceOutliers(// Input
			      const EddyCommandLineOptions&             clo,
			      ScanType                                  st,
			      const std::shared_ptr<DWIPredictionMaker> pmp,
			      const NEWIMAGE::volume<float>&            mask,
			      const ReplacementManager&                 rm,
			      bool                                      add_noise,
			      // Input/Output
			      ECScanManager&                            sm);

  /// Performs update of movement and EC parameters for one scan.
  static double MovAndECParamUpdate(// Input
				    const NEWIMAGE::volume<float>&                    pred,
				    std::shared_ptr<const NEWIMAGE::volume<float> >   susc,
				    std::shared_ptr<const NEWIMAGE::volume<float> >   bias,
				    const NEWIMAGE::volume<float>&                    pmask,
				    float                                             fwhm,
				    bool                                              very_verbose,
				    unsigned int                                      scindex,
				    // Input/output
				    EDDY::ECScan&                                     scan);

  /// Performs update of movement and EC parameters for one scan.
  static double MovAndECParamUpdate(// Input
				    const NEWIMAGE::volume<float>&                    pred,
				    std::shared_ptr<const NEWIMAGE::volume<float> >   susc,
				    std::shared_ptr<const NEWIMAGE::volume<float> >   bias,
				    const NEWIMAGE::volume<float>&                    pmask,
				    float                                             fwhm,
				    bool                                              very_verbose,
				    // These inputs are for debug purposes only
				    unsigned int                                      scindex,
				    unsigned int                                      iter,
				    unsigned int                                      level,
				    // Input/output
				    EDDY::ECScan&                                     scan);

  /// Performs update of long time-constant EC parameters for all scans
  // Does currently not use the bias parameter
  static std::vector<double> LongECParamUpdate(// Input
					       const std::vector<NEWIMAGE::volume<float> >&      pred,         // Predictions in model space
					       const NEWIMAGE::volume<float>&                    pmask,        // "Data valid" mask in model space
					       float                                             fwhm,         // FWHM for Gaussian smoothing
					       bool                                              very_verbose, // Detailed output to screen?
					       // Input/output
					       EDDY::ECScanManager&                              sm);          // Scans we want to register to predictions

  /// Performs update of long time-constant EC parameters for all scans
  // Does currently not use the bias parameter
  static std::vector<double> LongECParamUpdate(// Input
					       const std::vector<NEWIMAGE::volume<float> >&      pred,         // Predictions in model space
					       const NEWIMAGE::volume<float>&                    pmask,        // "Data valid" mask in model space
					       float                                             fwhm,         // FWHM for Gaussian smoothing
					       bool                                              very_verbose, // Detailed output to screen?
					       // These input parameters are for debugging only
					       unsigned int                                      iter,         // Iteration
					       unsigned int                                      level,        // Determines how much gets written
					       const std::vector<unsigned int>&                  debug_index,  // Indicies of scans to write debug info for
					       // Input/output
					       EDDY::ECScanManager&                              sm);          // Scans we want to register to predictions

};

} // End namespace EDDY

#endif // End #ifndef EddyGpuUtils_h