Skip to content

♻️ Refactor WSIReader.open#1092

Open
shaneahmed wants to merge 8 commits into
developfrom
restructure-wsireader-open
Open

♻️ Refactor WSIReader.open#1092
shaneahmed wants to merge 8 commits into
developfrom
restructure-wsireader-open

Conversation

@shaneahmed

Copy link
Copy Markdown
Member
  • Move WSIReader.open and all try_* functions to factory.py
  • WSIReader then imports lazily open_wsi from factory.py.
  • Move all is_* functions to detection.py
  • Move WSIReaderParams to types.py
  • Move WSIReader staticmethods for validating image read to factory.py

- Move `WSIReader.open` and all `try_*` functions to `factory.py`
- WSIReader then imports lazily `open_wsi` from `factory.py`.
- Move all `is_*` functions to `detection.py`
- Move `WSIReaderParams` to `types.py`
@shaneahmed shaneahmed requested a review from measty July 3, 2026 14:59
@shaneahmed shaneahmed self-assigned this Jul 3, 2026
@shaneahmed shaneahmed added this to the Release v2.2.0 milestone Jul 3, 2026
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.90%. Comparing base (2ee33aa) to head (93f2efe).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1092      +/-   ##
===========================================
+ Coverage    99.88%   99.90%   +0.01%     
===========================================
  Files           86       89       +3     
  Lines        11661    11687      +26     
  Branches      1531     1532       +1     
===========================================
+ Hits         11648    11676      +28     
+ Misses           7        5       -2     
  Partials         6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors WSI reader selection and format-detection logic by extracting WSIReader.open/try_* creation helpers into a dedicated factory.py, and moving is_* detection helpers into detection.py, while introducing a shared WSIReaderParams TypedDict in types.py.

Changes:

  • Added wsireader/factory.py implementing open_wsi, verify_supported_wsi, and the try_* reader-selection helpers.
  • Added wsireader/detection.py consolidating is_* helper functions (including NGFF/Zarr checks and fsspec JSON validation).
  • Updated WSIReader.open to lazily import and delegate to factory.open_wsi, and updated public imports/tests accordingly.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tiatoolbox/wsicore/wsireader/types.py Introduces WSIReaderParams TypedDict for reader-related kwargs.
tiatoolbox/wsicore/wsireader/factory.py New factory module providing open_wsi and try_* helper functions for reader selection.
tiatoolbox/wsicore/wsireader/detection.py New module centralizing WSI-type detection and validation utilities.
tiatoolbox/wsicore/wsireader/base.py Refactors WSIReader.open to call into the factory; removes inlined detection/selection helpers.
tiatoolbox/wsicore/wsireader/__init__.py Updates public imports to pull detection helpers from detection.py.
tiatoolbox/wsicore/__init__.py Moves WSIReaderParams definition to wsireader/types.py and re-exports it.
tests/test_wsireader.py Updates tests to import/use new factory/detection helpers; adds coverage for new edge cases.
tests/test_tiffreader.py Updates TIFF-related tests to call factory.try_tiff and patch new call sites.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +148 to +150
_, _, suffixes = utils.misc.split_path_name_ext(input_path)
last_suffix = suffixes[-1]

Comment on lines +237 to +242
def try_annotation_store(
input_path: Path,
last_suffix: str,
post_proc: str | callable | None,
kwargs: Unpack[WSIReaderParams],
) -> AnnotationStoreReader | None:
Comment on lines +78 to +81
try:
_ = zarr.open(path, **kwargs, mode="r")
except Exception: # skipcq: PYL-W0703 # noqa: BLE001
return False
Comment on lines +24 to 25
from .detection import is_dicom, is_ngff, is_tiled_tiff, is_url, is_zarr

Comment on lines 39 to 43
"WSIReader",
"_handle_tiff_wsi",
"_handle_virtual_wsi",
"fix_mangled_url_by_pathlib",
"is_dicom",
"is_ngff",
Comment on lines +344 to +346
def fix_mangled_url_by_pathlib(input_path: str | Path) -> str:
"""Fix URl mangled by Path."""
# Fix Mangled URL
Comment on lines +97 to +104
Args:
path (Path):
Path to the file to check.
min_version (Tuple[int, ...]):
Minimum version of the NGFF file to be considered valid.
max_version (Tuple[int, ...]):
Maximum version of the NGFF file to be considered valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants