From d6f8ffd1d35f99824c967afde6a0d936b36e1144 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Tue, 16 Jul 2024 16:26:29 +0100 Subject: [PATCH 1/6] Revert "fixing error by remove type annotation" This reverts commit 3d12a6c473fec58eaa347f6185775b862c8a21b4. --- mantidimaging/gui/dialogs/cor_inspection/presenter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mantidimaging/gui/dialogs/cor_inspection/presenter.py b/mantidimaging/gui/dialogs/cor_inspection/presenter.py index 47e83b68c19..7b4f2120541 100644 --- a/mantidimaging/gui/dialogs/cor_inspection/presenter.py +++ b/mantidimaging/gui/dialogs/cor_inspection/presenter.py @@ -108,5 +108,5 @@ def optimal_rotation_centre(self) -> ScalarCoR: return ScalarCoR(self.model.centre_value) @property - def optimal_iterations(self): - return self.model.centre_value + def optimal_iterations(self) -> int: + return int(self.model.centre_value) From 26aea5844bc04884989a4dc9efbc75dfcc3b6ea1 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Tue, 16 Jul 2024 17:27:36 +0100 Subject: [PATCH 2/6] Added type annotations to utility Signed-off-by: ashmeigh --- mantidimaging/core/utility/close_enough_point.py | 2 +- .../core/utility/command_line_arguments.py | 4 ++-- mantidimaging/core/utility/cor_interpolate.py | 2 +- mantidimaging/core/utility/cuda_check.py | 4 ++-- mantidimaging/core/utility/data_containers.py | 4 ++-- mantidimaging/core/utility/execution_timer.py | 14 +++++++------- mantidimaging/core/utility/histogram.py | 4 ++-- mantidimaging/core/utility/imat_log_file_parser.py | 3 ++- mantidimaging/core/utility/optional_imports.py | 4 ++-- mantidimaging/core/utility/sensible_roi.py | 2 +- mantidimaging/core/utility/size_calculator.py | 8 ++++---- mantidimaging/core/utility/version_check.py | 2 +- 12 files changed, 27 insertions(+), 26 deletions(-) diff --git a/mantidimaging/core/utility/close_enough_point.py b/mantidimaging/core/utility/close_enough_point.py index dc6ac735b74..7cb01866585 100644 --- a/mantidimaging/core/utility/close_enough_point.py +++ b/mantidimaging/core/utility/close_enough_point.py @@ -20,5 +20,5 @@ def __init__(self, points: Sequence[int] | Sequence[float]): self.y = int(points[1]) self.x = int(points[0]) - def __str__(self): + def __str__(self) -> str: return f"({self.x}, {self.y})" diff --git a/mantidimaging/core/utility/command_line_arguments.py b/mantidimaging/core/utility/command_line_arguments.py index c6c02f8a597..73858b53ac8 100644 --- a/mantidimaging/core/utility/command_line_arguments.py +++ b/mantidimaging/core/utility/command_line_arguments.py @@ -9,11 +9,11 @@ logger = getLogger(__name__) -def _get_filter_names(): +def _get_filter_names() -> dict[str, str]: return {package.filter_name.replace(" ", "-").lower(): package.filter_name for package in load_filter_packages()} -def _log_and_exit(msg: str): +def _log_and_exit(msg: str) -> None: """ Log an error message and exit. :param msg: The log message. diff --git a/mantidimaging/core/utility/cor_interpolate.py b/mantidimaging/core/utility/cor_interpolate.py index b1c800e5c58..d3fe7af8216 100644 --- a/mantidimaging/core/utility/cor_interpolate.py +++ b/mantidimaging/core/utility/cor_interpolate.py @@ -5,7 +5,7 @@ import numpy as np -def execute(data_length, slice_ids, cors_for_sinograms): +def execute(data_length: int, slice_ids: np.ndarray, cors_for_sinograms: np.ndarray) -> np.ndarray: """ Interpolates the Centers of Rotation for the sinograms that are not provided. diff --git a/mantidimaging/core/utility/cuda_check.py b/mantidimaging/core/utility/cuda_check.py index 7c9fca0a06f..87f9eed4fe4 100644 --- a/mantidimaging/core/utility/cuda_check.py +++ b/mantidimaging/core/utility/cuda_check.py @@ -39,7 +39,7 @@ class CudaChecker: _instance = None _cuda_is_present = False - def __new__(cls): + def __new__(cls) -> CudaChecker: """ Creates a singleton for storing the result of the Cuda check. """ @@ -56,7 +56,7 @@ def cuda_is_present(cls) -> bool: return cls._cuda_is_present @classmethod - def clear_instance(cls): + def clear_instance(cls) -> None: """ Resets the instance. Used for making sure mocks don't leak in tests. """ diff --git a/mantidimaging/core/utility/data_containers.py b/mantidimaging/core/utility/data_containers.py index 8be4df8040f..6082a8d5ee9 100644 --- a/mantidimaging/core/utility/data_containers.py +++ b/mantidimaging/core/utility/data_containers.py @@ -48,7 +48,7 @@ class ScalarCoR(SingleValue): __slots__ = 'value' value: float - def to_vec(self, detector_width): + def to_vec(self, detector_width: float) -> VectorCoR: return VectorCoR(detector_width / 2 - self.value) @@ -57,7 +57,7 @@ class VectorCoR(SingleValue): __slots__ = 'value' value: float - def to_scalar(self, detector_width): + def to_scalar(self, detector_width: float) -> ScalarCoR: return ScalarCoR(detector_width / 2 + self.value) diff --git a/mantidimaging/core/utility/execution_timer.py b/mantidimaging/core/utility/execution_timer.py index a1e56894f45..dcf9d287cb3 100644 --- a/mantidimaging/core/utility/execution_timer.py +++ b/mantidimaging/core/utility/execution_timer.py @@ -35,21 +35,21 @@ def __init__(self, msg: str = 'Elapsed time', logger: Logger = perf_logger): self.time_start: float | None = None self.time_end: float | None = None - def __str__(self): + def __str__(self) -> str: prefix = f'{self.msg}: ' if self.msg else '' sec = self.total_seconds return f'{prefix}{sec if sec else "unknown"} seconds' - def __enter__(self): + def __enter__(self) -> None: self.time_start = time.monotonic() self.time_end = None - def __exit__(self, *args): + def __exit__(self, *args) -> None: self.time_end = time.monotonic() self.logger.info(str(self)) @property - def total_seconds(self): + def total_seconds(self) -> float | None: """ Gets the total number of seconds the timer was running for, returns None if the timer has not been run or is still running. @@ -77,7 +77,7 @@ def __init__(self, self.pr = cProfile.Profile() - def __str__(self): + def __str__(self) -> str: out = StringIO() out.write(f'{self.msg}: \n' if self.msg else '') @@ -85,10 +85,10 @@ def __str__(self): ps.print_stats() return out.getvalue() - def __enter__(self): + def __enter__(self) -> None: self.pr.enable() - def __exit__(self, *args): + def __exit__(self, *args) -> None: self.pr.disable() if perf_logger.isEnabledFor(1): for line in str(self).split("\n")[:self.max_lines]: diff --git a/mantidimaging/core/utility/histogram.py b/mantidimaging/core/utility/histogram.py index a901a807965..8caeb0345b0 100644 --- a/mantidimaging/core/utility/histogram.py +++ b/mantidimaging/core/utility/histogram.py @@ -11,7 +11,7 @@ DEFAULT_NUM_BINS = 2048 -def set_histogram_log_scale(histogram: HistogramLUTItem): +def set_histogram_log_scale(histogram: HistogramLUTItem) -> None: """ Sets the y-values of a histogram to use a log scale. :param histogram: The HistogramLUTItem of an image. @@ -20,7 +20,7 @@ def set_histogram_log_scale(histogram: HistogramLUTItem): histogram.plot.setData(x_data, np.log(y_data + 1)) -def generate_histogram_from_image(image_data, num_bins=DEFAULT_NUM_BINS): +def generate_histogram_from_image(image_data: np.ndarray, num_bins: int = DEFAULT_NUM_BINS) -> tuple: histogram, bins = np.histogram(image_data.flatten(), num_bins) center = (bins[:-1] + bins[1:]) / 2 return center, histogram, bins diff --git a/mantidimaging/core/utility/imat_log_file_parser.py b/mantidimaging/core/utility/imat_log_file_parser.py index 90a2daba758..5b4e08ac307 100644 --- a/mantidimaging/core/utility/imat_log_file_parser.py +++ b/mantidimaging/core/utility/imat_log_file_parser.py @@ -10,6 +10,7 @@ from typing import TYPE_CHECKING import numpy +import numpy as np from mantidimaging.core.utility.data_containers import Counts, ProjectionAngles @@ -210,7 +211,7 @@ def get_seperator(first_row: str) -> bool: def source_file(self) -> Path: return self._source_file - def projection_numbers(self): + def projection_numbers(self) -> np.ndarray: proj_nums = numpy.zeros(len(self._data[IMATLogColumn.PROJECTION_NUMBER]), dtype=numpy.uint32) proj_nums[:] = self._data[IMATLogColumn.PROJECTION_NUMBER] return proj_nums diff --git a/mantidimaging/core/utility/optional_imports.py b/mantidimaging/core/utility/optional_imports.py index 13b2a78e64c..ed2ca09c0b8 100644 --- a/mantidimaging/core/utility/optional_imports.py +++ b/mantidimaging/core/utility/optional_imports.py @@ -35,9 +35,9 @@ def safe_import(name): return module -def check_availability(name): +def check_availability(name: str) -> bool: return safe_import(name) is not None -def tomopy_available(): +def tomopy_available() -> bool: return check_availability('tomopy') diff --git a/mantidimaging/core/utility/sensible_roi.py b/mantidimaging/core/utility/sensible_roi.py index 1cbe5b9f6d8..9655cee7225 100644 --- a/mantidimaging/core/utility/sensible_roi.py +++ b/mantidimaging/core/utility/sensible_roi.py @@ -40,7 +40,7 @@ def __iter__(self) -> Iterator[int]: """ return iter((self.left, self.top, self.right, self.bottom)) - def __str__(self): + def __str__(self) -> str: return f"Left: {self.left}, Top: {self.top}, Right: {self.right}, Bottom: {self.bottom}" def to_list_string(self) -> str: diff --git a/mantidimaging/core/utility/size_calculator.py b/mantidimaging/core/utility/size_calculator.py index 292de6e23f1..8c6aaec179c 100644 --- a/mantidimaging/core/utility/size_calculator.py +++ b/mantidimaging/core/utility/size_calculator.py @@ -30,17 +30,17 @@ def full_size(shape: Iterable[int]) -> int: return math.prod(shape) -def full_size_bytes(shape: Iterable[int], dtype: npt.DTypeLike): +def full_size_bytes(shape: Iterable[int], dtype: npt.DTypeLike) -> int: return full_size(shape) * _determine_dtype_size(dtype) -def full_size_KB(shape: Iterable[int], dtype: npt.DTypeLike): +def full_size_KB(shape: Iterable[int], dtype: npt.DTypeLike) -> float: return full_size_bytes(shape, dtype) / 1024 -def full_size_MB(shape: Iterable[int], dtype: npt.DTypeLike): +def full_size_MB(shape: Iterable[int], dtype: npt.DTypeLike) -> float: return full_size_KB(shape, dtype) / 1024 -def number_of_images_from_indices(start, end, increment): +def number_of_images_from_indices(start: int, end: int, increment: int) -> int: return int((end - start) / increment) if increment != 0 else 0 diff --git a/mantidimaging/core/utility/version_check.py b/mantidimaging/core/utility/version_check.py index 97b91a73a56..c5ffcff4ed1 100644 --- a/mantidimaging/core/utility/version_check.py +++ b/mantidimaging/core/utility/version_check.py @@ -127,5 +127,5 @@ def _parse_version(package_version_string: str) -> Version: return parse(normalised_version_string) -def _version_is_uptodate(local: str, remote: str): +def _version_is_uptodate(local: str, remote: str) -> bool: return _parse_version(local) >= _parse_version(remote) From 112def87ed2798c5749520aad73fca0bf49cb212 Mon Sep 17 00:00:00 2001 From: ashmeigh <56345053+ashmeigh@users.noreply.github.com> Date: Tue, 16 Jul 2024 20:38:49 +0100 Subject: [PATCH 3/6] Update presenter.py i made changes to my main by mistake --- mantidimaging/gui/dialogs/cor_inspection/presenter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mantidimaging/gui/dialogs/cor_inspection/presenter.py b/mantidimaging/gui/dialogs/cor_inspection/presenter.py index 7b4f2120541..47e83b68c19 100644 --- a/mantidimaging/gui/dialogs/cor_inspection/presenter.py +++ b/mantidimaging/gui/dialogs/cor_inspection/presenter.py @@ -108,5 +108,5 @@ def optimal_rotation_centre(self) -> ScalarCoR: return ScalarCoR(self.model.centre_value) @property - def optimal_iterations(self) -> int: - return int(self.model.centre_value) + def optimal_iterations(self): + return self.model.centre_value From 2d004e179773c87e3215447b6a2d59834d176289 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Thu, 18 Jul 2024 07:42:43 +0100 Subject: [PATCH 4/6] change types to Noreturn and just float Signed-off-by: ashmeigh --- mantidimaging/core/utility/command_line_arguments.py | 3 ++- mantidimaging/core/utility/execution_timer.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mantidimaging/core/utility/command_line_arguments.py b/mantidimaging/core/utility/command_line_arguments.py index 73858b53ac8..340e9dc4bb8 100644 --- a/mantidimaging/core/utility/command_line_arguments.py +++ b/mantidimaging/core/utility/command_line_arguments.py @@ -3,6 +3,7 @@ from __future__ import annotations from logging import getLogger import os +from typing import NoReturn from mantidimaging.core.operations.loader import load_filter_packages @@ -13,7 +14,7 @@ def _get_filter_names() -> dict[str, str]: return {package.filter_name.replace(" ", "-").lower(): package.filter_name for package in load_filter_packages()} -def _log_and_exit(msg: str) -> None: +def _log_and_exit(msg: str) -> NoReturn: """ Log an error message and exit. :param msg: The log message. diff --git a/mantidimaging/core/utility/execution_timer.py b/mantidimaging/core/utility/execution_timer.py index dcf9d287cb3..cfd2f5b3686 100644 --- a/mantidimaging/core/utility/execution_timer.py +++ b/mantidimaging/core/utility/execution_timer.py @@ -49,13 +49,13 @@ def __exit__(self, *args) -> None: self.logger.info(str(self)) @property - def total_seconds(self) -> float | None: + def total_seconds(self) -> float: """ Gets the total number of seconds the timer was running for, returns None if the timer has not been run or is still running. """ return self.time_end - self.time_start if \ - self.time_start and self.time_end else None + self.time_start and self.time_end else 0 class ExecutionProfiler: From 035bd28b8e92d9971511e927cc4a2795cc1c8700 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Thu, 18 Jul 2024 11:15:40 +0100 Subject: [PATCH 5/6] fixing test Signed-off-by: ashmeigh --- mantidimaging/core/utility/test/execution_timer_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mantidimaging/core/utility/test/execution_timer_test.py b/mantidimaging/core/utility/test/execution_timer_test.py index c68ea21a6e1..4ba2b6aef3b 100644 --- a/mantidimaging/core/utility/test/execution_timer_test.py +++ b/mantidimaging/core/utility/test/execution_timer_test.py @@ -12,11 +12,11 @@ class ExecutionTimerTest(unittest.TestCase): def test_execute(self): t = ExecutionTimer() - self.assertEqual(t.total_seconds, None) + self.assertEqual(t.total_seconds, 0.0) self.assertEqual(str(t), 'Elapsed time: unknown seconds') with t: - self.assertEqual(t.total_seconds, None) + self.assertEqual(t.total_seconds, 0.0) time.sleep(0.1) From b7abb1f49f4f67a4bc2544e6368804d98db6db4d Mon Sep 17 00:00:00 2001 From: ashmeigh <56345053+ashmeigh@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:55:20 +0100 Subject: [PATCH 6/6] Update version_check.py edited --- mantidimaging/core/utility/version_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mantidimaging/core/utility/version_check.py b/mantidimaging/core/utility/version_check.py index c5ffcff4ed1..97b91a73a56 100644 --- a/mantidimaging/core/utility/version_check.py +++ b/mantidimaging/core/utility/version_check.py @@ -127,5 +127,5 @@ def _parse_version(package_version_string: str) -> Version: return parse(normalised_version_string) -def _version_is_uptodate(local: str, remote: str) -> bool: +def _version_is_uptodate(local: str, remote: str): return _parse_version(local) >= _parse_version(remote)