Skip to content

Commit

Permalink
Add ability to load transformations from a file (#84)
Browse files Browse the repository at this point in the history
Partially addresses #17.

---------

Co-authored-by: Juan Nunez-Iglesias <[email protected]>
  • Loading branch information
andreasmarnold and jni authored Nov 17, 2023
1 parent 40370a1 commit e9bd67c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/affinder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

from .affinder import start_affinder
from .copy_tf import copy_affine
from .load_tf import load_affine
from .apply_tf import apply_affine
18 changes: 17 additions & 1 deletion src/affinder/_tests/test_affinder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from affinder import start_affinder, copy_affine, apply_affine
from affinder import start_affinder, copy_affine, apply_affine, load_affine
from affinder.affinder import AffineTransformChoices
from skimage import data, transform
import numpy as np
Expand Down Expand Up @@ -137,3 +137,19 @@ def test_apply_affine_nonimage():
widget = apply_affine()
with pytest.raises(NotImplementedError):
widget(ref_layer, mov_layer)


def test_load_affine(tmp_path):
affile = tmp_path / 'test_affine.txt'
affine = np.array([[2, 0, 5], [0, 2, 5], [0, 0, 1]])
np.savetxt(affile, affine, delimiter=',')

layer = napari.layers.Image(np.random.random((5, 5)))

widget = load_affine()
widget(layer, affile)

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

20 changes: 20 additions & 0 deletions src/affinder/load_tf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pathlib import Path

import numpy as np
from magicgui import magic_factory


@magic_factory(call_button='Load', affine={'mode': 'r'})
def load_affine(layer: 'napari.layers.Layer', affine: Path):
"""Load affine matrix from a file.
Parameters
----------
layer : napari.layers.Layer
Layer to load affine to.
affine : string or path
Path to the file containing affine matrix. Must be
comma-delimited txt.
"""
affine = np.loadtxt(affine, delimiter=',')
layer.affine = affine
7 changes: 6 additions & 1 deletion src/affinder/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ contributions:
- id: affinder.apply_affine
python_name: affinder:apply_affine
title: Apply affine...
- id: affinder.load_affine
python_name: affinder:load_affine
title: Load affine...
widgets:
- command: affinder.start_affinder
display_name: Start affinder
- command: affinder.copy_affine
display_name: Copy affine
- command: affinder.apply_affine
display_name: Apply affine
display_name: Apply affine
- command: affinder.load_affine
display_name: Load affine

0 comments on commit e9bd67c

Please sign in to comment.