I like writing scripts for QuPath. For me, it’s the best part.

One of the purposes of this blog is to try to help more people use QuPath in more inventive ways - and maybe even contribute to its development.

Another is for me to encourage myself to write up some of the design ideas and features - both to help straighten my own thoughts, and potentially to provoke discussion that helps make the software better.

So I figured that a regular series describing and demonstrating scripts might help in both cases. The plan is to:

  1. Show the kinds of things you can do with QuPath
  2. Add some new features that could be generally useful (which may or may not make it into the main software later)
  3. Explain the general concepts & design of QuPath
  4. Help demystify the process of creating new scripts

The length of the series will be an as-yet-unknown function of how useful it is, and both the time and enthusiasm I have for it.

I’ll aim to manage at least week of daily scripts, because I’m impatient to get started. After that it’ll probably slow down pretty quickly to a more sustainable pace.

Why scripts?

Scripts give a way to customize QuPath quickly and (arguably) easily, adding new features or interrogating data in new ways. They are especially handy for batch processing, and don’t require changing or updating QuPath itself (which is a much harder job).

Scripting Groovily

To get started, check out the Writing custom scripts section on the QuPath wiki.

Groovy is the programming language used for writing QuPath scripts. It’s very Java-like - so if you know any Java at all, you can get started with Groovy immediately. But once you get used to Groovy’s many handy tricks and shortcuts, you can write much shorter code more quickly.

For me at least, I find trial and error in a state of desperation and frustration to be the best (only?) way to pick up many important new skills. And reading scripts other people have written, trying to understand them, and tweaking them a bit is a good way to learn. So in that spirit, if you’re completely new to any kind of computer programming you can just dive straight in and try to pick up whatever Groovy you need for QuPath scripting as you go along.

Still, it’s a good idea to always be on the look out for things not behaving quite as you expect. If you like, you can sanity-check your custom QuPath scripts by posting them online (e.g. here or on Google Groups and asking for feedback.

On the other hand, if you have the time and motivation to be more systematic, there are plenty of books and online resources to teach Groovy, and even more for Java. If you prefer more strict rules in your life, you might prefer to learn a bit of Java and then switch to Groovy later (which is the way I did it); the syntax is so similar that Java code typically needs little or no modification to work in Groovy.

In any case, the focus here is on the QuPath-specific stuff, which isn’t described elsewhere. But there will be some extra Groovy explanations along the way.

Scripting Intellijently

Some magical people can write scripts and software by just typing code happily in a text editor and then running it. For the rest of us, help is available in the form of ‘Integrated Development Environments’ (IDEs).

For any moderately complicated script, the built-in Script editor in QuPath isn’t really enough. You can use it to run your script, but it doesn’t give much help for you to write it in the first place. An IDE can really give assistance.

If you aren’t familiar with IDEs, you can think of them as providing you with an army of faceless, more insightful, less obtrusive, mind-reading Clippys. Or, if you didn’t like Clippy, think of them as something else. But certainly consider using one.

Personally, I find IntelliJ to be wonderfully useful, especially for Groovy.

Instructions for setting up IntelliJ and QuPath together are here.