Skip to content

Commit

Permalink
🔀 Merge develop into dev-define-engines-abc
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneahmed committed Aug 30, 2023
1 parent 07935a1 commit 112d2b4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 36 deletions.
9 changes: 5 additions & 4 deletions pre-commit/missing_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import sys
import tokenize
from pathlib import Path
from typing import NoReturn

from requirements_consistency import parse_requirements

Expand Down Expand Up @@ -135,7 +136,7 @@ def stems(node: ast.Import | ast.ImportFrom) -> list[tuple[str, str]]:
)


def main():
def main() -> NoReturn:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Static analysis of requirements files and import statements.",
Expand Down Expand Up @@ -219,13 +220,13 @@ def find_bad_imports(
return result


def find_comments(path, line_num: int):
def find_comments(path: str | Path, line_num: int) -> list:
"""Find comments on the given line.
Args:
path:
path (str | Path):
Path to the file.
line_num:
line_num (int):
Line number to find comments on.
Returns:
Expand Down
5 changes: 3 additions & 2 deletions pre-commit/requirements_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import importlib
import sys
from pathlib import Path
from typing import NoReturn

import yaml
from pkg_resources import Requirement
Expand Down Expand Up @@ -100,7 +101,7 @@ def parse_conda(file_path: Path) -> dict[str, Requirement]:
return packages


def parse_setup_py(file_path) -> dict[str, Requirement]:
def parse_setup_py(file_path: Path) -> dict[str, Requirement]:
"""Parse a setup.py file.
Args:
Expand Down Expand Up @@ -233,7 +234,7 @@ def in_common_consistent(all_requirements: dict[Path, dict[str, Requirement]]) -
return consistent


def main():
def main() -> NoReturn:
"""Main entry point for the hook."""
root = Path(__file__).parent.parent
test_files_exist(root)
Expand Down
4 changes: 2 additions & 2 deletions tests/engines/_test_multi_task_segmentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
# ----------------------------------------------------


def _crash_func(_) -> None:
def _crash_func(_: object) -> None:
"""Helper to induce crash."""
msg = "Propagation Crash."
raise ValueError(msg)


def semantic_postproc_func(raw_output):
def semantic_postproc_func(raw_output: np.ndarray) -> np.ndarray:
"""Function to post process semantic segmentations.
Post processes semantic segmentation to form one map output.
Expand Down
4 changes: 2 additions & 2 deletions tests/engines/_test_nucleus_instance_segmentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
# ----------------------------------------------------


def _crash_func(_x) -> None:
def _crash_func(_x: object) -> None:
"""Helper to induce crash."""
msg = "Propagation Crash."
raise ValueError(msg)


def helper_tile_info():
def helper_tile_info() -> list:
"""Helper function for tile information."""
predictor = NucleusInstanceSegmentor(model="A")
# ! assuming the tiles organized as follows (coming out from
Expand Down
63 changes: 41 additions & 22 deletions tests/engines/_test_patch_predictor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test for Patch Predictor."""
from __future__ import annotations

import copy
import shutil
Expand Down Expand Up @@ -32,7 +33,10 @@
# -------------------------------------------------------------------------------------


def test_patch_dataset_path_imgs(sample_patch1, sample_patch2) -> None:
def test_patch_dataset_path_imgs(
sample_patch1: str | Path,
sample_patch2: str | Path,
) -> None:
"""Test for patch dataset with a list of file paths as input."""
size = (224, 224, 3)

Expand Down Expand Up @@ -212,32 +216,35 @@ def test_patch_dataset_crash(tmp_path: Path) -> None:
predefined_preproc_func("secret-dataset")


def test_wsi_patch_dataset(sample_wsi_dict, tmp_path: Path) -> None: # noqa: PLR0915
def test_wsi_patch_dataset( # noqa: PLR0915
sample_wsi_dict: dict,
tmp_path: Path,
) -> None:
"""A test for creation and bare output."""
# convert to pathlib Path to prevent wsireader complaint
mini_wsi_svs = Path(sample_wsi_dict["wsi2_4k_4k_svs"])
mini_wsi_jpg = Path(sample_wsi_dict["wsi2_4k_4k_jpg"])
mini_wsi_msk = Path(sample_wsi_dict["wsi2_4k_4k_msk"])

def reuse_init(img_path=mini_wsi_svs, **kwargs):
def reuse_init(img_path: Path = mini_wsi_svs, **kwargs: dict) -> WSIPatchDataset:
"""Testing function."""
return WSIPatchDataset(img_path=img_path, **kwargs)

def reuse_init_wsi(**kwargs):
def reuse_init_wsi(**kwargs: dict) -> WSIPatchDataset:
"""Testing function."""
return reuse_init(mode="wsi", **kwargs)

# test for ABC validate
# intentionally created to check error
# skipcq
class Proto(PatchDatasetABC):
def __init__(self) -> None:
def __init__(self: Proto) -> None:
super().__init__()
self.inputs = "CRASH"
self._check_input_integrity("wsi")

# skipcq
def __getitem__(self, idx):
def __getitem__(self: Proto, idx: int) -> object:
"""Get an item from the dataset."""

with pytest.raises(
Expand Down Expand Up @@ -416,7 +423,7 @@ def test_patch_dataset_abc() -> None:
# skipcq
class Proto(PatchDatasetABC):
# skipcq
def __init__(self) -> None:
def __init__(self: Proto) -> None:
super().__init__()

# crash due to undefined __getitem__
Expand All @@ -426,11 +433,11 @@ def __init__(self) -> None:
# skipcq
class Proto(PatchDatasetABC):
# skipcq
def __init__(self) -> None:
def __init__(self: Proto) -> None:
super().__init__()

# skipcq
def __getitem__(self, idx):
def __getitem__(self: Proto, idx: int) -> None:
"""Get an item from the dataset."""

ds = Proto() # skipcq
Expand Down Expand Up @@ -598,7 +605,11 @@ def test_io_config_delegation(remote_sample: Callable, tmp_path: Path) -> None:
shutil.rmtree(tmp_path / "dump", ignore_errors=True)


def test_patch_predictor_api(sample_patch1, sample_patch2, tmp_path: Path) -> None:
def test_patch_predictor_api(
sample_patch1: Path,
sample_patch2: Path,
tmp_path: Path,
) -> None:
"""Helper function to get the model output using API 1."""
save_dir_path = tmp_path

Expand Down Expand Up @@ -694,7 +705,11 @@ def test_patch_predictor_api(sample_patch1, sample_patch2, tmp_path: Path) -> No
assert len(output["predictions"]) == len(output["probabilities"])


def test_wsi_predictor_api(sample_wsi_dict, tmp_path: Path, chdir: Callable) -> None:
def test_wsi_predictor_api(
sample_wsi_dict: dict,
tmp_path: Path,
chdir: Callable,
) -> None:
"""Test normal run of wsi predictor."""
save_dir_path = tmp_path

Expand Down Expand Up @@ -814,7 +829,7 @@ def test_wsi_predictor_api(sample_wsi_dict, tmp_path: Path, chdir: Callable) ->
shutil.rmtree("output", ignore_errors=True)


def test_wsi_predictor_merge_predictions(sample_wsi_dict) -> None:
def test_wsi_predictor_merge_predictions(sample_wsi_dict: dict) -> None:
"""Test normal run of wsi predictor with merge predictions option."""
# convert to pathlib Path to prevent reader complaint
mini_wsi_svs = Path(sample_wsi_dict["wsi2_4k_4k_svs"])
Expand Down Expand Up @@ -915,11 +930,12 @@ def test_wsi_predictor_merge_predictions(sample_wsi_dict) -> None:


def _test_predictor_output(
inputs,
pretrained_model,
probabilities_check=None,
predictions_check=None,
on_gpu=ON_GPU,
inputs: list,
pretrained_model: str,
probabilities_check: list | None = None,
predictions_check: list | None = None,
*,
on_gpu: bool = ON_GPU,
) -> None:
"""Test the predictions of multiple models included in tiatoolbox."""
predictor = PatchPredictor(
Expand Down Expand Up @@ -954,7 +970,10 @@ def _test_predictor_output(
)


def test_patch_predictor_kather100k_output(sample_patch1, sample_patch2) -> None:
def test_patch_predictor_kather100k_output(
sample_patch1: Path,
sample_patch2: Path,
) -> None:
"""Test the output of patch prediction models on Kather100K dataset."""
inputs = [Path(sample_patch1), Path(sample_patch2)]
pretrained_info = {
Expand Down Expand Up @@ -989,7 +1008,7 @@ def test_patch_predictor_kather100k_output(sample_patch1, sample_patch2) -> None
break


def test_patch_predictor_pcam_output(sample_patch3, sample_patch4) -> None:
def test_patch_predictor_pcam_output(sample_patch3: Path, sample_patch4: Path) -> None:
"""Test the output of patch prediction models on PCam dataset."""
inputs = [Path(sample_patch3), Path(sample_patch4)]
pretrained_info = {
Expand Down Expand Up @@ -1029,7 +1048,7 @@ def test_patch_predictor_pcam_output(sample_patch3, sample_patch4) -> None:
# -------------------------------------------------------------------------------------


def test_command_line_models_file_not_found(sample_svs, tmp_path: Path) -> None:
def test_command_line_models_file_not_found(sample_svs: Path, tmp_path: Path) -> None:
"""Test for models CLI file not found error."""
runner = CliRunner()
model_file_not_found_result = runner.invoke(
Expand All @@ -1050,7 +1069,7 @@ def test_command_line_models_file_not_found(sample_svs, tmp_path: Path) -> None:
assert isinstance(model_file_not_found_result.exception, FileNotFoundError)


def test_command_line_models_incorrect_mode(sample_svs, tmp_path: Path) -> None:
def test_command_line_models_incorrect_mode(sample_svs: Path, tmp_path: Path) -> None:
"""Test for models CLI mode not in wsi, tile."""
runner = CliRunner()
mode_not_in_wsi_tile_result = runner.invoke(
Expand All @@ -1073,7 +1092,7 @@ def test_command_line_models_incorrect_mode(sample_svs, tmp_path: Path) -> None:
assert isinstance(mode_not_in_wsi_tile_result.exception, SystemExit)


def test_cli_model_single_file(sample_svs, tmp_path: Path) -> None:
def test_cli_model_single_file(sample_svs: Path, tmp_path: Path) -> None:
"""Test for models CLI single file."""
runner = CliRunner()
models_wsi_result = runner.invoke(
Expand Down
9 changes: 5 additions & 4 deletions tests/models/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test for predefined dataset within toolbox."""
from __future__ import annotations

import shutil
from pathlib import Path
Expand All @@ -16,23 +17,23 @@
class Proto1(DatasetInfoABC):
"""Intentionally created to check error with new attribute a."""

def __init__(self) -> None:
def __init__(self: Proto1) -> None:
"""Proto1 initialization."""
self.a = "a"


class Proto2(DatasetInfoABC):
"""Intentionally created to check error with attribute inputs."""

def __init__(self) -> None:
def __init__(self: Proto2) -> None:
"""Proto2 initialization."""
self.inputs = "a"


class Proto3(DatasetInfoABC):
"""Intentionally created to check error with attribute inputs and labels."""

def __init__(self) -> None:
def __init__(self: Proto3) -> None:
"""Proto3 initialization."""
self.inputs = "a"
self.labels = "a"
Expand All @@ -41,7 +42,7 @@ def __init__(self) -> None:
class Proto4(DatasetInfoABC):
"""Intentionally created to check error with attribute inputs and label names."""

def __init__(self) -> None:
def __init__(self: Proto4) -> None:
"""Proto4 initialization."""
self.inputs = "a"
self.label_names = "a"
Expand Down

0 comments on commit 112d2b4

Please sign in to comment.