Is this the right way to abort a SimpleITK filter?

I am using SimpleITK through C++ (in fact I’m trying to implement an interface to a system that doesn’t support SWIG).

It is unclear to me how to abort a SimpleITK filter. The system I am interfacing with provides a function that I can call to check if an abort is in progress. If the function returns true, I should attempt to abort the computation.

Is the following method correct?

  • add a command that is called on every sitkProgressEvent (is this the best event?)
  • in the Execute member function of the command check if an abort is in progress, and if yes, call the Abort() member function of the ProcessObject (is Abort() meant to be called this way?)

Example code to illustrate what I am currently doing:

class Aborter : public sitk::Command {
    sitk::ProcessObject &proc; // can't make this const due to calling Abort()
public:
    Aborter(sitk::ProcessObject &po) : proc(po)  { }

    virtual void Execute() {
        if (isAbortInProgess()) // this function is provided by the system I'm interfacing with
            proc.Abort();
    }
};

then

    auto filter = sitk::SmoothingRecursiveGaussianImageFilter();
    filter.SetSigma(sigma);
    Aborter cmd(filter);
    filter.AddCommand(sitk::sitkProgressEvent, cmd);
    auto image = filter.Execute(sim.image());
    if (isAbortInProgress())
        // finalize abort on my end, in practice throw an exception
1 Like

That is about right. You may want to monitor “IterationEvent” too. Some filters only report iterations and not progress. You should also monitor the “AbortEvent” to know when the filter is actually trying to abort.

You can find how to fully integrate a SimpleITK filter into a GUI the Slicer SimpleFilters module:

1 Like

Thanks Bradley!

So the best way is to check for an abort both in the progress and the iteration events.

Won’t this have a significant performance impact with some filters? I am hoping that the events are not fired so often that the slowdown would be noticeable. (Note: my command implementation doesn’t do much and I don’t expect it to be slower than the virtual function call itself.)

Also, I’m not interfacing with a GUI, just trying to use some ITK filters from Mathematica. It’s a personal project born out of a specific need. I’m only covering a handful of filters.

You can profile the cost of using the callbacks to determine if they are worth the extra time for you application.