-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmr_timings.py
More file actions
executable file
·258 lines (224 loc) · 6.81 KB
/
Copy pathmr_timings.py
File metadata and controls
executable file
·258 lines (224 loc) · 6.81 KB
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
# -*- coding: utf-8 -*-
"""
sirf.Gadgetron Views Test.
Compares the performance of two approaches that avoid copying data between C++ and Python:
- C++ algebra that is optimised by using templated loops
- Python algebra that directly accesses C++ data via NumPy Array API views
Usage:
mr_timings [--help | options]
Options:
-f <file>, --file=<file> raw data file
[default: simulated_MR_2D_cartesian.h5]
-p <path>, --path=<path> path to data files, defaults to data/examples/MR
subfolder of SIRF root folder
-t <tsts>, --tests=<tsts> number of tests [default: 1]
--non-interactive do not show plots
"""
import numpy
numpy_major = int(numpy.__version__[0])
if numpy_major < 2:
print('Using data views requires numpy version major not less than 2, exiting.')
exit()
import timeit
import importlib
from sirf.SIRF import copyto
from sirf.Utilities import examples_data_path, existing_filepath
__version__ = "0.1.0"
from docopt import docopt
args = docopt(__doc__, version=__version__)
#print(args)
# process command-line options
data_file = args['--file']
data_path = args['--path']
if data_path is None:
data_path = examples_data_path('MR')
input_file = existing_filepath(data_path, data_file)
ntests = int(args['--tests'])
# import engine module
mr = importlib.import_module('sirf.Gadgetron')
# obtain acquisitions and image data
acq = mr.AcquisitionData(input_file)
processed_data = mr.preprocess_acquisition_data(acq)
recon = mr.FullySampledReconstructor()
recon.set_input(processed_data)
print('---\n reconstructing...')
recon.process()
img = recon.get_output()
tests = 5
'''
tests:
x * 2
x + 2
x + y
x * y
x / y
'''
print('\n=== Comparing acquisitions algebra norms and timings...\n')
ax = acq
ay = ax + 0
az = ay + 0
vx = mr.AcquisitionDataView(ax)
vy = mr.AcquisitionDataView(ay)
vz = mr.AcquisitionDataView(az)
temp_t = numpy.zeros(tests)
view_t = numpy.zeros(tests)
for test in range(ntests):
start = timeit.default_timer()
ay = 2*ax
elapsed = timeit.default_timer() - start
temp_t[0] += elapsed
norm_y = ay.norm()
start = timeit.default_timer()
#vy.copy(vx)
copyto(vy, vx)
vy *= 2
elapsed = timeit.default_timer() - start
view_t[0] += elapsed
if test == 0:
print(f'norm(x * 2): {norm_y} {vy.norm()}')
start = timeit.default_timer()
ay = ax + 2
elapsed = timeit.default_timer() - start
temp_t[1] += elapsed
norm_y = ay.norm()
start = timeit.default_timer()
#vy.copy(vx)
copyto(vy, vx)
vy += 2
elapsed = timeit.default_timer() - start
view_t[1] += elapsed
if test == 0:
print(f'norm(x + 2): {norm_y} {vy.norm()}')
start = timeit.default_timer()
az = ax + ay
elapsed = timeit.default_timer() - start
temp_t[2] += elapsed
norm_z = az.norm()
start = timeit.default_timer()
#vz.copy(vx)
copyto(vz, vx)
vz += vy
elapsed = timeit.default_timer() - start
view_t[2] += elapsed
if test == 0:
print(f'norm(x + y): {norm_z} {vz.norm()}')
start = timeit.default_timer()
az = ax * ay
elapsed = timeit.default_timer() - start
temp_t[3] += elapsed
norm_z = az.norm()
start = timeit.default_timer()
#vz.copy(vx)
copyto(vz, vx)
vz *= vy
elapsed = timeit.default_timer() - start
view_t[3] += elapsed
if test == 0:
print(f'norm(x * y): {norm_z} {vz.norm()}')
ay += 1e-10
start = timeit.default_timer()
az = ax / ay
elapsed = timeit.default_timer() - start
temp_t[4] += elapsed
norm_z = az.norm()
start = timeit.default_timer()
#vz.copy(vx)
copyto(vz, vx)
vz /= vy
elapsed = timeit.default_timer() - start
view_t[4] += elapsed
if test == 0:
print(f'norm(x / y): {norm_z} {vz.norm()}')
view_t /= ntests
temp_t /= ntests
print('\ntest templates views')
print(f'x * 2 {temp_t[0]:.2e} {view_t[0]:.2e}')
print(f'x + 2 {temp_t[1]:.2e} {view_t[1]:.2e}')
print(f'x + y {temp_t[2]:.2e} {view_t[2]:.2e}')
print(f'x * y {temp_t[3]:.2e} {view_t[3]:.2e}')
print(f'x / y {temp_t[4]:.2e} {view_t[4]:.2e}')
print('\n=== Comparing images algebra norms and timings...\n')
ix = img
iy = ix + 0
iz = iy + 0
vx = mr.ImageDataView(ix)
vy = mr.ImageDataView(iy)
vz = mr.ImageDataView(iz)
temp_t = numpy.zeros(tests)
view_t = numpy.zeros(tests)
for test in range(ntests):
start = timeit.default_timer()
iy = 2*ix
elapsed = timeit.default_timer() - start
temp_t[0] += elapsed
norm_y = iy.norm()
start = timeit.default_timer()
#vy.copy(vx)
copyto(vy, vx)
vy *= 2
elapsed = timeit.default_timer() - start
view_t[0] += elapsed
if test == 0:
print(f'norm(x * 2): {norm_y} {vy.norm()}')
start = timeit.default_timer()
iy = ix + 2
elapsed = timeit.default_timer() - start
temp_t[1] += elapsed
norm_y = iy.norm()
start = timeit.default_timer()
#vy.copy(vx)
copyto(vy, vx)
vy += 2
elapsed = timeit.default_timer() - start
view_t[1] += elapsed
if test == 0:
print(f'norm(x + 2): {norm_y} {vy.norm()}')
start = timeit.default_timer()
iz = ix + iy
elapsed = timeit.default_timer() - start
temp_t[2] += elapsed
norm_z = iz.norm()
start = timeit.default_timer()
#vz.copy(vx)
copyto(vz, vx)
vz += vy
elapsed = timeit.default_timer() - start
view_t[2] += elapsed
if test == 0:
print(f'norm(x + y): {norm_z} {vz.norm()}')
start = timeit.default_timer()
iz = ix * iy
elapsed = timeit.default_timer() - start
temp_t[3] += elapsed
norm_z = iz.norm()
start = timeit.default_timer()
#vz.copy(vx)
copyto(vz, vx)
vz *= vy
elapsed = timeit.default_timer() - start
view_t[3] += elapsed
if test == 0:
print(f'norm(x * y): {norm_z} {vz.norm()}')
ay += 1e-10
start = timeit.default_timer()
iz = ix / iy
elapsed = timeit.default_timer() - start
temp_t[4] += elapsed
norm_z = iz.norm()
start = timeit.default_timer()
#vz.copy(vx)
copyto(vz, vx)
vz /= vy
elapsed = timeit.default_timer() - start
view_t[4] += elapsed
if test == 0:
print(f'norm(x / y): {norm_z} {vz.norm()}')
view_t /= ntests
temp_t /= ntests
print('\ntest templates views')
print(f'x * 2 {temp_t[0]:.2e} {view_t[0]:.2e}')
print(f'x + 2 {temp_t[1]:.2e} {view_t[1]:.2e}')
print(f'x + y {temp_t[2]:.2e} {view_t[2]:.2e}')
print(f'x * y {temp_t[3]:.2e} {view_t[3]:.2e}')
print(f'x / y {temp_t[4]:.2e} {view_t[4]:.2e}')
print('\n=== done with %s' % __file__)