Quantile slice sampling

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

Quantile slice sampling is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Samples a fixed number of slices from variable-length CT/MRI stacks using quantile indexing to produce consistent input depth.

How to use this skill

Skill file

Quantile Slice Sampling

Overview

3D medical imaging models require fixed-depth input, but CT/MRI scans vary widely in slice count (50-500+). Quantile slice sampling selects N evenly-spaced slices by computing quantile indices over the full stack. This preserves anatomical coverage from top to bottom regardless of scan length, unlike truncation (loses anatomy) or padding (wastes compute).

Quick Start

import numpy as np
from glob import glob

def sample_slices(scan_dir, n_slices=64):
    paths = sorted(glob(f"{scan_dir}/*"),
                   key=lambda x: int(x.split('/')[-1].split('.')[0]))
    n_scans = len(paths)
    if n_scans <= n_slices:
        indices = list(range(n_scans))
    else:
        indices = np.quantile(
            list(range(n_scans)),
            np.linspace(0., 1., n_slices)
        ).round().astype(int)
    return [paths[i] for i in indices]

# Sample 64 slices from a scan with 300+ slices
selected = sample_slices("/data/train_images/patient_001", n_slices=64)

Workflow

  1. List and sort all slice files in the scan directory
  2. Compute N quantile positions uniformly from 0 to 1
  3. Map quantile positions to integer indices in the slice array
  4. Load only the selected slices and stack into a 3D volume

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.