Dicom ipp z slice sort

From wenmin-wu/ds-skills · View interactive version

Dicom ipp z slice sort is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Order DICOM slices into a coherent 3D volume by sorting on ImagePositionPatient[2] (the Z coordinate in patient space), with a filename-integer fallback for series whose tag is missing — never trust filename alphabetical order, never trust InstanceNumber

How to use this skill

Skill file

Overview

DICOM filenames in a CT series are not guaranteed to be in anatomical order. Different scanners, anonymizers, and export tools produce wildly inconsistent name schemes — 1.dcm may be the topmost slice on one study and the bottommost on the next. InstanceNumber is also unreliable: it can be reset, missing, or assigned in scan order rather than spatial order. The single trustworthy ordering is ImagePositionPatient[2], the Z coordinate of the slice in patient space — sort by it ascending and you get a consistent head→feet (or feet→head, fix later) volume regardless of vendor. Keep an integer-filename fallback for the few exports that strip the tag entirely.

Quick Start

import pydicom
from os import listdir

def load_scans(dcm_dir):
    files = listdir(dcm_dir)
    try:
        slices = [pydicom.dcmread(f'{dcm_dir}/{f}') for f in files]
        slices.sort(key=lambda s: float(s.ImagePositionPatient[2]))
    except (AttributeError, KeyError):
        # fallback: integer-filename order when IPP tag is missing
        nums = sorted(int(f.split('.')[0]) for f in files)
        slices = [pydicom.dcmread(f'{dcm_dir}/{n}.dcm') for n in nums]
    return slices

Workflow

  1. List every .dcm file in the series directory
  2. Read each with pydicom.dcmread and key the sort on float(ds.ImagePositionPatient[2])
  3. Wrap in try/except (AttributeError, KeyError) to catch the rare missing-tag series
  4. In the fallback branch, sort by the integer extracted from the filename stem
  5. Stack pixel_arrays in the resulting order and apply RescaleSlope/Intercept to get HU

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.