AgentSkillsCN

fastmonai-upstream-guide

在fastMONAI的实施过程中,可查阅MONAI、TorchIO及nnU-Net的官方文档与源码,获取详尽的实现指导。当您计划引入新的变换操作、损失函数、评估指标、预处理流水线,或构建基于补丁的工作流时,可主动调用此技能,帮助确保fastMONAI的实现方案与上游库的通用模式及医学影像领域的最佳实践保持一致。

SKILL.md
--- frontmatter
name: fastmonai-upstream-guide
description: Consult MONAI, TorchIO, and nnU-Net documentation and source code for implementation guidance in fastMONAI. Use this skill proactively when implementing new transforms, loss functions, metrics, preprocessing pipelines, or patch-based workflows. Helps ensure fastMONAI implementations align with upstream library patterns and medical imaging best practices.

fastMONAI Upstream Implementation Guide

This skill provides implementation guidance by consulting MONAI, TorchIO, and nnU-Net patterns via live documentation and source code fetching.

When to Use This Skill

Use this skill proactively when:

  • Implementing new transforms or augmentations
  • Adding loss functions or metrics
  • Working on patch-based training/inference workflows
  • Establishing preprocessing or postprocessing pipelines
  • Ensuring consistency with upstream library conventions

Library-to-Module Mapping

fastMONAI ModulePrimary UpstreamWhat to Check
vision_augmentationTorchIOTransform classes, wrapper patterns
vision_metricsMONAI + nnU-NetMetric functions, accumulated patterns
vision_lossMONAILoss class signatures, parameters
vision_patchTorchIOQueue, Sampler, GridAggregator
vision_coreTorchIOScalarImage, LabelMap, resampling

Documentation URLs

TorchIO

MONAI

nnU-Net

Source Code URLs (for implementation details)

TorchIO GitHub

MONAI GitHub

nnU-Net GitHub

Workflow Steps

  1. Identify the implementation area

    • Transform/augmentation → TorchIO
    • Metric → MONAI (compute functions) + nnU-Net (accumulated patterns)
    • Loss function → MONAI
    • Patch workflow → TorchIO (Queue, GridSampler)
    • Preprocessing → nnU-Net conventions
  2. Fetch relevant documentation Use WebFetch to retrieve the appropriate documentation page from the URLs above.

  3. Fetch source code if needed For implementation details, fetch the raw GitHub file to see:

    • Function signatures and parameters
    • Default values and conventions
    • Internal implementation logic
  4. Identify the pattern

    • What parameters does the upstream function accept?
    • What tensor shapes does it expect?
    • What are the default behaviors?
  5. Apply to fastMONAI context

    • Make changes in nbs/*.ipynb notebooks (not .py files)
    • Follow existing wrapper patterns
    • Run nbdev_prepare after changes

fastMONAI-Specific Conventions

Transform Wrappers

All transform wrappers must expose .tio_transform property:

python
class MyTransform(DictTransform):
    def __init__(self, ...):
        self._transform = tio.SomeTransform(...)

    @property
    def tio_transform(self):
        return self._transform

Padding Mode

Always use padding_mode=0 (zero padding) for CropOrPad - this aligns with nnU-Net standards:

python
tio.CropOrPad(target_size, padding_mode=0)  # Correct
tio.CropOrPad(target_size)  # Avoid - uses TorchIO default

Tensor Shape Conventions

  • MONAI metrics expect: [B, C, D, H, W]
  • TorchIO uses: [C, D, H, W] for single images
  • fastMONAI MedImage/MedMask: 4D tensors with affine tracking

Accumulated Metrics (nnU-Net pattern)

For patch-based training, use accumulated TP/FP/FN rather than per-batch averaging:

python
class AccumulatedDice(Metric):
    # Accumulates across batches, not averages

nbdev Workflow

  • All code changes in nbs/*.ipynb
  • Run nbdev_prepare to regenerate .py files and run tests
  • Never edit fastMONAI/*.py directly

Example Queries

When implementing, fetch relevant docs/code:

"I need to implement a new spatial transform" → Fetch TorchIO spatial transform docs and source

"What's the signature for MONAI's compute_dice?" → Fetch MONAI metrics documentation

"How does nnU-Net handle validation metrics?" → Fetch nnU-Net training code for accumulated patterns

"I want to add a new loss function" → Fetch MONAI losses documentation and source

Quick Reference Commands

To check upstream implementations during development:

code
# TorchIO transform documentation
WebFetch: https://torchio.readthedocs.io/transforms/augmentation.html

# MONAI metric implementation
WebFetch: https://raw.githubusercontent.com/Project-MONAI/MONAI/dev/monai/metrics/meandice.py

# nnU-Net preprocessing patterns
WebFetch: https://github.com/MIC-DKFZ/nnUNet/blob/master/documentation/how_to_use_nnunet.md