-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_time.py
366 lines (273 loc) · 9.5 KB
/
measure_time.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Script to measure time for each algorithm
import os
import json
from typing import Tuple
from live_wdd.prepare_resize_data import setup_data
import subprocess
import sys
import os
path_current = os.path.abspath(os.getcwd())
sys.path.insert(1, '/Users/bangun/pyptychostem-master')
from STEM4D import WDD, Data4D
from live_wdd.live_wdd import prepare_livewdd
from live_wdd.wdd_udf import WDDUDF
from live_wdd.prepare_resize_data import setup_data
import time
import numpy as np
from libertem.io.dataset.base import DataSet
from libertem.api import Context
import click
from libertem.common import Shape
from perf_utils import timer
def run_pyptychostem_wdd(parfile_new):
"""
A function to run conventional Wigner Distribution Deconvolution
with pyptychostem
Parameters
----------
parfile_new
Path of datasets
Returns
-------
wdd
Instance from reconstruction contains phase, amplitude, etc
"""
# Create Dictionary
par_dictionary = {}
file = open(parfile_new)
for line in file:
if line.startswith('##'):
continue
split_line = line.rstrip().split('\t')
if len(split_line)!=2:
continue
key, value = split_line
par_dictionary[key] = value
# Load data
data_4D = Data4D(parfile_new)
data_4D.estimate_aperture_size()
# Dose
if int(par_dictionary.get('dose',-1)) >0:
print('Adding Dose..')
data_4D.apply_dose(int(par_dictionary.get('dose',-1)))
expansion_ratio = float(par_dictionary.get('CBED/BF',-1))
if expansion_ratio<1:
expansion_ratio = None
data_4D.truncate_ronchigram(expansion_ratio=expansion_ratio) # crops ronchigram to area of interest
# Spatial frequency
data_4D.apply_FT()
# Run wdd
wdd = WDD(data_4D)
wdd.run()
return wdd
def run_livewdd(ds: DataSet,
ctx: Context,
wiener_filter_compressed:np.ndarray,
coeff:Tuple,
scan_idx:np.ndarray,
row_exp:np.ndarray,
col_exp:np.ndarray,
complex_dtype:str):
"""
A function to run live Wigner Distribution Deconvolution
Parameters
----------
ds
DataSet from liberTEM
ctx
Context to run the UDF
scan_idx
Non zero index for overlapping region
wiener_filter_compressed
Wiener filter after dimensionality reduction for deonvolutoin process
row_exp
Fourier matrix applied on the row space
col_exp
Fourier matrix applied on the column space
coeff
Matrix for dimensionality reduction
complex_dtype
Number of floating points
Returns
-------
result
Dictionary results contains phase reconstruction and computation time
"""
# Run live wdd
live_wdd = ctx.run_udf(dataset=ds, roi = None,
udf= WDDUDF(wiener_filter_compressed,
scan_idx, coeff,
row_exp, col_exp, complex_dtype))
otf = live_wdd['reconstructed']
return otf
def compute_pyptycho(MC, parfile_new):
"""
Calculate pyptychoSTEM
Parameters
----------
parfile_new
Path to the datasets
MC
Number of trials
Returns
-------
result
Dictionary results contains phase reconstruction and computation time
"""
# Run PyPtychoSTEM
print('Run PyPtychoSTEM....')
# Pre-allocation time
time_all = {'perf': [],
'thread': [],
'process': []}
conv_wdd_mc = []
for it in range(MC):
print('Trials :', it)
# Start calculate time
start_perf = time.perf_counter()
start_thread = time.thread_time()
start_process = time.process_time()
# Run PyptychoSTEM
conv_wdd = run_pyptychostem_wdd(parfile_new)
conv_wdd_mc.append(conv_wdd.phase.tolist())
# Stop calculate time
end_perf = time.perf_counter()
end_thread = time.thread_time()
end_process = time.process_time()
# Calculate time
time_all['perf'].append(end_perf - start_perf)
time_all['thread'].append(end_thread - start_thread)
time_all['process'].append(end_process - start_process)
result = {'recon_phase':conv_wdd_mc,
'run_time': time_all}
return result
def compute_liveproc(path:str,
path_json:str,
MC:int):
"""
Calculate live Wigner Distribution Deconvolution
Parameters
----------
path
Path to the datasets
path_json
Path to the parameters datasets
MC
Number of trials
Returns
-------
result
Dictionary results contains phase reconstruction and computation time
"""
# Create context
ctx = Context()
# Pre-allocation time
time_all = {'perf': [],
'thread': [],
'process': []}
otf_mc = []
for it in range(MC):
print('Trials :', it)
# Start calculate time
start_perf = time.perf_counter()
start_thread = time.thread_time()
start_process = time.process_time()
# Prepare data for live processing
f = open(path_json)
# returns JSON object as
# a dictionary
par_dictionary = json.load(f)
dim = par_dictionary['dim']
order = par_dictionary['order']
complex_dtype = np.dtype(par_dictionary['complex_dtype'])
ds_shape = Shape(dim, sig_dims=2)
ds = ctx.load("npy", path=path, nav_shape=ds_shape.nav, sig_shape=ds_shape.sig)
ds.set_num_cores(4*18)
acc = float(par_dictionary['voltage'])# in kV
scan_real = float(par_dictionary['stepsize'])*1e-1 # in nm
semiconv=float(par_dictionary['aperture'])*1e3 # In mrad
print('Pre Computed for Live Processing...')
scan_idx, wiener_filter_compressed, row_exp, col_exp,coeff = prepare_livewdd(ds_shape, acc, scan_real,
semiconv, par_dictionary['rad'],
par_dictionary['com'], order,
complex_dtype,
6.0)
# Run Live WDD
print('Run Live Processing....')
with timer("Time for live processing"):
otf = run_livewdd(ds, ctx,
wiener_filter_compressed,
coeff,scan_idx,
row_exp,col_exp,
complex_dtype)
otf_mc.append(np.angle(otf).tolist())
# Stop calculate time
end_perf = time.perf_counter()
end_thread = time.thread_time()
end_process = time.process_time()
# Calculate time
time_all['perf'].append(end_perf - start_perf)
time_all['thread'].append(end_thread - start_thread)
time_all['process'].append(end_process - start_process)
result = {'recon_phase':otf_mc,
'run_time': time_all}
ctx.close()
return result
def main(solver:str,
MC:int,
parfile_new:str,
path_data:str,
path_json:str):
"""
Run the solver independently between pyptychostem and livewdd
Parameters
----------
solver
Choose between pyptychostem and livewdd solver
MC
Number of trials for Monte Carlo
parfile_new
Path for new dimension data for pyptychostem
path_data
Path for dimension ata for livewdd
path_json
Path for parameters of datasets
Returns
-------
result
Dictionary of result contains computation time and phase reconstruction
"""
if solver == 'pyptychostem':
result = compute_pyptycho(MC, parfile_new)
elif solver == 'livewdd':
result = compute_liveproc(path_data, path_json, MC)
else:
raise RuntimeError('No Solver!')
return result
if __name__ == '__main__':
# Evaluate scan or detector
type_increase = 'scan'
# Here we only evaluate time, to evaluate memory allocation
# use measure_memory.py
type_eval = 'Time'
MC, path_store, list_dim, set_scan_det = setup_data(type_increase, type_eval)
# Create directory
os.makedirs(path_store, exist_ok = True)
# Choose Solver
solver = 'pyptychostem' #livewdd, pyptychostem
formt = '.json'
file_name = os.path.join(path_store, solver + formt)
# Path file, using graphene form pyptychostem
parfile ='/Users/bangun/pyptychostem-master/parameters.txt'
total_result = []
for idx in range(len(list_dim)):
print('Processing dimension ', str(list_dim[idx]))
path_data, path_json = set_scan_det(list_dim[idx],parfile)
# Load Data after resize and run reconstruction
parfile_new = os.path.join(path_current,'LiveWDD_Data/parameters_new.txt')
# Run algorithm
result = main(solver, MC, parfile_new, path_data, path_json)
total_result.append({'dimension': list_dim[idx],
'result': result})
with open(file_name, 'w') as f:
json.dump(total_result, f)