-
Notifications
You must be signed in to change notification settings - Fork 102
⚡ Make WSIPatchDataset Pickleable to Support Windows Multithreading #947
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,7 +163,7 @@ class WSIPatchDataset(dataset_abc.PatchDatasetABC): | |
|
|
||
| """ | ||
|
|
||
| def __init__( # skipcq: PY-R1000 # noqa: PLR0915 | ||
| def __init__( # skipcq: PY-R1000 | ||
| self: WSIPatchDataset, | ||
| img_path: str | Path, | ||
| mode: str = "wsi", | ||
|
|
@@ -262,40 +262,17 @@ def __init__( # skipcq: PY-R1000 # noqa: PLR0915 | |
| raise ValueError(msg) | ||
|
|
||
| self.preproc_func = preproc_func | ||
| img_path = Path(img_path) | ||
| if mode == "wsi": | ||
| self.reader = WSIReader.open(img_path) | ||
| else: | ||
| logger.warning( | ||
| "WSIPatchDataset only reads image tile at " | ||
| '`units="baseline"` and `resolution=1.0`.', | ||
| stacklevel=2, | ||
| ) | ||
| img = imread(img_path) | ||
| axes = "YXS"[: len(img.shape)] | ||
| # initialise metadata for VirtualWSIReader. | ||
| # here, we simulate a whole-slide image, but with a single level. | ||
| # ! should we expose this so that use can provide their metadata ? | ||
| metadata = WSIMeta( | ||
| mpp=np.array([1.0, 1.0]), | ||
| axes=axes, | ||
| objective_power=10, | ||
| slide_dimensions=np.array(img.shape[:2][::-1]), | ||
| level_downsamples=[1.0], | ||
| level_dimensions=[np.array(img.shape[:2][::-1])], | ||
| ) | ||
| # infer value such that read if mask provided is through | ||
| # 'mpp' or 'power' as varying 'baseline' is locked atm | ||
| self.img_path = Path(img_path) | ||
| self.mode = mode | ||
| self.reader = None | ||
| reader = self._get_reader(self.img_path) | ||
| if mode != "wsi": | ||
| units = "mpp" | ||
| resolution = 1.0 | ||
| self.reader = VirtualWSIReader( | ||
| img, | ||
| info=metadata, | ||
| ) | ||
|
|
||
| # may decouple into misc ? | ||
| # the scaling factor will scale base level to requested read resolution/units | ||
| wsi_shape = self.reader.slide_dimensions(resolution=resolution, units=units) | ||
| wsi_shape = reader.slide_dimensions(resolution=resolution, units=units) | ||
|
|
||
| # use all patches, as long as it overlaps source image | ||
| self.inputs = PatchExtractor.get_coordinates( | ||
|
|
@@ -316,13 +293,13 @@ def __init__( # skipcq: PY-R1000 # noqa: PLR0915 | |
| mask = np.array(mask > 0, dtype=np.uint8) | ||
|
|
||
| mask_reader = VirtualWSIReader(mask) | ||
| mask_reader.info = self.reader.info | ||
| mask_reader.info = reader.info | ||
| elif auto_get_mask and mode == "wsi" and mask_path is None: | ||
| # if no mask provided and `wsi` mode, generate basic tissue | ||
| # mask on the fly | ||
| mask_reader = self.reader.tissue_mask(resolution=1.25, units="power") | ||
| mask_reader = reader.tissue_mask(resolution=1.25, units="power") | ||
| # ? will this mess up ? | ||
| mask_reader.info = self.reader.info | ||
| mask_reader.info = reader.info | ||
|
|
||
| if mask_reader is not None: | ||
| selected = PatchExtractor.filter_coordinates( | ||
|
|
@@ -344,10 +321,44 @@ def __init__( # skipcq: PY-R1000 # noqa: PLR0915 | |
| # Perform check on the input | ||
| self._check_input_integrity(mode="wsi") | ||
|
|
||
| def _get_reader(self: WSIPatchDataset, img_path: str | Path) -> WSIReader: | ||
shaneahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Get a reader for the image.""" | ||
| if self.mode == "wsi": | ||
| reader = WSIReader.open(img_path) | ||
| else: | ||
| logger.warning( | ||
| "WSIPatchDataset only reads image tile at " | ||
| '`units="baseline"` and `resolution=1.0`.', | ||
| stacklevel=2, | ||
| ) | ||
| img = imread(img_path) | ||
| axes = "YXS"[: len(img.shape)] | ||
| # initialise metadata for VirtualWSIReader. | ||
| # here, we simulate a whole-slide image, but with a single level. | ||
| # ! should we expose this so that use can provide their metadata ? | ||
| metadata = WSIMeta( | ||
| mpp=np.array([1.0, 1.0]), | ||
| axes=axes, | ||
| objective_power=10, | ||
| slide_dimensions=np.array(img.shape[:2][::-1]), | ||
| level_downsamples=[1.0], | ||
| level_dimensions=[np.array(img.shape[:2][::-1])], | ||
| ) | ||
| # infer value such that read if mask provided is through | ||
| # 'mpp' or 'power' as varying 'baseline' is locked atm | ||
| reader = VirtualWSIReader( | ||
| img, | ||
| info=metadata, | ||
| ) | ||
| return reader | ||
|
|
||
| def __getitem__(self: WSIPatchDataset, idx: int) -> dict: | ||
| """Get an item from the dataset.""" | ||
| coords = self.inputs[idx] | ||
| # Read image patch from the whole-slide image | ||
| if self.reader is None: | ||
|
||
| # only set the reader on first call so that it is initially picklable | ||
| self.reader = self._get_reader(self.img_path) | ||
| patch = self.reader.read_bounds( | ||
| coords, | ||
| resolution=self.resolution, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a temporary reader object during initialization defeats the purpose of lazy initialization. This reader is only used to get slide dimensions but will be discarded, causing unnecessary overhead. Consider caching the slide dimensions or refactoring to avoid creating the reader twice.