However, if I load it to 3D Slicer, the order is correct. I save it to nrrd format in 3D Slicer, and read it using SimpleITK, it’s correct too. Not sure why 3D Slicer read it in the correct order by SimpleITK not. The DICOm reader approach in 3D Slicer is “GDCM with DCMTK fallback”.
The code you shared looks fine. It assumes that you want to read the first series found in the “patient01” directory, if there are more it will read the first one encountered.
Without the input we cannot reproduce the problem, so can’t really help. Please share the input. If you can’t, due to patient privacy or other reasons, please create an artificial input which causes the same problem and share that.
That would be Image Position Patient [0020, 0032]. In case that is not enough (e.g. all slices have the same information in this tag), then some other tags can be used as a heuristic.
Something is wrong with your data, each slice is repeated, just with a different file name. This is exact repetition (subtracting repeated images which only differ by file name yields an image with all zeros). You’ll need to clean this dataset before using it.
I suspect that slicer is either cleaning things up after querying the data or possibly hiding the issue. Code below will highlight the problem (same values for IPP for different slices).
import SimpleITK as sitk
image_file_reader = sitk.ImageFileReader()
file_list = sitk.ImageSeriesReader_GetGDCMSeriesFileNames("patient01")
for file_name in file_list:
image_file_reader.SetFileName(file_name)
image_file_reader.ReadImageInformation()
print(f'{file_name}: {image_file_reader.GetMetaData("0020|0032")}') #image position patient
If you duplicated every slice then it means the slice spacing alternates between 0 and the real spacing value. Slicer can load this series because it can correctly load images with varying spacing. Still, you should remove the duplicate slices to keep things clean and efficient.
8: Unnamed Series [Scalar Volume]: Images are not equally spaced (a difference of 0.999803 vs 0 in spacings was detected). If loaded image appears distorted, enable ‘Acquisition geometry regularization’ in Application settings / DICOM / DICOMScalarVolumePlugin. Please use caution.