-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #507 from DiamondLightSource/distortion-correction…
…-wrapper Add distortion correction wrapper
- Loading branch information
Showing
18 changed files
with
349 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Dict, Optional | ||
|
||
from mpi4py.MPI import Comm | ||
|
||
from httomo.method_wrappers.generic import GenericMethodWrapper | ||
from httomo.preview import PreviewConfig | ||
from httomo.runner.methods_repository_interface import MethodRepository | ||
|
||
|
||
class DistortionCorrectionWrapper(GenericMethodWrapper): | ||
""" | ||
Wrapper for distortion correction methods. | ||
""" | ||
|
||
@classmethod | ||
def should_select_this_class(cls, module_path: str, method_name: str) -> bool: | ||
return "distortion_correction" in method_name | ||
|
||
@classmethod | ||
def requires_preview(cls) -> bool: | ||
return True | ||
|
||
def __init__( | ||
self, | ||
method_repository: MethodRepository, | ||
module_path: str, | ||
method_name: str, | ||
comm: Comm, | ||
preview_config: PreviewConfig, | ||
save_result: Optional[bool] = None, | ||
output_mapping: Dict[str, str] = {}, | ||
**kwargs, | ||
): | ||
super().__init__( | ||
method_repository, | ||
module_path, | ||
method_name, | ||
comm, | ||
save_result, | ||
output_mapping, | ||
**kwargs, | ||
) | ||
self._update_params_from_preview(preview_config) | ||
|
||
def _update_params_from_preview(self, preview_config: PreviewConfig) -> None: | ||
""" | ||
Extract information from preview config to define the parameter values required for | ||
distortion correction methods, and update `self._config_params`. | ||
""" | ||
SHIFT_PARAM_NAME = "shift_xy" | ||
STEP_PARAM_NAME = "step_xy" | ||
shift_param_value = [ | ||
preview_config.detector_x.start, | ||
preview_config.detector_y.start, | ||
] | ||
step_param_value = [1, 1] | ||
self.append_config_params( | ||
{ | ||
SHIFT_PARAM_NAME: shift_param_value, | ||
STEP_PARAM_NAME: step_param_value, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import pytest | ||
from mpi4py import MPI | ||
from pytest_mock import MockerFixture | ||
|
||
from httomo.method_wrappers.distortion_correction import DistortionCorrectionWrapper | ||
from httomo.preview import PreviewConfig, PreviewDimConfig | ||
from tests.testing_utils import make_mock_repo | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method_name, expected_result", | ||
[("distortion_correction", True), ("other_method", False)], | ||
ids=["should-select", "shouldn't-select"], | ||
) | ||
def test_class_only_selected_for_methods_with_distortion_correction_in_name( | ||
method_name: str, expected_result: bool | ||
): | ||
assert ( | ||
DistortionCorrectionWrapper.should_select_this_class( | ||
"dummy.module.path", method_name | ||
) | ||
is expected_result | ||
) | ||
|
||
|
||
def test_requires_preview_is_true(): | ||
assert DistortionCorrectionWrapper.requires_preview() is True | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"preview_config", | ||
[ | ||
PreviewConfig( | ||
angles=PreviewDimConfig(start=0, stop=180), | ||
detector_y=PreviewDimConfig(start=0, stop=128), | ||
detector_x=PreviewDimConfig(start=0, stop=160), | ||
), | ||
PreviewConfig( | ||
angles=PreviewDimConfig(start=0, stop=180), | ||
detector_y=PreviewDimConfig(start=5, stop=123), | ||
detector_x=PreviewDimConfig(start=0, stop=160), | ||
), | ||
PreviewConfig( | ||
angles=PreviewDimConfig(start=0, stop=180), | ||
detector_y=PreviewDimConfig(start=0, stop=128), | ||
detector_x=PreviewDimConfig(start=5, stop=155), | ||
), | ||
], | ||
ids=["no_cropping", "crop_det_y_both_ends", "crop_det_x_both_ends"], | ||
) | ||
def test_sets_shiftxy_and_stepxy_params_correctly( | ||
preview_config: PreviewConfig, mocker: MockerFixture | ||
): | ||
MODULE_PATH = "dummy.module.path" | ||
METHOD_NAME = "distortion_correction_dummy" | ||
COMM = MPI.COMM_WORLD | ||
|
||
# Patch method function import that occurs when the wrapper object is created, to instead | ||
# import the below dummy method function | ||
class FakeModule: | ||
def distortion_correction_dummy(shift_xy, step_xy): # type: ignore | ||
return shift_xy + step_xy | ||
|
||
mocker.patch( | ||
"httomo.method_wrappers.generic.import_module", return_value=FakeModule | ||
) | ||
|
||
wrapper = DistortionCorrectionWrapper( | ||
method_repository=make_mock_repo(mocker), | ||
module_path=MODULE_PATH, | ||
method_name=METHOD_NAME, | ||
comm=COMM, | ||
preview_config=preview_config, | ||
) | ||
|
||
# Check that the shift and step parameter values are present in the wrapper's parameter | ||
# config, and have the expected values based on the preview config | ||
expected_shift_values = [ | ||
preview_config.detector_x.start, | ||
preview_config.detector_y.start, | ||
] | ||
expected_step_values = [1, 1] | ||
|
||
SHIFT_PARAM_NAME = "shift_xy" | ||
STEP_PARAM_NAME = "step_xy" | ||
assert SHIFT_PARAM_NAME in wrapper.config_params | ||
assert wrapper.config_params[SHIFT_PARAM_NAME] == expected_shift_values | ||
assert STEP_PARAM_NAME in wrapper.config_params | ||
assert wrapper.config_params[STEP_PARAM_NAME] == expected_step_values |
Oops, something went wrong.