Skip to content

Commit

Permalink
add platformdirs + fix client imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczmarj committed Feb 21, 2024
1 parent f37da2e commit 29b3c02
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies = [
"opencv-python-headless>=4.0.0",
"pandas",
"pillow",
"platformdirs",
"scikit-image>=0.20.0",
"shapely",
# https://github.com/Bayer-Group/tiffslide/issues/72#issuecomment-1631015274
Expand Down
15 changes: 8 additions & 7 deletions wsinfer_mil/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import click
from PIL import Image

from wsinfer_mil.hfclient import load_torchscript_model_from_hf
from wsinfer_mil.client.hfmodel import load_torchscript_model_from_hf
from wsinfer_mil.client.localmodel import load_torchscript_model_from_filesystem
from wsinfer_mil.inference import infer_one_slide
from wsinfer_mil.localclient import load_torchscript_model_from_filesystem

logger = logging.getLogger(__name__)


def _load_tissue_mask(path: str | Path) -> Image.Image:
logger.info(f"Loading tissue mask: {path}")
with Image.open(path) as tissue_mask:
tissue_mask.load()
if tissue_mask.mode != "1":
Expand All @@ -36,7 +37,7 @@ def _load_tissue_mask(path: str | Path) -> Image.Image:

@click.group()
def cli() -> None:
pass
"""Run specimen-level inference using pre-trained models."""


@cli.command()
Expand All @@ -48,15 +49,15 @@ def cli() -> None:
"--wsi-path",
required=True,
help="Path to the whole slide image",
type=click.Path(exists=True, path_type=Path), # type:ignore
type=click.Path(exists=True, path_type=Path),
)
@click.option(
"--hf-repo-revision", help="Revision of the HuggingFace Hub repo", default=None
)
@click.option(
"--tissue-mask-path",
help="Path to the tissue mask for this image",
type=click.Path(exists=True, path_type=Path), # type:ignore
type=click.Path(exists=True, path_type=Path),
default=None,
)
@click.option(
Expand Down Expand Up @@ -97,12 +98,12 @@ def run(
"-i",
"--wsi-path",
help="Path to the whole slide image",
type=click.Path(exists=True, path_type=Path), # type:ignore
type=click.Path(exists=True, path_type=Path),
)
@click.option(
"--tissue-mask-path",
help="Path to the tissue mask for this image",
type=click.Path(exists=True, path_type=Path), # type:ignore
type=click.Path(exists=True, path_type=Path),
default=None,
)
@click.option(
Expand Down
4 changes: 3 additions & 1 deletion wsinfer_mil/defaults.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

from platformdirs import user_cache_dir

# Where we keep all files related to WSInfer MIL.
WSINFER_MIL_DIR = Path.home() / ".wsinfer-mil"
WSINFER_MIL_DIR = Path(user_cache_dir(appname="wsinfer-mil"))

# Cache for tissue masks, patch coordinates, and feature embeddings.
WSINFER_MIL_CACHE_DIR = WSINFER_MIL_DIR / "cache"
Expand Down
11 changes: 2 additions & 9 deletions wsinfer_mil/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@
from torch.utils.data import DataLoader

from wsinfer_mil.cache import Cache
from wsinfer_mil.client.localmodel import Model
from wsinfer_mil.data import WSIPatches
from wsinfer_mil.extractors import get_extractor_by_name
from wsinfer_mil.localclient import Model
from wsinfer_mil.patchlib.patch import patch_tissue
from wsinfer_mil.patchlib.segment import segment_tissue
from wsinfer_mil.quickhash import quickhash

# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger = logging.getLogger(__name__)


@dataclasses.dataclass
Expand Down

0 comments on commit 29b3c02

Please sign in to comment.