{
  "id": 593948,
  "title": "Image Order?",
  "url": "/competitions/rsna-intracranial-aneurysm-detection/discussion/593948",
  "author_name": "Vettejeep",
  "post_date": "2025-07-31T15:55:20.045000",
  "votes": 6,
  "comment_count": 8,
  "views": 0,
  "content": "<p>The dcm images seem to be in random order within each dcm folder - not ordered by location in the person's head. For example, I tried sorting by:<br>\ngetattr(dcm_img, 'ImagePositionPatient', 0)[2] and image number.<br>\nBoth seem to give image orders for random positions in the head.<br>\nThis has implications for 3d models and might then require image-by-image 2d analysis.<br>\nIs there a way to sort the images by position in the patient's head?</p>",
  "messages": [
    {
      "id": 3258969,
      "postDate": "2025-07-31T15:55:20.047Z",
      "content": "<p>The dcm images seem to be in random order within each dcm folder - not ordered by location in the person's head. For example, I tried sorting by:<br>\ngetattr(dcm_img, 'ImagePositionPatient', 0)[2] and image number.<br>\nBoth seem to give image orders for random positions in the head.<br>\nThis has implications for 3d models and might then require image-by-image 2d analysis.<br>\nIs there a way to sort the images by position in the patient's head?</p>",
      "rawMarkdown": "The dcm images seem to be in random order within each dcm folder - not ordered by location in the person's head. For example, I tried sorting by:\ngetattr(dcm_img, 'ImagePositionPatient', 0)[2] and image number.\nBoth seem to give image orders for random positions in the head.\nThis has implications for 3d models and might then require image-by-image 2d analysis.\nIs there a way to sort the images by position in the patient's head?",
      "votes": 6
    },
    {
      "id": 3258998,
      "postDate": "2025-07-31T16:58:26.593Z",
      "content": "<p>I think <code>ImagePositionPatient</code> should work well, since its the corresponding metadata for that slice's coordiantes. I use instance number as a backup just in case, but <code>ImagePositionPatient</code> should generally work. This is the function I wrote that worked for me. </p>\n<pre><code> ():\n    \n    dcm_files = [os.path.join(series_path, f)  f  os.listdir(series_path)  f.endswith()]\n    slices = [pydicom.dcmread(f)  f  dcm_files]\n\n    :\n        slices.sort(key= s: (s.ImagePositionPatient[]))\n     (AttributeError, IndexError):\n        slices.sort(key= s: (s.InstanceNumber))\n     slices\n</code></pre>",
      "rawMarkdown": "I think `ImagePositionPatient` should work well, since its the corresponding metadata for that slice's coordiantes. I use instance number as a backup just in case, but `ImagePositionPatient` should generally work. This is the function I wrote that worked for me. \n\n```\ndef load_sorted_dicom_series(series_path):\n    '''\n    Since the dicom series path files are not in spatial order by file name, we have\n    to use the metadata contained within the slices to proper sort them\n    '''\n    dcm_files = [os.path.join(series_path, f) for f in os.listdir(series_path) if f.endswith('.dcm')]\n    slices = [pydicom.dcmread(f) for f in dcm_files]\n\n    try:\n        slices.sort(key=lambda s: float(s.ImagePositionPatient[2]))\n    except (AttributeError, IndexError):\n        slices.sort(key=lambda s: int(s.InstanceNumber))\n    return slices\n```",
      "votes": 3,
      "replies": [
        {
          "id": 3259027,
          "postDate": "2025-07-31T17:57:47.947Z",
          "content": "<p><a href=\"https://www.kaggle.com/umarali1\" target=\"_blank\">@umarali1</a> - that seems to work - thank you. Have you run into any image sets where the \"except\" block executes?</p>",
          "rawMarkdown": "@umarali1 - that seems to work - thank you. Have you run into any image sets where the \"except\" block executes?",
          "replies": [
            {
              "id": 3259032,
              "postDate": "2025-07-31T18:07:54.287Z",
              "content": "<p>Nope, just checked and it seems like the except clause doesn't execute; at least for the particular <code>SeriesInstanceUID</code> I am looking at.</p>",
              "rawMarkdown": "Nope, just checked and it seems like the except clause doesn't execute; at least for the particular `SeriesInstanceUID` I am looking at."
            },
            {
              "id": 3266084,
              "postDate": "2025-08-08T11:28:49.457Z",
              "content": "<p>For me, it's failing in 322 images, for example:</p>\n<pre><code>'/kaggle/input/rsna-intracranial-aneurysm-detection/series/1.2.826.0.1.3.8.498./1.2.826.0.1.3.8.498..dcm'\n</code></pre>",
              "rawMarkdown": "For me, it's failing in 322 images, for example:\n\n```\n'/kaggle/input/rsna-intracranial-aneurysm-detection/series/1.2.826.0.1.3680043.8.498.43904644226033040820903702585969912893/1.2.826.0.1.3680043.8.498.75923386578885354784596598001171781013.dcm'\n```",
              "votes": 1
            }
          ]
        }
      ]
    },
    {
      "id": 3258984,
      "postDate": "2025-07-31T16:28:22.913Z",
      "content": "<p>I found <code>InstanceNumber</code> to be helpful in sorting them. I only checked on a handful but on those it seemed to make sense. </p>",
      "rawMarkdown": "I found `InstanceNumber` to be helpful in sorting them. I only checked on a handful but on those it seemed to make sense. ",
      "votes": 1
    },
    {
      "id": 3265727,
      "postDate": "2025-08-07T22:02:33Z",
      "content": "<p>Yes, you can sort the individual images within a series using <code>InstanceNumber</code> or <code>ImagePositionPatient</code>. This will allow you to create 3d image arrays.</p>\n<p>Alternatives are:<br>\n1) Use alternative software with DICOM series reading functionality (e.g. simpleITK <code>ImageSeriesReader()</code>).<br>\n2) Use DICOM conversion software like <code>dcm2niix</code> (free binaries available with web search) to automatically convert to NIfTI, after which nibabel can be used to load the 3D image array.</p>",
      "rawMarkdown": "Yes, you can sort the individual images within a series using ```InstanceNumber``` or ```ImagePositionPatient```. This will allow you to create 3d image arrays.\n\nAlternatives are:\n1) Use alternative software with DICOM series reading functionality (e.g. simpleITK ```ImageSeriesReader()```).\n2) Use DICOM conversion software like ```dcm2niix``` (free binaries available with web search) to automatically convert to NIfTI, after which nibabel can be used to load the 3D image array."
    },
    {
      "id": 3259014,
      "postDate": "2025-07-31T17:24:54.270Z",
      "content": "<p><a href=\"https://www.kaggle.com/devinanzelmo\" target=\"_blank\">@devinanzelmo</a>, I tried InstanceNumber but at least to my eyes the order seems repetitive in some way with a random length, not sorted purely by brain position. Here is an image from the first few pictures from the first aneurysm sorted by ImageNumber. Thanks for your comment.<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F581084%2F04fe7eda502fa07c4b2aefecea9b2525%2FbrainImageSet.jpg?generation=1753982626094076&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "@devinanzelmo, I tried InstanceNumber but at least to my eyes the order seems repetitive in some way with a random length, not sorted purely by brain position. Here is an image from the first few pictures from the first aneurysm sorted by ImageNumber. Thanks for your comment.\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F581084%2F04fe7eda502fa07c4b2aefecea9b2525%2FbrainImageSet.jpg?generation=1753982626094076&alt=media)",
      "replies": [
        {
          "id": 3265728,
          "postDate": "2025-08-07T22:05:41.043Z",
          "content": "<p>Agree this does not look correctly sorted. Can you try sorting by <code>ImagePositionPatient</code> and/or provide a code snippet to replicate your findings?</p>\n<p>Also, providing a single out of frame orthogonal slice (e.g. mid coronal or mid sagittal) will clearly show any sorting errors. For example using <code>np.transpose()</code> on the 3D array. This can be useful for quick reference.</p>",
          "rawMarkdown": "Agree this does not look correctly sorted. Can you try sorting by ```ImagePositionPatient``` and/or provide a code snippet to replicate your findings?\n\nAlso, providing a single out of frame orthogonal slice (e.g. mid coronal or mid sagittal) will clearly show any sorting errors. For example using ```np.transpose()``` on the 3D array. This can be useful for quick reference."
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 3258998,
      "author_name": "Umar",
      "author_url": "",
      "post_date": "2025-07-31T16:58:26.593000",
      "content": "<p>I think <code>ImagePositionPatient</code> should work well, since its the corresponding metadata for that slice's coordiantes. I use instance number as a backup just in case, but <code>ImagePositionPatient</code> should generally work. This is the function I wrote that worked for me. </p>\n<pre><code> ():\n    \n    dcm_files = [os.path.join(series_path, f)  f  os.listdir(series_path)  f.endswith()]\n    slices = [pydicom.dcmread(f)  f  dcm_files]\n\n    :\n        slices.sort(key= s: (s.ImagePositionPatient[]))\n     (AttributeError, IndexError):\n        slices.sort(key= s: (s.InstanceNumber))\n     slices\n</code></pre>",
      "votes": 3,
      "replies": [
        {
          "id": 3259027,
          "author_name": "Vettejeep",
          "author_url": "",
          "post_date": "2025-07-31T17:57:47.947000",
          "content": "<p><a href=\"https://www.kaggle.com/umarali1\" target=\"_blank\">@umarali1</a> - that seems to work - thank you. Have you run into any image sets where the \"except\" block executes?</p>",
          "votes": 0,
          "replies": [
            {
              "id": 3259032,
              "author_name": "Umar",
              "author_url": "",
              "post_date": "2025-07-31T18:07:54.287000",
              "content": "<p>Nope, just checked and it seems like the except clause doesn't execute; at least for the particular <code>SeriesInstanceUID</code> I am looking at.</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 3266084,
              "author_name": "MT",
              "author_url": "",
              "post_date": "2025-08-08T11:28:49.457000",
              "content": "<p>For me, it's failing in 322 images, for example:</p>\n<pre><code>'/kaggle/input/rsna-intracranial-aneurysm-detection/series/1.2.826.0.1.3.8.498./1.2.826.0.1.3.8.498..dcm'\n</code></pre>",
              "votes": 1,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 3258984,
      "author_name": "Devin Anzelmo",
      "author_url": "",
      "post_date": "2025-07-31T16:28:22.913000",
      "content": "<p>I found <code>InstanceNumber</code> to be helpful in sorting them. I only checked on a handful but on those it seemed to make sense. </p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 3265727,
      "author_name": "Evan Calabrese",
      "author_url": "",
      "post_date": "2025-08-07T22:02:33",
      "content": "<p>Yes, you can sort the individual images within a series using <code>InstanceNumber</code> or <code>ImagePositionPatient</code>. This will allow you to create 3d image arrays.</p>\n<p>Alternatives are:<br>\n1) Use alternative software with DICOM series reading functionality (e.g. simpleITK <code>ImageSeriesReader()</code>).<br>\n2) Use DICOM conversion software like <code>dcm2niix</code> (free binaries available with web search) to automatically convert to NIfTI, after which nibabel can be used to load the 3D image array.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 3259014,
      "author_name": "Vettejeep",
      "author_url": "",
      "post_date": "2025-07-31T17:24:54.270000",
      "content": "<p><a href=\"https://www.kaggle.com/devinanzelmo\" target=\"_blank\">@devinanzelmo</a>, I tried InstanceNumber but at least to my eyes the order seems repetitive in some way with a random length, not sorted purely by brain position. Here is an image from the first few pictures from the first aneurysm sorted by ImageNumber. Thanks for your comment.<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F581084%2F04fe7eda502fa07c4b2aefecea9b2525%2FbrainImageSet.jpg?generation=1753982626094076&amp;alt=media\" alt=\"\"></p>",
      "votes": 0,
      "replies": [
        {
          "id": 3265728,
          "author_name": "Evan Calabrese",
          "author_url": "",
          "post_date": "2025-08-07T22:05:41.043000",
          "content": "<p>Agree this does not look correctly sorted. Can you try sorting by <code>ImagePositionPatient</code> and/or provide a code snippet to replicate your findings?</p>\n<p>Also, providing a single out of frame orthogonal slice (e.g. mid coronal or mid sagittal) will clearly show any sorting errors. For example using <code>np.transpose()</code> on the 3D array. This can be useful for quick reference.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "3258969": "The dcm images seem to be in random order within each dcm folder - not ordered by location in the person's head. For example, I tried sorting by:\ngetattr(dcm_img, 'ImagePositionPatient', 0)[2] and image number.\nBoth seem to give image orders for random positions in the head.\nThis has implications for 3d models and might then require image-by-image 2d analysis.\nIs there a way to sort the images by position in the patient's head?",
    "3258998": "I think `ImagePositionPatient` should work well, since its the corresponding metadata for that slice's coordiantes. I use instance number as a backup just in case, but `ImagePositionPatient` should generally work. This is the function I wrote that worked for me. \n\n```\ndef load_sorted_dicom_series(series_path):\n    '''\n    Since the dicom series path files are not in spatial order by file name, we have\n    to use the metadata contained within the slices to proper sort them\n    '''\n    dcm_files = [os.path.join(series_path, f) for f in os.listdir(series_path) if f.endswith('.dcm')]\n    slices = [pydicom.dcmread(f) for f in dcm_files]\n\n    try:\n        slices.sort(key=lambda s: float(s.ImagePositionPatient[2]))\n    except (AttributeError, IndexError):\n        slices.sort(key=lambda s: int(s.InstanceNumber))\n    return slices\n```",
    "3258984": "I found `InstanceNumber` to be helpful in sorting them. I only checked on a handful but on those it seemed to make sense. ",
    "3265727": "Yes, you can sort the individual images within a series using ```InstanceNumber``` or ```ImagePositionPatient```. This will allow you to create 3d image arrays.\n\nAlternatives are:\n1) Use alternative software with DICOM series reading functionality (e.g. simpleITK ```ImageSeriesReader()```).\n2) Use DICOM conversion software like ```dcm2niix``` (free binaries available with web search) to automatically convert to NIfTI, after which nibabel can be used to load the 3D image array.",
    "3259014": "@devinanzelmo, I tried InstanceNumber but at least to my eyes the order seems repetitive in some way with a random length, not sorted purely by brain position. Here is an image from the first few pictures from the first aneurysm sorted by ImageNumber. Thanks for your comment.\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F581084%2F04fe7eda502fa07c4b2aefecea9b2525%2FbrainImageSet.jpg?generation=1753982626094076&alt=media)"
  }
}