forked from rs-station/reciprocalspaceship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refl.py
40 lines (32 loc) · 1.28 KB
/
refl.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
from reciprocalspaceship import DataSet
import msgpack
import numpy as np
# These are just functions which convert raw bytes from
# the msgpack to formats the DataSet constructor may parse
handlers = {
'double' : lambda x: np.frombuffer(x, dtype='float64'),
'bool' : lambda x: np.frombuffer(x, dtype='bool'),
'vec3<double>' : lambda x: np.frombuffer(x, dtype='float64').reshape((-1, 3)).tolist(),
'vec2<double>' : lambda x: np.frombuffer(x, dtype='float64').reshape((-1, 2)).tolist(),
'int6' : lambda x: np.frombuffer(x, dtype='int32').reshape((-1, 6)).tolist(),
'std::size_t' : lambda x: np.frombuffer(x, dtype='int64'),
'cctbx::miller::index<>' : lambda x: np.frombuffer(x, dtype='int32').reshape((-1, 3)).tolist(),
'int' : lambda x: np.frombuffer(x, dtype='int32'),
}
def read_refl(reflfile : str) -> DataSet:
"""
A parser for DIALS `.refl` files in the current (msgpack) format.
Parameters
----------
reflfile : str
DIALS reflection file `.refl`
Returns
-------
reciprocalspaceship.DataSet
"""
with open(reflfile, 'rb') as f:
refl_msgpack = msgpack.load(f)
data = refl_msgpack[2]['data']
columns = {k : handlers[v[0]](v[1][1]) for k,v in data.items()}
ds = DataSet(columns)
return ds