Skip to content

🔧 mypy type check wsicore #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/mypy-type-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ jobs:
tiatoolbox/models/models_abc.py \
tiatoolbox/models/architecture/__init__.py \
tiatoolbox/models/architecture/utils.py \
tiatoolbox/wsicore/__init__.py \
tiatoolbox/wsicore/wsimeta.py \
tiatoolbox/wsicore/metadata/
25 changes: 17 additions & 8 deletions tiatoolbox/wsicore/metadata/ngff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Literal

from tiatoolbox import __version__ as tiatoolbox_version
if TYPE_CHECKING:
from collections.abc import Iterator

if TYPE_CHECKING: # pragma: no cover
from numbers import Number
from dataclasses import dataclass, field

from tiatoolbox import __version__ as tiatoolbox_version

SpaceUnits = Literal[
"angstrom",
Expand Down Expand Up @@ -169,6 +170,14 @@ class Multiscales:
datasets: list[Dataset] = field(default_factory=lambda: [Dataset()])
version: str = "0.4"

def __iter__(self: Multiscales) -> Iterator:
"""Iterate over the values of the attributes in the `Multiscales` object.

Yields:
Iterator: An iterator
"""
yield from self.__dict__.values()


@dataclass
class Window:
Expand All @@ -186,10 +195,10 @@ class Window:

"""

end: Number = 255
max: Number = 255
min: Number = 0
start: Number = 0
end: int = 255
max: int = 255
min: int = 0
start: int = 0


@dataclass
Expand Down
16 changes: 11 additions & 5 deletions tiatoolbox/wsicore/wsimeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from numbers import Number
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

import numpy as np

Expand Down Expand Up @@ -121,7 +121,7 @@ def __init__(
self.level_downsamples = (
[float(x) for x in level_downsamples]
if level_downsamples is not None
else None
else [1.0]
)
self.level_count = (
int(level_count) if level_count is not None else len(self.level_dimensions)
Expand Down Expand Up @@ -212,7 +212,9 @@ def level_downsample(
ceil = int(np.ceil(level))
floor_downsample = level_downsamples[floor]
ceil_downsample = level_downsamples[ceil]
return np.interp(level, [floor, ceil], [floor_downsample, ceil_downsample])
return float(
np.interp(level, [floor, ceil], [floor_downsample, ceil_downsample])
)

def relative_level_scales(
self: WSIMeta,
Expand Down Expand Up @@ -260,20 +262,24 @@ def relative_level_scales(
[0.125, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0]

"""
base_scale: np.ndarray | float
msg: str

if units not in ("mpp", "power", "level", "baseline"):
msg = "Invalid units"
raise ValueError(msg)

level_downsamples = self.level_downsamples

def np_pair(x: Number | np.array) -> np.ndarray:
def np_pair(x: Resolution) -> np.ndarray:
"""Ensure input x is a numpy array of length 2."""
# If one number is given, the same value is used for x and y
if isinstance(x, Number):
return np.array([x] * 2)
return np.array(x)

if units == "level":
resolution = cast("float", resolution)
if resolution >= len(level_downsamples):
msg = (
f"Target scale level {resolution} > "
Expand All @@ -296,7 +302,7 @@ def np_pair(x: Number | np.array) -> np.ndarray:
if self.objective_power is None:
msg = (
"Objective power is None. "
"Cannot determine scale in terms of objective power.",
"Cannot determine scale in terms of objective power."
)
raise ValueError(
msg,
Expand Down
Loading