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

Allow SlideData to use existing h5path files #337

Open
wants to merge 22 commits into
base: load-data-in-workers
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove private _add_tiles
  • Loading branch information
tddough98 committed Oct 20, 2022
commit 1ea497fcc87f150511b9bb013de9bc24987bac7a
7 changes: 4 additions & 3 deletions pathml/core/h5managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ def remove_tile(self, key):
"""
if not isinstance(key, (str, tuple)):
raise KeyError(f"key must be str or tuple, check valid keys in repr")
if str(key) not in self.h5["tiles"].keys():
raise KeyError(f"key {key} is not in Tiles")
del self.h5["tiles"][str(key)]
if str(key) in self.h5["tiles"].keys():
del self.h5["tiles"][str(key)]
else:
logger.info((f"key {key} is not in Tiles")

def add_mask(self, key, mask):
"""
Expand Down
34 changes: 14 additions & 20 deletions pathml/core/slide_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ def __init__(
_load_from_h5path = True

if backend.lower() == "openslide":
backend_obj = pathml.core.OpenSlideBackend(filepath)
slide = pathml.core.OpenSlideBackend(filepath)
elif backend.lower() == "bioformats":
backend_obj = pathml.core.BioFormatsBackend(filepath, dtype)
slide = pathml.core.BioFormatsBackend(filepath, dtype)
elif backend.lower() == "dicom":
backend_obj = pathml.core.DICOMBackend(filepath)
slide = pathml.core.DICOMBackend(filepath)
elif backend.lower() == "h5path":
backend_obj = None
slide = None
else:
raise ValueError(f"invalid backend: {repr(backend)}.")

self._filepath = filepath if filepath else None
self.backend = backend
self.slide = backend_obj if backend_obj else None
self.slide = slide
self.name = name
self.labels = labels
self.slide_type = slide_type
Expand Down Expand Up @@ -223,8 +223,8 @@ def __init__(

self.masks = pathml.core.Masks(h5manager=self.h5manager, masks=masks)
self.tiles = pathml.core.Tiles(h5manager=self.h5manager, tiles=tiles)
self._add_tiles = tiles is None and not _load_from_h5path

self._generated_tiles = False
self.tile_size = tile_size
self._tile_stride = tile_stride
self.tile_level = tile_level
Expand All @@ -245,7 +245,8 @@ def tile_stride(self, value):
self._tile_stride = value

def get_tiles(self):
if self._add_tiles:
if self.slide is None and not self._generated_tiles:
self._generated_tiles = True
for tile in self._generate_tiles():
yield tile
else:
Expand Down Expand Up @@ -307,9 +308,6 @@ def run(
assert isinstance(
pipeline, pathml.preprocessing.pipeline.Pipeline
), f"pipeline is of type {type(pipeline)} but must be of type pathml.preprocessing.pipeline.Pipeline"
assert (
self.slide is not None or not self._add_tiles
), "cannot run pipeline because self.slide is None and no tiles already exist"

shutdown_after = False

Expand All @@ -329,13 +327,13 @@ def run(
futures, with_results=True, raise_errors=False
):
if future.status == "finished":
if self._add_tiles:
self.tiles.add(result)
self.tiles.add(result)
if future.status == "error":
typ, exc, tb = result
if typ is DropTileException:
# TODO: remove tile if it already is in Tiles
# TODO: figure out how to access tile.coords; need to get input that led to exception...
# TODO: remove existing tiles
# figure out how to access tile.coords from input tile
# self.tiles.remove(tile.coords)
pass
else:
raise exc.with_traceback(tb)
Expand All @@ -362,15 +360,11 @@ def run(
try:
pipeline.apply(tile)
except DropTileException:
if not self._add_tiles:
self.tiles.remove(tile.coords)
if self._add_tiles:
self.tiles.add(tile)
self.tiles.remove(tile.coords)
self.tiles.add(tile)

if write_dir:
self.write(Path(write_dir) / f"{self.name}.h5path")
# Tiles generated after pipeline run
self._add_tiles = False

@property
def shape(self):
Expand Down