forked from instamatic-dev/instamatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_dm.py
More file actions
178 lines (146 loc) · 5.69 KB
/
Copy pathprocess_dm.py
File metadata and controls
178 lines (146 loc) · 5.69 KB
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
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
from PIL import Image
from skimage.exposure import rescale_intensity
from instamatic.processing.ImgConversionDM import ImgConversionDM as ImgConversion
from instamatic.tools import relativistic_wavelength
# Script to process cRED data collecting using the DigitalMicrograph script `insteaDMatic`
# https://github.com/instamatic-dev/InsteaDMatic
#
# To use:
# Run `python process_dm.py cred_log.txt`
#
# Where the first argument is the path to the cred_log.txt file. Assumes the data are stored
# in a subdirectory `tiff/*.tif` from where cred_log.txt is stored.
#
# Defaults to `cred_log.txt` in the current directory if left blank.
#
# If the first argument is given as `all`, the script will look for
# all `cred_log.txt` files in the subdirectories, and iterate over those.
def img_convert(credlog, tiff_path='tiff2', mrc_path='RED', smv_path='SMV'):
credlog = Path(credlog)
drc = credlog.parent
image_fns = list(drc.glob('tiff/*.tif'))
n = len(image_fns)
if n == 0:
print('No files found matching `tiff/*.tif`')
exit()
else:
print(n)
buffer = []
with open(credlog) as f:
for line in f:
if line.startswith('Data Collection Time'):
timestamp = line.split(':', 1)[-1].strip()
if line.startswith('Camera length (mm):'):
camera_length = float(line.split()[-1])
if line.startswith('Oscillation angle'):
osc_angle = float(line.split()[-1])
if line.startswith('High tension (kV):'):
high_tension = float(line.split()[-1])
if line.startswith('Starting angle'):
start_angle = float(line.split()[-1])
if line.startswith('Ending angle'):
end_angle = float(line.split()[-1])
if line.startswith('Rotation axis'):
rotation_axis = float(line.split()[-1])
if line.startswith('Acquisition time'):
acquisition_time = float(line.split()[-1])
if line.startswith('Exposure Time'):
exposure_time = float(line.split()[-1])
if line.startswith('Image pixelsize x/y (1/nm):'):
inp = line.split()
pixelsize = (float(inp[-2]), float(inp[-1]))
if line.startswith('Image physical pixelsize x/y (um):'):
inp = line.split()
physical_pixelsize = (float(inp[-2]), float(inp[-1]))
if line.startswith('Binsize:'):
binsize = float(line.split()[-1])
if line.startswith('Image resolution x/y (px):'):
inp = line.split()
resolution = (int(inp[-2]), int(inp[-1]))
if line.startswith('Camera:'):
camera = line.split()[-1]
if line.startswith('Resolution:'):
resolution = line.split()[-1]
wavelength = relativistic_wavelength(high_tension * 1000)
# convert from um to mm
physical_pixelsize = physical_pixelsize[0] / 1000
# convert from 1/nm to 1/angstrom
pixelsize = pixelsize[0] * 10
# rotation axis
# for themisZ/Oneview: -171.0; for 2100LaB6/Orius: 53.0; otherwise: 0.0
rotation_axis = np.radians(rotation_axis)
print('timestamp:', timestamp)
print('Wavelength:', wavelength)
print('Camera:', camera)
print('Resolution (px):', resolution)
print('TEM Camera length (mm):', camera_length)
print('Pixelsize (1/Angstrom):', pixelsize)
print('Physical pixelsize (um):', physical_pixelsize)
print('Starting angle (deg.):', start_angle)
print('Ending angle (deg.):', end_angle)
print('Oscillation angle (deg./frame):', osc_angle)
print('Acquisition time (s/frame):', acquisition_time)
print('Rotation axis (rad.):', rotation_axis)
# print("Binsize:", binsize)
def extract_image_number(s):
p = Path(s)
return int(p.stem.split('_')[-1])
for i, fn in enumerate(image_fns):
j = extract_image_number(fn)
img = np.array(Image.open(fn))
h = {'ImageGetTime': timestamp, 'ImageExposureTime': exposure_time}
buffer.append((j, img, h))
if img.dtype != np.uint16:
max_val = max(img.max() for _, img, _ in buffer)
min_val = min(img.min() for _, img, _ in buffer)
for item in buffer:
# cast to 16 bit uint16
item[1] = rescale_intensity(
item[1], out_range='uint16', in_range=(min_val, max_val)
)
img_conv = ImgConversion(
buffer=buffer,
osc_angle=osc_angle,
start_angle=start_angle,
end_angle=end_angle,
rotation_axis=rotation_axis,
acquisition_time=acquisition_time,
flatfield=None,
pixelsize=pixelsize,
physical_pixelsize=physical_pixelsize,
wavelength=wavelength,
)
if mrc_path:
mrc_path = drc / mrc_path
if smv_path:
smv_path = drc / smv_path
if tiff_path:
tiff_drc_name = tiff_path
tiff_path = drc / tiff_path
img_conv.threadpoolwriter(
tiff_path=tiff_path, mrc_path=mrc_path, smv_path=smv_path, workers=8
)
if mrc_path:
img_conv.write_ed3d(mrc_path)
if smv_path:
img_conv.write_xds_inp(smv_path)
# img_conv.to_dials(smv_path)
img_conv.write_pets_inp(path=drc, tiff_path=tiff_drc_name)
def main():
try:
credlog = sys.argv[1]
except IndexError:
credlog = 'cRED_log.txt'
if credlog == 'all':
fns = Path('.').glob('**/cRED_log.txt')
for fn in fns:
print(fn)
img_convert(fn)
else:
img_convert(credlog)
if __name__ == '__main__':
main()