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

Updates to integrate new OPM APIs #261

Merged
merged 10 commits into from
Dec 27, 2021
2 changes: 1 addition & 1 deletion GANDLF/OPM
47 changes: 19 additions & 28 deletions gandlf_patchMiner
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,23 @@ import argparse
import pandas as pd
import openslide
import numpy as np
import yaml

from PIL import Image
from GANDLF.OPM.opm.patch_manager import PatchManager
from GANDLF.OPM.opm.utils import tissue_mask, alpha_channel_check, patch_size_check, parse_config
from GANDLF.OPM.opm.utils import (
tissue_mask,
alpha_channel_check,
patch_size_check,
parse_config,
generate_initial_mask,
)
from functools import partial
from pathlib import Path

Image.MAX_IMAGE_PIXELS = None
warnings.simplefilter("ignore")


def generate_initial_mask(slide_path, scale):
"""
Helper method to generate random coordinates within a slide
:param slide_path: Path to slide (str)
:param num_patches: Number of patches you want to generate
:return: list of n (x,y) coordinates
"""
# Open slide and get properties
slide = openslide.open_slide(slide_path)
slide_dims = slide.dimensions

# Call thumbnail for effiency, calculate scale relative to whole slide
slide_thumbnail = np.asarray(
slide.get_thumbnail((slide_dims[0] // scale, slide_dims[1] // scale))
)
real_scale = (
slide_dims[0] / slide_thumbnail.shape[1],
slide_dims[1] / slide_thumbnail.shape[0],
)

return tissue_mask(slide_thumbnail), real_scale


def parse_gandlf_csv(fpath):
df = pd.read_csv(fpath, dtype=str)
df = df.drop_duplicates()
Expand Down Expand Up @@ -80,13 +62,18 @@ if __name__ == "__main__":
"--config",
type=str,
dest="config",
help="config.yml for running OPM. ",
required=True,
help="config (in YAML) for running OPM. Needs 'scale' and 'patch_size' to be defined, otherwise defaults to 16 and (256, 256), respectively.",
required=False,
)

args = parser.parse_args()

cfg = parse_config(args.config)
if args.config is not None:
cfg = parse_config(args.config)
else:
cfg = {}
cfg["scale"] = 16
cfg["patch_size"] = (256, 256)

if not os.path.exists(args.output_path):
Path(args.output_path).mkdir(parents=True, exist_ok=True)
Expand All @@ -102,6 +89,8 @@ if __name__ == "__main__":
manager = PatchManager(slide, args.output_path)
manager.set_label_map(label)
manager.set_subjectID(sid)
manager.set_image_header("Channel_0")
manager.set_mask_header("Label")

# Generate an initial validity mask
mask, scale = generate_initial_mask(slide, cfg["scale"])
Expand All @@ -119,3 +108,5 @@ if __name__ == "__main__":
# Save patches releases saves all patches stored in manager, dumps to specified output file
manager.mine_patches(output_csv=out_csv_path, config=cfg)
print("Total time: {}".format(time.time() - start))

print("Finished.")
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"psutil",
"medcam",
"torchmetrics",
"OpenPatchMiner==0.1.6",
]

setup(
Expand Down