Skip to content

Commit 64da560

Browse files
author
fleishmang
committed
added local affine interpolation
1 parent 7147020 commit 64da560

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

bigstream/affine.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def ransac_affine(
1717
verbose=True,
1818
fix_spots=None,
1919
mov_spots=None,
20+
default=np.eye(4),
2021
**kwargs,
2122
):
2223
"""
@@ -32,6 +33,9 @@ def ransac_affine(
3233
num_sigma=min(max_radius-min_radius, num_sigma_max),
3334
threshold=0, exclude_border=cc_radius,
3435
)
36+
if fix_spots.shape[0] < 50:
37+
print('Fewer than 50 spots found in fixed image, returning default')
38+
return default
3539
if verbose:
3640
ns = fix_spots.shape[0]
3741
print(f'FIXED image: found {ns} key points')
@@ -42,6 +46,9 @@ def ransac_affine(
4246
num_sigma=min(max_radius-min_radius, num_sigma_max),
4347
threshold=0, exclude_border=cc_radius,
4448
)
49+
if mov_spots.shape[0] < 50:
50+
print('Fewer than 50 spots found in moving image, returning default')
51+
return default
4552
if verbose:
4653
ns = mov_spots.shape[0]
4754
print(f'MOVING image: found {ns} key points')
@@ -82,14 +89,6 @@ def ransac_affine(
8289
)
8390

8491

85-
def interpolate_affines(affines):
86-
"""
87-
"""
88-
89-
# TODO: replace identity matrices
90-
return affines
91-
92-
9392
def prepare_piecewise_ransac_affine(
9493
fix, mov,
9594
fix_spacing, mov_spacing,
@@ -143,3 +142,61 @@ def wrapped_ransac_affine(x, y, block_info=None):
143142
chunks=[1, 1, 1, 4, 4],
144143
)
145144

145+
146+
def interpolate_affines(affines):
147+
"""
148+
"""
149+
150+
# get block grid
151+
block_grid = affines.shape[:3]
152+
153+
# construct an all identities matrix for comparison
154+
all_identities = np.empty_like(affines)
155+
for i in range(np.prod(block_grid)):
156+
idx = np.unravel_index(i, block_grid)
157+
all_identities[idx] = np.eye(4)
158+
159+
# if affines are all identity, just return
160+
if np.all(affines == all_identities):
161+
return affines
162+
163+
# process continues until there are no identity matrices left
164+
new_affines = np.copy(affines)
165+
identities = True
166+
while identities:
167+
identities = False
168+
169+
# loop over all affine matrices
170+
for i in range(np.prod(block_grid)):
171+
idx = np.unravel_index(i, block_grid)
172+
173+
# if an identity matrix is found
174+
if np.all(new_affines[idx] == np.eye(4)):
175+
identities = True
176+
trans, denom = np.array([0, 0, 0]), 0
177+
178+
# average translations from 6 connected neighborhood
179+
for ax in range(3):
180+
if idx[ax] > 0:
181+
neighbor = tuple(
182+
x-1 if j == ax else x for j, x in enumerate(idx)
183+
)
184+
neighbor_trans = new_affines[neighbor][:3, -1]
185+
if not np.all(neighbor_trans == 0):
186+
trans = trans + neighbor_trans
187+
denom += 1
188+
if idx[ax] < block_grid[ax]-1:
189+
neighbor = tuple(
190+
x+1 if j == ax else x for j, x in enumerate(idx)
191+
)
192+
neighbor_trans = new_affines[neighbor][:3, -1]
193+
if not np.all(neighbor_trans == 0):
194+
trans = trans + neighbor_trans
195+
denom += 1
196+
197+
# normalize then update matrix
198+
if denom > 0: trans /= denom
199+
new_affines[idx][:3, -1] = trans
200+
201+
return new_affines
202+

0 commit comments

Comments
 (0)