Skip to content

Commit

Permalink
🚨 Fix linter errors in line with #625 (#647)
Browse files Browse the repository at this point in the history
- Fix Linter Errors in line with #625 
- This PR will help review of #625 by merging minor changes from that PR.

----------

Co-authored-by: John Pocock <John-P@users.noreply.github.com>
Co-authored-by: Mark Eastwood <20169086+measty@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 21, 2023
1 parent 7c12443 commit 3715915
Show file tree
Hide file tree
Showing 37 changed files with 223 additions and 104 deletions.
16 changes: 10 additions & 6 deletions pre-commit/notebook_markdown_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import copy
import json
from pathlib import Path
from typing import Any, Dict, List
from typing import Any

import mdformat


def format_notebook(notebook: Dict[str, Any]) -> Dict[str, Any]:
def format_notebook(notebook: dict[str, Any]) -> dict[str, Any]:
"""Format a notebook in MyST style.
Args:
Expand All @@ -36,7 +36,7 @@ def format_notebook(notebook: Dict[str, Any]) -> Dict[str, Any]:
return notebook


def main(files: List[Path]) -> None:
def main(files: list[Path]) -> None:
"""Check markdown cells in notebooks for common mistakes.
Args:
Expand All @@ -54,21 +54,25 @@ def main(files: List[Path]) -> None:
changed = any(
cell != formatted_cell
for cell, formatted_cell in zip(
notebook["cells"], formatted_notebook["cells"]
notebook["cells"],
formatted_notebook["cells"],
)
)
if not changed:
continue
print("Formatting notebook", path)
with open(path, "w") as fh:
with Path.open(path, "w") as fh:
json.dump(formatted_notebook, fh, indent=1, ensure_ascii=False)
fh.write("\n")


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Lint notebook markdown files.")
parser.add_argument(
"files", nargs="*", help="Notebook markdown files to lint.", type=Path
"files",
nargs="*",
help="Notebook markdown files to lint.",
type=Path,
)
args = parser.parse_args()
main(sorted(args.files))
31 changes: 19 additions & 12 deletions pre-commit/notebook_urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Simple check to ensure each code cell in a notebook is valid Python."""
from __future__ import annotations

import argparse
import json
import re
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import List, Set, Tuple


def git_branch_name() -> str:
Expand All @@ -18,7 +19,7 @@ def git_branch_name() -> str:
)


def git_branch_modified_paths(from_ref: str, to_ref: str) -> Set[Path]:
def git_branch_modified_paths(from_ref: str, to_ref: str) -> set[Path]:
"""Get a set of file paths modified on this branch vs develop."""
from_to = f"{from_ref}...{to_ref}"
return {
Expand All @@ -29,20 +30,20 @@ def git_branch_modified_paths(from_ref: str, to_ref: str) -> Set[Path]:
"diff",
"--name-only",
from_to,
]
],
)
.decode()
.strip()
.splitlines()
}


def git_previous_commit_modified_paths() -> Set[Path]:
def git_previous_commit_modified_paths() -> set[Path]:
"""Get a set of file paths modified in the previous commit."""
return {
Path(p)
for p in subprocess.check_output(
["/usr/bin/git", "diff", "--name-only", "HEAD~"]
["/usr/bin/git", "diff", "--name-only", "HEAD~"],
)
.decode()
.strip()
Expand Down Expand Up @@ -72,7 +73,7 @@ class PatternReplacement:
MAIN_BRANCHES = ("master", "main")


def main(files: List[Path], from_ref: str, to_ref: str) -> bool:
def main(files: list[Path], from_ref: str, to_ref: str) -> bool:
"""Check that URLs in the notebook are relative to the current branch.
Args:
Expand Down Expand Up @@ -150,7 +151,7 @@ def main(files: List[Path], from_ref: str, to_ref: str) -> bool:
# Write the file if it has changed
if changed:
print(f"Updating {path}")
with open(path, "w", encoding="utf-8") as fh:
with Path.open(path, "w", encoding="utf-8") as fh:
json.dump(notebook, fh, indent=1, ensure_ascii=False)
fh.write("\n")
else:
Expand All @@ -159,8 +160,10 @@ def main(files: List[Path], from_ref: str, to_ref: str) -> bool:


def check_notebook(
path: Path, to_ref: str, replacements: List[PatternReplacement]
) -> Tuple[bool, dict]:
path: Path,
to_ref: str,
replacements: list[PatternReplacement],
) -> tuple[bool, dict]:
"""Check the notebook for URL replacements.
Args:
Expand All @@ -183,7 +186,7 @@ def check_notebook(
return changed, None

# Load the notebook
with open(path, encoding="utf-8") as fh:
with Path.open(path, encoding="utf-8") as fh:
notebook = json.load(fh)
# Check each cell
for cell_num, cell in enumerate(notebook["cells"]):
Expand All @@ -197,7 +200,7 @@ def check_notebook(
return changed, notebook


def replace_line(line: str, to_ref: str, replacements: List[PatternReplacement]) -> str:
def replace_line(line: str, to_ref: str, replacements: list[PatternReplacement]) -> str:
"""Perform pattern replacements in the line.
Args:
Expand Down Expand Up @@ -230,7 +233,11 @@ def replace_line(line: str, to_ref: str, replacements: List[PatternReplacement])
default=list(Path.cwd().rglob("*.ipynb")),
)
parser.add_argument(
"-f", "--from-ref", help="Reference to diff from", type=str, default="develop"
"-f",
"--from-ref",
help="Reference to diff from",
type=str,
default="develop",
)
parser.add_argument(
"-t",
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from setuptools import find_packages, setup

with open("README.md") as readme_file:
with Path("README.md").open() as readme_file:
readme = readme_file.read()

with open("HISTORY.md") as history_file:
with Path("HISTORY.md").open() as history_file:
history = history_file.read()

install_requires = [
Expand Down
28 changes: 28 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


def pytest_configure(config):
"""Perform initial configuration for TIAToolbox tests."""
logger.info(
"🏁 Starting tests. TIAToolbox Version: %s. CI: %s",
tiatoolbox.__version__,
Expand Down Expand Up @@ -79,6 +80,7 @@ def __remote_sample(key: str) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_ndpi(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for ndpi images.
Download ndpi image for pytest.
"""
Expand All @@ -99,6 +101,7 @@ def sample_ndpi2(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_svs(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for svs images.
Download svs image for pytest.
"""
Expand All @@ -108,6 +111,7 @@ def sample_svs(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_ome_tiff(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for ome-tiff (brightfield pyramid) images.
Download ome-tiff image for pytest.
"""
Expand All @@ -117,6 +121,7 @@ def sample_ome_tiff(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_jp2(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for JP2 images.
Download jp2 image for pytest.
"""
Expand Down Expand Up @@ -183,6 +188,7 @@ def sample_svs_ndpi_wsis(sample_ndpi2, sample_svs, tmpdir_factory):
@pytest.fixture(scope="session")
def source_image(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for source image.
Download stain normalization source image for pytest.
"""
Expand All @@ -192,6 +198,7 @@ def source_image(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def norm_macenko(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for norm_macenko image.
Download norm_macenko image for pytest.
"""
Expand All @@ -201,6 +208,7 @@ def norm_macenko(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def norm_reinhard(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for norm_reinhard image.
Download norm_reinhard image for pytest.
"""
Expand All @@ -210,6 +218,7 @@ def norm_reinhard(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def norm_ruifrok(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for norm_ruifrok image.
Download norm_ruifrok image for pytest.
"""
Expand All @@ -219,6 +228,7 @@ def norm_ruifrok(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def norm_vahadane(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for norm_vahadane image.
Download norm_vahadane image for pytest.
"""
Expand Down Expand Up @@ -256,6 +266,7 @@ def sample_visual_fields(
@pytest.fixture(scope="session")
def patch_extr_vf_image(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for a visual field image.
Download TCGA-HE-7130-01Z-00-DX1 image for pytest.
"""
Expand All @@ -265,6 +276,7 @@ def patch_extr_vf_image(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_csv(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction csv.
Download sample patch extraction csv for pytest.
"""
Expand All @@ -274,6 +286,7 @@ def patch_extr_csv(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_json(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction json.
Download sample patch extraction json for pytest.
"""
Expand All @@ -283,6 +296,7 @@ def patch_extr_json(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_npy(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction npy.
Download sample patch extraction npy for pytest.
"""
Expand All @@ -292,6 +306,7 @@ def patch_extr_npy(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_csv_noheader(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction noheader csv.
Download sample patch extraction noheader csv for pytest.
"""
Expand All @@ -301,6 +316,7 @@ def patch_extr_csv_noheader(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_2col_json(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction 2col json.
Download sample patch extraction 2col json for pytest.
"""
Expand All @@ -310,6 +326,7 @@ def patch_extr_2col_json(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_2col_npy(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction 2col npy.
Download sample patch extraction 2col npy for pytest.
"""
Expand All @@ -319,6 +336,7 @@ def patch_extr_2col_npy(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_jp2_csv(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction jp2 csv.
Download sample patch extraction jp2 csv for pytest.
"""
Expand All @@ -328,6 +346,7 @@ def patch_extr_jp2_csv(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_jp2_read(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction jp2 read npy.
Download sample patch extraction jp2 read npy for pytest.
"""
Expand All @@ -337,6 +356,7 @@ def patch_extr_jp2_read(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_npy_read(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction read npy.
Download sample patch extraction read npy for pytest.
"""
Expand All @@ -346,6 +366,7 @@ def patch_extr_npy_read(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_svs_csv(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction svs csv.
Download sample patch extraction svs csv for pytest.
"""
Expand All @@ -355,6 +376,7 @@ def patch_extr_svs_csv(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_svs_header(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction svs_header csv.
Download sample patch extraction svs_header csv for pytest.
"""
Expand All @@ -364,6 +386,7 @@ def patch_extr_svs_header(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def patch_extr_svs_npy_read(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch extraction svs_read npy.
Download sample patch extraction svs_read npy for pytest.
"""
Expand All @@ -373,6 +396,7 @@ def patch_extr_svs_npy_read(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_patch1(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch 1.
Download sample patch 1 (Kather100K) for pytest.
"""
Expand All @@ -382,6 +406,7 @@ def sample_patch1(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_patch2(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch 2.
Download sample patch 2 (Kather100K) for pytest.
"""
Expand All @@ -391,6 +416,7 @@ def sample_patch2(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_patch3(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch 3.
Download sample patch 3 (PCam) for pytest.
"""
Expand All @@ -400,6 +426,7 @@ def sample_patch3(remote_sample) -> pathlib.Path:
@pytest.fixture(scope="session")
def sample_patch4(remote_sample) -> pathlib.Path:
"""Sample pytest fixture for sample patch 4.
Download sample patch 4 (PCam) for pytest.
"""
Expand All @@ -424,6 +451,7 @@ def dir_sample_patches(sample_patch1, sample_patch2, tmpdir_factory):
@pytest.fixture(scope="session")
def sample_wsi_dict(remote_sample):
"""Sample pytest fixture for torch wsi dataset.
Download svs image for pytest.
"""
Expand Down
Loading

0 comments on commit 3715915

Please sign in to comment.