Skip to content

Commit

Permalink
Merge branch 'main' into mixed-dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jni committed Dec 11, 2023
2 parents 308c005 + 927b50f commit 984c6df
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/google/yapf
rev: 'v0.40.2' # Use the sha / tag you want to point at
hooks:
- id: yapf
exclude: docs/conf.py
36 changes: 28 additions & 8 deletions src/affinder/_tests/test_affinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import zarr
import napari
import pytest
from pathlib import Path
from copy import copy
from scipy import ndimage as ndi

Expand Down Expand Up @@ -45,6 +46,12 @@
'./src/affinder/_tests/nuclei3D_labels.zarr', mode='r'
) #########

im0 = data.camera()
im1 = transform.rotate(im0[100:, 32:496], 60)
this_dir = Path(__file__).parent.absolute()
labels0 = zarr.open(this_dir / 'labels0.zarr', mode='r')
labels1 = zarr.open(this_dir / 'labels1.zarr', mode='r')


def make_vector_border(layer_pts):
vectors = np.zeros((layer_pts.shape[0], 2, layer_pts.shape[1]))
Expand Down Expand Up @@ -199,10 +206,12 @@ def test_2D_3D(make_napari_viewer, tmp_path, reference, moving):
# type Points of Vectors and not same dimensions as reference layer - so l1
# is a redundant layer that is no longer used as the real moving layer -
# this is why we use viewer.layers['layer1] instead of l1
expected_affine = np.array([[1.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 1.000000e+00, 2.890235e-17, 0.000000e+00],
[0.00000000e+00, -7.902889e-18, 1.000000e+00, 1.421085e-14],
[0.00000000e+00, 0.000000e+00, 0.000000e+00, 1.000000e+00]])
expected_affine = np.array(
[[1.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 1.000000e+00, 2.890235e-17, 0.000000e+00],
[0.00000000e+00, -7.902889e-18, 1.000000e+00, 1.421085e-14],
[0.00000000e+00, 0.000000e+00, 0.000000e+00, 1.000000e+00]]
)

np.testing.assert_allclose(
actual_affine, expected_affine, rtol=10, atol=1e-10
Expand Down Expand Up @@ -248,6 +257,20 @@ def test_apply_affine():
np.testing.assert_allclose(res_layer[0], ref_im)


def test_apply_affine_with_scale():
ref_im = np.random.random((5, 5))
mov_im = ndi.zoom(ref_im, 2, order=0)

ref_layer = napari.layers.Image(ref_im, scale=(0.2, 0.2))
mov_layer = napari.layers.Image(mov_im, scale=(0.4, 0.4))
mov_layer.affine = np.array([[0.25, 0, 0], [0, 0.25, 0], [0, 0, 1]])

widget = apply_affine()
res_layer = widget(ref_layer, mov_layer)

np.testing.assert_allclose(res_layer[0], ref_im)


def test_apply_affine_nonimage():
ref_im = np.random.random((5, 5))
mov_pts = np.random.random((5, 2))
Expand All @@ -271,7 +294,4 @@ def test_load_affine(tmp_path):
widget = load_affine()
widget(layer, affile)

np.testing.assert_allclose(
layer.affine, affine
)

np.testing.assert_allclose(layer.affine, affine)
21 changes: 19 additions & 2 deletions src/affinder/apply_tf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from magicgui import magic_factory
from skimage import transform
from napari.utils.transforms import CompositeAffine
import numpy as np


Expand Down Expand Up @@ -62,8 +63,24 @@ def apply_affine(
'Only image transforms supported at this point.'
)

reference_meta = CompositeAffine(
scale=reference_layer.scale,
translate=reference_layer.translate,
rotate=reference_layer.rotate,
shear=reference_layer.shear,
)
moving_meta = CompositeAffine(
scale=moving_layer.scale,
translate=moving_layer.translate,
rotate=moving_layer.rotate,
shear=moving_layer.shear,
)
# Find the transformation relative to the reference image
affine = np.linalg.inv(reference_layer.affine) @ moving_layer.affine
affine = (
np.linalg.inv(reference_meta)
@ np.linalg.inv(reference_layer.affine) @ moving_layer.affine
@ moving_meta
)

# Apply the transformation
transformed = _apply_affine_image(
Expand All @@ -74,7 +91,7 @@ def apply_affine(
layertype = 'image'
ref_metadata = {
n: getattr(reference_layer, n)
for n in ['scale', 'translate', 'rotate', 'shear']
for n in ['scale', 'translate', 'rotate', 'shear', 'affine']
}
mov_metadata = moving_layer.as_layer_data_tuple()[1]
name = {'name': moving_layer.name + '_transformed'}
Expand Down

0 comments on commit 984c6df

Please sign in to comment.