How do I write scripts?#

What will I learn?

  • What is a script?

  • How to find existing scripts

  • How to create a basic script from scratch

The user interface is the most user-friendly way of working with QuPath. However, it has some limits:

  • Analysis is done by hand and takes some time.

  • It doesn’t allow processing similar images in batch mode.

  • It doesn’t encourage reproducibility in analysis.

QuPath supports scripts as an alternative.

Scripts represent sequences of instructions that are interpreted by QuPath, for example:

  • Setting the image type.

  • Setting the colour deconvolution stains.

  • Applying a cell detection algorithm.

In QuPath, scripts are written in the script editor. You can open it by clicking on Automate ‣ Show script editor.

../../_images/script-editor1.png

Fig. 91 The QuPath script editor#

Groovy#

Scripts are written in Groovy. This is a language similar to Python but for the Java platform.

We created a script containg an overview of the Groovy syntax. This can be useful to have when beginning with Groovy scripting.

Finding existing scripts#

Before writing your own scripts, you can check if the script you want to write already exists. The user forum is a good source for scripts.

Be aware that scripts found on the Internet can be outdated: a script written for an earlier version is not guaranteed to work for the latest version.

What to do

  1. Create an empty folder on your computer.

  2. Open the LuCa-7color_[13860,52919]_1x1component_data.tif image in QuPath.

  3. Create a few rectangle annotations.

  4. Go to this image.sc topic.

  5. The post contains a script that exports all parts of the image located below annotations. Copy the script.

  6. Go to QuPath and click on Automate ‣ Show script editor. This opens up the script editor.

  7. Paste the script.

  8. Change /path/to/some/folder/ to the path of the folder you created on step 1. On Windows, the path should look like C:\Users\User\someFolder. On MacOS, the path should look like /Users/user/someFolder.

  9. Click on Run in the bottom right corner of the script editor. After some time, Done should be printed at the bottom of the script editor.

  10. Check the folder you created on step 1. It should now contain images named 1.ome.tiff, 2.ome.tiff, and so on.

  11. Drag 1.ome.tiff to QuPath to check the first image. It should represent a subset of the original image.

Creating basic scripts#

QuPath has some functions dedicated to scripting in two classes:

  • QP for general functions.

  • QPEx for GUI functions.

These functions should be enough for common use cases.

What to do

  1. Go to QP.

  2. Find a function that returns the name of the current image. You can use Control/Command+F to search for the function.

  3. Open any image in QuPath.

  4. Open the script editor.

  5. Use the function of step 2 to print the name of the current image.

Hint: you can use:

println someFunction()

to print the result of someFunction().

Tip

ChatGPT is not good at generating QuPath scripts.

Asking help#

You can use image.sc if you need help regarding a script (or anything else related to QuPath).

If you embed a script in your post, please surround your code with ```. This adds special formatting that makes it easier for others to copy the script.