Skip to content

Add --bbox_config CLI option for template-space bounding box crops - #191

Draft
akhanf with Copilot wants to merge 3 commits into
mainfrom
copilot/add-cli-option-custom-bounding-boxes
Draft

Add --bbox_config CLI option for template-space bounding box crops#191
akhanf with Copilot wants to merge 3 commits into
mainfrom
copilot/add-cli-option-custom-bounding-boxes

Conversation

Copilot AI commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Adds a mechanism to extract cropped NIfTI regions from SPIM zarr data in template space at arbitrary resolution, useful for targeted visualization or downstream analysis without registering the full volume.

Changes

CLI & config (snakebids.yml, Snakefile)

  • New --bbox_config option accepting a YAML file with a list of named bounding boxes
  • Parsed at planning time into bbox_configs / bbox_cfgs_by_name dicts

Rules (patches.smk)

  • extract_bbox_crop — per-bbox/stain rule; output BIDS entities: stain, space (template), res (e.g. 20um), desc (bbox name), level
  • all_bbox_template_crops — standalone target expanding over all bboxes × dataset stains

Script (extract_bbox_in_template_space.py)

Block-wise pull-resampling via zarrnii interp_by_block. Transform chain:

  1. ref ZYX vox → RAS mm (zarrnii ZYX affine)
  2. RAS template → RAS subject (composite inverse displacement field)
  3. RAS mm → flo ZYX vox (inverse SPIM zarr affine)

Only the subregion of the source zarr needed per output block is loaded — memory stays bounded at any input level.

Bbox config format

bounding_boxes:
  - name: entorhinal
    x: 5.2        # RAS mm centre
    y: -2.3
    z: -1.8
    units: mm
    size: [256, 256, 256]   # [nx, ny, nz] voxels along [R, A, S]
    resolution_um: 20
    input_level: 0
  - name: wholebrain
    use_brain_mask: true    # derives extent from template brain mask
    resolution_um: 100
    input_level: 2

The use_brain_mask: true special case computes the bounding box from the template brain mask non-zero extent, enabling whole-brain resampling at arbitrary resolution.

Copilot AI and others added 2 commits August 6, 2026 14:26
Co-authored-by: akhanf <11492701+akhanf@users.noreply.github.com>
…crops

Co-authored-by: akhanf <11492701+akhanf@users.noreply.github.com>
- target rule wasn't connected, added that in now
- script had the wrong transform and used interp_by_blocks directly -
  instead we can use apply_transform. Note: this requires a zarrnii fix
if using imaris datasets (in zarrnii PR #262)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants