# ITK: ResampleImageFilter and ImageFileWriter compatible with dask?

**URL:** <https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710>\
**Category:** Engineering\
**Tags:** dask, itk, python\
**Created:** [February 15, 2023, 11:08pm UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710 "2023-02-15T23:08:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keyn34](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@Keyn34](https://discourse.itk.org/u/Keyn34)\
**Post date:** [February 15, 2023, 11:08pm UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/1 "2023-02-15T23:08:38Z")

</div>

Hello everyone,

I hope that’s the correct category.  
I wanted to ask if someone can give me a hint or info on if ITK’s ResampleImageFilter and ImageFileWriter are compatible with [dask](https://github.com/dask/dask) and if someone has tested that so far.  
[This blog entry](https://blog.dask.org/2019/08/09/image-itk) explains an example using a deconvolution filter, but I doubt that resampling and especially writing are not comparable with such filters.

Thanks, as usual!

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [February 16, 2023, 3:04am UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/2 "2023-02-16T03:04:36Z")

</div>

Hi @Keyn34 ,

Yes, dask capabilities were significantly improved in `itk` 5.3.0. For more information, see [the 5.3 RC 4 release notes](https://github.com/InsightSoftwareConsortium/ITK/releases/tag/v5.3rc04).

It is optional but nice :-); the [`itk` Pythonic API](https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html) to ResampleImageFilter and ImageFileWriter are `itk.resample_image_filter` and `itk.imwrite`.

---

<div class="post-metadata">

**Author:** ![Keyn34](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@Keyn34](https://discourse.itk.org/u/Keyn34)\
**Post date:** [February 16, 2023, 8:57am UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/3 "2023-02-16T08:57:14Z")

</div>

Hello @matt.mccormick,

Thank you. I read the release notes, but I was unsure if those excellent improvements would also affect resampling an image or writing. Those changes are already live in the latest version; is that correct?

Thank you for the hint about the pythonic API. I sometimes get a bit confused about which route to go, switching between SimpleITK, ITK and also C++ and Python. 😃

Looking forward to trying it out now. 🙂

---

<div class="post-metadata">

**Author:** ![Keyn34](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@Keyn34](https://discourse.itk.org/u/Keyn34)\
**Post date:** [February 19, 2023, 10:16pm UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/4 "2023-02-19T22:16:16Z")

</div>

Hello @matt.mccormick, sorry to tag you again here. I would have a question concerning the parallel processing of chunks of one single image loaded with ITK. Would it make sense to take [this example](https://examples.itk.org/src/io/imagebase/processimagechunks/documentation) and process multiple chunks of one image in parallel by using dask? Especially the resampling that I mentioned earlier, and maybe also writing.

Or, is parallel processing where it makes sense already handled within ITK when working on single images? It would be interesting to have some more insight into that. Maybe there is documentation that you can suggest?

Again, sorry for the second tag, but I got very curious after trying ITK with dask. 🙂  
I appreciate any help you can provide.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [February 20, 2023, 2:15pm UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/5 "2023-02-20T14:15:40Z")

</div>

Most ITK filters are already parallel. That example is meant to show some custom per/chunk processing, while also reading/writing only one chunk at a time. The file format must support chunked reading/writing for this to work as intended.

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [February 21, 2023, 3:26pm UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/6 "2023-02-21T15:26:51Z")

</div>

Hi @Keyn34 ,

Great questions. Yes, resampling in parallel on chunks can be achieved via `itk` / `dask`. I will work on a post that describes the details 👍 .

- Matt

---

<div class="post-metadata">

**Author:** ![Keyn34](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@Keyn34](https://discourse.itk.org/u/Keyn34)\
**Post date:** [March 8, 2023, 5:07pm UTC](https://discourse.itk.org/t/itk-resampleimagefilter-and-imagefilewriter-compatible-with-dask/5710/7 "2023-03-08T17:07:49Z")

</div>

Hi @matt.mccormick,

Excellent that you like the idea and are willing to write a post about it! I am looking forward to it.

In the meantime, I tried to implement a resampler with `dask` and resorted to `scipy` as a resampling strategy. The sample code is below. Maybe someone might find it useful.

```auto
def resample_scipy(image_chunk, input_spacing: list, output_spacing: list = [1.5, 1.5, 1.5]):
    input_size = image_chunk.shape
    zoom_factors = [(input_spacing[i] / output_spacing[i]) for i in reversed(range(len(input_size)))]
    new_array = zoom(image_chunk, zoom_factors, order=3)
    return new_array

if __name__ == ' __main__':
    working_directory = './images'
    input_image_path = os.path.join(working_directory, 'ct.nii.gz')
    
    # Read the image
    itk_image = itk.imread(input_image_path)

    # Calculate chunk size and set up dask computation
    input_spacing = itk.spacing(itk_image)
    input_size = itk.size(itk_image)
    chunk_size = (input_size[2] / 4, input_size[0] / 4, input_size[1] / 4)
    print(chunk_size)
    image_dask = da.from_array(itk_image, chunks=chunk_size)
    result = da.map_blocks(resample_scipy, image_dask, input_spacing)

    # Invoke dask computation and write image
    output_image_path = os.path.join(working_directory, 'dask_scipy_resampled_itk_ct.nii.gz')
    itk.imwrite(itk.image_from_array(result), os.path.join(output_image_path))

    # If the image needs to be further processed, make sure to convert the dask array to numpy array to not invoke the computation again

```
