-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpiralGriddingGadget.cpp
315 lines (263 loc) · 15.4 KB
/
SpiralGriddingGadget.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "SpiralGriddingGadget.h"
#include "hoNDFFT.h"
#include "hoNDArray_math.h"
#include "hoNDArray_utils.h"
#include "hoNDArray_elemwise.h"
#include "CFL_IO2.h"
#include <gadgetron/mri_core_coil_map_estimation.h>
namespace Gadgetron
{
SpiralGriddingGadget::SpiralGriddingGadget()
: image_counter_(0)
{
}
int SpiralGriddingGadget::process_config(ACE_Message_Block *mb)
{
try
{
deserialize(mb->rd_ptr(), header);
}
catch (...)
{
GDEBUG("Error parsing ISMRMRD Header");
}
if (!header.acquisitionSystemInformation)
{
GDEBUG("acquisitionSystemInformation not found in header. Bailing out");
return GADGET_FAIL;
}
return GADGET_OK;
}
int SpiralGriddingGadget::process(GadgetContainerMessage<IsmrmrdReconData> *m1)
{
// Iterate over all the recon bits
for (std::vector<IsmrmrdReconBit>::iterator it = m1->getObjectPtr()->rbit_.begin();
it != m1->getObjectPtr()->rbit_.end(); ++it)
{
// Grab a reference to the buffer containing the imaging data
// We are ignoring the reference data
// IsmrmrdDataBuffered & dbuff2 = it->data_;
IsmrmrdReconBit &rbit = *it;
IsmrmrdDataBuffered &dbuff = it->data_;
auto &data = dbuff.data_;
size_t E0 = data.get_size(0);
size_t E1 = data.get_size(1);
size_t E2 = data.get_size(2);
size_t CHA = data.get_size(3);
size_t N = data.get_size(4);
size_t S = data.get_size(5);
size_t LOC = data.get_size(6);
matrix_size = header.encoding.front().encodedSpace.matrixSize;
hoNFFT_plan<float, 2> nufft = hoNFFT_plan(vector_td<size_t, 2>(matrix_size.x, matrix_size.y), gridding_os.value(), kernel_width.value());
// NFFT_prep_mode mode=NFFT_prep_mode::NC2C;
if (!perform_pics.value())
{
auto &traj = dbuff.trajectory_.value();
Gadgetron::hoNDArray<floatd2> trajectory(traj.get_size(1) * traj.get_size(2));
for (uint32_t i = 0; i < E0 * E1 * traj.get_size(0); i += traj.get_size(0))
{
trajectory[i / traj.get_size(0)] = vector_td<float, 2>(traj[i], traj[i + 1]);
}
nufft.preprocess(trajectory, NFFT_prep_mode::NC2C);
}
auto &dcw = dbuff.density_.value();
dcw.reshape(-1);
// Create an image array message
GadgetContainerMessage<IsmrmrdImageArray> *cm1 =
new GadgetContainerMessage<IsmrmrdImageArray>();
// Grab references to the image array data and headers
IsmrmrdImageArray &imarray = *cm1->getObjectPtr();
// The image array data will be [E0,E1,E2,1,N,S,LOC] big
// Will collapse across coils at the end
std::vector<size_t> data_dims(7);
data_dims[0] = matrix_size.x;
data_dims[1] = matrix_size.y;
data_dims[2] = matrix_size.z;
data_dims[3] = 1;
data_dims[4] = N;
data_dims[5] = S;
data_dims[6] = LOC;
imarray.data_.create(data_dims);
// ImageHeaders will be [N, S, LOC]
std::vector<size_t> header_dims(3);
header_dims[0] = N;
header_dims[1] = S;
header_dims[2] = LOC;
imarray.headers_.create(header_dims);
std::vector<size_t> img_dims(3);
img_dims[0] = matrix_size.x;
img_dims[1] = matrix_size.y;
img_dims[2] = matrix_size.z;
// We will not add any meta data
// so skip the meta_ part
auto &headers = dbuff.headers_;
// Loop over S and N and LOC
for (uint16_t loc = 0; loc < LOC; loc++)
{
for (uint16_t s = 0; s < S; s++)
{
for (uint16_t n = 0; n < N; n++)
{
// Set some information into the image header
// Use the middle acquisition header for some info
//[E1, E2, N, S, LOC]
ISMRMRD::AcquisitionHeader &acqhdr = dbuff.headers_(0,
0,
n, s, loc);
imarray.headers_(n, s, loc).measurement_uid = acqhdr.measurement_uid;
imarray.headers_(n, s, loc).matrix_size[0] = matrix_size.x;
imarray.headers_(n, s, loc).matrix_size[1] = matrix_size.y;
imarray.headers_(n, s, loc).matrix_size[2] = matrix_size.z;
imarray.headers_(n, s, loc).field_of_view[0] = dbuff.sampling_.encoded_FOV_[0];
imarray.headers_(n, s, loc).field_of_view[1] = dbuff.sampling_.encoded_FOV_[1];
imarray.headers_(n, s, loc).field_of_view[2] = dbuff.sampling_.encoded_FOV_[2];
imarray.headers_(n, s, loc).channels = 1;
imarray.headers_(n, s, loc).average = acqhdr.idx.average;
imarray.headers_(n, s, loc).slice = acqhdr.idx.slice;
imarray.headers_(n, s, loc).contrast = acqhdr.idx.contrast;
imarray.headers_(n, s, loc).phase = acqhdr.idx.phase;
imarray.headers_(n, s, loc).repetition = acqhdr.idx.repetition;
imarray.headers_(n, s, loc).set = acqhdr.idx.set;
imarray.headers_(n, s, loc).acquisition_time_stamp = acqhdr.acquisition_time_stamp;
imarray.headers_(n, s, loc).position[0] = acqhdr.position[0];
imarray.headers_(n, s, loc).position[1] = acqhdr.position[1];
imarray.headers_(n, s, loc).position[2] = acqhdr.position[2];
imarray.headers_(n, s, loc).read_dir[0] = acqhdr.read_dir[0];
imarray.headers_(n, s, loc).read_dir[1] = acqhdr.read_dir[1];
imarray.headers_(n, s, loc).read_dir[2] = acqhdr.read_dir[2];
imarray.headers_(n, s, loc).phase_dir[0] = acqhdr.phase_dir[0];
imarray.headers_(n, s, loc).phase_dir[1] = acqhdr.phase_dir[1];
imarray.headers_(n, s, loc).phase_dir[2] = acqhdr.phase_dir[2];
imarray.headers_(n, s, loc).slice_dir[0] = acqhdr.slice_dir[0];
imarray.headers_(n, s, loc).slice_dir[1] = acqhdr.slice_dir[1];
imarray.headers_(n, s, loc).slice_dir[2] = acqhdr.slice_dir[2];
imarray.headers_(n, s, loc).patient_table_position[0] = acqhdr.patient_table_position[0];
imarray.headers_(n, s, loc).patient_table_position[1] = acqhdr.patient_table_position[1];
imarray.headers_(n, s, loc).patient_table_position[2] = acqhdr.patient_table_position[2];
imarray.headers_(n, s, loc).data_type = ISMRMRD::ISMRMRD_CXFLOAT;
imarray.headers_(n, s, loc).image_index = ++image_counter_;
imarray.headers_(n, s, loc).image_series_index = 1;
if (perform_pics.value()) // do pics
{
std::string traj_filename = output_folder.value() + "Traj";
std::string DCF_filename = output_folder.value() + "DCF";
std::string data_filename = output_folder.value() + "coil_data";
// Export Trajectory
auto &traj_unscaled = dbuff.trajectory_.value();
hoNDArray<complex_float_t> traj_scaled = hoNDArray<complex_float_t>(traj_unscaled.get_size(0), traj_unscaled.get_size(1), traj_unscaled.get_size(2), traj_unscaled.get_size(3));
auto mat_sz = header.encoding[0].reconSpace.matrixSize;
for (uint32_t i = 0; i < E0 * E1 * E2 * traj_scaled.get_size(0); i += 3)
{
traj_scaled[i].real(traj_unscaled[i] * static_cast<float>(mat_sz.x));
traj_scaled[i + 1].real(traj_unscaled[i + 1] * static_cast<float>(mat_sz.y));
traj_scaled[i + 2].real(traj_unscaled[i + 2] * static_cast<float>(mat_sz.z));
}
traj_scaled.reshape(3, E0, E1 * E2);
CFL_IO2::hoNDArray2CFL2<complex_float_t>(traj_filename, traj_scaled);
// export density compensation
auto &dcf = dbuff.density_.value();
hoNDArray<complex_float_t> h0ND_dcf = hoNDArray<complex_float_t>(1, E0, E1 * E2);
for (uint32_t i = 0; i < E0 * E1 * E2; i++)
{
h0ND_dcf[i].real(std::sqrt(dcf[i]));
}
CFL_IO2::hoNDArray2CFL2<complex_float_t>(DCF_filename, h0ND_dcf);
// export Coildata
std::vector<size_t> chunk_dims2(5);
chunk_dims2[0] = 1;
chunk_dims2[1] = dbuff.data_.get_size(0);
chunk_dims2[2] = dbuff.data_.get_size(1) * dbuff.data_.get_size(2);
chunk_dims2[3] = CHA;
chunk_dims2[4] = 1;
hoNDArray<std::complex<float>> chunk2 = hoNDArray<std::complex<float>>(chunk_dims2, &dbuff.data_(0, 0, 0, 0, n, s, loc));
CFL_IO2::hoNDArray2CFL2<complex_float_t>(data_filename, chunk2);
// do bart pics
std::string sens_filename = output_folder.value() + "sensCFL"; // from RefRecoGadget
std::string img_filename = output_folder.value() + "pics_reco";
// bart pics -i 10 -l2 -r 1e-2 -p DCF -t Traj coil_data sensCFL out1
std::stringstream ss;
ss.clear();
ss << "bart " << pics_settings.value() << " ";
ss << "-p"
<< " " << DCF_filename << " ";
ss << "-t"
<< " " << traj_filename << " ";
ss << data_filename << " " << sens_filename << " " << img_filename;
std::string bart_cmd = ss.str();
GDEBUG_STREAM("Executing : " << bart_cmd.c_str());
std::system(bart_cmd.c_str());
auto pics_im = CFL_IO2::CFL2hoNDARRAY2<std::complex<float>>(img_filename);
hoNDArray<std::complex<float>> output = hoNDArray<std::complex<float>>(img_dims, &imarray.data_(0, 0, 0, 0, n, s, loc));
uint32_t nVoxels = (img_dims[2] > 0) ? img_dims[0] * img_dims[1] * img_dims[2] : img_dims[0] * img_dims[1];
std::copy(pics_im.begin(), pics_im.begin() + nVoxels, output.begin());
coil_images = permute(coil_images, {1, 0, 2});
output*=pics_scale_factor.value(); //auto scaling gadget sucks!
}
else // do gridding and coil combination
{
// Grab a wrapper around the relevant chunk of data [E0,E1,E2,CHA] for this loc, n, and s
// Each chunk will be [E0,E1,E2,CHA] big
std::vector<size_t> chunk_dims(4);
chunk_dims[0] = dbuff.data_.get_size(0);
chunk_dims[1] = dbuff.data_.get_size(1);
chunk_dims[2] = dbuff.data_.get_size(2);
chunk_dims[3] = CHA;
hoNDArray<std::complex<float>> chunk = hoNDArray<std::complex<float>>(chunk_dims, &dbuff.data_(0, 0, 0, 0, n, s, loc));
chunk.reshape(E0 * E1, E2 * CHA);
hoNDArray<std::complex<float>> coil_images(matrix_size.x, matrix_size.y, E2 * CHA);
nufft.compute(chunk, coil_images, &dcw, NFFT_comp_mode::BACKWARDS_NC2C);
coil_images.reshape(matrix_size.x, matrix_size.y, E2, CHA);
if (E2 > 1) // do 1D-fft along E2 if 3D acquistion
{
coil_images = permute(coil_images, {2, 0, 1, 3});
hoNDFFT<float>::instance()->ifft1c(coil_images);
coil_images = permute(coil_images, {1, 2, 0, 3});
}
// Square root of the sum of squares
// Each image will be [E0,E1,E2,1] big
hoNDArray<std::complex<float>> output = hoNDArray<std::complex<float>>(img_dims, &imarray.data_(0, 0, 0, 0, n, s, loc));
// Zero out the output
clear(output);
if (use_calculated_csm.value() && it->ref_.has_value()) // use coil sens from RefRecoGadget()
{ // adpative coil combine
hoNDArray<complex_float_t> &csm = it->ref_.value().data_;
multiplyConj(coil_images, csm, coil_images);
// Add up
for (size_t c = 0; c < CHA; c++)
{
output += hoNDArray<std::complex<float>>(img_dims, &coil_images(0, 0, 0, c));
}
}
else // sum of squares
{
// // Compute d* d in place
multiplyConj(coil_images, coil_images, coil_images);
// Add up
for (size_t c = 0; c < CHA; c++)
{
output += hoNDArray<std::complex<float>>(img_dims, &coil_images(0, 0, 0, c));
}
// // Take the square root in place
sqrt_inplace(&output);
} // sum of squares
} // do pics else
}
}
}
// Pass the image array down the chain
if (this->next()->putq(cm1) < 0)
{
m1->release();
return GADGET_FAIL;
}
// use and acquistion header and do undersampling in trajectory?
} // reconbit iteration
if (this->next()->putq(m1) < 0)
{
GERROR_STREAM("Put IsmrmrdReconData to Q failed ... ");
return GADGET_FAIL;
}
return GADGET_OK;
} // process()
GADGET_FACTORY_DECLARE(SpiralGriddingGadget)
}