13
13
-o output_dir Output directory [default: output].
14
14
--sort-by=<metric> Sort peaks by certain metric [default: SNR].
15
15
--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.
16
18
"""
17
19
18
20
from docopt import docopt
19
21
import os
20
22
import h5py
21
23
from tqdm import tqdm # show a progress bar for loops
22
24
import numpy as np
23
- from math import sin , atan , sqrt
25
+ from math import sin , atan , sqrt , exp , cos
24
26
import glob
25
27
26
28
@@ -78,7 +80,9 @@ def load_geom(filename):
78
80
return None
79
81
80
82
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' ):
82
86
data = h5py .File (cxi_file ,'r' )
83
87
basename , ext = os .path .splitext (os .path .basename (cxi_file ))
84
88
@@ -87,9 +91,15 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
87
91
os .makedirs ('%s' % output_dir )
88
92
89
93
nb_events = data ['entry_1/data_1/data' ].shape [0 ]
94
+
90
95
print ('Processing %s with geometry %s' % (cxi_file , geom_file ))
96
+ peak_lists = []
97
+ dataset_names = []
91
98
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 )
93
103
nb_peaks = np .nonzero (data ['entry_1/result_1/peakTotalIntensity' ][event_id ][:])[0 ].size
94
104
peak_list = []
95
105
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):
108
118
c = 2.99792458E8 # light speed in meters per sec
109
119
lam = h * c / photon_energy
110
120
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
111
127
peak = Peak (assX , assY , res , SNR , total_intensity , encoder_value , photon_energy )
112
128
peak_list .append (peak )
113
129
@@ -116,7 +132,7 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
116
132
elif sort_by == 'total_intensity' :
117
133
peak_list .sort (key = lambda peak : peak .total_intensity , reverse = True )
118
134
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 )
120
136
else :
121
137
print ('Unimplemented sort strategy: %s' % sort_by )
122
138
return None
@@ -135,11 +151,28 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
135
151
new_peak_list .append (peak_list [peak_id ])
136
152
for peak_id in LP_ids :
137
153
new_peak_list .append (peak_list [peak_id ])
154
+ peak_lists .append (new_peak_list )
155
+
138
156
# 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
+
143
176
144
177
145
178
if __name__ == '__main__' :
@@ -151,15 +184,18 @@ def extract_peaks(cxi_file, output_dir, sort_by='SNR', res_threshold=4.5E-10):
151
184
output_dir = argv ['-o' ]
152
185
det_dist = float (argv ['-d' ])
153
186
pixel_size = float (argv ['-p' ])
187
+ output_format = argv ['--output-format' ]
188
+ scale = argv ['--scale' ]
154
189
# load geometry
155
190
geom_x , geom_y , geom_z = load_geom (geom_file )
156
191
157
192
# load cxi file
158
193
if os .path .isdir (cxi_file_or_dir ): # extract peaks from multiple cxi files
159
194
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 )
163
195
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