Skip to content

Commit c4ea64e

Browse files
authored
Address typing errors (#1316)
1 parent e76fb9d commit c4ea64e

5 files changed

Lines changed: 17 additions & 12 deletions

File tree

src/torchio/data/image.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def __init__(
135135
tensor: TypeData | None = None,
136136
affine: TypeData | None = None,
137137
check_nans: bool = False, # removed by ITK by default
138-
reader: Callable = read_image,
138+
reader: Callable[[TypePath], TypeDataAffine] = read_image,
139139
**kwargs: dict[str, Any],
140140
):
141141
self.check_nans = check_nans
@@ -617,8 +617,8 @@ def read_and_check(self, path: TypePath) -> TypeDataAffine:
617617
# Make sure the data type is compatible with PyTorch
618618
if self.reader is not read_image and isinstance(tensor, np.ndarray):
619619
tensor = check_uint_to_int(tensor)
620-
tensor = self._parse_tensor_shape(tensor)
621-
tensor = self._parse_tensor(tensor)
620+
tensor = self._parse_tensor_shape(tensor) # type: ignore[assignment]
621+
tensor = self._parse_tensor(tensor) # type: ignore[assignment]
622622
affine = self._parse_affine(affine)
623623
if self.check_nans and torch.isnan(tensor).any():
624624
warnings.warn(

src/torchio/data/inference/aggregator.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def _crop_patch(
6363
index_ini, index_fin = location[:3], location[3:]
6464

6565
# If the patch is not at the border, we crop half the overlap
66-
crop_ini = half_overlap.copy()
67-
crop_fin = half_overlap.copy()
66+
crop_ini: np.ndarray = half_overlap.copy()
67+
crop_fin: np.ndarray = half_overlap.copy()
6868

6969
# If the volume has been padded, we don't need to worry about cropping
7070
if self.volume_padded:
@@ -78,9 +78,17 @@ def _crop_patch(
7878
new_index_fin = index_fin - crop_fin
7979
new_location = np.hstack((new_index_ini, new_index_fin))
8080

81-
patch_size = patch.shape[-3:]
81+
patch_size = np.asarray(patch.shape[-3:], dtype=int)
82+
crop_fin = crop_fin.astype(int)
8283
i_ini, j_ini, k_ini = crop_ini
8384
i_fin, j_fin, k_fin = patch_size - crop_fin
85+
# Make type checkers happy
86+
i_ini = int(i_ini)
87+
j_ini = int(j_ini)
88+
k_ini = int(k_ini)
89+
i_fin = int(i_fin)
90+
j_fin = int(j_fin)
91+
k_fin = int(k_fin)
8492
cropped_patch = patch[:, i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
8593
return cropped_patch, new_location
8694

src/torchio/data/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def ensure_4d(tensor: TypeData, num_spatial_dims=None) -> torch.Tensor:
459459
return tensor
460460

461461

462-
def check_uint_to_int(array):
462+
def check_uint_to_int(array: np.ndarray) -> np.ndarray:
463463
# This is because PyTorch won't take uint16 nor uint32
464464
if array.dtype == np.uint16:
465465
return array.astype(np.int32)

src/torchio/data/sampler/weighted.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def extract_patch( # type: ignore[override]
184184
i, j, k = self.get_random_index_ini(probability_map, cdf)
185185
index_ini = i, j, k
186186
si, sj, sk = self.patch_size
187-
patch_size = si, sj, sk
187+
patch_size = int(si), int(sj), int(sk)
188188
cropped_subject = self.crop(
189189
subject,
190190
index_ini,

tox.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,4 @@ commands = ruff format --diff
3636
dependency_groups =
3737
types
3838
commands =
39-
mypy \
40-
--install-types \
41-
--non-interactive \
42-
src
39+
mypy src

0 commit comments

Comments
 (0)