Skip to content
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

🧑‍💻 Define PatchPredictor #783

Merged
merged 140 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 139 commits
Commits
Show all changes
140 commits
Select commit Hold shift + click to select a range
ccac220
Updating engine_abc to support WSI
AbishekRajVG Nov 10, 2023
ed4193c
ruff basic checks addressed
AbishekRajVG Nov 10, 2023
bb99b58
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 10, 2023
02c041a
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Nov 16, 2023
e06c29f
Merge branch 'dev-define-engines-abc' of https://github.com/TissueIma…
AbishekRajVG Nov 16, 2023
b354ed9
EngineABC updated with WSI mode
AbishekRajVG Nov 17, 2023
45b7916
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Nov 17, 2023
3df8776
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2023
7eb36ee
Updating final file_dict logic for WSI mode in EngineABC
AbishekRajVG Nov 17, 2023
cb48a53
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2023
6a10e46
Preprocess images unified as the set_dataloader() method
AbishekRajVG Nov 21, 2023
007742b
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Nov 21, 2023
9073a03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2023
2e2e666
Fix lint errors
AbishekRajVG Nov 22, 2023
f74bed7
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Nov 22, 2023
a93ca54
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Nov 22, 2023
055e9fc
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Nov 22, 2023
24fcff9
Improving Unit test coverage: Merge Predictions
AbishekRajVG Nov 23, 2023
626ca66
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Nov 23, 2023
69d79f1
Improving unit test cases: ioconfig
AbishekRajVG Nov 24, 2023
c551097
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2023
5327066
Improving unit test coverage: wsi_predictor_merge_predictions
AbishekRajVG Nov 24, 2023
5eb553d
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Nov 24, 2023
456fb60
Improve EngineABC's post_process_wsi to support Annotation Store
AbishekRajVG Nov 27, 2023
b316c16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 27, 2023
6fcce1c
Reverting classes_predicted and updating class_dict in tiatoolbox.uti…
AbishekRajVG Nov 27, 2023
20b91e8
Merge Remote dev-update-engine-abc-wsi branch to local
AbishekRajVG Nov 27, 2023
a5a44e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 27, 2023
18e510f
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Nov 30, 2023
f6604f4
Update mypy-type-check.yml
shaneahmed Nov 30, 2023
d8e7cc2
Improving Zarr and Annotation Store tests in test_engine_abc
AbishekRajVG Dec 1, 2023
1096c31
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 1, 2023
cadad70
Fix lint error
AbishekRajVG Dec 1, 2023
89587a2
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Dec 1, 2023
2173c46
Adding patch_mode argument to the engine_abc.get_dataloader() method
AbishekRajVG Dec 1, 2023
dc5cdee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 1, 2023
5968b98
Reducing complexity in EngineABC as per review comments
AbishekRajVG Dec 15, 2023
d2ed5e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 15, 2023
3528f7c
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Dec 18, 2023
5a293b1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 18, 2023
9344c06
Replacing tolist() with Zarr groups
AbishekRajVG Dec 31, 2023
82ecb35
Merge branch 'dev-update-engine-abc-wsi' of https://github.com/Tissue…
AbishekRajVG Dec 31, 2023
6a8f070
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 31, 2023
07b8d3a
:test_tube: Remove failing test temporarily.
shaneahmed Jan 12, 2024
57162d2
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Jan 12, 2024
f9839db
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Jan 19, 2024
c043032
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Jan 26, 2024
702c558
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Feb 2, 2024
6a360ad
Merge branch 'dev-define-engines-abc' into dev-update-engine-abc-wsi
shaneahmed Feb 2, 2024
00f6d3b
:memo: Create temporary files.
shaneahmed Mar 20, 2024
abe8aab
:twisted_rightwards_arrows: Merge `dev-define-engines-abc` into `dev-…
shaneahmed Mar 20, 2024
71a3e62
Merge remote-tracking branch 'origin/dev-define-engines-abc' into dev…
shaneahmed Mar 20, 2024
b278a13
Merge remote-tracking branch 'refs/remotes/origin/dev-define-engines-…
shaneahmed Apr 26, 2024
d6afe62
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Apr 29, 2024
de58f04
:hammer: Add script for new predictor engine
shaneahmed May 3, 2024
dbdbf7a
:hammer: Add script for new predictor engine
shaneahmed May 3, 2024
e2abc0e
:hammer: Update script for new predictor engine
shaneahmed May 3, 2024
9ef5b54
:hammer: Update script for new predictor engine
shaneahmed May 3, 2024
ea48e1d
:hammer: Update script for new predictor engine
shaneahmed May 3, 2024
14585c1
:fire: Remove redundant files.
shaneahmed May 3, 2024
d2b7a1a
:hammer: Update cache_mode to True in WSI mode.
shaneahmed May 13, 2024
03932b0
:hammer: Add `_run_wsi_mode` to `engine_abc`.
shaneahmed May 13, 2024
1b01dda
:hammer: Add `save_path` for individual WSIs.
shaneahmed May 13, 2024
f2bbb52
:white_check_mark: Update tests
shaneahmed May 13, 2024
f066e19
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed May 13, 2024
59198e8
:technologist: Add dummy tests.
shaneahmed May 13, 2024
72d4b8d
:technologist: Add dummy tests.
shaneahmed May 13, 2024
a4d5d5b
:hammer: Add post_process_wsi as abstractmethod.
shaneahmed May 13, 2024
bc2054c
:test_tube: Update test_io_config_delegation
shaneahmed May 13, 2024
63765e8
:test_tube: Update test_io_config_delegation
shaneahmed May 13, 2024
06d283a
:test_tube: Update test_io_config_delegation
shaneahmed May 13, 2024
a1ffd07
:white_check_mark: Add `test_io_config_delegation`
shaneahmed May 13, 2024
400bfbf
:white_check_mark: Fix first two tests.
shaneahmed May 13, 2024
707a547
:test_tube: Fix tests.
shaneahmed May 13, 2024
73c1ab1
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed May 14, 2024
0288ddb
:white_check_mark: Add tests using multiple WSIs to check if the resu…
shaneahmed May 14, 2024
7f41803
:recycle: Move `test_patch_predictor_new` to `test_patch_predictor`
shaneahmed May 14, 2024
f94a241
Merge branch 'refs/heads/dev-define-engines-abc' into dev-define-patc…
shaneahmed May 14, 2024
be5d7d7
:rewind: Restore github workflow
shaneahmed May 14, 2024
0f071ef
:white_check_mark: Fix `test_patch_predictor_kather100k_output`
shaneahmed May 14, 2024
e0f4b6a
:fire: Remove redundant test for PCAM models.
shaneahmed May 14, 2024
b452067
:fire: Remove redundant test for single file with mask.
shaneahmed May 14, 2024
ec831fb
:recycle: Update cli code for patch_predictor.py
shaneahmed May 15, 2024
dad0a98
:white_check_mark: Fix cli tests
shaneahmed May 15, 2024
323e0a4
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jun 3, 2024
5693d4b
:white_check_mark: Add check for output_type
shaneahmed Jun 6, 2024
38aa74e
:recycle: Use processed predictions instead of raw_ouptut for save_ws…
shaneahmed Jun 6, 2024
3e04b2f
:white_check_mark: Fix error type
shaneahmed Jun 6, 2024
e24d009
:white_check_mark: Add tests for wsi mode for zarr
shaneahmed Jun 6, 2024
687cb94
:pushpin: Pin `pytest` version.
shaneahmed Jun 6, 2024
4b417e1
:pushpin: Pin `pytest` version to less than 8.2.2
shaneahmed Jun 6, 2024
7e95242
:pushpin: Pin `pytest` version to less than 8.2.2
shaneahmed Jun 6, 2024
b353e5f
:pushpin: Pin `pytest` version to less than 8.2.2
shaneahmed Jun 6, 2024
79f9ad5
:fire: Remove failing test
shaneahmed Jun 6, 2024
7e5aa45
:rewind: Revert changes to test_annotation_stores.py
shaneahmed Jun 7, 2024
ea7f45b
:recycle: Fix duplicate filter to avoid caplog errors.
shaneahmed Jun 7, 2024
e7c92bd
:recycle: Remove duplicate filter to avoid caplog errors.
shaneahmed Jun 7, 2024
f6a7f96
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jun 7, 2024
f373f47
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jun 12, 2024
97773eb
:recycle: Replace old `patch_predictor`
shaneahmed Jun 13, 2024
865542b
:bug: Fix `mypy` checks
shaneahmed Jun 13, 2024
141b61d
Merge remote-tracking branch 'origin/dev-define-patchpredictor' into …
shaneahmed Jun 13, 2024
17d4fdd
:test_tube: Add test for AnnotationStore
shaneahmed Jun 14, 2024
8641a37
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jun 18, 2024
7041051
:recycle: Use "probabilities" key for raw output of models.
shaneahmed Jun 20, 2024
b870293
Merge remote-tracking branch 'origin/dev-define-patchpredictor' into …
shaneahmed Jun 20, 2024
c3cba81
:recycle: Use "probabilities" key for raw output of models.
shaneahmed Jun 20, 2024
224dacb
:rewind: Revert unwanted changes.
shaneahmed Jun 20, 2024
34ceeca
:white_check_mark: Add tests for output types in cli
shaneahmed Jun 20, 2024
944e843
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jun 25, 2024
a9bc22f
:rewind: Add duplicate filter.
shaneahmed Jun 25, 2024
7eed2f1
:bug: Fix tests using duplicate filters.
shaneahmed Jun 25, 2024
347c279
:bug: Fix tests using duplicate filters.
shaneahmed Jun 25, 2024
0d1dda9
:recycle: Address review comments
shaneahmed Jul 4, 2024
906c2ae
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jul 5, 2024
059b864
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jul 5, 2024
d02c308
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2024
355dbff
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jul 8, 2024
5cfef61
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jul 10, 2024
5085035
:bug: Fix bug related to default values of resolution and units.
shaneahmed Jul 12, 2024
936eda7
:white_check_mark: Fix test
shaneahmed Jul 17, 2024
0b25bec
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Jul 17, 2024
574fed2
:white_check_mark: Add test for baseline
shaneahmed Jul 17, 2024
b91da56
:bug: Fix class predictions bug
shaneahmed Jul 17, 2024
45a339b
:memo: Update docstring
shaneahmed Jul 18, 2024
055f86b
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Aug 9, 2024
28515c5
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Aug 15, 2024
d773206
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Aug 15, 2024
20362ef
:bug: Fix output path for annotationstore output.
shaneahmed Aug 15, 2024
da0ce4f
:bug: Fix `scale_factor`.
shaneahmed Aug 15, 2024
606a2c0
:art: Update code structure for dataloader_units
shaneahmed Aug 16, 2024
dd712ad
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Aug 20, 2024
fea5be9
:sparkles: Add support for "power" in "scale_factor".
shaneahmed Aug 23, 2024
624e46a
:bug: Use staticmethod.
shaneahmed Aug 23, 2024
9fe0aaa
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Aug 25, 2024
94c2969
Merge branch 'refs/heads/dev-define-engines-abc' into dev-define-patc…
shaneahmed Aug 28, 2024
e4e6f22
:white_check_mark: Add test for objective power.
shaneahmed Aug 28, 2024
f8c65fb
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Sep 18, 2024
5c6d340
Merge branch 'dev-define-engines-abc' into dev-define-patchpredictor
shaneahmed Sep 19, 2024
a513161
[skip ci] :memo: Update docstring
shaneahmed Sep 20, 2024
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
107 changes: 78 additions & 29 deletions tests/engines/test_engine_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import copy
import logging
import shutil
from pathlib import Path
from typing import TYPE_CHECKING, NoReturn
Expand All @@ -11,15 +12,21 @@
import pytest
import torchvision.models as torch_models
import zarr
from typing_extensions import Unpack

from tiatoolbox.models.architecture import (
fetch_pretrained_weights,
get_pretrained_model,
)
from tiatoolbox.models.architecture.vanilla import CNNModel
from tiatoolbox.models.dataset import PatchDataset, WSIPatchDataset
from tiatoolbox.models.engine.engine_abc import EngineABC, prepare_engines_save_dir
from tiatoolbox.models.engine.engine_abc import (
EngineABC,
EngineABCRunParams,
prepare_engines_save_dir,
)
from tiatoolbox.models.engine.io_config import ModelIOConfigABC
from tiatoolbox.utils.misc import write_to_zarr_in_cache_mode

if TYPE_CHECKING:
import torch.nn
Expand Down Expand Up @@ -57,31 +64,38 @@ def get_dataloader(

def save_wsi_output(
self: EngineABC,
raw_output: dict,
processed_output: dict,
save_dir: Path,
**kwargs: dict,
) -> Path:
"""Test post_process_wsi."""
return super().save_wsi_output(
raw_output,
processed_output,
save_dir=save_dir,
**kwargs,
)

def post_process_wsi(
self: EngineABC,
raw_predictions: dict | Path,
**kwargs: Unpack[EngineABCRunParams],
) -> dict | Path:
"""Post process WSI output."""
return super().post_process_wsi(
raw_predictions=raw_predictions,
**kwargs,
)

def infer_wsi(
self: EngineABC,
dataloader: torch.utils.data.DataLoader,
img_label: str,
highest_input_resolution: list[dict],
save_dir: Path,
save_path: Path,
**kwargs: dict,
) -> dict | np.ndarray:
"""Test infer_wsi."""
return super().infer_wsi(
dataloader,
img_label,
highest_input_resolution,
save_dir,
save_path,
**kwargs,
)

Expand Down Expand Up @@ -115,13 +129,34 @@ def test_incorrect_ioconfig() -> NoReturn:
"""Test EngineABC initialization with incorrect ioconfig."""
model = torch_models.resnet18()
engine = TestEngineABC(model=model)

with pytest.raises(
ValueError,
match=r".*provide a valid ModelIOConfigABC.*",
match=r".*Must provide.*`ioconfig`.*",
):
engine.run(images=[], masks=[], ioconfig=None)


def test_incorrect_output_type() -> NoReturn:
"""Test EngineABC for incorrect output type."""
pretrained_model = "alexnet-kather100k"

# Test engine run without ioconfig
eng = TestEngineABC(model=pretrained_model)

with pytest.raises(
TypeError,
match=r".*output_type must be 'dict' or 'zarr' or 'annotationstore*",
):
_ = eng.run(
images=np.zeros((10, 224, 224, 3), dtype=np.uint8),
on_gpu=False,
patch_mode=True,
ioconfig=None,
output_type="random",
)


def test_pretrained_ioconfig() -> NoReturn:
"""Test EngineABC initialization with pretrained model name in the toolbox."""
pretrained_model = "alexnet-kather100k"
Expand All @@ -134,7 +169,7 @@ def test_pretrained_ioconfig() -> NoReturn:
patch_mode=True,
ioconfig=None,
)
assert "predictions" in out
assert "probabilities" in out
assert "labels" not in out


Expand All @@ -153,7 +188,7 @@ def test_ioconfig() -> NoReturn:
ioconfig=ioconfig,
)

assert "predictions" in out
assert "probabilities" in out
assert "labels" not in out


Expand Down Expand Up @@ -260,7 +295,7 @@ def test_engine_initalization() -> NoReturn:
assert isinstance(eng, EngineABC)


def test_engine_run(tmp_path: Path) -> NoReturn:
def test_engine_run(tmp_path: Path, sample_svs: Path) -> NoReturn:
"""Test engine run."""
eng = TestEngineABC(model="alexnet-kather100k")
assert isinstance(eng, EngineABC)
Expand Down Expand Up @@ -316,7 +351,7 @@ def test_engine_run(tmp_path: Path) -> NoReturn:
on_gpu=False,
patch_mode=True,
)
assert "predictions" in out
assert "probabilities" in out
assert "labels" not in out

eng = TestEngineABC(model="alexnet-kather100k")
Expand All @@ -325,7 +360,7 @@ def test_engine_run(tmp_path: Path) -> NoReturn:
on_gpu=False,
verbose=False,
)
assert "predictions" in out
assert "probabilities" in out
assert "labels" not in out

eng = TestEngineABC(model="alexnet-kather100k")
Expand All @@ -334,14 +369,14 @@ def test_engine_run(tmp_path: Path) -> NoReturn:
labels=list(range(10)),
on_gpu=False,
)
assert "predictions" in out
assert "probabilities" in out
assert "labels" in out

eng = TestEngineABC(model="alexnet-kather100k")

with pytest.raises(NotImplementedError):
eng.run(
images=np.zeros(shape=(10, 224, 224, 3)),
images=[sample_svs],
save_dir=tmp_path / "output",
patch_mode=False,
)
Expand All @@ -358,7 +393,7 @@ def test_engine_run_with_verbose() -> NoReturn:
on_gpu=False,
)

assert "predictions" in out
assert "probabilities" in out
assert "labels" in out


Expand Down Expand Up @@ -513,21 +548,28 @@ def test_eng_save_output(tmp_path: pytest.TempPathFactory) -> None:
save_path = tmp_path / "output.zarr"
_ = zarr.open(save_path, mode="w")
out = eng.save_wsi_output(
raw_output=save_path, save_path=save_path, output_type="zarr", save_dir=tmp_path
processed_output=save_path,
save_path=save_path,
output_type="zarr",
save_dir=tmp_path,
)

assert out.exists()
assert out.suffix == ".zarr"

# Test AnnotationStore
patch_output = {
"predictions": [1, 0, 1],
"coordinates": [(0, 0, 1, 1), (1, 1, 2, 2), (2, 2, 3, 3)],
"other": "other",
"predictions": np.array([1, 0, 1]),
"coordinates": np.array([(0, 0, 1, 1), (1, 1, 2, 2), (2, 2, 3, 3)]),
}
class_dict = {0: "class0", 1: "class1"}
save_path = tmp_path / "output_db.zarr"
zarr_group = zarr.open(save_path, mode="w")
_ = write_to_zarr_in_cache_mode(
zarr_group=zarr_group, output_data_to_save=patch_output
)
out = eng.save_wsi_output(
raw_output=patch_output,
processed_output=save_path,
scale_factor=(1.0, 1.0),
class_dict=class_dict,
save_dir=tmp_path,
Expand All @@ -542,28 +584,35 @@ def test_eng_save_output(tmp_path: pytest.TempPathFactory) -> None:
match=r".*supports zarr and AnnotationStore as output_type.",
):
eng.save_wsi_output(
raw_output=save_path,
processed_output=save_path,
save_path=save_path,
output_type="dict",
save_dir=tmp_path,
)


def test_io_config_delegation(tmp_path: Path) -> None:
def test_io_config_delegation(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
"""Test for delegating args to io config."""
# test not providing config / full input info for not pretrained models
model = CNNModel("resnet50")
eng = TestEngineABC(model=model)
with pytest.raises(ValueError, match=r".*Please provide a valid ModelIOConfigABC*"):
eng.run(
np.zeros((10, 224, 224, 3)), patch_mode=True, save_dir=tmp_path / "dump"
)

kwargs = {
"patch_input_shape": [512, 512],
"resolution": 1.75,
"units": "mpp",
}
with caplog.at_level(logging.WARNING):
eng.run(
np.zeros((10, 224, 224, 3)),
patch_mode=True,
save_dir=tmp_path / "dump",
patch_input_shape=kwargs["patch_input_shape"],
resolution=kwargs["resolution"],
units=kwargs["units"],
)
assert "provide a valid ModelIOConfigABC" in caplog.text
shutil.rmtree(tmp_path / "dump", ignore_errors=True)

# test providing config / full input info for non pretrained models
ioconfig = ModelIOConfigABC(
Expand Down
Loading
Loading