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

ADD: sphinx docs #53

Merged
merged 7 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
39 changes: 39 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "wsinfer"
copyright = "2022, Jakub Kaczmarzyk"
author = "Jakub Kaczmarzyk"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
"autoapi.extension",
]

autoapi_type = "python"
autoapi_dirs = ["../wsinfer"]
autoapi_options = [
"members",
"undoc-members",
# "private-members",
"show-inheritance",
"show-module-summary",
"special-members",
"imported-members",
]
templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "pydata_sphinx_theme"
html_static_path = ["_static"]
22 changes: 22 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Welcome to WSInfer!
===================

**WSInfer** is a program to run patch-based classification inference on whole
slide images.

Check out the :doc:`user_guide` for further information, including how to install the
project.

.. toctree::
:maxdepth: 2
:caption: Contents:

user_guide


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
2 changes: 2 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pydata-sphinx-theme
sphinx-autoapi
41 changes: 41 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
User Guide
==========

.. _installation:

Installation
------------

To use WSInfer, first install it using pip:

.. code-block:: console

pip install wsinfer --find-links https://girder.github.io/large_image_wheels

Examples
-------

List available models
^^^^^^^^^^^^^^^^^^^^^

.. code-block:: console

wsinfer list

Run model inference
^^^^^^^^^^^^^^^^^^^

.. code-block:: console

mkdir -p example-wsi-inference/sample-images
cd example-wsi-inference/sample-images
# Download a sample slide.
wget -nc https://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/CMU-1.svs
cd ..
# Run inference on the slide.
CUDA_VISIBLE_DEVICES=0 wsinfer run \
--wsi-dir sample-images/ \
--results-dir results/ \
--model resnet34 \
--weights TCGA-BRCA-v1 \
--num-workers 8
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ dev =
types-Pillow
types-PyYAML
types-tqdm
docs =
pydata-sphinx-theme
sphinx
sphinx-autoapi

[options.entry_points]
console_scripts =
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions wsinfer/cli/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

from ..modellib.run_inference import run_inference
from ..modellib import models
from ..patchlib.create_dense_patch_grid import create_grid_and_save_multi_slides
from ..patchlib.create_patches_fp import create_patches
from .._patchlib.create_dense_patch_grid import create_grid_and_save_multi_slides
from .._patchlib.create_patches_fp import create_patches

PathType = typing.Union[str, Path]

Expand Down
2 changes: 1 addition & 1 deletion wsinfer/cli/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import click

from ..patchlib.create_patches_fp import create_patches as _create_patches
from .._patchlib.create_patches_fp import create_patches as _create_patches


@click.command()
Expand Down
26 changes: 19 additions & 7 deletions wsinfer/modellib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@ def __post_init__(self):
if len(self.class_names) != self.num_classes:
raise ValueError("length of class_names must be equal to num_classes")

@classmethod
def from_yaml(cls, path):
with open(path) as f:
d = yaml.safe_load(f)
@staticmethod
def _validate_input(d) -> None:
"""Raise error if invalid input."""

if not isinstance(d, dict):
raise ValueError("expected YAML config to be a dictionary")
raise ValueError("expected config to be a dictionary")

# Validate contents.
# Validate keys.
Expand Down Expand Up @@ -171,6 +170,14 @@ def from_yaml(cls, path):
if not file.exists():
raise FileNotFoundError(f"'file' not found: {file}")

@classmethod
def from_yaml(cls, path):
"""Create a new instance of Weights from a YAML file."""

with open(path) as f:
d = yaml.safe_load(f)
cls._validate_input(d)

transform = PatchClassification(
resize_size=d["transform"]["resize_size"],
mean=d["transform"]["mean"],
Expand All @@ -189,13 +196,17 @@ def from_yaml(cls, path):
class_names=d["class_names"],
)

def load_model(self):
def load_model(self) -> torch.nn.Module:
"""Return the pytorch implementation of the architecture with weights loaded."""
model = _create_model(name=self.architecture, num_classes=self.num_classes)

# Load state dict.
if self.url and self.url_file_name:
state_dict = load_state_dict_from_url(
url=self.url, check_hash=True, file_name=self.url_file_name
url=self.url,
map_location="cpu",
check_hash=True,
file_name=self.url_file_name,
)
elif self.file:
state_dict = torch.load(self.file, map_location="cpu")
Expand All @@ -209,6 +220,7 @@ def load_model(self):
return model

def get_sha256_of_weights(self) -> str:
"""Return the sha256 of the weights file."""
if self.url and self.url_file_name:
p = Path(torch.hub.get_dir()) / "checkpoints" / self.url_file_name
elif self.file:
Expand Down