Running pixel classification JSON as a script in Qupath

Hi, is there a way to run the text in a JSON as a script in Qupath? (I know how to recall the JSON using a script)
I quite often share my scripts and would prefer if they were self contained, would be nice to avoid having to send the relevant JSONs and getting the user to put them in the right place, not a huge deal but would be neat!
Thanks,
Alan

Sounds like that could be handy to encapsulate everything in a single script :slight_smile:

This works for me:

def text = """
{
  "pixel_classifier_type": "OpenCVPixelClassifier",
  "metadata": {
    "inputPadding": 0,
    "inputResolution": {
      "pixelWidth": {
        "value": 14.534223554526047,
        "unit": "µm"
      },
      "pixelHeight": {
        "value": 14.534553630231871,
        "unit": "µm"
      },
      "zSpacing": {
        "value": 1.0,
        "unit": "z-slice"
      },
      "timeUnit": "SECONDS",
      "timepoints": []
    },
    "inputWidth": 512,
    "inputHeight": 512,
    "inputNumChannels": 3,
    "outputType": "CLASSIFICATION",
    "outputChannels": [],
    "classificationLabels": {
      "0": {
        "colorRGB": -12566464
      },
      "1": {
        "name": "Tumor",
        "colorRGB": -3670016
      }
    }
  },
  "op": {
    "type": "data.op.channels",
    "colorTransforms": [
      {
        "stains": {
          "name": "H-DAB default",
          "stain1": {
            "r": 0.6511078297640718,
            "g": 0.7011930397459234,
            "b": 0.2904942598947397,
            "name": "Hematoxylin",
            "isResidual": false
          },
          "stain2": {
            "r": 0.2691668699565374,
            "g": 0.5682411699082456,
            "b": 0.7775931898744414,
            "name": "DAB",
            "isResidual": false
          },
          "stain3": {
            "r": 0.6330435352304447,
            "g": -0.7128599063057326,
            "b": 0.3018056269931407,
            "name": "Residual",
            "isResidual": true
          },
          "maxRed": 255.0,
          "maxGreen": 255.0,
          "maxBlue": 255.0
        },
        "stainNumber": 1
      }
    ],
    "op": {
      "type": "op.core.sequential",
      "ops": [
        {
          "type": "op.gaussian",
          "sigmaX": 2.0,
          "sigmaY": 2.0
        },
        {
          "type": "op.constant",
          "thresholds": [
            0.2
          ]
        }
      ]
    }
  }
}
"""

def classifier = GsonTools.getInstance().fromJson(text, qupath.opencv.ml.pixel.OpenCVPixelClassifier)
createAnnotationsFromPixelClassifier(classifier, 0.0, 0.0)

I vaguely recall there being Groovy trouble whenever scripts are very long – but I’m not certain how long, and hopefully it won’t be an issue.

2 Likes

Superb, thanks Pete!

FYI String parameters can be input in this example by enclosing in triple quotes and plus signs i.e.

String x = 0.3
...
 {
          "type": "op.constant",
          "thresholds": [
            """+ x +"""
          ]
...
1 Like

Actually, in this case I think the triple quotes aren’t so much enclosing but rather breaking up the string and inserting a bit in between.

There’s actually a Groovy way to include variables without requiring that, by using $ or possibly ${}. Here it is:

x = 10
something = """
Here is my variable: $x
Here it is again: ${x}
And here I'm doing something with it: ${x * 2}
And here I've forgotten the curly braces: $x * 2
"""
print something

This should display

Here is my variable: 10
Here it is again: 10
And here I'm doing something with it: 20
And here I've forgotten the curly brackets: 10 * 2

Note that, as far as I recall, this doesn’t work for strings that are defined using 'single quotes'.

2 Likes

Fumbled my way to a solution, good to know how to do it properly! thanks Pete :slight_smile:

2 Likes

To be fair, I’ve only started using the ${} trick very recently, it was all " + " for me before then…