Skip to content

Commit c2f19c2

Browse files
Type Annotation Enhancement in mantidimaging/core/ Directory part 2 (#2284)
2 parents 5e19bdc + 2c27547 commit c2f19c2

File tree

10 files changed

+32
-28
lines changed

10 files changed

+32
-28
lines changed

mantidimaging/core/io/instrument_log_implmentations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ def read_imat_date(time_stamp: str) -> datetime:
100100
locale.setlocale(locale.LC_TIME, lc)
101101

102102
@staticmethod
103-
def _has_imat_header(line: str):
103+
def _has_imat_header(line: str) -> bool:
104104
HEADERS = [
105105
"TIME STAMP,IMAGE TYPE,IMAGE COUNTER,COUNTS BM3 before image,COUNTS BM3 after image",
106106
"TIME STAMP IMAGE TYPE IMAGE COUNTER COUNTS BM3 before image COUNTS BM3 after image",
107107
]
108108
return line.strip() in HEADERS
109109

110110
@classmethod
111-
def _has_imat_data_line(cls, line: str):
111+
def _has_imat_data_line(cls, line: str) -> bool:
112112
try:
113113
_ = cls.read_imat_date(line[:24])
114114
except ValueError:

mantidimaging/core/io/saver.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@
3636
package_version = CheckVersion().get_version()
3737

3838

39-
def write_fits(data: np.ndarray, filename: str, overwrite: bool = False, description: str | None = ""):
39+
def write_fits(data: np.ndarray, filename: str, overwrite: bool = False, description: str | None = "") -> None:
4040
hdu = fits.PrimaryHDU(data)
4141
hdulist = fits.HDUList([hdu])
4242
hdulist.writeto(filename, overwrite=overwrite)
4343

4444

45-
def write_img(data: np.ndarray, filename: str, overwrite: bool = False, description: str | None = ""):
45+
def write_img(data: np.ndarray, filename: str, overwrite: bool = False, description: str | None = "") -> None:
4646
tifffile.imwrite(filename, data, description=description, metadata=None, software="Mantid Imaging")
4747

4848

49-
def write_nxs(data: np.ndarray, filename: str, projection_angles: np.ndarray | None = None, overwrite: bool = False):
49+
def write_nxs(data: np.ndarray,
50+
filename: str,
51+
projection_angles: np.ndarray | None = None,
52+
overwrite: bool = False) -> None:
5053
import h5py
5154
nxs = h5py.File(filename, 'w')
5255

@@ -177,7 +180,7 @@ def image_save(images: ImageStack,
177180
return names
178181

179182

180-
def nexus_save(dataset: StrictDataset, path: str, sample_name: str, save_as_float: bool):
183+
def nexus_save(dataset: StrictDataset, path: str, sample_name: str, save_as_float: bool) -> None:
181184
"""
182185
Uses information from a StrictDataset to create a NeXus file.
183186
:param dataset: The dataset to save as a NeXus file.
@@ -199,7 +202,7 @@ def nexus_save(dataset: StrictDataset, path: str, sample_name: str, save_as_floa
199202
nexus_file.close()
200203

201204

202-
def _nexus_save(nexus_file: h5py.File, dataset: StrictDataset, sample_name: str, save_as_float: bool):
205+
def _nexus_save(nexus_file: h5py.File, dataset: StrictDataset, sample_name: str, save_as_float: bool) -> None:
203206
"""
204207
Takes a NeXus file and writes the StrictDataset information to it.
205208
:param nexus_file: The NeXus file.
@@ -252,7 +255,7 @@ def _nexus_save(nexus_file: h5py.File, dataset: StrictDataset, sample_name: str,
252255

253256

254257
def _save_processed_data_to_nexus(nexus_file: h5py.File, dataset: StrictDataset, rotation_angle: h5py.Dataset,
255-
image_key: h5py.Dataset, save_as_float: bool):
258+
image_key: h5py.Dataset, save_as_float: bool) -> None:
256259
data = nexus_file.create_group(NEXUS_PROCESSED_DATA_PATH)
257260
data["rotation_angle"] = rotation_angle
258261
data["image_key"] = image_key
@@ -266,7 +269,7 @@ def _save_processed_data_to_nexus(nexus_file: h5py.File, dataset: StrictDataset,
266269
process.create_dataset("version", data=np.bytes_(package_version))
267270

268271

269-
def _save_image_stacks_to_nexus(dataset: StrictDataset, data_group: h5py.Group, save_as_float: bool):
272+
def _save_image_stacks_to_nexus(dataset: StrictDataset, data_group: h5py.Group, save_as_float: bool) -> None:
270273
combined_data_shape = (sum([len(arr) for arr in dataset.nexus_arrays]), ) + dataset.nexus_arrays[0].shape[1:]
271274

272275
index = 0
@@ -305,7 +308,7 @@ def scale_row(row):
305308
return converted, factors
306309

307310

308-
def _save_recon_to_nexus(nexus_file: h5py.File, recon: ImageStack, sample_path: str):
311+
def _save_recon_to_nexus(nexus_file: h5py.File, recon: ImageStack, sample_path: str) -> None:
309312
"""
310313
Saves a recon to a NeXus file.
311314
:param nexus_file: The NeXus file.
@@ -369,7 +372,7 @@ def _create_pixel_size_arrays(recon: ImageStack) -> tuple[np.ndarray, np.ndarray
369372
return x_arr, y_arr, z_arr
370373

371374

372-
def _set_nx_class(group: h5py.Group, class_name: str):
375+
def _set_nx_class(group: h5py.Group, class_name: str) -> None:
373376
"""
374377
Sets the NX_class attribute of data in a NeXus file.
375378
:param group: The h5py group.

mantidimaging/core/net/help_pages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
SECTION_USER_GUIDE = f"{DOCS_BASE}/user_guide/"
1010

1111

12-
def open_user_operation_docs(operation_name: str):
12+
def open_user_operation_docs(operation_name: str) -> None:
1313
page_url = "operations/index"
1414
section = operation_name.lower().replace(" ", "-")
1515
open_help_webpage(SECTION_USER_GUIDE, page_url, section)
1616

1717

18-
def open_help_webpage(section_url: str, page_url: str, section: str | None = None):
18+
def open_help_webpage(section_url: str, page_url: str, section: str | None = None) -> None:
1919
if section is not None:
2020
url = f"{section_url}{page_url}.html#{section}"
2121
else:

mantidimaging/core/parallel/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def run_compute_func(func: ComputeFuncType,
3333
num_operations: int,
3434
arrays: list[pu.SharedArray] | pu.SharedArray,
3535
params: dict[str, Any],
36-
progress=None):
36+
progress=None) -> None:
3737
if isinstance(arrays, pu.SharedArray):
3838
arrays = [arrays]
3939
all_data_in_shared_memory, data = _check_shared_mem_and_get_data(arrays)

mantidimaging/core/parallel/utility.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
LOG = getLogger(__name__)
2424

2525

26-
def enough_memory(shape, dtype):
26+
def enough_memory(shape, dtype) -> bool:
2727
return full_size_KB(shape=shape, dtype=dtype) < system_free_memory().kb()
2828

2929

@@ -103,7 +103,7 @@ def multiprocessing_necessary(shape: int, is_shared_data: bool) -> bool:
103103
return True
104104

105105

106-
def execute_impl(img_num: int, partial_func: partial, is_shared_data: bool, progress: Progress, msg: str):
106+
def execute_impl(img_num: int, partial_func: partial, is_shared_data: bool, progress: Progress, msg: str) -> None:
107107
task_name = f"{msg}"
108108
progress = Progress.ensure_instance(progress, num_steps=img_num, task_name=task_name)
109109
indices_list = range(img_num)
@@ -128,7 +128,7 @@ def run_compute_func_impl(worker_func: Callable[[int], None],
128128
num_operations: int,
129129
is_shared_data: bool,
130130
progress=None,
131-
msg: str = ""):
131+
msg: str = "") -> None:
132132
task_name = f"{msg}"
133133
progress = Progress.ensure_instance(progress, num_steps=num_operations, task_name=task_name)
134134
indices_list = range(num_operations)

mantidimaging/core/reconstruct/astra_recon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
# Full credit for following code to Daniil Kazantzev
2525
# Source:
2626
# https://github.com/dkazanc/ToMoBAR/blob/5990aaa264e2f08bd9b0069c8847e5021fbf2ee2/src/Python/tomobar/supp/astraOP.py#L20-L70
27-
def rotation_matrix2d(theta: float):
27+
def rotation_matrix2d(theta: float) -> np.ndarray:
2828
return np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
2929

3030

31-
def vec_geom_init2d(angles_rad: ProjectionAngles, detector_spacing_x: float, center_rot_offset: float):
31+
def vec_geom_init2d(angles_rad: ProjectionAngles, detector_spacing_x: float, center_rot_offset: float) -> np.ndarray:
3232
angles_value = angles_rad.value
3333
s0 = [0.0, -1.0] # source
3434
u0 = [detector_spacing_x, 0.0] # detector coordinates

mantidimaging/core/reconstruct/base_recon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def find_cor(images: ImageStack, slice_idx: int, start_cor: float, recon_params:
2020
raise NotImplementedError("Base class call")
2121

2222
@staticmethod
23-
def prepare_sinogram(data: np.ndarray, recon_params: ReconstructionParameters):
23+
def prepare_sinogram(data: np.ndarray, recon_params: ReconstructionParameters) -> np.ndarray:
2424
logged_data = BaseRecon.negative_log(data)
2525
if recon_params.beam_hardening_coefs is not None:
2626
coefs = np.array([0.0, 1.0] + recon_params.beam_hardening_coefs)

mantidimaging/core/reconstruct/tomopy_recon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def single_sino(sino: np.ndarray,
3636
cor: ScalarCoR,
3737
proj_angles: ProjectionAngles,
3838
recon_params: ReconstructionParameters,
39-
progress: Progress | None = None):
39+
progress: Progress | None = None) -> np.ndarray:
4040
sino = BaseRecon.prepare_sinogram(sino, recon_params)
4141
volume = tomopy.recon(tomo=[sino],
4242
sinogram_order=True,

mantidimaging/core/rotation/data_model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ def angle_in_degrees(self) -> Degrees:
130130
return Degrees(-np.rad2deg(np.arctan(self.gradient.value)))
131131

132132
@property
133-
def has_results(self):
133+
def has_results(self) -> bool:
134134
return self._cached_gradient is not None and self._cached_cor is not None
135135

136136
@property
137-
def empty(self):
137+
def empty(self) -> bool:
138138
return not self._points
139139

140140
@property
141-
def num_points(self):
141+
def num_points(self) -> int:
142142
return len(self._points)
143143

144144
@property
145-
def stack_properties(self):
145+
def stack_properties(self) -> dict:
146146
return {
147147
# TODO remove float casts
148148
const.COR_TILT_ROTATION_CENTRE: float(self.cor.value),

mantidimaging/core/rotation/polyfit_correlation.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
def do_calculate_correlation_err(store: np.ndarray, search_index: int, p0_and_180: tuple[np.ndarray, np.ndarray],
20-
image_width: int):
20+
image_width: int) -> None:
2121
"""
2222
Calculates squared sum error in the difference between the projection at 0 degrees, and the one at 180 degrees
2323
"""
@@ -68,7 +68,7 @@ def find_center(images: ImageStack, progress: Progress) -> tuple[ScalarCoR, Degr
6868
return ScalarCoR(images.h_middle + -offset), theta
6969

7070

71-
def compute_correlation_error(index: int, arrays: list[Any], params: dict[str, Any]):
71+
def compute_correlation_error(index: int, arrays: list[Any], params: dict[str, Any]) -> None:
7272
min_correlation_error = arrays[0]
7373
shared_projections = arrays[1]
7474
shared_search_range = arrays[2]
@@ -79,7 +79,8 @@ def compute_correlation_error(index: int, arrays: list[Any], params: dict[str, A
7979
(shared_projections[0], shared_projections[1]), image_width)
8080

8181

82-
def _find_shift(images: ImageStack, search_range: range, min_correlation_error: np.ndarray, shift: np.ndarray):
82+
def _find_shift(images: ImageStack, search_range: range, min_correlation_error: np.ndarray,
83+
shift: np.ndarray) -> np.ndarray:
8384
# Then we just find the index of the minimum one (minimum error)
8485
min_correlation_error = np.transpose(min_correlation_error)
8586
# argmin returns a list of where the minimum argument is found

0 commit comments

Comments
 (0)