Skip to content

Commit

Permalink
Use matrices directly, not napari Affine objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jni committed Dec 12, 2023
1 parent 389de3f commit c0a2b93
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/affinder/affinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,20 @@ def remove_pts_layers(viewer, layers):
viewer.layers.remove(layer)


def convert_affine_to_ndims(affine, target_ndims):
if affine.ndim == target_ndims:
return affine
new_affine = deepcopy(affine)
if affine.ndim < target_ndims:
converted_matrix = np.identity(target_ndims + 1)
start_i = target_ndims - affine.ndim
converted_matrix[start_i:, start_i:] = affine.affine_matrix
new_affine.affine_matrix = converted_matrix
elif affine.ndim > target_ndims:
new_affine.affine_matrix = (
affine.affine_matrix[affine.ndim - target_ndims:,
affine.ndim - target_ndims:]
)

return new_affine
def convert_affine_to_ndims(affine, target_ndim):
"""Either embed or slice an affine matrix to match the target ndims."""
affine = np.asarray(affine)
diff = affine.ndim - target_ndim
if diff == 0:
out = affine
if diff < 0:
# target is larger, so embed
out = np.identity(target_ndim + 1)
out[-diff:, -diff:] = affine
else: # diff > 0
out = affine[diff:, diff:]

return out


def convert_affine_matrix_to_ndims(matrix, target_ndims):
Expand Down

0 comments on commit c0a2b93

Please sign in to comment.