diff --git a/src/affinder/__init__.py b/src/affinder/__init__.py index ffaba85..f14c759 100644 --- a/src/affinder/__init__.py +++ b/src/affinder/__init__.py @@ -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 diff --git a/src/affinder/_tests/test_affinder.py b/src/affinder/_tests/test_affinder.py index 1258086..ef5f0c7 100644 --- a/src/affinder/_tests/test_affinder.py +++ b/src/affinder/_tests/test_affinder.py @@ -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 @@ -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 + ) + diff --git a/src/affinder/load_tf.py b/src/affinder/load_tf.py new file mode 100644 index 0000000..d85c488 --- /dev/null +++ b/src/affinder/load_tf.py @@ -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 diff --git a/src/affinder/napari.yaml b/src/affinder/napari.yaml index 91b50f6..16084a2 100644 --- a/src/affinder/napari.yaml +++ b/src/affinder/napari.yaml @@ -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 \ No newline at end of file + display_name: Apply affine + - command: affinder.load_affine + display_name: Load affine \ No newline at end of file