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!