From 540dabf8073ab8e3abf8618d2c59f9b8006027df Mon Sep 17 00:00:00 2001 From: swaradgat19 Date: Wed, 3 Jan 2024 17:08:48 -0500 Subject: [PATCH] handled paquo import using global variable HAS_PAQUO --- .gitignore | 2 +- wsinfer/qupath.py | 72 +++++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 17e8df1..fb560af 100644 --- a/.gitignore +++ b/.gitignore @@ -173,7 +173,7 @@ cython_debug/ /wsinfer/_version.py #QuPath Project -/QuPathProject +model-outputs-qupath/ *.backup # Extras diff --git a/wsinfer/qupath.py b/wsinfer/qupath.py index a761f94..10f919b 100644 --- a/wsinfer/qupath.py +++ b/wsinfer/qupath.py @@ -4,62 +4,68 @@ import json from pathlib import Path +try: + from paquo.projects import QuPathProject + + HAS_PAQUO = True +except Exception: + HAS_PAQUO = False + def add_image_and_geojson( - qupath_proj, + qupath_proj: QuPathProject, *, image_path: Path | str, geojson_path: Path | str, ) -> None: with open(geojson_path) as f: - # FIXME: check that a 'features' key is present and raise a useful error if not - geojson_features = json.load(f)["features"] + try: + geojson_features = json.load(f)["features"] + except Exception as e: + print(f"Unable to find features key:: {e}") entry = qupath_proj.add_image(image_path) - # FIXME: test that the 'load_geojson' function exists. If not, raise a useful error try: - entry.hierarchy.load_geojson(geojson_features) # type: ignore + entry.hierarchy.load_geojson(geojson_features) except Exception as e: print(f"Failed to run load_geojson function with error:: {e}") -def make_qupath_project(wsi_dir, results_dir): - try: - from paquo.projects import QuPathProject - except Exception as e: +def make_qupath_project(wsi_dir: Path, results_dir: Path) -> None: + if not HAS_PAQUO: print( - f"""Cannot find QuPath with error:: {e}. + """Cannot find QuPath. QuPath is required to use this functionality but it cannot be found. If QuPath is installed, please use define the environment variable PAQUO_QUPATH_DIR with the location of the QuPath installation. If QuPath is not installed, please install it from https://qupath.github.io/.""" ) sys.exit(1) + else: + print("Found QuPath successfully!") + QUPATH_PROJECT_DIRECTORY = results_dir / "model-outputs-qupath" - print("Found QuPath successfully!") - QUPATH_PROJECT_DIRECTORY = results_dir / "model-outputs-qupath" - - csv_files = list((results_dir / "model-outputs-csv").glob("*.csv")) - slides_and_geojsons = [] + csv_files = list((results_dir / "model-outputs-csv").glob("*.csv")) + slides_and_geojsons = [] - for csv_file in csv_files: - file_name = csv_file.stem + for csv_file in csv_files: + file_name = csv_file.stem - json_file = results_dir / "model-outputs-geojson" / (file_name + ".json") - image_file = wsi_dir / (file_name + ".svs") + json_file = results_dir / "model-outputs-geojson" / (file_name + ".json") + image_file = wsi_dir / (file_name + ".svs") - if json_file.exists() and image_file.exists(): - matching_pair = (image_file, json_file) - slides_and_geojsons.append(matching_pair) - else: - print(f"Skipping CSV: {csv_file.name} (No corresponding JSON)") + if json_file.exists() and image_file.exists(): + matching_pair = (image_file, json_file) + slides_and_geojsons.append(matching_pair) + else: + print(f"Skipping CSV: {csv_file.name} (No corresponding JSON)") - with QuPathProject(QUPATH_PROJECT_DIRECTORY, mode="w") as qp: - for image_path, geojson_path in slides_and_geojsons: - try: - add_image_and_geojson( - qp, image_path=image_path, geojson_path=geojson_path - ) - except Exception as e: - print(f"Failed to add image/geojson with error:: {e}") - print("Successfully created QuPath Project!") + with QuPathProject(QUPATH_PROJECT_DIRECTORY, mode="w") as qp: + for image_path, geojson_path in slides_and_geojsons: + try: + add_image_and_geojson( + qp, image_path=image_path, geojson_path=geojson_path + ) + except Exception as e: + print(f"Failed to add image/geojson with error:: {e}") + print("Successfully created QuPath Project!")