Import *.json files in qupath

Hi, trying to import *.json files in qupath using this script (QuPathGeoJSONImportExport/qupath_import.groovy at 09f4c53249abe14028b5af2e61377d2eeadb4d86 · choosehappy/QuPathGeoJSONImportExport · GitHub).

Created a qupath project with these *.svs files:

Then tried importing this *.json file (Databases in MS COCO (json) format) in qupath using the script from the link above, but I get several error messages (se below). Has anyone tried importing *.json files in qupath and have an idea how to make this work? I realize the solution is probably not as straight forward as my naive copy/paste approach above:)

def gson = GsonTools.getInstance(true)
def json = new File("C:/QuPath/Mitosis_canine_breast_270421/MITOS_WSI_CMC_ODAEL.json").text

// Read the annotations
def type = new com.google.gson.reflect.TypeToken<List<QuPath.lib.objects.PathObject>>() {}.getType()
def deserializedAnnotations = gson.fromJson(json, type)
addObjects(deserializedAnnotations) 



ERROR: It looks like you have tried to import a class 'QuPath.lib.objects.PathObject' that doesn't exist!
You should probably remove the broken import statement in your script (around line 6).
Then you may want to check 'Run -> Include default imports' is selected, or alternatively add 
    import qupath.lib.objects.PathObject
at the start of the script. Full error message below.

ERROR: MultipleCompilationErrorsException at line 5: startup failed:
Script19.groovy: 6: unable to resolve class QuPath.lib.objects.PathObject
 @ line 6, column 55.
   e.gson.reflect.TypeToken<List<QuPath.lib
                                 ^

Script19.groovy: 6: unable to resolve class QuPath.lib.objects.PathObject
 @ line 6, column 55.
   e.gson.reflect.TypeToken<List<QuPath.lib
                                 ^

Script19.groovy: 6: unable to resolve class QuPath.lib.objects.PathObject
 @ line 6, column 55.
   e.gson.reflect.TypeToken<List<QuPath.lib
                                 ^

Script19.groovy: 6: unable to resolve class QuPath.lib.objects.PathObject
 @ line 6, column 55.
   e.gson.reflect.TypeToken<List<QuPath.lib
                                 ^

4 errors


ERROR: org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:287)
    org.codehaus.groovy.control.CompilationUnit$ISourceUnitOperation.doPhaseOperation(CompilationUnit.java:893)
    org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:623)
    groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:389)
    groovy.lang.GroovyClassLoader.lambda$parseClass$3(GroovyClassLoader.java:332)
    org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163)
    org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154)
    groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:330)
    groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:314)
    groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:257)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:336)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:153)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:926)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:859)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:782)
    qupath.lib.gui.scripting.DefaultScriptEditor$2.run(DefaultScriptEditor.java:1271)
    java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.base/java.lang.Thread.run(Unknown Source)

the capitalization is wrong – it should be

qupath.lib.objects.PathObject

as in the original script.

Thanks, Pete! Still get an error message, though. Any idea how this can be done?

def gson = GsonTools.getInstance(true)
def json = new File("C:/QuPath/Mitosis_canine_breast_270421/MITOS_WSI_CMC_ODAEL.json").text

// Read the annotations
def type = new com.google.gson.reflect.TypeToken<List<qupath.lib.objects.PathObject>>() {}.getType()
def deserializedAnnotations = gson.fromJson(json, type)
addObjects(deserializedAnnotations)   

ERROR: IllegalStateException at line 6: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

ERROR: com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
com.google.gson.Gson.fromJson(Gson.java:932)
com.google.gson.Gson.fromJson(Gson.java:897)
com.google.gson.Gson.fromJson(Gson.java:846)
com.google.gson.Gson$fromJson.call(Unknown Source)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
Script22.run(Script22.groovy:7)
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:926)
qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:859)
qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:782)
qupath.lib.gui.scripting.DefaultScriptEditor$2.run(DefaultScriptEditor.java:1271)
java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
java.base/java.util.concurrent.FutureTask.run(Unknown Source)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.base/java.lang.Thread.run(Unknown Source)

Try this code snippet

EDIT: too late to the party! Anyway… I get the error you mentioned sometimes when the JSON text doesn’t start with “[” and ends with “]”

2 Likes

Hmm, any way to edit the json file (some editing tool)?

Yes, the contents of C:/QuPath/Mitosis_canine_breast_270421/MITOS_WSI_CMC_ODAEL.json would be needed to decipher the error. QuPath v0.3 will try harder to parse different potentially valid inputs, but the script here assumes you have a JSON array of Feature objects. A file might contain GeoJSON in some other way, depending upon how it was written.

2 Likes

MITOS_WSI_CMC_ODAEL.zip (713.8 KB)

Thanks, I’m not sure that file contains GeoJSON. At least, it contains a lot of other info and would require custom parsing – the GeoJSON script won’t work.

(Since the JSON file doesn’t use pretty printing, and my Atom crashed when I tried to open it, it’s hard to explore quickly)

1 Like

The *.json file looks like this in Atom (see below). I can see it starts with “{” - @melvingelbard have you added a “[” and “]” to the start and end of the file successfully in the past?

Beginning of file:

End of file:

Unfortunately I think @petebankhead is right, this does not quite look like GeoJSON… Adding a [ and ] might make your code run without errors but I doubt it will understand what the file actually contains.

You might want to write a custom parsing method yourself, but it might not be very straightforward (also the file is huge and makes my PC freeze…)

1 Like

Yes, I think you’re right. Looked at the file in more detail and probably should be possible to parse out the interesting pieces and put them in a readable json format based on the json export script I guess, but quite a big job:) Might try the “Sliderunner” approach suggested in the paper. Thanks for your input, both of you!

1 Like