Skip to content

Commit c33849d

Browse files
committed
add scaling option and h5 format support
1 parent 3c99640 commit c33849d

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

extract_peaks.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
-o output_dir Output directory [default: output].
1414
--sort-by=<metric> Sort peaks by certain metric [default: SNR].
1515
--res=<resolution> Resolution threshold for high priority peaks in angstrom [default: 4.5].
16+
--output-format=output_format Ourput format, multiple plain txt files or single hdf5 files [default: h5].
17+
--scale Special option for cxij6916 for filtered region scaling.
1618
"""
1719

1820
from docopt import docopt
1921
import os
2022
import h5py
2123
from tqdm import tqdm # show a progress bar for loops
2224
import numpy as np
23-
from math import sin, atan, sqrt
25+
from math import sin, atan, sqrt, exp, cos
2426
import glob
2527

2628

@@ -78,7 +80,9 @@ def load_geom(filename):
7880
return None
7981

8082

81-
def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
83+
def extract_peaks(cxi_file, output_dir, sort_by='SNR',
84+
res_threshold=4.5E-10, scale=False,
85+
output_format='h5'):
8286
data = h5py.File(cxi_file,'r')
8387
basename, ext = os.path.splitext(os.path.basename(cxi_file))
8488

@@ -87,9 +91,15 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
8791
os.makedirs('%s' % output_dir)
8892

8993
nb_events = data['entry_1/data_1/data'].shape[0]
94+
9095
print('Processing %s with geometry %s' % (cxi_file, geom_file))
96+
peak_lists = []
97+
dataset_names = []
9198
for event_id in tqdm(range(nb_events)):
92-
output = output_dir + '/' + basename + '-e%04d.txt' % event_id
99+
if output_format == 'txt':
100+
output = output_dir + '/' + basename + '-e%04d.txt' % event_id
101+
elif output_format == 'h5':
102+
dataset_names.append(basename + '-e%04d' % event_id)
93103
nb_peaks = np.nonzero(data['entry_1/result_1/peakTotalIntensity'][event_id][:])[0].size
94104
peak_list = []
95105
for peak_id in range(nb_peaks):
@@ -108,6 +118,12 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
108118
c = 2.99792458E8 # light speed in meters per sec
109119
lam = h * c / photon_energy
110120
res = lam / (sin(0.5 * atan(r * pixel_size / det_dist)) * 2)
121+
if scale:
122+
zn_thickness = 50E-6
123+
absorption_length = 41.73E-6
124+
theta = atan(r * pixel_size / det_dist) # scattering angle
125+
scaling = exp(zn_thickness / absorption_length / cos(theta))
126+
total_intensity *= scaling
111127
peak = Peak(assX, assY, res, SNR, total_intensity, encoder_value, photon_energy)
112128
peak_list.append(peak)
113129

@@ -116,7 +132,7 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
116132
elif sort_by == 'total_intensity':
117133
peak_list.sort(key=lambda peak: peak.total_intensity, reverse=True)
118134
elif sort_by == 'res':
119-
peak_list.sort(key=lambda peak: peak.total_intensity, reverse=False)
135+
peak_list.sort(key=lambda peak: peak.res, reverse=False)
120136
else:
121137
print('Unimplemented sort strategy: %s' % sort_by)
122138
return None
@@ -135,11 +151,28 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
135151
new_peak_list.append(peak_list[peak_id])
136152
for peak_id in LP_ids:
137153
new_peak_list.append(peak_list[peak_id])
154+
peak_lists.append(new_peak_list)
155+
138156
# write to file
139-
f = open(output, 'w')
140-
for peak in (new_peak_list):
141-
f.write('%.5e %.5e %.5e %.5e %.5e %.5e\n' %
142-
(peak.x, peak.y, peak.total_intensity, peak.SNR, peak.photon_energy, peak.encoder_value))
157+
if output_format == 'txt':
158+
f = open(output, 'w')
159+
for peak in new_peak_list:
160+
f.write('%.5e %.5e %.5e %.5e %.5e %.5e %5.2e\n' %
161+
(peak.x, peak.y, peak.total_intensity,
162+
peak.SNR, peak.photon_energy, peak.encoder_value,
163+
peak.res))
164+
if output_format == 'h5':
165+
output = output_dir + '/' + basename + '.h5'
166+
f = h5py.File(output)
167+
for i, peak_list in enumerate(peak_lists):
168+
peak_array = []
169+
for peak in peak_list:
170+
peak_array.append([peak.x, peak.y, peak.total_intensity,
171+
peak.SNR, peak.photon_energy, peak.encoder_value,
172+
peak.res])
173+
f.create_dataset(dataset_names[i], data=np.asarray(peak_array))
174+
f.close()
175+
143176

144177

145178
if __name__ == '__main__':
@@ -151,15 +184,18 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
151184
output_dir = argv['-o']
152185
det_dist = float(argv['-d'])
153186
pixel_size = float(argv['-p'])
187+
output_format = argv['--output-format']
188+
scale = argv['--scale']
154189
# load geometry
155190
geom_x, geom_y, geom_z = load_geom(geom_file)
156191

157192
# load cxi file
158193
if os.path.isdir(cxi_file_or_dir): # extract peaks from multiple cxi files
159194
cxi_files = glob.glob(cxi_file_or_dir + '/' + '*.cxi')
160-
for cxi_file in cxi_files:
161-
experiment, run_id, class_id = parse_peak_list_filename(cxi_file)
162-
extract_peaks(cxi_file, output_dir+'/'+class_id, sort_by=sort_by, res_threshold=res_threshold)
163195
else: # extract peaks from single cxi file
164-
cxi_file = cxi_file_or_dir
165-
extract_peaks(cxi_file_or_dir, output_dir+'/'+'c00', sort_by=sort_by, res_threshold=res_threshold)
196+
cxi_files = [cxi_file_or_dir, ]
197+
for cxi_file in cxi_files:
198+
experiment, run_id, class_id = parse_peak_list_filename(cxi_file)
199+
extract_peaks(cxi_file, output_dir+'/'+class_id,
200+
sort_by=sort_by, res_threshold=res_threshold,
201+
scale=scale, output_format=output_format)

0 commit comments

Comments
 (0)