Bayer-Group / paquo

PAthological QUpath Obsession - QuPath and Python conversations
GNU General Public License v3.0
103 stars 16 forks source link

Need help in adding a GeoJSON file to an SVS image #104

Closed swaradgat19 closed 1 year ago

swaradgat19 commented 1 year ago

I want to add a geojson file (my model outputs) to an existing svs image in a project. In QuPath, I do it by simply dragging the JSON file on the corresponding image.

I want to perform this for a bunch of svs images in a folder. Currently I'm doing it for a single image. Here is the code for the same: Here, I want to do the paquo equivalent of dragging the CMU-1.json file onto the CMU-1.svs image.

import random
import os
import json

from shapely.geometry import Polygon

from paquo.images import QuPathImageType
from paquo.projects import QuPathProject
from paquo.hierarchy import QuPathPathObjectHierarchy

DIRECTORY = "../wsinfer/wsinfer/SlideImages"

qp = QuPathProject("QuPathProject/", mode='a')

# Adds images to the QuPath Project
filename = f"{DIRECTORY}/CMU-1.svs"
try:
    qp.add_image(f"{filename}", image_type=QuPathImageType.OTHER)
    print(f"{DIRECTORY}/{filename} added successfully!")
except:
    print(f"{DIRECTORY}/{filename} already exists")

# Equivalent of dragging and dropping geojson file to the respective svs file
geojson_DIRECTORY = "../wsinfer/geojson-results/"

filename = "CMU-1.json"
with open(f"{geojson_DIRECTORY}/{filename}") as f:
    detections = json.load(f)

feature_list = detections["features"]
with QuPathProject('QuPathProject/', mode="a") as qp:
    qp.images[0].hierarchy.load_geojson(feature_list)

It gives me the following error:

Traceback (most recent call last):
  File "Gson.java", line 970, in com.google.gson.Gson.fromJson
Exception: Java Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/sggat/Testing/qupath_test.py", line 50, in <module>
    qp.images[-1].hierarchy.load_geojson(feature_list)
  File "/home/sggat/anaconda3/lib/python3.11/site-packages/paquo/hierarchy.py", line 431, in load_geojson
    ao = QuPathPathAnnotationObject.from_geojson(annotation)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sggat/anaconda3/lib/python3.11/site-packages/paquo/pathobjects.py", line 197, in from_geojson
    java_obj = gson.fromJson(String(json.dumps(geojson)), cls.java_class)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
java.lang.java.lang.ClassCastException: java.lang.ClassCastException: Cannot cast qupath.lib.objects.PathTileObject to qupath.lib.objects.PathAnnotationObject

Could you point out where I'm going wrong and how to solve this?

kaczmarj commented 1 year ago

could this be an issue with loading detections vs annotations?

i exported detection tiles to geojson via the qupath gui, and then i tried to add those geojson detections to another qupath project using paquo. i faced the same error as the first post in this issue.

i have included an example geojson to help in debugging.

{
  "features": [
    {
      "geometry": {
        "coordinates": [
          [
            [
              0,
              23904
            ],
            [
              350,
              23904
            ],
            [
              350,
              24254
            ],
            [
              0,
              24254
            ],
            [
              0,
              23904
            ]
          ]
        ],
        "type": "Polygon"
      },
      "properties": {
        "isLocked": true,
        "measurements": {
          "prob_notumor": 0.9998319149017334,
          "prob_tumor": 0.00016804548795334995
        },
        "objectType": "tile"
      },
      "type": "Feature"
    }
  ],
  "type": "FeatureCollection"
}
kaczmarj commented 1 year ago

thank you @ap-- !!

ap-- commented 1 year ago

Thanks for filing the issue and providing an example!

A new release should be on its way.