From 4d55269d0e339851ce84bffb83873318a9b18394 Mon Sep 17 00:00:00 2001 From: Tony Tung Date: Thu, 4 Apr 2019 18:50:07 -0700 Subject: [PATCH] Move the registration tests next to their sources. 1. Group `_apply_transform` and `_learn_transform` into a `_registration` package. This is because there is common code to the two (basically `TransformsList`). 2. Move tests into each package. `test_registration.py` is broken up into multiple files, each tied to the package they reside in. 3. Ninja'd in the removal of `starfish/spots/test/__init__.py`. Not even sure why that's there. --- .../api/image/apply_transform/index.rst | 4 +- .../api/image/learn_transform/index.rst | 4 +- starfish/image/__init__.py | 4 +- .../test => image/_registration}/__init__.py | 0 .../_apply_transform/__init__.py | 0 .../_apply_transform/_base.py | 2 +- .../_apply_transform/test/__init__.py | 0 .../_apply_transform/test/test_warp.py | 44 +++++++++ .../_apply_transform/warp.py | 4 +- .../_learn_transform/__init__.py | 0 .../_learn_transform/_base.py | 2 +- .../_learn_transform/test/__init__.py | 0 .../_learn_transform/test/test_translation.py | 35 +++++++ .../_learn_transform/translation.py | 2 +- starfish/image/_registration/test/__init__.py | 0 .../test/test_transforms_list.py | 29 ++++++ .../transforms_list.py | 0 .../image/registration/test_registration.py | 97 ------------------- 18 files changed, 119 insertions(+), 108 deletions(-) rename starfish/{spots/test => image/_registration}/__init__.py (100%) rename starfish/image/{ => _registration}/_apply_transform/__init__.py (100%) rename starfish/image/{ => _registration}/_apply_transform/_base.py (95%) create mode 100644 starfish/image/_registration/_apply_transform/test/__init__.py create mode 100644 starfish/image/_registration/_apply_transform/test/test_warp.py rename starfish/image/{ => _registration}/_apply_transform/warp.py (95%) rename starfish/image/{ => _registration}/_learn_transform/__init__.py (100%) rename starfish/image/{ => _registration}/_learn_transform/_base.py (95%) create mode 100644 starfish/image/_registration/_learn_transform/test/__init__.py create mode 100644 starfish/image/_registration/_learn_transform/test/test_translation.py rename starfish/image/{ => _registration}/_learn_transform/translation.py (97%) create mode 100644 starfish/image/_registration/test/__init__.py create mode 100644 starfish/image/_registration/test/test_transforms_list.py rename starfish/image/{_learn_transform => _registration}/transforms_list.py (100%) delete mode 100644 starfish/test/image/registration/test_registration.py diff --git a/docs/source/api/image/apply_transform/index.rst b/docs/source/api/image/apply_transform/index.rst index 52ef0ed33..425f0590c 100644 --- a/docs/source/api/image/apply_transform/index.rst +++ b/docs/source/api/image/apply_transform/index.rst @@ -16,5 +16,5 @@ ApplyTransform can be imported using ``starfish.image.ApplyTransform``, the subc Warp ----- -.. autoclass:: starfish.image._apply_transform.warp.Warp - :members: \ No newline at end of file +.. autoclass:: starfish.image._registration._apply_transform.warp.Warp + :members: diff --git a/docs/source/api/image/learn_transform/index.rst b/docs/source/api/image/learn_transform/index.rst index c56f32b1f..2859dd8f9 100644 --- a/docs/source/api/image/learn_transform/index.rst +++ b/docs/source/api/image/learn_transform/index.rst @@ -16,5 +16,5 @@ LearnTransform can be imported using ``starfish.image.LearnTransform``, the subc Translation ------------ -.. autoclass:: starfish.image._learn_transform.translation.Translation - :members: \ No newline at end of file +.. autoclass:: starfish.image._registration._learn_transform.translation.Translation + :members: diff --git a/starfish/image/__init__.py b/starfish/image/__init__.py index 2a7aff09a..ed674c4c2 100644 --- a/starfish/image/__init__.py +++ b/starfish/image/__init__.py @@ -1,4 +1,4 @@ -from ._apply_transform import ApplyTransform from ._filter import Filter -from ._learn_transform import LearnTransform +from ._registration._apply_transform import ApplyTransform +from ._registration._learn_transform import LearnTransform from ._segmentation import Segmentation diff --git a/starfish/spots/test/__init__.py b/starfish/image/_registration/__init__.py similarity index 100% rename from starfish/spots/test/__init__.py rename to starfish/image/_registration/__init__.py diff --git a/starfish/image/_apply_transform/__init__.py b/starfish/image/_registration/_apply_transform/__init__.py similarity index 100% rename from starfish/image/_apply_transform/__init__.py rename to starfish/image/_registration/_apply_transform/__init__.py diff --git a/starfish/image/_apply_transform/_base.py b/starfish/image/_registration/_apply_transform/_base.py similarity index 95% rename from starfish/image/_apply_transform/_base.py rename to starfish/image/_registration/_apply_transform/_base.py index ef68f6317..d486ceaa5 100644 --- a/starfish/image/_apply_transform/_base.py +++ b/starfish/image/_registration/_apply_transform/_base.py @@ -1,7 +1,7 @@ from abc import abstractmethod from typing import Type -from starfish.image._learn_transform.transforms_list import TransformsList +from starfish.image._registration.transforms_list import TransformsList from starfish.imagestack.imagestack import ImageStack from starfish.pipeline import PipelineComponent from starfish.pipeline.algorithmbase import AlgorithmBase diff --git a/starfish/image/_registration/_apply_transform/test/__init__.py b/starfish/image/_registration/_apply_transform/test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/starfish/image/_registration/_apply_transform/test/test_warp.py b/starfish/image/_registration/_apply_transform/test/test_warp.py new file mode 100644 index 000000000..2add75309 --- /dev/null +++ b/starfish/image/_registration/_apply_transform/test/test_warp.py @@ -0,0 +1,44 @@ +import numpy as np + +from starfish import data +from starfish.image._registration._apply_transform.warp import Warp +from starfish.image._registration._learn_transform.translation import Translation +from starfish.types import Axes + + +expected_registered_values = np.array( + [[0.090654, 0.090593, 0.091554, 0.091661, 0.089967, 0.094072, 0.097398, + 0.099046, 0.100969, 0.112108], + [0.09926, 0.096925, 0.096269, 0.097002, 0.095842, 0.097704, 0.09984, + 0.101457, 0.105455, 0.106004], + [0.109834, 0.103609, 0.102693, 0.099931, 0.098222, 0.10074, 0.10251, + 0.103838, 0.106874, 0.113451], + [0.12369, 0.112428, 0.111482, 0.10631, 0.106203, 0.104753, 0.106706, + 0.105013, 0.10811, 0.11371], + [0.141802, 0.129946, 0.124285, 0.120928, 0.115908, 0.110735, 0.110735, + 0.107454, 0.109468, 0.109255], + [0.147326, 0.14464, 0.141436, 0.132845, 0.124071, 0.121828, 0.118074, + 0.112306, 0.109163, 0.109483], + [0.145296, 0.150362, 0.15082, 0.140337, 0.133806, 0.1299, 0.120592, + 0.114046, 0.115496, 0.111666], + [0.131121, 0.145525, 0.150011, 0.146609, 0.137407, 0.129198, 0.127306, + 0.118029, 0.116594, 0.111559], + [0.126482, 0.132372, 0.142596, 0.149538, 0.144701, 0.137469, 0.125353, + 0.121996, 0.117342, 0.118273], + [0.122866, 0.126543, 0.133669, 0.145418, 0.150515, 0.140139, 0.129992, + 0.124605, 0.120867, 0.121889]], dtype=np.float32) + + +def test_calculate_translation_transforms_and_apply(): + exp = data.ISS(use_test_data=True) + stack = exp.fov().get_image('primary') + reference_stack = exp.fov().get_image('dots') + translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) + # Calculate max_proj accrss + mp = stack.max_proj(Axes.CH, Axes.ZPLANE) + transform_list = translation.run(mp) + apply_transform = Warp() + warped_stack = apply_transform.run(stack=stack, transforms_list=transform_list) + assert np.allclose( + expected_registered_values, + warped_stack.xarray[2, 2, 0, 40:50, 40:50]) diff --git a/starfish/image/_apply_transform/warp.py b/starfish/image/_registration/_apply_transform/warp.py similarity index 95% rename from starfish/image/_apply_transform/warp.py rename to starfish/image/_registration/_apply_transform/warp.py index 52ac51010..db34ce2dd 100644 --- a/starfish/image/_apply_transform/warp.py +++ b/starfish/image/_registration/_apply_transform/warp.py @@ -8,8 +8,8 @@ from tqdm import tqdm from starfish.config import StarfishConfig -from starfish.image._apply_transform._base import ApplyTransformBase -from starfish.image._learn_transform.transforms_list import TransformsList +from starfish.image._registration._apply_transform._base import ApplyTransformBase +from starfish.image._registration.transforms_list import TransformsList from starfish.imagestack.imagestack import ImageStack from starfish.types import Axes from starfish.util import click diff --git a/starfish/image/_learn_transform/__init__.py b/starfish/image/_registration/_learn_transform/__init__.py similarity index 100% rename from starfish/image/_learn_transform/__init__.py rename to starfish/image/_registration/_learn_transform/__init__.py diff --git a/starfish/image/_learn_transform/_base.py b/starfish/image/_registration/_learn_transform/_base.py similarity index 95% rename from starfish/image/_learn_transform/_base.py rename to starfish/image/_registration/_learn_transform/_base.py index e302759c1..375799714 100644 --- a/starfish/image/_learn_transform/_base.py +++ b/starfish/image/_registration/_learn_transform/_base.py @@ -2,7 +2,7 @@ from typing import Type -from starfish.image._learn_transform.transforms_list import TransformsList +from starfish.image._registration.transforms_list import TransformsList from starfish.imagestack.imagestack import ImageStack from starfish.pipeline.algorithmbase import AlgorithmBase from starfish.pipeline.pipelinecomponent import PipelineComponent diff --git a/starfish/image/_registration/_learn_transform/test/__init__.py b/starfish/image/_registration/_learn_transform/test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/starfish/image/_registration/_learn_transform/test/test_translation.py b/starfish/image/_registration/_learn_transform/test/test_translation.py new file mode 100644 index 000000000..7549e89d0 --- /dev/null +++ b/starfish/image/_registration/_learn_transform/test/test_translation.py @@ -0,0 +1,35 @@ +import numpy as np + +from starfish import data +from starfish.image._registration._learn_transform.translation import Translation +from starfish.types import Axes + + +ISS_SHIFTS = [[-23, 6], [-22, 2], [-22, -3], [-15, -4]] + + +def test_learn_transforms_throws_error(): + exp = data.ISS(use_test_data=True) + stack = exp.fov().get_image('primary') + reference_stack = exp.fov().get_image('dots') + translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) + try: + translation.run(stack) + except ValueError as e: + # Assert value error is thrown when the stack is not max projected across all other axes. + assert e.args[0] == "Only axes: r can have a length > 1, please use the MaxProj filter." + + +def test_learn_transforms_translation(): + exp = data.ISS(use_test_data=True) + stack = exp.fov().get_image('primary') + reference_stack = exp.fov().get_image('dots') + translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) + # Calculate max_proj accrss CH/Z + stack = stack.max_proj(Axes.CH, Axes.ZPLANE) + transform_list = translation.run(stack) + # assert there's a transofrmation object for each round + assert len(transform_list.transforms) == stack.num_rounds + for (_, _, transform), shift in zip(transform_list.transforms, ISS_SHIFTS): + # assert that each TransformationObject has the correct translation shift + assert np.array_equal(transform.translation, shift) diff --git a/starfish/image/_learn_transform/translation.py b/starfish/image/_registration/_learn_transform/translation.py similarity index 97% rename from starfish/image/_learn_transform/translation.py rename to starfish/image/_registration/_learn_transform/translation.py index 79d0e72fd..2ce1c1db9 100644 --- a/starfish/image/_learn_transform/translation.py +++ b/starfish/image/_registration/_learn_transform/translation.py @@ -2,7 +2,7 @@ from skimage.feature import register_translation from skimage.transform._geometric import SimilarityTransform -from starfish.image._learn_transform.transforms_list import TransformsList +from starfish.image._registration.transforms_list import TransformsList from starfish.imagestack.imagestack import ImageStack from starfish.types import Axes, TransformType from starfish.util import click diff --git a/starfish/image/_registration/test/__init__.py b/starfish/image/_registration/test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/starfish/image/_registration/test/test_transforms_list.py b/starfish/image/_registration/test/test_transforms_list.py new file mode 100644 index 000000000..3bb3c0f6f --- /dev/null +++ b/starfish/image/_registration/test/test_transforms_list.py @@ -0,0 +1,29 @@ +import tempfile + +import numpy as np + +from starfish import data +from starfish.image._registration._learn_transform.translation import Translation +from starfish.image._registration.transforms_list import TransformsList +from starfish.types import Axes, TransformType + + +ISS_SHIFTS = [[-23, 6], [-22, 2], [-22, -3], [-15, -4]] + + +def test_export_import_transforms_object(): + exp = data.ISS(use_test_data=True) + stack = exp.fov().get_image('primary') + reference_stack = exp.fov().get_image('dots') + translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) + # Calculate max_proj accrss CH/Z + stack = stack.max_proj(Axes.CH, Axes.ZPLANE) + transform_list = translation.run(stack) + _, filename = tempfile.mkstemp() + # save to tempfile and import + transform_list.to_json(filename) + imported = TransformsList.from_json(filename) + for (_, transform_type, transform), shift in zip(imported.transforms, ISS_SHIFTS): + # assert that each TransformationObject has the correct translation shift + assert transform_type == TransformType.SIMILARITY + assert np.array_equal(transform.translation, shift) diff --git a/starfish/image/_learn_transform/transforms_list.py b/starfish/image/_registration/transforms_list.py similarity index 100% rename from starfish/image/_learn_transform/transforms_list.py rename to starfish/image/_registration/transforms_list.py diff --git a/starfish/test/image/registration/test_registration.py b/starfish/test/image/registration/test_registration.py deleted file mode 100644 index 832c7f5e6..000000000 --- a/starfish/test/image/registration/test_registration.py +++ /dev/null @@ -1,97 +0,0 @@ -import tempfile - -import numpy as np - -from starfish import data -from starfish.image._apply_transform.warp import Warp -from starfish.image._learn_transform.translation import TransformsList, Translation -from starfish.types import Axes, TransformType - - -ISS_SHIFTS = [[-23, 6], [-22, 2], [-22, -3], [-15, -4]] - -expected_registered_values = np.array( - [[0.090654, 0.090593, 0.091554, 0.091661, 0.089967, 0.094072, 0.097398, - 0.099046, 0.100969, 0.112108], - [0.09926, 0.096925, 0.096269, 0.097002, 0.095842, 0.097704, 0.09984, - 0.101457, 0.105455, 0.106004], - [0.109834, 0.103609, 0.102693, 0.099931, 0.098222, 0.10074, 0.10251, - 0.103838, 0.106874, 0.113451], - [0.12369, 0.112428, 0.111482, 0.10631, 0.106203, 0.104753, 0.106706, - 0.105013, 0.10811, 0.11371], - [0.141802, 0.129946, 0.124285, 0.120928, 0.115908, 0.110735, 0.110735, - 0.107454, 0.109468, 0.109255], - [0.147326, 0.14464, 0.141436, 0.132845, 0.124071, 0.121828, 0.118074, - 0.112306, 0.109163, 0.109483], - [0.145296, 0.150362, 0.15082, 0.140337, 0.133806, 0.1299, 0.120592, - 0.114046, 0.115496, 0.111666], - [0.131121, 0.145525, 0.150011, 0.146609, 0.137407, 0.129198, 0.127306, - 0.118029, 0.116594, 0.111559], - [0.126482, 0.132372, 0.142596, 0.149538, 0.144701, 0.137469, 0.125353, - 0.121996, 0.117342, 0.118273], - [0.122866, 0.126543, 0.133669, 0.145418, 0.150515, 0.140139, 0.129992, - 0.124605, 0.120867, 0.121889]], dtype=np.float32) - - -def test_learn_transforms_throws_error(): - exp = data.ISS(use_test_data=True) - stack = exp.fov().get_image('primary') - reference_stack = exp.fov().get_image('dots') - translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) - try: - translation.run(stack) - except ValueError as e: - # Assert value error is thrown when the stack is not max projected - # across all other axes. - assert e.args[0] == "Only axes: r can have a length > 1," \ - " please use the MaxProj filter." - - -def test_learn_transforms_translation(): - exp = data.ISS(use_test_data=True) - stack = exp.fov().get_image('primary') - reference_stack = exp.fov().get_image('dots') - translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) - # Calculate max_proj accrss CH/Z - stack = stack.max_proj(Axes.CH, Axes.ZPLANE) - transform_list = translation.run(stack) - # assert there's a transofrmation object for each round - assert len(transform_list.transforms) == stack.num_rounds - for (_, _, transform), shift in zip(transform_list.transforms, ISS_SHIFTS): - # assert that each TransformationObject has the correct translation - # shift - assert np.array_equal(transform.translation, shift) - - -def test_export_import_transforms_object(): - exp = data.ISS(use_test_data=True) - stack = exp.fov().get_image('primary') - reference_stack = exp.fov().get_image('dots') - translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) - # Calculate max_proj accrss CH/Z - stack = stack.max_proj(Axes.CH, Axes.ZPLANE) - transform_list = translation.run(stack) - _, filename = tempfile.mkstemp() - # save to tempfile and import - transform_list.to_json(filename) - imported = TransformsList.from_json(filename) - for (_, transform_type, transform), shift in zip(imported.transforms, ISS_SHIFTS): - # assert that each TransformationObject has the correct translation - # shift - assert transform_type == TransformType.SIMILARITY - assert np.array_equal(transform.translation, shift) - - -def test_calculate_translation_transforms_and_apply(): - exp = data.ISS(use_test_data=True) - stack = exp.fov().get_image('primary') - reference_stack = exp.fov().get_image('dots') - translation = Translation(reference_stack=reference_stack, axes=Axes.ROUND) - # Calculate max_proj accrss - mp = stack.max_proj(Axes.CH, Axes.ZPLANE) - transform_list = translation.run(mp) - apply_transform = Warp() - warped_stack = apply_transform.run(stack=stack, transforms_list=transform_list) - assert np.allclose( - expected_registered_values, - warped_stack.xarray[2, 2, 0, 40:50, 40:50])