-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdem2.py
133 lines (121 loc) · 4.29 KB
/
dem2.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
import glob
import mmap
import multiprocessing
import numpy as np
import os
import stitch.glb as glb
import stitch.Rigid as st
import stitch.Wobbly as stw
import sys
me = "dem.py"
# '/media/user/Daten1/ADf_1.2.HC_hFTAA_SMA-Cy3_Pdxl-647/'
di = sys.argv[1]
globs = ('*640*.raw', '*488*.raw', '*561*.raw')
ou = os.path.split(os.path.realpath(di))[-1]
sx = sy = sz = 8
def open(path):
a = np.memmap(path, dtype, 'r+', 0, (nx, ny, nz),
order='F')[::sx, ::sy, ::sz]
a.setflags(write=False)
return a
def tile(g):
g = os.path.join(di, g)
return tile0(glob.glob(g))
def tile0(path):
assert path
lst = []
for p in path:
for e in os.path.basename(p).split('_'):
if len(e) > 0 and e[0] == 'X':
x = int(e[1:])
if len(e) > 0 and e[0] == 'Y':
y = int(e[1:])
lst.append(((x, y), p))
lst.sort(reverse=True)
tile_positions, path = zip(*lst)
x, y = zip(*tile_positions)
tx = len(set(x))
ty = len(set(y))
return path, tx, ty
verbose = True
dtype = np.dtype("<u2")
nx, ny, nz = 2048, 2048, 4299
path, tx, ty = tile(globs[0])
processes = (tx - 1) * ty + tx * (ty - 1)
sys.stderr.write("%s: processes = %s\n" % (me, processes))
glb.SRC[:] = (open(e) for e in path)
kx, ky, kz = glb.SRC[0].shape
ox = 434 // sx
oy = 425 // sy
tile_positions = []
positions = []
pairs = []
for x in range(tx):
for y in range(ty):
tile_positions.append((x, y))
positions.append((x * (kx - ox), y * (ky - oy), 0))
i = x * ty + y
if x + 1 < tx:
pairs.append((i, (x + 1) * ty + y))
if y + 1 < ty:
pairs.append((i, x * ty + y + 1))
shifts, qualities = st.align((kx, ky, kz),
pairs,
positions,
tile_positions,
depth=[434 // sx, 425 // sy, None],
max_shifts=[(-80 // sx, 80 // sx),
(-80 // sy, 80 // sy),
(-120 // sz, 120 // sz)],
clip=25000,
processes=processes,
verbose=verbose)
positions = st.place(pairs, positions, shifts)
displacements, qualities, status = stw.align(
(kx, ky, kz),
pairs,
positions,
max_shifts=((-20 // sx, 20 // sx), (-20 // sy, 20 // sy)),
prepare=True,
find_shifts=dict(cutoff=3 * np.sqrt(2) / sx),
processes=processes,
verbose=verbose)
positions_new, components = stw.place0((kx, ky, kz),
pairs,
positions,
displacements,
qualities,
status,
smooth=dict(method='window',
window='hamming',
window_length=100 // sx,
binary=None),
min_quality=-np.inf,
processes=processes,
verbose=verbose)
wobble, status = stw.place1((kx, ky, kz),
positions,
positions_new,
components,
smooth=dict(method='window',
window='bartlett',
window_length=20 // sx,
binary=10),
processes=processes,
verbose=verbose)
ux, uy, uz = stw.shape_wobbly((kx, ky, kz), positions, wobble)
if not os.path.exists(ou):
os.makedirs(ou)
for i, g in enumerate(globs):
path = tile(g)[0]
output = os.path.join(ou, "%dx%dx%dle.%d.raw" % (ux, uy, uz, i))
sink = np.memmap(output, dtype, 'w+', 0, (ux, uy, uz), order='F')
glb.SINK[:] = [sink]
glb.SRC[:] = (open(e) for e in path)
stw.stitch((kx, ky, kz),
positions,
wobble,
status,
processes,
verbose=verbose)
sys.stderr.write("%s\n" % output)