-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_add_tomograms.py
90 lines (73 loc) · 2.74 KB
/
3_add_tomograms.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
import os
from glob import glob
import imageio
import h5py
import mobie
from elf.transformation import bdv_to_native, compute_affine_matrix, native_to_bdv
ROOT = '/g/schwab/Kimberly/From_Giulia'
def scale_affine(affine):
scale = compute_affine_matrix(scale=3*(1e3,))
trafo = bdv_to_native(affine)
trafo = trafo @ scale
return native_to_bdv(trafo)
def load_affines():
affines = {}
trafo_file = os.path.join(ROOT, 'tomogram_affines.txt')
tomo_name = None
with open(trafo_file, 'r') as f:
for line in f:
line = line.rstrip('\n')
if len(line) == 5:
tomo_name = line
elif len(line) > 10:
affine = line.split()
affine = list(map(float, affine))
affine = scale_affine(affine)
affines[tomo_name] = affine
else:
tomo_name = None
return affines
def add_tomograms():
tomo_paths = glob(os.path.join(ROOT, '*_lm.*')) + glob(os.path.join(ROOT, '*_hm.*'))
affines = load_affines()
chunks = (32, 128, 128)
scale_factors = [[1, 2, 2],
[1, 2, 2],
[1, 2, 2],
[1, 2, 2],
[2, 2, 2]]
for tomo in tomo_paths:
im = imageio.volread(tomo).astype('uint16')
settings = {
'contrastLimits': [im.min(), im.max()],
'blendingMode': 'sumOccluding'
}
tomo_name = os.path.splitext(os.path.split(tomo)[1])[0]
affine = affines[tomo_name]
# low mag tomograms are at 5nm, high mac ones at 1.25 nm
if 'lm' in tomo_name:
resolution = (0.005, 0.005, 0.005)
else:
resolution = (0.00125, 0.00125, 0.00125)
tmp_folder = f'tmp_{tomo_name}'
os.makedirs(tmp_folder, exist_ok=True)
tmp_path = os.path.join(tmp_folder, 'vol.h5')
with h5py.File(tmp_path, 'w') as f:
f.create_dataset('data', data=im, chunks=chunks)
tomo_name = f'em-tomogram-{tomo_name}'
mobie.add_image_data(input_path=tmp_path,
input_key='data',
root='./data',
dataset_name='yeast',
image_name=tomo_name,
resolution=resolution,
chunks=chunks,
scale_factors=scale_factors,
transformation=affine,
settings=settings,
tmp_folder=tmp_folder,
target='local',
max_jobs=16,
unit='micrometer')
if __name__ == '__main__':
add_tomograms()