# Why is the first axis swapped in ITK versus SimpleITK?

**URL:** https://discourse.itk.org/t/why-is-the-first-axis-swapped-in-itk-versus-simpleitk/3055
**Category:** Community
**Tags:** python, itkv5, simpleitk
**Created:** [May 9, 2020, 4:44pm UTC](https://discourse.itk.org/t/why-is-the-first-axis-swapped-in-itk-versus-simpleitk/3055 "2020-05-09T16:44:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jonasteuwen](https://discourse.itk.org/user_avatar/discourse.itk.org/jonasteuwen/32/1335_2.png) [@jonasteuwen](https://discourse.itk.org/u/jonasteuwen)
#### Post date: [May 9, 2020, 4:44pm UTC](https://discourse.itk.org/t/why-is-the-first-axis-swapped-in-itk-versus-simpleitk/3055/1 "2020-05-09T16:44:38Z")

</div>

I want to read a DICOM series with ITK (5.1rc) and compare it with the SimpleITK version:

```auto
def read_dcm_series_sitk(files):
    file_reader = sitk.ImageFileReader()
    file_reader.SetFileName(str(files[0]))
    file_reader.ReadImageInformation()
    series_ids = [file_reader.GetMetaData('0020|000e')]
    with tempfile.TemporaryDirectory() as tmpdir_name:
        for f in files:
            os.symlink(os.path.abspath(f), os.path.join(tmpdir_name, os.path.basename(f)))
        sorted_filenames = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(tmpdir_name, series_ids[0])
        sitk_image = sitk.ReadImage(sorted_filenames)

    
    return sitk_image

def read_dcm_series_itk(files):
    PixelType = itk.ctype('double') # is int32 available?
    ImageType = itk.Image[PixelType, 3]

    reader = itk.ImageSeriesReader[ImageType].New()
    dicomIO = itk.GDCMImageIO.New()
    reader.SetImageIO(dicomIO)
    reader.SetFileNames(list(files))

    reader.Update()
    itk_image = reader.GetOutput()

    return itk_image

```

I now have a list of filenames which constitute a complete CT volume (after applying slope and intercept which ITK internally does becomes int32)

So I read as follows:

```auto
itk_image = read_dcm_series_itk(fns)
sitk_image = read_dcm_series_sitk(fns)

```

Nevertheless, the first axis is flipped:

```auto
np.all(itk.array_from_image(itk_image)[::-1] == sitk.GetArrayFromImage(sitk_image))

True

```

Is this an oversight of mine, and expected behaviour?

---

<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: [May 11, 2020, 12:52pm UTC](https://discourse.itk.org/t/why-is-the-first-axis-swapped-in-itk-versus-simpleitk/3055/2 "2020-05-11T12:52:47Z")

</div>

Hello,

It looks like the (arbitrarily ordered) file list is directly being used in native ITK python series reader. While in SimpleITK the series ID and the GDCM series sorting functionality are being used.

You can see SimpleITK’s implementation of [GetGDCMSeriesFileNames](https://github.com/SimpleITK/SimpleITK/blob/9252e5cc1c193bf083ee46391231079deb58708d/Code/IO/src/sitkImageSeriesReader.cxx#L45-L64) for details of the code that needs to be added to the `read_dcm_series_itk`.

You can inspect the different between `sorted_filenames` and `files` to see if that accounts for the difference.

---

<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: [May 14, 2020, 2:24am UTC](https://discourse.itk.org/t/why-is-the-first-axis-swapped-in-itk-versus-simpleitk/3055/3 "2020-05-14T02:24:59Z")

</div>

If the files are sorted correctly, a shorter call is:

```auto
itk_image = itk.imread(list(files))

```

---

<div class="post-metadata">

### Author: ![jonasteuwen](https://discourse.itk.org/user_avatar/discourse.itk.org/jonasteuwen/32/1335_2.png) [@jonasteuwen](https://discourse.itk.org/u/jonasteuwen)
#### Post date: [May 15, 2020, 10:25am UTC](https://discourse.itk.org/t/why-is-the-first-axis-swapped-in-itk-versus-simpleitk/3055/4 "2020-05-15T10:25:08Z")

</div>

Thanks for the replies! I presort the images based on the dicom headers (InstanceNumber) as I understood how that works, but you are right, @blowekamp that is an easy check. Will write down my solution here when I figured it out.

By the way, the reason that I am looking into ITK versus SimpleITK is the new xarray ability in ITK and the back-and-forth passing to ITK images and the ability to store metadata in the xarray (for instance DCE-MRI you can store a lot of the acquisition parameters in the xarray and set chunking when saving). For most of my applications SimpleITK is by far enough but this xarray is quite useful!
