data-model
Construct and validate TorchIO images, subjects/studies, affines, points, bounding boxes, lazy/eager sources, and safe save/load workflows.
- License 许可
- Apache-2.0
- 最近更新
- 2026-10-08
- Stars
- ★ 11
- Forks
- 3
01 前言 Front Matter
- name
- data-model
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Construct and validate TorchIO images, subjects/studies, affines, points, bounding boxes, lazy/eager sources, and safe save/load workflows.
- disable-model-invocation
- true
02 资源文件 3
- 参考文档references/api-reference.md8.6 KB
- 参考文档references/io-and-formats.md6.4 KB
- 参考文档references/troubleshooting.md6.6 KB
03 SKILL.md 正文
TorchIO Data Model
Use this sub-skill when an agent needs to create, inspect, validate, or persist TorchIO data containers before applying transforms, sampling patches, or using CLI workflows.
Route by task
- Build in-memory images, choose
ScalarImagevsLabelMap, inspect shape/spacing/orientation, or handle tensor layout errors: read API reference. - Combine images, annotations, and metadata into
SubjectorStudy: read API reference. - Add landmarks or 3D boxes with voxel/anatomical axes: read API reference.
- Choose tensor, NumPy, path, NiBabel, SimpleITK, bytes, file-like, remote, or zarr sources: read I/O and formats.
- Understand lazy loading, backend behavior, saving, and round-trip checks: read I/O and formats.
- Recover from validation errors or API drift such as
source=tensor, 3D tensors, channel-last arrays, bad affines, and annotation format confusion: read Troubleshooting. - Run a synthetic data sanity check without repository files:
python scripts/data_model_smoke.py.
Core patterns
Construct valid images
import torch
import torchio as tio
image = tio.ScalarImage(torch.randn(1, 32, 40, 24))
label = tio.LabelMap(torch.randint(0, 4, (1, 32, 40, 24), dtype=torch.int16))
TorchIO tensor images are channel-first 4D tensors with shape (C, I, J, K). A single 3D volume still needs a leading channel dimension, usually tensor.unsqueeze(0). In-memory tensor and NumPy images are eager and immediately loaded; path and supported lazy backend sources are loaded on demand.
Preserve physical metadata
affine = tio.AffineMatrix.from_spacing((0.8, 0.8, 2.5), origin=(10, 20, 30))
image = tio.ScalarImage(torch.randn(1, 32, 40, 24), affine=affine, sequence="T1")
assert image.spacing == (0.8, 0.8, 2.5)
assert image.orientation == ("R", "A", "S")
assert image.metadata["sequence"] == "T1"
Use a 4x4 voxel-to-world affine for spacing, origin, direction, and orientation. Arbitrary keyword metadata is available through image.metadata, attribute access, or dict-style lookup.
Group data in a Subject or Study
subject = tio.Subject(
t1=image,
seg=label,
age=62,
)
study = tio.Study(t1=image, seg=label) # alias for Subject
A Subject stores named images, Points, BoundingBoxes, and metadata. Spatial properties such as subject.spatial_shape and subject.spacing check consistency across all images and raise if they disagree.
Add annotations
points = tio.Points(torch.tensor([[5.0, 6.0, 7.0]]), affine=image.affine)
boxes = tio.BoundingBoxes(
torch.tensor([[2, 3, 4, 10, 12, 16]], dtype=torch.float32),
format=tio.BoundingBoxFormat.IJKIJK,
labels=torch.tensor([1]),
affine=image.affine,
)
subject = tio.Subject(t1=image, landmarks=points, lesions=boxes)
Points require (N, 3) coordinates; bounding boxes require (N, 6) coordinates plus a BoundingBoxFormat. Attach annotations either at subject level or image level using the points= and bounding_boxes= image constructor keywords.
Drift guardrails
- For in-memory tensors, prefer
tio.ScalarImage(tensor)andtio.LabelMap(tensor). Do not write old-stylesource=tensorin generated examples unless matching an existing compatibility path intentionally. - Use
ScalarImagefor continuous intensity data andLabelMapfor segmentation labels. Transform logic treats labels semantically, especially for interpolation. - Keep transform pipeline design in the
transformssub-skill, patch queues/samplers inpatch-workflows, and CLI conversion/cache workflows incli-and-io.
Quick validation
Run the bundled smoke test after editing data-model examples:
python scripts/data_model_smoke.py
The script uses only synthetic tensors, temporary files, and public TorchIO imports. It checks construction, metadata, affine spacing/orientation, annotations, Subject/Study, and save/load round trips.
04 同仓库更多 Skills
obsidian-clipper-template-creator
Guide for creating templates for the Obsidian Web Clipper. Use when you want to create a new clipping template, understand available variables, or format clipped content.
obsidian-clipper-template-creator
Guide for creating templates for the Obsidian Web Clipper. Use when you want to create a new clipping template, understand available variables, or format clipped content.
python-api
Programmatic Object-Detection-Metrics API guidance for BoundingBox, BoundingBoxes, Evaluator, enums, coordinate conversion, AP/mAP semantics, and source-style import diagnostics.
file-evaluation
Folder-based PASCAL VOC AP/mAP evaluation from ground-truth and detection text files.
object-detection-metrics
Guides legacy Object-Detection-Metrics PASCAL VOC AP/mAP evaluation from detection text folders and direct Python API classes.
nx-workspace-patterns
Configure and optimize Nx monorepo workspaces. Use when setting up Nx, configuring project boundaries, optimizing build caching, or implementing affected commands.