# Is this the right way to abort a SimpleITK filter?

**URL:** <https://discourse.itk.org/t/is-this-the-right-way-to-abort-a-simpleitk-filter/1250>\
**Category:** Beginner Questions\
**Tags:** simpleitk\
**Created:** [September 4, 2018, 4:02pm UTC](https://discourse.itk.org/t/is-this-the-right-way-to-abort-a-simpleitk-filter/1250 "2018-09-04T16:02:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![szhorvat](https://discourse.itk.org/user_avatar/discourse.itk.org/szhorvat/32/384_2.png) [@szhorvat](https://discourse.itk.org/u/szhorvat)\
**Post date:** [September 4, 2018, 4:02pm UTC](https://discourse.itk.org/t/is-this-the-right-way-to-abort-a-simpleitk-filter/1250/1 "2018-09-04T16:02:41Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [September 4, 2018, 4:17pm UTC](https://discourse.itk.org/t/is-this-the-right-way-to-abort-a-simpleitk-filter/1250/2 "2018-09-04T16:17:41Z")

</div>

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:

> <https://github.com/SimpleITK/SlicerSimpleFilters/blob/92cbe91774350aca201b01de91ec1fa265173789/SimpleFilters/SimpleFilters.py#L416>

---

<div class="post-metadata">

**Author:** ![szhorvat](https://discourse.itk.org/user_avatar/discourse.itk.org/szhorvat/32/384_2.png) [@szhorvat](https://discourse.itk.org/u/szhorvat)\
**Post date:** [September 4, 2018, 4:25pm UTC](https://discourse.itk.org/t/is-this-the-right-way-to-abort-a-simpleitk-filter/1250/3 "2018-09-04T16:25:54Z")

</div>

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](http://wolfram.com/). It’s a personal project born out of a specific need. I’m only covering a handful of filters.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [September 4, 2018, 4:59pm UTC](https://discourse.itk.org/t/is-this-the-right-way-to-abort-a-simpleitk-filter/1250/4 "2018-09-04T16:59:24Z")

</div>

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