-
Notifications
You must be signed in to change notification settings - Fork 11
/
crystfel.py
270 lines (230 loc) · 8.49 KB
/
crystfel.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
import numpy as np
import pandas as pd
from reciprocalspaceship import DataSet
from reciprocalspaceship.utils import angle_between
def _parse_stream(filename: str) -> dict:
"""
Parses stream and returns all indexed peak positions
Parameters
----------
filename : stream filename
name of a .stream file
Returns
--------
(dict, np.ndarray)
"""
answ_crystals = {}
def contains_filename(s):
return s.startswith("Image filename")
def contains_event(s):
return s.startswith("Event")
def contains_serial_number(s):
return s.startswith("Image serial number")
def starts_chunk_peaks(s):
return s.startswith(" fs/px ss/px (1/d)/nm^-1 Intensity Panel")
def ends_chunk_peaks(s):
return s.startswith("End of peak list")
def starts_crystal_peaks(s):
return s.startswith(
" h k l I sigma(I) peak background fs/px ss/px panel"
)
def is_photon_energy(s):
return s.startswith("photon_energy_eV")
def is_astar(s):
return s.startswith("astar")
def is_bstar(s):
return s.startswith("bstar")
def is_cstar(s):
return s.startswith("cstar")
def ends_crystal_peaks(s):
return s.startswith("End of reflections")
def eV2Angstrom(e_eV):
return 12398.0 / e_eV
# add unit cell parameters parsing
with open(filename, "r") as stream:
is_unit_cell = False
get_cellparam = lambda s: float(s.split()[2])
rv_cell_param = None
a, b, c, al, be, ga = [
None
] * 6 # None's are needed since stream not always has all 6 parameters
for line in stream:
if "Begin unit cell" in line:
is_unit_cell = True
continue
elif is_unit_cell:
if line.startswith("a ="):
a = get_cellparam(line)
if line.startswith("b ="):
b = get_cellparam(line)
if line.startswith("c ="):
c = get_cellparam(line)
if line.startswith("al ="):
al = get_cellparam(line)
if line.startswith("be ="):
be = get_cellparam(line)
if line.startswith("ga ="):
ga = get_cellparam(line)
is_unit_cell = False # gamma is the last parameters
elif "End unit cell" in line:
rv_cell_param = np.array([a, b, c, al, be, ga])
break
with open(filename, "r") as stream:
is_chunk = False
is_crystal = False
current_filename = None
current_event = None # to handle non-event streams
current_serial_number = None
corrupted_chunk = False
crystal_peak_number = 0
crystal_idx = 0
for line in stream:
# analyzing what we have
if ends_chunk_peaks(line):
is_chunk = False
chunk_peak_number = 0
elif ends_crystal_peaks(line):
is_crystal = False
crystal_peak_number = 0
elif is_photon_energy(line):
photon_energy = float(line.split()[2])
elif is_astar(line):
astar = (
np.array(line.split()[2:5], dtype="float32") / 10.0
) # crystfel's notation uses nm-1
elif is_bstar(line):
bstar = (
np.array(line.split()[2:5], dtype="float32") / 10.0
) # crystfel's notation uses nm-1
elif is_cstar(line):
cstar = (
np.array(line.split()[2:5], dtype="float32") / 10.0
) # crystfel's notation uses nm-1
# since it's the last line needed to construct Ewald offset,
# we'll pre-compute the matrices here
A = np.array([astar, bstar, cstar]).T
lambda_inv = 1 / eV2Angstrom(photon_energy)
s0 = np.array([0, 0, lambda_inv]).T
elif is_crystal:
# example line:
# h k l I sigma(I) peak background fs/px ss/px panel
# -63 41 9 -41.31 57.45 195.00 170.86 731.0 1350.4 p0
crystal_peak_number += 1
h, k, l, I, sigmaI, peak, background, xdet, ydet, panel = [
i for i in line.split()
]
h, k, l = map(int, [h, k, l])
# calculate ewald offset and s1
hkl = np.array([h, k, l])
q = A @ hkl
s1 = q + s0
s1x, s1y, s1z = s1
s1_norm = np.linalg.norm(s1)
ewald_offset = s1_norm - lambda_inv
# project calculated s1 onto the ewald sphere
s1_obs = lambda_inv * s1 / s1_norm
# Compute the angular ewald offset
q_obs = s1_obs - s0
qangle = np.sign(ewald_offset) * angle_between(q, q_obs)
record = {
"H": h,
"K": k,
"L": l,
"I": float(I),
"SigI": float(sigmaI),
"BATCH": crystal_idx,
"s1x": s1x,
"s1y": s1y,
"s1z": s1z,
"ewald_offset": ewald_offset,
"angular_ewald_offset": qangle,
"XDET": float(xdet),
"YDET": float(ydet),
}
if current_event is not None:
name = (
current_filename,
current_event,
current_serial_number,
crystal_idx,
crystal_peak_number,
)
else:
name = (
current_filename,
current_serial_number,
crystal_idx,
crystal_peak_number,
)
answ_crystals[name] = record
# start analyzing where we are now
if corrupted_chunk:
if "Begin chunk" not in line:
continue
else:
is_crystal, is_chunk = False, False
corrupted_chunk = False
continue
if contains_filename(line):
current_filename = line.split()[-1]
elif contains_event(line):
current_event = line.split()[-1][2:]
elif contains_serial_number(line):
current_serial_number = line.split()[-1]
elif starts_chunk_peaks(line):
is_chunk = True
continue
elif starts_crystal_peaks(line):
crystal_idx += 1
is_crystal = True
continue
return answ_crystals, rv_cell_param
def read_crystfel(streamfile: str, spacegroup=None) -> DataSet:
"""
Initialize attributes and populate the DataSet object with data from a CrystFEL stream with indexed reflections.
This is the output format used by CrystFEL software when processing still diffraction data.
Parameters
----------
streamfile : str
name of a .stream file
spacegroup : gemmi.SpaceGroup or int or string (optional)
optionally set the spacegroup of the returned DataSet.
Returns
--------
rs.DataSet
"""
if not streamfile.endswith(".stream"):
raise ValueError("Stream file should end with .stream")
# read data from stream file
d, cell = _parse_stream(streamfile)
df = pd.DataFrame.from_records(list(d.values()))
# set mtztypes as in precognition.py
# hkl -- H
# I, sigmaI -- J, Q
# BATCH -- B
# s1{x,y,z} -- R
# ewald_offset -- R
mtzdtypes = {
"H": "H",
"K": "H",
"L": "H",
"I": "J",
"SigI": "Q",
"BATCH": "B",
"s1x": "R",
"s1y": "R",
"s1z": "R",
"ewald_offset": "R",
"angular_ewald_offset": "R",
"XDET": "R",
"YDET": "R",
}
dataset = DataSet(
spacegroup=spacegroup,
cell=cell,
merged=False, # CrystFEL stream is always unmerged
)
for k, v in df.items():
dataset[k] = v.astype(mtzdtypes[k])
dataset.set_index(["H", "K", "L"], inplace=True)
return dataset