Skip to content

Commit

Permalink
Merge pull request #226 from Dana-Farber-AIOS/myFeature
Browse files Browse the repository at this point in the history
added write functionality within run with optional flag, defaulting t…
  • Loading branch information
ryanccarelli committed Nov 12, 2021
2 parents 0edc110 + 30cfc9e commit 1308b58
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A toolkit for computational pathology and machine learning.

**View [documentation](https://pathml.readthedocs.io/en/latest/)**

**Please cite [our paper](https://www.biorxiv.org/content/10.1101/2021.10.21.465212v1)**
**Please cite [our paper](https://www.biorxiv.org/content/10.1101/2021.10.21.465212)**

# Installation

Expand Down
7 changes: 7 additions & 0 deletions pathml/core/slide_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def run(
level=0,
tile_pad=False,
overwrite_existing_tiles=False,
write_dir=None,
):
"""
Run a preprocessing pipeline on SlideData.
Expand All @@ -269,6 +270,9 @@ def run(
Defaults to ``False``.
overwrite_existing_tiles (bool): Whether to overwrite existing tiles. If ``False``, running a pipeline will
fail if ``tiles is not None``. Defaults to ``False``.
write_dir (str): Path to directory to write the processed slide to. The processed SlideData object
will be written to the directory immediately after the pipeline has completed running.
The filepath will default to "<write_dir>/<slide.name>.h5path. Defaults to ``None``.
"""
assert isinstance(
pipeline, pathml.preprocessing.pipeline.Pipeline
Expand Down Expand Up @@ -320,6 +324,9 @@ def run(
pipeline.apply(tile)
self.tiles.add(tile)

if write_dir:
self.write(Path(write_dir) / f"{self.name}.h5path")

@property
def shape(self):
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/core_tests/test_slide_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,23 @@ def compare_dict_ignore_order(d1, d2):
if d1[k1] != d2[k2]:
return False
return True


@pytest.mark.parametrize("write", [True, False])
def test_run_and_write(tmpdir, write):
wsi = HESlide("tests/testdata/small_HE.svs", backend="openslide", name="testwrite")
pipe = Pipeline()

if write:
write_dir_arg = tmpdir
else:
write_dir_arg = None

wsi.run(pipe, tile_size=500, distributed=False, write_dir=write_dir_arg)

written_path = tmpdir / "testwrite.h5path"

if write:
assert written_path.isfile()
else:
assert not written_path.isfile()
21 changes: 20 additions & 1 deletion tests/core_tests/test_slide_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
License: GNU GPL 2.0
"""

from dask.distributed import Client
import pytest
from pathlib import Path

from pathml.core import SlideData, Tile
Expand Down Expand Up @@ -38,3 +38,22 @@ def test_run_pipeline_and_tile_dataset_and_reshape(slide_dataset):
tile_after_reshape = slide_dataset[0].tiles[0]
assert isinstance(tile_after_reshape, Tile)
assert tile_after_reshape.image.shape == (25, 25, 3)


@pytest.mark.parametrize("write", [True, False])
def test_run_and_write_dataset(tmpdir, write, slide_dataset):
pipe = Pipeline()

if write:
write_dir_arg = tmpdir
else:
write_dir_arg = None

slide_dataset.run(pipe, tile_size=500, distributed=False, write_dir=write_dir_arg)

for s in slide_dataset:
written_path = tmpdir / f"{s.name}.h5path"
if write:
assert written_path.isfile()
else:
assert not written_path.isfile()

0 comments on commit 1308b58

Please sign in to comment.