-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleProgramMRIFFT.py
252 lines (204 loc) · 8.62 KB
/
sampleProgramMRIFFT.py
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
# from Pillow import Image
import numpy
import pyfftw
import time
from MRI_FFT.OneD import Direct1d
from MRI_FFT.TwoD import Direct2d
from MRI_FFT.TwoD import OneDDecomp
from MRI_FFT.ThreeD import Direct3d
from MRI_FFT.ThreeD import TwoDDecomp
# create 2D MRI data
# -------------------------------------------
# Images:
# noface.jpg: 1024*1024
# noface_long.jpg: 1024*256
# noface_longmed.jpg: 1024*512
# noface_medium.jpg: 512*512
# noface_medsmall.jpg: 512*256
# noface_small.jpg: 256*256
#
print('Generating MRI Data')
# 1D data
data1d = numpy.random.rand(10).astype(numpy.complex64)
MRIdata1d = pyfftw.interfaces.numpy_fft.fft(data1d).astype('complex64')
# print(MRIdata1d.dtype)
# print(MRIdata1d.shape)
print('1D data generated')
# 2D data
# Convert to black and white
img = Image.open("noface_small.jpg").convert('L')
img.load()
# img.show()
a = numpy.array(img)
b = pyfftw.n_byte_align(a, 16)
MRIdata2d = pyfftw.interfaces.numpy_fft.fft2(b).astype('complex64')
# print(MRIdata2d.dtype)
# print(MRIdata2d.shape)
# # Generates a warning as complex data lost during conversion
# img = Image.fromarray(MRIdata.astype('uint8'))
# img.show()
print('2D data generated')
#3D data
img = Image.open("noface_small.jpg").convert('L')
img.load()
imgArray = numpy.array(img)
data3d = numpy.zeros((imgArray.shape[0], imgArray.shape[1], imgArray.shape[1]))
for i in range(0, MRIdata2d.shape[0]):
data3d[i, :, :] = imgArray
b = pyfftw.n_byte_align(data3d, 16, dtype='complex64')
MRIdata3d = pyfftw.interfaces.numpy_fft.fftn(b)
# img = Image.fromarray(MRIdata3d[3, :, :].astype('uint8'))
# img.show()
# print(MRIdata3d.dtype)
# print(MRIdata3d.shape)
print('All Data Generated')
# -------------------------------------------
def oneD_direct():
direct1dObject = Direct1d()
result = direct1dObject.ifft1D(MRIdata1d)
print('Original 1D data: ', data1d)
print('After fft & ifft: ', result)
# Most efficient when all data has been collected in advance
def twoD_direct():
direct2dObject = Direct2d(MRIdata2d.shape)
start = time.time()
# perform a timed 2d ifft
result = direct2dObject.ifft2D(MRIdata2d)
elapsed = time.time() - start
print("Direct 2D Calculation Time: ", elapsed, "seconds", "\n")
# img = Image.fromarray(result.astype('uint8'))
# img.show()
# Most efficient when calculations can be performed during the scan
# Performs a 2D ifft using 1D decomposition across the second axis
def twoD_oneddecomp1():
OneDDecompObject = OneDDecomp(MRIdata2d.shape, 1)
start = time.time()
# 1D iffts that can be completed during the scan: all but the last 1D array
# send the data one line at a time
for line in range(0, MRIdata2d.shape[1] - 1):
# perform a timed 2d ifft
OneDDecompObject.append1D(MRIdata2d[:, line])
# scan completed: Record the scan time, then perform the last ifft pass
# seperated from the for loop so that it can be timed
endOfScanTime = time.time()
line = MRIdata2d.shape[1] - 1
# Returns the 2D complex array
result = OneDDecompObject.append1D(MRIdata2d[:, line])
scanDuration = endOfScanTime - start
finalPassDuration = time.time() - endOfScanTime
calculationDuration = time.time() - start
print("2D Transform with 1D Decomposition, axis 1")
print("Minimum Scan Duration for maximum decomposition benefit: ", scanDuration, "seconds")
print("Final ifft Pass Duration: ", finalPassDuration, "seconds")
print("Total Calculation Time: ", calculationDuration, "seconds", "\n")
# img = Image.fromarray(result.astype('uint8'))
# img.show()
# Most efficient when calculations can be performed during the scan
# Performs a 2D ifft using 1D decomposition across the first axis
def twoD_oneddecomp0():
OneDDecompObject = OneDDecomp(MRIdata2d.shape, 0)
start = time.time()
# 1D iffts that can be completed during the scan: all but the last 1D array
# send the data one line at a time
for line in range(0, MRIdata2d.shape[0] - 1):
# perform a timed 2d ifft
OneDDecompObject.append1D(MRIdata2d[line, :])
# scan completed: Record the scan time, then perform the last ifft pass
# seperated from the for loop so that it can be timed
endOfScanTime = time.time()
line = MRIdata2d.shape[0] - 1
# Returns the 2D complex array
result = OneDDecompObject.append1D(MRIdata2d[line, :])
scanDuration = endOfScanTime - start
finalPassDuration = time.time() - endOfScanTime
calculationDuration = time.time() - start
print("2D Transform with 1D Decomposition, axis 0")
print("Minimum Scan Duration for maximum decomposition benefit: ", scanDuration, "seconds")
print("Final ifft Pass Duration: ", finalPassDuration, "seconds")
print("Total Calculation Time: ", calculationDuration, "seconds", "\n")
# img = Image.fromarray(result.astype('uint8'))
# img.show()
def threeD_direct():
direct3dObject = Direct3d(MRIdata3d.shape)
start = time.time()
# perform a timed 3d ifft
result = direct3dObject.ifft3D(MRIdata3d)
elapsed = time.time() - start
print("Direct 3D Calculation Time: ", elapsed, "seconds", "\n")
# img3d = Image.fromarray(result[3, :, :].astype('uint8'))
# img3d.show()
def threeD_twoddecomp0():
TwoDDecompObject = TwoDDecomp(MRIdata3d.shape, 0)
start = time.time()
# send the data one line at a time
for line in range(0, MRIdata3d.shape[0] - 1):
# perform a timed 2d ifft
TwoDDecompObject.append2D(MRIdata3d[line, :, :])
# scan completed: Record the scan time, then perform the last ifft pass
# seperated from the for loop so that it can be timed
endOfScanTime = time.time()
line = MRIdata3d.shape[0] - 1
# Returns the 2D complex array
result = TwoDDecompObject.append2D(MRIdata3d[line, :, :])
scanDuration = endOfScanTime - start
finalPassDuration = time.time() - endOfScanTime
calculationDuration = time.time() - start
print("3D Transform with 2D Decomposition, axis 0")
print("Minimum Scan Duration for maximum decomposition benefit: ", scanDuration, "seconds")
print("Final ifft Pass Duration: ", finalPassDuration, "seconds")
print("Total Calculation Time: ", calculationDuration, "seconds", "\n")
# img = Image.fromarray(result[line, :, :].astype('uint8'))
# img.show()
def threeD_twoddecomp1():
TwoDDecompObject = TwoDDecomp(MRIdata3d.shape, 1)
start = time.time()
# send the data one line at a time
for line in range(0, MRIdata3d.shape[1] - 1):
# perform a timed 2d ifft
TwoDDecompObject.append2D(MRIdata3d[:, line, :])
# scan completed: Record the scan time, then perform the last ifft pass
# seperated from the for loop so that it can be timed
endOfScanTime = time.time()
line = MRIdata3d.shape[1] - 1
# Returns the 2D complex array
result = TwoDDecompObject.append2D(MRIdata3d[:, line, :])
scanDuration = endOfScanTime - start
finalPassDuration = time.time() - endOfScanTime
calculationDuration = time.time() - start
print("3D Transform with 2D Decomposition, axis 1")
print("Minimum Scan Duration for maximum decomposition benefit: ", scanDuration, "seconds")
print("Final ifft Pass Duration: ", finalPassDuration, "seconds")
print("Total Calculation Time: ", calculationDuration, "seconds", "\n")
# img = Image.fromarray(result[line, :, :].astype('uint8'))
# img.show()
def threeD_twoddecomp2():
TwoDDecompObject = TwoDDecomp(MRIdata3d.shape, 2)
start = time.time()
# send the data one line at a time
for line in range(0, MRIdata3d.shape[2] - 1):
# perform a timed 2d ifft
TwoDDecompObject.append2D(MRIdata3d[:, :, line])
# scan completed: Record the scan time, then perform the last ifft pass
# seperated from the for loop so that it can be timed
endOfScanTime = time.time()
line = MRIdata3d.shape[2] - 1
# Returns the 2D complex array
result = TwoDDecompObject.append2D(MRIdata3d[:, :, line])
scanDuration = endOfScanTime - start
finalPassDuration = time.time() - endOfScanTime
calculationDuration = time.time() - start
print("3D Transform with 2D Decomposition, axis 2")
print("Minimum Scan Duration for maximum decomposition benefit: ", scanDuration, "seconds")
print("Final ifft Pass Duration: ", finalPassDuration, "seconds")
print("Total Calculation Time: ", calculationDuration, "seconds", "\n")
# img = Image.fromarray(result[line, :, :].astype('uint8'))
# img.show()
if __name__ == "__main__":
oneD_direct()
twoD_direct()
twoD_oneddecomp0()
twoD_oneddecomp1()
threeD_direct()
threeD_twoddecomp0()
threeD_twoddecomp1()
threeD_twoddecomp2()