Skip to content

Commit

Permalink
handled paquo import using global variable HAS_PAQUO
Browse files Browse the repository at this point in the history
  • Loading branch information
swaradgat19 committed Jan 3, 2024
1 parent 99d52a9 commit 540dabf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cython_debug/
/wsinfer/_version.py

#QuPath Project
/QuPathProject
model-outputs-qupath/
*.backup

# Extras
Expand Down
72 changes: 39 additions & 33 deletions wsinfer/qupath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")

0 comments on commit 540dabf

Please sign in to comment.